- spelling
[oweals/busybox.git] / init / halt.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Poweroff reboot and halt, oh my.
4  *
5  * Copyright 2006 by Rob Landley <rob@landley.net>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include "busybox.h"
11 #include <signal.h>
12 #include <sys/reboot.h>
13 #include <unistd.h>
14
15 int halt_main(int argc, char *argv[])
16 {
17         static const int magic[] = {
18 #ifdef RB_HALT_SYSTEM
19 RB_HALT_SYSTEM,
20 #elif defined RB_HALT
21 RB_HALT,
22 #endif
23 #ifdef RB_POWER_OFF
24 RB_POWER_OFF,
25 #elif defined RB_POWERDOWN
26 RB_POWERDOWN,
27 #endif
28 RB_AUTOBOOT
29         };
30         static const int signals[] = {SIGUSR1, SIGUSR2, SIGTERM};
31
32         char *delay = "hpr";
33         int which, flags, rc = 1;
34
35         /* Figure out which applet we're running */
36         for(which=0;delay[which]!=*bb_applet_name;which++);
37
38         /* Parse and handle arguments */
39         flags = bb_getopt_ulflags(argc, argv, "d:nf", &delay);
40         if (flags&1) sleep(atoi(delay));
41         if (!(flags&2)) sync();
42
43         /* Perform action. */
44         if (ENABLE_INIT && !(flags & 4)) {
45                 if (ENABLE_FEATURE_INITRD) {
46                         long *pidlist=find_pid_by_name("linuxrc");
47                         if (*pidlist>0) rc = kill(*pidlist,signals[which]);
48                         if (ENABLE_FEATURE_CLEAN_UP) free(pidlist);
49                 }
50                 if (rc) rc = kill(1,signals[which]);
51         } else rc = reboot(magic[which]);
52
53         if (rc) bb_error_msg("No.");
54         return rc;
55 }