Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 3 | * Poweroff reboot and halt, oh my. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 5 | * Copyright 2006 by Rob Landley <rob@landley.net> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 6 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 11 | #include <sys/reboot.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 12 | |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 13 | int halt_main(int argc, char **argv); |
| 14 | int halt_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 15 | { |
Bernhard Reutner-Fischer | d765ee5 | 2006-05-26 20:34:02 +0000 | [diff] [blame] | 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 | }; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 29 | static const int signals[] = { SIGUSR1, SIGUSR2, SIGTERM }; |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 31 | char *delay; |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 32 | int which, flags, rc = 1; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 33 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 34 | /* Figure out which applet we're running */ |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 35 | for (which = 0; "hpr"[which] != *applet_name; which++); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 36 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 37 | /* Parse and handle arguments */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 38 | flags = getopt32(argc, argv, "d:nf", &delay); |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 39 | if (flags & 1) sleep(xatou(delay)); |
| 40 | if (!(flags & 2)) sync(); |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 41 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 42 | /* Perform action. */ |
| 43 | if (ENABLE_INIT && !(flags & 4)) { |
| 44 | if (ENABLE_FEATURE_INITRD) { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 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); |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 50 | } |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 51 | if (rc) |
| 52 | rc = kill(1, signals[which]); |
| 53 | } else |
| 54 | rc = reboot(magic[which]); |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 56 | if (rc) |
| 57 | bb_error_msg("no"); |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 58 | return rc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 59 | } |