making note of my changes.
[oweals/busybox.git] / init.c
1 /*
2  * Mini init implementation for busybox
3  *
4  *
5  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6  * Adjusted by so many folks, it's impossible to keep track.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <termios.h>
33 #include <paths.h>
34 #include <sys/types.h>
35 #include <sys/fcntl.h>
36 #include <sys/wait.h>
37 #include <string.h>
38 #include <sys/mount.h>
39 #include <sys/reboot.h>
40 #include <sys/kdaemon.h>
41 #include <sys/sysmacros.h>
42 #include <linux/serial.h>       /* for serial_struct */
43 #include <sys/vt.h>             /* for vt_stat */
44 #include <sys/ioctl.h>
45 #include <linux/version.h>
46 #ifdef BB_SYSLOGD
47 #include <sys/syslog.h>
48 #endif
49
50 #if ! defined BB_FEATURE_USE_PROCFS
51 #error Sorry, I depend on the /proc filesystem right now.
52 #endif
53
54 #ifndef KERNEL_VERSION
55 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
56 #endif
57
58
59 #define VT_PRIMARY      "/dev/tty1"       /* Primary virtual console */
60 #define VT_SECONDARY    "/dev/tty2"       /* Virtual console */
61 #define VT_LOG          "/dev/tty3"       /* Virtual console */
62 #define SERIAL_CON0     "/dev/ttyS0"      /* Primary serial console */
63 #define SERIAL_CON1     "/dev/ttyS1"      /* Serial console */
64 #define SHELL           "-sh"             /* Default shell */
65 #define INITTAB         "/etc/inittab"    /* inittab file location */
66 #define INIT_SCRIPT     "/etc/init.d/rcS" /* Default sysinit script. */
67
68 #define LOG             0x1
69 #define CONSOLE         0x2
70
71 /* Allowed init action types */
72 typedef enum {
73     SYSINIT=1,
74     RESPAWN,
75     ASKFIRST,
76     WAIT,
77     ONCE
78 } initActionEnum;
79
80 /* And now a list of the actions we support in the version of init */
81 typedef struct initActionType{
82     const char* name;
83     initActionEnum action;
84 } initActionType;
85
86 static const struct initActionType actions[] = {
87     {"sysinit",     SYSINIT},
88     {"respawn",     RESPAWN},
89     {"askfirst",    ASKFIRST},
90     {"wait",        WAIT},
91     {"once",        ONCE},
92     {0}
93 };
94
95 /* Set up a linked list of initactions, to be read from inittab */
96 typedef struct initActionTag initAction;
97 struct initActionTag {
98     pid_t pid;
99     char process[256];
100     char console[256];
101     initAction *nextPtr;
102     initActionEnum action;
103 };
104 initAction* initActionList = NULL;
105
106
107 static char *console = _PATH_CONSOLE;
108 static char *second_console = VT_SECONDARY;
109 static char *log = VT_LOG;
110 static int kernel_version = 0;
111
112
113 /* try to open up the specified device */
114 int device_open(char *device, int mode)
115 {
116     int m, f, fd = -1;
117
118     m = mode | O_NONBLOCK;
119
120     /* Retry up to 5 times */
121     for (f = 0; f < 5; f++)
122         if ((fd = open(device, m)) >= 0)
123             break;
124     if (fd < 0)
125         return fd;
126     /* Reset original flags. */
127     if (m != mode)
128         fcntl(fd, F_SETFL, mode);
129     return fd;
130 }
131
132 /* print a message to the specified device:
133  * device may be bitwise-or'd from LOG | CONSOLE */
134 void message(int device, char *fmt, ...)
135 {
136     va_list arguments;
137     int fd;
138
139 #ifdef BB_SYSLOGD
140
141     /* Log the message to syslogd */
142     if (device & LOG ) {
143         char msg[1024];
144         va_start(arguments, fmt);
145         vsnprintf(msg, sizeof(msg), fmt, arguments);
146         va_end(arguments);
147         syslog(LOG_DAEMON|LOG_NOTICE, msg);
148     }
149
150 #else
151     static int log_fd=-1;
152
153     /* Take full control of the log tty, and never close it.
154      * It's mine, all mine!  Muhahahaha! */
155     if (log_fd < 0) {
156         if (log == NULL) {
157         /* don't even try to log, because there is no such console */
158         log_fd = -2;
159         /* log to main console instead */
160         device = CONSOLE;
161     }
162     else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
163             log_fd=-1;
164             fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
165             fflush(stderr);
166             return;
167         }
168     }
169     if ( (device & LOG) && (log_fd >= 0) ) {
170         va_start(arguments, fmt);
171         vdprintf(log_fd, fmt, arguments);
172         va_end(arguments);
173     }
174 #endif
175
176     if (device & CONSOLE) {
177         /* Always send console messages to /dev/console so people will see them. */
178         if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
179             va_start(arguments, fmt);
180             vdprintf(fd, fmt, arguments);
181             va_end(arguments);
182             close(fd);
183         } else {
184             fprintf(stderr, "Bummer, can't print: ");
185             va_start(arguments, fmt);
186             vfprintf(stderr, fmt, arguments);
187             fflush(stderr);
188             va_end(arguments);
189         }
190     }
191 }
192
193
194 /* Set terminal settings to reasonable defaults */
195 void set_term( int fd)
196 {
197     struct termios tty;
198     static const char control_characters[] = {
199         '\003', '\034', '\177', '\025', '\004', '\0',
200         '\1', '\0', '\021', '\023', '\032', '\0', '\022',
201         '\017', '\027', '\026', '\0'
202         };
203
204     tcgetattr(fd, &tty);
205
206     /* set control chars */
207     memcpy(tty.c_cc, control_characters, sizeof(control_characters));
208
209     /* use line dicipline 0 */
210     tty.c_line = 0;
211
212     /* Make it be sane */
213     //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
214     //tty.c_cflag |= HUPCL|CLOCAL;
215
216     /* input modes */
217     tty.c_iflag = ICRNL|IXON|IXOFF;
218
219     /* output modes */
220     tty.c_oflag = OPOST|ONLCR;
221
222     /* local modes */
223     tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
224
225     tcsetattr(fd, TCSANOW, &tty);
226 }
227
228 /* How much memory does this machine have? */
229 static int mem_total()
230 {
231     char s[80];
232     char *p = "/proc/meminfo";
233     FILE *f;
234     const char pattern[] = "MemTotal:";
235
236     if ((f = fopen(p, "r")) < 0) {
237         message(LOG, "Error opening %s: %s\n", p, strerror( errno));
238         return -1;
239     }
240     while (NULL != fgets(s, 79, f)) {
241         p = strstr(s, pattern);
242         if (NULL != p) {
243             fclose(f);
244             return (atoi(p + strlen(pattern)));
245         }
246     }
247     return -1;
248 }
249
250 static void console_init()
251 {
252     int fd;
253     int tried_devcons = 0;
254     int tried_vtprimary = 0;
255     struct serial_struct sr;
256     char *s;
257
258     if ((s = getenv("CONSOLE")) != NULL) {
259         console = s;
260     }
261 #if #cpu(sparc)
262     /* sparc kernel supports console=tty[ab] parameter which is also 
263      * passed to init, so catch it here */
264     else if ((s = getenv("console")) != NULL) {
265         /* remap tty[ab] to /dev/ttyS[01] */
266         if (strcmp( s, "ttya" )==0)
267             console = SERIAL_CON0;
268         else if (strcmp( s, "ttyb" )==0)
269             console = SERIAL_CON1;
270     }
271 #endif
272     else {
273         struct vt_stat vt;
274         static char the_console[13];
275
276         console = the_console;
277         /* 2.2 kernels: identify the real console backend and try to use it */
278         if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
279             /* this is a serial console */
280             snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
281         }
282         else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
283             /* this is linux virtual tty */
284             snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
285         } else {
286             console = _PATH_CONSOLE;
287             tried_devcons++;
288         }
289     }
290
291     while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
292         /* Can't open selected console -- try /dev/console */
293         if (!tried_devcons) {
294             tried_devcons++;
295             console = _PATH_CONSOLE;
296             continue;
297         }
298         /* Can't open selected console -- try vt1 */
299         if (!tried_vtprimary) {
300             tried_vtprimary++;
301             console = VT_PRIMARY;
302             continue;
303         }
304         break;
305     }
306     if (fd < 0)
307         /* Perhaps we should panic here? */
308         console = "/dev/null";
309     else {
310         /* check for serial console and disable logging to tty3 & running a
311         * shell to tty2 */
312         if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
313             message(LOG|CONSOLE, "serial console detected.  Disabling 2nd virtual terminal.\r\n", console );
314             log = NULL;
315             second_console = NULL;
316         }
317         close(fd);
318     }
319     message(LOG, "console=%s\n", console );
320 }
321
322 static int waitfor(int pid)
323 {
324     int status, wpid;
325
326     while (1) {
327         wpid = wait(&status);
328         if (wpid > 0 ) {
329             message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
330             break;
331         }
332         if (wpid == pid )
333             break;
334     }
335     return wpid;
336 }
337
338 static pid_t run(char* command, 
339         char *terminal, int get_enter)
340 {
341     int i;
342     pid_t pid;
343     char* tmpCmd;
344     char* cmd[255];
345     static const char press_enter[] =
346         "\nPlease press Enter to activate this console. ";
347
348     if ((pid = fork()) == 0) {
349         pid_t shell_pgid = getpid ();
350
351         /* Clean up */
352         close(0);
353         close(1);
354         close(2);
355         setsid();
356
357         if (device_open(terminal, O_RDWR) < 0) {
358             message(LOG|CONSOLE, "Bummer, can't open %s\r\n", terminal);
359             exit(1);
360         }
361         dup(0);
362         dup(0);
363         /* Grab control of the terminal.  */
364         if (tcsetpgrp (0, getpgrp()) < 0) {
365             message(LOG|CONSOLE, "tcsetpgrp error: %s\r\n", strerror(errno));
366         }
367         set_term(0);
368
369         /* Reset signal handlers set for parent process */
370         signal(SIGUSR1, SIG_DFL);
371         signal(SIGUSR2, SIG_DFL);
372         signal(SIGINT, SIG_DFL);
373         signal(SIGTERM, SIG_DFL);
374
375
376         if (get_enter==TRUE) {
377             /*
378              * Save memory by not exec-ing anything large (like a shell)
379              * before the user wants it. This is critical if swap is not
380              * enabled and the system has low memory. Generally this will
381              * be run on the second virtual console, and the first will
382              * be allowed to start a shell or whatever an init script 
383              * specifies.
384              */
385             char c;
386             message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n", 
387                     command, shell_pgid, terminal );
388             write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
389             read(fileno(stdin), &c, 1);
390         }
391
392         /* Convert command (char*) into cmd (char**, one word per string) */
393         for (tmpCmd=command, i=0; (tmpCmd=strsep(&command, " \t")) != NULL;) {
394             if (*tmpCmd != '\0') {
395                 cmd[i] = tmpCmd;
396                 tmpCmd++;
397                 i++;
398             }
399         }
400         cmd[i] = NULL;
401
402         /* Log the process name and args */
403         message(LOG, "Starting pid %d, console %s: '%s'\r\n", 
404                 shell_pgid, terminal, cmd[0]);
405
406         /* Now run it.  The new program will take over this PID, 
407          * so nothing further in init.c should be run. */
408         execvp(cmd[0], cmd);
409
410         /* We're still here?  Some error happened. */
411         message(LOG|CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
412                 strerror(errno));
413         exit(-1);
414     }
415     return pid;
416 }
417
418 /* Make sure there is enough memory to do something useful. *
419  * Calls swapon if needed so be sure /proc is mounted. */
420 static void check_memory()
421 {
422     struct stat statbuf;
423
424     if (mem_total() > 3500)
425         return;
426
427     if (stat("/etc/fstab", &statbuf) == 0) {
428         /* Try to turn on swap */
429         waitfor(run("/bin/swapon swapon -a", log, FALSE));
430         if (mem_total() < 3500)
431             goto goodnight;
432     } else
433         goto goodnight;
434     return;
435
436 goodnight:
437         message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
438         while (1) sleep(1);
439 }
440
441 #ifndef DEBUG_INIT
442 static void shutdown_system(void)
443 {
444     /* Allow Ctrl-Alt-Del to reboot system. */
445     reboot(RB_ENABLE_CAD);
446     message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
447     sync();
448
449     /* Send signals to every process _except_ pid 1 */
450     message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
451     kill(-1, SIGHUP);
452     sleep(2);
453     sync();
454
455     message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
456     kill(-1, SIGKILL);
457     sleep(1);
458
459     message(CONSOLE, "Disabling swap.\r\n");
460     waitfor(run( "swapoff -a", console, FALSE));
461     message(CONSOLE, "Unmounting filesystems.\r\n");
462     waitfor(run( "umount -a", console, FALSE));
463     sync();
464     if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
465         /* bdflush, kupdate not needed for kernels >2.2.11 */
466         message(CONSOLE, "Flushing buffers.\r\n");
467         bdflush(1, 0);
468         sync();
469     }
470 }
471
472 static void halt_signal(int sig)
473 {
474     shutdown_system();
475     message(CONSOLE,
476             "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
477     sync();
478 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
479     if (sig == SIGUSR2)
480         reboot(RB_POWER_OFF);
481     else
482 #endif
483     reboot(RB_HALT_SYSTEM);
484     exit(0);
485 }
486
487 static void reboot_signal(int sig)
488 {
489     shutdown_system();
490     message(CONSOLE, "Please stand by while rebooting the system.\r\n");
491     sync();
492     reboot(RB_AUTOBOOT);
493     exit(0);
494 }
495
496 #endif
497
498 void new_initAction (const struct initActionType *a, 
499         char* process, char* cons)
500 {
501     initAction* newAction;
502
503     /* If BusyBox detects that a serial console is in use, 
504      * then entries containing non-empty id fields will _not_ be run.
505      */
506     if (second_console != NULL && *cons != '\0')
507         return;
508
509     newAction = calloc ((size_t)(1), sizeof(initAction));
510     if (!newAction) {
511         fprintf(stderr, "Memory allocation failure\n");
512         while (1) sleep(1);
513     }
514     newAction->nextPtr = initActionList;
515     initActionList = newAction;
516     strncpy( newAction->process, process, 255);
517     newAction->action = a->action;
518     if (*cons != '\0')
519         strncpy(newAction->console, cons, 255);
520     else
521         strncpy(newAction->console, console, 255);
522     newAction->pid = 0;
523 }
524
525 void delete_initAction (initAction *action)
526 {
527     initAction *a, *b=NULL;
528     for( a=initActionList ; a; b=a, a=a->nextPtr) {
529         if (a == action && b != NULL) {
530             b->nextPtr=a->nextPtr;
531             free( a);
532             break;
533         }
534     }
535 }
536
537 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
538  * then parse_inittab() simply adds in some default
539  * actions(i.e runs INIT_SCRIPT and then starts a pair 
540  * of "askfirst" shells.  If BB_FEATURE_USE_INITTAB 
541  * _is_ defined, but /etc/inittab is missing == same
542  * default behavior.
543  * */
544 void parse_inittab(void) 
545 {
546 #ifdef BB_FEATURE_USE_INITTAB
547     FILE* file;
548     char buf[256], buf1[256];
549     char *p, *q, *r, *s;
550     const struct initActionType *a = actions;
551     int foundIt;
552
553
554     file = fopen(INITTAB, "r");
555     if (file == NULL) {
556         /* No inittab file -- set up some default behavior */
557 #endif
558         /* Askfirst shell on tty1 */
559         new_initAction( &(actions[3]), SHELL, console );
560         /* Askfirst shell on tty2 */
561         if (second_console != NULL) 
562             new_initAction( &(actions[3]), SHELL, second_console );
563         /* sysinit */
564         new_initAction( &(actions[0]), INIT_SCRIPT, console );
565
566         return;
567 #ifdef BB_FEATURE_USE_INITTAB
568     }
569
570     while ( fgets(buf, 255, file) != NULL) {
571         foundIt=FALSE;
572         for(p = buf; *p == ' ' || *p == '\t'; p++);
573         if (*p == '#' || *p == '\n') continue;
574
575         /* Trim the trailing \n */
576         q = strrchr( p, '\n');
577         if (q != NULL)
578             *q='\0';
579
580         /* Keep a copy around for posterity's sake (and error msgs) */
581         strcpy(buf1, buf);
582
583         /* Grab the ID field */
584         s=p;
585         p = strchr( p, ':');
586         if ( p != NULL || *(p+1) != '\0' ) {
587             *p='\0';
588             ++p;
589         }
590
591         /* Now peal off the process field from the end
592          * of the string */
593         q = strrchr( p, ':');
594         if ( q == NULL || *(q+1) == '\0' ) {
595             fprintf(stderr, "Bad inittab entry: %s\n", buf1);
596             continue;
597         } else {
598             *q='\0';
599             ++q;
600         }
601
602         /* Now peal off the action field */
603         r = strrchr( p, ':');
604         if ( r == NULL || *(r+1) == '\0') {
605             fprintf(stderr, "Bad inittab entry: %s\n", buf1);
606             continue;
607         } else {
608             ++r;
609         }
610
611         /* Ok, now process it */
612         a = actions;
613         while (a->name != 0) {
614             if (strcmp(a->name, r) == 0) {
615                 new_initAction( a, q, s);
616                 foundIt=TRUE;
617             }
618             a++;
619         }
620         if (foundIt==TRUE)
621             continue;
622         else {
623             /* Choke on an unknown action */
624             fprintf(stderr, "Bad inittab entry: %s\n", buf1);
625         }
626     }
627     return;
628 #endif
629 }
630
631 extern int init_main(int argc, char **argv)
632 {
633     initAction *a;
634     pid_t wpid;
635     int status;
636
637 #ifndef DEBUG_INIT
638     /* Expect to be PID 1 iff we are run as init (not linuxrc) */
639     if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
640         usage( "init\n\nInit is the parent of all processes.\n\n"
641                 "This version of init is designed to be run only by the kernel\n");
642     }
643
644     /* from the controlling terminal */
645     setsid();
646
647     /* Set up sig handlers  -- be sure to clear all of these in run() */
648     signal(SIGUSR1, halt_signal);
649     signal(SIGUSR2, reboot_signal);
650     signal(SIGINT, reboot_signal);
651     signal(SIGTERM, reboot_signal);
652
653     /* Turn off rebooting via CTL-ALT-DEL -- we get a 
654      * SIGINT on CAD so we can shut things down gracefully... */
655     reboot(RB_DISABLE_CAD);
656 #endif 
657     
658     /* Figure out where the default console should be */
659     console_init();
660
661     /* Close whatever files are open, and reset the console. */
662     close(0);
663     close(1);
664     close(2);
665     set_term(0);
666
667     /* Make sure PATH is set to something sane */
668     putenv(_PATH_STDPATH);
669
670    
671     /* Hello world */
672 #ifndef DEBUG_INIT
673     message(CONSOLE|LOG, 
674             "init started:  BusyBox v%s (%s) multi-call binary\r\n", 
675             BB_VER, BB_BT);
676 #else
677     message(CONSOLE|LOG, 
678             "init(%d) started:  BusyBox v%s (%s) multi-call binary\r\n", 
679             getpid(), BB_VER, BB_BT);
680 #endif
681
682     
683     /* Mount /proc */
684     if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
685         message(CONSOLE|LOG, "Mounting /proc: done.\n");
686         kernel_version = get_kernel_revision();
687     } else
688         message(CONSOLE|LOG, "Mounting /proc: failed!\n");
689
690     /* Make sure there is enough memory to do something useful. */
691     check_memory();
692
693     /* Check if we are supposed to be in single user mode */
694     if ( argc > 1 && (!strcmp(argv[1], "single") || 
695                 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) 
696     {
697         /* Ask first then start a shell on tty2 */
698         if (second_console != NULL) 
699             new_initAction( &(actions[3]), SHELL, second_console);
700         /* Ask first then start a shell on tty1 */
701         new_initAction( &(actions[3]), SHELL, console);
702     } else {
703         /* Not in single user mode -- see what inittab says */
704
705         /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
706          * then parse_inittab() simply adds in some default
707          * actions(i.e runs INIT_SCRIPT and then starts a pair 
708          * of "askfirst" shells */
709         parse_inittab();
710     }
711
712     /* Now run everything that needs to be run */
713
714     /* First run the sysinit command */
715     for( a=initActionList ; a; a=a->nextPtr) {
716         if (a->action == SYSINIT) {
717             waitfor(run(a->process, console, FALSE));
718             /* Now remove the "sysinit" entry from the list */
719             delete_initAction( a);
720         }
721     }
722     /* Next run anything that wants to block */
723     for( a=initActionList ; a; a=a->nextPtr) {
724         if (a->action == WAIT) {
725             waitfor(run(a->process, console, FALSE));
726             /* Now remove the "wait" entry from the list */
727             delete_initAction( a);
728         }
729     }
730     /* Next run anything to be run only once */
731     for( a=initActionList ; a; a=a->nextPtr) {
732         if (a->action == ONCE) {
733             run(a->process, console, FALSE);
734             /* Now remove the "once" entry from the list */
735             delete_initAction( a);
736         }
737     }
738
739     /* Now run the looping stuff for the rest of forever */
740     while (1) {
741         for( a=initActionList ; a; a=a->nextPtr) {
742             /* Only run stuff with pid==0.  If they have
743              * a pid, that means they are still running */
744             if (a->pid == 0) {
745                 switch(a->action) {
746                     case RESPAWN:
747                         /* run the respawn stuff */
748                         a->pid = run(a->process, console, FALSE);
749                         break;
750                     case ASKFIRST:
751                         /* run the askfirst stuff */
752                         a->pid = run(a->process, console, TRUE);
753                         break;
754                     /* silence the compiler's incessant whining */
755                     default:
756                         break;
757                 }
758             }
759         }
760         /* Wait for a child process to exit */
761         wpid = wait(&status);
762         if (wpid > 0 ) {
763             /* Find out who died and clean up their corpse */
764             for( a=initActionList ; a; a=a->nextPtr) {
765                 if (a->pid==wpid) {
766                     a->pid=0;
767                     message(LOG, "Process '%s' (pid %d) exited.  Scheduling it for restart.\n", 
768                             a->process, wpid);
769                 }
770             }
771         }
772         sleep(1);
773     }
774 }
775