Changeset 5b63423e3ea22c19a8187dfcce162d43e0159643

Show
Ignore:
Timestamp:
05/20/09 14:25:34 (5 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1242829534 +0000
git-parent:

[661f070c138a0744279b82279757660688235692]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1242829534 +0000
Message:

pull out the watchdog stuff into utils and integrate it back into noitd and now into stratcond, refs #131

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/noitd.c

    r781897b r5b63423  
    4949#include "utils/noit_hash.h" 
    5050#include "utils/noit_security.h" 
     51#include "utils/noit_watchdog.h" 
    5152#include "noit_listener.h" 
    5253#include "noit_console.h" 
     
    133134} 
    134135 
    135 /* Watchdog stuff */ 
    136 static int *lifeline = NULL; 
    137 static unsigned long last_tick_time() { 
    138   static struct timeval lastchange = { 0, 0 }; 
    139   static int lastcheck = 0; 
    140   struct timeval now, diff; 
    141  
    142   gettimeofday(&now, NULL); 
    143   if(lastcheck != *lifeline) { 
    144     lastcheck = *lifeline; 
    145     memcpy(&lastchange, &now, sizeof(lastchange)); 
    146   } 
    147   sub_timeval(now, lastchange, &diff); 
    148   return (unsigned long)diff.tv_sec; 
    149 } 
    150 static void it_ticks() { 
    151   (*lifeline)++; 
    152 } 
    153 static void setup_mmap() { 
    154   lifeline = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, 
    155                   MAP_SHARED|MAP_ANON, -1, 0); 
    156   if(lifeline == (void *)-1) { 
    157     noitL(noit_error, "Failed to mmap anon for watchdog\n"); 
    158     exit(-1); 
    159   } 
    160 } 
    161  
    162 static int watch_over_child(int (*func)()) { 
    163   int child_pid; 
    164   while(1) { 
    165     child_pid = fork(); 
    166     if(child_pid == -1) { 
    167       noitL(noit_error, "fork failed: %s\n", strerror(errno)); 
    168       exit(-1); 
    169     } 
    170     if(child_pid == 0) { 
    171       /* This sets up things so we start alive */ 
    172       it_ticks(); 
    173       /* run the program */ 
    174       exit(func()); 
    175     } 
    176     else { 
    177       int sig = -1, exit_val = -1; 
    178       signal(SIGHUP, SIG_IGN); 
    179       while(1) { 
    180         unsigned long ltt; 
    181         int status, rv; 
    182         sleep(1); /* Just check child status every second */ 
    183         rv = waitpid(child_pid, &status, WNOHANG); 
    184         if(rv == 0) { 
    185           /* Nothing */ 
    186         } 
    187         else if (rv == child_pid) { 
    188           /* We died!... we need to relaunch, unless the status was a requested exit (2) */ 
    189           sig = WTERMSIG(status); 
    190           exit_val = WEXITSTATUS(status); 
    191           if(sig == SIGINT || sig == SIGQUIT || 
    192              (sig == 0 && (exit_val == 2 || exit_val < 0))) { 
    193             noitL(noit_error, "noitd shutdown acknowledged.\n"); 
    194             exit(0); 
    195           } 
    196           break; 
    197         } 
    198         else { 
    199           noitL(noit_error, "Unexpected return from waitpid: %d\n", rv); 
    200           exit(-1); 
    201         } 
    202         /* Now check out timeout */ 
    203         if((ltt = last_tick_time()) > CHILD_WATCHDOG_TIMEOUT) { 
    204           noitL(noit_error, 
    205                 "Watchdog timeout (%lu s)... terminating child\n", 
    206                 ltt); 
    207           kill(child_pid, SIGKILL); 
    208         } 
    209       } 
    210       noitL(noit_error, "noitd child died [%d/%d], restarting.\n", exit_val, sig); 
    211     } 
    212   } 
    213 } 
    214  
    215136static int __reload_needed = 0; 
    216137static void request_conf_reload(int sig) { 
     
    219140  } 
    220141} 
    221 static int watchdog_tick(eventer_t e, int mask, void *unused, struct timeval *now) { 
    222   it_ticks(); 
     142static int noitice_hup(eventer_t e, int mask, void *unused, struct timeval *now) { 
    223143  if(__reload_needed) { 
    224144    noitL(noit_error, "SIGHUP received, performing reload\n"); 
     
    249169  } 
    250170 
    251   /* Setup our hearbeat */ 
     171  /* Setup our heartbeat */ 
     172  noit_watchdog_child_eventer_heartbeat(); 
     173 
    252174  e = eventer_alloc(); 
    253175  e->mask = EVENTER_RECURRENT; 
    254   e->callback = watchdog_tick
     176  e->callback = noitice_hup
    255177  eventer_add_recurrent(e); 
    256178 
     
    325247  } 
    326248 
    327   setup_mmap(); 
     249  noit_watchdog_prefork_init(); 
    328250 
    329251  if(chdir("/") != 0) { 
     
    340262  if(fork()) exit(0); 
    341263 
    342   return watch_over_child(child_main); 
    343 
     264  signal(SIGHUP, SIG_IGN); 
     265  return noit_watchdog_start_child("noitd", child_main, 0); 
     266
  • src/stratcond.c

    r88a7178 r5b63423  
    4545#include "utils/noit_hash.h" 
    4646#include "utils/noit_security.h" 
     47#include "utils/noit_watchdog.h" 
    4748#include "noit_listener.h" 
    4849#include "noit_console.h" 
     
    127128} 
    128129 
    129 int main(int argc, char **argv) { 
     130static int child_main() { 
    130131  char conf_str[1024]; 
    131  
    132   parse_clargs(argc, argv); 
    133  
    134   if(chdir("/") != 0) { 
    135     fprintf(stderr, "cannot chdir(\"/\"): %s\n", strerror(errno)); 
    136     exit(2); 
    137   } 
    138   if(!foreground) { 
    139     close(STDIN_FILENO); 
    140     close(STDOUT_FILENO); 
    141     close(STDERR_FILENO); 
    142     if(fork()) exit(0); 
    143     setsid(); 
    144     if(fork()) exit(0); 
    145   } 
    146132 
    147133  /* First initialize logging, so we can log errors */ 
     
    181167    exit(-1); 
    182168  } 
     169 
     170  noit_watchdog_child_eventer_heartbeat(); 
     171 
    183172  noit_console_init(APPNAME); 
    184173  stratcon_realtime_http_init(APPNAME); 
     
    206195  return 0; 
    207196} 
     197 
     198int main(int argc, char **argv) { 
     199  parse_clargs(argc, argv); 
     200 
     201  if(chdir("/") != 0) { 
     202    fprintf(stderr, "cannot chdir(\"/\"): %s\n", strerror(errno)); 
     203    exit(2); 
     204  } 
     205 
     206  noit_watchdog_prefork_init(); 
     207 
     208  if(foreground) exit(child_main()); 
     209 
     210  close(STDIN_FILENO); 
     211  close(STDOUT_FILENO); 
     212  close(STDERR_FILENO); 
     213  if(fork()) exit(0); 
     214  setsid(); 
     215  if(fork()) exit(0); 
     216 
     217  return noit_watchdog_start_child("stratcond", child_main, 0); 
     218} 
  • src/utils/Makefile.in

    r1ba4732 r5b63423  
    1111 
    1212OBJS=noit_hash.o noit_skiplist.o noit_log.o noit_sem.o noit_str.o \ 
    13         noit_b64.o noit_security.o
     13        noit_b64.o noit_security.o noit_watchdog.o
    1414        @ATOMIC_OBJS@ 
    1515