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 | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 11 | #include <sys/reboot.h> |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 12 | #include <sys/syslog.h> |
Eric Andersen | b01ed65 | 2003-06-27 17:08:15 +0000 | [diff] [blame] | 13 | #include "init_shared.h" |
| 14 | |
Bernhard Reutner-Fischer | 9a5686b | 2006-05-31 18:57:56 +0000 | [diff] [blame] | 15 | const char * const init_sending_format = "Sending SIG%s to all processes."; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 16 | #ifndef CONFIG_INIT |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 17 | const char * const bb_shutdown_format = "\r%s\n"; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 18 | int bb_shutdown_system(unsigned long magic) |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 19 | { |
| 20 | int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK; |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 21 | const char *message; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 22 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 23 | /* Don't kill ourself */ |
| 24 | signal(SIGTERM,SIG_IGN); |
| 25 | signal(SIGHUP,SIG_IGN); |
Bernhard Reutner-Fischer | b180e5a | 2006-05-26 14:24:56 +0000 | [diff] [blame] | 26 | bb_setpgrp; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 27 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 28 | /* Allow Ctrl-Alt-Del to reboot system. */ |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 29 | #ifndef RB_ENABLE_CAD |
| 30 | #define RB_ENABLE_CAD 0x89abcdef |
| 31 | #endif |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 32 | reboot(RB_ENABLE_CAD); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 34 | openlog(applet_name, 0, pri); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 35 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 36 | message = "\nThe system is going down NOW !!"; |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 37 | syslog(pri, "%s", message); |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 38 | printf(bb_shutdown_format, message); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 39 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 40 | sync(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 41 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 42 | /* Send signals to every process _except_ pid 1 */ |
Bernhard Reutner-Fischer | 7ae1553 | 2006-05-30 18:17:21 +0000 | [diff] [blame] | 43 | message = "TERM"; |
| 44 | syslog(pri, init_sending_format, message); |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 45 | printf(bb_shutdown_format, message); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 46 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 47 | kill(-1, SIGTERM); |
| 48 | sleep(1); |
| 49 | sync(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 50 | |
Bernhard Reutner-Fischer | 7ae1553 | 2006-05-30 18:17:21 +0000 | [diff] [blame] | 51 | message = "KILL"; |
| 52 | syslog(pri, init_sending_format, message); |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 53 | printf(bb_shutdown_format, message); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 54 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 55 | kill(-1, SIGKILL); |
| 56 | sleep(1); |
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 | sync(); |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 59 | |
Eric Andersen | 20c9f1e | 2003-07-22 17:14:10 +0000 | [diff] [blame] | 60 | reboot(magic); |
| 61 | return 0; /* Shrug */ |
Eric Andersen | 0246222 | 2003-07-22 09:41:39 +0000 | [diff] [blame] | 62 | } |
| 63 | #endif |