Changeset 5b63423e3ea22c19a8187dfcce162d43e0159643
- Timestamp:
- 05/20/09 14:25:34 (4 years ago)
- git-parent:
- Files:
-
- src/noitd.c (modified) (6 diffs)
- src/stratcond.c (modified) (4 diffs)
- src/utils/Makefile.in (modified) (1 diff)
- src/utils/noit_watchdog.c (added)
- src/utils/noit_watchdog.h (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/noitd.c
r781897b r5b63423 49 49 #include "utils/noit_hash.h" 50 50 #include "utils/noit_security.h" 51 #include "utils/noit_watchdog.h" 51 52 #include "noit_listener.h" 52 53 #include "noit_console.h" … … 133 134 } 134 135 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 215 136 static int __reload_needed = 0; 216 137 static void request_conf_reload(int sig) { … … 219 140 } 220 141 } 221 static int watchdog_tick(eventer_t e, int mask, void *unused, struct timeval *now) { 222 it_ticks(); 142 static int noitice_hup(eventer_t e, int mask, void *unused, struct timeval *now) { 223 143 if(__reload_needed) { 224 144 noitL(noit_error, "SIGHUP received, performing reload\n"); … … 249 169 } 250 170 251 /* Setup our hearbeat */ 171 /* Setup our heartbeat */ 172 noit_watchdog_child_eventer_heartbeat(); 173 252 174 e = eventer_alloc(); 253 175 e->mask = EVENTER_RECURRENT; 254 e->callback = watchdog_tick;176 e->callback = noitice_hup; 255 177 eventer_add_recurrent(e); 256 178 … … 325 247 } 326 248 327 setup_mmap();249 noit_watchdog_prefork_init(); 328 250 329 251 if(chdir("/") != 0) { … … 340 262 if(fork()) exit(0); 341 263 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 45 45 #include "utils/noit_hash.h" 46 46 #include "utils/noit_security.h" 47 #include "utils/noit_watchdog.h" 47 48 #include "noit_listener.h" 48 49 #include "noit_console.h" … … 127 128 } 128 129 129 int main(int argc, char **argv) {130 static int child_main() { 130 131 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 }146 132 147 133 /* First initialize logging, so we can log errors */ … … 181 167 exit(-1); 182 168 } 169 170 noit_watchdog_child_eventer_heartbeat(); 171 183 172 noit_console_init(APPNAME); 184 173 stratcon_realtime_http_init(APPNAME); … … 206 195 return 0; 207 196 } 197 198 int 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 11 11 12 12 OBJS=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 \ 14 14 @ATOMIC_OBJS@ 15 15
