void _logeater_handle_signal_if_needed(void) { int signo; if ( ! got_signal ) return; signo = got_signal; got_signal = 0; #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) if ( signo == SIGQUIT || signo == SIGUSR1 ) { handle_sigquit(); return; } #endif server_close(); if ( config.logeater_client ) requiem_client_destroy(config.logeater_client, REQUIEM_CLIENT_EXIT_STATUS_FAILURE); requiem_deinit(); #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) if ( signo == SIGHUP ) { requiem_log(REQUIEM_LOG_WARN, "signal %d received, restarting (%s).\n", signo, get_restart_string()); handle_sighup(); } #endif requiem_log(REQUIEM_LOG_WARN, "signal %d received, terminating requiem-logeater.\n", signo); exit(2); }
void _lml_handle_signal_if_needed(void) { int signo; if ( ! got_signal ) return; signo = got_signal; got_signal = 0; #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) if ( signo == SIGQUIT || signo == SIGUSR1 ) { handle_sigquit(); return; } #endif server_close(); if ( config.lml_client ) prelude_client_destroy(config.lml_client, PRELUDE_CLIENT_EXIT_STATUS_FAILURE); prelude_deinit(); #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) if ( signo == SIGHUP ) { prelude_log(PRELUDE_LOG_WARN, "signal %d received, restarting (%s).\n", signo, get_restart_string()); handle_sighup(); } #endif prelude_log(PRELUDE_LOG_WARN, "signal %d received, terminating prelude-lml.\n", signo); exit(2); }
void check_signals(void) { if (mf_sigchld) handle_sigchld(); if (mf_sigalrm) handle_sigalrm(); if (mf_sigint) handle_sigint(); if (mf_sigquit) handle_sigquit(); if (mf_sigterm) handle_sigterm(); if (mf_sigpipe) handle_sigpipe(); if (mf_sigusr1) handle_sigusr1(); if (mf_sigusr2) handle_sigusr2(); if (mf_sighup) handle_sighup(); if (mf_stupid) { syslog(LOG_ERR,"set a signal handler,but forgot to write code to handle it"); mf_stupid = 0; } }
int main(int argc, char *argv[]) { double interval; int fd, rc; reload_pending = 0; sym_names_count = sizeof(sym_names) / sizeof(struct symbol_names); varinfo_size = VARINFO_SIZE; varinfo = calloc(varinfo_size, 1); if (!varinfo) { cpuplugd_error("Out of memory: varinfo\n"); exit(1); } /* * varinfo must start with '\n' for correct string matching * in get_var_rvalue(). */ varinfo[0] = '\n'; /* Parse the command line options */ parse_options(argc, argv); /* flock() lock file to prevent multiple instances of cpuplugd */ fd = open(LOCKFILE, O_CREAT | O_RDONLY, S_IRUSR); if (fd == -1) { cpuplugd_error("Cannot open lock file %s: %s\n", LOCKFILE, strerror(errno)); exit(1); } rc = flock(fd, LOCK_EX | LOCK_NB); if (rc) { cpuplugd_error("flock() failed on lock file %s: %s\nThis might " "indicate that an instance of this daemon is " "already running.\n", LOCKFILE, strerror(errno)); exit(1); } /* Make sure that the daemon is not started multiple times */ check_if_started_twice(); /* Store daemon pid also in foreground mode */ handle_signals(); handle_sighup(); /* Need 1 history level minimum for internal symbols */ history_max = 1; /* * Parse arguments from the configuration file, also calculate * history_max */ parse_configfile(configfile); if (history_max > MAX_HISTORY) cpuplugd_exit("History depth %i exceeded maximum (%i)\n", history_max, MAX_HISTORY); /* Check the settings in the configuration file */ check_config(); if (!foreground) { rc = daemon(1, 0); if (rc < 0) cpuplugd_exit("Detach from terminal failed: %s\n", strerror(errno)); } /* Store daemon pid */ store_pid(); /* Unlock lock file */ flock(fd, LOCK_UN); close(fd); /* Install signal handler for floating point exceptions */ rc = feenableexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID); act.sa_flags = SA_NODEFER; sigemptyset(&act.sa_mask); act.sa_handler = sigfpe_handler; if (sigaction(SIGFPE, &act, NULL) < 0) cpuplugd_exit("sigaction( SIGFPE, ... ) failed - reason %s\n", strerror(errno)); setup_history(); /* Main loop */ while (1) { if (reload_pending) { // check for daemon reload reload_daemon(); reload_pending = 0; } history_prev = history_current; history_current = (history_current + 1) % (history_max + 1); time_read(×tamps[history_current]); proc_read(meminfo + history_current * meminfo_size, "/proc/meminfo", meminfo_size); proc_read(vmstat + history_current * vmstat_size, "/proc/vmstat", vmstat_size); proc_cpu_read(cpustat + history_current * cpustat_size); interval = timestamps[history_current] - timestamps[history_prev]; cpuplugd_debug("config update interval: %ld seconds\n", cfg.update); cpuplugd_debug("real update interval: %f seconds\n", interval); /* Run code that may signal failure via longjmp. */ if (cpu == 1) { if (setjmp(jmpenv) == 0) eval_cpu_rules(); else cpuplugd_error("Floating point exception, " "skipping cpu rule " "evaluation.\n"); } if (memory == 1) { if (setjmp(jmpenv) == 0) eval_mem_rules(interval); else cpuplugd_error("Floating point exception, " "skipping memory rule " "evaluation.\n"); } sleep(cfg.update); } return 0; }