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