blob: 459359189899965733579d5b5bd8ac6a4d3ffb7b [file] [log] [blame]
Rob Landley28964802008-01-19 17:08:39 -06001/* vi: set sw=4 ts=4:
2 *
3 * oneit.c, tiny one-process init replacement.
Rob Landleyb23d2302006-11-25 13:48:02 -05004 *
Rob Landleyfece5cb2007-12-03 20:05:57 -06005 * Copyright 2005, 2007 by Rob Landley <rob@landley.net>.
6 *
7 * Not in SUSv3.
Rob Landley28964802008-01-19 17:08:39 -06008
Rob Landleyb1487dc2008-06-26 22:48:43 -05009USE_ONEIT(NEWTOY(oneit, "^<1c:p", TOYFLAG_SBIN))
Rob Landley55928b12008-01-19 17:43:27 -060010
Rob Landley28964802008-01-19 17:08:39 -060011config ONEIT
12 bool "oneit"
13 default y
14 help
15 usage: oneit [-p] [-c /dev/tty0] command [...]
16
17 A simple init program that runs a single supplied command line with a
18 controlling tty (so CTRL-C can kill it).
19
20 -p Power off instead of rebooting when command exits.
21 -c Which console device to use.
22
23 The oneit command runs the supplied command line as a child process
24 (because PID 1 has signals blocked), attached to /dev/tty0, in its
25 own session. Then oneit reaps zombies until the child exits, at
26 which point it reboots (or with -p, powers off) the system.
27*/
Rob Landleyb23d2302006-11-25 13:48:02 -050028
29#include "toys.h"
30#include <sys/reboot.h>
31
Rob Landleyb1aaba12008-01-20 17:25:44 -060032DEFINE_GLOBALS(
33 char *console;
34)
35
36#define TT this.oneit
37
Rob Landleyb23d2302006-11-25 13:48:02 -050038// The minimum amount of work necessary to get ctrl-c and such to work is:
39//
40// - Fork a child (PID 1 is special: can't exit, has various signals blocked).
41// - Do a setsid() (so we have our own session).
42// - In the child, attach stdio to /dev/tty0 (/dev/console is special)
43// - Exec the rest of the command line.
44//
45// PID 1 then reaps zombies until the child process it spawned exits, at which
46// point it calls sync() and reboot(). I could stick a kill -1 in there.
47
Rob Landleya8f25802007-12-09 15:30:36 -060048
Rob Landleyefda21c2007-11-29 18:14:37 -060049void oneit_main(void)
Rob Landleyb23d2302006-11-25 13:48:02 -050050{
51 int i;
52 pid_t pid;
53
54 // Create a new child process.
55 pid = vfork();
56 if (pid) {
57
58 // pid 1 just reaps zombies until it gets its child, then halts the system.
59 while (pid!=wait(&i));
60 sync();
Rob Landleycca44502008-12-14 02:12:07 -060061
62 // PID 1 can't call reboot() because it kills the task that calls it,
63 // which causes the kernel to panic before the actual reboot happens.
64 if (!vfork()) reboot((toys.optflags&1) ? RB_POWER_OFF : RB_AUTOBOOT);
65 sleep(5);
66 _exit(1);
Rob Landleyb23d2302006-11-25 13:48:02 -050067 }
Rob Landley2c226852007-11-15 18:30:30 -060068
Rob Landleyb23d2302006-11-25 13:48:02 -050069 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
70 setsid();
71 for (i=0; i<3; i++) {
72 close(i);
Rob Landleya8f25802007-12-09 15:30:36 -060073 xopen(TT.console ? TT.console : "/dev/tty0",O_RDWR);
Rob Landleyb23d2302006-11-25 13:48:02 -050074 }
75
Rob Landleyefda21c2007-11-29 18:14:37 -060076 // Can't xexec() here, because we vforked so we don't want to error_exit().
Rob Landleyb23d2302006-11-25 13:48:02 -050077 toy_exec(toys.optargs);
78 execvp(*toys.optargs, toys.optargs);
79 _exit(127);
80}