Patch from Lars Kellogg-Stedman <lars@larsshack.org> to fix bug
[oweals/busybox.git] / init.c
diff --git a/init.c b/init.c
index 570b8a697fe23e8d6640eb42d85eea75fdbd1f85..c26314c8be9b5dfaf2b743d6a9d47fac9fb02483 100644 (file)
--- a/init.c
+++ b/init.c
@@ -43,9 +43,6 @@
 #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
@@ -85,13 +82,14 @@ static const int RB_ENABLE_CAD = 0x89abcdef;
 static const int RB_DISABLE_CAD = 0;
 #define RB_POWER_OFF    0x4321fedc
 static const int RB_AUTOBOOT = 0x01234567;
-#if defined(__GLIBC__) || defined (__UCLIBC__)
-#include <sys/reboot.h>
+#endif
+
+#if __GNU_LIBRARY__ > 5
+  #include <sys/reboot.h>
   #define init_reboot(magic) reboot(magic)
 #else
   #define init_reboot(magic) reboot(0xfee1dead, 672274793, magic)
 #endif
-#endif
 
 #ifndef _PATH_STDPATH
 #define _PATH_STDPATH  "/usr/bin:/bin:/usr/sbin:/sbin"
@@ -112,13 +110,11 @@ static const int RB_AUTOBOOT = 0x01234567;
 
 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
 
-#if defined(__GLIBC__)
-#include <sys/kdaemon.h>
+#if __GNU_LIBRARY__ > 5
+       #include <sys/kdaemon.h>
 #else
-#include <sys/syscall.h>
-#include <linux/unistd.h>
-static _syscall2(int, bdflush, int, func, int, data);
-#endif                                                 /* __GLIBC__ */
+       extern int bdflush (int func, long int data);
+#endif
 
 
 #define VT_PRIMARY   "/dev/tty1"     /* Primary virtual console */
@@ -279,7 +275,8 @@ static void set_term(int fd)
 
        /* Make it be sane */
        tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
-       tty.c_cflag |= HUPCL|CLOCAL;
+       tty.c_cflag |= CREAD|HUPCL|CLOCAL;
+
 
        /* input modes */
        tty.c_iflag = ICRNL | IXON | IXOFF;
@@ -331,7 +328,7 @@ static void console_init()
        }
 
        if ((s = getenv("CONSOLE")) != NULL) {
-               snprintf(console, sizeof(console) - 1, "%s", s);
+               safe_strncpy(console, s, sizeof(console));
        }
 #if #cpu(sparc)
        /* sparc kernel supports console=tty[ab] parameter which is also 
@@ -339,9 +336,9 @@ static void console_init()
        else if ((s = getenv("console")) != NULL) {
                /* remap tty[ab] to /dev/ttyS[01] */
                if (strcmp(s, "ttya") == 0)
-                       snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON0);
+                       safe_strncpy(console, SERIAL_CON0, sizeof(console));
                else if (strcmp(s, "ttyb") == 0)
-                       snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON1);
+                       safe_strncpy(console, SERIAL_CON1, sizeof(console));
        }
 #endif
        else {
@@ -354,7 +351,7 @@ static void console_init()
                        snprintf(console, sizeof(console) - 1, "/dev/tty%d",
                                         vt.v_active);
                } else {
-                       snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
+                       safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
                        tried_devcons++;
                }
        }
@@ -363,20 +360,20 @@ static void console_init()
                /* Can't open selected console -- try /dev/console */
                if (!tried_devcons) {
                        tried_devcons++;
-                       snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
+                       safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
                        continue;
                }
                /* Can't open selected console -- try vt1 */
                if (!tried_vtprimary) {
                        tried_vtprimary++;
-                       snprintf(console, sizeof(console) - 1, "%s", VT_PRIMARY);
+                       safe_strncpy(console, VT_PRIMARY, sizeof(console));
                        continue;
                }
                break;
        }
        if (fd < 0) {
                /* Perhaps we should panic here? */
-               snprintf(console, sizeof(console) - 1, "/dev/null");
+               safe_strncpy(console, "/dev/null", sizeof(console));
        } else {
                /* check for serial console and disable logging to tty5 & running a
                   * shell to tty2-4 */
@@ -388,7 +385,7 @@ static void console_init()
                        /* Force the TERM setting to vt102 for serial console --
                         * iff TERM is set to linux (the default) */
                        if (strcmp( termType, "TERM=linux" ) == 0)
-                               snprintf(termType, sizeof(termType) - 1, "TERM=vt102");
+                               safe_strncpy(termType, "TERM=vt102", sizeof(termType));
                        message(LOG | CONSOLE,
                                        "serial console detected.  Disabling virtual terminals.\r\n");
                }
@@ -655,7 +652,7 @@ static void shutdown_system(void)
        kill(-1, SIGKILL);
        sleep(1);
 
-       /* run everything to be run at "ctrlaltdel" */
+       /* run everything to be run at "shutdown" */
        run_actions(SHUTDOWN);
 
        sync();