blob: 38ca38222570aba64bd5dd440571b3b492ddd99f [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
Eric Andersencc8ed391999-10-05 16:24:54 +000010#include <signal.h>
Eric Andersen02462222003-07-22 09:41:39 +000011#include <sys/reboot.h>
12#include "busybox.h"
Eric Andersenb01ed652003-06-27 17:08:15 +000013
Rob Landley64612912006-01-30 08:31:37 +000014#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000015
Rob Landley64612912006-01-30 08:31:37 +000016int halt_main(int argc, char *argv[])
Eric Andersencc8ed391999-10-05 16:24:54 +000017{
Rob Landley0a7c8ef2006-02-22 17:01:00 +000018 static const int magic[] = {RB_HALT_SYSTEM, RB_POWER_OFF, RB_AUTOBOOT};
19 static const int signals[] = {SIGUSR1, SIGUSR2, SIGTERM};
20
Rob Landley64612912006-01-30 08:31:37 +000021 char *delay = "hpr";
Rob Landley0a7c8ef2006-02-22 17:01:00 +000022 int which, flags, rc = 1;
Eric Andersen02462222003-07-22 09:41:39 +000023
Rob Landley64612912006-01-30 08:31:37 +000024 /* Figure out which applet we're running */
25 for(which=0;delay[which]!=*bb_applet_name;which++);
Eric Andersen02462222003-07-22 09:41:39 +000026
Rob Landley64612912006-01-30 08:31:37 +000027 /* 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 Andersencc8ed391999-10-05 16:24:54 +000044}