init.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini init implementation for busybox
  4. *
  5. * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
  6. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  7. * Adjusted by so many folks, it's impossible to keep track.
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. */
  11. //applet:IF_INIT(APPLET(init, BB_DIR_SBIN, BB_SUID_DROP))
  12. //applet:IF_FEATURE_INITRD(APPLET_ODDNAME(linuxrc, init, BB_DIR_ROOT, BB_SUID_DROP, linuxrc))
  13. //kbuild:lib-$(CONFIG_INIT) += init.o
  14. //config:config INIT
  15. //config: bool "init"
  16. //config: default y
  17. //config: select FEATURE_SYSLOG
  18. //config: help
  19. //config: init is the first program run when the system boots.
  20. //config:
  21. //config:config FEATURE_USE_INITTAB
  22. //config: bool "Support reading an inittab file"
  23. //config: default y
  24. //config: depends on INIT
  25. //config: help
  26. //config: Allow init to read an inittab file when the system boot.
  27. //config:
  28. //config:config FEATURE_KILL_REMOVED
  29. //config: bool "Support killing processes that have been removed from inittab"
  30. //config: default n
  31. //config: depends on FEATURE_USE_INITTAB
  32. //config: help
  33. //config: When respawn entries are removed from inittab and a SIGHUP is
  34. //config: sent to init, this option will make init kill the processes
  35. //config: that have been removed.
  36. //config:
  37. //config:config FEATURE_KILL_DELAY
  38. //config: int "How long to wait between TERM and KILL (0 - send TERM only)" if FEATURE_KILL_REMOVED
  39. //config: range 0 1024
  40. //config: default 0
  41. //config: depends on FEATURE_KILL_REMOVED
  42. //config: help
  43. //config: With nonzero setting, init sends TERM, forks, child waits N
  44. //config: seconds, sends KILL and exits. Setting it too high is unwise
  45. //config: (child will hang around for too long and could actually kill
  46. //config: the wrong process!)
  47. //config:
  48. //config:config FEATURE_INIT_SCTTY
  49. //config: bool "Run commands with leading dash with controlling tty"
  50. //config: default y
  51. //config: depends on INIT
  52. //config: help
  53. //config: If this option is enabled, init will try to give a controlling
  54. //config: tty to any command which has leading hyphen (often it's "-/bin/sh").
  55. //config: More precisely, init will do "ioctl(STDIN_FILENO, TIOCSCTTY, 0)".
  56. //config: If device attached to STDIN_FILENO can be a ctty but is not yet
  57. //config: a ctty for other session, it will become this process' ctty.
  58. //config: This is not the traditional init behavour, but is often what you want
  59. //config: in an embedded system where the console is only accessed during
  60. //config: development or for maintenance.
  61. //config: NB: using cttyhack applet may work better.
  62. //config:
  63. //config:config FEATURE_INIT_SYSLOG
  64. //config: bool "Enable init to write to syslog"
  65. //config: default y
  66. //config: depends on INIT
  67. //config:
  68. //config:config FEATURE_EXTRA_QUIET
  69. //config: bool "Be _extra_ quiet on boot"
  70. //config: default y
  71. //config: depends on INIT
  72. //config: help
  73. //config: Prevent init from logging some messages to the console during boot.
  74. //config:
  75. //config:config FEATURE_INIT_COREDUMPS
  76. //config: bool "Support dumping core for child processes (debugging only)"
  77. //config: default y
  78. //config: depends on INIT
  79. //config: help
  80. //config: If this option is enabled and the file /.init_enable_core
  81. //config: exists, then init will call setrlimit() to allow unlimited
  82. //config: core file sizes. If this option is disabled, processes
  83. //config: will not generate any core files.
  84. //config:
  85. //config:config FEATURE_INITRD
  86. //config: bool "Support running init from within an initrd (not initramfs)"
  87. //config: default y
  88. //config: depends on INIT
  89. //config: help
  90. //config: Legacy support for running init under the old-style initrd. Allows
  91. //config: the name linuxrc to act as init, and it doesn't assume init is PID 1.
  92. //config:
  93. //config: This does not apply to initramfs, which runs /init as PID 1 and
  94. //config: requires no special support.
  95. //config:
  96. //config:config INIT_TERMINAL_TYPE
  97. //config: string "Initial terminal type"
  98. //config: default "linux"
  99. //config: depends on INIT
  100. //config: help
  101. //config: This is the initial value set by init for the TERM environment
  102. //config: variable. This variable is used by programs which make use of
  103. //config: extended terminal capabilities.
  104. //config:
  105. //config: Note that on Linux, init attempts to detect serial terminal and
  106. //config: sets TERM to "vt102" if one is found.
  107. #define DEBUG_SEGV_HANDLER 0
  108. #include "libbb.h"
  109. #include <syslog.h>
  110. #include <paths.h>
  111. #include <sys/resource.h>
  112. #ifdef __linux__
  113. # include <linux/vt.h>
  114. # include <sys/sysinfo.h>
  115. #endif
  116. #include "reboot.h" /* reboot() constants */
  117. #if DEBUG_SEGV_HANDLER
  118. # undef _GNU_SOURCE
  119. # define _GNU_SOURCE 1
  120. # undef __USE_GNU
  121. # define __USE_GNU 1
  122. # include <execinfo.h>
  123. # include <sys/ucontext.h>
  124. #endif
  125. /* Used only for sanitizing purposes in set_sane_term() below. On systems where
  126. * the baud rate is stored in a separate field, we can safely disable them. */
  127. #ifndef CBAUD
  128. # define CBAUD 0
  129. # define CBAUDEX 0
  130. #endif
  131. /* Was a CONFIG_xxx option. A lot of people were building
  132. * not fully functional init by switching it on! */
  133. #define DEBUG_INIT 0
  134. #define COMMAND_SIZE 256
  135. #define CONSOLE_NAME_SIZE 32
  136. /* Default sysinit script. */
  137. #ifndef INIT_SCRIPT
  138. #define INIT_SCRIPT "/etc/init.d/rcS"
  139. #endif
  140. /* Each type of actions can appear many times. They will be
  141. * handled in order. RESTART is an exception, only 1st is used.
  142. */
  143. /* Start these actions first and wait for completion */
  144. #define SYSINIT 0x01
  145. /* Start these after SYSINIT and wait for completion */
  146. #define WAIT 0x02
  147. /* Start these after WAIT and *dont* wait for completion */
  148. #define ONCE 0x04
  149. /*
  150. * NB: while SYSINIT/WAIT/ONCE are being processed,
  151. * SIGHUP ("reread /etc/inittab") will be processed only after
  152. * each group of actions. If new inittab adds, say, a SYSINIT action,
  153. * it will not be run, since init is already "past SYSINIT stage".
  154. */
  155. /* Start these after ONCE are started, restart on exit */
  156. #define RESPAWN 0x08
  157. /* Like RESPAWN, but wait for <Enter> to be pressed on tty */
  158. #define ASKFIRST 0x10
  159. /*
  160. * Start these on SIGINT, and wait for completion.
  161. * Then go back to respawning RESPAWN and ASKFIRST actions.
  162. * NB: kernel sends SIGINT to us if Ctrl-Alt-Del was pressed.
  163. */
  164. #define CTRLALTDEL 0x20
  165. /*
  166. * Start these before killing all processes in preparation for
  167. * running RESTART actions or doing low-level halt/reboot/poweroff
  168. * (initiated by SIGUSR1/SIGTERM/SIGUSR2).
  169. * Wait for completion before proceeding.
  170. */
  171. #define SHUTDOWN 0x40
  172. /*
  173. * exec() on SIGQUIT. SHUTDOWN actions are started and waited for,
  174. * then all processes are killed, then init exec's 1st RESTART action,
  175. * replacing itself by it. If no RESTART action specified,
  176. * SIGQUIT has no effect.
  177. */
  178. #define RESTART 0x80
  179. /* A linked list of init_actions, to be read from inittab */
  180. struct init_action {
  181. struct init_action *next;
  182. pid_t pid;
  183. uint8_t action_type;
  184. char terminal[CONSOLE_NAME_SIZE];
  185. char command[COMMAND_SIZE];
  186. };
  187. static struct init_action *init_action_list = NULL;
  188. static const char *log_console = VC_5;
  189. enum {
  190. L_LOG = 0x1,
  191. L_CONSOLE = 0x2,
  192. };
  193. /* Print a message to the specified device.
  194. * "where" may be bitwise-or'd from L_LOG | L_CONSOLE
  195. * NB: careful, we can be called after vfork!
  196. */
  197. #define dbg_message(...) do { if (DEBUG_INIT) message(__VA_ARGS__); } while (0)
  198. static void message(int where, const char *fmt, ...)
  199. __attribute__ ((format(printf, 2, 3)));
  200. static void message(int where, const char *fmt, ...)
  201. {
  202. va_list arguments;
  203. unsigned l;
  204. char msg[128];
  205. msg[0] = '\r';
  206. va_start(arguments, fmt);
  207. l = 1 + vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments);
  208. if (l > sizeof(msg) - 1)
  209. l = sizeof(msg) - 1;
  210. va_end(arguments);
  211. #if ENABLE_FEATURE_INIT_SYSLOG
  212. msg[l] = '\0';
  213. if (where & L_LOG) {
  214. /* Log the message to syslogd */
  215. openlog(applet_name, 0, LOG_DAEMON);
  216. /* don't print "\r" */
  217. syslog(LOG_INFO, "%s", msg + 1);
  218. closelog();
  219. }
  220. msg[l++] = '\n';
  221. msg[l] = '\0';
  222. #else
  223. {
  224. static int log_fd = -1;
  225. msg[l++] = '\n';
  226. msg[l] = '\0';
  227. /* Take full control of the log tty, and never close it.
  228. * It's mine, all mine! Muhahahaha! */
  229. if (log_fd < 0) {
  230. if (!log_console) {
  231. log_fd = STDERR_FILENO;
  232. } else {
  233. log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
  234. if (log_fd < 0) {
  235. bb_error_msg("can't log to %s", log_console);
  236. where = L_CONSOLE;
  237. } else {
  238. close_on_exec_on(log_fd);
  239. }
  240. }
  241. }
  242. if (where & L_LOG) {
  243. full_write(log_fd, msg, l);
  244. if (log_fd == STDERR_FILENO)
  245. return; /* don't print dup messages */
  246. }
  247. }
  248. #endif
  249. if (where & L_CONSOLE) {
  250. /* Send console messages to console so people will see them. */
  251. full_write(STDERR_FILENO, msg, l);
  252. }
  253. }
  254. static void console_init(void)
  255. {
  256. #ifdef VT_OPENQRY
  257. int vtno;
  258. #endif
  259. char *s;
  260. s = getenv("CONSOLE");
  261. if (!s)
  262. s = getenv("console");
  263. if (s) {
  264. int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY);
  265. if (fd >= 0) {
  266. dup2(fd, STDIN_FILENO);
  267. dup2(fd, STDOUT_FILENO);
  268. xmove_fd(fd, STDERR_FILENO);
  269. }
  270. dbg_message(L_LOG, "console='%s'", s);
  271. } else {
  272. /* Make sure fd 0,1,2 are not closed
  273. * (so that they won't be used by future opens) */
  274. bb_sanitize_stdio();
  275. // Users report problems
  276. // /* Make sure init can't be blocked by writing to stderr */
  277. // fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
  278. }
  279. s = getenv("TERM");
  280. #ifdef VT_OPENQRY
  281. if (ioctl(STDIN_FILENO, VT_OPENQRY, &vtno) != 0) {
  282. /* Not a linux terminal, probably serial console.
  283. * Force the TERM setting to vt102
  284. * if TERM is set to linux (the default) */
  285. if (!s || strcmp(s, "linux") == 0)
  286. putenv((char*)"TERM=vt102");
  287. if (!ENABLE_FEATURE_INIT_SYSLOG)
  288. log_console = NULL;
  289. } else
  290. #endif
  291. if (!s)
  292. putenv((char*)"TERM=" CONFIG_INIT_TERMINAL_TYPE);
  293. }
  294. /* Set terminal settings to reasonable defaults.
  295. * NB: careful, we can be called after vfork! */
  296. static void set_sane_term(void)
  297. {
  298. struct termios tty;
  299. tcgetattr(STDIN_FILENO, &tty);
  300. /* set control chars */
  301. tty.c_cc[VINTR] = 3; /* C-c */
  302. tty.c_cc[VQUIT] = 28; /* C-\ */
  303. tty.c_cc[VERASE] = 127; /* C-? */
  304. tty.c_cc[VKILL] = 21; /* C-u */
  305. tty.c_cc[VEOF] = 4; /* C-d */
  306. tty.c_cc[VSTART] = 17; /* C-q */
  307. tty.c_cc[VSTOP] = 19; /* C-s */
  308. tty.c_cc[VSUSP] = 26; /* C-z */
  309. #ifdef __linux__
  310. /* use line discipline 0 */
  311. tty.c_line = 0;
  312. #endif
  313. /* Make it be sane */
  314. #ifndef CRTSCTS
  315. # define CRTSCTS 0
  316. #endif
  317. /* added CRTSCTS to fix Debian bug 528560 */
  318. tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD | CRTSCTS;
  319. tty.c_cflag |= CREAD | HUPCL | CLOCAL;
  320. /* input modes */
  321. tty.c_iflag = ICRNL | IXON | IXOFF;
  322. /* output modes */
  323. tty.c_oflag = OPOST | ONLCR;
  324. /* local modes */
  325. tty.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
  326. tcsetattr_stdin_TCSANOW(&tty);
  327. }
  328. /* Open the new terminal device.
  329. * NB: careful, we can be called after vfork! */
  330. static int open_stdio_to_tty(const char* tty_name)
  331. {
  332. /* empty tty_name means "use init's tty", else... */
  333. if (tty_name[0]) {
  334. int fd;
  335. close(STDIN_FILENO);
  336. /* fd can be only < 0 or 0: */
  337. fd = device_open(tty_name, O_RDWR);
  338. if (fd) {
  339. message(L_LOG | L_CONSOLE, "can't open %s: %s",
  340. tty_name, strerror(errno));
  341. return 0; /* failure */
  342. }
  343. dup2(STDIN_FILENO, STDOUT_FILENO);
  344. dup2(STDIN_FILENO, STDERR_FILENO);
  345. }
  346. set_sane_term();
  347. return 1; /* success */
  348. }
  349. static void reset_sighandlers_and_unblock_sigs(void)
  350. {
  351. bb_signals(0
  352. + (1 << SIGUSR1)
  353. + (1 << SIGUSR2)
  354. + (1 << SIGTERM)
  355. + (1 << SIGQUIT)
  356. + (1 << SIGINT)
  357. + (1 << SIGHUP)
  358. + (1 << SIGTSTP)
  359. + (1 << SIGSTOP)
  360. , SIG_DFL);
  361. sigprocmask_allsigs(SIG_UNBLOCK);
  362. }
  363. /* Wrapper around exec:
  364. * Takes string (max COMMAND_SIZE chars).
  365. * If chars like '>' detected, execs '[-]/bin/sh -c "exec ......."'.
  366. * Otherwise splits words on whitespace, deals with leading dash,
  367. * and uses plain exec().
  368. * NB: careful, we can be called after vfork!
  369. */
  370. static void init_exec(const char *command)
  371. {
  372. char *cmd[COMMAND_SIZE / 2];
  373. char buf[COMMAND_SIZE + 6]; /* COMMAND_SIZE+strlen("exec ")+1 */
  374. int dash = (command[0] == '-' /* maybe? && command[1] == '/' */);
  375. command += dash;
  376. /* See if any special /bin/sh requiring characters are present */
  377. if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
  378. sprintf(buf, "exec %s", command); /* excluding "-" */
  379. /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */
  380. cmd[0] = (char*)(LIBBB_DEFAULT_LOGIN_SHELL + !dash);
  381. cmd[1] = (char*)"-c";
  382. cmd[2] = buf;
  383. cmd[3] = NULL;
  384. command = LIBBB_DEFAULT_LOGIN_SHELL + 1;
  385. } else {
  386. /* Convert command (char*) into cmd (char**, one word per string) */
  387. char *word, *next;
  388. int i = 0;
  389. next = strcpy(buf, command - dash); /* command including "-" */
  390. command = next + dash;
  391. while ((word = strsep(&next, " \t")) != NULL) {
  392. if (*word != '\0') { /* not two spaces/tabs together? */
  393. cmd[i] = word;
  394. i++;
  395. }
  396. }
  397. cmd[i] = NULL;
  398. }
  399. /* If we saw leading "-", it is interactive shell.
  400. * Try harder to give it a controlling tty.
  401. */
  402. if (ENABLE_FEATURE_INIT_SCTTY && dash) {
  403. /* _Attempt_ to make stdin a controlling tty. */
  404. ioctl(STDIN_FILENO, TIOCSCTTY, 0 /*only try, don't steal*/);
  405. }
  406. /* Here command never contains the dash, cmd[0] might */
  407. BB_EXECVP(command, cmd);
  408. message(L_LOG | L_CONSOLE, "can't run '%s': %s", command, strerror(errno));
  409. /* returns if execvp fails */
  410. }
  411. /* Used only by run_actions */
  412. static pid_t run(const struct init_action *a)
  413. {
  414. pid_t pid;
  415. /* Careful: don't be affected by a signal in vforked child */
  416. sigprocmask_allsigs(SIG_BLOCK);
  417. if (BB_MMU && (a->action_type & ASKFIRST))
  418. pid = fork();
  419. else
  420. pid = vfork();
  421. if (pid < 0)
  422. message(L_LOG | L_CONSOLE, "can't fork");
  423. if (pid) {
  424. sigprocmask_allsigs(SIG_UNBLOCK);
  425. return pid; /* Parent or error */
  426. }
  427. /* Child */
  428. /* Reset signal handlers that were set by the parent process */
  429. reset_sighandlers_and_unblock_sigs();
  430. /* Create a new session and make ourself the process group leader */
  431. setsid();
  432. /* Open the new terminal device */
  433. if (!open_stdio_to_tty(a->terminal))
  434. _exit(EXIT_FAILURE);
  435. /* NB: on NOMMU we can't wait for input in child, so
  436. * "askfirst" will work the same as "respawn". */
  437. if (BB_MMU && (a->action_type & ASKFIRST)) {
  438. static const char press_enter[] ALIGN1 =
  439. #ifdef CUSTOMIZED_BANNER
  440. #include CUSTOMIZED_BANNER
  441. #endif
  442. "\nPlease press Enter to activate this console. ";
  443. char c;
  444. /*
  445. * Save memory by not exec-ing anything large (like a shell)
  446. * before the user wants it. This is critical if swap is not
  447. * enabled and the system has low memory. Generally this will
  448. * be run on the second virtual console, and the first will
  449. * be allowed to start a shell or whatever an init script
  450. * specifies.
  451. */
  452. dbg_message(L_LOG, "waiting for enter to start '%s'"
  453. "(pid %d, tty '%s')\n",
  454. a->command, getpid(), a->terminal);
  455. full_write(STDOUT_FILENO, press_enter, sizeof(press_enter) - 1);
  456. while (safe_read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
  457. continue;
  458. }
  459. /*
  460. * When a file named /.init_enable_core exists, setrlimit is called
  461. * before processes are spawned to set core file size as unlimited.
  462. * This is for debugging only. Don't use this is production, unless
  463. * you want core dumps lying about....
  464. */
  465. if (ENABLE_FEATURE_INIT_COREDUMPS) {
  466. if (access("/.init_enable_core", F_OK) == 0) {
  467. struct rlimit limit;
  468. limit.rlim_cur = RLIM_INFINITY;
  469. limit.rlim_max = RLIM_INFINITY;
  470. setrlimit(RLIMIT_CORE, &limit);
  471. }
  472. }
  473. /* Log the process name and args */
  474. message(L_LOG, "starting pid %d, tty '%s': '%s'",
  475. getpid(), a->terminal, a->command);
  476. /* Now run it. The new program will take over this PID,
  477. * so nothing further in init.c should be run. */
  478. init_exec(a->command);
  479. /* We're still here? Some error happened. */
  480. _exit(-1);
  481. }
  482. static struct init_action *mark_terminated(pid_t pid)
  483. {
  484. struct init_action *a;
  485. if (pid > 0) {
  486. update_utmp(pid, DEAD_PROCESS,
  487. /*tty_name:*/ NULL,
  488. /*username:*/ NULL,
  489. /*hostname:*/ NULL
  490. );
  491. for (a = init_action_list; a; a = a->next) {
  492. if (a->pid == pid) {
  493. a->pid = 0;
  494. return a;
  495. }
  496. }
  497. }
  498. return NULL;
  499. }
  500. static void waitfor(pid_t pid)
  501. {
  502. /* waitfor(run(x)): protect against failed fork inside run() */
  503. if (pid <= 0)
  504. return;
  505. /* Wait for any child (prevent zombies from exiting orphaned processes)
  506. * but exit the loop only when specified one has exited. */
  507. while (1) {
  508. pid_t wpid = wait(NULL);
  509. mark_terminated(wpid);
  510. /* Unsafe. SIGTSTP handler might have wait'ed it already */
  511. /*if (wpid == pid) break;*/
  512. /* More reliable: */
  513. if (kill(pid, 0))
  514. break;
  515. }
  516. }
  517. /* Run all commands of a particular type */
  518. static void run_actions(int action_type)
  519. {
  520. struct init_action *a;
  521. for (a = init_action_list; a; a = a->next) {
  522. if (!(a->action_type & action_type))
  523. continue;
  524. if (a->action_type & (SYSINIT | WAIT | ONCE | CTRLALTDEL | SHUTDOWN)) {
  525. pid_t pid = run(a);
  526. if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN))
  527. waitfor(pid);
  528. }
  529. if (a->action_type & (RESPAWN | ASKFIRST)) {
  530. /* Only run stuff with pid == 0. If pid != 0,
  531. * it is already running
  532. */
  533. if (a->pid == 0)
  534. a->pid = run(a);
  535. }
  536. }
  537. }
  538. static void new_init_action(uint8_t action_type, const char *command, const char *cons)
  539. {
  540. struct init_action *a, **nextp;
  541. /* Scenario:
  542. * old inittab:
  543. * ::shutdown:umount -a -r
  544. * ::shutdown:swapoff -a
  545. * new inittab:
  546. * ::shutdown:swapoff -a
  547. * ::shutdown:umount -a -r
  548. * On reload, we must ensure entries end up in correct order.
  549. * To achieve that, if we find a matching entry, we move it
  550. * to the end.
  551. */
  552. nextp = &init_action_list;
  553. while ((a = *nextp) != NULL) {
  554. /* Don't enter action if it's already in the list.
  555. * This prevents losing running RESPAWNs.
  556. */
  557. if (strcmp(a->command, command) == 0
  558. && strcmp(a->terminal, cons) == 0
  559. ) {
  560. /* Remove from list */
  561. *nextp = a->next;
  562. /* Find the end of the list */
  563. while (*nextp != NULL)
  564. nextp = &(*nextp)->next;
  565. a->next = NULL;
  566. goto append;
  567. }
  568. nextp = &a->next;
  569. }
  570. a = xzalloc(sizeof(*a));
  571. /* Append to the end of the list */
  572. append:
  573. *nextp = a;
  574. a->action_type = action_type;
  575. safe_strncpy(a->command, command, sizeof(a->command));
  576. safe_strncpy(a->terminal, cons, sizeof(a->terminal));
  577. dbg_message(L_LOG | L_CONSOLE, "command='%s' action=%d tty='%s'\n",
  578. a->command, a->action_type, a->terminal);
  579. }
  580. /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
  581. * then parse_inittab() simply adds in some default
  582. * actions(i.e., runs INIT_SCRIPT and then starts a pair
  583. * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
  584. * _is_ defined, but /etc/inittab is missing, this
  585. * results in the same set of default behaviors.
  586. */
  587. static void parse_inittab(void)
  588. {
  589. #if ENABLE_FEATURE_USE_INITTAB
  590. char *token[4];
  591. parser_t *parser = config_open2("/etc/inittab", fopen_for_read);
  592. if (parser == NULL)
  593. #endif
  594. {
  595. /* No inittab file - set up some default behavior */
  596. /* Reboot on Ctrl-Alt-Del */
  597. new_init_action(CTRLALTDEL, "reboot", "");
  598. /* Umount all filesystems on halt/reboot */
  599. new_init_action(SHUTDOWN, "umount -a -r", "");
  600. /* Swapoff on halt/reboot */
  601. if (ENABLE_SWAPONOFF)
  602. new_init_action(SHUTDOWN, "swapoff -a", "");
  603. /* Prepare to restart init when a QUIT is received */
  604. new_init_action(RESTART, "init", "");
  605. /* Askfirst shell on tty1-4 */
  606. new_init_action(ASKFIRST, bb_default_login_shell, "");
  607. //TODO: VC_1 instead of ""? "" is console -> ctty problems -> angry users
  608. new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
  609. new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
  610. new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
  611. /* sysinit */
  612. new_init_action(SYSINIT, INIT_SCRIPT, "");
  613. return;
  614. }
  615. #if ENABLE_FEATURE_USE_INITTAB
  616. /* optional_tty:ignored_runlevel:action:command
  617. * Delims are not to be collapsed and need exactly 4 tokens
  618. */
  619. while (config_read(parser, token, 4, 0, "#:",
  620. PARSE_NORMAL & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
  621. /* order must correspond to SYSINIT..RESTART constants */
  622. static const char actions[] ALIGN1 =
  623. "sysinit\0""wait\0""once\0""respawn\0""askfirst\0"
  624. "ctrlaltdel\0""shutdown\0""restart\0";
  625. int action;
  626. char *tty = token[0];
  627. if (!token[3]) /* less than 4 tokens */
  628. goto bad_entry;
  629. action = index_in_strings(actions, token[2]);
  630. if (action < 0 || !token[3][0]) /* token[3]: command */
  631. goto bad_entry;
  632. /* turn .*TTY -> /dev/TTY */
  633. if (tty[0]) {
  634. tty = concat_path_file("/dev/", skip_dev_pfx(tty));
  635. }
  636. new_init_action(1 << action, token[3], tty);
  637. if (tty[0])
  638. free(tty);
  639. continue;
  640. bad_entry:
  641. message(L_LOG | L_CONSOLE, "Bad inittab entry at line %d",
  642. parser->lineno);
  643. }
  644. config_close(parser);
  645. #endif
  646. }
  647. static void pause_and_low_level_reboot(unsigned magic) NORETURN;
  648. static void pause_and_low_level_reboot(unsigned magic)
  649. {
  650. pid_t pid;
  651. /* Allow time for last message to reach serial console, etc */
  652. sleep(1);
  653. /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS)
  654. * in linux/kernel/sys.c, which can cause the machine to panic when
  655. * the init process exits... */
  656. pid = vfork();
  657. if (pid == 0) { /* child */
  658. reboot(magic);
  659. _exit(EXIT_SUCCESS);
  660. }
  661. while (1)
  662. sleep(1);
  663. }
  664. static void run_shutdown_and_kill_processes(void)
  665. {
  666. /* Run everything to be run at "shutdown". This is done _prior_
  667. * to killing everything, in case people wish to use scripts to
  668. * shut things down gracefully... */
  669. run_actions(SHUTDOWN);
  670. message(L_CONSOLE | L_LOG, "The system is going down NOW!");
  671. /* Send signals to every process _except_ pid 1 */
  672. kill(-1, SIGTERM);
  673. message(L_CONSOLE | L_LOG, "Sent SIG%s to all processes", "TERM");
  674. sync();
  675. sleep(1);
  676. kill(-1, SIGKILL);
  677. message(L_CONSOLE, "Sent SIG%s to all processes", "KILL");
  678. sync();
  679. /*sleep(1); - callers take care about making a pause */
  680. }
  681. /* Signal handling by init:
  682. *
  683. * For process with PID==1, on entry kernel sets all signals to SIG_DFL
  684. * and unmasks all signals. However, for process with PID==1,
  685. * default action (SIG_DFL) on any signal is to ignore it,
  686. * even for special signals SIGKILL and SIGCONT.
  687. * Also, any signal can be caught or blocked.
  688. * (but SIGSTOP is still handled specially, at least in 2.6.20)
  689. *
  690. * We install two kinds of handlers, "immediate" and "delayed".
  691. *
  692. * Immediate handlers execute at any time, even while, say, sysinit
  693. * is running.
  694. *
  695. * Delayed handlers just set a flag variable. The variable is checked
  696. * in the main loop and acted upon.
  697. *
  698. * halt/poweroff/reboot and restart have immediate handlers.
  699. * They only traverse linked list of struct action's, never modify it,
  700. * this should be safe to do even in signal handler. Also they
  701. * never return.
  702. *
  703. * SIGSTOP and SIGTSTP have immediate handlers. They just wait
  704. * for SIGCONT to happen.
  705. *
  706. * SIGHUP has a delayed handler, because modifying linked list
  707. * of struct action's from a signal handler while it is manipulated
  708. * by the program may be disastrous.
  709. *
  710. * Ctrl-Alt-Del has a delayed handler. Not a must, but allowing
  711. * it to happen even somewhere inside "sysinit" would be a bit awkward.
  712. *
  713. * There is a tiny probability that SIGHUP and Ctrl-Alt-Del will collide
  714. * and only one will be remembered and acted upon.
  715. */
  716. /* The SIGUSR[12]/SIGTERM handler */
  717. static void halt_reboot_pwoff(int sig) NORETURN;
  718. static void halt_reboot_pwoff(int sig)
  719. {
  720. const char *m;
  721. unsigned rb;
  722. /* We may call run() and it unmasks signals,
  723. * including the one masked inside this signal handler.
  724. * Testcase which would start multiple reboot scripts:
  725. * while true; do reboot; done
  726. * Preventing it:
  727. */
  728. reset_sighandlers_and_unblock_sigs();
  729. run_shutdown_and_kill_processes();
  730. m = "halt";
  731. rb = RB_HALT_SYSTEM;
  732. if (sig == SIGTERM) {
  733. m = "reboot";
  734. rb = RB_AUTOBOOT;
  735. } else if (sig == SIGUSR2) {
  736. m = "poweroff";
  737. rb = RB_POWER_OFF;
  738. }
  739. message(L_CONSOLE, "Requesting system %s", m);
  740. pause_and_low_level_reboot(rb);
  741. /* not reached */
  742. }
  743. /* Handler for QUIT - exec "restart" action,
  744. * else (no such action defined) do nothing */
  745. static void restart_handler(int sig UNUSED_PARAM)
  746. {
  747. struct init_action *a;
  748. for (a = init_action_list; a; a = a->next) {
  749. if (!(a->action_type & RESTART))
  750. continue;
  751. /* Starting from here, we won't return.
  752. * Thus don't need to worry about preserving errno
  753. * and such.
  754. */
  755. reset_sighandlers_and_unblock_sigs();
  756. run_shutdown_and_kill_processes();
  757. #ifdef RB_ENABLE_CAD
  758. /* Allow Ctrl-Alt-Del to reboot the system.
  759. * This is how kernel sets it up for init, we follow suit.
  760. */
  761. reboot(RB_ENABLE_CAD); /* misnomer */
  762. #endif
  763. if (open_stdio_to_tty(a->terminal)) {
  764. dbg_message(L_CONSOLE, "Trying to re-exec %s", a->command);
  765. /* Theoretically should be safe.
  766. * But in practice, kernel bugs may leave
  767. * unkillable processes, and wait() may block forever.
  768. * Oh well. Hoping "new" init won't be too surprised
  769. * by having children it didn't create.
  770. */
  771. //while (wait(NULL) > 0)
  772. // continue;
  773. init_exec(a->command);
  774. }
  775. /* Open or exec failed */
  776. pause_and_low_level_reboot(RB_HALT_SYSTEM);
  777. /* not reached */
  778. }
  779. }
  780. /* The SIGSTOP/SIGTSTP handler
  781. * NB: inside it, all signals except SIGCONT are masked
  782. * via appropriate setup in sigaction().
  783. */
  784. static void stop_handler(int sig UNUSED_PARAM)
  785. {
  786. smallint saved_bb_got_signal;
  787. int saved_errno;
  788. saved_bb_got_signal = bb_got_signal;
  789. saved_errno = errno;
  790. signal(SIGCONT, record_signo);
  791. while (1) {
  792. pid_t wpid;
  793. if (bb_got_signal == SIGCONT)
  794. break;
  795. /* NB: this can accidentally wait() for a process
  796. * which we waitfor() elsewhere! waitfor() must have
  797. * code which is resilient against this.
  798. */
  799. wpid = wait_any_nohang(NULL);
  800. mark_terminated(wpid);
  801. sleep(1);
  802. }
  803. signal(SIGCONT, SIG_DFL);
  804. errno = saved_errno;
  805. bb_got_signal = saved_bb_got_signal;
  806. }
  807. #if ENABLE_FEATURE_USE_INITTAB
  808. static void reload_inittab(void)
  809. {
  810. struct init_action *a, **nextp;
  811. message(L_LOG, "reloading /etc/inittab");
  812. /* Disable old entries */
  813. for (a = init_action_list; a; a = a->next)
  814. a->action_type = 0;
  815. /* Append new entries, or modify existing entries
  816. * (incl. setting a->action_type) if cmd and device name
  817. * match new ones. End result: only entries with
  818. * a->action_type == 0 are stale.
  819. */
  820. parse_inittab();
  821. #if ENABLE_FEATURE_KILL_REMOVED
  822. /* Kill stale entries */
  823. /* Be nice and send SIGTERM first */
  824. for (a = init_action_list; a; a = a->next)
  825. if (a->action_type == 0 && a->pid != 0)
  826. kill(a->pid, SIGTERM);
  827. if (CONFIG_FEATURE_KILL_DELAY) {
  828. /* NB: parent will wait in NOMMU case */
  829. if ((BB_MMU ? fork() : vfork()) == 0) { /* child */
  830. sleep(CONFIG_FEATURE_KILL_DELAY);
  831. for (a = init_action_list; a; a = a->next)
  832. if (a->action_type == 0 && a->pid != 0)
  833. kill(a->pid, SIGKILL);
  834. _exit(EXIT_SUCCESS);
  835. }
  836. }
  837. #endif
  838. /* Remove stale entries and SYSINIT entries.
  839. * We never rerun SYSINIT entries anyway,
  840. * removing them too saves a few bytes */
  841. nextp = &init_action_list;
  842. while ((a = *nextp) != NULL) {
  843. if ((a->action_type & ~SYSINIT) == 0) {
  844. *nextp = a->next;
  845. free(a);
  846. } else {
  847. nextp = &a->next;
  848. }
  849. }
  850. /* Not needed: */
  851. /* run_actions(RESPAWN | ASKFIRST); */
  852. /* - we return to main loop, which does this automagically */
  853. }
  854. #endif
  855. static int check_delayed_sigs(void)
  856. {
  857. int sigs_seen = 0;
  858. while (1) {
  859. smallint sig = bb_got_signal;
  860. if (!sig)
  861. return sigs_seen;
  862. bb_got_signal = 0;
  863. sigs_seen = 1;
  864. #if ENABLE_FEATURE_USE_INITTAB
  865. if (sig == SIGHUP)
  866. reload_inittab();
  867. #endif
  868. if (sig == SIGINT)
  869. run_actions(CTRLALTDEL);
  870. }
  871. }
  872. #if DEBUG_SEGV_HANDLER
  873. static
  874. void handle_sigsegv(int sig, siginfo_t *info, void *ucontext)
  875. {
  876. long ip;
  877. ucontext_t *uc;
  878. uc = ucontext;
  879. ip = uc->uc_mcontext.gregs[REG_EIP];
  880. fdprintf(2, "signal:%d address:0x%lx ip:0x%lx\n",
  881. sig,
  882. /* this is void*, but using %p would print "(null)"
  883. * even for ptrs which are not exactly 0, but, say, 0x123:
  884. */
  885. (long)info->si_addr,
  886. ip);
  887. {
  888. /* glibc extension */
  889. void *array[50];
  890. int size;
  891. size = backtrace(array, 50);
  892. backtrace_symbols_fd(array, size, 2);
  893. }
  894. for (;;) sleep(9999);
  895. }
  896. #endif
  897. int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  898. int init_main(int argc UNUSED_PARAM, char **argv)
  899. {
  900. if (argv[1] && strcmp(argv[1], "-q") == 0) {
  901. return kill(1, SIGHUP);
  902. }
  903. #if DEBUG_SEGV_HANDLER
  904. {
  905. struct sigaction sa;
  906. memset(&sa, 0, sizeof(sa));
  907. sa.sa_sigaction = handle_sigsegv;
  908. sa.sa_flags = SA_SIGINFO;
  909. sigaction(SIGSEGV, &sa, NULL);
  910. sigaction(SIGILL, &sa, NULL);
  911. sigaction(SIGFPE, &sa, NULL);
  912. sigaction(SIGBUS, &sa, NULL);
  913. }
  914. #endif
  915. if (!DEBUG_INIT) {
  916. /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
  917. if (getpid() != 1
  918. && (!ENABLE_FEATURE_INITRD || applet_name[0] != 'l') /* not linuxrc? */
  919. ) {
  920. bb_error_msg_and_die("must be run as PID 1");
  921. }
  922. #ifdef RB_DISABLE_CAD
  923. /* Turn off rebooting via CTL-ALT-DEL - we get a
  924. * SIGINT on CAD so we can shut things down gracefully... */
  925. reboot(RB_DISABLE_CAD); /* misnomer */
  926. #endif
  927. }
  928. /* If, say, xmalloc would ever die, we don't want to oops kernel
  929. * by exiting.
  930. * NB: we set die_sleep *after* PID 1 check and bb_show_usage.
  931. * Otherwise, for example, "init u" ("please rexec yourself"
  932. * command for sysvinit) will show help text (which isn't too bad),
  933. * *and sleep forever* (which is bad!)
  934. */
  935. die_sleep = 30 * 24*60*60;
  936. /* Figure out where the default console should be */
  937. console_init();
  938. set_sane_term();
  939. xchdir("/");
  940. setsid();
  941. /* Make sure environs is set to something sane */
  942. putenv((char *) "HOME=/");
  943. putenv((char *) bb_PATH_root_path);
  944. putenv((char *) "SHELL=/bin/sh");
  945. putenv((char *) "USER=root"); /* needed? why? */
  946. if (argv[1])
  947. xsetenv("RUNLEVEL", argv[1]);
  948. #if !ENABLE_FEATURE_EXTRA_QUIET
  949. /* Hello world */
  950. message(L_CONSOLE | L_LOG, "init started: %s", bb_banner);
  951. #endif
  952. /* struct sysinfo is linux-specific */
  953. #ifdef __linux__
  954. /* Make sure there is enough memory to do something useful. */
  955. if (ENABLE_SWAPONOFF) {
  956. struct sysinfo info;
  957. if (sysinfo(&info) == 0
  958. && (info.mem_unit ? info.mem_unit : 1) * (long long)info.totalram < 1024*1024
  959. ) {
  960. message(L_CONSOLE, "Low memory, forcing swapon");
  961. /* swapon -a requires /proc typically */
  962. new_init_action(SYSINIT, "mount -t proc proc /proc", "");
  963. /* Try to turn on swap */
  964. new_init_action(SYSINIT, "swapon -a", "");
  965. run_actions(SYSINIT); /* wait and removing */
  966. }
  967. }
  968. #endif
  969. /* Check if we are supposed to be in single user mode */
  970. if (argv[1]
  971. && (strcmp(argv[1], "single") == 0 || strcmp(argv[1], "-s") == 0 || LONE_CHAR(argv[1], '1'))
  972. ) {
  973. /* ??? shouldn't we set RUNLEVEL="b" here? */
  974. /* Start a shell on console */
  975. new_init_action(RESPAWN, bb_default_login_shell, "");
  976. } else {
  977. /* Not in single user mode - see what inittab says */
  978. /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
  979. * then parse_inittab() simply adds in some default
  980. * actions(i.e., INIT_SCRIPT and a pair
  981. * of "askfirst" shells */
  982. parse_inittab();
  983. }
  984. #if ENABLE_SELINUX
  985. if (getenv("SELINUX_INIT") == NULL) {
  986. int enforce = 0;
  987. putenv((char*)"SELINUX_INIT=YES");
  988. if (selinux_init_load_policy(&enforce) == 0) {
  989. BB_EXECVP(argv[0], argv);
  990. } else if (enforce > 0) {
  991. /* SELinux in enforcing mode but load_policy failed */
  992. message(L_CONSOLE, "can't load SELinux Policy. "
  993. "Machine is in enforcing mode. Halting now.");
  994. return EXIT_FAILURE;
  995. }
  996. }
  997. #endif
  998. /* Make the command line just say "init" - thats all, nothing else */
  999. strncpy(argv[0], "init", strlen(argv[0]));
  1000. /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
  1001. while (*++argv)
  1002. memset(*argv, 0, strlen(*argv));
  1003. /* Set up signal handlers */
  1004. if (!DEBUG_INIT) {
  1005. struct sigaction sa;
  1006. bb_signals(0
  1007. + (1 << SIGUSR1) /* halt */
  1008. + (1 << SIGTERM) /* reboot */
  1009. + (1 << SIGUSR2) /* poweroff */
  1010. , halt_reboot_pwoff);
  1011. signal(SIGQUIT, restart_handler); /* re-exec another init */
  1012. /* Stop handler must allow only SIGCONT inside itself */
  1013. memset(&sa, 0, sizeof(sa));
  1014. sigfillset(&sa.sa_mask);
  1015. sigdelset(&sa.sa_mask, SIGCONT);
  1016. sa.sa_handler = stop_handler;
  1017. /* NB: sa_flags doesn't have SA_RESTART.
  1018. * It must be able to interrupt wait().
  1019. */
  1020. sigaction_set(SIGTSTP, &sa); /* pause */
  1021. /* Does not work as intended, at least in 2.6.20.
  1022. * SIGSTOP is simply ignored by init:
  1023. */
  1024. sigaction_set(SIGSTOP, &sa); /* pause */
  1025. /* SIGINT (Ctrl-Alt-Del) must interrupt wait(),
  1026. * setting handler without SA_RESTART flag.
  1027. */
  1028. bb_signals_recursive_norestart((1 << SIGINT), record_signo);
  1029. }
  1030. /* Set up "reread /etc/inittab" handler.
  1031. * Handler is set up without SA_RESTART, it will interrupt syscalls.
  1032. */
  1033. if (!DEBUG_INIT && ENABLE_FEATURE_USE_INITTAB)
  1034. bb_signals_recursive_norestart((1 << SIGHUP), record_signo);
  1035. /* Now run everything that needs to be run */
  1036. /* First run the sysinit command */
  1037. run_actions(SYSINIT);
  1038. check_delayed_sigs();
  1039. /* Next run anything that wants to block */
  1040. run_actions(WAIT);
  1041. check_delayed_sigs();
  1042. /* Next run anything to be run only once */
  1043. run_actions(ONCE);
  1044. /* Now run the looping stuff for the rest of forever.
  1045. */
  1046. while (1) {
  1047. int maybe_WNOHANG;
  1048. maybe_WNOHANG = check_delayed_sigs();
  1049. /* (Re)run the respawn/askfirst stuff */
  1050. run_actions(RESPAWN | ASKFIRST);
  1051. maybe_WNOHANG |= check_delayed_sigs();
  1052. /* Don't consume all CPU time - sleep a bit */
  1053. sleep(1);
  1054. maybe_WNOHANG |= check_delayed_sigs();
  1055. /* Wait for any child process(es) to exit.
  1056. *
  1057. * If check_delayed_sigs above reported that a signal
  1058. * was caught, wait will be nonblocking. This ensures
  1059. * that if SIGHUP has reloaded inittab, respawn and askfirst
  1060. * actions will not be delayed until next child death.
  1061. */
  1062. if (maybe_WNOHANG)
  1063. maybe_WNOHANG = WNOHANG;
  1064. while (1) {
  1065. pid_t wpid;
  1066. struct init_action *a;
  1067. /* If signals happen _in_ the wait, they interrupt it,
  1068. * bb_signals_recursive_norestart set them up that way
  1069. */
  1070. wpid = waitpid(-1, NULL, maybe_WNOHANG);
  1071. if (wpid <= 0)
  1072. break;
  1073. a = mark_terminated(wpid);
  1074. if (a) {
  1075. message(L_LOG, "process '%s' (pid %d) exited. "
  1076. "Scheduling for restart.",
  1077. a->command, wpid);
  1078. }
  1079. /* See if anyone else is waiting to be reaped */
  1080. maybe_WNOHANG = WNOHANG;
  1081. }
  1082. } /* while (1) */
  1083. }
  1084. //usage:#define linuxrc_trivial_usage NOUSAGE_STR
  1085. //usage:#define linuxrc_full_usage ""
  1086. //usage:#define init_trivial_usage
  1087. //usage: ""
  1088. //usage:#define init_full_usage "\n\n"
  1089. //usage: "Init is the parent of all processes"
  1090. //usage:
  1091. //usage:#define init_notes_usage
  1092. //usage: "This version of init is designed to be run only by the kernel.\n"
  1093. //usage: "\n"
  1094. //usage: "BusyBox init doesn't support multiple runlevels. The runlevels field of\n"
  1095. //usage: "the /etc/inittab file is completely ignored by BusyBox init. If you want\n"
  1096. //usage: "runlevels, use sysvinit.\n"
  1097. //usage: "\n"
  1098. //usage: "BusyBox init works just fine without an inittab. If no inittab is found,\n"
  1099. //usage: "it has the following default behavior:\n"
  1100. //usage: "\n"
  1101. //usage: " ::sysinit:/etc/init.d/rcS\n"
  1102. //usage: " ::askfirst:/bin/sh\n"
  1103. //usage: " ::ctrlaltdel:/sbin/reboot\n"
  1104. //usage: " ::shutdown:/sbin/swapoff -a\n"
  1105. //usage: " ::shutdown:/bin/umount -a -r\n"
  1106. //usage: " ::restart:/sbin/init\n"
  1107. //usage: "\n"
  1108. //usage: "if it detects that /dev/console is _not_ a serial console, it will also run:\n"
  1109. //usage: "\n"
  1110. //usage: " tty2::askfirst:/bin/sh\n"
  1111. //usage: " tty3::askfirst:/bin/sh\n"
  1112. //usage: " tty4::askfirst:/bin/sh\n"
  1113. //usage: "\n"
  1114. //usage: "If you choose to use an /etc/inittab file, the inittab entry format is as follows:\n"
  1115. //usage: "\n"
  1116. //usage: " <id>:<runlevels>:<action>:<process>\n"
  1117. //usage: "\n"
  1118. //usage: " <id>:\n"
  1119. //usage: "\n"
  1120. //usage: " WARNING: This field has a non-traditional meaning for BusyBox init!\n"
  1121. //usage: " The id field is used by BusyBox init to specify the controlling tty for\n"
  1122. //usage: " the specified process to run on. The contents of this field are\n"
  1123. //usage: " appended to \"/dev/\" and used as-is. There is no need for this field to\n"
  1124. //usage: " be unique, although if it isn't you may have strange results. If this\n"
  1125. //usage: " field is left blank, the controlling tty is set to the console. Also\n"
  1126. //usage: " note that if BusyBox detects that a serial console is in use, then only\n"
  1127. //usage: " entries whose controlling tty is either the serial console or /dev/null\n"
  1128. //usage: " will be run. BusyBox init does nothing with utmp. We don't need no\n"
  1129. //usage: " stinkin' utmp.\n"
  1130. //usage: "\n"
  1131. //usage: " <runlevels>:\n"
  1132. //usage: "\n"
  1133. //usage: " The runlevels field is completely ignored.\n"
  1134. //usage: "\n"
  1135. //usage: " <action>:\n"
  1136. //usage: "\n"
  1137. //usage: " Valid actions include: sysinit, respawn, askfirst, wait,\n"
  1138. //usage: " once, restart, ctrlaltdel, and shutdown.\n"
  1139. //usage: "\n"
  1140. //usage: " The available actions can be classified into two groups: actions\n"
  1141. //usage: " that are run only once, and actions that are re-run when the specified\n"
  1142. //usage: " process exits.\n"
  1143. //usage: "\n"
  1144. //usage: " Run only-once actions:\n"
  1145. //usage: "\n"
  1146. //usage: " 'sysinit' is the first item run on boot. init waits until all\n"
  1147. //usage: " sysinit actions are completed before continuing. Following the\n"
  1148. //usage: " completion of all sysinit actions, all 'wait' actions are run.\n"
  1149. //usage: " 'wait' actions, like 'sysinit' actions, cause init to wait until\n"
  1150. //usage: " the specified task completes. 'once' actions are asynchronous,\n"
  1151. //usage: " therefore, init does not wait for them to complete. 'restart' is\n"
  1152. //usage: " the action taken to restart the init process. By default this should\n"
  1153. //usage: " simply run /sbin/init, but can be a script which runs pivot_root or it\n"
  1154. //usage: " can do all sorts of other interesting things. The 'ctrlaltdel' init\n"
  1155. //usage: " actions are run when the system detects that someone on the system\n"
  1156. //usage: " console has pressed the CTRL-ALT-DEL key combination. Typically one\n"
  1157. //usage: " wants to run 'reboot' at this point to cause the system to reboot.\n"
  1158. //usage: " Finally the 'shutdown' action specifies the actions to taken when\n"
  1159. //usage: " init is told to reboot. Unmounting filesystems and disabling swap\n"
  1160. //usage: " is a very good here.\n"
  1161. //usage: "\n"
  1162. //usage: " Run repeatedly actions:\n"
  1163. //usage: "\n"
  1164. //usage: " 'respawn' actions are run after the 'once' actions. When a process\n"
  1165. //usage: " started with a 'respawn' action exits, init automatically restarts\n"
  1166. //usage: " it. Unlike sysvinit, BusyBox init does not stop processes from\n"
  1167. //usage: " respawning out of control. The 'askfirst' actions acts just like\n"
  1168. //usage: " respawn, except that before running the specified process it\n"
  1169. //usage: " displays the line \"Please press Enter to activate this console.\"\n"
  1170. //usage: " and then waits for the user to press enter before starting the\n"
  1171. //usage: " specified process.\n"
  1172. //usage: "\n"
  1173. //usage: " Unrecognized actions (like initdefault) will cause init to emit an\n"
  1174. //usage: " error message, and then go along with its business. All actions are\n"
  1175. //usage: " run in the order they appear in /etc/inittab.\n"
  1176. //usage: "\n"
  1177. //usage: " <process>:\n"
  1178. //usage: "\n"
  1179. //usage: " Specifies the process to be executed and its command line.\n"
  1180. //usage: "\n"
  1181. //usage: "Example /etc/inittab file:\n"
  1182. //usage: "\n"
  1183. //usage: " # This is run first except when booting in single-user mode\n"
  1184. //usage: " #\n"
  1185. //usage: " ::sysinit:/etc/init.d/rcS\n"
  1186. //usage: " \n"
  1187. //usage: " # /bin/sh invocations on selected ttys\n"
  1188. //usage: " #\n"
  1189. //usage: " # Start an \"askfirst\" shell on the console (whatever that may be)\n"
  1190. //usage: " ::askfirst:-/bin/sh\n"
  1191. //usage: " # Start an \"askfirst\" shell on /dev/tty2-4\n"
  1192. //usage: " tty2::askfirst:-/bin/sh\n"
  1193. //usage: " tty3::askfirst:-/bin/sh\n"
  1194. //usage: " tty4::askfirst:-/bin/sh\n"
  1195. //usage: " \n"
  1196. //usage: " # /sbin/getty invocations for selected ttys\n"
  1197. //usage: " #\n"
  1198. //usage: " tty4::respawn:/sbin/getty 38400 tty4\n"
  1199. //usage: " tty5::respawn:/sbin/getty 38400 tty5\n"
  1200. //usage: " \n"
  1201. //usage: " \n"
  1202. //usage: " # Example of how to put a getty on a serial line (for a terminal)\n"
  1203. //usage: " #\n"
  1204. //usage: " #::respawn:/sbin/getty -L ttyS0 9600 vt100\n"
  1205. //usage: " #::respawn:/sbin/getty -L ttyS1 9600 vt100\n"
  1206. //usage: " #\n"
  1207. //usage: " # Example how to put a getty on a modem line\n"
  1208. //usage: " #::respawn:/sbin/getty 57600 ttyS2\n"
  1209. //usage: " \n"
  1210. //usage: " # Stuff to do when restarting the init process\n"
  1211. //usage: " ::restart:/sbin/init\n"
  1212. //usage: " \n"
  1213. //usage: " # Stuff to do before rebooting\n"
  1214. //usage: " ::ctrlaltdel:/sbin/reboot\n"
  1215. //usage: " ::shutdown:/bin/umount -a -r\n"
  1216. //usage: " ::shutdown:/sbin/swapoff -a\n"