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 | /* |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 3 | * Poweroff reboot and halt, oh my. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 5 | * Copyright 2006 by Rob Landley <rob@landley.net> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 9 | //config:config HALT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "halt (4 kb)" |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: Stop all processes and halt the system. |
Denys Vlasenko | 97e9a72 | 2016-11-14 04:55:04 +0100 | [diff] [blame] | 14 | //config: |
| 15 | //config:config POWEROFF |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 16 | //config: bool "poweroff (4 kb)" |
Denys Vlasenko | 97e9a72 | 2016-11-14 04:55:04 +0100 | [diff] [blame] | 17 | //config: default y |
| 18 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 19 | //config: Stop all processes and power off the system. |
Denys Vlasenko | 97e9a72 | 2016-11-14 04:55:04 +0100 | [diff] [blame] | 20 | //config: |
| 21 | //config:config REBOOT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 22 | //config: bool "reboot (4 kb)" |
Denys Vlasenko | 97e9a72 | 2016-11-14 04:55:04 +0100 | [diff] [blame] | 23 | //config: default y |
| 24 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 25 | //config: Stop all processes and reboot the system. |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 26 | //config: |
Denys Vlasenko | 95121d9 | 2018-02-19 15:52:35 +0100 | [diff] [blame] | 27 | //config:config FEATURE_WAIT_FOR_INIT |
| 28 | //config: bool "Before signaling init, make sure it is ready for it" |
| 29 | //config: default y |
| 30 | //config: depends on HALT || POWEROFF || REBOOT |
| 31 | //config: help |
| 32 | //config: In rare cases, poweroff may be commanded by firmware to OS |
| 33 | //config: even before init process exists. On Linux, this spawns |
| 34 | //config: "/sbin/poweroff" very early. This option adds code |
| 35 | //config: which checks that init is ready to receive poweroff |
| 36 | //config: commands. Code size increase of ~80 bytes. |
| 37 | //config: |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 38 | //config:config FEATURE_CALL_TELINIT |
| 39 | //config: bool "Call telinit on shutdown and reboot" |
| 40 | //config: default y |
Denys Vlasenko | 97e9a72 | 2016-11-14 04:55:04 +0100 | [diff] [blame] | 41 | //config: depends on (HALT || POWEROFF || REBOOT) && !INIT |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 42 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 43 | //config: Call an external program (normally telinit) to facilitate |
| 44 | //config: a switch to a proper runlevel. |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 45 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 46 | //config: This option is only available if you selected halt and friends, |
| 47 | //config: but did not select init. |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 48 | //config: |
| 49 | //config:config TELINIT_PATH |
| 50 | //config: string "Path to telinit executable" |
| 51 | //config: default "/sbin/telinit" |
| 52 | //config: depends on FEATURE_CALL_TELINIT |
| 53 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 54 | //config: When busybox halt and friends have to call external telinit |
| 55 | //config: to facilitate proper shutdown, this path is to be used when |
| 56 | //config: locating telinit executable. |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 57 | |
Denys Vlasenko | 97e9a72 | 2016-11-14 04:55:04 +0100 | [diff] [blame] | 58 | //applet:IF_HALT(APPLET(halt, BB_DIR_SBIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 59 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | 97e9a72 | 2016-11-14 04:55:04 +0100 | [diff] [blame] | 60 | //applet:IF_POWEROFF(APPLET_ODDNAME(poweroff, halt, BB_DIR_SBIN, BB_SUID_DROP, poweroff)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 61 | //applet:IF_REBOOT( APPLET_ODDNAME(reboot, halt, BB_DIR_SBIN, BB_SUID_DROP, reboot)) |
Denys Vlasenko | 97e9a72 | 2016-11-14 04:55:04 +0100 | [diff] [blame] | 62 | |
| 63 | //kbuild:lib-$(CONFIG_HALT) += halt.o |
| 64 | //kbuild:lib-$(CONFIG_POWEROFF) += halt.o |
| 65 | //kbuild:lib-$(CONFIG_REBOOT) += halt.o |
| 66 | |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 67 | //usage:#define halt_trivial_usage |
Denys Vlasenko | a2f18d9 | 2020-12-18 04:12:51 +0100 | [diff] [blame] | 68 | //usage: "[-d DELAY] [-nf"IF_FEATURE_WTMP("w")"]" |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 69 | //usage:#define halt_full_usage "\n\n" |
| 70 | //usage: "Halt the system\n" |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 71 | //usage: "\n -d SEC Delay interval" |
| 72 | //usage: "\n -n Do not sync" |
| 73 | //usage: "\n -f Force (don't go through init)" |
| 74 | //usage: IF_FEATURE_WTMP( |
| 75 | //usage: "\n -w Only write a wtmp record" |
| 76 | //usage: ) |
| 77 | //usage: |
| 78 | //usage:#define poweroff_trivial_usage |
Denys Vlasenko | a2f18d9 | 2020-12-18 04:12:51 +0100 | [diff] [blame] | 79 | //usage: "[-d DELAY] [-nf]" |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 80 | //usage:#define poweroff_full_usage "\n\n" |
| 81 | //usage: "Halt and shut off power\n" |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 82 | //usage: "\n -d SEC Delay interval" |
| 83 | //usage: "\n -n Do not sync" |
| 84 | //usage: "\n -f Force (don't go through init)" |
| 85 | //usage: |
| 86 | //usage:#define reboot_trivial_usage |
Denys Vlasenko | a2f18d9 | 2020-12-18 04:12:51 +0100 | [diff] [blame] | 87 | //usage: "[-d DELAY] [-nf]" |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 88 | //usage:#define reboot_full_usage "\n\n" |
| 89 | //usage: "Reboot the system\n" |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 90 | //usage: "\n -d SEC Delay interval" |
| 91 | //usage: "\n -n Do not sync" |
| 92 | //usage: "\n -f Force (don't go through init)" |
| 93 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 94 | #include "libbb.h" |
Jeremie Koenig | 714674e | 2010-05-27 15:38:44 +0200 | [diff] [blame] | 95 | #include "reboot.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | 680b86a | 2008-01-24 02:28:00 +0000 | [diff] [blame] | 97 | #if ENABLE_FEATURE_WTMP |
| 98 | #include <sys/utsname.h> |
Denis Vlasenko | 680b86a | 2008-01-24 02:28:00 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | 9725daa | 2008-09-11 09:54:23 +0000 | [diff] [blame] | 100 | static void write_wtmp(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 101 | { |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 102 | struct utmpx utmp; |
Denis Vlasenko | 680b86a | 2008-01-24 02:28:00 +0000 | [diff] [blame] | 103 | struct utsname uts; |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 104 | /* "man utmp" says wtmp file should *not* be created automagically */ |
| 105 | /*if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) { |
Denis Vlasenko | 680b86a | 2008-01-24 02:28:00 +0000 | [diff] [blame] | 106 | close(creat(bb_path_wtmp_file, 0664)); |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 107 | }*/ |
Denis Vlasenko | 680b86a | 2008-01-24 02:28:00 +0000 | [diff] [blame] | 108 | memset(&utmp, 0, sizeof(utmp)); |
Bernhard Reutner-Fischer | 62d8503 | 2008-06-01 10:10:22 +0000 | [diff] [blame] | 109 | utmp.ut_tv.tv_sec = time(NULL); |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 110 | strcpy(utmp.ut_user, "shutdown"); /* it is wide enough */ |
Denis Vlasenko | 680b86a | 2008-01-24 02:28:00 +0000 | [diff] [blame] | 111 | utmp.ut_type = RUN_LVL; |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 112 | utmp.ut_id[0] = '~'; utmp.ut_id[1] = '~'; /* = strcpy(utmp.ut_id, "~~"); */ |
| 113 | utmp.ut_line[0] = '~'; utmp.ut_line[1] = '~'; /* = strcpy(utmp.ut_line, "~~"); */ |
| 114 | uname(&uts); |
| 115 | safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host)); |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 116 | updwtmpx(bb_path_wtmp_file, &utmp); |
Denis Vlasenko | 9725daa | 2008-09-11 09:54:23 +0000 | [diff] [blame] | 117 | } |
| 118 | #else |
| 119 | #define write_wtmp() ((void)0) |
| 120 | #endif |
| 121 | |
Denys Vlasenko | 95121d9 | 2018-02-19 15:52:35 +0100 | [diff] [blame] | 122 | #if ENABLE_FEATURE_WAIT_FOR_INIT |
| 123 | /* In Linux, "poweroff" may be spawned even before init. |
| 124 | * For example, with ACPI: |
| 125 | * linux/drivers/acpi/bus.c: |
| 126 | * static void sb_notify_work(struct work_struct *dummy) |
| 127 | * orderly_poweroff(true); |
| 128 | * linux/kernel/reboot.c: |
| 129 | * static int run_cmd(const char *cmd) |
| 130 | * ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
| 131 | * poweroff_cmd[] = "/sbin/poweroff"; |
| 132 | * static int __orderly_poweroff(bool force) |
| 133 | * ret = run_cmd(poweroff_cmd); |
| 134 | * |
| 135 | * We want to make sure init exists and listens to signals. |
| 136 | */ |
| 137 | static int init_was_not_there(void) |
| 138 | { |
| 139 | enum { initial = 5 }; /* 5 seconds should be plenty for timeout */ |
| 140 | int cnt = initial - 1; |
| 141 | |
| 142 | /* Just existence of PID 1 does not mean it installed |
| 143 | * the handlers already. |
| 144 | */ |
| 145 | #if 0 |
| 146 | while (kill(1, 0) != 0 && --cnt >= 0) |
Denys Vlasenko | ec16c03 | 2020-11-29 11:37:34 +0100 | [diff] [blame] | 147 | sleep1(); |
Denys Vlasenko | 95121d9 | 2018-02-19 15:52:35 +0100 | [diff] [blame] | 148 | #endif |
| 149 | /* ... so let's wait for some evidence a usual startup event, |
| 150 | * mounting of /proc, happened. By that time init should be ready |
| 151 | * for signals. |
| 152 | */ |
| 153 | while (access("/proc/meminfo", F_OK) != 0 && --cnt >= 0) |
Denys Vlasenko | ec16c03 | 2020-11-29 11:37:34 +0100 | [diff] [blame] | 154 | sleep1(); |
Denys Vlasenko | 95121d9 | 2018-02-19 15:52:35 +0100 | [diff] [blame] | 155 | |
| 156 | /* Does it look like init wasn't there? */ |
| 157 | return (cnt != initial - 1); |
| 158 | } |
| 159 | #else |
| 160 | /* Assume it's always there */ |
| 161 | # define init_was_not_there() 0 |
| 162 | #endif |
Denis Vlasenko | 9725daa | 2008-09-11 09:54:23 +0000 | [diff] [blame] | 163 | |
| 164 | int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 165 | int halt_main(int argc UNUSED_PARAM, char **argv) |
| 166 | { |
| 167 | static const int magic[] = { |
| 168 | RB_HALT_SYSTEM, |
| 169 | RB_POWER_OFF, |
Denis Vlasenko | ea7c9b3 | 2008-09-25 10:39:10 +0000 | [diff] [blame] | 170 | RB_AUTOBOOT |
Denis Vlasenko | 9725daa | 2008-09-11 09:54:23 +0000 | [diff] [blame] | 171 | }; |
| 172 | static const smallint signals[] = { SIGUSR1, SIGUSR2, SIGTERM }; |
| 173 | |
| 174 | int delay = 0; |
| 175 | int which, flags, rc; |
| 176 | |
| 177 | /* Figure out which applet we're running */ |
Denys Vlasenko | 97e9a72 | 2016-11-14 04:55:04 +0100 | [diff] [blame] | 178 | if (ENABLE_HALT && !ENABLE_POWEROFF && !ENABLE_REBOOT) |
| 179 | which = 0; |
| 180 | else |
| 181 | if (!ENABLE_HALT && ENABLE_POWEROFF && !ENABLE_REBOOT) |
| 182 | which = 1; |
| 183 | else |
| 184 | if (!ENABLE_HALT && !ENABLE_POWEROFF && ENABLE_REBOOT) |
| 185 | which = 2; |
| 186 | else |
Denis Vlasenko | 9725daa | 2008-09-11 09:54:23 +0000 | [diff] [blame] | 187 | for (which = 0; "hpr"[which] != applet_name[0]; which++) |
| 188 | continue; |
| 189 | |
| 190 | /* Parse and handle arguments */ |
Denis Vlasenko | e12c902 | 2009-04-12 15:59:35 +0000 | [diff] [blame] | 191 | /* We support -w even if !ENABLE_FEATURE_WTMP, |
| 192 | * in order to not break scripts. |
| 193 | * -i (shut down network interfaces) is ignored. |
| 194 | */ |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 195 | flags = getopt32(argv, "d:+nfwi", &delay); |
Denis Vlasenko | 9725daa | 2008-09-11 09:54:23 +0000 | [diff] [blame] | 196 | |
| 197 | sleep(delay); |
| 198 | |
| 199 | write_wtmp(); |
| 200 | |
Denis Vlasenko | 680b86a | 2008-01-24 02:28:00 +0000 | [diff] [blame] | 201 | if (flags & 8) /* -w */ |
Bernhard Reutner-Fischer | ef9876a | 2008-07-21 11:30:51 +0000 | [diff] [blame] | 202 | return EXIT_SUCCESS; |
Bernhard Reutner-Fischer | d93179f | 2008-09-01 15:24:52 +0000 | [diff] [blame] | 203 | |
Denis Vlasenko | 680b86a | 2008-01-24 02:28:00 +0000 | [diff] [blame] | 204 | if (!(flags & 2)) /* no -n */ |
| 205 | sync(); |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 206 | |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 207 | /* Perform action. */ |
Denis Vlasenko | 1e28f61 | 2008-08-03 18:43:45 +0000 | [diff] [blame] | 208 | rc = 1; |
| 209 | if (!(flags & 4)) { /* no -f */ |
| 210 | //TODO: I tend to think that signalling linuxrc is wrong |
| 211 | // pity original author didn't comment on it... |
Denys Vlasenko | 52977a7 | 2016-03-31 00:42:57 +0200 | [diff] [blame] | 212 | if (ENABLE_LINUXRC) { |
Alexander Shishkin | 97af2ff | 2009-07-27 02:49:35 +0200 | [diff] [blame] | 213 | /* talk to linuxrc */ |
| 214 | /* bbox init/linuxrc assumed */ |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 215 | pid_t *pidlist = find_pid_by_name("linuxrc"); |
| 216 | if (pidlist[0] > 0) |
| 217 | rc = kill(pidlist[0], signals[which]); |
| 218 | if (ENABLE_FEATURE_CLEAN_UP) |
| 219 | free(pidlist); |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 220 | } |
Denis Vlasenko | e12c902 | 2009-04-12 15:59:35 +0000 | [diff] [blame] | 221 | if (rc) { |
Alexander Shishkin | 97af2ff | 2009-07-27 02:49:35 +0200 | [diff] [blame] | 222 | /* talk to init */ |
| 223 | if (!ENABLE_FEATURE_CALL_TELINIT) { |
| 224 | /* bbox init assumed */ |
| 225 | rc = kill(1, signals[which]); |
Denys Vlasenko | 95121d9 | 2018-02-19 15:52:35 +0100 | [diff] [blame] | 226 | if (init_was_not_there()) |
| 227 | rc = kill(1, signals[which]); |
Alexander Shishkin | 97af2ff | 2009-07-27 02:49:35 +0200 | [diff] [blame] | 228 | } else { |
| 229 | /* SysV style init assumed */ |
| 230 | /* runlevels: |
| 231 | * 0 == shutdown |
| 232 | * 6 == reboot */ |
Denys Vlasenko | 104e014 | 2011-07-03 01:46:02 +0200 | [diff] [blame] | 233 | execlp(CONFIG_TELINIT_PATH, |
Alexander Shishkin | 97af2ff | 2009-07-27 02:49:35 +0200 | [diff] [blame] | 234 | CONFIG_TELINIT_PATH, |
| 235 | which == 2 ? "6" : "0", |
| 236 | (char *)NULL |
| 237 | ); |
Denys Vlasenko | db43d3d | 2011-07-08 06:23:08 +0200 | [diff] [blame] | 238 | bb_perror_msg_and_die("can't execute '%s'", |
Denys Vlasenko | 104e014 | 2011-07-03 01:46:02 +0200 | [diff] [blame] | 239 | CONFIG_TELINIT_PATH); |
Alexander Shishkin | 97af2ff | 2009-07-27 02:49:35 +0200 | [diff] [blame] | 240 | } |
Denis Vlasenko | e12c902 | 2009-04-12 15:59:35 +0000 | [diff] [blame] | 241 | } |
| 242 | } else { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 243 | rc = reboot(magic[which]); |
Denis Vlasenko | e12c902 | 2009-04-12 15:59:35 +0000 | [diff] [blame] | 244 | } |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 245 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 246 | if (rc) |
Denis Vlasenko | fb1a23d | 2009-03-07 01:54:24 +0000 | [diff] [blame] | 247 | bb_perror_nomsg_and_die(); |
Rob Landley | 6461291 | 2006-01-30 08:31:37 +0000 | [diff] [blame] | 248 | return rc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 249 | } |