Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Stuff shared between init, reboot, halt, and poweroff |
| 4 | * |
| 5 | * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org> |
| 6 | * |
| 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 Andersen | b01ed65 | 2003-06-27 17:08:15 +0000 | [diff] [blame] | 23 | #include <signal.h> |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <getopt.h> |
| 27 | #include <sys/reboot.h> |
| 28 | #include <sys/reboot.h> |
| 29 | #include <sys/syslog.h> |
Eric Andersen | b01ed65 | 2003-06-27 17:08:15 +0000 | [diff] [blame] | 30 | #include "busybox.h" |
Eric Andersen | b01ed65 | 2003-06-27 17:08:15 +0000 | [diff] [blame] | 31 | #include "init_shared.h" |
| 32 | |
Eric Andersen | b01ed65 | 2003-06-27 17:08:15 +0000 | [diff] [blame] | 33 | extern int kill_init(int sig) |
| 34 | { |
| 35 | #ifdef CONFIG_FEATURE_INITRD |
| 36 | /* don't assume init's pid == 1 */ |
| 37 | long *pid = find_pid_by_name("init"); |
| 38 | if (!pid || *pid<=0) { |
| 39 | pid = find_pid_by_name("linuxrc"); |
| 40 | if (!pid || *pid<=0) |
| 41 | bb_error_msg_and_die("no process killed"); |
| 42 | } |
| 43 | return(kill(*pid, sig)); |
| 44 | #else |
| 45 | return(kill(1, sig)); |
| 46 | #endif |
| 47 | } |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 48 | |
| 49 | #ifndef CONFIG_INIT |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 50 | const char * const bb_shutdown_format = "\r%s\n"; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 51 | extern int bb_shutdown_system(unsigned long magic) |
| 52 | { |
| 53 | int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK; |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 54 | const char *message; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 55 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 56 | /* Don't kill ourself */ |
| 57 | signal(SIGTERM,SIG_IGN); |
| 58 | signal(SIGHUP,SIG_IGN); |
| 59 | setpgrp(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 60 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 61 | /* Allow Ctrl-Alt-Del to reboot system. */ |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 62 | #ifndef RB_ENABLE_CAD |
| 63 | #define RB_ENABLE_CAD 0x89abcdef |
| 64 | #endif |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 65 | reboot(RB_ENABLE_CAD); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 66 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 67 | openlog(bb_applet_name, 0, pri); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 68 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 69 | message = "\nThe system is going down NOW !!"; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 70 | syslog(pri, "%s", message); |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 71 | printf(bb_shutdown_format, message); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 72 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 73 | sync(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 74 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 75 | /* Send signals to every process _except_ pid 1 */ |
| 76 | message = "Sending SIGTERM to all processes."; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 77 | syslog(pri, "%s", message); |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 78 | printf(bb_shutdown_format, message); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 79 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 80 | kill(-1, SIGTERM); |
| 81 | sleep(1); |
| 82 | sync(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 83 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 84 | message = "Sending SIGKILL to all processes."; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 85 | syslog(pri, "%s", message); |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 86 | printf(bb_shutdown_format, message); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 87 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 88 | kill(-1, SIGKILL); |
| 89 | sleep(1); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 90 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 91 | sync(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 92 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 93 | reboot(magic); |
| 94 | return 0; /* Shrug */ |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 95 | } |
| 96 | #endif |
| 97 | |