Changeset 9267e43ddfd7b4b47ee778995e83ee06f228731a

Show
Ignore:
Timestamp:
09/07/11 19:14:08 (3 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1315422848 -0400
git-parent:

[9799031c4d6cbb100f1ccbc5c773a07e3d988542]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1315422848 -0400
Message:

refactor this into something reusable

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/Makefile.in

    r191ce1d r9267e43  
    4949        noit_config.h noit_console.h noit_console_telnet.h noit_defines.h \ 
    5050        noit_filters.h noit_http.h noit_jlog_listener.h noit_listener.h \ 
    51         noit_livestream_listener.h noit_module.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 \ 
    5353        stratcon_datastore.h stratcon_iep.h stratcon_jlog_streamer.h \ 
    5454        stratcon_realtime_http.h \ 
     
    6868        jlog/jlog.h jlog/jlog_config.h jlog/jlog_hash.h jlog/jlog_io.h 
    6969 
    70 NOIT_OBJS=noitd.o noit_listener.o \ 
     70NOIT_OBJS=noitd.o noit_main.o noit_listener.o \ 
    7171        noit_console.o noit_console_state.o noit_console_telnet.o \ 
    7272        noit_console_complete.o noit_check_resolver.o \ 
     
    8383        jlog/libjlog.a utils/libnoit_utils.a json-lib/libnoitjson.a 
    8484 
    85 STRATCON_OBJS=stratcond.o noit_listener.o \ 
     85STRATCON_OBJS=stratcond.o noit_main.o noit_listener.o \ 
    8686        noit_console.o noit_console_state.o noit_console_telnet.o \ 
    8787        noit_console_complete.o noit_xml.o \ 
  • src/noitd.c

    r634b4a3 r9267e43  
    4444#endif 
    4545 
     46#include "noit_main.h" 
    4647#include "eventer/eventer.h" 
    4748#include "utils/noit_log.h" 
     
    9192} 
    9293 
    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 } 
    11394void parse_clargs(int argc, char **argv) { 
    11495  int c; 
     
    128109        break; 
    129110      case 'l': 
    130         enable_logs[enable_logs_cnt++] = strdup(optarg); 
     111        noit_main_enable_log(optarg); 
    131112        break; 
    132113      case 'L': 
    133         disable_logs[disable_logs_cnt++] = strdup(optarg); 
     114        noit_main_disable_log(optarg); 
    134115        break; 
    135116      case 'u': 
     
    155136    } 
    156137  } 
    157 } 
    158  
    159 static 
    160 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; 
    177138} 
    178139 
     
    267228 
    268229int 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  
    275230  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  
    4747#include "utils/noit_watchdog.h" 
    4848#include "utils/noit_lockfile.h" 
     49#include "noit_main.h" 
    4950#include "noit_listener.h" 
    5051#include "noit_console.h" 
     
    6667static int debug = 0; 
    6768static 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; 
    7269static char *glider = NULL; 
    7370 
     
    8582} 
    8683 
    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 } 
    10784void parse_clargs(int argc, char **argv) { 
    10885  int c; 
    109   enable_logs = calloc(argc, sizeof(*enable_logs)); 
    110   disable_logs = calloc(argc, sizeof(*disable_logs)); 
    11186  while((c = getopt(argc, argv, "Mrshc:dDu:g:t:l:L:G:")) != EOF) { 
    11287    switch(c) { 
     
    11893        break; 
    11994      case 'l': 
    120         enable_logs[enable_logs_cnt++] = strdup(optarg); 
     95        noit_main_enable_log(optarg); 
    12196        break; 
    12297      case 'L': 
    123         disable_logs[disable_logs_cnt++] = strdup(optarg); 
     98        noit_main_disable_log(optarg); 
    12499        break; 
    125100      case 'r': 
     
    257232 
    258233int 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; 
    263234  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