X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=init%2Finit.c;h=cad64f63ae25f185e5d4a1bf51d515d844ea923c;hb=2aced7ea043aff64bd4e5f2814818fb4d0c803d4;hp=0db3554134b9ca371cd813604848d563769e0775;hpb=646129163a26171d48a335f7e22828848cf28cc5;p=oweals%2Fbusybox.git diff --git a/init/init.c b/init/init.c index 0db355413..cad64f63a 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. */ -/* Turn this on to disable all the dangerous - rebooting stuff when debugging. -#define DEBUG_INIT -*/ - -#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 - #define INIT_BUFFS_SIZE 256 /* From */ @@ -47,7 +31,7 @@ struct vt_stat { unsigned short v_signal; /* signal to send */ unsigned short v_state; /* vt bitmask */ }; -static const int VT_GETSTATE = 0x5603; /* get global vt state info */ +enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */ /* From */ struct serial_struct { @@ -72,7 +56,6 @@ struct serial_struct { int reserved[1]; }; - #ifndef _PATH_STDPATH #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin" #endif @@ -86,11 +69,8 @@ struct serial_struct { */ #define CORE_ENABLE_FLAG_FILE "/.init_enable_core" #include -#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. */ @@ -139,28 +119,33 @@ 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; -static const int LOG = 0x1; -static const int CONSOLE = 0x2; +#endif + +enum { + LOG = 0x1, + CONSOLE = 0x2, #if defined CONFIG_FEATURE_EXTRA_QUIET -static const int MAYBE_CONSOLE = 0x0; + MAYBE_CONSOLE = 0x0, #else -#define MAYBE_CONSOLE CONSOLE + MAYBE_CONSOLE = CONSOLE, #endif -#ifndef RB_HALT_SYSTEM -static const int RB_HALT_SYSTEM = 0xcdef0123; -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; +#ifndef RB_HALT_SYSTEM + RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */ + RB_ENABLE_CAD = 0x89abcdef, + RB_DISABLE_CAD = 0, + RB_POWER_OFF = 0x4321fedc, + RB_AUTOBOOT = 0x01234567, #endif +}; static const char * const environment[] = { "HOME=/", @@ -172,9 +157,10 @@ static const char * const environment[] = { /* Function prototypes */ static void delete_init_action(struct init_action *a); -static int waitfor(const struct init_action *a); -static void halt_signal(int sig); - +static int waitfor(const struct init_action *a, pid_t pid); +#if !ENABLE_DEBUG_INIT +static void shutdown_signal(int sig); +#endif static void loop_forever(void) { @@ -184,12 +170,13 @@ static void loop_forever(void) /* Print a message to the specified device. * Device may be bitwise-or'd from LOG | CONSOLE */ -#ifndef DEBUG_INIT -static inline void messageD(int device, const char *fmt, ...) +#if ENABLE_DEBUG_INIT +#define messageD message +#else +static inline void messageD(int ATTRIBUTE_UNUSED device, + const char ATTRIBUTE_UNUSED *fmt, ...) { } -#else -#define messageD message #endif static void message(int device, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); @@ -197,14 +184,14 @@ static void message(int device, const char *fmt, ...) { va_list arguments; int l; - char msg[1024]; + RESERVE_CONFIG_BUFFER(msg, 1024); #ifndef CONFIG_SYSLOGD static int log_fd = -1; #endif msg[0] = '\r'; va_start(arguments, fmt); - l = vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments) + 1; + l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1; va_end(arguments); #ifdef CONFIG_SYSLOGD @@ -234,18 +221,18 @@ 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); -#ifdef DEBUG_INIT +#if ENABLE_DEBUG_INIT /* all descriptors may be closed */ } else { bb_error_msg("Bummer, can't print: "); @@ -255,14 +242,15 @@ static void message(int device, const char *fmt, ...) #endif } } + RELEASE_CONFIG_BUFFER(msg); } /* Set terminal settings to reasonable defaults */ -static void set_term(int fd) +static void set_term(void) { struct termios tty; - tcgetattr(fd, &tty); + tcgetattr(STDIN_FILENO, &tty); /* set control chars */ tty.c_cc[VINTR] = 3; /* C-c */ @@ -292,7 +280,7 @@ static void set_term(int fd) tty.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN; - tcsetattr(fd, TCSANOW, &tty); + tcsetattr(STDIN_FILENO, TCSANOW, &tty); } static void console_init(void) @@ -305,15 +293,6 @@ static void console_init(void) if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) { safe_strncpy(console, s, sizeof(console)); -#if #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) { @@ -323,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++; } } @@ -331,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++; } @@ -378,9 +357,30 @@ static void fixup_argv(int argc, char **argv, char *new_argv0) } } +/* Open the new terminal device */ +static void open_new_terminal(const char * const device, const int fail) { + struct stat sb; + + if ((device_open(device, O_RDWR)) < 0) { + if (stat(device, &sb) != 0) { + message(LOG | CONSOLE, "device '%s' does not exist.", device); + } else { + message(LOG | CONSOLE, "Bummer, can't open %s", device); + } + if (fail) + _exit(1); + /* else */ +#if !ENABLE_DEBUG_INIT + shutdown_signal(SIGUSR1); +#else + _exit(2); +#endif + } +} + static pid_t run(const struct init_action *a) { - int i, junk; + int i; pid_t pid; char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath; char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */ @@ -397,7 +397,6 @@ static pid_t run(const struct init_action *a) sigprocmask(SIG_BLOCK, &nmask, &omask); if ((pid = fork()) == 0) { - struct stat sb; /* Clean up */ close(0); @@ -421,17 +420,10 @@ static pid_t run(const struct init_action *a) setsid(); /* Open the new terminal device */ - if ((device_open(a->terminal, O_RDWR)) < 0) { - if (stat(a->terminal, &sb) != 0) { - message(LOG | CONSOLE, "device '%s' does not exist.", a->terminal); - } else { - message(LOG | CONSOLE, "Bummer, can't open %s", a->terminal); - } - _exit(1); - } + open_new_terminal(a->terminal, 1); /* Make sure the terminal will act fairly normal for us */ - set_term(0); + set_term(); /* Setup stdout, stderr for the new process so * they point to the supplied terminal */ dup(0); @@ -440,7 +432,6 @@ static pid_t run(const struct init_action *a) /* If the init Action requires us to wait, then force the * supplied terminal to be the controlling tty. */ if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) { - pid_t pgrp, tmp_pid; /* Now fork off another process to just hang around */ if ((pid = fork()) < 0) { @@ -456,17 +447,9 @@ static pid_t run(const struct init_action *a) signal(SIGQUIT, SIG_IGN); signal(SIGCHLD, SIG_DFL); - /* Wait for child to exit */ - while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) { - if (tmp_pid == -1 && errno == ECHILD) { - break; - } - /* FIXME handle other errors */ - } - + waitfor(NULL, pid); /* See if stealing the controlling tty back is necessary */ - pgrp = tcgetpgrp(0); - if (pgrp != getpid()) + if (tcgetpgrp(0) != getpid()) _exit(0); /* Use a temporary process to steal the controlling tty. */ @@ -479,10 +462,7 @@ static pid_t run(const struct init_action *a) ioctl(0, TIOCSCTTY, 1); _exit(0); } - while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) { - if (tmp_pid < 0 && errno == ECHILD) - break; - } + waitfor(NULL, pid); _exit(0); } @@ -556,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') ; } @@ -567,12 +547,15 @@ static pid_t run(const struct init_action *a) getpid(), a->terminal, cmdpath); #if defined CONFIG_FEATURE_INIT_COREDUMPS - if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) { - struct rlimit limit; + { + struct stat sb; + if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) { + struct rlimit limit; - limit.rlim_cur = RLIM_INFINITY; - limit.rlim_max = RLIM_INFINITY; - setrlimit(RLIMIT_CORE, &limit); + limit.rlim_cur = RLIM_INFINITY; + limit.rlim_max = RLIM_INFINITY; + setrlimit(RLIMIT_CORE, &limit); + } } #endif @@ -588,15 +571,15 @@ static pid_t run(const struct init_action *a) return pid; } -static int waitfor(const struct init_action *a) +static int waitfor(const struct init_action *a, pid_t pid) { - int pid; + int runpid; int status, wpid; - pid = run(a); + runpid = (NULL == a)? pid : run(a); while (1) { - wpid = waitpid(pid,&status,0); - if (wpid == pid) + wpid = waitpid(runpid,&status,0); + if (wpid == runpid) break; if (wpid == -1 && errno == ECHILD) { /* we missed its termination */ @@ -616,8 +599,10 @@ static void run_actions(int action) for (a = init_action_list; a; a = tmp) { tmp = a->next; if (a->action == action) { - if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) { - waitfor(a); + if (access(a->terminal, R_OK | W_OK)) { + delete_init_action(a); + } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) { + waitfor(a, 0); delete_init_action(a); } else if (a->action & ONCE) { run(a); @@ -633,7 +618,7 @@ static void run_actions(int action) } } -#ifndef DEBUG_INIT +#if !ENABLE_DEBUG_INIT static void init_reboot(unsigned long magic) { pid_t pid; @@ -677,19 +662,19 @@ 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); sync(); } -static void exec_signal(int sig) +static void exec_signal(int sig ATTRIBUTE_UNUSED) { struct init_action *a, *tmp; sigset_t unblock_signals; @@ -719,18 +704,10 @@ static void exec_signal(int sig) close(2); /* Open the new terminal device */ - if ((device_open(a->terminal, O_RDWR)) < 0) { - struct stat sb; - if (stat(a->terminal, &sb) != 0) { - message(LOG | CONSOLE, "device '%s' does not exist.", a->terminal); - } else { - message(LOG | CONSOLE, "Bummer, can't open %s", a->terminal); - } - halt_signal(SIGUSR1); - } + open_new_terminal(a->terminal, 0); /* Make sure the terminal will act fairly normal for us */ - set_term(0); + set_term(); /* Setup stdout, stderr on the supplied terminal */ dup(0); dup(0); @@ -748,44 +725,41 @@ static void exec_signal(int sig) } } -static void halt_signal(int sig) +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); + char *m; + int rb; - if (sig == SIGUSR2) - init_reboot(RB_POWER_OFF); - else - init_reboot(RB_HALT_SYSTEM); - - loop_forever(); -} - -static void reboot_signal(int sig) -{ 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(); } -static void ctrlaltdel_signal(int sig) +static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED) { run_actions(CTRLALTDEL); } /* The SIGSTOP & SIGTSTP handler */ -static void stop_handler(int sig) +static void stop_handler(int sig ATTRIBUTE_UNUSED) { int saved_errno = errno; @@ -797,12 +771,12 @@ static void stop_handler(int sig) } /* The SIGCONT handler */ -static void cont_handler(int sig) +static void cont_handler(int sig ATTRIBUTE_UNUSED) { got_cont = 1; } -#endif /* ! DEBUG_INIT */ +#endif /* ! ENABLE_DEBUG_INIT */ static void new_init_action(int action, const char *command, const char *cons) { @@ -811,9 +785,6 @@ static void new_init_action(int action, const char *command, const char *cons) if (*cons == '\0') cons = console; - /* do not run entries if console device is not available */ - if (access(cons, R_OK | W_OK)) - return; if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST)) return; @@ -843,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); } @@ -980,7 +948,7 @@ static void parse_inittab(void) } #ifdef CONFIG_FEATURE_USE_INITTAB -static void reload_signal(int sig) +static void reload_signal(int sig ATTRIBUTE_UNUSED) { struct init_action *a, *tmp; @@ -1006,16 +974,15 @@ static void reload_signal(int sig) } #endif /* CONFIG_FEATURE_USE_INITTAB */ -extern int init_main(int argc, char **argv) +int init_main(int argc, char **argv) { struct init_action *a; pid_t wpid; - int status; if (argc > 1 && !strcmp(argv[1], "-q")) { return kill(1,SIGHUP); } -#ifndef DEBUG_INIT +#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"))) @@ -1026,10 +993,10 @@ extern 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); @@ -1048,7 +1015,7 @@ extern int init_main(int argc, char **argv) close(2); if (device_open(console, O_RDWR | O_NOCTTY) == 0) { - set_term(0); + set_term(); close(0); } @@ -1060,6 +1027,9 @@ extern 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); @@ -1094,6 +1064,22 @@ extern int init_main(int argc, char **argv) parse_inittab(); } +#ifdef CONFIG_SELINUX + if (getenv("SELINUX_INIT") == NULL) { + int enforce = 0; + + putenv("SELINUX_INIT=YES"); + if (selinux_init_load_policy(&enforce) == 0) { + execv(argv[0], argv); + } else if (enforce > 0) { + /* SELinux in enforcing mode but load_policy failed */ + /* At this point, we probably can't open /dev/console, so log() won't work */ + message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now."); + exit(1); + } + } +#endif /* CONFIG_SELINUX */ + /* Make the command line just say "init" -- thats all, nothing else */ fixup_argv(argc, argv, "init"); @@ -1128,7 +1114,7 @@ extern int init_main(int argc, char **argv) sleep(1); /* Wait for a child process to exit */ - wpid = wait(&status); + wpid = wait(NULL); while (wpid > 0) { /* Find out who died and clean up their corpse */ for (a = init_action_list; a; a = a->next) { @@ -1142,15 +1128,7 @@ extern int init_main(int argc, char **argv) } } /* see if anyone else is waiting to be reaped */ - wpid = waitpid (-1, &status, WNOHANG); + wpid = waitpid (-1, NULL, WNOHANG); } } } - -/* -Local Variables: -c-file-style: "linux" -c-basic-offset: 4 -tab-width: 4 -End: -*/