blob: 47480fc212c24e8cfe18ef9f175d89a3b899add5 [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 Landleye9a7a622006-09-22 02:52:41 +00007 * Licensed under GPL version 2, 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 Andersen02462222003-07-22 09:41:39 +000011#include <sys/reboot.h>
Eric Andersen02462222003-07-22 09:41:39 +000012#include <sys/syslog.h>
Eric Andersenb01ed652003-06-27 17:08:15 +000013#include "init_shared.h"
14
Bernhard Reutner-Fischer9a5686b2006-05-31 18:57:56 +000015const char * const init_sending_format = "Sending SIG%s to all processes.";
Eric Andersen02462222003-07-22 09:41:39 +000016#ifndef CONFIG_INIT
Eric Andersen20c9f1e2003-07-22 17:14:10 +000017const char * const bb_shutdown_format = "\r%s\n";
Rob Landleydfba7412006-03-06 20:47:33 +000018int bb_shutdown_system(unsigned long magic)
Eric Andersen02462222003-07-22 09:41:39 +000019{
20 int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK;
Eric Andersen20c9f1e2003-07-22 17:14:10 +000021 const char *message;
Eric Andersen02462222003-07-22 09:41:39 +000022
Eric Andersen20c9f1e2003-07-22 17:14:10 +000023 /* Don't kill ourself */
24 signal(SIGTERM,SIG_IGN);
25 signal(SIGHUP,SIG_IGN);
Bernhard Reutner-Fischerb180e5a2006-05-26 14:24:56 +000026 bb_setpgrp;
Eric Andersen02462222003-07-22 09:41:39 +000027
Eric Andersen20c9f1e2003-07-22 17:14:10 +000028 /* Allow Ctrl-Alt-Del to reboot system. */
Eric Andersen02462222003-07-22 09:41:39 +000029#ifndef RB_ENABLE_CAD
30#define RB_ENABLE_CAD 0x89abcdef
31#endif
Eric Andersen20c9f1e2003-07-22 17:14:10 +000032 reboot(RB_ENABLE_CAD);
Eric Andersen02462222003-07-22 09:41:39 +000033
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000034 openlog(applet_name, 0, pri);
Eric Andersen02462222003-07-22 09:41:39 +000035
Eric Andersen20c9f1e2003-07-22 17:14:10 +000036 message = "\nThe system is going down NOW !!";
Eric Andersen02462222003-07-22 09:41:39 +000037 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000038 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000039
Eric Andersen20c9f1e2003-07-22 17:14:10 +000040 sync();
Eric Andersen02462222003-07-22 09:41:39 +000041
Eric Andersen20c9f1e2003-07-22 17:14:10 +000042 /* Send signals to every process _except_ pid 1 */
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +000043 message = "TERM";
44 syslog(pri, init_sending_format, message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000045 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000046
Eric Andersen20c9f1e2003-07-22 17:14:10 +000047 kill(-1, SIGTERM);
48 sleep(1);
49 sync();
Eric Andersen02462222003-07-22 09:41:39 +000050
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +000051 message = "KILL";
52 syslog(pri, init_sending_format, message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000053 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000054
Eric Andersen20c9f1e2003-07-22 17:14:10 +000055 kill(-1, SIGKILL);
56 sleep(1);
Eric Andersen02462222003-07-22 09:41:39 +000057
Eric Andersen20c9f1e2003-07-22 17:14:10 +000058 sync();
Eric Andersen02462222003-07-22 09:41:39 +000059
Eric Andersen20c9f1e2003-07-22 17:14:10 +000060 reboot(magic);
61 return 0; /* Shrug */
Eric Andersen02462222003-07-22 09:41:39 +000062}
63#endif