diff options
Diffstat (limited to 'init/init.c')
-rw-r--r-- | init/init.c | 291 |
1 files changed, 148 insertions, 143 deletions
diff --git a/init/init.c b/init/init.c index a7bbd5e64..a2cc3b5f5 100644 --- a/init/init.c +++ b/init/init.c | |||
@@ -108,138 +108,6 @@ | |||
108 | //config: Note that on Linux, init attempts to detect serial terminal and | 108 | //config: Note that on Linux, init attempts to detect serial terminal and |
109 | //config: sets TERM to "vt102" if one is found. | 109 | //config: sets TERM to "vt102" if one is found. |
110 | 110 | ||
111 | //usage:#define init_trivial_usage | ||
112 | //usage: "" | ||
113 | //usage:#define init_full_usage "\n\n" | ||
114 | //usage: "Init is the parent of all processes" | ||
115 | //usage: | ||
116 | //usage:#define init_notes_usage | ||
117 | //usage: "This version of init is designed to be run only by the kernel.\n" | ||
118 | //usage: "\n" | ||
119 | //usage: "BusyBox init doesn't support multiple runlevels. The runlevels field of\n" | ||
120 | //usage: "the /etc/inittab file is completely ignored by BusyBox init. If you want\n" | ||
121 | //usage: "runlevels, use sysvinit.\n" | ||
122 | //usage: "\n" | ||
123 | //usage: "BusyBox init works just fine without an inittab. If no inittab is found,\n" | ||
124 | //usage: "it has the following default behavior:\n" | ||
125 | //usage: "\n" | ||
126 | //usage: " ::sysinit:/etc/init.d/rcS\n" | ||
127 | //usage: " ::askfirst:/bin/sh\n" | ||
128 | //usage: " ::ctrlaltdel:/sbin/reboot\n" | ||
129 | //usage: " ::shutdown:/sbin/swapoff -a\n" | ||
130 | //usage: " ::shutdown:/bin/umount -a -r\n" | ||
131 | //usage: " ::restart:/sbin/init\n" | ||
132 | //usage: "\n" | ||
133 | //usage: "if it detects that /dev/console is _not_ a serial console, it will also run:\n" | ||
134 | //usage: "\n" | ||
135 | //usage: " tty2::askfirst:/bin/sh\n" | ||
136 | //usage: " tty3::askfirst:/bin/sh\n" | ||
137 | //usage: " tty4::askfirst:/bin/sh\n" | ||
138 | //usage: "\n" | ||
139 | //usage: "If you choose to use an /etc/inittab file, the inittab entry format is as follows:\n" | ||
140 | //usage: "\n" | ||
141 | //usage: " <id>:<runlevels>:<action>:<process>\n" | ||
142 | //usage: "\n" | ||
143 | //usage: " <id>:\n" | ||
144 | //usage: "\n" | ||
145 | //usage: " WARNING: This field has a non-traditional meaning for BusyBox init!\n" | ||
146 | //usage: " The id field is used by BusyBox init to specify the controlling tty for\n" | ||
147 | //usage: " the specified process to run on. The contents of this field are\n" | ||
148 | //usage: " appended to \"/dev/\" and used as-is. There is no need for this field to\n" | ||
149 | //usage: " be unique, although if it isn't you may have strange results. If this\n" | ||
150 | //usage: " field is left blank, the controlling tty is set to the console. Also\n" | ||
151 | //usage: " note that if BusyBox detects that a serial console is in use, then only\n" | ||
152 | //usage: " entries whose controlling tty is either the serial console or /dev/null\n" | ||
153 | //usage: " will be run. BusyBox init does nothing with utmp. We don't need no\n" | ||
154 | //usage: " stinkin' utmp.\n" | ||
155 | //usage: "\n" | ||
156 | //usage: " <runlevels>:\n" | ||
157 | //usage: "\n" | ||
158 | //usage: " The runlevels field is completely ignored.\n" | ||
159 | //usage: "\n" | ||
160 | //usage: " <action>:\n" | ||
161 | //usage: "\n" | ||
162 | //usage: " Valid actions include: sysinit, respawn, askfirst, wait,\n" | ||
163 | //usage: " once, restart, ctrlaltdel, and shutdown.\n" | ||
164 | //usage: "\n" | ||
165 | //usage: " The available actions can be classified into two groups: actions\n" | ||
166 | //usage: " that are run only once, and actions that are re-run when the specified\n" | ||
167 | //usage: " process exits.\n" | ||
168 | //usage: "\n" | ||
169 | //usage: " Run only-once actions:\n" | ||
170 | //usage: "\n" | ||
171 | //usage: " 'sysinit' is the first item run on boot. init waits until all\n" | ||
172 | //usage: " sysinit actions are completed before continuing. Following the\n" | ||
173 | //usage: " completion of all sysinit actions, all 'wait' actions are run.\n" | ||
174 | //usage: " 'wait' actions, like 'sysinit' actions, cause init to wait until\n" | ||
175 | //usage: " the specified task completes. 'once' actions are asynchronous,\n" | ||
176 | //usage: " therefore, init does not wait for them to complete. 'restart' is\n" | ||
177 | //usage: " the action taken to restart the init process. By default this should\n" | ||
178 | //usage: " simply run /sbin/init, but can be a script which runs pivot_root or it\n" | ||
179 | //usage: " can do all sorts of other interesting things. The 'ctrlaltdel' init\n" | ||
180 | //usage: " actions are run when the system detects that someone on the system\n" | ||
181 | //usage: " console has pressed the CTRL-ALT-DEL key combination. Typically one\n" | ||
182 | //usage: " wants to run 'reboot' at this point to cause the system to reboot.\n" | ||
183 | //usage: " Finally the 'shutdown' action specifies the actions to taken when\n" | ||
184 | //usage: " init is told to reboot. Unmounting filesystems and disabling swap\n" | ||
185 | //usage: " is a very good here.\n" | ||
186 | //usage: "\n" | ||
187 | //usage: " Run repeatedly actions:\n" | ||
188 | //usage: "\n" | ||
189 | //usage: " 'respawn' actions are run after the 'once' actions. When a process\n" | ||
190 | //usage: " started with a 'respawn' action exits, init automatically restarts\n" | ||
191 | //usage: " it. Unlike sysvinit, BusyBox init does not stop processes from\n" | ||
192 | //usage: " respawning out of control. The 'askfirst' actions acts just like\n" | ||
193 | //usage: " respawn, except that before running the specified process it\n" | ||
194 | //usage: " displays the line \"Please press Enter to activate this console.\"\n" | ||
195 | //usage: " and then waits for the user to press enter before starting the\n" | ||
196 | //usage: " specified process.\n" | ||
197 | //usage: "\n" | ||
198 | //usage: " Unrecognized actions (like initdefault) will cause init to emit an\n" | ||
199 | //usage: " error message, and then go along with its business. All actions are\n" | ||
200 | //usage: " run in the order they appear in /etc/inittab.\n" | ||
201 | //usage: "\n" | ||
202 | //usage: " <process>:\n" | ||
203 | //usage: "\n" | ||
204 | //usage: " Specifies the process to be executed and its command line.\n" | ||
205 | //usage: "\n" | ||
206 | //usage: "Example /etc/inittab file:\n" | ||
207 | //usage: "\n" | ||
208 | //usage: " # This is run first except when booting in single-user mode\n" | ||
209 | //usage: " #\n" | ||
210 | //usage: " ::sysinit:/etc/init.d/rcS\n" | ||
211 | //usage: " \n" | ||
212 | //usage: " # /bin/sh invocations on selected ttys\n" | ||
213 | //usage: " #\n" | ||
214 | //usage: " # Start an \"askfirst\" shell on the console (whatever that may be)\n" | ||
215 | //usage: " ::askfirst:-/bin/sh\n" | ||
216 | //usage: " # Start an \"askfirst\" shell on /dev/tty2-4\n" | ||
217 | //usage: " tty2::askfirst:-/bin/sh\n" | ||
218 | //usage: " tty3::askfirst:-/bin/sh\n" | ||
219 | //usage: " tty4::askfirst:-/bin/sh\n" | ||
220 | //usage: " \n" | ||
221 | //usage: " # /sbin/getty invocations for selected ttys\n" | ||
222 | //usage: " #\n" | ||
223 | //usage: " tty4::respawn:/sbin/getty 38400 tty4\n" | ||
224 | //usage: " tty5::respawn:/sbin/getty 38400 tty5\n" | ||
225 | //usage: " \n" | ||
226 | //usage: " \n" | ||
227 | //usage: " # Example of how to put a getty on a serial line (for a terminal)\n" | ||
228 | //usage: " #\n" | ||
229 | //usage: " #::respawn:/sbin/getty -L ttyS0 9600 vt100\n" | ||
230 | //usage: " #::respawn:/sbin/getty -L ttyS1 9600 vt100\n" | ||
231 | //usage: " #\n" | ||
232 | //usage: " # Example how to put a getty on a modem line\n" | ||
233 | //usage: " #::respawn:/sbin/getty 57600 ttyS2\n" | ||
234 | //usage: " \n" | ||
235 | //usage: " # Stuff to do when restarting the init process\n" | ||
236 | //usage: " ::restart:/sbin/init\n" | ||
237 | //usage: " \n" | ||
238 | //usage: " # Stuff to do before rebooting\n" | ||
239 | //usage: " ::ctrlaltdel:/sbin/reboot\n" | ||
240 | //usage: " ::shutdown:/bin/umount -a -r\n" | ||
241 | //usage: " ::shutdown:/sbin/swapoff -a\n" | ||
242 | |||
243 | #include "libbb.h" | 111 | #include "libbb.h" |
244 | #include <syslog.h> | 112 | #include <syslog.h> |
245 | #include <paths.h> | 113 | #include <paths.h> |
@@ -533,20 +401,22 @@ static void init_exec(const char *command) | |||
533 | char buf[COMMAND_SIZE + 6]; /* COMMAND_SIZE+strlen("exec ")+1 */ | 401 | char buf[COMMAND_SIZE + 6]; /* COMMAND_SIZE+strlen("exec ")+1 */ |
534 | int dash = (command[0] == '-' /* maybe? && command[1] == '/' */); | 402 | int dash = (command[0] == '-' /* maybe? && command[1] == '/' */); |
535 | 403 | ||
404 | command += dash; | ||
405 | |||
536 | /* See if any special /bin/sh requiring characters are present */ | 406 | /* See if any special /bin/sh requiring characters are present */ |
537 | if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) { | 407 | if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) { |
538 | strcpy(buf, "exec "); | 408 | sprintf(buf, "exec %s", command); /* excluding "-" */ |
539 | strcpy(buf + 5, command + dash); /* excluding "-" */ | ||
540 | /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */ | 409 | /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */ |
541 | cmd[0] = (char*)(LIBBB_DEFAULT_LOGIN_SHELL + !dash); | 410 | cmd[0] = (char*)(LIBBB_DEFAULT_LOGIN_SHELL + !dash); |
542 | cmd[1] = (char*)"-c"; | 411 | cmd[1] = (char*)"-c"; |
543 | cmd[2] = buf; | 412 | cmd[2] = buf; |
544 | cmd[3] = NULL; | 413 | cmd[3] = NULL; |
414 | command = LIBBB_DEFAULT_LOGIN_SHELL + 1; | ||
545 | } else { | 415 | } else { |
546 | /* Convert command (char*) into cmd (char**, one word per string) */ | 416 | /* Convert command (char*) into cmd (char**, one word per string) */ |
547 | char *word, *next; | 417 | char *word, *next; |
548 | int i = 0; | 418 | int i = 0; |
549 | next = strcpy(buf, command); /* including "-" */ | 419 | next = strcpy(buf, command - dash); /* command including "-" */ |
550 | while ((word = strsep(&next, " \t")) != NULL) { | 420 | while ((word = strsep(&next, " \t")) != NULL) { |
551 | if (*word != '\0') { /* not two spaces/tabs together? */ | 421 | if (*word != '\0') { /* not two spaces/tabs together? */ |
552 | cmd[i] = word; | 422 | cmd[i] = word; |
@@ -557,14 +427,14 @@ static void init_exec(const char *command) | |||
557 | } | 427 | } |
558 | /* If we saw leading "-", it is interactive shell. | 428 | /* If we saw leading "-", it is interactive shell. |
559 | * Try harder to give it a controlling tty. | 429 | * Try harder to give it a controlling tty. |
560 | * And skip "-" in actual exec call. */ | 430 | */ |
561 | if (dash) { | 431 | if (ENABLE_FEATURE_INIT_SCTTY && dash) { |
562 | /* _Attempt_ to make stdin a controlling tty. */ | 432 | /* _Attempt_ to make stdin a controlling tty. */ |
563 | if (ENABLE_FEATURE_INIT_SCTTY) | 433 | ioctl(STDIN_FILENO, TIOCSCTTY, 0 /*only try, don't steal*/); |
564 | ioctl(STDIN_FILENO, TIOCSCTTY, 0 /*only try, don't steal*/); | ||
565 | } | 434 | } |
566 | BB_EXECVP(cmd[0] + dash, cmd); | 435 | /* Here command never contains the dash, cmd[0] might */ |
567 | message(L_LOG | L_CONSOLE, "can't run '%s': %s", cmd[0], strerror(errno)); | 436 | BB_EXECVP(command, cmd); |
437 | message(L_LOG | L_CONSOLE, "can't run '%s': %s", command, strerror(errno)); | ||
568 | /* returns if execvp fails */ | 438 | /* returns if execvp fails */ |
569 | } | 439 | } |
570 | 440 | ||
@@ -1095,9 +965,9 @@ int init_main(int argc UNUSED_PARAM, char **argv) | |||
1095 | if (!DEBUG_INIT) { | 965 | if (!DEBUG_INIT) { |
1096 | /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */ | 966 | /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */ |
1097 | if (getpid() != 1 | 967 | if (getpid() != 1 |
1098 | && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc")) | 968 | && (!ENABLE_FEATURE_INITRD || applet_name[0] != 'l') /* not linuxrc? */ |
1099 | ) { | 969 | ) { |
1100 | bb_show_usage(); | 970 | bb_error_msg_and_die("must be run as PID 1"); |
1101 | } | 971 | } |
1102 | #ifdef RB_DISABLE_CAD | 972 | #ifdef RB_DISABLE_CAD |
1103 | /* Turn off rebooting via CTL-ALT-DEL - we get a | 973 | /* Turn off rebooting via CTL-ALT-DEL - we get a |
@@ -1286,3 +1156,138 @@ int init_main(int argc UNUSED_PARAM, char **argv) | |||
1286 | } | 1156 | } |
1287 | } /* while (1) */ | 1157 | } /* while (1) */ |
1288 | } | 1158 | } |
1159 | |||
1160 | //usage:#define linuxrc_trivial_usage NOUSAGE_STR | ||
1161 | //usage:#define linuxrc_full_usage "" | ||
1162 | |||
1163 | //usage:#define init_trivial_usage | ||
1164 | //usage: "" | ||
1165 | //usage:#define init_full_usage "\n\n" | ||
1166 | //usage: "Init is the parent of all processes" | ||
1167 | //usage: | ||
1168 | //usage:#define init_notes_usage | ||
1169 | //usage: "This version of init is designed to be run only by the kernel.\n" | ||
1170 | //usage: "\n" | ||
1171 | //usage: "BusyBox init doesn't support multiple runlevels. The runlevels field of\n" | ||
1172 | //usage: "the /etc/inittab file is completely ignored by BusyBox init. If you want\n" | ||
1173 | //usage: "runlevels, use sysvinit.\n" | ||
1174 | //usage: "\n" | ||
1175 | //usage: "BusyBox init works just fine without an inittab. If no inittab is found,\n" | ||
1176 | //usage: "it has the following default behavior:\n" | ||
1177 | //usage: "\n" | ||
1178 | //usage: " ::sysinit:/etc/init.d/rcS\n" | ||
1179 | //usage: " ::askfirst:/bin/sh\n" | ||
1180 | //usage: " ::ctrlaltdel:/sbin/reboot\n" | ||
1181 | //usage: " ::shutdown:/sbin/swapoff -a\n" | ||
1182 | //usage: " ::shutdown:/bin/umount -a -r\n" | ||
1183 | //usage: " ::restart:/sbin/init\n" | ||
1184 | //usage: "\n" | ||
1185 | //usage: "if it detects that /dev/console is _not_ a serial console, it will also run:\n" | ||
1186 | //usage: "\n" | ||
1187 | //usage: " tty2::askfirst:/bin/sh\n" | ||
1188 | //usage: " tty3::askfirst:/bin/sh\n" | ||
1189 | //usage: " tty4::askfirst:/bin/sh\n" | ||
1190 | //usage: "\n" | ||
1191 | //usage: "If you choose to use an /etc/inittab file, the inittab entry format is as follows:\n" | ||
1192 | //usage: "\n" | ||
1193 | //usage: " <id>:<runlevels>:<action>:<process>\n" | ||
1194 | //usage: "\n" | ||
1195 | //usage: " <id>:\n" | ||
1196 | //usage: "\n" | ||
1197 | //usage: " WARNING: This field has a non-traditional meaning for BusyBox init!\n" | ||
1198 | //usage: " The id field is used by BusyBox init to specify the controlling tty for\n" | ||
1199 | //usage: " the specified process to run on. The contents of this field are\n" | ||
1200 | //usage: " appended to \"/dev/\" and used as-is. There is no need for this field to\n" | ||
1201 | //usage: " be unique, although if it isn't you may have strange results. If this\n" | ||
1202 | //usage: " field is left blank, the controlling tty is set to the console. Also\n" | ||
1203 | //usage: " note that if BusyBox detects that a serial console is in use, then only\n" | ||
1204 | //usage: " entries whose controlling tty is either the serial console or /dev/null\n" | ||
1205 | //usage: " will be run. BusyBox init does nothing with utmp. We don't need no\n" | ||
1206 | //usage: " stinkin' utmp.\n" | ||
1207 | //usage: "\n" | ||
1208 | //usage: " <runlevels>:\n" | ||
1209 | //usage: "\n" | ||
1210 | //usage: " The runlevels field is completely ignored.\n" | ||
1211 | //usage: "\n" | ||
1212 | //usage: " <action>:\n" | ||
1213 | //usage: "\n" | ||
1214 | //usage: " Valid actions include: sysinit, respawn, askfirst, wait,\n" | ||
1215 | //usage: " once, restart, ctrlaltdel, and shutdown.\n" | ||
1216 | //usage: "\n" | ||
1217 | //usage: " The available actions can be classified into two groups: actions\n" | ||
1218 | //usage: " that are run only once, and actions that are re-run when the specified\n" | ||
1219 | //usage: " process exits.\n" | ||
1220 | //usage: "\n" | ||
1221 | //usage: " Run only-once actions:\n" | ||
1222 | //usage: "\n" | ||
1223 | //usage: " 'sysinit' is the first item run on boot. init waits until all\n" | ||
1224 | //usage: " sysinit actions are completed before continuing. Following the\n" | ||
1225 | //usage: " completion of all sysinit actions, all 'wait' actions are run.\n" | ||
1226 | //usage: " 'wait' actions, like 'sysinit' actions, cause init to wait until\n" | ||
1227 | //usage: " the specified task completes. 'once' actions are asynchronous,\n" | ||
1228 | //usage: " therefore, init does not wait for them to complete. 'restart' is\n" | ||
1229 | //usage: " the action taken to restart the init process. By default this should\n" | ||
1230 | //usage: " simply run /sbin/init, but can be a script which runs pivot_root or it\n" | ||
1231 | //usage: " can do all sorts of other interesting things. The 'ctrlaltdel' init\n" | ||
1232 | //usage: " actions are run when the system detects that someone on the system\n" | ||
1233 | //usage: " console has pressed the CTRL-ALT-DEL key combination. Typically one\n" | ||
1234 | //usage: " wants to run 'reboot' at this point to cause the system to reboot.\n" | ||
1235 | //usage: " Finally the 'shutdown' action specifies the actions to taken when\n" | ||
1236 | //usage: " init is told to reboot. Unmounting filesystems and disabling swap\n" | ||
1237 | //usage: " is a very good here.\n" | ||
1238 | //usage: "\n" | ||
1239 | //usage: " Run repeatedly actions:\n" | ||
1240 | //usage: "\n" | ||
1241 | //usage: " 'respawn' actions are run after the 'once' actions. When a process\n" | ||
1242 | //usage: " started with a 'respawn' action exits, init automatically restarts\n" | ||
1243 | //usage: " it. Unlike sysvinit, BusyBox init does not stop processes from\n" | ||
1244 | //usage: " respawning out of control. The 'askfirst' actions acts just like\n" | ||
1245 | //usage: " respawn, except that before running the specified process it\n" | ||
1246 | //usage: " displays the line \"Please press Enter to activate this console.\"\n" | ||
1247 | //usage: " and then waits for the user to press enter before starting the\n" | ||
1248 | //usage: " specified process.\n" | ||
1249 | //usage: "\n" | ||
1250 | //usage: " Unrecognized actions (like initdefault) will cause init to emit an\n" | ||
1251 | //usage: " error message, and then go along with its business. All actions are\n" | ||
1252 | //usage: " run in the order they appear in /etc/inittab.\n" | ||
1253 | //usage: "\n" | ||
1254 | //usage: " <process>:\n" | ||
1255 | //usage: "\n" | ||
1256 | //usage: " Specifies the process to be executed and its command line.\n" | ||
1257 | //usage: "\n" | ||
1258 | //usage: "Example /etc/inittab file:\n" | ||
1259 | //usage: "\n" | ||
1260 | //usage: " # This is run first except when booting in single-user mode\n" | ||
1261 | //usage: " #\n" | ||
1262 | //usage: " ::sysinit:/etc/init.d/rcS\n" | ||
1263 | //usage: " \n" | ||
1264 | //usage: " # /bin/sh invocations on selected ttys\n" | ||
1265 | //usage: " #\n" | ||
1266 | //usage: " # Start an \"askfirst\" shell on the console (whatever that may be)\n" | ||
1267 | //usage: " ::askfirst:-/bin/sh\n" | ||
1268 | //usage: " # Start an \"askfirst\" shell on /dev/tty2-4\n" | ||
1269 | //usage: " tty2::askfirst:-/bin/sh\n" | ||
1270 | //usage: " tty3::askfirst:-/bin/sh\n" | ||
1271 | //usage: " tty4::askfirst:-/bin/sh\n" | ||
1272 | //usage: " \n" | ||
1273 | //usage: " # /sbin/getty invocations for selected ttys\n" | ||
1274 | //usage: " #\n" | ||
1275 | //usage: " tty4::respawn:/sbin/getty 38400 tty4\n" | ||
1276 | //usage: " tty5::respawn:/sbin/getty 38400 tty5\n" | ||
1277 | //usage: " \n" | ||
1278 | //usage: " \n" | ||
1279 | //usage: " # Example of how to put a getty on a serial line (for a terminal)\n" | ||
1280 | //usage: " #\n" | ||
1281 | //usage: " #::respawn:/sbin/getty -L ttyS0 9600 vt100\n" | ||
1282 | //usage: " #::respawn:/sbin/getty -L ttyS1 9600 vt100\n" | ||
1283 | //usage: " #\n" | ||
1284 | //usage: " # Example how to put a getty on a modem line\n" | ||
1285 | //usage: " #::respawn:/sbin/getty 57600 ttyS2\n" | ||
1286 | //usage: " \n" | ||
1287 | //usage: " # Stuff to do when restarting the init process\n" | ||
1288 | //usage: " ::restart:/sbin/init\n" | ||
1289 | //usage: " \n" | ||
1290 | //usage: " # Stuff to do before rebooting\n" | ||
1291 | //usage: " ::ctrlaltdel:/sbin/reboot\n" | ||
1292 | //usage: " ::shutdown:/bin/umount -a -r\n" | ||
1293 | //usage: " ::shutdown:/sbin/swapoff -a\n" | ||