1 /* vi: set sw=4 ts=4: */
3 * Mini init implementation for busybox
6 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
7 * Adjusted by so many folks, it's impossible to keep track.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* Turn this on to disable all the dangerous
26 rebooting stuff when debugging.
40 #include <sys/fcntl.h>
41 #include <sys/ioctl.h>
42 #include <sys/mount.h>
43 #include <sys/types.h>
46 # include <sys/syslog.h>
49 /* From <linux/vt.h> */
51 unsigned short v_active; /* active vt */
52 unsigned short v_signal; /* signal to send */
53 unsigned short v_state; /* vt bitmask */
55 #define VT_GETSTATE 0x5603 /* get global vt state info */
57 /* From <linux/serial.h> */
58 struct serial_struct {
67 unsigned short close_delay;
68 char reserved_char[2];
70 unsigned short closing_wait; /* time to wait before closing */
71 unsigned short closing_wait2; /* no longer used... */
77 #ifndef RB_HALT_SYSTEM
78 #define RB_HALT_SYSTEM 0xcdef0123
79 #define RB_ENABLE_CAD 0x89abcdef
80 #define RB_DISABLE_CAD 0
81 #define RB_POWER_OFF 0x4321fedc
82 #define RB_AUTOBOOT 0x01234567
83 #if defined(__GLIBC__)
84 #include <sys/reboot.h>
85 #define init_reboot(magic) reboot(magic)
87 #define init_reboot(magic) reboot(0xfee1dead, 672274793, magic)
92 #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
96 #if defined BB_FEATURE_INIT_COREDUMPS
98 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
99 * before processes are spawned to set core file size as unlimited.
100 * This is for debugging only. Don't use this is production, unless
101 * you want core dumps lying about....
103 #define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
104 #include <sys/resource.h>
105 #include <sys/time.h>
108 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
110 #if defined(__GLIBC__)
111 #include <sys/kdaemon.h>
113 #include <linux/unistd.h> /* for _syscall() macro */
114 static _syscall2(int, bdflush, int, func, int, data);
115 #endif /* __GLIBC__ */
118 #define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
119 #define VT_SECONDARY "/dev/tty2" /* Virtual console */
120 #define VT_LOG "/dev/tty3" /* Virtual console */
121 #define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
122 #define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
123 #define SHELL "/bin/sh" /* Default shell */
124 #define INITTAB "/etc/inittab" /* inittab file location */
126 #define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
132 /* Allowed init action types */
142 /* A mapping between "inittab" action name strings and action type codes. */
143 typedef struct initActionType {
145 initActionEnum action;
148 static const struct initActionType actions[] = {
149 {"sysinit", SYSINIT},
150 {"respawn", RESPAWN},
151 {"askfirst", ASKFIRST},
154 {"ctrlaltdel", CTRLALTDEL},
158 /* Set up a linked list of initActions, to be read from inittab */
159 typedef struct initActionTag initAction;
160 struct initActionTag {
165 initActionEnum action;
167 initAction *initActionList = NULL;
170 static char *secondConsole = VT_SECONDARY;
171 static char *log = VT_LOG;
172 static int kernelVersion = 0;
173 static char termType[32] = "TERM=linux";
174 static char console[32] = _PATH_CONSOLE;
176 static void delete_initAction(initAction * action);
179 /* Print a message to the specified device.
180 * Device may be bitwise-or'd from LOG | CONSOLE */
181 static void message(int device, char *fmt, ...)
182 __attribute__ ((format (printf, 2, 3)));
183 static void message(int device, char *fmt, ...)
190 /* Log the message to syslogd */
194 va_start(arguments, fmt);
195 vsnprintf(msg, sizeof(msg), fmt, arguments);
197 openlog("init", 0, LOG_USER);
198 syslog(LOG_USER|LOG_INFO, msg);
202 static int log_fd = -1;
204 /* Take full control of the log tty, and never close it.
205 * It's mine, all mine! Muhahahaha! */
208 /* don't even try to log, because there is no such console */
210 /* log to main console instead */
212 } else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
214 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
220 if ((device & LOG) && (log_fd >= 0)) {
221 va_start(arguments, fmt);
222 vdprintf(log_fd, fmt, arguments);
227 if (device & CONSOLE) {
228 /* Always send console messages to /dev/console so people will see them. */
231 device_open(_PATH_CONSOLE,
232 O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) {
233 va_start(arguments, fmt);
234 vdprintf(fd, fmt, arguments);
238 fprintf(stderr, "Bummer, can't print: ");
239 va_start(arguments, fmt);
240 vfprintf(stderr, fmt, arguments);
247 #define CTRLCHAR(ch) ((ch)-0x40)
249 /* Set terminal settings to reasonable defaults */
250 void set_term(int fd)
256 /* set control chars */
257 tty.c_cc[VINTR] = CTRLCHAR('C'); /* Ctrl-C */
258 tty.c_cc[VQUIT] = CTRLCHAR('\\'); /* Ctrl-\ */
259 tty.c_cc[VERASE] = CTRLCHAR('?'); /* Ctrl-? */
260 tty.c_cc[VKILL] = CTRLCHAR('U'); /* Ctrl-U */
261 tty.c_cc[VEOF] = CTRLCHAR('D'); /* Ctrl-D */
262 tty.c_cc[VSTART] = CTRLCHAR('Q'); /* Ctrl-Q */
263 tty.c_cc[VSTOP] = CTRLCHAR('S'); /* Ctrl-S */
264 tty.c_cc[VSUSP] = CTRLCHAR('Z'); /* Ctrl-Z */
266 /* use line dicipline 0 */
269 /* Make it be sane */
270 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
271 tty.c_cflag |= HUPCL|CLOCAL;
274 tty.c_iflag = ICRNL | IXON | IXOFF;
277 tty.c_oflag = OPOST | ONLCR;
281 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
283 tcsetattr(fd, TCSANOW, &tty);
286 /* How much memory does this machine have? */
287 static int check_free_memory()
291 /* Pre initialize mem_unit in case this kernel is something prior to
292 * the linux 2.4 kernel (which will actually fill in mem_unit... */
294 if (sysinfo(&info) != 0) {
295 printf("Error checking free memory: %s\n", strerror(errno));
298 if (info.mem_unit==0) {
299 /* Looks like we have a kernel prior to Linux 2.4.x */
301 info.totalram/=info.mem_unit;
302 info.totalswap/=info.mem_unit;
304 /* Bah. Linux 2.4.x completely changed sysinfo. This can in theory
305 overflow a 32 bit unsigned long, but who puts more then 4GiB ram+swap
306 on an embedded system? */
308 info.totalram*=info.mem_unit;
309 info.totalswap*=info.mem_unit;
312 return(info.totalram+info.totalswap);
315 static void console_init()
318 int tried_devcons = 0;
319 int tried_vtprimary = 0;
321 struct serial_struct sr;
324 if ((s = getenv("TERM")) != NULL) {
325 snprintf(termType, sizeof(termType) - 1, "TERM=%s", s);
328 if ((s = getenv("CONSOLE")) != NULL) {
329 snprintf(console, sizeof(console) - 1, "%s", s);
332 /* sparc kernel supports console=tty[ab] parameter which is also
333 * passed to init, so catch it here */
334 else if ((s = getenv("console")) != NULL) {
335 /* remap tty[ab] to /dev/ttyS[01] */
336 if (strcmp(s, "ttya") == 0)
337 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON0);
338 else if (strcmp(s, "ttyb") == 0)
339 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON1);
343 /* 2.2 kernels: identify the real console backend and try to use it */
344 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
345 /* this is a serial console */
346 snprintf(console, sizeof(console) - 1, "/dev/ttyS%d", sr.line);
347 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
348 /* this is linux virtual tty */
349 snprintf(console, sizeof(console) - 1, "/dev/tty%d",
352 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
357 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
358 /* Can't open selected console -- try /dev/console */
359 if (!tried_devcons) {
361 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
364 /* Can't open selected console -- try vt1 */
365 if (!tried_vtprimary) {
367 snprintf(console, sizeof(console) - 1, "%s", VT_PRIMARY);
373 /* Perhaps we should panic here? */
374 snprintf(console, sizeof(console) - 1, "/dev/null");
376 /* check for serial console and disable logging to tty3 & running a
378 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
380 secondConsole = NULL;
381 /* Force the TERM setting to vt102 for serial console --
382 * iff TERM is set to linux (the default) */
383 if (strcmp( termType, "TERM=linux" ) == 0)
384 snprintf(termType, sizeof(termType) - 1, "TERM=vt102");
385 message(LOG | CONSOLE,
386 "serial console detected. Disabling virtual terminals.\r\n");
390 message(LOG, "console=%s\n", console);
393 static pid_t run(char *command, char *terminal, int get_enter)
400 static const char press_enter[] =
402 "\nPlease press Enter to activate this console. ";
403 char *environment[] = {
405 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
413 if ((pid = fork()) == 0) {
420 /* Reset signal handlers set for parent process */
421 signal(SIGUSR1, SIG_DFL);
422 signal(SIGUSR2, SIG_DFL);
423 signal(SIGINT, SIG_DFL);
424 signal(SIGTERM, SIG_DFL);
425 signal(SIGHUP, SIG_DFL);
427 if ((fd = device_open(terminal, O_RDWR)) < 0) {
428 message(LOG | CONSOLE, "Bummer, can't open %s\r\n", terminal);
434 ioctl(0, TIOCSCTTY, 0);
435 tcsetpgrp(0, getpgrp());
438 if (get_enter == TRUE) {
440 * Save memory by not exec-ing anything large (like a shell)
441 * before the user wants it. This is critical if swap is not
442 * enabled and the system has low memory. Generally this will
443 * be run on the second virtual console, and the first will
444 * be allowed to start a shell or whatever an init script
449 pid_t shell_pgid = getpid();
450 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
451 command, shell_pgid, terminal);
453 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
454 read(fileno(stdin), &c, 1);
458 /* Log the process name and args */
459 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
460 shell_pgid, terminal, command);
463 /* See if any special /bin/sh requiring characters are present */
464 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
467 strcpy(buf, "exec ");
468 strncat(buf, command, sizeof(buf) - strlen(buf) - 1);
472 /* Convert command (char*) into cmd (char**, one word per string) */
473 for (tmpCmd = command, i = 0;
474 (tmpCmd = strsep(&command, " \t")) != NULL;) {
475 if (*tmpCmd != '\0') {
484 #if defined BB_FEATURE_INIT_COREDUMPS
487 if (stat (CORE_ENABLE_FLAG_FILE, &sb) == 0) {
489 limit.rlim_cur = RLIM_INFINITY;
490 limit.rlim_max = RLIM_INFINITY;
491 setrlimit(RLIMIT_CORE, &limit);
496 /* Now run it. The new program will take over this PID,
497 * so nothing further in init.c should be run. */
498 execve(cmd[0], cmd, environment);
500 /* We're still here? Some error happened. */
501 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
508 static int waitfor(char *command, char *terminal, int get_enter)
511 int pid = run(command, terminal, get_enter);
514 wpid = wait(&status);
515 if (wpid > 0 && wpid != pid) {
524 /* Make sure there is enough memory to do something useful. *
525 * Calls "swapon -a" if needed so be sure /etc/fstab is present... */
526 static void check_memory()
530 if (check_free_memory() > 1000)
533 if (stat("/etc/fstab", &statBuf) == 0) {
534 /* swapon -a requires /proc typically */
535 waitfor("mount proc /proc -t proc", console, FALSE);
536 /* Try to turn on swap */
537 waitfor("swapon -a", console, FALSE);
538 if (check_free_memory() < 1000)
546 "Sorry, your computer does not have enough memory.\r\n");
551 /* Run all commands to be run right before halt/reboot */
552 static void run_lastAction(void)
555 for (a = initActionList; a; a = a->nextPtr) {
556 if (a->action == CTRLALTDEL) {
557 waitfor(a->process, a->console, FALSE);
558 delete_initAction(a);
565 static void shutdown_system(void)
568 /* first disable our SIGHUP signal */
569 signal(SIGHUP, SIG_DFL);
571 /* Allow Ctrl-Alt-Del to reboot system. */
572 init_reboot(RB_ENABLE_CAD);
574 message(CONSOLE|LOG, "\r\nThe system is going down NOW !!\r\n");
577 /* Send signals to every process _except_ pid 1 */
578 message(CONSOLE|LOG, "Sending SIGTERM to all processes.\r\n");
583 message(CONSOLE|LOG, "Sending SIGKILL to all processes.\r\n");
587 /* run everything to be run at "ctrlaltdel" */
591 if (kernelVersion > 0 && kernelVersion <= KERNEL_VERSION(2,2,11)) {
592 /* bdflush, kupdate not needed for kernels >2.2.11 */
598 static void halt_signal(int sig)
602 "The system is halted. Press %s or turn off power\r\n",
603 (secondConsole == NULL) /* serial console */
604 ? "Reset" : "CTRL-ALT-DEL");
607 /* allow time for last message to reach serial console */
610 if (sig == SIGUSR2 && kernelVersion >= KERNEL_VERSION(2,2,0))
611 init_reboot(RB_POWER_OFF);
613 init_reboot(RB_HALT_SYSTEM);
617 static void reboot_signal(int sig)
620 message(CONSOLE|LOG, "Please stand by while rebooting the system.\r\n");
623 /* allow time for last message to reach serial console */
626 init_reboot(RB_AUTOBOOT);
630 #if defined BB_FEATURE_INIT_CHROOT
632 #if ! defined BB_FEATURE_USE_PROCFS
633 #error Sorry, I depend on the /proc filesystem right now.
636 static void check_chroot(int sig)
638 char *argv_init[2] = { "init", NULL, };
639 char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
640 char rootpath[256], *tc;
643 if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
645 "SIGHUP recived, but could not open proc file\r\n");
649 if (read(fd, rootpath, sizeof(rootpath)) == -1) {
651 "SIGHUP recived, but could not read proc file\r\n");
657 if (rootpath[0] == '\0') {
659 "SIGHUP recived, but new root is not valid: %s\r\n",
665 tc = strrchr(rootpath, '\n');
668 /* Ok, making it this far means we commit */
669 message(CONSOLE, "Please stand by, changing root to `%s'.\r\n",
672 /* kill all other programs first */
673 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
678 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
683 /* ok, we don't need /proc anymore. we also assume that the signaling
684 * process left the rest of the filesystems alone for us */
687 /* Ok, now we chroot. Hopefully we only have two things mounted, the
688 * new chroot'd mount point, and the old "/" mount. s,
689 * we go ahead and unmount the old "/". This should trigger the kernel
690 * to set things up the Right Way(tm). */
692 if (!chroot(rootpath))
695 /* If the chroot fails, we are already too far to turn back, so we
696 * continue and hope that executing init below will revive the system */
698 /* close all of our descriptors and open new ones */
702 open("/dev/console", O_RDWR, 0);
706 message(CONSOLE, "Executing real init...\r\n");
707 /* execute init in the (hopefully) new root */
708 execve("/sbin/init", argv_init, envp_init);
711 "ERROR: Could not exec new init. Press %s to reboot.\r\n",
712 (secondConsole == NULL) /* serial console */
713 ? "Reset" : "CTRL-ALT-DEL");
716 #endif /* BB_FEATURE_INIT_CHROOT */
718 #endif /* ! DEBUG_INIT */
720 void new_initAction(initActionEnum action, char *process, char *cons)
722 initAction *newAction;
727 /* If BusyBox detects that a serial console is in use,
728 * then entries not refering to the console or null devices will _not_ be run.
729 * The exception to this rule is the null device.
731 if (secondConsole == NULL && strcmp(cons, console)
732 && strcmp(cons, "/dev/null"))
735 newAction = calloc((size_t) (1), sizeof(initAction));
737 message(LOG | CONSOLE, "Memory allocation failure\n");
741 newAction->nextPtr = initActionList;
742 initActionList = newAction;
743 strncpy(newAction->process, process, 255);
744 newAction->action = action;
745 strncpy(newAction->console, cons, 255);
747 // message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
748 // newAction->process, newAction->action, newAction->console);
751 static void delete_initAction(initAction * action)
753 initAction *a, *b = NULL;
755 for (a = initActionList; a; b = a, a = a->nextPtr) {
758 initActionList = a->nextPtr;
760 b->nextPtr = a->nextPtr;
768 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
769 * then parse_inittab() simply adds in some default
770 * actions(i.e runs INIT_SCRIPT and then starts a pair
771 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
772 * _is_ defined, but /etc/inittab is missing, this
773 * results in the same set of default behaviors.
775 void parse_inittab(void)
777 #ifdef BB_FEATURE_USE_INITTAB
779 char buf[256], lineAsRead[256], tmpConsole[256];
780 char *id, *runlev, *action, *process, *eol;
781 const struct initActionType *a = actions;
785 file = fopen(INITTAB, "r");
787 /* No inittab file -- set up some default behavior */
789 /* Swapoff on halt/reboot */
790 new_initAction(CTRLALTDEL, "/sbin/swapoff -a > /dev/null 2>&1", console);
791 /* Umount all filesystems on halt/reboot */
792 new_initAction(CTRLALTDEL, "/bin/umount -a -r > /dev/null 2>&1", console);
793 /* Askfirst shell on tty1 */
794 new_initAction(ASKFIRST, SHELL, console);
795 /* Askfirst shell on tty2 */
796 if (secondConsole != NULL)
797 new_initAction(ASKFIRST, SHELL, secondConsole);
799 new_initAction(SYSINIT, INIT_SCRIPT, console);
802 #ifdef BB_FEATURE_USE_INITTAB
805 while (fgets(buf, 255, file) != NULL) {
807 /* Skip leading spaces */
808 for (id = buf; *id == ' ' || *id == '\t'; id++);
810 /* Skip the line if it's a comment */
811 if (*id == '#' || *id == '\n')
814 /* Trim the trailing \n */
815 eol = strrchr(id, '\n');
819 /* Keep a copy around for posterity's sake (and error msgs) */
820 strcpy(lineAsRead, buf);
822 /* Separate the ID field from the runlevels */
823 runlev = strchr(id, ':');
824 if (runlev == NULL || *(runlev + 1) == '\0') {
825 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
832 /* Separate the runlevels from the action */
833 action = strchr(runlev, ':');
834 if (action == NULL || *(action + 1) == '\0') {
835 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
842 /* Separate the action from the process */
843 process = strchr(action, ':');
844 if (process == NULL || *(process + 1) == '\0') {
845 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
852 /* Ok, now process it */
854 while (a->name != 0) {
855 if (strcmp(a->name, action) == 0) {
859 strcpy(tmpConsole, "/dev/");
860 strncat(tmpConsole, id, 200);
861 if (stat(tmpConsole, &statBuf) != 0) {
862 message(LOG | CONSOLE,
863 "device '%s' does not exist. Did you read the directions?\n",
869 new_initAction(a->action, process, id);
877 /* Choke on an unknown action */
878 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
882 #endif /* BB_FEATURE_USE_INITTAB */
887 extern int init_main(int argc, char **argv)
894 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
896 #ifdef BB_FEATURE_LINUXRC
897 && strstr(argv[0], "linuxrc") == NULL
901 usage("init\n\nInit is the parent of all processes.\n\n"
902 "This version of init is designed to be run only "
905 /* Set up sig handlers -- be sure to
906 * clear all of these in run() */
907 signal(SIGUSR1, halt_signal);
908 signal(SIGUSR2, halt_signal);
909 signal(SIGINT, reboot_signal);
910 signal(SIGTERM, reboot_signal);
911 #if defined BB_FEATURE_INIT_CHROOT
912 signal(SIGHUP, check_chroot);
915 /* Turn off rebooting via CTL-ALT-DEL -- we get a
916 * SIGINT on CAD so we can shut things down gracefully... */
917 init_reboot(RB_DISABLE_CAD);
920 /* Figure out what kernel this is running */
921 kernelVersion = get_kernel_revision();
923 /* Figure out where the default console should be */
926 /* Close whatever files are open, and reset the console. */
934 /* Make sure PATH is set to something sane */
935 putenv(_PATH_STDPATH);
940 #if ! defined BB_FEATURE_EXTRA_QUIET
944 "init started: BusyBox v%s (%s) multi-call binary\r\n",
948 #if ! defined BB_FEATURE_EXTRA_QUIET
952 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
953 getpid(), BB_VER, BB_BT);
957 /* Make sure there is enough memory to do something useful. */
960 /* Check if we are supposed to be in single user mode */
961 if (argc > 1 && (!strcmp(argv[1], "single") ||
962 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
963 /* Ask first then start a shell on tty2 */
964 if (secondConsole != NULL)
965 new_initAction(ASKFIRST, SHELL, secondConsole);
966 /* Start a shell on tty1 */
967 new_initAction(RESPAWN, SHELL, console);
969 /* Not in single user mode -- see what inittab says */
971 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
972 * then parse_inittab() simply adds in some default
973 * actions(i.e runs INIT_SCRIPT and then starts a pair
974 * of "askfirst" shells */
978 /* Fix up argv[0] to be certain we claim to be init */
979 strncpy(argv[0], "init", strlen(argv[0])+1);
981 strncpy(argv[1], "\0", strlen(argv[1])+1);
983 /* Now run everything that needs to be run */
985 /* First run the sysinit command */
986 for (a = initActionList; a; a = a->nextPtr) {
987 if (a->action == SYSINIT) {
988 waitfor(a->process, a->console, FALSE);
989 /* Now remove the "sysinit" entry from the list */
990 delete_initAction(a);
993 /* Next run anything that wants to block */
994 for (a = initActionList; a; a = a->nextPtr) {
995 if (a->action == WAIT) {
996 waitfor(a->process, a->console, FALSE);
997 /* Now remove the "wait" entry from the list */
998 delete_initAction(a);
1001 /* Next run anything to be run only once */
1002 for (a = initActionList; a; a = a->nextPtr) {
1003 if (a->action == ONCE) {
1004 run(a->process, a->console, FALSE);
1005 /* Now remove the "once" entry from the list */
1006 delete_initAction(a);
1009 /* If there is nothing else to do, stop */
1010 if (initActionList == NULL) {
1011 message(LOG | CONSOLE,
1012 "No more tasks for init -- sleeping forever.\n");
1017 /* Now run the looping stuff for the rest of forever */
1019 for (a = initActionList; a; a = a->nextPtr) {
1020 /* Only run stuff with pid==0. If they have
1021 * a pid, that means they are still running */
1023 switch (a->action) {
1025 /* run the respawn stuff */
1026 a->pid = run(a->process, a->console, FALSE);
1029 /* run the askfirst stuff */
1030 a->pid = run(a->process, a->console, TRUE);
1032 /* silence the compiler's incessant whining */
1038 /* Wait for a child process to exit */
1039 wpid = wait(&status);
1041 /* Find out who died and clean up their corpse */
1042 for (a = initActionList; a; a = a->nextPtr) {
1043 if (a->pid == wpid) {
1046 "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
1057 c-file-style: "linux"