blob: d796a4aea6f0053e32c40c091e2d0c40c807463f [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
Rob Landley64612912006-01-30 08:31:37 +00003 * Poweroff reboot and halt, oh my.
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
Rob Landley64612912006-01-30 08:31:37 +00005 * Copyright 2006 by Rob Landley <rob@landley.net>
Eric Andersenc4996011999-10-20 22:08:37 +00006 *
Rob Landleye9a7a622006-09-22 02:52:41 +00007 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
Eric Andersenc4996011999-10-20 22:08:37 +00008 */
9
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000010#include "busybox.h"
Eric Andersen02462222003-07-22 09:41:39 +000011#include <sys/reboot.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000012
Rob Landley64612912006-01-30 08:31:37 +000013int halt_main(int argc, char *argv[])
Eric Andersencc8ed391999-10-05 16:24:54 +000014{
Bernhard Reutner-Fischerd765ee52006-05-26 20:34:02 +000015 static const int magic[] = {
16#ifdef RB_HALT_SYSTEM
17RB_HALT_SYSTEM,
18#elif defined RB_HALT
19RB_HALT,
20#endif
21#ifdef RB_POWER_OFF
22RB_POWER_OFF,
23#elif defined RB_POWERDOWN
24RB_POWERDOWN,
25#endif
26RB_AUTOBOOT
27 };
Rob Landley0a7c8ef2006-02-22 17:01:00 +000028 static const int signals[] = {SIGUSR1, SIGUSR2, SIGTERM};
29
Rob Landley64612912006-01-30 08:31:37 +000030 char *delay = "hpr";
Rob Landley0a7c8ef2006-02-22 17:01:00 +000031 int which, flags, rc = 1;
Eric Andersen02462222003-07-22 09:41:39 +000032
Rob Landley64612912006-01-30 08:31:37 +000033 /* Figure out which applet we're running */
34 for(which=0;delay[which]!=*bb_applet_name;which++);
Eric Andersen02462222003-07-22 09:41:39 +000035
Rob Landley64612912006-01-30 08:31:37 +000036 /* Parse and handle arguments */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000037 flags = getopt32(argc, argv, "d:nf", &delay);
Rob Landley64612912006-01-30 08:31:37 +000038 if (flags&1) sleep(atoi(delay));
39 if (!(flags&2)) sync();
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000040
Rob Landley64612912006-01-30 08:31:37 +000041 /* 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 Andersencc8ed391999-10-05 16:24:54 +000053}