blob: d1f561b639e5184a0759214dd85de5312a621844 [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 Landley2edf5262006-01-22 02:41:51 +00007 * Licensed under GPLv2 or later, 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 Andersencc8ed391999-10-05 16:24:54 +000011#include <signal.h>
Eric Andersen02462222003-07-22 09:41:39 +000012#include <sys/reboot.h>
Rob Landley64612912006-01-30 08:31:37 +000013#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000014
Rob Landley64612912006-01-30 08:31:37 +000015int halt_main(int argc, char *argv[])
Eric Andersencc8ed391999-10-05 16:24:54 +000016{
Bernhard Reutner-Fischerd765ee52006-05-26 20:34:02 +000017 static const int magic[] = {
18#ifdef RB_HALT_SYSTEM
19RB_HALT_SYSTEM,
20#elif defined RB_HALT
21RB_HALT,
22#endif
23#ifdef RB_POWER_OFF
24RB_POWER_OFF,
25#elif defined RB_POWERDOWN
26RB_POWERDOWN,
27#endif
28RB_AUTOBOOT
29 };
Rob Landley0a7c8ef2006-02-22 17:01:00 +000030 static const int signals[] = {SIGUSR1, SIGUSR2, SIGTERM};
31
Rob Landley64612912006-01-30 08:31:37 +000032 char *delay = "hpr";
Rob Landley0a7c8ef2006-02-22 17:01:00 +000033 int which, flags, rc = 1;
Eric Andersen02462222003-07-22 09:41:39 +000034
Rob Landley64612912006-01-30 08:31:37 +000035 /* Figure out which applet we're running */
36 for(which=0;delay[which]!=*bb_applet_name;which++);
Eric Andersen02462222003-07-22 09:41:39 +000037
Rob Landley64612912006-01-30 08:31:37 +000038 /* Parse and handle arguments */
39 flags = bb_getopt_ulflags(argc, argv, "d:nf", &delay);
40 if (flags&1) sleep(atoi(delay));
41 if (!(flags&2)) sync();
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000042
Rob Landley64612912006-01-30 08:31:37 +000043 /* Perform action. */
44 if (ENABLE_INIT && !(flags & 4)) {
45 if (ENABLE_FEATURE_INITRD) {
46 long *pidlist=find_pid_by_name("linuxrc");
47 if (*pidlist>0) rc = kill(*pidlist,signals[which]);
48 if (ENABLE_FEATURE_CLEAN_UP) free(pidlist);
49 }
50 if (rc) rc = kill(1,signals[which]);
51 } else rc = reboot(magic[which]);
52
53 if (rc) bb_error_msg("No.");
54 return rc;
Eric Andersencc8ed391999-10-05 16:24:54 +000055}