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