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 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 6 | * |
Rob Landley | 2edf526 | 2006-01-22 02:41:51 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | b01ed65 | 2003-06-27 17:08:15 +0000 | [diff] [blame] | 10 | #include <signal.h> |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | #include <unistd.h> |
| 13 | #include <getopt.h> |
| 14 | #include <sys/reboot.h> |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 15 | #include <sys/syslog.h> |
Eric Andersen | b01ed65 | 2003-06-27 17:08:15 +0000 | [diff] [blame] | 16 | #include "busybox.h" |
Eric Andersen | b01ed65 | 2003-06-27 17:08:15 +0000 | [diff] [blame] | 17 | #include "init_shared.h" |
| 18 | |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 19 | #ifndef CONFIG_INIT |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 20 | const char * const bb_shutdown_format = "\r%s\n"; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 21 | int bb_shutdown_system(unsigned long magic) |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 22 | { |
| 23 | int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK; |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 24 | const char *message; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 25 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 26 | /* Don't kill ourself */ |
| 27 | signal(SIGTERM,SIG_IGN); |
| 28 | signal(SIGHUP,SIG_IGN); |
| 29 | setpgrp(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 30 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 31 | /* Allow Ctrl-Alt-Del to reboot system. */ |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 32 | #ifndef RB_ENABLE_CAD |
| 33 | #define RB_ENABLE_CAD 0x89abcdef |
| 34 | #endif |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 35 | reboot(RB_ENABLE_CAD); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 36 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 37 | openlog(bb_applet_name, 0, pri); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 38 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 39 | message = "\nThe system is going down NOW !!"; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 40 | syslog(pri, "%s", message); |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 41 | printf(bb_shutdown_format, message); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 42 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 43 | sync(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 44 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 45 | /* Send signals to every process _except_ pid 1 */ |
| 46 | message = "Sending SIGTERM to all processes."; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 47 | syslog(pri, "%s", message); |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 48 | printf(bb_shutdown_format, message); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 50 | kill(-1, SIGTERM); |
| 51 | sleep(1); |
| 52 | sync(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 53 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 54 | message = "Sending SIGKILL to all processes."; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 55 | syslog(pri, "%s", message); |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 56 | printf(bb_shutdown_format, message); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 57 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 58 | kill(-1, SIGKILL); |
| 59 | sleep(1); |
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 | sync(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 62 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 63 | reboot(magic); |
| 64 | return 0; /* Shrug */ |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 65 | } |
| 66 | #endif |