Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini reboot implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Eric Andersen | 1d1d2f9 | 2002-04-13 08:31:59 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * |
| 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | #include <signal.h> |
Eric Andersen | 34fd00a | 2002-09-17 08:40:12 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include <getopt.h> |
| 28 | |
Glenn L McGrath | 4e05b9b | 2002-12-07 23:14:40 +0000 | [diff] [blame] | 29 | #include "busybox.h" |
| 30 | |
Eric Andersen | 34fd00a | 2002-09-17 08:40:12 +0000 | [diff] [blame] | 31 | #if (__GNU_LIBRARY__ > 5) || defined(__dietlibc__) |
| 32 | #include <sys/reboot.h> |
| 33 | #define init_reboot(magic) reboot(magic) |
| 34 | #else |
| 35 | #define init_reboot(magic) reboot(0xfee1dead, 672274793, magic) |
| 36 | #endif |
Glenn L McGrath | 4e05b9b | 2002-12-07 23:14:40 +0000 | [diff] [blame] | 37 | |
Eric Andersen | 34fd00a | 2002-09-17 08:40:12 +0000 | [diff] [blame] | 38 | #ifndef RB_ENABLE_CAD |
| 39 | static const int RB_ENABLE_CAD = 0x89abcdef; |
| 40 | static const int RB_AUTOBOOT = 0x01234567; |
| 41 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 42 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | extern int reboot_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | { |
Eric Andersen | 34fd00a | 2002-09-17 08:40:12 +0000 | [diff] [blame] | 45 | int delay = 0; /* delay in seconds before rebooting */ |
| 46 | int rc; |
| 47 | |
| 48 | while ((rc = getopt(argc, argv, "d:")) > 0) { |
| 49 | switch (rc) { |
| 50 | case 'd': |
| 51 | delay = atoi(optarg); |
| 52 | break; |
| 53 | |
| 54 | default: |
| 55 | show_usage(); |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if(delay > 0) |
| 61 | sleep(delay); |
| 62 | |
| 63 | #ifdef CONFIG_USER_INIT |
| 64 | /* Don't kill ourself */ |
| 65 | signal(SIGTERM,SIG_IGN); |
| 66 | signal(SIGHUP,SIG_IGN); |
| 67 | setpgrp(); |
| 68 | |
| 69 | /* Allow Ctrl-Alt-Del to reboot system. */ |
| 70 | init_reboot(RB_ENABLE_CAD); |
| 71 | |
| 72 | message(CONSOLE|LOG, "\n\rThe system is going down NOW !!\n"); |
| 73 | sync(); |
| 74 | |
| 75 | /* Send signals to every process _except_ pid 1 */ |
| 76 | message(CONSOLE|LOG, "\rSending SIGTERM to all processes.\n"); |
| 77 | kill(-1, SIGTERM); |
| 78 | sleep(1); |
| 79 | sync(); |
| 80 | |
| 81 | message(CONSOLE|LOG, "\rSending SIGKILL to all processes.\n"); |
| 82 | kill(-1, SIGKILL); |
| 83 | sleep(1); |
| 84 | |
| 85 | sync(); |
| 86 | if (kernelVersion > 0 && kernelVersion <= KERNEL_VERSION(2,2,11)) { |
| 87 | /* bdflush, kupdate not needed for kernels >2.2.11 */ |
| 88 | bdflush(1, 0); |
| 89 | sync(); |
| 90 | } |
| 91 | |
| 92 | init_reboot(RB_AUTOBOOT); |
| 93 | exit(0); /* Shrug */ |
| 94 | #else |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 95 | #ifdef CONFIG_FEATURE_INITRD |
Eric Andersen | 34fd00a | 2002-09-17 08:40:12 +0000 | [diff] [blame] | 96 | { |
| 97 | /* don't assume init's pid == 1 */ |
| 98 | long *pid = find_pid_by_name("init"); |
| 99 | if (!pid || *pid<=0) |
| 100 | pid = find_pid_by_name("linuxrc"); |
Eric Andersen | 371ca19 | 2001-10-03 21:26:12 +0000 | [diff] [blame] | 101 | if (!pid || *pid<=0) |
| 102 | error_msg_and_die("no process killed"); |
Eric Andersen | 34fd00a | 2002-09-17 08:40:12 +0000 | [diff] [blame] | 103 | fflush(stdout); |
| 104 | return(kill(*pid, SIGTERM)); |
Eric Andersen | 371ca19 | 2001-10-03 21:26:12 +0000 | [diff] [blame] | 105 | } |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 106 | #else |
Eric Andersen | c97ec34 | 2001-04-03 18:01:51 +0000 | [diff] [blame] | 107 | return(kill(1, SIGTERM)); |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 108 | #endif |
Eric Andersen | 34fd00a | 2002-09-17 08:40:12 +0000 | [diff] [blame] | 109 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 110 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | Local Variables: |
| 114 | c-file-style: "linux" |
| 115 | c-basic-offset: 4 |
| 116 | tab-width: 4 |
| 117 | End: |
| 118 | */ |