Erik Andersen | ccc7488 | 2000-01-27 02:40:21 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini init implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * Adjusted by so many folks, it's impossible to keep track. |
| 8 | * |
Rob Landley | 2edf526 | 2006-01-22 02:41:51 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 13 | #include <syslog.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 14 | #include <paths.h> |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 15 | #include <sys/reboot.h> |
Eric Andersen | 2c1de61 | 2003-04-24 11:41:28 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 17 | #define COMMAND_SIZE 256 |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 18 | #define CONSOLE_NAME_SIZE 32 |
| 19 | #define MAXENV 16 /* Number of env. vars */ |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 20 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 21 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 22 | * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called |
Erik Andersen | 183da4a | 2000-04-04 18:36:37 +0000 | [diff] [blame] | 23 | * before processes are spawned to set core file size as unlimited. |
| 24 | * This is for debugging only. Don't use this is production, unless |
| 25 | * you want core dumps lying about.... |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 26 | */ |
| 27 | #define CORE_ENABLE_FLAG_FILE "/.init_enable_core" |
| 28 | #include <sys/resource.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 29 | |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 30 | #define INITTAB "/etc/inittab" /* inittab file location */ |
Erik Andersen | 96e2abd | 2000-01-07 11:40:44 +0000 | [diff] [blame] | 31 | #ifndef INIT_SCRIPT |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 32 | #define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */ |
Erik Andersen | 96e2abd | 2000-01-07 11:40:44 +0000 | [diff] [blame] | 33 | #endif |
Erik Andersen | 9c88cac | 1999-12-30 09:25:17 +0000 | [diff] [blame] | 34 | |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 35 | /* Allowed init action types */ |
Denis Vlasenko | ad4da98 | 2008-04-05 04:24:23 +0000 | [diff] [blame] | 36 | #define SYSINIT 0x01 |
| 37 | #define RESPAWN 0x02 |
| 38 | /* like respawn, but wait for <Enter> to be pressed on tty: */ |
| 39 | #define ASKFIRST 0x04 |
| 40 | #define WAIT 0x08 |
| 41 | #define ONCE 0x10 |
| 42 | #define CTRLALTDEL 0x20 |
| 43 | #define SHUTDOWN 0x40 |
| 44 | #define RESTART 0x80 |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 45 | |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 46 | /* |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 47 | #define STR_SYSINIT "\x01" |
| 48 | #define STR_RESPAWN "\x02" |
| 49 | #define STR_ASKFIRST "\x04" |
| 50 | #define STR_WAIT "\x08" |
| 51 | #define STR_ONCE "\x10" |
| 52 | #define STR_CTRLALTDEL "\x20" |
| 53 | #define STR_SHUTDOWN "\x40" |
| 54 | #define STR_RESTART "\x80" |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 55 | */ |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 56 | /* Set up a linked list of init_actions, to be read from inittab */ |
| 57 | struct init_action { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 58 | struct init_action *next; |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 59 | pid_t pid; |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 60 | uint8_t action_type; |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 61 | char terminal[CONSOLE_NAME_SIZE]; |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 62 | char command[COMMAND_SIZE]; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 63 | }; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 64 | |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 65 | /* Static variables */ |
| 66 | static struct init_action *init_action_list = NULL; |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | 4c97863 | 2007-02-03 03:31:13 +0000 | [diff] [blame] | 68 | static const char *log_console = VC_5; |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 69 | static sig_atomic_t got_cont = 0; |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 70 | |
| 71 | enum { |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 72 | L_LOG = 0x1, |
| 73 | L_CONSOLE = 0x2, |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 75 | #if ENABLE_FEATURE_EXTRA_QUIET |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 76 | MAYBE_CONSOLE = 0x0, |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 77 | #else |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 78 | MAYBE_CONSOLE = L_CONSOLE, |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 79 | #endif |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 80 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 81 | #ifndef RB_HALT_SYSTEM |
Bernhard Reutner-Fischer | cf1f2ac | 2006-06-02 10:43:17 +0000 | [diff] [blame] | 82 | RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 83 | RB_ENABLE_CAD = 0x89abcdef, |
| 84 | RB_DISABLE_CAD = 0, |
| 85 | RB_POWER_OFF = 0x4321fedc, |
| 86 | RB_AUTOBOOT = 0x01234567, |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 87 | #endif |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 88 | }; |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 90 | static const char *const environment[] = { |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 91 | "HOME=/", |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 92 | bb_PATH_root_path, |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 93 | "SHELL=/bin/sh", |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 94 | "USER=root", |
| 95 | NULL |
| 96 | }; |
| 97 | |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 98 | /* Function prototypes */ |
| 99 | static void delete_init_action(struct init_action *a); |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 100 | static void halt_reboot_pwoff(int sig) NORETURN; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | 21e20cb | 2008-01-04 15:10:47 +0000 | [diff] [blame] | 102 | static void waitfor(pid_t pid) |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 103 | { |
Denis Vlasenko | 21e20cb | 2008-01-04 15:10:47 +0000 | [diff] [blame] | 104 | /* waitfor(run(x)): protect against failed fork inside run() */ |
| 105 | if (pid <= 0) |
| 106 | return; |
| 107 | |
| 108 | /* Wait for any child (prevent zombies from exiting orphaned processes) |
| 109 | * but exit the loop only when specified one has exited. */ |
| 110 | while (wait(NULL) != pid) |
| 111 | continue; |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 114 | static void loop_forever(void) NORETURN; |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 115 | static void loop_forever(void) |
Matt Kraai | 67a4640 | 2001-06-03 05:55:52 +0000 | [diff] [blame] | 116 | { |
| 117 | while (1) |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 118 | sleep(1); |
Matt Kraai | 67a4640 | 2001-06-03 05:55:52 +0000 | [diff] [blame] | 119 | } |
Eric Andersen | 7ef1a5b | 2001-03-20 17:39:08 +0000 | [diff] [blame] | 120 | |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 121 | /* Print a message to the specified device. |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 122 | * "where" may be bitwise-or'd from L_LOG | L_CONSOLE |
| 123 | * NB: careful, we can be called after vfork! |
| 124 | */ |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 125 | #define messageD(...) do { if (ENABLE_DEBUG_INIT) message(__VA_ARGS__); } while (0) |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 126 | static void message(int where, const char *fmt, ...) |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 127 | __attribute__ ((format(printf, 2, 3))); |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 128 | static void message(int where, const char *fmt, ...) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 129 | { |
Eric Andersen | 2223701 | 2003-01-23 07:08:26 +0000 | [diff] [blame] | 130 | static int log_fd = -1; |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 131 | va_list arguments; |
| 132 | int l; |
| 133 | char msg[128]; |
| 134 | |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 135 | msg[0] = '\r'; |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 136 | va_start(arguments, fmt); |
| 137 | vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments); |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 138 | va_end(arguments); |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 139 | msg[sizeof(msg) - 2] = '\0'; |
| 140 | l = strlen(msg); |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 141 | |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 142 | if (ENABLE_FEATURE_INIT_SYSLOG) { |
| 143 | /* Log the message to syslogd */ |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 144 | if (where & L_LOG) { |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 145 | /* don't out "\r" */ |
| 146 | openlog(applet_name, 0, LOG_DAEMON); |
| 147 | syslog(LOG_INFO, "init: %s", msg + 1); |
| 148 | closelog(); |
| 149 | } |
| 150 | msg[l++] = '\n'; |
| 151 | msg[l] = '\0'; |
| 152 | } else { |
| 153 | msg[l++] = '\n'; |
| 154 | msg[l] = '\0'; |
| 155 | /* Take full control of the log tty, and never close it. |
| 156 | * It's mine, all mine! Muhahahaha! */ |
| 157 | if (log_fd < 0) { |
| 158 | if (!log_console) { |
| 159 | log_fd = 2; |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 160 | } else { |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 161 | log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY); |
| 162 | if (log_fd < 0) { |
| 163 | bb_error_msg("can't log to %s", log_console); |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 164 | where = L_CONSOLE; |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 165 | } else { |
| 166 | close_on_exec_on(log_fd); |
| 167 | } |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 168 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | } |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 170 | if (where & L_LOG) { |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 171 | full_write(log_fd, msg, l); |
| 172 | if (log_fd == 2) |
| 173 | return; /* don't print dup messages */ |
| 174 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 175 | } |
Eric Andersen | 4c78147 | 1999-11-26 08:12:56 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 177 | if (where & L_CONSOLE) { |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 178 | /* Send console messages to console so people will see them. */ |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 179 | full_write(STDERR_FILENO, msg, l); |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 180 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 183 | /* From <linux/serial.h> */ |
| 184 | struct serial_struct { |
| 185 | int type; |
| 186 | int line; |
| 187 | unsigned int port; |
| 188 | int irq; |
| 189 | int flags; |
| 190 | int xmit_fifo_size; |
| 191 | int custom_divisor; |
| 192 | int baud_base; |
| 193 | unsigned short close_delay; |
| 194 | char io_type; |
| 195 | char reserved_char[1]; |
| 196 | int hub6; |
| 197 | unsigned short closing_wait; /* time to wait before closing */ |
| 198 | unsigned short closing_wait2; /* no longer used... */ |
| 199 | unsigned char *iomem_base; |
| 200 | unsigned short iomem_reg_shift; |
| 201 | unsigned int port_high; |
| 202 | unsigned long iomap_base; /* cookie passed into ioremap */ |
| 203 | int reserved[1]; |
| 204 | /* Paranoia (imagine 64bit kernel overwriting 32bit userspace stack) */ |
| 205 | uint32_t bbox_reserved[16]; |
| 206 | }; |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 207 | static void console_init(void) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 208 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 209 | struct serial_struct sr; |
| 210 | char *s; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 211 | |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 212 | s = getenv("CONSOLE"); |
| 213 | if (!s) s = getenv("console"); |
| 214 | if (s) { |
| 215 | int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY); |
| 216 | if (fd >= 0) { |
| 217 | dup2(fd, 0); |
| 218 | dup2(fd, 1); |
Denis Vlasenko | e421b5e | 2008-03-17 22:01:42 +0000 | [diff] [blame] | 219 | xmove_fd(fd, 2); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 220 | } |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 221 | messageD(L_LOG, "console='%s'", s); |
Denis Vlasenko | d8540f7 | 2007-06-14 07:53:06 +0000 | [diff] [blame] | 222 | } else { |
Denis Vlasenko | fb274df | 2008-03-17 13:26:51 +0000 | [diff] [blame] | 223 | /* Make sure fd 0,1,2 are not closed |
| 224 | * (so that they won't be used by future opens) */ |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 225 | bb_sanitize_stdio(); |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 226 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 227 | |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 228 | s = getenv("TERM"); |
Denis Vlasenko | e421b5e | 2008-03-17 22:01:42 +0000 | [diff] [blame] | 229 | if (ioctl(STDIN_FILENO, TIOCGSERIAL, &sr) == 0) { |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 230 | /* Force the TERM setting to vt102 for serial console |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 231 | * if TERM is set to linux (the default) */ |
| 232 | if (!s || strcmp(s, "linux") == 0) |
| 233 | putenv((char*)"TERM=vt102"); |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 234 | if (!ENABLE_FEATURE_INIT_SYSLOG) |
| 235 | log_console = NULL; |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 236 | } else if (!s) |
| 237 | putenv((char*)"TERM=linux"); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 238 | } |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 239 | |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 240 | /* Set terminal settings to reasonable defaults. |
| 241 | * NB: careful, we can be called after vfork! */ |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 242 | static void set_sane_term(void) |
Eric Andersen | 7ef1a5b | 2001-03-20 17:39:08 +0000 | [diff] [blame] | 243 | { |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 244 | struct termios tty; |
Eric Andersen | 7ef1a5b | 2001-03-20 17:39:08 +0000 | [diff] [blame] | 245 | |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 246 | tcgetattr(STDIN_FILENO, &tty); |
| 247 | |
| 248 | /* set control chars */ |
| 249 | tty.c_cc[VINTR] = 3; /* C-c */ |
| 250 | tty.c_cc[VQUIT] = 28; /* C-\ */ |
| 251 | tty.c_cc[VERASE] = 127; /* C-? */ |
| 252 | tty.c_cc[VKILL] = 21; /* C-u */ |
| 253 | tty.c_cc[VEOF] = 4; /* C-d */ |
| 254 | tty.c_cc[VSTART] = 17; /* C-q */ |
| 255 | tty.c_cc[VSTOP] = 19; /* C-s */ |
| 256 | tty.c_cc[VSUSP] = 26; /* C-z */ |
| 257 | |
| 258 | /* use line dicipline 0 */ |
| 259 | tty.c_line = 0; |
| 260 | |
| 261 | /* Make it be sane */ |
| 262 | tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD; |
| 263 | tty.c_cflag |= CREAD | HUPCL | CLOCAL; |
| 264 | |
| 265 | /* input modes */ |
| 266 | tty.c_iflag = ICRNL | IXON | IXOFF; |
| 267 | |
| 268 | /* output modes */ |
| 269 | tty.c_oflag = OPOST | ONLCR; |
| 270 | |
| 271 | /* local modes */ |
| 272 | tty.c_lflag = |
| 273 | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN; |
| 274 | |
| 275 | tcsetattr(STDIN_FILENO, TCSANOW, &tty); |
Eric Andersen | 7ef1a5b | 2001-03-20 17:39:08 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 278 | /* Open the new terminal device. |
| 279 | * NB: careful, we can be called after vfork! */ |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 280 | static void open_stdio_to_tty(const char* tty_name, int exit_on_failure) |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 281 | { |
| 282 | /* empty tty_name means "use init's tty", else... */ |
| 283 | if (tty_name[0]) { |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 284 | int fd; |
| 285 | close(0); |
| 286 | /* fd can be only < 0 or 0: */ |
| 287 | fd = device_open(tty_name, O_RDWR); |
| 288 | if (fd) { |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 289 | message(L_LOG | L_CONSOLE, "Can't open %s: %s", |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 290 | tty_name, strerror(errno)); |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 291 | if (exit_on_failure) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 292 | _exit(EXIT_FAILURE); |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 293 | if (ENABLE_DEBUG_INIT) |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 294 | _exit(2); |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 295 | /* NB: we don't reach this if we were called after vfork. |
| 296 | * Thus halt_reboot_pwoff() itself need not be vfork-safe. */ |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 297 | halt_reboot_pwoff(SIGUSR1); /* halt the system */ |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 298 | } |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 299 | dup2(0, 1); |
| 300 | dup2(0, 2); |
Bernhard Reutner-Fischer | 0da069d | 2006-05-29 12:54:16 +0000 | [diff] [blame] | 301 | } |
Denis Vlasenko | d238a47 | 2007-03-05 19:22:04 +0000 | [diff] [blame] | 302 | set_sane_term(); |
Bernhard Reutner-Fischer | 0da069d | 2006-05-29 12:54:16 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 305 | /* Wrapper around exec: |
| 306 | * Takes string (max COMMAND_SIZE chars). |
| 307 | * If chars like '>' detected, execs '[-]/bin/sh -c "exec ......."'. |
| 308 | * Otherwise splits words on whitespace, deals with leading dash, |
| 309 | * and uses plain exec(). |
| 310 | * NB: careful, we can be called after vfork! |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 311 | */ |
| 312 | static void init_exec(const char *command) |
| 313 | { |
| 314 | char *cmd[COMMAND_SIZE / 2]; |
| 315 | char buf[COMMAND_SIZE + 6]; /* COMMAND_SIZE+strlen("exec ")+1 */ |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 316 | int dash = (command[0] == '-' /* maybe? && command[1] == '/' */); |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 317 | |
| 318 | /* See if any special /bin/sh requiring characters are present */ |
| 319 | if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) { |
| 320 | strcpy(buf, "exec "); |
| 321 | strcpy(buf + 5, command + dash); /* excluding "-" */ |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 322 | /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */ |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 323 | cmd[0] = (char*)(LIBBB_DEFAULT_LOGIN_SHELL + !dash); |
| 324 | cmd[1] = (char*)"-c"; |
| 325 | cmd[2] = buf; |
| 326 | cmd[3] = NULL; |
| 327 | } else { |
| 328 | /* Convert command (char*) into cmd (char**, one word per string) */ |
| 329 | char *word, *next; |
| 330 | int i = 0; |
| 331 | next = strcpy(buf, command); /* including "-" */ |
| 332 | while ((word = strsep(&next, " \t")) != NULL) { |
| 333 | if (*word != '\0') { /* not two spaces/tabs together? */ |
| 334 | cmd[i] = word; |
| 335 | i++; |
| 336 | } |
| 337 | } |
| 338 | cmd[i] = NULL; |
| 339 | } |
| 340 | /* If we saw leading "-", it is interactive shell. |
| 341 | * Try harder to give it a controlling tty. |
| 342 | * And skip "-" in actual exec call. */ |
| 343 | if (dash) { |
| 344 | /* _Attempt_ to make stdin a controlling tty. */ |
| 345 | if (ENABLE_FEATURE_INIT_SCTTY) |
| 346 | ioctl(STDIN_FILENO, TIOCSCTTY, 0 /*only try, don't steal*/); |
| 347 | } |
| 348 | BB_EXECVP(cmd[0] + dash, cmd); |
| 349 | message(L_LOG | L_CONSOLE, "Cannot run '%s': %s", |
| 350 | cmd[0], strerror(errno)); |
| 351 | /* returns if execvp fails */ |
| 352 | } |
| 353 | |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 354 | /* Used only by run_actions */ |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 355 | static pid_t run(const struct init_action *a) |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 356 | { |
Mike Frysinger | 10427ab | 2005-07-06 05:00:48 +0000 | [diff] [blame] | 357 | pid_t pid; |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 358 | sigset_t nmask, omask; |
Erik Andersen | ac6e71f | 2000-01-08 22:04:33 +0000 | [diff] [blame] | 359 | |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 360 | /* Block sigchild while forking (why?) */ |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 361 | sigemptyset(&nmask); |
| 362 | sigaddset(&nmask, SIGCHLD); |
| 363 | sigprocmask(SIG_BLOCK, &nmask, &omask); |
Denis Vlasenko | ad4da98 | 2008-04-05 04:24:23 +0000 | [diff] [blame] | 364 | if (BB_MMU && (a->action_type & ASKFIRST)) |
| 365 | pid = fork(); |
| 366 | else |
| 367 | pid = vfork(); |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 368 | sigprocmask(SIG_SETMASK, &omask, NULL); |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 369 | |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 370 | if (pid < 0) |
| 371 | message(L_LOG | L_CONSOLE, "Can't fork"); |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 372 | if (pid) |
| 373 | return pid; |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 374 | |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 375 | /* Child */ |
| 376 | |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 377 | /* Reset signal handlers that were set by the parent process */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 378 | bb_signals(0 |
| 379 | + (1 << SIGUSR1) |
| 380 | + (1 << SIGUSR2) |
| 381 | + (1 << SIGINT) |
| 382 | + (1 << SIGTERM) |
| 383 | + (1 << SIGHUP) |
| 384 | + (1 << SIGQUIT) |
| 385 | + (1 << SIGCONT) |
| 386 | + (1 << SIGSTOP) |
| 387 | + (1 << SIGTSTP) |
| 388 | , SIG_DFL); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 389 | |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 390 | /* Create a new session and make ourself the process |
| 391 | * group leader */ |
| 392 | setsid(); |
Eric Andersen | 599e3ce | 2002-07-03 11:08:10 +0000 | [diff] [blame] | 393 | |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 394 | /* Open the new terminal device */ |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 395 | open_stdio_to_tty(a->terminal, 1 /* - exit if open fails */); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 396 | |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 397 | // NB: do not enable unless you change vfork to fork above |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 398 | #ifdef BUT_RUN_ACTIONS_ALREADY_DOES_WAITING |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 399 | /* If the init Action requires us to wait, then force the |
| 400 | * supplied terminal to be the controlling tty. */ |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 401 | if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) { |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 402 | /* Now fork off another process to just hang around */ |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 403 | pid = fork(); |
Denis Vlasenko | f6ccc62 | 2007-11-10 01:57:35 +0000 | [diff] [blame] | 404 | if (pid < 0) { |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 405 | message(L_LOG | L_CONSOLE, "Can't fork"); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 406 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (pid > 0) { |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 410 | /* Parent - wait till the child is done */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 411 | bb_signals(0 |
| 412 | + (1 << SIGINT) |
| 413 | + (1 << SIGTSTP) |
| 414 | + (1 << SIGQUIT) |
| 415 | , SIG_IGN); |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 416 | signal(SIGCHLD, SIG_DFL); |
| 417 | |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 418 | waitfor(pid); |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 419 | /* See if stealing the controlling tty back is necessary */ |
| 420 | if (tcgetpgrp(0) != getpid()) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 421 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 422 | |
| 423 | /* Use a temporary process to steal the controlling tty. */ |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 424 | pid = fork(); |
| 425 | if (pid < 0) { |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 426 | message(L_LOG | L_CONSOLE, "Can't fork"); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 427 | _exit(EXIT_FAILURE); |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 428 | } |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 429 | if (pid == 0) { |
| 430 | setsid(); |
| 431 | ioctl(0, TIOCSCTTY, 1); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 432 | _exit(EXIT_SUCCESS); |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 433 | } |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 434 | waitfor(pid); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 435 | _exit(EXIT_SUCCESS); |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 438 | /* Child - fall though to actually execute things */ |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 439 | } |
Denis Vlasenko | 5adfa44 | 2007-12-25 16:08:53 +0000 | [diff] [blame] | 440 | #endif |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 441 | |
Denis Vlasenko | ad4da98 | 2008-04-05 04:24:23 +0000 | [diff] [blame] | 442 | /* NB: on NOMMU we can't wait for input in child, so |
| 443 | * "askfirst" will work the same as "respawn". */ |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 444 | if (BB_MMU && (a->action_type & ASKFIRST)) { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 445 | static const char press_enter[] ALIGN1 = |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 446 | #ifdef CUSTOMIZED_BANNER |
| 447 | #include CUSTOMIZED_BANNER |
Eric Andersen | 1f50e84 | 2004-08-16 09:29:42 +0000 | [diff] [blame] | 448 | #endif |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 449 | "\nPlease press Enter to activate this console. "; |
| 450 | char c; |
| 451 | /* |
| 452 | * Save memory by not exec-ing anything large (like a shell) |
| 453 | * before the user wants it. This is critical if swap is not |
| 454 | * enabled and the system has low memory. Generally this will |
| 455 | * be run on the second virtual console, and the first will |
| 456 | * be allowed to start a shell or whatever an init script |
| 457 | * specifies. |
| 458 | */ |
| 459 | messageD(L_LOG, "waiting for enter to start '%s'" |
| 460 | "(pid %d, tty '%s')\n", |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 461 | a->command, getpid(), a->terminal); |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 462 | full_write(STDOUT_FILENO, press_enter, sizeof(press_enter) - 1); |
| 463 | while (safe_read(STDIN_FILENO, &c, 1) == 1 && c != '\n') |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 464 | continue; |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 465 | } |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 466 | |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 467 | if (ENABLE_FEATURE_INIT_COREDUMPS) { |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 468 | struct stat sb; |
| 469 | if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) { |
| 470 | struct rlimit limit; |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 471 | limit.rlim_cur = RLIM_INFINITY; |
| 472 | limit.rlim_max = RLIM_INFINITY; |
| 473 | setrlimit(RLIMIT_CORE, &limit); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 474 | } |
Erik Andersen | 05df239 | 2000-01-13 04:43:48 +0000 | [diff] [blame] | 475 | } |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 476 | |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 477 | /* Log the process name and args */ |
| 478 | message(L_LOG, "starting pid %d, tty '%s': '%s'", |
| 479 | getpid(), a->terminal, a->command); |
| 480 | |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 481 | /* Now run it. The new program will take over this PID, |
| 482 | * so nothing further in init.c should be run. */ |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 483 | init_exec(a->command); |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 484 | /* We're still here? Some error happened. */ |
Denis Vlasenko | 966bb43 | 2007-02-27 19:20:33 +0000 | [diff] [blame] | 485 | _exit(-1); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 488 | /* Run all commands of a particular type */ |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 489 | static void run_actions(int action_type) |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 490 | { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 491 | struct init_action *a, *tmp; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 492 | |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 493 | for (a = init_action_list; a; a = tmp) { |
| 494 | tmp = a->next; |
Denis Vlasenko | ad4da98 | 2008-04-05 04:24:23 +0000 | [diff] [blame] | 495 | if (a->action_type & action_type) { |
Denis Vlasenko | d7e2e12 | 2007-12-10 08:40:29 +0000 | [diff] [blame] | 496 | // Pointless: run() will error out if open of device fails. |
| 497 | ///* a->terminal of "" means "init's console" */ |
| 498 | //if (a->terminal[0] && access(a->terminal, R_OK | W_OK)) { |
| 499 | // //message(L_LOG | L_CONSOLE, "Device %s cannot be opened in RW mode", a->terminal /*, strerror(errno)*/); |
| 500 | // delete_init_action(a); |
| 501 | //} else |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 502 | if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) { |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 503 | waitfor(run(a)); |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 504 | delete_init_action(a); |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 505 | } else if (a->action_type & ONCE) { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 506 | run(a); |
| 507 | delete_init_action(a); |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 508 | } else if (a->action_type & (RESPAWN | ASKFIRST)) { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 509 | /* Only run stuff with pid==0. If they have |
| 510 | * a pid, that means it is still running */ |
| 511 | if (a->pid == 0) { |
| 512 | a->pid = run(a); |
| 513 | } |
| 514 | } |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
Eric Andersen | 2c1de61 | 2003-04-24 11:41:28 +0000 | [diff] [blame] | 519 | static void init_reboot(unsigned long magic) |
| 520 | { |
| 521 | pid_t pid; |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 522 | /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS) in |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 523 | * linux/kernel/sys.c, which can cause the machine to panic when |
Eric Andersen | 2c1de61 | 2003-04-24 11:41:28 +0000 | [diff] [blame] | 524 | * the init process is killed.... */ |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 525 | pid = vfork(); |
| 526 | if (pid == 0) { /* child */ |
Eric Andersen | 2c1de61 | 2003-04-24 11:41:28 +0000 | [diff] [blame] | 527 | reboot(magic); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 528 | _exit(EXIT_SUCCESS); |
Eric Andersen | 2c1de61 | 2003-04-24 11:41:28 +0000 | [diff] [blame] | 529 | } |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 530 | waitfor(pid); |
Eric Andersen | 2c1de61 | 2003-04-24 11:41:28 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 533 | static void kill_all_processes(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 534 | { |
Eric Andersen | a9cc896 | 2002-09-16 06:49:06 +0000 | [diff] [blame] | 535 | /* run everything to be run at "shutdown". This is done _prior_ |
| 536 | * to killing everything, in case people wish to use scripts to |
| 537 | * shut things down gracefully... */ |
| 538 | run_actions(SHUTDOWN); |
| 539 | |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 540 | /* first disable all our signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 541 | sigprocmask_allsigs(SIG_BLOCK); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 542 | |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 543 | message(L_CONSOLE | L_LOG, "The system is going down NOW!"); |
| 544 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 545 | /* Allow Ctrl-Alt-Del to reboot system. */ |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 546 | init_reboot(RB_ENABLE_CAD); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 547 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 548 | /* Send signals to every process _except_ pid 1 */ |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 549 | message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "TERM"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 550 | kill(-1, SIGTERM); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 551 | sync(); |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 552 | sleep(1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 553 | |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 554 | message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "KILL"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 555 | kill(-1, SIGKILL); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 556 | sync(); |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 557 | sleep(1); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 560 | static void halt_reboot_pwoff(int sig) |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 561 | { |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 562 | const char *m = "halt"; |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 563 | int rb; |
| 564 | |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 565 | kill_all_processes(); |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 566 | |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 567 | rb = RB_HALT_SYSTEM; |
| 568 | if (sig == SIGTERM) { |
| 569 | m = "reboot"; |
| 570 | rb = RB_AUTOBOOT; |
| 571 | } else if (sig == SIGUSR2) { |
| 572 | m = "poweroff"; |
| 573 | rb = RB_POWER_OFF; |
| 574 | } |
| 575 | message(L_CONSOLE | L_LOG, "Requesting system %s", m); |
| 576 | /* allow time for last message to reach serial console */ |
| 577 | sleep(2); |
| 578 | init_reboot(rb); |
| 579 | loop_forever(); |
| 580 | } |
| 581 | |
Denis Vlasenko | a58a637 | 2008-02-19 12:10:18 +0000 | [diff] [blame] | 582 | /* Handler for QUIT - exec "restart" action, |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 583 | * else (no such action defined) do nothing */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 584 | static void exec_restart_action(int sig UNUSED_PARAM) |
Eric Andersen | 730f826 | 2001-12-17 23:13:08 +0000 | [diff] [blame] | 585 | { |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 586 | struct init_action *a; |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 587 | |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 588 | for (a = init_action_list; a; a = a->next) { |
| 589 | if (a->action_type & RESTART) { |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 590 | kill_all_processes(); |
Eric Andersen | 5222d31 | 2002-07-03 05:15:23 +0000 | [diff] [blame] | 591 | |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 592 | /* unblock all signals (blocked in kill_all_processes()) */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 593 | sigprocmask_allsigs(SIG_UNBLOCK); |
Eric Andersen | 5222d31 | 2002-07-03 05:15:23 +0000 | [diff] [blame] | 594 | |
Eric Andersen | 3c8064f | 2003-07-05 08:29:01 +0000 | [diff] [blame] | 595 | /* Open the new terminal device */ |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 596 | open_stdio_to_tty(a->terminal, 0 /* - halt if open fails */); |
Eric Andersen | 3c8064f | 2003-07-05 08:29:01 +0000 | [diff] [blame] | 597 | |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 598 | messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command); |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 599 | init_exec(a->command); |
Eric Andersen | 730f826 | 2001-12-17 23:13:08 +0000 | [diff] [blame] | 600 | sleep(2); |
| 601 | init_reboot(RB_HALT_SYSTEM); |
| 602 | loop_forever(); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 607 | static void ctrlaltdel_signal(int sig UNUSED_PARAM) |
Eric Andersen | c97ec34 | 2001-04-03 18:01:51 +0000 | [diff] [blame] | 608 | { |
| 609 | run_actions(CTRLALTDEL); |
| 610 | } |
| 611 | |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 612 | /* The SIGSTOP & SIGTSTP handler */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 613 | static void stop_handler(int sig UNUSED_PARAM) |
Eric Andersen | ed8a9be | 2001-11-30 19:10:58 +0000 | [diff] [blame] | 614 | { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 615 | int saved_errno = errno; |
Eric Andersen | ed8a9be | 2001-11-30 19:10:58 +0000 | [diff] [blame] | 616 | |
| 617 | got_cont = 0; |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 618 | while (!got_cont) |
| 619 | pause(); |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 620 | |
Eric Andersen | ed8a9be | 2001-11-30 19:10:58 +0000 | [diff] [blame] | 621 | errno = saved_errno; |
| 622 | } |
| 623 | |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 624 | /* The SIGCONT handler */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 625 | static void cont_handler(int sig UNUSED_PARAM) |
Eric Andersen | ed8a9be | 2001-11-30 19:10:58 +0000 | [diff] [blame] | 626 | { |
| 627 | got_cont = 1; |
| 628 | } |
| 629 | |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 630 | static void new_init_action(uint8_t action_type, const char *command, const char *cons) |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 631 | { |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 632 | struct init_action *a, *last; |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 633 | |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 634 | // Why? |
| 635 | // if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST)) |
| 636 | // return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 637 | |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 638 | /* Append to the end of the list */ |
Eric Andersen | 2271809 | 2004-10-08 08:17:39 +0000 | [diff] [blame] | 639 | for (a = last = init_action_list; a; a = a->next) { |
Eric Andersen | 82baf63 | 2004-10-08 08:21:54 +0000 | [diff] [blame] | 640 | /* don't enter action if it's already in the list, |
| 641 | * but do overwrite existing actions */ |
Denis Vlasenko | ec27feb | 2007-02-17 15:52:02 +0000 | [diff] [blame] | 642 | if ((strcmp(a->command, command) == 0) |
| 643 | && (strcmp(a->terminal, cons) == 0) |
| 644 | ) { |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 645 | a->action_type = action_type; |
Eric Andersen | 6fd0e31 | 2003-07-22 09:48:56 +0000 | [diff] [blame] | 646 | return; |
| 647 | } |
Eric Andersen | 2271809 | 2004-10-08 08:17:39 +0000 | [diff] [blame] | 648 | last = a; |
Eric Andersen | 6fd0e31 | 2003-07-22 09:48:56 +0000 | [diff] [blame] | 649 | } |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 650 | |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 651 | a = xzalloc(sizeof(*a)); |
Eric Andersen | 2271809 | 2004-10-08 08:17:39 +0000 | [diff] [blame] | 652 | if (last) { |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 653 | last->next = a; |
Eric Andersen | 1644db9 | 2001-09-05 20:18:15 +0000 | [diff] [blame] | 654 | } else { |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 655 | init_action_list = a; |
Eric Andersen | 1644db9 | 2001-09-05 20:18:15 +0000 | [diff] [blame] | 656 | } |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 657 | a->action_type = action_type; |
| 658 | safe_strncpy(a->command, command, sizeof(a->command)); |
| 659 | safe_strncpy(a->terminal, cons, sizeof(a->terminal)); |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 660 | messageD(L_LOG | L_CONSOLE, "command='%s' action=%d tty='%s'\n", |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 661 | a->command, a->action_type, a->terminal); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 664 | static void delete_init_action(struct init_action *action) |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 665 | { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 666 | struct init_action *a, *b = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 667 | |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 668 | for (a = init_action_list; a; b = a, a = a->next) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 669 | if (a == action) { |
| 670 | if (b == NULL) { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 671 | init_action_list = a->next; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 672 | } else { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 673 | b->next = a->next; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 674 | } |
| 675 | free(a); |
| 676 | break; |
| 677 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 678 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 681 | /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined, |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 682 | * then parse_inittab() simply adds in some default |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 683 | * actions(i.e., runs INIT_SCRIPT and then starts a pair |
| 684 | * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB |
| 685 | * _is_ defined, but /etc/inittab is missing, this |
Erik Andersen | 812d466 | 2000-01-07 18:30:40 +0000 | [diff] [blame] | 686 | * results in the same set of default behaviors. |
Glenn L McGrath | baf55a8 | 2002-08-22 18:22:10 +0000 | [diff] [blame] | 687 | */ |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 688 | static void parse_inittab(void) |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 689 | { |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 690 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 691 | FILE *file; |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 692 | char buf[COMMAND_SIZE]; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 693 | |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 694 | if (ENABLE_FEATURE_USE_INITTAB) |
| 695 | file = fopen(INITTAB, "r"); |
| 696 | else |
| 697 | file = NULL; |
| 698 | |
| 699 | /* No inittab file -- set up some default behavior */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 700 | if (file == NULL) { |
Eric Andersen | c97ec34 | 2001-04-03 18:01:51 +0000 | [diff] [blame] | 701 | /* Reboot on Ctrl-Alt-Del */ |
Denis Vlasenko | 2f0c0d0 | 2007-01-21 00:41:04 +0000 | [diff] [blame] | 702 | new_init_action(CTRLALTDEL, "reboot", ""); |
Eric Andersen | 1644db9 | 2001-09-05 20:18:15 +0000 | [diff] [blame] | 703 | /* Umount all filesystems on halt/reboot */ |
Denis Vlasenko | 2f0c0d0 | 2007-01-21 00:41:04 +0000 | [diff] [blame] | 704 | new_init_action(SHUTDOWN, "umount -a -r", ""); |
Eric Andersen | 1644db9 | 2001-09-05 20:18:15 +0000 | [diff] [blame] | 705 | /* Swapoff on halt/reboot */ |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 706 | if (ENABLE_SWAPONOFF) |
| 707 | new_init_action(SHUTDOWN, "swapoff -a", ""); |
Denis Vlasenko | a58a637 | 2008-02-19 12:10:18 +0000 | [diff] [blame] | 708 | /* Prepare to restart init when a QUIT is received */ |
Denis Vlasenko | 2f0c0d0 | 2007-01-21 00:41:04 +0000 | [diff] [blame] | 709 | new_init_action(RESTART, "init", ""); |
Eric Andersen | 3bc2b20 | 2002-07-29 06:39:58 +0000 | [diff] [blame] | 710 | /* Askfirst shell on tty1-4 */ |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 711 | new_init_action(ASKFIRST, bb_default_login_shell, ""); |
| 712 | new_init_action(ASKFIRST, bb_default_login_shell, VC_2); |
| 713 | new_init_action(ASKFIRST, bb_default_login_shell, VC_3); |
| 714 | new_init_action(ASKFIRST, bb_default_login_shell, VC_4); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 715 | /* sysinit */ |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 716 | new_init_action(SYSINIT, INIT_SCRIPT, ""); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 717 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 718 | return; |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 719 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 720 | |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 721 | while (fgets(buf, COMMAND_SIZE, file) != NULL) { |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 722 | static const char actions[] = |
| 723 | STR_SYSINIT "sysinit\0" |
| 724 | STR_RESPAWN "respawn\0" |
| 725 | STR_ASKFIRST "askfirst\0" |
| 726 | STR_WAIT "wait\0" |
| 727 | STR_ONCE "once\0" |
| 728 | STR_CTRLALTDEL "ctrlaltdel\0" |
| 729 | STR_SHUTDOWN "shutdown\0" |
| 730 | STR_RESTART "restart\0" |
| 731 | ; |
| 732 | char tmpConsole[CONSOLE_NAME_SIZE]; |
| 733 | char *id, *runlev, *action, *command; |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 734 | const char *a; |
| 735 | |
Eric Andersen | b596636 | 2000-05-31 20:04:38 +0000 | [diff] [blame] | 736 | /* Skip leading spaces */ |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 737 | id = skip_whitespace(buf); |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 738 | /* Trim the trailing '\n' */ |
| 739 | *strchrnul(id, '\n') = '\0'; |
Denis Vlasenko | c882f34 | 2008-01-29 09:56:21 +0000 | [diff] [blame] | 740 | /* Skip the line if it is a comment */ |
| 741 | if (*id == '#' || *id == '\0') |
| 742 | continue; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 743 | |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 744 | /* Line is: "id:runlevel_ignored:action:command" */ |
Eric Andersen | b596636 | 2000-05-31 20:04:38 +0000 | [diff] [blame] | 745 | runlev = strchr(id, ':'); |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 746 | if (runlev == NULL /*|| runlev[1] == '\0' - not needed */) |
| 747 | goto bad_entry; |
| 748 | action = strchr(runlev + 1, ':'); |
| 749 | if (action == NULL /*|| action[1] == '\0' - not needed */) |
| 750 | goto bad_entry; |
| 751 | command = strchr(action + 1, ':'); |
| 752 | if (command == NULL || command[1] == '\0') |
| 753 | goto bad_entry; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 754 | |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 755 | *command = '\0'; /* action => ":action\0" now */ |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 756 | for (a = actions; a[0]; a += strlen(a) + 1) { |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 757 | if (strcmp(a + 1, action + 1) == 0) { |
| 758 | *runlev = '\0'; |
Eric Andersen | b596636 | 2000-05-31 20:04:38 +0000 | [diff] [blame] | 759 | if (*id != '\0') { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 760 | if (strncmp(id, "/dev/", 5) == 0) |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 761 | id += 5; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 762 | strcpy(tmpConsole, "/dev/"); |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 763 | safe_strncpy(tmpConsole + 5, id, |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 764 | sizeof(tmpConsole) - 5); |
Eric Andersen | b596636 | 2000-05-31 20:04:38 +0000 | [diff] [blame] | 765 | id = tmpConsole; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 766 | } |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 767 | new_init_action((uint8_t)a[0], command + 1, id); |
| 768 | goto next_line; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 769 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 770 | } |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 771 | *command = ':'; |
| 772 | /* Choke on an unknown action */ |
| 773 | bad_entry: |
| 774 | message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", id); |
| 775 | next_line: ; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 776 | } |
Eric Andersen | d8636ca | 2002-05-15 22:19:09 +0000 | [diff] [blame] | 777 | fclose(file); |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 778 | #else |
| 779 | char *token[4]; |
Denis Vlasenko | a474b68 | 2008-07-17 17:58:44 +0000 | [diff] [blame] | 780 | /* order must correspond to SYSINIT..RESTART constants */ |
| 781 | static const char actions[] ALIGN1 = |
| 782 | "sysinit\0""respawn\0""askfirst\0""wait\0""once\0" |
| 783 | "ctrlaltdel\0""shutdown\0""restart\0"; |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 784 | |
| 785 | parser_t *parser = config_open(INITTAB); |
| 786 | /* No inittab file -- set up some default behavior */ |
| 787 | if (parser == NULL) { |
| 788 | /* Reboot on Ctrl-Alt-Del */ |
| 789 | new_init_action(CTRLALTDEL, "reboot", ""); |
| 790 | /* Umount all filesystems on halt/reboot */ |
| 791 | new_init_action(SHUTDOWN, "umount -a -r", ""); |
| 792 | /* Swapoff on halt/reboot */ |
| 793 | if (ENABLE_SWAPONOFF) |
| 794 | new_init_action(SHUTDOWN, "swapoff -a", ""); |
| 795 | /* Prepare to restart init when a QUIT is received */ |
| 796 | new_init_action(RESTART, "init", ""); |
| 797 | /* Askfirst shell on tty1-4 */ |
| 798 | new_init_action(ASKFIRST, bb_default_login_shell, ""); |
Denis Vlasenko | a474b68 | 2008-07-17 17:58:44 +0000 | [diff] [blame] | 799 | //TODO: VC_1 instead of ""? "" is console -> ctty problems -> angry users |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 800 | new_init_action(ASKFIRST, bb_default_login_shell, VC_2); |
| 801 | new_init_action(ASKFIRST, bb_default_login_shell, VC_3); |
| 802 | new_init_action(ASKFIRST, bb_default_login_shell, VC_4); |
| 803 | /* sysinit */ |
| 804 | new_init_action(SYSINIT, INIT_SCRIPT, ""); |
| 805 | |
| 806 | return; |
| 807 | } |
| 808 | /* optional_tty:ignored_runlevel:action:command |
Denis Vlasenko | a474b68 | 2008-07-17 17:58:44 +0000 | [diff] [blame] | 809 | * Delims are not to be collapsed and need exactly 4 tokens |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 810 | */ |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame^] | 811 | while (config_read(parser, token, 4, 0, "#:", PARSE_DONT_TRIM|PARSE_DONT_REDUCE|PARSE_LAST_IS_GREEDY)) { |
Denis Vlasenko | a474b68 | 2008-07-17 17:58:44 +0000 | [diff] [blame] | 812 | int action; |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 813 | char *tty = token[0]; |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 814 | |
Denis Vlasenko | a474b68 | 2008-07-17 17:58:44 +0000 | [diff] [blame] | 815 | if (!token[3]) /* less than 4 tokens */ |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 816 | goto bad_entry; |
Denis Vlasenko | a474b68 | 2008-07-17 17:58:44 +0000 | [diff] [blame] | 817 | action = index_in_strings(actions, token[2]); |
| 818 | if (action < 0 || !token[3][0]) /* token[3]: command */ |
| 819 | goto bad_entry; |
| 820 | /* turn .*TTY -> /dev/TTY */ |
| 821 | if (tty[0]) { |
| 822 | if (strncmp(tty, "/dev/", 5) == 0) |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 823 | tty += 5; |
| 824 | tty = concat_path_file("/dev/", tty); |
Denis Vlasenko | a474b68 | 2008-07-17 17:58:44 +0000 | [diff] [blame] | 825 | } |
| 826 | new_init_action(1 << action, token[3], tty); |
| 827 | if (tty[0]) |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 828 | free(tty); |
| 829 | continue; |
| 830 | bad_entry: |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame^] | 831 | message(L_LOG | L_CONSOLE, "Bad inittab entry at line %d", parser->lineno); |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 832 | } |
| 833 | config_close(parser); |
| 834 | #endif |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Denis Vlasenko | ad4da98 | 2008-04-05 04:24:23 +0000 | [diff] [blame] | 837 | #if ENABLE_FEATURE_USE_INITTAB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 838 | static void reload_signal(int sig UNUSED_PARAM) |
Eric Andersen | 6fd0e31 | 2003-07-22 09:48:56 +0000 | [diff] [blame] | 839 | { |
Eric Andersen | 82baf63 | 2004-10-08 08:21:54 +0000 | [diff] [blame] | 840 | struct init_action *a, *tmp; |
| 841 | |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 842 | message(L_LOG, "reloading /etc/inittab"); |
Eric Andersen | 82baf63 | 2004-10-08 08:21:54 +0000 | [diff] [blame] | 843 | |
| 844 | /* disable old entrys */ |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 845 | for (a = init_action_list; a; a = a->next) { |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 846 | a->action_type = ONCE; |
Eric Andersen | 82baf63 | 2004-10-08 08:21:54 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | parse_inittab(); |
| 850 | |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 851 | if (ENABLE_FEATURE_KILL_REMOVED) { |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 852 | /* Be nice and send SIGTERM first */ |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 853 | for (a = init_action_list; a; a = a->next) { |
| 854 | pid_t pid = a->pid; |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 855 | if ((a->action_type & ONCE) && pid != 0) { |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 856 | kill(pid, SIGTERM); |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 857 | } |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 858 | } |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 859 | #if CONFIG_FEATURE_KILL_DELAY |
Denis Vlasenko | 671ca33 | 2008-02-19 14:13:20 +0000 | [diff] [blame] | 860 | /* NB: parent will wait in NOMMU case */ |
| 861 | if ((BB_MMU ? fork() : vfork()) == 0) { /* child */ |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 862 | sleep(CONFIG_FEATURE_KILL_DELAY); |
| 863 | for (a = init_action_list; a; a = a->next) { |
| 864 | pid_t pid = a->pid; |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 865 | if ((a->action_type & ONCE) && pid != 0) { |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 866 | kill(pid, SIGKILL); |
| 867 | } |
| 868 | } |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 869 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | d55268d | 2007-12-26 18:32:58 +0000 | [diff] [blame] | 870 | } |
| 871 | #endif |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 872 | } |
Denis Vlasenko | ec5631b | 2007-12-25 01:08:58 +0000 | [diff] [blame] | 873 | |
Eric Andersen | 82baf63 | 2004-10-08 08:21:54 +0000 | [diff] [blame] | 874 | /* remove unused entrys */ |
| 875 | for (a = init_action_list; a; a = tmp) { |
| 876 | tmp = a->next; |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 877 | if ((a->action_type & (ONCE | SYSINIT | WAIT)) && a->pid == 0) { |
Eric Andersen | 82baf63 | 2004-10-08 08:21:54 +0000 | [diff] [blame] | 878 | delete_init_action(a); |
| 879 | } |
| 880 | } |
Denis Vlasenko | ad4da98 | 2008-04-05 04:24:23 +0000 | [diff] [blame] | 881 | run_actions(RESPAWN | ASKFIRST); |
Eric Andersen | 6fd0e31 | 2003-07-22 09:48:56 +0000 | [diff] [blame] | 882 | } |
Denis Vlasenko | ad4da98 | 2008-04-05 04:24:23 +0000 | [diff] [blame] | 883 | #endif |
Eric Andersen | 82baf63 | 2004-10-08 08:21:54 +0000 | [diff] [blame] | 884 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 885 | int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 886 | int init_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 887 | { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 888 | struct init_action *a; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 889 | pid_t wpid; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 890 | |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 891 | die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */ |
| 892 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 893 | if (argv[1] && !strcmp(argv[1], "-q")) { |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 894 | return kill(1, SIGHUP); |
Eric Andersen | 730f826 | 2001-12-17 23:13:08 +0000 | [diff] [blame] | 895 | } |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 896 | |
| 897 | if (!ENABLE_DEBUG_INIT) { |
| 898 | /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */ |
| 899 | if (getpid() != 1 |
| 900 | && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc")) |
| 901 | ) { |
| 902 | bb_show_usage(); |
| 903 | } |
| 904 | /* Set up sig handlers -- be sure to |
| 905 | * clear all of these in run() */ |
Denis Vlasenko | 99a6184 | 2008-02-19 12:08:38 +0000 | [diff] [blame] | 906 | signal(SIGQUIT, exec_restart_action); |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 907 | bb_signals(0 |
| 908 | + (1 << SIGUSR1) /* halt */ |
| 909 | + (1 << SIGUSR2) /* poweroff */ |
| 910 | + (1 << SIGTERM) /* reboot */ |
| 911 | , halt_reboot_pwoff); |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 912 | signal(SIGINT, ctrlaltdel_signal); |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 913 | signal(SIGCONT, cont_handler); |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 914 | bb_signals(0 |
| 915 | + (1 << SIGSTOP) |
| 916 | + (1 << SIGTSTP) |
| 917 | , stop_handler); |
Mike Frysinger | bb50fdf | 2007-12-25 04:30:14 +0000 | [diff] [blame] | 918 | |
| 919 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
| 920 | * SIGINT on CAD so we can shut things down gracefully... */ |
| 921 | init_reboot(RB_DISABLE_CAD); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 922 | } |
Denis Vlasenko | d8540f7 | 2007-06-14 07:53:06 +0000 | [diff] [blame] | 923 | |
Eric Andersen | 599e3ce | 2002-07-03 11:08:10 +0000 | [diff] [blame] | 924 | /* Figure out where the default console should be */ |
| 925 | console_init(); |
Denis Vlasenko | d238a47 | 2007-03-05 19:22:04 +0000 | [diff] [blame] | 926 | set_sane_term(); |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 927 | xchdir("/"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 928 | setsid(); |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 929 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 930 | const char *const *e; |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 931 | /* Make sure environs is set to something sane */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 932 | for (e = environment; *e; e++) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 933 | putenv((char *) *e); |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 934 | } |
Rob Landley | cba1b96 | 2006-07-09 17:28:17 +0000 | [diff] [blame] | 935 | |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 936 | if (argv[1]) |
| 937 | setenv("RUNLEVEL", argv[1], 1); |
Rob Landley | cba1b96 | 2006-07-09 17:28:17 +0000 | [diff] [blame] | 938 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 939 | /* Hello world */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 940 | message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_banner); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 941 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 942 | /* Make sure there is enough memory to do something useful. */ |
Rob Landley | c3386a4 | 2005-08-30 18:50:37 +0000 | [diff] [blame] | 943 | if (ENABLE_SWAPONOFF) { |
| 944 | struct sysinfo info; |
| 945 | |
| 946 | if (!sysinfo(&info) && |
Denis Vlasenko | dca0b70 | 2006-10-27 09:05:02 +0000 | [diff] [blame] | 947 | (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024) |
Rob Landley | c3386a4 | 2005-08-30 18:50:37 +0000 | [diff] [blame] | 948 | { |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 949 | message(L_CONSOLE, "Low memory, forcing swapon"); |
Rob Landley | c3386a4 | 2005-08-30 18:50:37 +0000 | [diff] [blame] | 950 | /* swapon -a requires /proc typically */ |
Denis Vlasenko | 2f0c0d0 | 2007-01-21 00:41:04 +0000 | [diff] [blame] | 951 | new_init_action(SYSINIT, "mount -t proc proc /proc", ""); |
Rob Landley | c3386a4 | 2005-08-30 18:50:37 +0000 | [diff] [blame] | 952 | /* Try to turn on swap */ |
Denis Vlasenko | 2f0c0d0 | 2007-01-21 00:41:04 +0000 | [diff] [blame] | 953 | new_init_action(SYSINIT, "swapon -a", ""); |
Rob Landley | c3386a4 | 2005-08-30 18:50:37 +0000 | [diff] [blame] | 954 | run_actions(SYSINIT); /* wait and removing */ |
| 955 | } |
| 956 | } |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 957 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 958 | /* Check if we are supposed to be in single user mode */ |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 959 | if (argv[1] |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 960 | && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1')) |
| 961 | ) { |
Bernhard Reutner-Fischer | 54d50a0 | 2008-07-17 14:00:42 +0000 | [diff] [blame] | 962 | /* ??? shouldn't we set RUNLEVEL="b" here? */ |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 963 | /* Start a shell on console */ |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 964 | new_init_action(RESPAWN, bb_default_login_shell, ""); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 965 | } else { |
| 966 | /* Not in single user mode -- see what inittab says */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 967 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 968 | /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 969 | * then parse_inittab() simply adds in some default |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 970 | * actions(i.e., runs INIT_SCRIPT and then starts a pair |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 971 | * of "askfirst" shells */ |
| 972 | parse_inittab(); |
Eric Andersen | d00c262 | 1999-12-07 08:37:31 +0000 | [diff] [blame] | 973 | } |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 974 | |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 975 | #if ENABLE_SELINUX |
Rob Landley | b3ede5a | 2006-03-27 23:09:12 +0000 | [diff] [blame] | 976 | if (getenv("SELINUX_INIT") == NULL) { |
| 977 | int enforce = 0; |
| 978 | |
Denis Vlasenko | d8540f7 | 2007-06-14 07:53:06 +0000 | [diff] [blame] | 979 | putenv((char*)"SELINUX_INIT=YES"); |
Rob Landley | b3ede5a | 2006-03-27 23:09:12 +0000 | [diff] [blame] | 980 | if (selinux_init_load_policy(&enforce) == 0) { |
Denis Vlasenko | 4921b54 | 2007-02-03 02:17:41 +0000 | [diff] [blame] | 981 | BB_EXECVP(argv[0], argv); |
Rob Landley | b3ede5a | 2006-03-27 23:09:12 +0000 | [diff] [blame] | 982 | } else if (enforce > 0) { |
| 983 | /* SELinux in enforcing mode but load_policy failed */ |
Denis Vlasenko | b716754 | 2007-02-27 19:20:00 +0000 | [diff] [blame] | 984 | message(L_CONSOLE, "Cannot load SELinux Policy. " |
| 985 | "Machine is in enforcing mode. Halting now."); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 986 | exit(EXIT_FAILURE); |
Rob Landley | b3ede5a | 2006-03-27 23:09:12 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | #endif /* CONFIG_SELINUX */ |
| 990 | |
Denis Vlasenko | 2afabe8 | 2007-12-10 07:06:04 +0000 | [diff] [blame] | 991 | /* Make the command line just say "init" - thats all, nothing else */ |
| 992 | strncpy(argv[0], "init", strlen(argv[0])); |
| 993 | /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */ |
| 994 | while (*++argv) |
| 995 | memset(*argv, 0, strlen(*argv)); |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 996 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 997 | /* Now run everything that needs to be run */ |
| 998 | |
| 999 | /* First run the sysinit command */ |
Eric Andersen | 1644db9 | 2001-09-05 20:18:15 +0000 | [diff] [blame] | 1000 | run_actions(SYSINIT); |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 1001 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1002 | /* Next run anything that wants to block */ |
Eric Andersen | 1644db9 | 2001-09-05 20:18:15 +0000 | [diff] [blame] | 1003 | run_actions(WAIT); |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 1004 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1005 | /* Next run anything to be run only once */ |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 1006 | run_actions(ONCE); |
| 1007 | |
Eric Andersen | 6fd0e31 | 2003-07-22 09:48:56 +0000 | [diff] [blame] | 1008 | /* Redefine SIGHUP to reread /etc/inittab */ |
Denis Vlasenko | ad4da98 | 2008-04-05 04:24:23 +0000 | [diff] [blame] | 1009 | #if ENABLE_FEATURE_USE_INITTAB |
| 1010 | signal(SIGHUP, reload_signal); |
| 1011 | #else |
| 1012 | signal(SIGHUP, SIG_IGN); |
| 1013 | #endif |
Eric Andersen | 82baf63 | 2004-10-08 08:21:54 +0000 | [diff] [blame] | 1014 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1015 | /* Now run the looping stuff for the rest of forever */ |
| 1016 | while (1) { |
Denis Vlasenko | ad4da98 | 2008-04-05 04:24:23 +0000 | [diff] [blame] | 1017 | /* run the respawn/askfirst stuff */ |
| 1018 | run_actions(RESPAWN | ASKFIRST); |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 1019 | |
| 1020 | /* Don't consume all CPU time -- sleep a bit */ |
| 1021 | sleep(1); |
| 1022 | |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1023 | /* Wait for any child process to exit */ |
Bernhard Reutner-Fischer | c58dbf2 | 2006-05-30 12:16:54 +0000 | [diff] [blame] | 1024 | wpid = wait(NULL); |
Eric Andersen | 87812dc | 2004-04-12 19:21:54 +0000 | [diff] [blame] | 1025 | while (wpid > 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1026 | /* Find out who died and clean up their corpse */ |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 1027 | for (a = init_action_list; a; a = a->next) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1028 | if (a->pid == wpid) { |
Eric Andersen | 0298be8 | 2002-03-05 15:12:19 +0000 | [diff] [blame] | 1029 | /* Set the pid to 0 so that the process gets |
| 1030 | * restarted by run_actions() */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1031 | a->pid = 0; |
Denis Vlasenko | 7a2ca5e | 2007-02-21 00:15:20 +0000 | [diff] [blame] | 1032 | message(L_LOG, "process '%s' (pid %d) exited. " |
Denis Vlasenko | a37e713 | 2008-02-19 02:57:07 +0000 | [diff] [blame] | 1033 | "Scheduling for restart.", |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 1034 | a->command, wpid); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1035 | } |
| 1036 | } |
Eric Andersen | cf1fee0 | 2002-12-17 09:48:16 +0000 | [diff] [blame] | 1037 | /* see if anyone else is waiting to be reaped */ |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1038 | wpid = wait_any_nohang(NULL); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1039 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1040 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1041 | } |