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 | 2edf526 | 2006-01-22 02:41:51 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | #include <signal.h> |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 11 | #include <sys/reboot.h> |
| 12 | #include "busybox.h" |
Eric Andersen | b01ed65 | 2003-06-27 17:08:15 +0000 | [diff] [blame] | 13 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 15 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 16 | int halt_main(int argc, char *argv[]) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 17 | { |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 18 | static const int magic[] = {RB_HALT_SYSTEM, RB_POWER_OFF, RB_AUTOBOOT}; |
| 19 | static const int signals[] = {SIGUSR1, SIGUSR2, SIGTERM}; |
| 20 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 21 | char *delay = "hpr"; |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 22 | int which, flags, rc = 1; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 23 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 24 | /* Figure out which applet we're running */ |
| 25 | for(which=0;delay[which]!=*bb_applet_name;which++); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 26 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 27 | /* Parse and handle arguments */ |
| 28 | flags = bb_getopt_ulflags(argc, argv, "d:nf", &delay); |
| 29 | if (flags&1) sleep(atoi(delay)); |
| 30 | if (!(flags&2)) sync(); |
| 31 | |
| 32 | /* Perform action. */ |
| 33 | if (ENABLE_INIT && !(flags & 4)) { |
| 34 | if (ENABLE_FEATURE_INITRD) { |
| 35 | long *pidlist=find_pid_by_name("linuxrc"); |
| 36 | if (*pidlist>0) rc = kill(*pidlist,signals[which]); |
| 37 | if (ENABLE_FEATURE_CLEAN_UP) free(pidlist); |
| 38 | } |
| 39 | if (rc) rc = kill(1,signals[which]); |
| 40 | } else rc = reboot(magic[which]); |
| 41 | |
| 42 | if (rc) bb_error_msg("No."); |
| 43 | return rc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | } |