blob: 6404da55a1a0b89ae3932044ae247395325653f6 [file] [log] [blame]
Eric Andersen02462222003-07-22 09:41:39 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Stuff shared between init, reboot, halt, and poweroff
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen02462222003-07-22 09:41:39 +00006 *
Rob Landley2edf5262006-01-22 02:41:51 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen02462222003-07-22 09:41:39 +00008 */
9
Eric Andersenb01ed652003-06-27 17:08:15 +000010#include <signal.h>
Eric Andersen02462222003-07-22 09:41:39 +000011#include <stdlib.h>
Bernhard Reutner-Fischerb180e5a2006-05-26 14:24:56 +000012#include <sys/types.h>
Eric Andersen02462222003-07-22 09:41:39 +000013#include <unistd.h>
14#include <getopt.h>
15#include <sys/reboot.h>
Eric Andersen02462222003-07-22 09:41:39 +000016#include <sys/syslog.h>
Eric Andersenb01ed652003-06-27 17:08:15 +000017#include "busybox.h"
Eric Andersenb01ed652003-06-27 17:08:15 +000018#include "init_shared.h"
19
Eric Andersen02462222003-07-22 09:41:39 +000020#ifndef CONFIG_INIT
Eric Andersen20c9f1e2003-07-22 17:14:10 +000021const char * const bb_shutdown_format = "\r%s\n";
Rob Landleydfba7412006-03-06 20:47:33 +000022int bb_shutdown_system(unsigned long magic)
Eric Andersen02462222003-07-22 09:41:39 +000023{
24 int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK;
Eric Andersen20c9f1e2003-07-22 17:14:10 +000025 const char *message;
Eric Andersen02462222003-07-22 09:41:39 +000026
Eric Andersen20c9f1e2003-07-22 17:14:10 +000027 /* Don't kill ourself */
28 signal(SIGTERM,SIG_IGN);
29 signal(SIGHUP,SIG_IGN);
Bernhard Reutner-Fischerb180e5a2006-05-26 14:24:56 +000030 bb_setpgrp;
Eric Andersen02462222003-07-22 09:41:39 +000031
Eric Andersen20c9f1e2003-07-22 17:14:10 +000032 /* Allow Ctrl-Alt-Del to reboot system. */
Eric Andersen02462222003-07-22 09:41:39 +000033#ifndef RB_ENABLE_CAD
34#define RB_ENABLE_CAD 0x89abcdef
35#endif
Eric Andersen20c9f1e2003-07-22 17:14:10 +000036 reboot(RB_ENABLE_CAD);
Eric Andersen02462222003-07-22 09:41:39 +000037
Eric Andersen20c9f1e2003-07-22 17:14:10 +000038 openlog(bb_applet_name, 0, pri);
Eric Andersen02462222003-07-22 09:41:39 +000039
Eric Andersen20c9f1e2003-07-22 17:14:10 +000040 message = "\nThe system is going down NOW !!";
Eric Andersen02462222003-07-22 09:41:39 +000041 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000042 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000043
Eric Andersen20c9f1e2003-07-22 17:14:10 +000044 sync();
Eric Andersen02462222003-07-22 09:41:39 +000045
Eric Andersen20c9f1e2003-07-22 17:14:10 +000046 /* Send signals to every process _except_ pid 1 */
47 message = "Sending SIGTERM to all processes.";
Eric Andersen02462222003-07-22 09:41:39 +000048 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000049 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000050
Eric Andersen20c9f1e2003-07-22 17:14:10 +000051 kill(-1, SIGTERM);
52 sleep(1);
53 sync();
Eric Andersen02462222003-07-22 09:41:39 +000054
Eric Andersen20c9f1e2003-07-22 17:14:10 +000055 message = "Sending SIGKILL to all processes.";
Eric Andersen02462222003-07-22 09:41:39 +000056 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000057 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000058
Eric Andersen20c9f1e2003-07-22 17:14:10 +000059 kill(-1, SIGKILL);
60 sleep(1);
Eric Andersen02462222003-07-22 09:41:39 +000061
Eric Andersen20c9f1e2003-07-22 17:14:10 +000062 sync();
Eric Andersen02462222003-07-22 09:41:39 +000063
Eric Andersen20c9f1e2003-07-22 17:14:10 +000064 reboot(magic);
65 return 0; /* Shrug */
Eric Andersen02462222003-07-22 09:41:39 +000066}
67#endif