From b23d230ad55e028a4516e0cd60b7ecdc25504e4b Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 25 Nov 2006 13:48:02 -0500 Subject: [PATCH] Add oneit. --- toys/Config.in | 16 ++++++++++++++++ toys/oneit.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ toys/toylist.h | 1 + 3 files changed, 62 insertions(+) create mode 100644 toys/oneit.c diff --git a/toys/Config.in b/toys/Config.in index 77e59e4..59271dd 100644 --- a/toys/Config.in +++ b/toys/Config.in @@ -34,6 +34,22 @@ config HELLO help A hello world program. You don't need this. +config ONEIT + bool "oneit" + default n + help + usage: oneit [-p] command [...] + + A simple init program that runs a single supplied command line with a + controlling tty (so CTRL-C can kill it). + + -p Power off instead of rebooting when command exits. + + The oneit command runs the supplied command line as a child process + (because PID 1 has signals blocked), attached to /dev/tty0, in its + own session. Then oneit reaps zombies until the child exits, at + which point it reboots (or with -p, powers off) the system. + config PWD bool "pwd" default n diff --git a/toys/oneit.c b/toys/oneit.c new file mode 100644 index 0000000..1f87e5f --- /dev/null +++ b/toys/oneit.c @@ -0,0 +1,45 @@ +/* oneit.c, tiny one-process init replacement. + * + * Copyright 2005 by Rob Landley . + */ + +#include "toys.h" +#include + +// The minimum amount of work necessary to get ctrl-c and such to work is: +// +// - Fork a child (PID 1 is special: can't exit, has various signals blocked). +// - Do a setsid() (so we have our own session). +// - In the child, attach stdio to /dev/tty0 (/dev/console is special) +// - Exec the rest of the command line. +// +// PID 1 then reaps zombies until the child process it spawned exits, at which +// point it calls sync() and reboot(). I could stick a kill -1 in there. + +int oneit_main(void) +{ + int i; + pid_t pid; + + // Create a new child process. + pid = vfork(); + if (pid) { + + // pid 1 just reaps zombies until it gets its child, then halts the system. + while (pid!=wait(&i)); + sync(); + reboot(toys.optflags ? RB_POWER_OFF : RB_AUTOBOOT); + } + + // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works. + setsid(); + for (i=0; i<3; i++) { + close(i); + open("/dev/tty0",O_RDWR); + } + + // Can't xexec() here because we vforked so we don't want to error_exit(). + toy_exec(toys.optargs); + execvp(*toys.optargs, toys.optargs); + _exit(127); +} diff --git a/toys/toylist.h b/toys/toylist.h index a61aff9..ef42749 100644 --- a/toys/toylist.h +++ b/toys/toylist.h @@ -57,6 +57,7 @@ USE_TOYSH(NEWTOY(cd, NULL, TOYFLAG_NOFORK)) USE_DF(NEWTOY(df, "Pkt*a", TOYFLAG_USR|TOYFLAG_SBIN)) USE_TOYSH(NEWTOY(exit, NULL, TOYFLAG_NOFORK)) USE_HELLO(NEWTOY(hello, NULL, TOYFLAG_NOFORK|TOYFLAG_USR)) +USE_ONEIT(NEWTOY(oneit, "+p<1", TOYFLAG_SBIN)) USE_PWD(NEWTOY(pwd, NULL, TOYFLAG_BIN)) USE_TOYSH(OLDTOY(sh, toysh, "c:i", TOYFLAG_BIN)) USE_TOYSH(NEWTOY(toysh, "c:i", TOYFLAG_BIN)) -- 2.7.4