blob: 0ad55a433c661858cee70b5ba8ee3cf2bd749338 [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Eric Andersenb01ed652003-06-27 17:08:15 +000023#include <signal.h>
Eric Andersen02462222003-07-22 09:41:39 +000024#include <stdlib.h>
25#include <unistd.h>
26#include <getopt.h>
27#include <sys/reboot.h>
Eric Andersen02462222003-07-22 09:41:39 +000028#include <sys/syslog.h>
Eric Andersenb01ed652003-06-27 17:08:15 +000029#include "busybox.h"
Eric Andersenb01ed652003-06-27 17:08:15 +000030#include "init_shared.h"
31
Eric Andersenb01ed652003-06-27 17:08:15 +000032extern int kill_init(int sig)
33{
34#ifdef CONFIG_FEATURE_INITRD
35 /* don't assume init's pid == 1 */
36 long *pid = find_pid_by_name("init");
37 if (!pid || *pid<=0) {
38 pid = find_pid_by_name("linuxrc");
39 if (!pid || *pid<=0)
40 bb_error_msg_and_die("no process killed");
41 }
42 return(kill(*pid, sig));
43#else
44 return(kill(1, sig));
45#endif
46}
Eric Andersen02462222003-07-22 09:41:39 +000047
48#ifndef CONFIG_INIT
Eric Andersen20c9f1e2003-07-22 17:14:10 +000049const char * const bb_shutdown_format = "\r%s\n";
Eric Andersen02462222003-07-22 09:41:39 +000050extern int bb_shutdown_system(unsigned long magic)
51{
52 int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK;
Eric Andersen20c9f1e2003-07-22 17:14:10 +000053 const char *message;
Eric Andersen02462222003-07-22 09:41:39 +000054
Eric Andersen20c9f1e2003-07-22 17:14:10 +000055 /* Don't kill ourself */
56 signal(SIGTERM,SIG_IGN);
57 signal(SIGHUP,SIG_IGN);
58 setpgrp();
Eric Andersen02462222003-07-22 09:41:39 +000059
Eric Andersen20c9f1e2003-07-22 17:14:10 +000060 /* Allow Ctrl-Alt-Del to reboot system. */
Eric Andersen02462222003-07-22 09:41:39 +000061#ifndef RB_ENABLE_CAD
62#define RB_ENABLE_CAD 0x89abcdef
63#endif
Eric Andersen20c9f1e2003-07-22 17:14:10 +000064 reboot(RB_ENABLE_CAD);
Eric Andersen02462222003-07-22 09:41:39 +000065
Eric Andersen20c9f1e2003-07-22 17:14:10 +000066 openlog(bb_applet_name, 0, pri);
Eric Andersen02462222003-07-22 09:41:39 +000067
Eric Andersen20c9f1e2003-07-22 17:14:10 +000068 message = "\nThe system is going down NOW !!";
Eric Andersen02462222003-07-22 09:41:39 +000069 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000070 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000071
Eric Andersen20c9f1e2003-07-22 17:14:10 +000072 sync();
Eric Andersen02462222003-07-22 09:41:39 +000073
Eric Andersen20c9f1e2003-07-22 17:14:10 +000074 /* Send signals to every process _except_ pid 1 */
75 message = "Sending SIGTERM to all processes.";
Eric Andersen02462222003-07-22 09:41:39 +000076 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000077 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000078
Eric Andersen20c9f1e2003-07-22 17:14:10 +000079 kill(-1, SIGTERM);
80 sleep(1);
81 sync();
Eric Andersen02462222003-07-22 09:41:39 +000082
Eric Andersen20c9f1e2003-07-22 17:14:10 +000083 message = "Sending SIGKILL to all processes.";
Eric Andersen02462222003-07-22 09:41:39 +000084 syslog(pri, "%s", message);
Eric Andersen20c9f1e2003-07-22 17:14:10 +000085 printf(bb_shutdown_format, message);
Eric Andersen02462222003-07-22 09:41:39 +000086
Eric Andersen20c9f1e2003-07-22 17:14:10 +000087 kill(-1, SIGKILL);
88 sleep(1);
Eric Andersen02462222003-07-22 09:41:39 +000089
Eric Andersen20c9f1e2003-07-22 17:14:10 +000090 sync();
Eric Andersen02462222003-07-22 09:41:39 +000091
Eric Andersen20c9f1e2003-07-22 17:14:10 +000092 reboot(magic);
93 return 0; /* Shrug */
Eric Andersen02462222003-07-22 09:41:39 +000094}
95#endif