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
41 #define FINALIZE_CRITICAL_ATTEMPTS 10
43 static bool ignore_proc(pid_t pid) {
47 /* TODO: add more ignore rules here: device-mapper, etc */
52 static bool is_kernel_thread(pid_t pid)
59 snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long)pid);
62 return true; /* not really, but has the desired effect */
64 count = fread(&c, 1, 1, f);
69 static int killall(int sign) {
72 unsigned int processes = 0;
74 if ((dir = opendir("/proc")) == NULL)
77 while ((d = readdir(dir))) {
80 if (parse_pid(d->d_name, &pid) < 0)
83 if (is_kernel_thread(pid))
89 if (kill(pid, sign) == 0)
92 log_warning("Could not kill %d: %m", pid);
100 static int send_signal(int sign) {
101 sigset_t mask, oldmask;
106 assert_se(sigemptyset(&mask) == 0);
107 assert_se(sigaddset(&mask, SIGCHLD) == 0);
108 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
111 if (kill(-1, SIGSTOP) < 0)
112 log_warning("Failed kill(-1, SIGSTOP): %m");
114 processes = killall(sign);
116 if (kill(-1, SIGCONT) < 0)
117 log_warning("Failed kill(-1, SIGCONT): %m");
122 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
124 usec_t n = now(CLOCK_MONOTONIC);
126 pid_t pid = waitpid(-1, NULL, WNOHANG);
129 else if (pid < 0 && errno == ECHILD) {
134 if (--processes == 0)
141 timespec_store(&ts, until - n);
142 if (sigtimedwait(&mask, NULL, &ts) != SIGCHLD)
143 log_warning("Failed: sigtimedwait did not return SIGCHLD: %m");
147 sigprocmask(SIG_SETMASK, &oldmask, NULL);
152 static int rescue_send_signal(int sign) {
153 sigset_t mask, oldmask;
159 sigaddset(&mask, SIGCHLD);
160 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
163 if (kill(-1, SIGSTOP) < 0)
164 log_warning("Failed kill(-1, SIGSTOP): %m");
168 log_warning("Failed kill(-1, %d): %m", sign);
170 if (kill(-1, SIGCONT) < 0)
171 log_warning("Failed kill(-1, SIGCONT): %m");
176 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
178 usec_t n = now(CLOCK_MONOTONIC);
180 pid_t pid = waitpid(-1, NULL, WNOHANG);
183 else if (pid < 0 && errno == ECHILD)
190 timespec_store(&ts, until - n);
191 if (sigtimedwait(&mask, NULL, &ts) != SIGCHLD)
192 log_warning("Failed: sigtimedwait did not return SIGCHLD: %m");
196 sigprocmask(SIG_SETMASK, &oldmask, NULL);
202 int main(int argc, char *argv[]) {
204 bool need_umount = true, need_swapoff = true, need_loop_detach = true;
206 log_parse_environment();
207 log_set_target(LOG_TARGET_KMSG); /* syslog will die if not gone yet */
211 log_error("Not executed by init (pid-1).");
217 log_error("Invalid number of arguments.");
222 if (streq(argv[1], "reboot"))
224 else if (streq(argv[1], "poweroff"))
226 else if (streq(argv[1], "halt"))
227 cmd = RB_HALT_SYSTEM;
228 else if (streq(argv[1], "kexec"))
229 cmd = LINUX_REBOOT_CMD_KEXEC;
231 log_error("Unknown action '%s'.", argv[1]);
236 /* lock us into memory */
237 if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0)
238 log_warning("Cannot lock process memory: %m");
240 log_info("Sending SIGTERM to processes");
241 r = send_signal(SIGTERM);
243 log_warning("Cannot send SIGTERM to all process: %s", strerror(r));
245 log_info("Sending SIGKILL to processes");
246 r = send_signal(SIGKILL);
248 log_warning("Cannot send SIGKILL to all process: %s", strerror(r));
251 /* preventing that we won't block umounts */
253 log_warning("Cannot chdir(\"/\"): %m. Unmounts likely to fail.");
255 /* umount all mountpoints, swaps, and loopback devices */
256 retries = FINALIZE_ATTEMPTS;
257 while (need_umount || need_swapoff || need_loop_detach) {
259 log_info("Unmounting filesystems.");
264 log_warning("Not all filesystems unmounted, %d left.", r);
266 log_error("Error unmounting filesystems: %s", strerror(-r));
270 log_info("Disabling swaps.");
273 need_swapoff = false;
275 log_warning("Not all swaps are off, %d left.", r);
277 log_error("Error turning off swaps: %s", strerror(-r));
280 if (need_loop_detach) {
281 log_info("Detaching loop devices.");
282 r = loopback_detach_all();
284 need_loop_detach = false;
286 log_warning("Not all loop devices detached, %d left.", r);
288 log_error("Error detaching loop devices: %s", strerror(-r));
292 if (need_umount || need_swapoff || need_loop_detach) {
295 if (retries <= FINALIZE_CRITICAL_ATTEMPTS) {
296 log_warning("Approaching critical level to finalize filesystem and devices, try to kill all processes.");
297 rescue_send_signal(SIGTERM);
298 rescue_send_signal(SIGKILL);
302 log_info("Action still required, %d tries left", retries);
304 log_error("Tried enough but still action required need_umount=%d, need_swapoff=%d, need_loop_detach=%d", need_umount, need_swapoff, need_loop_detach);
313 if (cmd == LINUX_REBOOT_CMD_KEXEC) {
314 /* we cheat and exec kexec to avoid doing all its work */
317 log_error("Could not fork: %m. Falling back to reboot.");
319 } else if (pid > 0) {
320 waitpid(pid, NULL, 0);
321 log_warning("Failed %s -e -x -f. Falling back to reboot", KEXEC_BINARY_PATH);
324 const char *args[5] = {KEXEC_BINARY_PATH, "-e", "-f", "-x", NULL};
325 execv(args[0], (char * const *) args);
337 log_error("Critical error while doing system shutdown: %s", strerror(r));