usage.c: remove reference to busybox.h
[platform/upstream/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 GPL version 2, see file LICENSE in this tarball for details.
8  */
9
10 #include "libbb.h"
11 #include <sys/reboot.h>
12
13 int halt_main(int argc, char **argv);
14 int halt_main(int argc, char **argv)
15 {
16         static const int magic[] = {
17 #ifdef RB_HALT_SYSTEM
18 RB_HALT_SYSTEM,
19 #elif defined RB_HALT
20 RB_HALT,
21 #endif
22 #ifdef RB_POWER_OFF
23 RB_POWER_OFF,
24 #elif defined RB_POWERDOWN
25 RB_POWERDOWN,
26 #endif
27 RB_AUTOBOOT
28         };
29         static const int signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
30
31         char *delay;
32         int which, flags, rc = 1;
33
34         /* Figure out which applet we're running */
35         for (which = 0; "hpr"[which] != *applet_name; which++);
36
37         /* Parse and handle arguments */
38         flags = getopt32(argc, argv, "d:nf", &delay);
39         if (flags & 1) sleep(xatou(delay));
40         if (!(flags & 2)) sync();
41
42         /* Perform action. */
43         if (ENABLE_INIT && !(flags & 4)) {
44                 if (ENABLE_FEATURE_INITRD) {
45                         pid_t *pidlist = find_pid_by_name("linuxrc");
46                         if (pidlist[0] > 0)
47                                 rc = kill(pidlist[0], signals[which]);
48                         if (ENABLE_FEATURE_CLEAN_UP)
49                                 free(pidlist);
50                 }
51                 if (rc)
52                         rc = kill(1, signals[which]);
53         } else
54                 rc = reboot(magic[which]);
55
56         if (rc)
57                 bb_error_msg("no");
58         return rc;
59 }