blob: a6cf48bbe1fb4bec383b01fd82914cebaf984624 [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 };
Denis Vlasenko35fb5122006-11-01 09:16:49 +000028 static const int signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
Rob Landley0a7c8ef2006-02-22 17:01:00 +000029
Denis Vlasenko35fb5122006-11-01 09:16:49 +000030 char *delay;
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 */
Denis Vlasenko35fb5122006-11-01 09:16:49 +000034 for (which = 0; "hpr"[which] != *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);
Denis Vlasenko35fb5122006-11-01 09:16:49 +000038 if (flags & 1) sleep(xatou(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) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000044 pid_t *pidlist = find_pid_by_name("linuxrc");
45 if (pidlist[0] > 0)
46 rc = kill(pidlist[0], signals[which]);
47 if (ENABLE_FEATURE_CLEAN_UP)
48 free(pidlist);
Rob Landley64612912006-01-30 08:31:37 +000049 }
Denis Vlasenko35fb5122006-11-01 09:16:49 +000050 if (rc)
51 rc = kill(1, signals[which]);
52 } else
53 rc = reboot(magic[which]);
Rob Landley64612912006-01-30 08:31:37 +000054
Denis Vlasenko35fb5122006-11-01 09:16:49 +000055 if (rc)
56 bb_error_msg("no");
Rob Landley64612912006-01-30 08:31:37 +000057 return rc;
Eric Andersencc8ed391999-10-05 16:24:54 +000058}