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