Applied patch from David Douthitt to fix build error in df.c when
[oweals/busybox.git] / init.c
diff --git a/init.c b/init.c
index 49951b0fd439d87d4e0239be5dfbfc3dfc0385d0..0620e1ea1e2432c6f6b5864fca690ce360929258 100644 (file)
--- a/init.c
+++ b/init.c
@@ -27,7 +27,6 @@
 #define DEBUG_INIT
 */
 
-#include "busybox.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/mount.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include "busybox.h"
+#define bb_need_full_version
+#define BB_DECLARE_EXTERN
+#include "messages.c"
 #ifdef BB_SYSLOGD
 # include <sys/syslog.h>
 #endif
 
-#define bb_need_full_version
-#define BB_DECLARE_EXTERN
-#include "messages.c"
 
 /* From <linux/vt.h> */
 struct vt_stat {
@@ -134,6 +134,8 @@ static _syscall2(int, bdflush, int, func, int, data);
 #define INIT_SCRIPT  "/etc/init.d/rcS"   /* Default sysinit script. */
 #endif
 
+#define MAXENV 16              /* Number of env. vars */
+//static const int MAXENV = 16;        /* Number of env. vars */
 static const int LOG = 0x1;
 static const int CONSOLE = 0x2;
 
@@ -394,7 +396,8 @@ static void console_init()
 
 static pid_t run(char *command, char *terminal, int get_enter)
 {
-       int i, fd;
+       int i=0, j=0;
+       int fd;
        pid_t pid;
        char *tmpCmd;
         char *cmd[255], *cmdpath;
@@ -406,15 +409,21 @@ static pid_t run(char *command, char *terminal, int get_enter)
 #endif
 
                "\nPlease press Enter to activate this console. ";
-       char *environment[] = {
+       char *environment[MAXENV+1] = {
                "HOME=/",
                "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
                "SHELL=/bin/sh",
                termType,
-               "USER=root",
-               0
+               "USER=root"
        };
 
+       while (environment[i]) i++;
+       while ((environ[j]) && (i < MAXENV)) {
+               if (strncmp(environ[j], "TERM=", 5))
+                       environment[i++] = environ[j];
+               j++;
+       }
+
        if ((pid = fork()) == 0) {
                /* Clean up */
                ioctl(0, TIOCNOTTY, 0);
@@ -451,9 +460,8 @@ static pid_t run(char *command, char *terminal, int get_enter)
                         * specifies.
                         */
 #ifdef DEBUG_INIT
-                       pid_t shell_pgid = getpid();
                        message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
-                                       command, shell_pgid, terminal);
+                                       command, getpid(), terminal);
 #endif
                        write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
                        getc(stdin);
@@ -462,7 +470,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
 #ifdef DEBUG_INIT
                /* Log the process name and args */
                message(LOG, "Starting pid %d, console %s: '%s'\r\n",
-                               shell_pgid, terminal, command);
+                               getpid(), terminal, command);
 #endif
 
                /* See if any special /bin/sh requiring characters are present */
@@ -660,98 +668,6 @@ static void reboot_signal(int sig)
        exit(0);
 }
 
-#if defined BB_FEATURE_INIT_CHROOT
-
-#warning BB_FEATURE_INIT_CHROOT is out of date and should be rewritten to us
-#warning pivot root instead.  Do not even bother till this work is done...
-#warning You have been warned.
-
-#if ! defined BB_FEATURE_USE_PROCFS
-#error Sorry, I depend on the /proc filesystem right now.
-#endif
-
-static void check_chroot(int sig)
-{
-       char *argv_init[2] = { "init", NULL, };
-       char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
-       char rootpath[256], *tc;
-       int fd;
-
-       if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
-               message(CONSOLE,
-                               "SIGHUP recived, but could not open proc file\r\n");
-               sleep(2);
-               return;
-       }
-       if (read(fd, rootpath, sizeof(rootpath)) == -1) {
-               message(CONSOLE,
-                               "SIGHUP recived, but could not read proc file\r\n");
-               sleep(2);
-               return;
-       }
-       close(fd);
-
-       if (rootpath[0] == '\0') {
-               message(CONSOLE,
-                               "SIGHUP recived, but new root is not valid: %s\r\n",
-                               rootpath);
-               sleep(2);
-               return;
-       }
-
-       tc = strrchr(rootpath, '\n');
-       *tc = '\0';
-
-       /* Ok, making it this far means we commit */
-       message(CONSOLE, "Please stand by, changing root to `%s'.\r\n",
-                       rootpath);
-
-       /* kill all other programs first */
-       message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
-       kill(-1, SIGTERM);
-       sleep(2);
-       sync();
-
-       message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
-       kill(-1, SIGKILL);
-       sleep(2);
-       sync();
-
-       /* ok, we don't need /proc anymore. we also assume that the signaling
-        * process left the rest of the filesystems alone for us */
-       umount("/proc");
-
-       /* Ok, now we chroot. Hopefully we only have two things mounted, the
-        * new chroot'd mount point, and the old "/" mount. s,
-        * we go ahead and unmount the old "/". This should trigger the kernel
-        * to set things up the Right Way(tm). */
-
-       if (!chroot(rootpath))
-               umount("/dev/root");
-
-       /* If the chroot fails, we are already too far to turn back, so we
-        * continue and hope that executing init below will revive the system */
-
-       /* close all of our descriptors and open new ones */
-       close(0);
-       close(1);
-       close(2);
-       open("/dev/console", O_RDWR, 0);
-       dup(0);
-       dup(0);
-
-       message(CONSOLE, "Executing real init...\r\n");
-       /* execute init in the (hopefully) new root */
-       execve("/sbin/init", argv_init, envp_init);
-
-       message(CONSOLE,
-                       "ERROR: Could not exec new init. Press %s to reboot.\r\n",
-                       (secondConsole == NULL) /* serial console */
-                       ? "Reset" : "CTRL-ALT-DEL");
-       return;
-}
-#endif                                                 /* BB_FEATURE_INIT_CHROOT */
-
 #endif                                                 /* ! DEBUG_INIT */
 
 void new_initAction(initActionEnum action, char *process, char *cons)
@@ -941,9 +857,7 @@ extern int init_main(int argc, char **argv)
 #endif
                          )
        {
-                       usage("init\n\nInit is the parent of all processes.\n\n"
-                                 "This version of init is designed to be run only "
-                                 "by the kernel.");
+                       show_usage();
        }
        /* Set up sig handlers  -- be sure to
         * clear all of these in run() */
@@ -951,9 +865,6 @@ extern int init_main(int argc, char **argv)
        signal(SIGUSR2, halt_signal);
        signal(SIGINT, reboot_signal);
        signal(SIGTERM, reboot_signal);
-#if defined BB_FEATURE_INIT_CHROOT
-       signal(SIGHUP, check_chroot);
-#endif
 
        /* Turn off rebooting via CTL-ALT-DEL -- we get a 
         * SIGINT on CAD so we can shut things down gracefully... */