[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Mini init implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
|
---|
| 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
---|
| 7 | * Adjusted by so many folks, it's impossible to keep track.
|
---|
| 8 | *
|
---|
| 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | #include "busybox.h"
|
---|
| 13 | #include <stdio.h>
|
---|
| 14 | #include <stdlib.h>
|
---|
| 15 | #include <errno.h>
|
---|
| 16 | #include <paths.h>
|
---|
| 17 | #include <signal.h>
|
---|
| 18 | #include <stdarg.h>
|
---|
| 19 | #include <string.h>
|
---|
| 20 | #include <termios.h>
|
---|
| 21 | #include <unistd.h>
|
---|
| 22 | #include <limits.h>
|
---|
| 23 | #include <fcntl.h>
|
---|
| 24 | #include <sys/ioctl.h>
|
---|
| 25 | #include <sys/types.h>
|
---|
| 26 | #include <sys/wait.h>
|
---|
| 27 | #include <sys/reboot.h>
|
---|
| 28 |
|
---|
| 29 | #include "init_shared.h"
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | #ifdef CONFIG_SYSLOGD
|
---|
| 33 | # include <sys/syslog.h>
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | #ifdef CONFIG_SELINUX
|
---|
| 38 | # include <selinux/selinux.h>
|
---|
| 39 | #endif /* CONFIG_SELINUX */
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | #define INIT_BUFFS_SIZE 256
|
---|
| 43 |
|
---|
| 44 | /* From <linux/vt.h> */
|
---|
| 45 | struct vt_stat {
|
---|
| 46 | unsigned short v_active; /* active vt */
|
---|
| 47 | unsigned short v_signal; /* signal to send */
|
---|
| 48 | unsigned short v_state; /* vt bitmask */
|
---|
| 49 | };
|
---|
| 50 | enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
|
---|
| 51 |
|
---|
| 52 | /* From <linux/serial.h> */
|
---|
| 53 | struct serial_struct {
|
---|
| 54 | int type;
|
---|
| 55 | int line;
|
---|
| 56 | unsigned int port;
|
---|
| 57 | int irq;
|
---|
| 58 | int flags;
|
---|
| 59 | int xmit_fifo_size;
|
---|
| 60 | int custom_divisor;
|
---|
| 61 | int baud_base;
|
---|
| 62 | unsigned short close_delay;
|
---|
| 63 | char io_type;
|
---|
| 64 | char reserved_char[1];
|
---|
| 65 | int hub6;
|
---|
| 66 | unsigned short closing_wait; /* time to wait before closing */
|
---|
| 67 | unsigned short closing_wait2; /* no longer used... */
|
---|
| 68 | unsigned char *iomem_base;
|
---|
| 69 | unsigned short iomem_reg_shift;
|
---|
| 70 | unsigned int port_high;
|
---|
| 71 | unsigned long iomap_base; /* cookie passed into ioremap */
|
---|
| 72 | int reserved[1];
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | #ifndef _PATH_STDPATH
|
---|
| 77 | #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
|
---|
| 78 | #endif
|
---|
| 79 |
|
---|
| 80 | #if defined CONFIG_FEATURE_INIT_COREDUMPS
|
---|
| 81 | /*
|
---|
| 82 | * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
|
---|
| 83 | * before processes are spawned to set core file size as unlimited.
|
---|
| 84 | * This is for debugging only. Don't use this is production, unless
|
---|
| 85 | * you want core dumps lying about....
|
---|
| 86 | */
|
---|
| 87 | #define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
|
---|
| 88 | #include <sys/resource.h>
|
---|
| 89 | #endif
|
---|
| 90 |
|
---|
| 91 | #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
|
---|
| 92 |
|
---|
| 93 | #define INITTAB "/etc/inittab" /* inittab file location */
|
---|
| 94 | #ifndef INIT_SCRIPT
|
---|
| 95 | #define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
|
---|
| 96 | #endif
|
---|
| 97 |
|
---|
| 98 | #define MAXENV 16 /* Number of env. vars */
|
---|
| 99 |
|
---|
| 100 | #define CONSOLE_BUFF_SIZE 32
|
---|
| 101 |
|
---|
| 102 | /* Allowed init action types */
|
---|
| 103 | #define SYSINIT 0x001
|
---|
| 104 | #define RESPAWN 0x002
|
---|
| 105 | #define ASKFIRST 0x004
|
---|
| 106 | #define WAIT 0x008
|
---|
| 107 | #define ONCE 0x010
|
---|
| 108 | #define CTRLALTDEL 0x020
|
---|
| 109 | #define SHUTDOWN 0x040
|
---|
| 110 | #define RESTART 0x080
|
---|
| 111 |
|
---|
| 112 | /* A mapping between "inittab" action name strings and action type codes. */
|
---|
| 113 | struct init_action_type {
|
---|
| 114 | const char *name;
|
---|
| 115 | int action;
|
---|
| 116 | };
|
---|
| 117 |
|
---|
| 118 | static const struct init_action_type actions[] = {
|
---|
| 119 | {"sysinit", SYSINIT},
|
---|
| 120 | {"respawn", RESPAWN},
|
---|
| 121 | {"askfirst", ASKFIRST},
|
---|
| 122 | {"wait", WAIT},
|
---|
| 123 | {"once", ONCE},
|
---|
| 124 | {"ctrlaltdel", CTRLALTDEL},
|
---|
| 125 | {"shutdown", SHUTDOWN},
|
---|
| 126 | {"restart", RESTART},
|
---|
| 127 | {0, 0}
|
---|
| 128 | };
|
---|
| 129 |
|
---|
| 130 | /* Set up a linked list of init_actions, to be read from inittab */
|
---|
| 131 | struct init_action {
|
---|
| 132 | pid_t pid;
|
---|
| 133 | char command[INIT_BUFFS_SIZE];
|
---|
| 134 | char terminal[CONSOLE_BUFF_SIZE];
|
---|
| 135 | struct init_action *next;
|
---|
| 136 | int action;
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | /* Static variables */
|
---|
| 140 | static struct init_action *init_action_list = NULL;
|
---|
| 141 | static char console[CONSOLE_BUFF_SIZE] = CONSOLE_DEV;
|
---|
| 142 |
|
---|
| 143 | #ifndef CONFIG_SYSLOGD
|
---|
| 144 | static char *log_console = VC_5;
|
---|
| 145 | #endif
|
---|
| 146 | #if !ENABLE_DEBUG_INIT
|
---|
| 147 | static sig_atomic_t got_cont = 0;
|
---|
| 148 | #endif
|
---|
| 149 |
|
---|
| 150 | enum {
|
---|
| 151 | LOG = 0x1,
|
---|
| 152 | CONSOLE = 0x2,
|
---|
| 153 |
|
---|
| 154 | #if defined CONFIG_FEATURE_EXTRA_QUIET
|
---|
| 155 | MAYBE_CONSOLE = 0x0,
|
---|
| 156 | #else
|
---|
| 157 | MAYBE_CONSOLE = CONSOLE,
|
---|
| 158 | #endif
|
---|
| 159 |
|
---|
| 160 | #ifndef RB_HALT_SYSTEM
|
---|
| 161 | RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
|
---|
| 162 | RB_ENABLE_CAD = 0x89abcdef,
|
---|
| 163 | RB_DISABLE_CAD = 0,
|
---|
| 164 | RB_POWER_OFF = 0x4321fedc,
|
---|
| 165 | RB_AUTOBOOT = 0x01234567,
|
---|
| 166 | #endif
|
---|
| 167 | };
|
---|
| 168 |
|
---|
| 169 | static const char * const environment[] = {
|
---|
| 170 | "HOME=/",
|
---|
| 171 | "PATH=" _PATH_STDPATH,
|
---|
| 172 | "SHELL=/bin/sh",
|
---|
| 173 | "USER=root",
|
---|
| 174 | NULL
|
---|
| 175 | };
|
---|
| 176 |
|
---|
| 177 | /* Function prototypes */
|
---|
| 178 | static void delete_init_action(struct init_action *a);
|
---|
| 179 | static int waitfor(const struct init_action *a, pid_t pid);
|
---|
| 180 | #if !ENABLE_DEBUG_INIT
|
---|
| 181 | static void shutdown_signal(int sig);
|
---|
| 182 | #endif
|
---|
| 183 |
|
---|
| 184 | static void loop_forever(void)
|
---|
| 185 | {
|
---|
| 186 | while (1)
|
---|
| 187 | sleep(1);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /* Print a message to the specified device.
|
---|
| 191 | * Device may be bitwise-or'd from LOG | CONSOLE */
|
---|
| 192 | #if ENABLE_DEBUG_INIT
|
---|
| 193 | #define messageD message
|
---|
| 194 | #else
|
---|
| 195 | static inline void messageD(int ATTRIBUTE_UNUSED device,
|
---|
| 196 | const char ATTRIBUTE_UNUSED *fmt, ...)
|
---|
| 197 | {
|
---|
| 198 | }
|
---|
| 199 | #endif
|
---|
| 200 | static void message(int device, const char *fmt, ...)
|
---|
| 201 | __attribute__ ((format(printf, 2, 3)));
|
---|
| 202 | static void message(int device, const char *fmt, ...)
|
---|
| 203 | {
|
---|
| 204 | va_list arguments;
|
---|
| 205 | int l;
|
---|
| 206 | RESERVE_CONFIG_BUFFER(msg, 1024);
|
---|
| 207 | #ifndef CONFIG_SYSLOGD
|
---|
| 208 | static int log_fd = -1;
|
---|
| 209 | #endif
|
---|
| 210 |
|
---|
| 211 | msg[0] = '\r';
|
---|
| 212 | va_start(arguments, fmt);
|
---|
| 213 | l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
|
---|
| 214 | va_end(arguments);
|
---|
| 215 |
|
---|
| 216 | #ifdef CONFIG_SYSLOGD
|
---|
| 217 | /* Log the message to syslogd */
|
---|
| 218 | if (device & LOG) {
|
---|
| 219 | /* don`t out "\r\n" */
|
---|
| 220 | openlog(bb_applet_name, 0, LOG_DAEMON);
|
---|
| 221 | syslog(LOG_INFO, "%s", msg + 1);
|
---|
| 222 | closelog();
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | msg[l++] = '\n';
|
---|
| 226 | msg[l] = 0;
|
---|
| 227 | #else
|
---|
| 228 |
|
---|
| 229 | msg[l++] = '\n';
|
---|
| 230 | msg[l] = 0;
|
---|
| 231 | /* Take full control of the log tty, and never close it.
|
---|
| 232 | * It's mine, all mine! Muhahahaha! */
|
---|
| 233 | if (log_fd < 0) {
|
---|
| 234 | if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
|
---|
| 235 | log_fd = -2;
|
---|
| 236 | bb_error_msg("Bummer, can't write to log on %s!", log_console);
|
---|
| 237 | device = CONSOLE;
|
---|
| 238 | } else {
|
---|
| 239 | fcntl(log_fd, F_SETFD, FD_CLOEXEC);
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | if ((device & LOG) && (log_fd >= 0)) {
|
---|
| 243 | bb_full_write(log_fd, msg, l);
|
---|
| 244 | }
|
---|
| 245 | #endif
|
---|
| 246 |
|
---|
| 247 | if (device & CONSOLE) {
|
---|
| 248 | int fd = device_open(CONSOLE_DEV,
|
---|
| 249 | O_WRONLY | O_NOCTTY | O_NONBLOCK);
|
---|
| 250 | /* Always send console messages to /dev/console so people will see them. */
|
---|
| 251 | if (fd >= 0) {
|
---|
| 252 | bb_full_write(fd, msg, l);
|
---|
| 253 | close(fd);
|
---|
| 254 | #if ENABLE_DEBUG_INIT
|
---|
| 255 | /* all descriptors may be closed */
|
---|
| 256 | } else {
|
---|
| 257 | bb_error_msg("Bummer, can't print: ");
|
---|
| 258 | va_start(arguments, fmt);
|
---|
| 259 | vfprintf(stderr, fmt, arguments);
|
---|
| 260 | va_end(arguments);
|
---|
| 261 | #endif
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | RELEASE_CONFIG_BUFFER(msg);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | /* Set terminal settings to reasonable defaults */
|
---|
| 268 | static void set_term(void)
|
---|
| 269 | {
|
---|
| 270 | struct termios tty;
|
---|
| 271 |
|
---|
| 272 | tcgetattr(STDIN_FILENO, &tty);
|
---|
| 273 |
|
---|
| 274 | /* set control chars */
|
---|
| 275 | tty.c_cc[VINTR] = 3; /* C-c */
|
---|
| 276 | tty.c_cc[VQUIT] = 28; /* C-\ */
|
---|
| 277 | tty.c_cc[VERASE] = 127; /* C-? */
|
---|
| 278 | tty.c_cc[VKILL] = 21; /* C-u */
|
---|
| 279 | tty.c_cc[VEOF] = 4; /* C-d */
|
---|
| 280 | tty.c_cc[VSTART] = 17; /* C-q */
|
---|
| 281 | tty.c_cc[VSTOP] = 19; /* C-s */
|
---|
| 282 | tty.c_cc[VSUSP] = 26; /* C-z */
|
---|
| 283 |
|
---|
| 284 | /* use line dicipline 0 */
|
---|
| 285 | tty.c_line = 0;
|
---|
| 286 |
|
---|
| 287 | /* Make it be sane */
|
---|
| 288 | tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
|
---|
| 289 | tty.c_cflag |= CREAD | HUPCL | CLOCAL;
|
---|
| 290 |
|
---|
| 291 |
|
---|
| 292 | /* input modes */
|
---|
| 293 | tty.c_iflag = ICRNL | IXON | IXOFF;
|
---|
| 294 |
|
---|
| 295 | /* output modes */
|
---|
| 296 | tty.c_oflag = OPOST | ONLCR;
|
---|
| 297 |
|
---|
| 298 | /* local modes */
|
---|
| 299 | tty.c_lflag =
|
---|
| 300 | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
|
---|
| 301 |
|
---|
| 302 | tcsetattr(STDIN_FILENO, TCSANOW, &tty);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | static void console_init(void)
|
---|
| 306 | {
|
---|
| 307 | int fd;
|
---|
| 308 | int tried = 0;
|
---|
| 309 | struct vt_stat vt;
|
---|
| 310 | struct serial_struct sr;
|
---|
| 311 | char *s;
|
---|
| 312 |
|
---|
| 313 | if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
|
---|
| 314 | safe_strncpy(console, s, sizeof(console));
|
---|
| 315 | #if 0 /* #cpu(sparc) */
|
---|
| 316 | /* sparc kernel supports console=tty[ab] parameter which is also
|
---|
| 317 | * passed to init, so catch it here */
|
---|
| 318 | /* remap tty[ab] to /dev/ttyS[01] */
|
---|
| 319 | if (strcmp(s, "ttya") == 0)
|
---|
| 320 | safe_strncpy(console, SC_0, sizeof(console));
|
---|
| 321 | else if (strcmp(s, "ttyb") == 0)
|
---|
| 322 | safe_strncpy(console, SC_1, sizeof(console));
|
---|
| 323 | #endif
|
---|
| 324 | } else {
|
---|
| 325 | /* 2.2 kernels: identify the real console backend and try to use it */
|
---|
| 326 | if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
|
---|
| 327 | /* this is a serial console */
|
---|
| 328 | snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
|
---|
| 329 | } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
|
---|
| 330 | /* this is linux virtual tty */
|
---|
| 331 | snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
|
---|
| 332 | } else {
|
---|
| 333 | safe_strncpy(console, CONSOLE_DEV, sizeof(console));
|
---|
| 334 | tried++;
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
|
---|
| 339 | /* Can't open selected console -- try
|
---|
| 340 | logical system console and VT_MASTER */
|
---|
| 341 | safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC),
|
---|
| 342 | sizeof(console));
|
---|
| 343 | tried++;
|
---|
| 344 | }
|
---|
| 345 | if (fd < 0) {
|
---|
| 346 | /* Perhaps we should panic here? */
|
---|
| 347 | #ifndef CONFIG_SYSLOGD
|
---|
| 348 | log_console =
|
---|
| 349 | #endif
|
---|
| 350 | safe_strncpy(console, bb_dev_null, sizeof(console));
|
---|
| 351 | } else {
|
---|
| 352 | s = getenv("TERM");
|
---|
| 353 | /* check for serial console */
|
---|
| 354 | if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
|
---|
| 355 | /* Force the TERM setting to vt102 for serial console --
|
---|
| 356 | * if TERM is set to linux (the default) */
|
---|
| 357 | if (s == NULL || strcmp(s, "linux") == 0)
|
---|
| 358 | putenv("TERM=vt102");
|
---|
| 359 | #ifndef CONFIG_SYSLOGD
|
---|
| 360 | log_console = console;
|
---|
| 361 | #endif
|
---|
| 362 | } else {
|
---|
| 363 | if (s == NULL)
|
---|
| 364 | putenv("TERM=linux");
|
---|
| 365 | }
|
---|
| 366 | close(fd);
|
---|
| 367 | }
|
---|
| 368 | messageD(LOG, "console=%s", console);
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | static void fixup_argv(int argc, char **argv, char *new_argv0)
|
---|
| 372 | {
|
---|
| 373 | int len;
|
---|
| 374 |
|
---|
| 375 | /* Fix up argv[0] to be certain we claim to be init */
|
---|
| 376 | len = strlen(argv[0]);
|
---|
| 377 | memset(argv[0], 0, len);
|
---|
| 378 | safe_strncpy(argv[0], new_argv0, len + 1);
|
---|
| 379 |
|
---|
| 380 | /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
|
---|
| 381 | len = 1;
|
---|
| 382 | while (argc > len) {
|
---|
| 383 | memset(argv[len], 0, strlen(argv[len]));
|
---|
| 384 | len++;
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | /* Open the new terminal device */
|
---|
| 389 | static void open_new_terminal(const char * const device, const int fail) {
|
---|
| 390 | struct stat sb;
|
---|
| 391 |
|
---|
| 392 | if ((device_open(device, O_RDWR)) < 0) {
|
---|
| 393 | if (stat(device, &sb) != 0) {
|
---|
| 394 | message(LOG | CONSOLE, "device '%s' does not exist.", device);
|
---|
| 395 | } else {
|
---|
| 396 | message(LOG | CONSOLE, "Bummer, can't open %s", device);
|
---|
| 397 | }
|
---|
| 398 | if (fail)
|
---|
| 399 | _exit(1);
|
---|
| 400 | /* else */
|
---|
| 401 | #if !ENABLE_DEBUG_INIT
|
---|
| 402 | shutdown_signal(SIGUSR1);
|
---|
| 403 | #else
|
---|
| 404 | _exit(2);
|
---|
| 405 | #endif
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | static pid_t run(const struct init_action *a)
|
---|
| 410 | {
|
---|
| 411 | int i;
|
---|
| 412 | pid_t pid;
|
---|
| 413 | char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
|
---|
| 414 | char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
|
---|
| 415 | sigset_t nmask, omask;
|
---|
| 416 | static const char press_enter[] =
|
---|
| 417 | #ifdef CUSTOMIZED_BANNER
|
---|
| 418 | #include CUSTOMIZED_BANNER
|
---|
| 419 | #endif
|
---|
| 420 | "\nPlease press Enter to activate this console. ";
|
---|
| 421 |
|
---|
| 422 | /* Block sigchild while forking. */
|
---|
| 423 | sigemptyset(&nmask);
|
---|
| 424 | sigaddset(&nmask, SIGCHLD);
|
---|
| 425 | sigprocmask(SIG_BLOCK, &nmask, &omask);
|
---|
| 426 |
|
---|
| 427 | if ((pid = fork()) == 0) {
|
---|
| 428 |
|
---|
| 429 | /* Clean up */
|
---|
| 430 | close(0);
|
---|
| 431 | close(1);
|
---|
| 432 | close(2);
|
---|
| 433 | sigprocmask(SIG_SETMASK, &omask, NULL);
|
---|
| 434 |
|
---|
| 435 | /* Reset signal handlers that were set by the parent process */
|
---|
| 436 | signal(SIGUSR1, SIG_DFL);
|
---|
| 437 | signal(SIGUSR2, SIG_DFL);
|
---|
| 438 | signal(SIGINT, SIG_DFL);
|
---|
| 439 | signal(SIGTERM, SIG_DFL);
|
---|
| 440 | signal(SIGHUP, SIG_DFL);
|
---|
| 441 | signal(SIGQUIT, SIG_DFL);
|
---|
| 442 | signal(SIGCONT, SIG_DFL);
|
---|
| 443 | signal(SIGSTOP, SIG_DFL);
|
---|
| 444 | signal(SIGTSTP, SIG_DFL);
|
---|
| 445 |
|
---|
| 446 | /* Create a new session and make ourself the process
|
---|
| 447 | * group leader */
|
---|
| 448 | setsid();
|
---|
| 449 |
|
---|
| 450 | /* Open the new terminal device */
|
---|
| 451 | open_new_terminal(a->terminal, 1);
|
---|
| 452 |
|
---|
| 453 | /* Make sure the terminal will act fairly normal for us */
|
---|
| 454 | set_term();
|
---|
| 455 | /* Setup stdout, stderr for the new process so
|
---|
| 456 | * they point to the supplied terminal */
|
---|
| 457 | dup(0);
|
---|
| 458 | dup(0);
|
---|
| 459 |
|
---|
| 460 | /* If the init Action requires us to wait, then force the
|
---|
| 461 | * supplied terminal to be the controlling tty. */
|
---|
| 462 | if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
|
---|
| 463 |
|
---|
| 464 | /* Now fork off another process to just hang around */
|
---|
| 465 | if ((pid = fork()) < 0) {
|
---|
| 466 | message(LOG | CONSOLE, "Can't fork!");
|
---|
| 467 | _exit(1);
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | if (pid > 0) {
|
---|
| 471 |
|
---|
| 472 | /* We are the parent -- wait till the child is done */
|
---|
| 473 | signal(SIGINT, SIG_IGN);
|
---|
| 474 | signal(SIGTSTP, SIG_IGN);
|
---|
| 475 | signal(SIGQUIT, SIG_IGN);
|
---|
| 476 | signal(SIGCHLD, SIG_DFL);
|
---|
| 477 |
|
---|
| 478 | waitfor(NULL, pid);
|
---|
| 479 | /* See if stealing the controlling tty back is necessary */
|
---|
| 480 | if (tcgetpgrp(0) != getpid())
|
---|
| 481 | _exit(0);
|
---|
| 482 |
|
---|
| 483 | /* Use a temporary process to steal the controlling tty. */
|
---|
| 484 | if ((pid = fork()) < 0) {
|
---|
| 485 | message(LOG | CONSOLE, "Can't fork!");
|
---|
| 486 | _exit(1);
|
---|
| 487 | }
|
---|
| 488 | if (pid == 0) {
|
---|
| 489 | setsid();
|
---|
| 490 | ioctl(0, TIOCSCTTY, 1);
|
---|
| 491 | _exit(0);
|
---|
| 492 | }
|
---|
| 493 | waitfor(NULL, pid);
|
---|
| 494 | _exit(0);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | /* Now fall though to actually execute things */
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | /* See if any special /bin/sh requiring characters are present */
|
---|
| 501 | if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
|
---|
| 502 | cmd[0] = (char *)DEFAULT_SHELL;
|
---|
| 503 | cmd[1] = "-c";
|
---|
| 504 | cmd[2] = strcat(strcpy(buf, "exec "), a->command);
|
---|
| 505 | cmd[3] = NULL;
|
---|
| 506 | } else {
|
---|
| 507 | /* Convert command (char*) into cmd (char**, one word per string) */
|
---|
| 508 | strcpy(buf, a->command);
|
---|
| 509 | s = buf;
|
---|
| 510 | for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
|
---|
| 511 | if (*tmpCmd != '\0') {
|
---|
| 512 | cmd[i] = tmpCmd;
|
---|
| 513 | i++;
|
---|
| 514 | }
|
---|
| 515 | }
|
---|
| 516 | cmd[i] = NULL;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | cmdpath = cmd[0];
|
---|
| 520 |
|
---|
| 521 | /*
|
---|
| 522 | Interactive shells want to see a dash in argv[0]. This
|
---|
| 523 | typically is handled by login, argv will be setup this
|
---|
| 524 | way if a dash appears at the front of the command path
|
---|
| 525 | (like "-/bin/sh").
|
---|
| 526 | */
|
---|
| 527 |
|
---|
| 528 | if (*cmdpath == '-') {
|
---|
| 529 |
|
---|
| 530 | /* skip over the dash */
|
---|
| 531 | ++cmdpath;
|
---|
| 532 |
|
---|
| 533 | /* find the last component in the command pathname */
|
---|
| 534 | s = bb_get_last_path_component(cmdpath);
|
---|
| 535 |
|
---|
| 536 | /* make a new argv[0] */
|
---|
| 537 | if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
|
---|
| 538 | message(LOG | CONSOLE, bb_msg_memory_exhausted);
|
---|
| 539 | cmd[0] = cmdpath;
|
---|
| 540 | } else {
|
---|
| 541 | cmd[0][0] = '-';
|
---|
| 542 | strcpy(cmd[0] + 1, s);
|
---|
| 543 | }
|
---|
| 544 | #ifdef CONFIG_FEATURE_INIT_SCTTY
|
---|
| 545 | /* Establish this process as session leader and
|
---|
| 546 | * (attempt) to make the tty (if any) a controlling tty.
|
---|
| 547 | */
|
---|
| 548 | (void) setsid();
|
---|
| 549 | (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
|
---|
| 550 | #endif
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 | #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
|
---|
| 554 | if (a->action & ASKFIRST) {
|
---|
| 555 | char c;
|
---|
| 556 | /*
|
---|
| 557 | * Save memory by not exec-ing anything large (like a shell)
|
---|
| 558 | * before the user wants it. This is critical if swap is not
|
---|
| 559 | * enabled and the system has low memory. Generally this will
|
---|
| 560 | * be run on the second virtual console, and the first will
|
---|
| 561 | * be allowed to start a shell or whatever an init script
|
---|
| 562 | * specifies.
|
---|
| 563 | */
|
---|
| 564 | messageD(LOG, "Waiting for enter to start '%s'"
|
---|
| 565 | "(pid %d, terminal %s)\n",
|
---|
| 566 | cmdpath, getpid(), a->terminal);
|
---|
| 567 | bb_full_write(1, press_enter, sizeof(press_enter) - 1);
|
---|
| 568 | while(read(0, &c, 1) == 1 && c != '\n')
|
---|
| 569 | ;
|
---|
| 570 | }
|
---|
| 571 | #endif
|
---|
| 572 |
|
---|
| 573 | /* Log the process name and args */
|
---|
| 574 | message(LOG, "Starting pid %d, console %s: '%s'",
|
---|
| 575 | getpid(), a->terminal, cmdpath);
|
---|
| 576 |
|
---|
| 577 | #if defined CONFIG_FEATURE_INIT_COREDUMPS
|
---|
| 578 | {
|
---|
| 579 | struct stat sb;
|
---|
| 580 | if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
|
---|
| 581 | struct rlimit limit;
|
---|
| 582 |
|
---|
| 583 | limit.rlim_cur = RLIM_INFINITY;
|
---|
| 584 | limit.rlim_max = RLIM_INFINITY;
|
---|
| 585 | setrlimit(RLIMIT_CORE, &limit);
|
---|
| 586 | }
|
---|
| 587 | }
|
---|
| 588 | #endif
|
---|
| 589 |
|
---|
| 590 | /* Now run it. The new program will take over this PID,
|
---|
| 591 | * so nothing further in init.c should be run. */
|
---|
| 592 | execv(cmdpath, cmd);
|
---|
| 593 |
|
---|
| 594 | /* We're still here? Some error happened. */
|
---|
| 595 | message(LOG | CONSOLE, "Bummer, could not run '%s': %m", cmdpath);
|
---|
| 596 | _exit(-1);
|
---|
| 597 | }
|
---|
| 598 | sigprocmask(SIG_SETMASK, &omask, NULL);
|
---|
| 599 | return pid;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 | static int waitfor(const struct init_action *a, pid_t pid)
|
---|
| 603 | {
|
---|
| 604 | int runpid;
|
---|
| 605 | int status, wpid;
|
---|
| 606 |
|
---|
| 607 | runpid = (NULL == a)? pid : run(a);
|
---|
| 608 | while (1) {
|
---|
| 609 | wpid = waitpid(runpid,&status,0);
|
---|
| 610 | if (wpid == runpid)
|
---|
| 611 | break;
|
---|
| 612 | if (wpid == -1 && errno == ECHILD) {
|
---|
| 613 | /* we missed its termination */
|
---|
| 614 | break;
|
---|
| 615 | }
|
---|
| 616 | /* FIXME other errors should maybe trigger an error, but allow
|
---|
| 617 | * the program to continue */
|
---|
| 618 | }
|
---|
| 619 | return wpid;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | /* Run all commands of a particular type */
|
---|
| 623 | static void run_actions(int action)
|
---|
| 624 | {
|
---|
| 625 | struct init_action *a, *tmp;
|
---|
| 626 |
|
---|
| 627 | for (a = init_action_list; a; a = tmp) {
|
---|
| 628 | tmp = a->next;
|
---|
| 629 | if (a->action == action) {
|
---|
| 630 | if (access(a->terminal, R_OK | W_OK)) {
|
---|
| 631 | delete_init_action(a);
|
---|
| 632 | } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
|
---|
| 633 | waitfor(a, 0);
|
---|
| 634 | delete_init_action(a);
|
---|
| 635 | } else if (a->action & ONCE) {
|
---|
| 636 | run(a);
|
---|
| 637 | delete_init_action(a);
|
---|
| 638 | } else if (a->action & (RESPAWN | ASKFIRST)) {
|
---|
| 639 | /* Only run stuff with pid==0. If they have
|
---|
| 640 | * a pid, that means it is still running */
|
---|
| 641 | if (a->pid == 0) {
|
---|
| 642 | a->pid = run(a);
|
---|
| 643 | }
|
---|
| 644 | }
|
---|
| 645 | }
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | #if !ENABLE_DEBUG_INIT
|
---|
| 650 | static void init_reboot(unsigned long magic)
|
---|
| 651 | {
|
---|
| 652 | pid_t pid;
|
---|
| 653 | /* We have to fork here, since the kernel calls do_exit(0) in
|
---|
| 654 | * linux/kernel/sys.c, which can cause the machine to panic when
|
---|
| 655 | * the init process is killed.... */
|
---|
| 656 | if ((pid = fork()) == 0) {
|
---|
| 657 | reboot(magic);
|
---|
| 658 | _exit(0);
|
---|
| 659 | }
|
---|
| 660 | waitpid (pid, NULL, 0);
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | static void shutdown_system(void)
|
---|
| 664 | {
|
---|
| 665 | sigset_t block_signals;
|
---|
| 666 |
|
---|
| 667 | /* run everything to be run at "shutdown". This is done _prior_
|
---|
| 668 | * to killing everything, in case people wish to use scripts to
|
---|
| 669 | * shut things down gracefully... */
|
---|
| 670 | run_actions(SHUTDOWN);
|
---|
| 671 |
|
---|
| 672 | /* first disable all our signals */
|
---|
| 673 | sigemptyset(&block_signals);
|
---|
| 674 | sigaddset(&block_signals, SIGHUP);
|
---|
| 675 | sigaddset(&block_signals, SIGQUIT);
|
---|
| 676 | sigaddset(&block_signals, SIGCHLD);
|
---|
| 677 | sigaddset(&block_signals, SIGUSR1);
|
---|
| 678 | sigaddset(&block_signals, SIGUSR2);
|
---|
| 679 | sigaddset(&block_signals, SIGINT);
|
---|
| 680 | sigaddset(&block_signals, SIGTERM);
|
---|
| 681 | sigaddset(&block_signals, SIGCONT);
|
---|
| 682 | sigaddset(&block_signals, SIGSTOP);
|
---|
| 683 | sigaddset(&block_signals, SIGTSTP);
|
---|
| 684 | sigprocmask(SIG_BLOCK, &block_signals, NULL);
|
---|
| 685 |
|
---|
| 686 | /* Allow Ctrl-Alt-Del to reboot system. */
|
---|
| 687 | init_reboot(RB_ENABLE_CAD);
|
---|
| 688 |
|
---|
| 689 | message(CONSOLE | LOG, "The system is going down NOW !!");
|
---|
| 690 | sync();
|
---|
| 691 |
|
---|
| 692 | /* Send signals to every process _except_ pid 1 */
|
---|
| 693 | message(CONSOLE | LOG, init_sending_format, "TERM");
|
---|
| 694 | kill(-1, SIGTERM);
|
---|
| 695 | sleep(1);
|
---|
| 696 | sync();
|
---|
| 697 |
|
---|
| 698 | message(CONSOLE | LOG, init_sending_format, "KILL");
|
---|
| 699 | kill(-1, SIGKILL);
|
---|
| 700 | sleep(1);
|
---|
| 701 |
|
---|
| 702 | sync();
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | static void exec_signal(int sig ATTRIBUTE_UNUSED)
|
---|
| 706 | {
|
---|
| 707 | struct init_action *a, *tmp;
|
---|
| 708 | sigset_t unblock_signals;
|
---|
| 709 |
|
---|
| 710 | for (a = init_action_list; a; a = tmp) {
|
---|
| 711 | tmp = a->next;
|
---|
| 712 | if (a->action & RESTART) {
|
---|
| 713 | shutdown_system();
|
---|
| 714 |
|
---|
| 715 | /* unblock all signals, blocked in shutdown_system() */
|
---|
| 716 | sigemptyset(&unblock_signals);
|
---|
| 717 | sigaddset(&unblock_signals, SIGHUP);
|
---|
| 718 | sigaddset(&unblock_signals, SIGQUIT);
|
---|
| 719 | sigaddset(&unblock_signals, SIGCHLD);
|
---|
| 720 | sigaddset(&unblock_signals, SIGUSR1);
|
---|
| 721 | sigaddset(&unblock_signals, SIGUSR2);
|
---|
| 722 | sigaddset(&unblock_signals, SIGINT);
|
---|
| 723 | sigaddset(&unblock_signals, SIGTERM);
|
---|
| 724 | sigaddset(&unblock_signals, SIGCONT);
|
---|
| 725 | sigaddset(&unblock_signals, SIGSTOP);
|
---|
| 726 | sigaddset(&unblock_signals, SIGTSTP);
|
---|
| 727 | sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
|
---|
| 728 |
|
---|
| 729 | /* Close whatever files are open. */
|
---|
| 730 | close(0);
|
---|
| 731 | close(1);
|
---|
| 732 | close(2);
|
---|
| 733 |
|
---|
| 734 | /* Open the new terminal device */
|
---|
| 735 | open_new_terminal(a->terminal, 0);
|
---|
| 736 |
|
---|
| 737 | /* Make sure the terminal will act fairly normal for us */
|
---|
| 738 | set_term();
|
---|
| 739 | /* Setup stdout, stderr on the supplied terminal */
|
---|
| 740 | dup(0);
|
---|
| 741 | dup(0);
|
---|
| 742 |
|
---|
| 743 | messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command);
|
---|
| 744 | execl(a->command, a->command, NULL);
|
---|
| 745 |
|
---|
| 746 | message(CONSOLE | LOG, "exec of '%s' failed: %m",
|
---|
| 747 | a->command);
|
---|
| 748 | sync();
|
---|
| 749 | sleep(2);
|
---|
| 750 | init_reboot(RB_HALT_SYSTEM);
|
---|
| 751 | loop_forever();
|
---|
| 752 | }
|
---|
| 753 | }
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | static void shutdown_signal(int sig)
|
---|
| 757 | {
|
---|
| 758 | char *m;
|
---|
| 759 | int rb;
|
---|
| 760 |
|
---|
| 761 | shutdown_system();
|
---|
| 762 |
|
---|
| 763 | if (sig == SIGTERM) {
|
---|
| 764 | m = "reboot";
|
---|
| 765 | rb = RB_AUTOBOOT;
|
---|
| 766 | } else if (sig == SIGUSR2) {
|
---|
| 767 | m = "poweroff";
|
---|
| 768 | rb = RB_POWER_OFF;
|
---|
| 769 | } else {
|
---|
| 770 | m = "halt";
|
---|
| 771 | rb = RB_HALT_SYSTEM;
|
---|
| 772 | }
|
---|
| 773 | message(CONSOLE | LOG, "Requesting system %s.", m);
|
---|
| 774 | sync();
|
---|
| 775 |
|
---|
| 776 | /* allow time for last message to reach serial console */
|
---|
| 777 | sleep(2);
|
---|
| 778 |
|
---|
| 779 | init_reboot(rb);
|
---|
| 780 |
|
---|
| 781 | loop_forever();
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 | static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
|
---|
| 785 | {
|
---|
| 786 | run_actions(CTRLALTDEL);
|
---|
| 787 | }
|
---|
| 788 |
|
---|
| 789 | /* The SIGSTOP & SIGTSTP handler */
|
---|
| 790 | static void stop_handler(int sig ATTRIBUTE_UNUSED)
|
---|
| 791 | {
|
---|
| 792 | int saved_errno = errno;
|
---|
| 793 |
|
---|
| 794 | got_cont = 0;
|
---|
| 795 | while (!got_cont)
|
---|
| 796 | pause();
|
---|
| 797 | got_cont = 0;
|
---|
| 798 | errno = saved_errno;
|
---|
| 799 | }
|
---|
| 800 |
|
---|
| 801 | /* The SIGCONT handler */
|
---|
| 802 | static void cont_handler(int sig ATTRIBUTE_UNUSED)
|
---|
| 803 | {
|
---|
| 804 | got_cont = 1;
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | #endif /* ! ENABLE_DEBUG_INIT */
|
---|
| 808 |
|
---|
| 809 | static void new_init_action(int action, const char *command, const char *cons)
|
---|
| 810 | {
|
---|
| 811 | struct init_action *new_action, *a, *last;
|
---|
| 812 |
|
---|
| 813 | if (*cons == '\0')
|
---|
| 814 | cons = console;
|
---|
| 815 |
|
---|
| 816 | if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
|
---|
| 817 | return;
|
---|
| 818 |
|
---|
| 819 | new_action = calloc((size_t) (1), sizeof(struct init_action));
|
---|
| 820 | if (!new_action) {
|
---|
| 821 | message(LOG | CONSOLE, "Memory allocation failure");
|
---|
| 822 | loop_forever();
|
---|
| 823 | }
|
---|
| 824 |
|
---|
| 825 | /* Append to the end of the list */
|
---|
| 826 | for (a = last = init_action_list; a; a = a->next) {
|
---|
| 827 | /* don't enter action if it's already in the list,
|
---|
| 828 | * but do overwrite existing actions */
|
---|
| 829 | if ((strcmp(a->command, command) == 0) &&
|
---|
| 830 | (strcmp(a->terminal, cons) ==0)) {
|
---|
| 831 | a->action = action;
|
---|
| 832 | free(new_action);
|
---|
| 833 | return;
|
---|
| 834 | }
|
---|
| 835 | last = a;
|
---|
| 836 | }
|
---|
| 837 | if (last) {
|
---|
| 838 | last->next = new_action;
|
---|
| 839 | } else {
|
---|
| 840 | init_action_list = new_action;
|
---|
| 841 | }
|
---|
| 842 | strcpy(new_action->command, command);
|
---|
| 843 | new_action->action = action;
|
---|
| 844 | strcpy(new_action->terminal, cons);
|
---|
| 845 | #if 0 /* calloc zeroed always */
|
---|
| 846 | new_action->pid = 0;
|
---|
| 847 | #endif
|
---|
| 848 | messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
|
---|
| 849 | new_action->command, new_action->action, new_action->terminal);
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | static void delete_init_action(struct init_action *action)
|
---|
| 853 | {
|
---|
| 854 | struct init_action *a, *b = NULL;
|
---|
| 855 |
|
---|
| 856 | for (a = init_action_list; a; b = a, a = a->next) {
|
---|
| 857 | if (a == action) {
|
---|
| 858 | if (b == NULL) {
|
---|
| 859 | init_action_list = a->next;
|
---|
| 860 | } else {
|
---|
| 861 | b->next = a->next;
|
---|
| 862 | }
|
---|
| 863 | free(a);
|
---|
| 864 | break;
|
---|
| 865 | }
|
---|
| 866 | }
|
---|
| 867 | }
|
---|
| 868 |
|
---|
| 869 | /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
|
---|
| 870 | * then parse_inittab() simply adds in some default
|
---|
| 871 | * actions(i.e., runs INIT_SCRIPT and then starts a pair
|
---|
| 872 | * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
|
---|
| 873 | * _is_ defined, but /etc/inittab is missing, this
|
---|
| 874 | * results in the same set of default behaviors.
|
---|
| 875 | */
|
---|
| 876 | static void parse_inittab(void)
|
---|
| 877 | {
|
---|
| 878 | #ifdef CONFIG_FEATURE_USE_INITTAB
|
---|
| 879 | FILE *file;
|
---|
| 880 | char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
|
---|
| 881 | char tmpConsole[CONSOLE_BUFF_SIZE];
|
---|
| 882 | char *id, *runlev, *action, *command, *eol;
|
---|
| 883 | const struct init_action_type *a = actions;
|
---|
| 884 |
|
---|
| 885 |
|
---|
| 886 | file = fopen(INITTAB, "r");
|
---|
| 887 | if (file == NULL) {
|
---|
| 888 | /* No inittab file -- set up some default behavior */
|
---|
| 889 | #endif
|
---|
| 890 | /* Reboot on Ctrl-Alt-Del */
|
---|
| 891 | new_init_action(CTRLALTDEL, "/sbin/reboot", "");
|
---|
| 892 | /* Umount all filesystems on halt/reboot */
|
---|
| 893 | new_init_action(SHUTDOWN, "/bin/umount -a -r", "");
|
---|
| 894 | /* Swapoff on halt/reboot */
|
---|
| 895 | if(ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "/sbin/swapoff -a", "");
|
---|
| 896 | /* Prepare to restart init when a HUP is received */
|
---|
| 897 | new_init_action(RESTART, "/sbin/init", "");
|
---|
| 898 | /* Askfirst shell on tty1-4 */
|
---|
| 899 | new_init_action(ASKFIRST, bb_default_login_shell, "");
|
---|
| 900 | new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
|
---|
| 901 | new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
|
---|
| 902 | new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
|
---|
| 903 | /* sysinit */
|
---|
| 904 | new_init_action(SYSINIT, INIT_SCRIPT, "");
|
---|
| 905 |
|
---|
| 906 | return;
|
---|
| 907 | #ifdef CONFIG_FEATURE_USE_INITTAB
|
---|
| 908 | }
|
---|
| 909 |
|
---|
| 910 | while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
|
---|
| 911 | /* Skip leading spaces */
|
---|
| 912 | for (id = buf; *id == ' ' || *id == '\t'; id++);
|
---|
| 913 |
|
---|
| 914 | /* Skip the line if it's a comment */
|
---|
| 915 | if (*id == '#' || *id == '\n')
|
---|
| 916 | continue;
|
---|
| 917 |
|
---|
| 918 | /* Trim the trailing \n */
|
---|
| 919 | eol = strrchr(id, '\n');
|
---|
| 920 | if (eol != NULL)
|
---|
| 921 | *eol = '\0';
|
---|
| 922 |
|
---|
| 923 | /* Keep a copy around for posterity's sake (and error msgs) */
|
---|
| 924 | strcpy(lineAsRead, buf);
|
---|
| 925 |
|
---|
| 926 | /* Separate the ID field from the runlevels */
|
---|
| 927 | runlev = strchr(id, ':');
|
---|
| 928 | if (runlev == NULL || *(runlev + 1) == '\0') {
|
---|
| 929 | message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
|
---|
| 930 | continue;
|
---|
| 931 | } else {
|
---|
| 932 | *runlev = '\0';
|
---|
| 933 | ++runlev;
|
---|
| 934 | }
|
---|
| 935 |
|
---|
| 936 | /* Separate the runlevels from the action */
|
---|
| 937 | action = strchr(runlev, ':');
|
---|
| 938 | if (action == NULL || *(action + 1) == '\0') {
|
---|
| 939 | message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
|
---|
| 940 | continue;
|
---|
| 941 | } else {
|
---|
| 942 | *action = '\0';
|
---|
| 943 | ++action;
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | /* Separate the action from the command */
|
---|
| 947 | command = strchr(action, ':');
|
---|
| 948 | if (command == NULL || *(command + 1) == '\0') {
|
---|
| 949 | message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
|
---|
| 950 | continue;
|
---|
| 951 | } else {
|
---|
| 952 | *command = '\0';
|
---|
| 953 | ++command;
|
---|
| 954 | }
|
---|
| 955 |
|
---|
| 956 | /* Ok, now process it */
|
---|
| 957 | for (a = actions; a->name != 0; a++) {
|
---|
| 958 | if (strcmp(a->name, action) == 0) {
|
---|
| 959 | if (*id != '\0') {
|
---|
| 960 | if(strncmp(id, "/dev/", 5) == 0)
|
---|
| 961 | id += 5;
|
---|
| 962 | strcpy(tmpConsole, "/dev/");
|
---|
| 963 | safe_strncpy(tmpConsole + 5, id,
|
---|
| 964 | CONSOLE_BUFF_SIZE - 5);
|
---|
| 965 | id = tmpConsole;
|
---|
| 966 | }
|
---|
| 967 | new_init_action(a->action, command, id);
|
---|
| 968 | break;
|
---|
| 969 | }
|
---|
| 970 | }
|
---|
| 971 | if (a->name == 0) {
|
---|
| 972 | /* Choke on an unknown action */
|
---|
| 973 | message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
|
---|
| 974 | }
|
---|
| 975 | }
|
---|
| 976 | fclose(file);
|
---|
| 977 | return;
|
---|
| 978 | #endif /* CONFIG_FEATURE_USE_INITTAB */
|
---|
| 979 | }
|
---|
| 980 |
|
---|
| 981 | #ifdef CONFIG_FEATURE_USE_INITTAB
|
---|
| 982 | static void reload_signal(int sig ATTRIBUTE_UNUSED)
|
---|
| 983 | {
|
---|
| 984 | struct init_action *a, *tmp;
|
---|
| 985 |
|
---|
| 986 | message(LOG, "Reloading /etc/inittab");
|
---|
| 987 |
|
---|
| 988 | /* disable old entrys */
|
---|
| 989 | for (a = init_action_list; a; a = a->next ) {
|
---|
| 990 | a->action = ONCE;
|
---|
| 991 | }
|
---|
| 992 |
|
---|
| 993 | parse_inittab();
|
---|
| 994 |
|
---|
| 995 | /* remove unused entrys */
|
---|
| 996 | for (a = init_action_list; a; a = tmp) {
|
---|
| 997 | tmp = a->next;
|
---|
| 998 | if (a->action & (ONCE | SYSINIT | WAIT ) &&
|
---|
| 999 | a->pid == 0 ) {
|
---|
| 1000 | delete_init_action(a);
|
---|
| 1001 | }
|
---|
| 1002 | }
|
---|
| 1003 | run_actions(RESPAWN);
|
---|
| 1004 | return;
|
---|
| 1005 | }
|
---|
| 1006 | #endif /* CONFIG_FEATURE_USE_INITTAB */
|
---|
| 1007 |
|
---|
| 1008 | int init_main(int argc, char **argv)
|
---|
| 1009 | {
|
---|
| 1010 | struct init_action *a;
|
---|
| 1011 | pid_t wpid;
|
---|
| 1012 |
|
---|
| 1013 | if (argc > 1 && !strcmp(argv[1], "-q")) {
|
---|
| 1014 | return kill(1,SIGHUP);
|
---|
| 1015 | }
|
---|
| 1016 | #if !ENABLE_DEBUG_INIT
|
---|
| 1017 | /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
|
---|
| 1018 | if (getpid() != 1 &&
|
---|
| 1019 | (!ENABLE_FEATURE_INITRD || !strstr(bb_applet_name, "linuxrc")))
|
---|
| 1020 | {
|
---|
| 1021 | bb_show_usage();
|
---|
| 1022 | }
|
---|
| 1023 | /* Set up sig handlers -- be sure to
|
---|
| 1024 | * clear all of these in run() */
|
---|
| 1025 | signal(SIGHUP, exec_signal);
|
---|
| 1026 | signal(SIGQUIT, exec_signal);
|
---|
| 1027 | signal(SIGUSR1, shutdown_signal);
|
---|
| 1028 | signal(SIGUSR2, shutdown_signal);
|
---|
| 1029 | signal(SIGINT, ctrlaltdel_signal);
|
---|
| 1030 | signal(SIGTERM, shutdown_signal);
|
---|
| 1031 | signal(SIGCONT, cont_handler);
|
---|
| 1032 | signal(SIGSTOP, stop_handler);
|
---|
| 1033 | signal(SIGTSTP, stop_handler);
|
---|
| 1034 |
|
---|
| 1035 | /* Turn off rebooting via CTL-ALT-DEL -- we get a
|
---|
| 1036 | * SIGINT on CAD so we can shut things down gracefully... */
|
---|
| 1037 | init_reboot(RB_DISABLE_CAD);
|
---|
| 1038 | #endif
|
---|
| 1039 |
|
---|
| 1040 | /* Figure out where the default console should be */
|
---|
| 1041 | console_init();
|
---|
| 1042 |
|
---|
| 1043 | /* Close whatever files are open, and reset the console. */
|
---|
| 1044 | close(0);
|
---|
| 1045 | close(1);
|
---|
| 1046 | close(2);
|
---|
| 1047 |
|
---|
| 1048 | if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
|
---|
| 1049 | set_term();
|
---|
| 1050 | close(0);
|
---|
| 1051 | }
|
---|
| 1052 |
|
---|
| 1053 | chdir("/");
|
---|
| 1054 | setsid();
|
---|
| 1055 | {
|
---|
| 1056 | const char * const *e;
|
---|
| 1057 | /* Make sure environs is set to something sane */
|
---|
| 1058 | for(e = environment; *e; e++)
|
---|
| 1059 | putenv((char *) *e);
|
---|
| 1060 | }
|
---|
| 1061 | /* Hello world */
|
---|
| 1062 | message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
|
---|
| 1063 |
|
---|
| 1064 | /* Make sure there is enough memory to do something useful. */
|
---|
| 1065 | if (ENABLE_SWAPONOFF) {
|
---|
| 1066 | struct sysinfo info;
|
---|
| 1067 |
|
---|
| 1068 | if (!sysinfo(&info) &&
|
---|
| 1069 | (info.mem_unit ? : 1) * (long long)info.totalram < MEGABYTE)
|
---|
| 1070 | {
|
---|
| 1071 | message(CONSOLE,"Low memory: forcing swapon.");
|
---|
| 1072 | /* swapon -a requires /proc typically */
|
---|
| 1073 | new_init_action(SYSINIT, "/bin/mount -t proc proc /proc", "");
|
---|
| 1074 | /* Try to turn on swap */
|
---|
| 1075 | new_init_action(SYSINIT, "/sbin/swapon -a", "");
|
---|
| 1076 | run_actions(SYSINIT); /* wait and removing */
|
---|
| 1077 | }
|
---|
| 1078 | }
|
---|
| 1079 |
|
---|
| 1080 | /* Check if we are supposed to be in single user mode */
|
---|
| 1081 | if (argc > 1 && (!strcmp(argv[1], "single") ||
|
---|
| 1082 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
|
---|
| 1083 | /* Start a shell on console */
|
---|
| 1084 | new_init_action(RESPAWN, bb_default_login_shell, "");
|
---|
| 1085 | } else {
|
---|
| 1086 | /* Not in single user mode -- see what inittab says */
|
---|
| 1087 |
|
---|
| 1088 | /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
|
---|
| 1089 | * then parse_inittab() simply adds in some default
|
---|
| 1090 | * actions(i.e., runs INIT_SCRIPT and then starts a pair
|
---|
| 1091 | * of "askfirst" shells */
|
---|
| 1092 | parse_inittab();
|
---|
| 1093 | }
|
---|
| 1094 |
|
---|
| 1095 | #ifdef CONFIG_SELINUX
|
---|
| 1096 | if (getenv("SELINUX_INIT") == NULL) {
|
---|
| 1097 | int enforce = 0;
|
---|
| 1098 |
|
---|
| 1099 | putenv("SELINUX_INIT=YES");
|
---|
| 1100 | if (selinux_init_load_policy(&enforce) == 0) {
|
---|
| 1101 | execv(argv[0], argv);
|
---|
| 1102 | } else if (enforce > 0) {
|
---|
| 1103 | /* SELinux in enforcing mode but load_policy failed */
|
---|
| 1104 | /* At this point, we probably can't open /dev/console, so log() won't work */
|
---|
| 1105 | message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
|
---|
| 1106 | exit(1);
|
---|
| 1107 | }
|
---|
| 1108 | }
|
---|
| 1109 | #endif /* CONFIG_SELINUX */
|
---|
| 1110 |
|
---|
| 1111 | /* Make the command line just say "init" -- thats all, nothing else */
|
---|
| 1112 | fixup_argv(argc, argv, "init");
|
---|
| 1113 |
|
---|
| 1114 | /* Now run everything that needs to be run */
|
---|
| 1115 |
|
---|
| 1116 | /* First run the sysinit command */
|
---|
| 1117 | run_actions(SYSINIT);
|
---|
| 1118 |
|
---|
| 1119 | /* Next run anything that wants to block */
|
---|
| 1120 | run_actions(WAIT);
|
---|
| 1121 |
|
---|
| 1122 | /* Next run anything to be run only once */
|
---|
| 1123 | run_actions(ONCE);
|
---|
| 1124 |
|
---|
| 1125 | #ifdef CONFIG_FEATURE_USE_INITTAB
|
---|
| 1126 | /* Redefine SIGHUP to reread /etc/inittab */
|
---|
| 1127 | signal(SIGHUP, reload_signal);
|
---|
| 1128 | #else
|
---|
| 1129 | signal(SIGHUP, SIG_IGN);
|
---|
| 1130 | #endif /* CONFIG_FEATURE_USE_INITTAB */
|
---|
| 1131 |
|
---|
| 1132 |
|
---|
| 1133 | /* Now run the looping stuff for the rest of forever */
|
---|
| 1134 | while (1) {
|
---|
| 1135 | /* run the respawn stuff */
|
---|
| 1136 | run_actions(RESPAWN);
|
---|
| 1137 |
|
---|
| 1138 | /* run the askfirst stuff */
|
---|
| 1139 | run_actions(ASKFIRST);
|
---|
| 1140 |
|
---|
| 1141 | /* Don't consume all CPU time -- sleep a bit */
|
---|
| 1142 | sleep(1);
|
---|
| 1143 |
|
---|
| 1144 | /* Wait for a child process to exit */
|
---|
| 1145 | wpid = wait(NULL);
|
---|
| 1146 | while (wpid > 0) {
|
---|
| 1147 | /* Find out who died and clean up their corpse */
|
---|
| 1148 | for (a = init_action_list; a; a = a->next) {
|
---|
| 1149 | if (a->pid == wpid) {
|
---|
| 1150 | /* Set the pid to 0 so that the process gets
|
---|
| 1151 | * restarted by run_actions() */
|
---|
| 1152 | a->pid = 0;
|
---|
| 1153 | message(LOG, "Process '%s' (pid %d) exited. "
|
---|
| 1154 | "Scheduling it for restart.",
|
---|
| 1155 | a->command, wpid);
|
---|
| 1156 | }
|
---|
| 1157 | }
|
---|
| 1158 | /* see if anyone else is waiting to be reaped */
|
---|
| 1159 | wpid = waitpid (-1, NULL, WNOHANG);
|
---|
| 1160 | }
|
---|
| 1161 | }
|
---|
| 1162 | }
|
---|