static int noitice_hup(eventer_t e, int mask, void *unused, struct timeval *now) { if(__reload_needed) { noitL(noit_error, "SIGHUP received, performing reload\n"); if(noit_conf_load(config_file) == -1) { noitL(noit_error, "Cannot load config: '%s'\n", config_file); exit(-1); } noit_poller_reload(NULL); __reload_needed = 0; } return 0; }
static int child_main() { eventer_t e; /* Load our config... * to ensure it is current w.r.t. to this child starting */ if(noit_conf_load(config_file) == -1) { noitL(noit_error, "Cannot load config: '%s'\n", config_file); exit(2); } noit_log_reopen_all(); signal(SIGHUP, request_conf_reload); /* initialize the eventer */ if(eventer_init() == -1) { noitL(noit_stderr, "Cannot initialize eventer\n"); exit(-1); } /* rotation init requires, eventer_init() */ noit_conf_log_init_rotate(APPNAME, noit_false); /* Setup our heartbeat */ noit_watchdog_child_eventer_heartbeat(); e = eventer_alloc(); e->mask = EVENTER_RECURRENT; e->callback = noitice_hup; eventer_add_recurrent(e); /* Initialize all of our listeners */ noit_console_init(APPNAME); noit_console_conf_init(); noit_console_conf_checks_init(); noit_capabilities_listener_init(); noit_jlog_listener_init(); noit_http_rest_init(); noit_check_rest_init(); noit_filters_rest_init(); noit_livestream_listener_init(); noit_module_init(); if(strict_module_load && noit_module_load_failures() > 0) { noitL(noit_stderr, "Failed to load some modules and -M given.\n"); exit(2); } /* Drop privileges */ if(chrootpath && noit_security_chroot(chrootpath)) { noitL(noit_stderr, "Failed to chroot(), exiting.\n"); exit(2); } if(noit_security_usergroup(droptouser, droptogroup, noit_false)) { noitL(noit_stderr, "Failed to drop privileges, exiting.\n"); exit(2); } /* Prepare for launch... */ noit_filters_init(); noit_poller_init(); noit_listener_init(APPNAME); /* Write our log out, and setup a watchdog to write it out on change. */ noit_conf_write_log(NULL); noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */ noit_conf_watch_and_journal_watchdog(noit_conf_write_log, NULL); eventer_loop(); return 0; }
static int child_main() { char conf_str[1024]; /* Next (re)load the configs */ if(noit_conf_load(config_file) == -1) { fprintf(stderr, "Cannot load config: '%s'\n", config_file); exit(2); } noit_log_reopen_all(); /* Lastly, run through all other system inits */ if(!noit_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation", conf_str, sizeof(conf_str))) { noitL(noit_stderr, "Cannot find '%s' in configuration\n", "/" APPNAME "/eventer/@implementation"); exit(2); } if(eventer_choose(conf_str) == -1) { noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str); exit(2); } if(configure_eventer() != 0) { noitL(noit_stderr, "Cannot configure eventer\n"); exit(2); } if(eventer_init() == -1) { noitL(noit_stderr, "Cannot init eventer %s\n", conf_str); exit(2); } /* rotation init requires, eventer_init() */ noit_conf_log_init_rotate(APPNAME, noit_false); noit_watchdog_child_eventer_heartbeat(); noit_console_init(APPNAME); noit_console_conf_init(); noit_http_rest_init(); stratcon_realtime_http_init(APPNAME); noit_capabilities_listener_init(); noit_listener_init(APPNAME); noit_module_init(); if(strict_module_load && noit_module_load_failures() > 0) { noitL(noit_stderr, "Failed to load some modules and -M given.\n"); exit(2); } if(stratcon_datastore_get_enabled()) stratcon_datastore_init(); /* Drop privileges */ if(chrootpath && noit_security_chroot(chrootpath)) { noitL(noit_stderr, "Failed to chroot(), exiting.\n"); exit(-1); } if(noit_security_usergroup(droptouser, droptogroup, noit_false)) { noitL(noit_stderr, "Failed to drop privileges, exiting.\n"); exit(-1); } stratcon_jlog_streamer_init(APPNAME); if(stratcon_iep_get_enabled()) stratcon_iep_init(); if(stratcon_datastore_get_enabled()) { /* Write our log out, and setup a watchdog to write it out on change. */ stratcon_datastore_saveconfig(NULL); noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */ } else noit_conf_coalesce_changes(INT_MAX); noit_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL); eventer_loop(); return 0; }
int main(int argc, char **argv) { int fd, lockfd = -1; char lockfile[PATH_MAX]; char user[32], group[32]; char *trace_dir = NULL; parse_clargs(argc, argv); noit_log_init(); noit_log_stream_add_stream(noit_debug, noit_stderr); noit_log_stream_add_stream(noit_error, noit_stderr); /* Next load the configs */ noit_conf_init(APPNAME); if(noit_conf_load(config_file) == -1) { fprintf(stderr, "Cannot load config: '%s'\n", config_file); exit(-1); } /* Reinitialize the logging system now that we have a config */ snprintf(user, sizeof(user), "%d", getuid()); snprintf(group, sizeof(group), "%d", getgid()); if(noit_security_usergroup(droptouser, droptogroup, noit_true)) { noitL(noit_stderr, "Failed to drop privileges, exiting.\n"); exit(-1); } noit_conf_log_init(APPNAME); cli_log_switches(); if(noit_security_usergroup(user, group, noit_true)) { noitL(noit_stderr, "Failed to regain privileges, exiting.\n"); exit(-1); } if(debug) noit_debug->enabled = 1; if(!glider) noit_conf_get_string(NULL, "/" APPNAME "/watchdog/@glider", &glider); noit_watchdog_glider(glider); noit_conf_get_string(NULL, "/" APPNAME "/watchdog/@tracedir", &trace_dir); if(trace_dir) noit_watchdog_glider_trace_dir(trace_dir); if(chdir("/") != 0) { fprintf(stderr, "cannot chdir(\"/\"): %s\n", strerror(errno)); exit(2); } noit_watchdog_prefork_init(); /* Acquire the lock so that we can throw an error if it doesn't work. * If we've started -D, we'll have the lock. * If not we will daemon and must reacquire the lock. */ lockfd = -1; lockfile[0] = '\0'; if(noit_conf_get_stringbuf(NULL, "/" APPNAME "/@lockfile", lockfile, sizeof(lockfile))) { if((lockfd = noit_lockfile_acquire(lockfile)) < 0) { noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile); exit(-1); } } if(foreground) exit(child_main()); /* This isn't inherited across forks... */ if(lockfd >= 0) noit_lockfile_release(lockfd); fd = open("/dev/null", O_RDWR); dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); if(fork()) exit(0); setsid(); if(fork()) exit(0); /* Reacquire the lock */ if(*lockfile) { if(noit_lockfile_acquire(lockfile) < 0) { noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile); exit(-1); } } return noit_watchdog_start_child("stratcond", child_main, 0); }
int noit_main(const char *appname, const char *config_filename, int debug, int foreground, const char *_glider, const char *drop_to_user, const char *drop_to_group, int (*passed_child_main)(void)) { int fd, lockfd, watchdog_timeout = 0; char conf_str[1024]; char lockfile[PATH_MAX]; char user[32], group[32]; char *trace_dir = NULL; char appscratch[1024]; char *glider = (char *)_glider; char *watchdog_timeout_str; /* First initialize logging, so we can log errors */ noit_log_init(); noit_log_stream_add_stream(noit_debug, noit_stderr); noit_log_stream_add_stream(noit_error, noit_stderr); /* Next load the configs */ noit_conf_init(appname); if(noit_conf_load(config_filename) == -1) { fprintf(stderr, "Cannot load config: '%s'\n", config_filename); exit(-1); } /* Reinitialize the logging system now that we have a config */ snprintf(user, sizeof(user), "%d", getuid()); snprintf(group, sizeof(group), "%d", getgid()); if(noit_security_usergroup(drop_to_user, drop_to_group, noit_true)) { noitL(noit_stderr, "Failed to drop privileges, exiting.\n"); exit(-1); } noit_conf_log_init(appname); cli_log_switches(); if(noit_security_usergroup(user, group, noit_true)) { noitL(noit_stderr, "Failed to regain privileges, exiting.\n"); exit(-1); } if(debug) noit_debug->enabled = 1; snprintf(appscratch, sizeof(appscratch), "/%s/watchdog/@glider", appname); if(!glider) noit_conf_get_string(NULL, appscratch, &glider); noit_watchdog_glider(glider); snprintf(appscratch, sizeof(appscratch), "/%s/watchdog/@tracedir", appname); noit_conf_get_string(NULL, appscratch, &trace_dir); if(trace_dir) noit_watchdog_glider_trace_dir(trace_dir); /* Lastly, run through all other system inits */ snprintf(appscratch, sizeof(appscratch), "/%s/eventer/@implementation", appname); if(!noit_conf_get_stringbuf(NULL, appscratch, conf_str, sizeof(conf_str))) { noitL(noit_stderr, "Cannot find '%s' in configuration\n", appscratch); exit(-1); } if(eventer_choose(conf_str) == -1) { noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str); exit(-1); } if(configure_eventer(appname) != 0) { noitL(noit_stderr, "Cannot configure eventer\n"); exit(-1); } noit_watchdog_prefork_init(); if(chdir("/") != 0) { noitL(noit_stderr, "Failed chdir(\"/\"): %s\n", strerror(errno)); exit(-1); } /* Acquire the lock so that we can throw an error if it doesn't work. * If we've started -D, we'll have the lock. * If not we will daemon and must reacquire the lock. */ lockfd = -1; lockfile[0] = '\0'; snprintf(appscratch, sizeof(appscratch), "/%s/@lockfile", appname); if(noit_conf_get_stringbuf(NULL, appscratch, lockfile, sizeof(lockfile))) { if((lockfd = noit_lockfile_acquire(lockfile)) < 0) { noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile); exit(-1); } } if(foreground) return passed_child_main(); watchdog_timeout_str = getenv("WATCHDOG_TIMEOUT"); if(watchdog_timeout_str) { watchdog_timeout = atoi(watchdog_timeout_str); noitL(noit_error, "Setting watchdog timeout to %d\n", watchdog_timeout); } /* This isn't inherited across forks... */ if(lockfd >= 0) noit_lockfile_release(lockfd); fd = open("/dev/null", O_RDWR); dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); if(fork()) exit(0); setsid(); if(fork()) exit(0); /* Reacquire the lock */ if(*lockfile) { if(noit_lockfile_acquire(lockfile) < 0) { noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile); exit(-1); } } signal(SIGHUP, SIG_IGN); return noit_watchdog_start_child("noitd", passed_child_main, watchdog_timeout); }