X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=init%2Finit.c;h=2e8ca3ca83d35d02b44f6c0acf386bfb2d7275a2;hb=98ee06d3d46aa7f89c204681c7075b53300a6a6e;hp=d072a22091f766601624b2b3355b3b3bcec778b4;hpb=7ae155398062be93c71116fcc757471d5a324ddf;p=oweals%2Fbusybox.git diff --git a/init/init.c b/init/init.c index d072a2209..2e8ca3ca8 100644 --- a/init/init.c +++ b/init/init.c @@ -9,36 +9,20 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include -#include +#include "busybox.h" #include #include #include -#include -#include -#include -#include -#include -#include #include -#include #include #include -#include "busybox.h" #include "init_shared.h" - #ifdef CONFIG_SYSLOGD # include #endif - -#ifdef CONFIG_SELINUX -# include -#endif /* CONFIG_SELINUX */ - - #define INIT_BUFFS_SIZE 256 /* From */ @@ -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 #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) { @@ -189,10 +173,7 @@ static void loop_forever(void) #if ENABLE_DEBUG_INIT #define messageD message #else -static inline void messageD(int ATTRIBUTE_UNUSED device, - const char ATTRIBUTE_UNUSED *fmt, ...) -{ -} +#define messageD(...) do {} while (0) #endif static void message(int device, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); @@ -214,7 +195,7 @@ static void message(int device, const char *fmt, ...) /* Log the message to syslogd */ if (device & LOG) { /* don`t out "\r\n" */ - openlog(bb_applet_name, 0, LOG_DAEMON); + openlog(applet_name, 0, LOG_DAEMON); syslog(LOG_INFO, "%s", msg + 1); closelog(); } @@ -230,28 +211,28 @@ static void message(int device, const char *fmt, ...) if (log_fd < 0) { if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) { log_fd = -2; - bb_error_msg("Bummer, can't write to log on %s!", log_console); + bb_error_msg("bummer, can't write to log on %s!", log_console); device = CONSOLE; } else { fcntl(log_fd, F_SETFD, FD_CLOEXEC); } } 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 */ } else { - bb_error_msg("Bummer, can't print: "); + bb_error_msg("bummer, can't print: "); va_start(arguments, fmt); vfprintf(stderr, fmt, arguments); va_end(arguments); @@ -309,15 +290,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 +299,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 +307,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 +355,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 +367,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,8 +533,8 @@ 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); - while(read(0, &c, 1) == 1 && c != '\n') + full_write(1, press_enter, sizeof(press_enter) - 1); + while (read(0, &c, 1) == 1 && c != '\n') ; } #endif @@ -585,7 +561,7 @@ static pid_t run(const struct init_action *a) execv(cmdpath, cmd); /* We're still here? Some error happened. */ - message(LOG | CONSOLE, "Bummer, could not run '%s': %m", cmdpath); + message(LOG | CONSOLE, "Bummer, cannot run '%s': %m", cmdpath); _exit(-1); } sigprocmask(SIG_SETMASK, &omask, NULL); @@ -746,33 +722,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(); - - /* 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(); -} + char *m; + int rb; -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 +811,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); } @@ -1012,7 +982,7 @@ int init_main(int argc, char **argv) #if !ENABLE_DEBUG_INIT /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */ if (getpid() != 1 && - (!ENABLE_FEATURE_INITRD || !strstr(bb_applet_name, "linuxrc"))) + (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc"))) { bb_show_usage(); } @@ -1020,10 +990,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); @@ -1051,9 +1021,12 @@ int init_main(int argc, char **argv) { const char * const *e; /* Make sure environs is set to something sane */ - for(e = environment; *e; e++) + 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); @@ -1062,7 +1035,7 @@ int init_main(int argc, char **argv) struct sysinfo info; if (!sysinfo(&info) && - (info.mem_unit ? : 1) * (long long)info.totalram < MEGABYTE) + (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024) { message(CONSOLE,"Low memory: forcing swapon."); /* swapon -a requires /proc typically */ @@ -1074,8 +1047,9 @@ int init_main(int argc, char **argv) } /* Check if we are supposed to be in single user mode */ - if (argc > 1 && (!strcmp(argv[1], "single") || - !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { + if (argc > 1 + && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1')) + ) { /* Start a shell on console */ new_init_action(RESPAWN, bb_default_login_shell, ""); } else { @@ -1152,7 +1126,7 @@ int init_main(int argc, char **argv) } } /* see if anyone else is waiting to be reaped */ - wpid = waitpid (-1, NULL, WNOHANG); + wpid = waitpid(-1, NULL, WNOHANG); } } }