init.c 39 KB

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