annotate toys/oneit.c @ 156:1e8f4b05cb65

Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment tweaks.
author Rob Landley <rob@landley.net>
date Thu, 15 Nov 2007 18:30:30 -0600
parents c1f4f9101af7
children 25447caf1b4b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
34
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* oneit.c, tiny one-process init replacement.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2005 by Rob Landley <rob@landley.net>.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 */
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
5
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 #include "toys.h"
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 #include <sys/reboot.h>
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 // The minimum amount of work necessary to get ctrl-c and such to work is:
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 //
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 // - Fork a child (PID 1 is special: can't exit, has various signals blocked).
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 // - Do a setsid() (so we have our own session).
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 // - In the child, attach stdio to /dev/tty0 (/dev/console is special)
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 // - Exec the rest of the command line.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 //
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 // PID 1 then reaps zombies until the child process it spawned exits, at which
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 // point it calls sync() and reboot(). I could stick a kill -1 in there.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 int oneit_main(void)
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 {
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 int i;
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 pid_t pid;
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
23
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 // Create a new child process.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 pid = vfork();
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 if (pid) {
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 // pid 1 just reaps zombies until it gets its child, then halts the system.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 while (pid!=wait(&i));
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 sync();
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 reboot(toys.optflags ? RB_POWER_OFF : RB_AUTOBOOT);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 }
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 34
diff changeset
33
34
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 setsid();
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 for (i=0; i<3; i++) {
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 close(i);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 open("/dev/tty0",O_RDWR);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 }
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
40
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 // Can't xexec() here because we vforked so we don't want to error_exit().
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 toy_exec(toys.optargs);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 execvp(*toys.optargs, toys.optargs);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 _exit(127);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 }