blob: 785c3813018f9905d0ba5f3f39fa4705accaf833 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
Rob Landley64612912006-01-30 08:31:37 +00003 * Poweroff reboot and halt, oh my.
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
Rob Landley64612912006-01-30 08:31:37 +00005 * Copyright 2006 by Rob Landley <rob@landley.net>
Eric Andersenc4996011999-10-20 22:08:37 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersenc4996011999-10-20 22:08:37 +00008 */
Denys Vlasenkob1db09b2010-10-12 13:47:15 +02009//config:config HALT
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "halt (4 kb)"
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Stop all processes and halt the system.
Denys Vlasenko97e9a722016-11-14 04:55:04 +010014//config:
15//config:config POWEROFF
Denys Vlasenkob097a842018-12-28 03:20:17 +010016//config: bool "poweroff (4 kb)"
Denys Vlasenko97e9a722016-11-14 04:55:04 +010017//config: default y
18//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//config: Stop all processes and power off the system.
Denys Vlasenko97e9a722016-11-14 04:55:04 +010020//config:
21//config:config REBOOT
Denys Vlasenkob097a842018-12-28 03:20:17 +010022//config: bool "reboot (4 kb)"
Denys Vlasenko97e9a722016-11-14 04:55:04 +010023//config: default y
24//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020025//config: Stop all processes and reboot the system.
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020026//config:
Denys Vlasenko95121d92018-02-19 15:52:35 +010027//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 Vlasenkob1db09b2010-10-12 13:47:15 +020038//config:config FEATURE_CALL_TELINIT
39//config: bool "Call telinit on shutdown and reboot"
40//config: default y
Denys Vlasenko97e9a722016-11-14 04:55:04 +010041//config: depends on (HALT || POWEROFF || REBOOT) && !INIT
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020042//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020043//config: Call an external program (normally telinit) to facilitate
44//config: a switch to a proper runlevel.
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020045//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020046//config: This option is only available if you selected halt and friends,
47//config: but did not select init.
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020048//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 Vlasenko72089cf2017-07-21 09:50:55 +020054//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 Vlasenkob1db09b2010-10-12 13:47:15 +020057
Denys Vlasenko97e9a722016-11-14 04:55:04 +010058//applet:IF_HALT(APPLET(halt, BB_DIR_SBIN, BB_SUID_DROP))
Denys Vlasenko205d48e2017-01-29 14:57:33 +010059// APPLET_ODDNAME:name main location suid_type help
Denys Vlasenko97e9a722016-11-14 04:55:04 +010060//applet:IF_POWEROFF(APPLET_ODDNAME(poweroff, halt, BB_DIR_SBIN, BB_SUID_DROP, poweroff))
Denys Vlasenko205d48e2017-01-29 14:57:33 +010061//applet:IF_REBOOT( APPLET_ODDNAME(reboot, halt, BB_DIR_SBIN, BB_SUID_DROP, reboot))
Denys Vlasenko97e9a722016-11-14 04:55:04 +010062
63//kbuild:lib-$(CONFIG_HALT) += halt.o
64//kbuild:lib-$(CONFIG_POWEROFF) += halt.o
65//kbuild:lib-$(CONFIG_REBOOT) += halt.o
66
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020067//usage:#define halt_trivial_usage
68//usage: "[-d DELAY] [-n] [-f]" IF_FEATURE_WTMP(" [-w]")
69//usage:#define halt_full_usage "\n\n"
70//usage: "Halt the system\n"
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020071//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
79//usage: "[-d DELAY] [-n] [-f]"
80//usage:#define poweroff_full_usage "\n\n"
81//usage: "Halt and shut off power\n"
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020082//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
87//usage: "[-d DELAY] [-n] [-f]"
88//usage:#define reboot_full_usage "\n\n"
89//usage: "Reboot the system\n"
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020090//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 Vlasenkob6adbf12007-05-26 19:00:18 +000094#include "libbb.h"
Jeremie Koenig714674e2010-05-27 15:38:44 +020095#include "reboot.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000096
Denis Vlasenko680b86a2008-01-24 02:28:00 +000097#if ENABLE_FEATURE_WTMP
98#include <sys/utsname.h>
Denis Vlasenko680b86a2008-01-24 02:28:00 +000099
Denis Vlasenko9725daa2008-09-11 09:54:23 +0000100static void write_wtmp(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000101{
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +0200102 struct utmpx utmp;
Denis Vlasenko680b86a2008-01-24 02:28:00 +0000103 struct utsname uts;
Denys Vlasenko3a416112010-04-05 22:10:38 +0200104 /* "man utmp" says wtmp file should *not* be created automagically */
105 /*if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
Denis Vlasenko680b86a2008-01-24 02:28:00 +0000106 close(creat(bb_path_wtmp_file, 0664));
Denys Vlasenko3a416112010-04-05 22:10:38 +0200107 }*/
Denis Vlasenko680b86a2008-01-24 02:28:00 +0000108 memset(&utmp, 0, sizeof(utmp));
Bernhard Reutner-Fischer62d85032008-06-01 10:10:22 +0000109 utmp.ut_tv.tv_sec = time(NULL);
Denys Vlasenko3a416112010-04-05 22:10:38 +0200110 strcpy(utmp.ut_user, "shutdown"); /* it is wide enough */
Denis Vlasenko680b86a2008-01-24 02:28:00 +0000111 utmp.ut_type = RUN_LVL;
Denys Vlasenko3a416112010-04-05 22:10:38 +0200112 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-Fischer86a7f182015-04-02 23:03:46 +0200116 updwtmpx(bb_path_wtmp_file, &utmp);
Denis Vlasenko9725daa2008-09-11 09:54:23 +0000117}
118#else
119#define write_wtmp() ((void)0)
120#endif
121
Denys Vlasenko95121d92018-02-19 15:52:35 +0100122#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 */
137static 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)
147 sleep(1);
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)
154 sleep(1);
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 Vlasenko9725daa2008-09-11 09:54:23 +0000163
164int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
165int halt_main(int argc UNUSED_PARAM, char **argv)
166{
167 static const int magic[] = {
168 RB_HALT_SYSTEM,
169 RB_POWER_OFF,
Denis Vlasenkoea7c9b32008-09-25 10:39:10 +0000170 RB_AUTOBOOT
Denis Vlasenko9725daa2008-09-11 09:54:23 +0000171 };
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 Vlasenko97e9a722016-11-14 04:55:04 +0100178 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 Vlasenko9725daa2008-09-11 09:54:23 +0000187 for (which = 0; "hpr"[which] != applet_name[0]; which++)
188 continue;
189
190 /* Parse and handle arguments */
Denis Vlasenkoe12c9022009-04-12 15:59:35 +0000191 /* 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 Vlasenko237bedd2016-07-06 21:58:02 +0200195 flags = getopt32(argv, "d:+nfwi", &delay);
Denis Vlasenko9725daa2008-09-11 09:54:23 +0000196
197 sleep(delay);
198
199 write_wtmp();
200
Denis Vlasenko680b86a2008-01-24 02:28:00 +0000201 if (flags & 8) /* -w */
Bernhard Reutner-Fischeref9876a2008-07-21 11:30:51 +0000202 return EXIT_SUCCESS;
Bernhard Reutner-Fischerd93179f2008-09-01 15:24:52 +0000203
Denis Vlasenko680b86a2008-01-24 02:28:00 +0000204 if (!(flags & 2)) /* no -n */
205 sync();
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +0000206
Rob Landley64612912006-01-30 08:31:37 +0000207 /* Perform action. */
Denis Vlasenko1e28f612008-08-03 18:43:45 +0000208 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 Vlasenko52977a72016-03-31 00:42:57 +0200212 if (ENABLE_LINUXRC) {
Alexander Shishkin97af2ff2009-07-27 02:49:35 +0200213 /* talk to linuxrc */
214 /* bbox init/linuxrc assumed */
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000215 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 Landley64612912006-01-30 08:31:37 +0000220 }
Denis Vlasenkoe12c9022009-04-12 15:59:35 +0000221 if (rc) {
Alexander Shishkin97af2ff2009-07-27 02:49:35 +0200222 /* talk to init */
223 if (!ENABLE_FEATURE_CALL_TELINIT) {
224 /* bbox init assumed */
225 rc = kill(1, signals[which]);
Denys Vlasenko95121d92018-02-19 15:52:35 +0100226 if (init_was_not_there())
227 rc = kill(1, signals[which]);
Alexander Shishkin97af2ff2009-07-27 02:49:35 +0200228 } else {
229 /* SysV style init assumed */
230 /* runlevels:
231 * 0 == shutdown
232 * 6 == reboot */
Denys Vlasenko104e0142011-07-03 01:46:02 +0200233 execlp(CONFIG_TELINIT_PATH,
Alexander Shishkin97af2ff2009-07-27 02:49:35 +0200234 CONFIG_TELINIT_PATH,
235 which == 2 ? "6" : "0",
236 (char *)NULL
237 );
Denys Vlasenkodb43d3d2011-07-08 06:23:08 +0200238 bb_perror_msg_and_die("can't execute '%s'",
Denys Vlasenko104e0142011-07-03 01:46:02 +0200239 CONFIG_TELINIT_PATH);
Alexander Shishkin97af2ff2009-07-27 02:49:35 +0200240 }
Denis Vlasenkoe12c9022009-04-12 15:59:35 +0000241 }
242 } else {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000243 rc = reboot(magic[which]);
Denis Vlasenkoe12c9022009-04-12 15:59:35 +0000244 }
Rob Landley64612912006-01-30 08:31:37 +0000245
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000246 if (rc)
Denis Vlasenkofb1a23d2009-03-07 01:54:24 +0000247 bb_perror_nomsg_and_die();
Rob Landley64612912006-01-30 08:31:37 +0000248 return rc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000249}