Announce 1.2.1
[oweals/busybox.git] / init / init.c
index b17ebc2ce9f7d57d911bebcd4be4b280a9cc55fc..cad64f63ae25f185e5d4a1bf51d515d844ea923c 100644 (file)
@@ -9,36 +9,20 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
+#include "busybox.h"
 #include <errno.h>
 #include <paths.h>
 #include <signal.h>
-#include <stdarg.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-#include <limits.h>
-#include <fcntl.h>
 #include <sys/ioctl.h>
-#include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/reboot.h>
-#include "busybox.h"
 
 #include "init_shared.h"
 
-
 #ifdef CONFIG_SYSLOGD
 # include <sys/syslog.h>
 #endif
 
-
-#ifdef CONFIG_SELINUX
-# include <selinux/selinux.h>
-#endif /* CONFIG_SELINUX */
-
-
 #define INIT_BUFFS_SIZE 256
 
 /* From <linux/vt.h> */
@@ -72,7 +56,6 @@ struct serial_struct {
        int     reserved[1];
 };
 
-
 #ifndef _PATH_STDPATH
 #define _PATH_STDPATH  "/usr/bin:/bin:/usr/sbin:/sbin"
 #endif
@@ -88,8 +71,6 @@ struct serial_struct {
 #include <sys/resource.h>
 #endif
 
-#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
-
 #define INITTAB      "/etc/inittab"    /* inittab file location */
 #ifndef INIT_SCRIPT
 #define INIT_SCRIPT  "/etc/init.d/rcS" /* Default sysinit script. */
@@ -138,12 +119,14 @@ struct init_action {
 
 /* Static variables */
 static struct init_action *init_action_list = NULL;
-static char console[CONSOLE_BUFF_SIZE] = _PATH_CONSOLE;
+static char console[CONSOLE_BUFF_SIZE] = CONSOLE_DEV;
 
 #ifndef CONFIG_SYSLOGD
 static char *log_console = VC_5;
 #endif
+#if !ENABLE_DEBUG_INIT
 static sig_atomic_t got_cont = 0;
+#endif
 
 enum {
        LOG = 0x1,
@@ -156,7 +139,7 @@ enum {
 #endif
 
 #ifndef RB_HALT_SYSTEM
-       RB_HALT_SYSTEM = 0xcdef0123,
+       RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
        RB_ENABLE_CAD = 0x89abcdef,
        RB_DISABLE_CAD = 0,
        RB_POWER_OFF = 0x4321fedc,
@@ -175,8 +158,9 @@ static const char * const environment[] = {
 /* Function prototypes */
 static void delete_init_action(struct init_action *a);
 static int waitfor(const struct init_action *a, pid_t pid);
-static void halt_signal(int sig);
-
+#if !ENABLE_DEBUG_INIT
+static void shutdown_signal(int sig);
+#endif
 
 static void loop_forever(void)
 {
@@ -237,16 +221,16 @@ static void message(int device, const char *fmt, ...)
                }
        }
        if ((device & LOG) && (log_fd >= 0)) {
-               bb_full_write(log_fd, msg, l);
+               full_write(log_fd, msg, l);
        }
 #endif
 
        if (device & CONSOLE) {
-               int fd = device_open(_PATH_CONSOLE,
+               int fd = device_open(CONSOLE_DEV,
                                        O_WRONLY | O_NOCTTY | O_NONBLOCK);
                /* Always send console messages to /dev/console so people will see them. */
                if (fd >= 0) {
-                       bb_full_write(fd, msg, l);
+                       full_write(fd, msg, l);
                        close(fd);
 #if ENABLE_DEBUG_INIT
                /* all descriptors may be closed */
@@ -309,15 +293,6 @@ static void console_init(void)
 
        if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
                safe_strncpy(console, s, sizeof(console));
-#if 0 /* #cpu(sparc) */
-       /* sparc kernel supports console=tty[ab] parameter which is also
-        * passed to init, so catch it here */
-               /* remap tty[ab] to /dev/ttyS[01] */
-               if (strcmp(s, "ttya") == 0)
-                       safe_strncpy(console, SC_0, sizeof(console));
-               else if (strcmp(s, "ttyb") == 0)
-                       safe_strncpy(console, SC_1, sizeof(console));
-#endif
        } else {
                /* 2.2 kernels: identify the real console backend and try to use it */
                if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
@@ -327,7 +302,7 @@ static void console_init(void)
                        /* this is linux virtual tty */
                        snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
                } else {
-                       safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
+                       safe_strncpy(console, CONSOLE_DEV, sizeof(console));
                        tried++;
                }
        }
@@ -335,7 +310,7 @@ static void console_init(void)
        while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
                /* Can't open selected console -- try
                        logical system console and VT_MASTER */
-               safe_strncpy(console, (tried == 0 ? _PATH_CONSOLE : CURRENT_VC),
+               safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC),
                                                        sizeof(console));
                tried++;
        }
@@ -383,7 +358,7 @@ static void fixup_argv(int argc, char **argv, char *new_argv0)
 }
 
 /* Open the new terminal device */
-static void open_new_terminal(const char *device, char fail) {
+static void open_new_terminal(const char * const device, const int fail) {
        struct stat sb;
 
        if ((device_open(device, O_RDWR)) < 0) {
@@ -395,7 +370,11 @@ static void open_new_terminal(const char *device, char fail) {
                if (fail)
                        _exit(1);
                /* else */
-               halt_signal(SIGUSR1);
+#if !ENABLE_DEBUG_INIT
+               shutdown_signal(SIGUSR1);
+#else
+               _exit(2);
+#endif
        }
 }
 
@@ -557,7 +536,7 @@ static pid_t run(const struct init_action *a)
                        messageD(LOG, "Waiting for enter to start '%s'"
                                                "(pid %d, terminal %s)\n",
                                          cmdpath, getpid(), a->terminal);
-                       bb_full_write(1, press_enter, sizeof(press_enter) - 1);
+                       full_write(1, press_enter, sizeof(press_enter) - 1);
                        while(read(0, &c, 1) == 1 && c != '\n')
                                ;
                }
@@ -683,12 +662,12 @@ static void shutdown_system(void)
        sync();
 
        /* Send signals to every process _except_ pid 1 */
-       message(CONSOLE | LOG, "Sending SIGTERM to all processes.");
+       message(CONSOLE | LOG, init_sending_format, "TERM");
        kill(-1, SIGTERM);
        sleep(1);
        sync();
 
-       message(CONSOLE | LOG, "Sending SIGKILL to all processes.");
+       message(CONSOLE | LOG, init_sending_format, "KILL");
        kill(-1, SIGKILL);
        sleep(1);
 
@@ -746,33 +725,30 @@ static void exec_signal(int sig ATTRIBUTE_UNUSED)
        }
 }
 
-static void halt_signal(int sig ATTRIBUTE_UNUSED)
+static void shutdown_signal(int sig)
 {
-       shutdown_system();
-       message(CONSOLE | LOG, "The system is halted.");
-       sync();
+       char *m;
+       int rb;
 
-       /* allow time for last message to reach serial console */
-       sleep(2);
-
-       if (sig == SIGUSR2)
-               init_reboot(RB_POWER_OFF);
-       else
-               init_reboot(RB_HALT_SYSTEM);
-
-       loop_forever();
-}
-
-static void reboot_signal(int sig ATTRIBUTE_UNUSED)
-{
        shutdown_system();
-       message(CONSOLE | LOG, "Please stand by while rebooting the system.");
+
+       if (sig == SIGTERM) {
+               m = "reboot";
+               rb = RB_AUTOBOOT;
+       } else if (sig == SIGUSR2) {
+               m = "poweroff";
+               rb = RB_POWER_OFF;
+       } else {
+               m = "halt";
+               rb = RB_HALT_SYSTEM;
+       }
+       message(CONSOLE | LOG, "Requesting system %s.", m);
        sync();
 
        /* allow time for last message to reach serial console */
        sleep(2);
 
-       init_reboot(RB_AUTOBOOT);
+       init_reboot(rb);
 
        loop_forever();
 }
@@ -838,9 +814,6 @@ static void new_init_action(int action, const char *command, const char *cons)
        strcpy(new_action->command, command);
        new_action->action = action;
        strcpy(new_action->terminal, cons);
-#if 0   /* calloc zeroed always */
-       new_action->pid = 0;
-#endif
        messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
                new_action->command, new_action->action, new_action->terminal);
 }
@@ -1020,10 +993,10 @@ int init_main(int argc, char **argv)
         * clear all of these in run() */
        signal(SIGHUP, exec_signal);
        signal(SIGQUIT, exec_signal);
-       signal(SIGUSR1, halt_signal);
-       signal(SIGUSR2, halt_signal);
+       signal(SIGUSR1, shutdown_signal);
+       signal(SIGUSR2, shutdown_signal);
        signal(SIGINT, ctrlaltdel_signal);
-       signal(SIGTERM, reboot_signal);
+       signal(SIGTERM, shutdown_signal);
        signal(SIGCONT, cont_handler);
        signal(SIGSTOP, stop_handler);
        signal(SIGTSTP, stop_handler);
@@ -1054,6 +1027,9 @@ int init_main(int argc, char **argv)
                for(e = environment; *e; e++)
                        putenv((char *) *e);
        }
+
+       if (argc > 1) setenv("RUNLEVEL", argv[1], 1);
+
        /* Hello world */
        message(MAYBE_CONSOLE | LOG, "init started:  %s", bb_msg_full_version);