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