annotate sources/toys/oneit.c @ 1248:3e7bf55b1bfb

Tighten up test.sh usage message.
author Rob Landley <rob@landley.net>
date Wed, 15 Sep 2010 18:06:30 -0500
parents 1ad777637d2d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1133
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * oneit.c, tiny one-process init replacement.
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2005, 2010 by Rob Landley <rob@landley.net>.
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 */
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 #include <fcntl.h>
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 #include <stdio.h>
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 #include <stdlib.h>
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 #include <sys/stat.h>
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 #include <sys/types.h>
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 #include <unistd.h>
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 #include <sys/reboot.h>
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 char *console;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 int poweroff;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 // The minimum amount of work necessary to get ctrl-c and such to work is:
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 //
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 // - Fork a child (PID 1 is special: can't exit, has various signals blocked).
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 // - Do a setsid() (so we have our own session).
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 // - In the child, attach stdio to /dev/tty0 (/dev/console is special)
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 // - Exec the rest of the command line.
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 //
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 // PID 1 then reaps zombies until the child process it spawned exits, at which
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 // point it calls sync() and reboot(). I could stick a kill -1 in there.
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
28
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
29
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 int main(int argc, char *argv[])
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 {
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 int i, args;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 pid_t pid;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
34
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 for (args=1; args<argc; args++) {
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 if (*argv[args]=='-') switch (argv[args][1]) {
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 case 'c':
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 console=argv[++args];
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 continue;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 case 'p':
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 poweroff++;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 continue;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 default:
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 args=argc;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 break;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 }
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 break;
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 }
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 if (args>=argc) {
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 fprintf(stderr,
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 "usage: oneit [-p] [-c /dev/tty0] command [...]\n\n"
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 "A simple init program that runs a single supplied command line with a\n"
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 "controlling tty (so CTRL-C can kill it).\n\n"
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 "-p\tPower off instead of rebooting when command exits.\n"
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 "-c\tWhich console device to use.\n\n"
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 "The oneit command runs the supplied command line as a child process\n"
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 "(because PID 1 has signals blocked), attached to /dev/tty0, in its\n"
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 "own session. Then oneit reaps zombies until the child exits, at\n"
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 "which point it reboots (or with -p, powers off) the system.\n");
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 exit(1);
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 }
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
62
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 // Create a new child process.
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 pid = vfork();
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 if (pid) {
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 chdir("/");
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
67
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 // pid 1 just reaps zombies until it gets its child, then halts the system.
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 while (pid!=wait(&i));
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 sync();
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
71
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 // PID 1 can't call reboot() because that syscall kills the task that calls
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 // it, which causes the kernel to panic before the actual reboot happens.
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 if (!vfork()) reboot(poweroff ? RB_POWER_OFF : RB_AUTOBOOT);
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 sleep(5);
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 _exit(1);
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 }
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
78
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 setsid();
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 if (!console) console="/dev/tty0";
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 for (i=0; i<3; i++) {
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 close(i);
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 if(-1==open(console, O_RDWR)) {
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 fprintf(stderr, "Can't open '%s'\n", console);
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 exit(1);
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 }
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 }
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
89
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 execvp(argv[args], argv+args);
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 _exit(127);
1ad777637d2d Migrate oneit out of toybox into sources/toys as a standalone program, and fix it so pid 1 has / as its working directory so we can umount /home if necessary.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 }