/* oneit.c, tiny one-process init replacement. * * Copyright 2005 by Rob Landley. Released under gpl v2. */ #include #include #include #include #include // World's most brain-dead init program. Does the minimum amount of work // necessary to get ctrl-c and such to work. // // Fork a child (PID 1 is special: it can't exit and has various signals // blocked). In the child, attach stdio to /dev/tty0 (so we have a controlling // tty, plus do a setsid()), then 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(). int main(int argc, char *argv[]) { int a; pid_t pid; // pid 1 just reaps zombies until it gets its child, then halts the system. pid=fork(); if(pid) { while(pid!=wait()); sync(); reboot(LINUX_REBOOT_CMD_HALT); } // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works. setsid(); for(a=0;a<3;a++) { close(a); open("/dev/tty0",O_RDWR); } execvp(argv[1],argv+1); }