- add libbb function str_tolower to convert a string to lowercase.
[oweals/busybox.git] / init / init.c
index e1ad1e6157b7bff201cf91937e73b7272aa9aa04..8c32b7791b2e67a7671b1b40119e735e916d2c12 100644 (file)
@@ -16,7 +16,7 @@
 #include <sys/wait.h>
 #include <sys/reboot.h>
 
-#if ENABLE_SYSLOGD
+#if ENABLE_FEATURE_INIT_SYSLOG
 # include <sys/syslog.h>
 #endif
 
@@ -84,7 +84,7 @@ struct init_action {
 /* Static variables */
 static struct init_action *init_action_list = NULL;
 
-#if !ENABLE_SYSLOGD
+#if !ENABLE_FEATURE_INIT_SYSLOG
 static const char *log_console = VC_5;
 #endif
 #if !ENABLE_DEBUG_INIT
@@ -144,7 +144,7 @@ static void message(int device, const char *fmt, ...)
        __attribute__ ((format(printf, 2, 3)));
 static void message(int device, const char *fmt, ...)
 {
-#if !ENABLE_SYSLOGD
+#if !ENABLE_FEATURE_INIT_SYSLOG
        static int log_fd = -1;
 #endif
 
@@ -159,7 +159,7 @@ static void message(int device, const char *fmt, ...)
        msg[sizeof(msg) - 2] = '\0';
        l = strlen(msg);
 
-#if ENABLE_SYSLOGD
+#if ENABLE_FEATURE_INIT_SYSLOG
        /* Log the message to syslogd */
        if (device & L_LOG) {
                /* don't out "\r" */
@@ -201,7 +201,7 @@ static void message(int device, const char *fmt, ...)
 }
 
 /* Set terminal settings to reasonable defaults */
-static void set_term(void)
+static void set_sane_term(void)
 {
        struct termios tty;
 
@@ -285,7 +285,7 @@ static void console_init(void)
                 * if TERM is set to linux (the default) */
                if (!s || strcmp(s, "linux") == 0)
                        putenv((char*)"TERM=vt102");
-#if !ENABLE_SYSLOGD
+#if !ENABLE_FEATURE_INIT_SYSLOG
                log_console = NULL;
 #endif
        } else if (!s)
@@ -313,9 +313,8 @@ static void open_stdio_to_tty(const char* tty_name, int fail)
 {
        /* empty tty_name means "use init's tty", else... */
        if (tty_name[0]) {
-               close(0);
-               if ((device_open(tty_name, O_RDWR)) < 0) {
-                       dup2(1, 0); /* restore fd #0 - avoid nasty surprises */
+               int fd = device_open(tty_name, O_RDWR);
+               if (fd < 0) {
                        message(L_LOG | L_CONSOLE, "Can't open %s: %s",
                                tty_name, strerror(errno));
                        if (fail)
@@ -325,13 +324,14 @@ static void open_stdio_to_tty(const char* tty_name, int fail)
 #else
                        _exit(2);
 #endif
+               } else {
+                       dup2(fd, 0);
+                       dup2(fd, 1);
+                       dup2(fd, 2);
+                       if (fd > 2) close(fd);
                }
        }
-       close(1);
-       close(2);
-       set_term();
-       dup(0);
-       dup(0);
+       set_sane_term();
 }
 
 static pid_t run(const struct init_action *a)
@@ -880,8 +880,7 @@ static void reload_signal(int sig ATTRIBUTE_UNUSED)
        /* remove unused entrys */
        for (a = init_action_list; a; a = tmp) {
                tmp = a->next;
-               if (a->action & (ONCE | SYSINIT | WAIT ) &&
-                               a->pid == 0 ) {
+               if ((a->action & (ONCE | SYSINIT | WAIT)) && a->pid == 0) {
                        delete_init_action(a);
                }
        }
@@ -926,7 +925,7 @@ int init_main(int argc, char **argv)
 
        /* Figure out where the default console should be */
        console_init();
-       set_term();
+       set_sane_term();
        chdir("/");
        setsid();
        {