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 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 13 | int halt_main(int argc, char *argv[]) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 14 | { |
Bernhard Reutner-Fischer | d765ee5 | 2006-05-26 20:34:02 +0000 | [diff] [blame] | 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 | }; |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 28 | static const int signals[] = {SIGUSR1, SIGUSR2, SIGTERM}; |
| 29 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 30 | char *delay = "hpr"; |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 31 | int which, flags, rc = 1; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 32 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 33 | /* Figure out which applet we're running */ |
| 34 | for(which=0;delay[which]!=*bb_applet_name;which++); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 35 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 36 | /* Parse and handle arguments */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame^] | 37 | flags = getopt32(argc, argv, "d:nf", &delay); |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 38 | if (flags&1) sleep(atoi(delay)); |
| 39 | if (!(flags&2)) sync(); |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 40 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 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; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | } |