blob: 51e1496c24b94443c20fc4658c4173a6d4c95e6f [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>
12#include <unistd.h>
13#include <getopt.h>
14#include <sys/reboot.h>
Eric Andersen02462222003-07-22 09:41:39 +000015#include <sys/syslog.h>
Eric Andersenb01ed652003-06-27 17:08:15 +000016#include "busybox.h"
Eric Andersenb01ed652003-06-27 17:08:15 +000017#include "init_shared.h"
18
Eric Andersen02462222003-07-22 09:41:39 +000019#ifndef CONFIG_INIT
Eric Andersen20c9f1e2003-07-22 17:14:10 +000020const char * const bb_shutdown_format = "\r%s\n";
Rob Landleydfba7412006-03-06 20:47:33 +000021int bb_shutdown_system(unsigned long magic)
Eric Andersen02462222003-07-22 09:41:39 +000022{
23 int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK;
Eric Andersen20c9f1e2003-07-22 17:14:10 +000024 const char *message;
Eric Andersen02462222003-07-22 09:41:39 +000025
Eric Andersen20c9f1e2003-07-22 17:14:10 +000026 /* Don't kill ourself */
27 signal(SIGTERM,SIG_IGN);
28 signal(SIGHUP,SIG_IGN);
29 setpgrp();
Eric Andersen02462222003-07-22 09:41:39 +000030
Eric Andersen20c9f1e2003-07-22 17:14:10 +000031 /* Allow Ctrl-Alt-Del to reboot system. */
Eric Andersen02462222003-07-22 09:41:39 +000032#ifndef RB_ENABLE_CAD
33#define RB_ENABLE_CAD 0x89abcdef
34#endif
Eric Andersen20c9f1e2003-07-22 17:14:10 +000035 reboot(RB_ENABLE_CAD);
Eric Andersen02462222003-07-22 09:41:39 +000036
Eric Andersen20c9f1e2003-07-22 17:14:10 +000037 openlog(bb_applet_name, 0, pri);
Eric Andersen02462222003-07-22 09:41:39 +000038
Eric Andersen20c9f1e2003-07-22 17:14:10 +000039 message = "\nThe system is going down NOW !!";
Eric Andersen02462222003-07-22 09:41:39 +000040 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000041 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000042
Eric Andersen20c9f1e2003-07-22 17:14:10 +000043 sync();
Eric Andersen02462222003-07-22 09:41:39 +000044
Eric Andersen20c9f1e2003-07-22 17:14:10 +000045 /* Send signals to every process _except_ pid 1 */
46 message = "Sending SIGTERM to all processes.";
Eric Andersen02462222003-07-22 09:41:39 +000047 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000048 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000049
Eric Andersen20c9f1e2003-07-22 17:14:10 +000050 kill(-1, SIGTERM);
51 sleep(1);
52 sync();
Eric Andersen02462222003-07-22 09:41:39 +000053
Eric Andersen20c9f1e2003-07-22 17:14:10 +000054 message = "Sending SIGKILL to all processes.";
Eric Andersen02462222003-07-22 09:41:39 +000055 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000056 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000057
Eric Andersen20c9f1e2003-07-22 17:14:10 +000058 kill(-1, SIGKILL);
59 sleep(1);
Eric Andersen02462222003-07-22 09:41:39 +000060
Eric Andersen20c9f1e2003-07-22 17:14:10 +000061 sync();
Eric Andersen02462222003-07-22 09:41:39 +000062
Eric Andersen20c9f1e2003-07-22 17:14:10 +000063 reboot(magic);
64 return 0; /* Shrug */
Eric Andersen02462222003-07-22 09:41:39 +000065}
66#endif