blob: 1c9eedde53c52fa886a18bc5ba69055ad985dab3 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
3 * Mini reboot implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersen1d1d2f92002-04-13 08:31:59 +00006 * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Eric Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include <signal.h>
Eric Andersen34fd00a2002-09-17 08:40:12 +000026#include <stdlib.h>
27#include <unistd.h>
28#include <getopt.h>
29
30#if (__GNU_LIBRARY__ > 5) || defined(__dietlibc__)
31 #include <sys/reboot.h>
32 #define init_reboot(magic) reboot(magic)
33#else
34 #define init_reboot(magic) reboot(0xfee1dead, 672274793, magic)
35#endif
36#ifndef RB_ENABLE_CAD
37static const int RB_ENABLE_CAD = 0x89abcdef;
38static const int RB_AUTOBOOT = 0x01234567;
39#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000040
Erik Andersene49d5ec2000-02-08 19:58:47 +000041extern int reboot_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000042{
Eric Andersen34fd00a2002-09-17 08:40:12 +000043 int delay = 0; /* delay in seconds before rebooting */
44 int rc;
45
46 while ((rc = getopt(argc, argv, "d:")) > 0) {
47 switch (rc) {
48 case 'd':
49 delay = atoi(optarg);
50 break;
51
52 default:
53 show_usage();
54 break;
55 }
56 }
57
58 if(delay > 0)
59 sleep(delay);
60
61#ifdef CONFIG_USER_INIT
62 /* Don't kill ourself */
63 signal(SIGTERM,SIG_IGN);
64 signal(SIGHUP,SIG_IGN);
65 setpgrp();
66
67 /* Allow Ctrl-Alt-Del to reboot system. */
68 init_reboot(RB_ENABLE_CAD);
69
70 message(CONSOLE|LOG, "\n\rThe system is going down NOW !!\n");
71 sync();
72
73 /* Send signals to every process _except_ pid 1 */
74 message(CONSOLE|LOG, "\rSending SIGTERM to all processes.\n");
75 kill(-1, SIGTERM);
76 sleep(1);
77 sync();
78
79 message(CONSOLE|LOG, "\rSending SIGKILL to all processes.\n");
80 kill(-1, SIGKILL);
81 sleep(1);
82
83 sync();
84 if (kernelVersion > 0 && kernelVersion <= KERNEL_VERSION(2,2,11)) {
85 /* bdflush, kupdate not needed for kernels >2.2.11 */
86 bdflush(1, 0);
87 sync();
88 }
89
90 init_reboot(RB_AUTOBOOT);
91 exit(0); /* Shrug */
92#else
Eric Andersenbdfd0d72001-10-24 05:00:29 +000093#ifdef CONFIG_FEATURE_INITRD
Eric Andersen34fd00a2002-09-17 08:40:12 +000094 {
95 /* don't assume init's pid == 1 */
96 long *pid = find_pid_by_name("init");
97 if (!pid || *pid<=0)
98 pid = find_pid_by_name("linuxrc");
Eric Andersen371ca192001-10-03 21:26:12 +000099 if (!pid || *pid<=0)
100 error_msg_and_die("no process killed");
Eric Andersen34fd00a2002-09-17 08:40:12 +0000101 fflush(stdout);
102 return(kill(*pid, SIGTERM));
Eric Andersen371ca192001-10-03 21:26:12 +0000103 }
Erik Andersen2ac2fae2000-03-07 23:32:17 +0000104#else
Eric Andersenc97ec342001-04-03 18:01:51 +0000105 return(kill(1, SIGTERM));
Erik Andersen2ac2fae2000-03-07 23:32:17 +0000106#endif
Eric Andersen34fd00a2002-09-17 08:40:12 +0000107#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000108}
Erik Andersen029011b2000-03-04 21:19:32 +0000109
110/*
111Local Variables:
112c-file-style: "linux"
113c-basic-offset: 4
114tab-width: 4
115End:
116*/