[821] | 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 | *
|
---|
[1765] | 7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
[1765] | 10 | #include "libbb.h"
|
---|
[821] | 11 | #include <sys/reboot.h>
|
---|
| 12 |
|
---|
[1765] | 13 | int halt_main(int argc, char **argv);
|
---|
| 14 | int halt_main(int argc, char **argv)
|
---|
[821] | 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 | };
|
---|
[1765] | 29 | static const int signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
|
---|
[821] | 30 |
|
---|
[1765] | 31 | char *delay;
|
---|
[821] | 32 | int which, flags, rc = 1;
|
---|
| 33 |
|
---|
| 34 | /* Figure out which applet we're running */
|
---|
[1765] | 35 | for (which = 0; "hpr"[which] != *applet_name; which++);
|
---|
[821] | 36 |
|
---|
| 37 | /* Parse and handle arguments */
|
---|
[1765] | 38 | flags = getopt32(argv, "d:nf", &delay);
|
---|
| 39 | if (flags & 1) sleep(xatou(delay));
|
---|
| 40 | if (!(flags & 2)) sync();
|
---|
[821] | 41 |
|
---|
| 42 | /* Perform action. */
|
---|
| 43 | if (ENABLE_INIT && !(flags & 4)) {
|
---|
| 44 | if (ENABLE_FEATURE_INITRD) {
|
---|
[1765] | 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);
|
---|
[821] | 50 | }
|
---|
[1765] | 51 | if (rc)
|
---|
| 52 | rc = kill(1, signals[which]);
|
---|
| 53 | } else
|
---|
| 54 | rc = reboot(magic[which]);
|
---|
[821] | 55 |
|
---|
[1765] | 56 | if (rc)
|
---|
| 57 | bb_error_msg("no");
|
---|
[821] | 58 | return rc;
|
---|
| 59 | }
|
---|