init.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. #endif
  114. #if ENABLE_FEATURE_UTMP
  115. # include <utmp.h> /* DEAD_PROCESS */
  116. #endif
  117. #include "reboot.h" /* reboot() constants */
  118. /* Used only for sanitizing purposes in set_sane_term() below. On systems where
  119. * the baud rate is stored in a separate field, we can safely disable them. */
  120. #ifndef CBAUD
  121. # define CBAUD 0
  122. # define CBAUDEX 0
  123. #endif
  124. /* Was a CONFIG_xxx option. A lot of people were building
  125. * not fully functional init by switching it on! */
  126. #define DEBUG_INIT 0
  127. #define COMMAND_SIZE 256
  128. #define CONSOLE_NAME_SIZE 32
  129. /* Default sysinit script. */
  130. #ifndef INIT_SCRIPT
  131. #define INIT_SCRIPT "/etc/init.d/rcS"
  132. #endif
  133. /* Each type of actions can appear many times. They will be
  134. * handled in order. RESTART is an exception, only 1st is used.
  135. */
  136. /* Start these actions first and wait for completion */
  137. #define SYSINIT 0x01
  138. /* Start these after SYSINIT and wait for completion */
  139. #define WAIT 0x02
  140. /* Start these after WAIT and *dont* wait for completion */
  141. #define ONCE 0x04
  142. /*
  143. * NB: while SYSINIT/WAIT/ONCE are being processed,
  144. * SIGHUP ("reread /etc/inittab") will be processed only after
  145. * each group of actions. If new inittab adds, say, a SYSINIT action,
  146. * it will not be run, since init is already "past SYSINIT stage".
  147. */
  148. /* Start these after ONCE are started, restart on exit */
  149. #define RESPAWN 0x08
  150. /* Like RESPAWN, but wait for <Enter> to be pressed on tty */
  151. #define ASKFIRST 0x10
  152. /*
  153. * Start these on SIGINT, and wait for completion.
  154. * Then go back to respawning RESPAWN and ASKFIRST actions.
  155. * NB: kernel sends SIGINT to us if Ctrl-Alt-Del was pressed.
  156. */
  157. #define CTRLALTDEL 0x20
  158. /*
  159. * Start these before killing all processes in preparation for
  160. * running RESTART actions or doing low-level halt/reboot/poweroff
  161. * (initiated by SIGUSR1/SIGTERM/SIGUSR2).
  162. * Wait for completion before proceeding.
  163. */
  164. #define SHUTDOWN 0x40
  165. /*
  166. * exec() on SIGQUIT. SHUTDOWN actions are started and waited for,
  167. * then all processes are killed, then init exec's 1st RESTART action,
  168. * replacing itself by it. If no RESTART action specified,
  169. * SIGQUIT has no effect.
  170. */
  171. #define RESTART 0x80
  172. /* A linked list of init_actions, to be read from inittab */
  173. struct init_action {
  174. struct init_action *next;
  175. pid_t pid;
  176. uint8_t action_type;
  177. char terminal[CONSOLE_NAME_SIZE];
  178. char command[COMMAND_SIZE];
  179. };
  180. static struct init_action *init_action_list = NULL;
  181. static const char *log_console = VC_5;
  182. enum {
  183. L_LOG = 0x1,
  184. L_CONSOLE = 0x2,
  185. };
  186. /* Print a message to the specified device.
  187. * "where" may be bitwise-or'd from L_LOG | L_CONSOLE
  188. * NB: careful, we can be called after vfork!
  189. */
  190. #define dbg_message(...) do { if (DEBUG_INIT) message(__VA_ARGS__); } while (0)
  191. static void message(int where, const char *fmt, ...)
  192. __attribute__ ((format(printf, 2, 3)));
  193. static void message(int where, const char *fmt, ...)
  194. {
  195. va_list arguments;
  196. unsigned l;
  197. char msg[128];
  198. msg[0] = '\r';
  199. va_start(arguments, fmt);
  200. l = 1 + vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments);
  201. if (l > sizeof(msg) - 1)
  202. l = sizeof(msg) - 1;
  203. va_end(arguments);
  204. #if ENABLE_FEATURE_INIT_SYSLOG
  205. msg[l] = '\0';
  206. if (where & L_LOG) {
  207. /* Log the message to syslogd */
  208. openlog(applet_name, 0, LOG_DAEMON);
  209. /* don't print "\r" */
  210. syslog(LOG_INFO, "%s", msg + 1);
  211. closelog();
  212. }
  213. msg[l++] = '\n';
  214. msg[l] = '\0';
  215. #else
  216. {
  217. static int log_fd = -1;
  218. msg[l++] = '\n';
  219. msg[l] = '\0';
  220. /* Take full control of the log tty, and never close it.
  221. * It's mine, all mine! Muhahahaha! */
  222. if (log_fd < 0) {
  223. if (!log_console) {
  224. log_fd = STDERR_FILENO;
  225. } else {
  226. log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
  227. if (log_fd < 0) {
  228. bb_error_msg("can't log to %s", log_console);
  229. where = L_CONSOLE;
  230. } else {
  231. close_on_exec_on(log_fd);
  232. }
  233. }
  234. }
  235. if (where & L_LOG) {
  236. full_write(log_fd, msg, l);
  237. if (log_fd == STDERR_FILENO)
  238. return; /* don't print dup messages */
  239. }
  240. }
  241. #endif
  242. if (where & L_CONSOLE) {
  243. /* Send console messages to console so people will see them. */
  244. full_write(STDERR_FILENO, msg, l);
  245. }
  246. }
  247. static void console_init(void)
  248. {
  249. #ifdef VT_OPENQRY
  250. int vtno;
  251. #endif
  252. char *s;
  253. s = getenv("CONSOLE");
  254. if (!s)
  255. s = getenv("console");
  256. if (s) {
  257. int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY);
  258. if (fd >= 0) {
  259. dup2(fd, STDIN_FILENO);
  260. dup2(fd, STDOUT_FILENO);
  261. xmove_fd(fd, STDERR_FILENO);
  262. }
  263. dbg_message(L_LOG, "console='%s'", s);
  264. } else {
  265. /* Make sure fd 0,1,2 are not closed
  266. * (so that they won't be used by future opens) */
  267. bb_sanitize_stdio();
  268. // Users report problems
  269. // /* Make sure init can't be blocked by writing to stderr */
  270. // fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
  271. }
  272. s = getenv("TERM");
  273. #ifdef VT_OPENQRY
  274. if (ioctl(STDIN_FILENO, VT_OPENQRY, &vtno) != 0) {
  275. /* Not a linux terminal, probably serial console.
  276. * Force the TERM setting to vt102
  277. * if TERM is set to linux (the default) */
  278. if (!s || strcmp(s, "linux") == 0)
  279. putenv((char*)"TERM=vt102");
  280. if (!ENABLE_FEATURE_INIT_SYSLOG)
  281. log_console = NULL;
  282. } else
  283. #endif
  284. if (!s)
  285. putenv((char*)"TERM=" CONFIG_INIT_TERMINAL_TYPE);
  286. }
  287. /* Set terminal settings to reasonable defaults.
  288. * NB: careful, we can be called after vfork! */
  289. static void set_sane_term(void)
  290. {
  291. struct termios tty;
  292. tcgetattr(STDIN_FILENO, &tty);
  293. /* set control chars */
  294. tty.c_cc[VINTR] = 3; /* C-c */
  295. tty.c_cc[VQUIT] = 28; /* C-\ */
  296. tty.c_cc[VERASE] = 127; /* C-? */
  297. tty.c_cc[VKILL] = 21; /* C-u */
  298. tty.c_cc[VEOF] = 4; /* C-d */
  299. tty.c_cc[VSTART] = 17; /* C-q */
  300. tty.c_cc[VSTOP] = 19; /* C-s */
  301. tty.c_cc[VSUSP] = 26; /* C-z */
  302. #ifdef __linux__
  303. /* use line discipline 0 */
  304. tty.c_line = 0;
  305. #endif
  306. /* Make it be sane */
  307. #ifndef CRTSCTS
  308. # define CRTSCTS 0
  309. #endif
  310. /* added CRTSCTS to fix Debian bug 528560 */
  311. tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD | CRTSCTS;
  312. tty.c_cflag |= CREAD | HUPCL | CLOCAL;
  313. /* input modes */
  314. tty.c_iflag = ICRNL | IXON | IXOFF;
  315. /* output modes */
  316. tty.c_oflag = OPOST | ONLCR;
  317. /* local modes */
  318. tty.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
  319. tcsetattr_stdin_TCSANOW(&tty);
  320. }
  321. /* Open the new terminal device.
  322. * NB: careful, we can be called after vfork! */
  323. static int open_stdio_to_tty(const char* tty_name)
  324. {
  325. /* empty tty_name means "use init's tty", else... */
  326. if (tty_name[0]) {
  327. int fd;
  328. close(STDIN_FILENO);
  329. /* fd can be only < 0 or 0: */
  330. fd = device_open(tty_name, O_RDWR);
  331. if (fd) {
  332. message(L_LOG | L_CONSOLE, "can't open %s: %s",
  333. tty_name, strerror(errno));
  334. return 0; /* failure */
  335. }
  336. dup2(STDIN_FILENO, STDOUT_FILENO);
  337. dup2(STDIN_FILENO, STDERR_FILENO);
  338. }
  339. set_sane_term();
  340. return 1; /* success */
  341. }
  342. static void reset_sighandlers_and_unblock_sigs(void)
  343. {
  344. bb_signals(0
  345. + (1 << SIGUSR1)
  346. + (1 << SIGUSR2)
  347. + (1 << SIGTERM)
  348. + (1 << SIGQUIT)
  349. + (1 << SIGINT)
  350. + (1 << SIGHUP)
  351. + (1 << SIGTSTP)
  352. + (1 << SIGSTOP)
  353. , SIG_DFL);
  354. sigprocmask_allsigs(SIG_UNBLOCK);
  355. }
  356. /* Wrapper around exec:
  357. * Takes string (max COMMAND_SIZE chars).
  358. * If chars like '>' detected, execs '[-]/bin/sh -c "exec ......."'.
  359. * Otherwise splits words on whitespace, deals with leading dash,
  360. * and uses plain exec().
  361. * NB: careful, we can be called after vfork!
  362. */
  363. static void init_exec(const char *command)
  364. {
  365. char *cmd[COMMAND_SIZE / 2];
  366. char buf[COMMAND_SIZE + 6]; /* COMMAND_SIZE+strlen("exec ")+1 */
  367. int dash = (command[0] == '-' /* maybe? && command[1] == '/' */);
  368. /* See if any special /bin/sh requiring characters are present */
  369. if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
  370. strcpy(buf, "exec ");
  371. strcpy(buf + 5, command + dash); /* excluding "-" */
  372. /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */
  373. cmd[0] = (char*)(LIBBB_DEFAULT_LOGIN_SHELL + !dash);
  374. cmd[1] = (char*)"-c";
  375. cmd[2] = buf;
  376. cmd[3] = NULL;
  377. } else {
  378. /* Convert command (char*) into cmd (char**, one word per string) */
  379. char *word, *next;
  380. int i = 0;
  381. next = strcpy(buf, command); /* including "-" */
  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. * And skip "-" in actual exec call. */
  393. if (dash) {
  394. /* _Attempt_ to make stdin a controlling tty. */
  395. if (ENABLE_FEATURE_INIT_SCTTY)
  396. ioctl(STDIN_FILENO, TIOCSCTTY, 0 /*only try, don't steal*/);
  397. }
  398. BB_EXECVP(cmd[0] + dash, cmd);
  399. message(L_LOG | L_CONSOLE, "can't run '%s': %s", cmd[0], 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"