Changeset 9267e43ddfd7b4b47ee778995e83ee06f228731a
- Timestamp:
- 09/07/11 19:14:08 (2 years ago)
- git-parent:
- Files:
-
- src/Makefile.in (modified) (3 diffs)
- src/noit_main.c (added)
- src/noit_main.h (added)
- src/noitd.c (modified) (5 diffs)
- src/stratcond.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/Makefile.in
r191ce1d r9267e43 49 49 noit_config.h noit_console.h noit_console_telnet.h noit_defines.h \ 50 50 noit_filters.h noit_http.h noit_jlog_listener.h noit_listener.h \ 51 noit_livestream_listener.h noit_m odule.h noit_rest.h noit_tokenizer.h \52 noit_ xml.h \51 noit_livestream_listener.h noit_main.h noit_module.h noit_rest.h \ 52 noit_tokenizer.h noit_xml.h \ 53 53 stratcon_datastore.h stratcon_iep.h stratcon_jlog_streamer.h \ 54 54 stratcon_realtime_http.h \ … … 68 68 jlog/jlog.h jlog/jlog_config.h jlog/jlog_hash.h jlog/jlog_io.h 69 69 70 NOIT_OBJS=noitd.o noit_ listener.o \70 NOIT_OBJS=noitd.o noit_main.o noit_listener.o \ 71 71 noit_console.o noit_console_state.o noit_console_telnet.o \ 72 72 noit_console_complete.o noit_check_resolver.o \ … … 83 83 jlog/libjlog.a utils/libnoit_utils.a json-lib/libnoitjson.a 84 84 85 STRATCON_OBJS=stratcond.o noit_ listener.o \85 STRATCON_OBJS=stratcond.o noit_main.o noit_listener.o \ 86 86 noit_console.o noit_console_state.o noit_console_telnet.o \ 87 87 noit_console_complete.o noit_xml.o \ src/noitd.c
r634b4a3 r9267e43 44 44 #endif 45 45 46 #include "noit_main.h" 46 47 #include "eventer/eventer.h" 47 48 #include "utils/noit_log.h" … … 91 92 } 92 93 93 void cli_log_switches() {94 int i;95 noit_log_stream_t ls;96 for(i=0; i<enable_logs_cnt; i++) {97 ls = noit_log_stream_find(enable_logs[i]);98 if(!ls) noitL(noit_error, "No such log: '%s'\n", enable_logs[i]);99 if(ls && !ls->enabled) {100 noitL(noit_error, "Enabling %s\n", enable_logs[i]);101 ls->enabled = 1;102 }103 }104 for(i=0; i<disable_logs_cnt; i++) {105 ls = noit_log_stream_find(disable_logs[i]);106 if(!ls) noitL(noit_error, "No such log: '%s'\n", enable_logs[i]);107 if(ls && ls->enabled) {108 noitL(noit_error, "Disabling %s\n", disable_logs[i]);109 ls->enabled = 0;110 }111 }112 }113 94 void parse_clargs(int argc, char **argv) { 114 95 int c; … … 128 109 break; 129 110 case 'l': 130 enable_logs[enable_logs_cnt++] = strdup(optarg);111 noit_main_enable_log(optarg); 131 112 break; 132 113 case 'L': 133 disable_logs[disable_logs_cnt++] = strdup(optarg);114 noit_main_disable_log(optarg); 134 115 break; 135 116 case 'u': … … 155 136 } 156 137 } 157 }158 159 static160 int configure_eventer() {161 int rv = 0;162 noit_hash_table *table;163 table = noit_conf_get_hash(NULL, "/" APPNAME "/eventer/config");164 if(table) {165 noit_hash_iter iter = NOIT_HASH_ITER_ZERO;166 const char *key, *value;167 int klen;168 while(noit_hash_next_str(table, &iter, &key, &klen, &value)) {169 int subrv;170 if((subrv = eventer_propset(key, value)) != 0)171 rv = subrv;172 }173 noit_hash_destroy(table, free, free);174 free(table);175 }176 return rv;177 138 } 178 139 … … 267 228 268 229 int main(int argc, char **argv) { 269 int fd, lockfd;270 char conf_str[1024];271 char lockfile[PATH_MAX];272 char user[32], group[32];273 char *trace_dir = NULL;274 275 230 parse_clargs(argc, argv); 276 277 /* First initialize logging, so we can log errors */ 278 noit_log_init(); 279 noit_log_stream_add_stream(noit_debug, noit_stderr); 280 noit_log_stream_add_stream(noit_error, noit_stderr); 281 282 /* Next load the configs */ 283 noit_conf_init(APPNAME); 284 if(noit_conf_load(config_file) == -1) { 285 fprintf(stderr, "Cannot load config: '%s'\n", config_file); 286 exit(-1); 287 } 288 289 /* Reinitialize the logging system now that we have a config */ 290 snprintf(user, sizeof(user), "%d", getuid()); 291 snprintf(group, sizeof(group), "%d", getgid()); 292 if(noit_security_usergroup(droptouser, droptogroup, noit_true)) { 293 noitL(noit_stderr, "Failed to drop privileges, exiting.\n"); 294 exit(-1); 295 } 296 noit_conf_log_init(APPNAME); 297 cli_log_switches(); 298 if(noit_security_usergroup(user, group, noit_true)) { 299 noitL(noit_stderr, "Failed to regain privileges, exiting.\n"); 300 exit(-1); 301 } 302 if(debug) 303 noit_debug->enabled = 1; 304 305 if(!glider) noit_conf_get_string(NULL, "/" APPNAME "/watchdog/@glider", &glider); 306 noit_watchdog_glider(glider); 307 noit_conf_get_string(NULL, "/" APPNAME "/watchdog/@tracedir", &trace_dir); 308 if(trace_dir) noit_watchdog_glider_trace_dir(trace_dir); 309 310 /* Lastly, run through all other system inits */ 311 if(!noit_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation", 312 conf_str, sizeof(conf_str))) { 313 noitL(noit_stderr, "Cannot find '%s' in configuration\n", 314 "/" APPNAME "/eventer/@implementation"); 315 exit(-1); 316 } 317 if(eventer_choose(conf_str) == -1) { 318 noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str); 319 exit(-1); 320 } 321 if(configure_eventer() != 0) { 322 noitL(noit_stderr, "Cannot configure eventer\n"); 323 exit(-1); 324 } 325 326 noit_watchdog_prefork_init(); 327 328 if(chdir("/") != 0) { 329 noitL(noit_stderr, "Failed chdir(\"/\"): %s\n", strerror(errno)); 330 exit(-1); 331 } 332 333 /* Acquire the lock so that we can throw an error if it doesn't work. 334 * If we've started -D, we'll have the lock. 335 * If not we will daemon and must reacquire the lock. 336 */ 337 lockfd = -1; 338 lockfile[0] = '\0'; 339 if(noit_conf_get_stringbuf(NULL, "/" APPNAME "/@lockfile", 340 lockfile, sizeof(lockfile))) { 341 if((lockfd = noit_lockfile_acquire(lockfile)) < 0) { 342 noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile); 343 exit(-1); 344 } 345 } 346 347 if(foreground) return child_main(); 348 349 /* This isn't inherited across forks... */ 350 if(lockfd >= 0) noit_lockfile_release(lockfd); 351 352 fd = open("/dev/null", O_RDWR); 353 dup2(fd, STDIN_FILENO); 354 dup2(fd, STDOUT_FILENO); 355 dup2(fd, STDERR_FILENO); 356 if(fork()) exit(0); 357 setsid(); 358 if(fork()) exit(0); 359 360 /* Reacquire the lock */ 361 if(*lockfile) { 362 if(noit_lockfile_acquire(lockfile) < 0) { 363 noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile); 364 exit(-1); 365 } 366 } 367 368 signal(SIGHUP, SIG_IGN); 369 return noit_watchdog_start_child("noitd", child_main, 0); 370 } 231 return noit_main(APPNAME, config_file, debug, foreground, 232 glider, droptouser, droptogroup, child_main); 233 } src/stratcond.c
r379bcaf r9267e43 47 47 #include "utils/noit_watchdog.h" 48 48 #include "utils/noit_lockfile.h" 49 #include "noit_main.h" 49 50 #include "noit_listener.h" 50 51 #include "noit_console.h" … … 66 67 static int debug = 0; 67 68 static int strict_module_load = 0; 68 static char **enable_logs;69 static int enable_logs_cnt = 0;70 static char **disable_logs;71 static int disable_logs_cnt = 0;72 69 static char *glider = NULL; 73 70 … … 85 82 } 86 83 87 void cli_log_switches() {88 int i;89 noit_log_stream_t ls;90 for(i=0; i<enable_logs_cnt; i++) {91 ls = noit_log_stream_find(enable_logs[i]);92 if(!ls) noitL(noit_error, "No such log: '%s'\n", enable_logs[i]);93 if(ls && !ls->enabled) {94 noitL(noit_error, "Enabling %s\n", enable_logs[i]);95 ls->enabled = 1;96 }97 }98 for(i=0; i<disable_logs_cnt; i++) {99 ls = noit_log_stream_find(disable_logs[i]);100 if(!ls) noitL(noit_error, "No such log: '%s'\n", enable_logs[i]);101 if(ls && ls->enabled) {102 noitL(noit_error, "Disabling %s\n", disable_logs[i]);103 ls->enabled = 0;104 }105 }106 }107 84 void parse_clargs(int argc, char **argv) { 108 85 int c; 109 enable_logs = calloc(argc, sizeof(*enable_logs));110 disable_logs = calloc(argc, sizeof(*disable_logs));111 86 while((c = getopt(argc, argv, "Mrshc:dDu:g:t:l:L:G:")) != EOF) { 112 87 switch(c) { … … 118 93 break; 119 94 case 'l': 120 enable_logs[enable_logs_cnt++] = strdup(optarg);95 noit_main_enable_log(optarg); 121 96 break; 122 97 case 'L': 123 disable_logs[disable_logs_cnt++] = strdup(optarg);98 noit_main_disable_log(optarg); 124 99 break; 125 100 case 'r': … … 257 232 258 233 int main(int argc, char **argv) { 259 int fd, lockfd = -1;260 char lockfile[PATH_MAX];261 char user[32], group[32];262 char *trace_dir = NULL;263 234 parse_clargs(argc, argv); 264 265 noit_log_init(); 266 noit_log_stream_add_stream(noit_debug, noit_stderr); 267 noit_log_stream_add_stream(noit_error, noit_stderr); 268 269 /* Next load the configs */ 270 noit_conf_init(APPNAME); 271 if(noit_conf_load(config_file) == -1) { 272 fprintf(stderr, "Cannot load config: '%s'\n", config_file); 273 exit(-1); 274 } 275 276 /* Reinitialize the logging system now that we have a config */ 277 snprintf(user, sizeof(user), "%d", getuid()); 278 snprintf(group, sizeof(group), "%d", getgid()); 279 if(noit_security_usergroup(droptouser, droptogroup, noit_true)) { 280 noitL(noit_stderr, "Failed to drop privileges, exiting.\n"); 281 exit(-1); 282 } 283 noit_conf_log_init(APPNAME); 284 cli_log_switches(); 285 if(noit_security_usergroup(user, group, noit_true)) { 286 noitL(noit_stderr, "Failed to regain privileges, exiting.\n"); 287 exit(-1); 288 } 289 if(debug) 290 noit_debug->enabled = 1; 291 292 if(!glider) noit_conf_get_string(NULL, "/" APPNAME "/watchdog/@glider", &glider); 293 noit_watchdog_glider(glider); 294 noit_conf_get_string(NULL, "/" APPNAME "/watchdog/@tracedir", &trace_dir); 295 if(trace_dir) noit_watchdog_glider_trace_dir(trace_dir); 296 297 if(chdir("/") != 0) { 298 fprintf(stderr, "cannot chdir(\"/\"): %s\n", strerror(errno)); 299 exit(2); 300 } 301 302 noit_watchdog_prefork_init(); 303 304 /* Acquire the lock so that we can throw an error if it doesn't work. 305 * If we've started -D, we'll have the lock. 306 * If not we will daemon and must reacquire the lock. 307 */ 308 lockfd = -1; 309 lockfile[0] = '\0'; 310 if(noit_conf_get_stringbuf(NULL, "/" APPNAME "/@lockfile", 311 lockfile, sizeof(lockfile))) { 312 if((lockfd = noit_lockfile_acquire(lockfile)) < 0) { 313 noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile); 314 exit(-1); 315 } 316 } 317 318 if(foreground) exit(child_main()); 319 320 /* This isn't inherited across forks... */ 321 if(lockfd >= 0) noit_lockfile_release(lockfd); 322 323 fd = open("/dev/null", O_RDWR); 324 dup2(fd, STDIN_FILENO); 325 dup2(fd, STDOUT_FILENO); 326 dup2(fd, STDERR_FILENO); 327 if(fork()) exit(0); 328 setsid(); 329 if(fork()) exit(0); 330 331 /* Reacquire the lock */ 332 if(*lockfile) { 333 if(noit_lockfile_acquire(lockfile) < 0) { 334 noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile); 335 exit(-1); 336 } 337 } 338 339 return noit_watchdog_start_child("stratcond", child_main, 0); 340 } 235 return noit_main(APPNAME, config_file, debug, foreground, 236 glider, droptouser, droptogroup, child_main); 237 }
