blob: 8faf9d8c3f404d0faeaea13ac5e638fc475b4995 [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
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000013int halt_main(int argc, char **argv);
14int halt_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000015{
Bernhard Reutner-Fischerd765ee52006-05-26 20:34:02 +000016 static const int magic[] = {
17#ifdef RB_HALT_SYSTEM
18RB_HALT_SYSTEM,
19#elif defined RB_HALT
20RB_HALT,
21#endif
22#ifdef RB_POWER_OFF
23RB_POWER_OFF,
24#elif defined RB_POWERDOWN
25RB_POWERDOWN,
26#endif
27RB_AUTOBOOT
28 };
Denis Vlasenko35fb5122006-11-01 09:16:49 +000029 static const int signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
Rob Landley0a7c8ef2006-02-22 17:01:00 +000030
Denis Vlasenko35fb5122006-11-01 09:16:49 +000031 char *delay;
Rob Landley0a7c8ef2006-02-22 17:01:00 +000032 int which, flags, rc = 1;
Eric Andersen02462222003-07-22 09:41:39 +000033
Rob Landley64612912006-01-30 08:31:37 +000034 /* Figure out which applet we're running */
Denis Vlasenko35fb5122006-11-01 09:16:49 +000035 for (which = 0; "hpr"[which] != *applet_name; which++);
Eric Andersen02462222003-07-22 09:41:39 +000036
Rob Landley64612912006-01-30 08:31:37 +000037 /* Parse and handle arguments */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000038 flags = getopt32(argc, argv, "d:nf", &delay);
Denis Vlasenko35fb5122006-11-01 09:16:49 +000039 if (flags & 1) sleep(xatou(delay));
40 if (!(flags & 2)) sync();
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000041
Rob Landley64612912006-01-30 08:31:37 +000042 /* Perform action. */
43 if (ENABLE_INIT && !(flags & 4)) {
44 if (ENABLE_FEATURE_INITRD) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000045 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 Landley64612912006-01-30 08:31:37 +000050 }
Denis Vlasenko35fb5122006-11-01 09:16:49 +000051 if (rc)
52 rc = kill(1, signals[which]);
53 } else
54 rc = reboot(magic[which]);
Rob Landley64612912006-01-30 08:31:37 +000055
Denis Vlasenko35fb5122006-11-01 09:16:49 +000056 if (rc)
57 bb_error_msg("no");
Rob Landley64612912006-01-30 08:31:37 +000058 return rc;
Eric Andersencc8ed391999-10-05 16:24:54 +000059}