blob: 0abe72fdf65a677815b310c02027955753048b74 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000010#include "busybox.h"
Eric Andersenb01ed652003-06-27 17:08:15 +000011#include <signal.h>
Eric Andersen02462222003-07-22 09:41:39 +000012#include <stdlib.h>
Bernhard Reutner-Fischerb180e5a2006-05-26 14:24:56 +000013#include <sys/types.h>
Eric Andersen02462222003-07-22 09:41:39 +000014#include <unistd.h>
15#include <getopt.h>
16#include <sys/reboot.h>
Eric Andersen02462222003-07-22 09:41:39 +000017#include <sys/syslog.h>
Eric Andersenb01ed652003-06-27 17:08:15 +000018#include "init_shared.h"
19
Bernhard Reutner-Fischer9a5686b2006-05-31 18:57:56 +000020const char * const init_sending_format = "Sending SIG%s to all processes.";
Eric Andersen02462222003-07-22 09:41:39 +000021#ifndef CONFIG_INIT
Eric Andersen20c9f1e2003-07-22 17:14:10 +000022const char * const bb_shutdown_format = "\r%s\n";
Rob Landleydfba7412006-03-06 20:47:33 +000023int bb_shutdown_system(unsigned long magic)
Eric Andersen02462222003-07-22 09:41:39 +000024{
25 int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK;
Eric Andersen20c9f1e2003-07-22 17:14:10 +000026 const char *message;
Eric Andersen02462222003-07-22 09:41:39 +000027
Eric Andersen20c9f1e2003-07-22 17:14:10 +000028 /* Don't kill ourself */
29 signal(SIGTERM,SIG_IGN);
30 signal(SIGHUP,SIG_IGN);
Bernhard Reutner-Fischerb180e5a2006-05-26 14:24:56 +000031 bb_setpgrp;
Eric Andersen02462222003-07-22 09:41:39 +000032
Eric Andersen20c9f1e2003-07-22 17:14:10 +000033 /* Allow Ctrl-Alt-Del to reboot system. */
Eric Andersen02462222003-07-22 09:41:39 +000034#ifndef RB_ENABLE_CAD
35#define RB_ENABLE_CAD 0x89abcdef
36#endif
Eric Andersen20c9f1e2003-07-22 17:14:10 +000037 reboot(RB_ENABLE_CAD);
Eric Andersen02462222003-07-22 09:41:39 +000038
Eric Andersen20c9f1e2003-07-22 17:14:10 +000039 openlog(bb_applet_name, 0, pri);
Eric Andersen02462222003-07-22 09:41:39 +000040
Eric Andersen20c9f1e2003-07-22 17:14:10 +000041 message = "\nThe system is going down NOW !!";
Eric Andersen02462222003-07-22 09:41:39 +000042 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000043 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000044
Eric Andersen20c9f1e2003-07-22 17:14:10 +000045 sync();
Eric Andersen02462222003-07-22 09:41:39 +000046
Eric Andersen20c9f1e2003-07-22 17:14:10 +000047 /* Send signals to every process _except_ pid 1 */
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +000048 message = "TERM";
49 syslog(pri, init_sending_format, message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000050 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000051
Eric Andersen20c9f1e2003-07-22 17:14:10 +000052 kill(-1, SIGTERM);
53 sleep(1);
54 sync();
Eric Andersen02462222003-07-22 09:41:39 +000055
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +000056 message = "KILL";
57 syslog(pri, init_sending_format, message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000058 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000059
Eric Andersen20c9f1e2003-07-22 17:14:10 +000060 kill(-1, SIGKILL);
61 sleep(1);
Eric Andersen02462222003-07-22 09:41:39 +000062
Eric Andersen20c9f1e2003-07-22 17:14:10 +000063 sync();
Eric Andersen02462222003-07-22 09:41:39 +000064
Eric Andersen20c9f1e2003-07-22 17:14:10 +000065 reboot(magic);
66 return 0; /* Shrug */
Eric Andersen02462222003-07-22 09:41:39 +000067}
68#endif