1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 ProFUSION embedded systems
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
24 #include <sys/reboot.h>
25 #include <linux/reboot.h>
39 #define TIMEOUT_USEC (5 * USEC_PER_SEC)
40 #define FINALIZE_ATTEMPTS 50
42 static bool ignore_proc(pid_t pid) {
46 /* TODO: add more ignore rules here: device-mapper, etc */
51 static bool is_kernel_thread(pid_t pid)
58 snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long)pid);
61 return true; /* not really, but has the desired effect */
63 count = fread(&c, 1, 1, f);
68 static int killall(int sign) {
71 unsigned int n_processes = 0;
73 if ((dir = opendir("/proc")) == NULL)
76 while ((d = readdir(dir))) {
79 if (parse_pid(d->d_name, &pid) < 0)
82 if (is_kernel_thread(pid))
88 if (kill(pid, sign) == 0)
91 log_warning("Could not kill %d: %m", pid);
99 static int send_signal(int sign) {
100 sigset_t mask, oldmask;
105 assert_se(sigemptyset(&mask) == 0);
106 assert_se(sigaddset(&mask, SIGCHLD) == 0);
107 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
110 if (kill(-1, SIGSTOP) < 0)
111 log_warning("kill(-1, SIGSTOP) failed: %m");
113 n_processes = killall(sign);
115 if (kill(-1, SIGCONT) < 0)
116 log_warning("kill(-1, SIGCONT) failed: %m");
118 if (n_processes <= 0)
121 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
124 usec_t n = now(CLOCK_MONOTONIC);
127 pid_t pid = waitpid(-1, NULL, WNOHANG);
131 else if (pid < 0 && errno == ECHILD) {
136 if (--n_processes == 0)
143 timespec_store(&ts, until - n);
144 if ((k = sigtimedwait(&mask, NULL, &ts)) != SIGCHLD) {
146 log_warning("sigtimedwait() returned unexpected signal.");
147 if (k < 0 && errno != EAGAIN)
148 log_warning("sigtimedwait() failed: %m");
153 sigprocmask(SIG_SETMASK, &oldmask, NULL);
158 static int rescue_send_signal(int sign) {
159 sigset_t mask, oldmask;
165 sigaddset(&mask, SIGCHLD);
166 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
169 if (kill(-1, SIGSTOP) < 0)
170 log_warning("kill(-1, SIGSTOP) failed: %m");
174 log_warning("kill(-1, %d) failed: %m", sign);
176 if (kill(-1, SIGCONT) < 0)
177 log_warning("kill(-1, SIGCONT) failed: %m");
182 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
185 usec_t n = now(CLOCK_MONOTONIC);
188 pid_t pid = waitpid(-1, NULL, WNOHANG);
191 else if (pid < 0 && errno == ECHILD)
198 timespec_store(&ts, until - n);
199 if ((k = sigtimedwait(&mask, NULL, &ts)) != SIGCHLD) {
201 log_warning("sigtimedwait() returned unexpected signal.");
202 if (k < 0 && errno != EAGAIN)
203 log_warning("sigtimedwait() failed: %m");
208 sigprocmask(SIG_SETMASK, &oldmask, NULL);
213 int main(int argc, char *argv[]) {
216 bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true;
217 bool killed_everbody = false;
219 log_parse_environment();
220 log_set_target(LOG_TARGET_CONSOLE); /* syslog will die if not gone yet */
224 log_error("Not executed by init (pid 1).");
230 log_error("Invalid number of arguments.");
235 if (streq(argv[1], "reboot"))
237 else if (streq(argv[1], "poweroff"))
239 else if (streq(argv[1], "halt"))
240 cmd = RB_HALT_SYSTEM;
241 else if (streq(argv[1], "kexec"))
242 cmd = LINUX_REBOOT_CMD_KEXEC;
244 log_error("Unknown action '%s'.", argv[1]);
249 /* lock us into memory */
250 if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0)
251 log_warning("Cannot lock process memory: %m");
253 log_info("Sending SIGTERM to remaining processes...");
254 r = send_signal(SIGTERM);
256 log_warning("Failed to send SIGTERM to remaining processes: %s", strerror(r));
258 log_info("Sending SIGKILL to remaining processes...");
259 r = send_signal(SIGKILL);
261 log_warning("Failed to send SIGKILL to remaining processes: %s", strerror(r));
263 /* Unmount all mountpoints, swaps, and loopback devices */
264 for (retries = 0; retries < FINALIZE_ATTEMPTS; retries++) {
265 bool changed = false;
268 log_info("Unmounting file systems.");
269 r = umount_all(&changed);
273 log_info("Not all file systems unmounted, %d left.", r);
275 log_error("Failed to unmount file systems: %s", strerror(-r));
279 log_info("Disabling swaps.");
280 r = swapoff_all(&changed);
282 need_swapoff = false;
284 log_info("Not all swaps are turned off, %d left.", r);
286 log_error("Failed to turn off swaps: %s", strerror(-r));
289 if (need_loop_detach) {
290 log_info("Detaching loop devices.");
291 r = loopback_detach_all(&changed);
293 need_loop_detach = false;
295 log_info("Not all loop devices detached, %d left.", r);
297 log_error("Failed to detach loop devices: %s", strerror(-r));
300 if (need_dm_detach) {
301 log_info("Detaching DM devices.");
302 r = dm_detach_all(&changed);
304 need_dm_detach = false;
306 log_warning("Not all DM devices detached, %d left.", r);
308 log_error("Failed to detach DM devices: %s", strerror(-r));
311 if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach)
315 /* If in this iteration we didn't manage to
316 * unmount/deactivate anything, we either kill more
317 * processes, or simply give up */
320 if (killed_everbody) {
321 /* Hmm, we already killed everybody,
322 * let's just give up */
323 log_error("Cannot finalize remaining file systems and devices, giving up.");
327 log_warning("Cannot finalize remaining file systems and devices, trying to kill remaining processes.");
328 rescue_send_signal(SIGTERM);
329 rescue_send_signal(SIGKILL);
330 killed_everbody = true;
333 log_debug("Couldn't finalize remaining file systems and devices after %u retries, trying again.", retries+1);
336 if (retries >= FINALIZE_ATTEMPTS)
337 log_error("Too many interations, giving up.");
341 if (cmd == LINUX_REBOOT_CMD_KEXEC) {
342 /* We cheat and exec kexec to avoid doing all its work */
346 log_error("Could not fork: %m. Falling back to normal reboot.");
348 wait_for_terminate_and_warn("kexec", pid);
349 log_warning("kexec failed. Falling back to normal reboot.");
352 const char *args[5] = { "/sbin/kexec", "-e", "-f", "-x", NULL };
353 execv(args[0], (char * const *) args);
361 log_error("Failed to invoke reboot(): %m");
366 log_error("Critical error while doing system shutdown: %s", strerror(-r));