1 /* vi: set sw=4 ts=4: */
3 * Poweroff reboot and halt, oh my.
5 * Copyright 2006 by Rob Landley <rob@landley.net>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 //config: bool "halt (3.7 kb)"
13 //config: Stop all processes and halt the system.
15 //config:config POWEROFF
16 //config: bool "poweroff (3.7 kb)"
19 //config: Stop all processes and power off the system.
21 //config:config REBOOT
22 //config: bool "reboot (3.7 kb)"
25 //config: Stop all processes and reboot the system.
27 //config:config FEATURE_WAIT_FOR_INIT
28 //config: bool "Before signaling init, make sure it is ready for it"
30 //config: depends on HALT || POWEROFF || REBOOT
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.
38 //config:config FEATURE_CALL_TELINIT
39 //config: bool "Call telinit on shutdown and reboot"
41 //config: depends on (HALT || POWEROFF || REBOOT) && !INIT
43 //config: Call an external program (normally telinit) to facilitate
44 //config: a switch to a proper runlevel.
46 //config: This option is only available if you selected halt and friends,
47 //config: but did not select init.
49 //config:config TELINIT_PATH
50 //config: string "Path to telinit executable"
51 //config: default "/sbin/telinit"
52 //config: depends on FEATURE_CALL_TELINIT
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.
58 //applet:IF_HALT(APPLET(halt, BB_DIR_SBIN, BB_SUID_DROP))
59 // APPLET_ODDNAME:name main location suid_type help
60 //applet:IF_POWEROFF(APPLET_ODDNAME(poweroff, halt, BB_DIR_SBIN, BB_SUID_DROP, poweroff))
61 //applet:IF_REBOOT( APPLET_ODDNAME(reboot, halt, BB_DIR_SBIN, BB_SUID_DROP, reboot))
63 //kbuild:lib-$(CONFIG_HALT) += halt.o
64 //kbuild:lib-$(CONFIG_POWEROFF) += halt.o
65 //kbuild:lib-$(CONFIG_REBOOT) += halt.o
67 //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"
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"
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"
82 //usage: "\n -d SEC Delay interval"
83 //usage: "\n -n Do not sync"
84 //usage: "\n -f Force (don't go through init)"
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"
90 //usage: "\n -d SEC Delay interval"
91 //usage: "\n -n Do not sync"
92 //usage: "\n -f Force (don't go through init)"
97 #if ENABLE_FEATURE_WTMP
98 #include <sys/utsname.h>
100 static void write_wtmp(void)
104 /* "man utmp" says wtmp file should *not* be created automagically */
105 /*if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
106 close(creat(bb_path_wtmp_file, 0664));
108 memset(&utmp, 0, sizeof(utmp));
109 utmp.ut_tv.tv_sec = time(NULL);
110 strcpy(utmp.ut_user, "shutdown"); /* it is wide enough */
111 utmp.ut_type = RUN_LVL;
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, "~~"); */
115 safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host));
116 updwtmpx(bb_path_wtmp_file, &utmp);
119 #define write_wtmp() ((void)0)
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);
135 * We want to make sure init exists and listens to signals.
137 static int init_was_not_there(void)
139 enum { initial = 5 }; /* 5 seconds should be plenty for timeout */
140 int cnt = initial - 1;
142 /* Just existence of PID 1 does not mean it installed
143 * the handlers already.
146 while (kill(1, 0) != 0 && --cnt >= 0)
149 /* ... so let's wait for some evidence a usual startup event,
150 * mounting of /proc, happened. By that time init should be ready
153 while (access("/proc/meminfo", F_OK) != 0 && --cnt >= 0)
156 /* Does it look like init wasn't there? */
157 return (cnt != initial - 1);
160 /* Assume it's always there */
161 # define init_was_not_there() 0
164 int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
165 int halt_main(int argc UNUSED_PARAM, char **argv)
167 static const int magic[] = {
172 static const smallint signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
175 int which, flags, rc;
177 /* Figure out which applet we're running */
178 if (ENABLE_HALT && !ENABLE_POWEROFF && !ENABLE_REBOOT)
181 if (!ENABLE_HALT && ENABLE_POWEROFF && !ENABLE_REBOOT)
184 if (!ENABLE_HALT && !ENABLE_POWEROFF && ENABLE_REBOOT)
187 for (which = 0; "hpr"[which] != applet_name[0]; which++)
190 /* Parse and handle arguments */
191 /* We support -w even if !ENABLE_FEATURE_WTMP,
192 * in order to not break scripts.
193 * -i (shut down network interfaces) is ignored.
195 flags = getopt32(argv, "d:+nfwi", &delay);
201 if (flags & 8) /* -w */
204 if (!(flags & 2)) /* no -n */
207 /* Perform action. */
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...
212 if (ENABLE_LINUXRC) {
213 /* talk to linuxrc */
214 /* bbox init/linuxrc assumed */
215 pid_t *pidlist = find_pid_by_name("linuxrc");
217 rc = kill(pidlist[0], signals[which]);
218 if (ENABLE_FEATURE_CLEAN_UP)
223 if (!ENABLE_FEATURE_CALL_TELINIT) {
224 /* bbox init assumed */
225 rc = kill(1, signals[which]);
226 if (init_was_not_there())
227 rc = kill(1, signals[which]);
229 /* SysV style init assumed */
233 execlp(CONFIG_TELINIT_PATH,
235 which == 2 ? "6" : "0",
238 bb_perror_msg_and_die("can't execute '%s'",
239 CONFIG_TELINIT_PATH);
243 rc = reboot(magic[which]);
247 bb_perror_nomsg_and_die();