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 _noreturn_ static void freeze(void) {
48 static bool ignore_proc(pid_t pid) {
52 /* TODO: add more ignore rules here: device-mapper, etc */
57 static bool is_kernel_thread(pid_t pid)
64 snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long)pid);
67 return true; /* not really, but has the desired effect */
69 count = fread(&c, 1, 1, f);
74 static int killall(int sign) {
77 unsigned int processes = 0;
79 if ((dir = opendir("/proc")) == NULL)
82 while ((d = readdir(dir))) {
85 if (parse_pid(d->d_name, &pid) < 0)
88 if (is_kernel_thread(pid))
94 if (kill(pid, sign) == 0)
97 log_warning("Could not kill %d: %m", pid);
105 static int send_signal(int sign) {
106 sigset_t mask, oldmask;
111 assert_se(sigemptyset(&mask) == 0);
112 assert_se(sigaddset(&mask, SIGCHLD) == 0);
113 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
116 if (kill(-1, SIGSTOP) < 0)
117 log_warning("Failed kill(-1, SIGSTOP): %m");
119 processes = killall(sign);
121 if (kill(-1, SIGCONT) < 0)
122 log_warning("Failed kill(-1, SIGCONT): %m");
127 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
129 usec_t n = now(CLOCK_MONOTONIC);
131 pid_t pid = waitpid(-1, NULL, WNOHANG);
134 else if (pid < 0 && errno == ECHILD) {
139 if (--processes == 0)
146 timespec_store(&ts, until - n);
147 if (sigtimedwait(&mask, NULL, &ts) != SIGCHLD)
148 log_warning("Failed: sigtimedwait did not return SIGCHLD: %m");
152 sigprocmask(SIG_SETMASK, &oldmask, NULL);
157 static int rescue_send_signal(int sign) {
158 sigset_t mask, oldmask;
164 sigaddset(&mask, SIGCHLD);
165 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
168 if (kill(-1, SIGSTOP) < 0)
169 log_warning("Failed kill(-1, SIGSTOP): %m");
173 log_warning("Failed kill(-1, %d): %m", sign);
175 if (kill(-1, SIGCONT) < 0)
176 log_warning("Failed kill(-1, SIGCONT): %m");
181 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
183 usec_t n = now(CLOCK_MONOTONIC);
185 pid_t pid = waitpid(-1, NULL, WNOHANG);
188 else if (pid < 0 && errno == ECHILD)
195 timespec_store(&ts, until - n);
196 if (sigtimedwait(&mask, NULL, &ts) != SIGCHLD)
197 log_warning("Failed: sigtimedwait did not return SIGCHLD: %m");
201 sigprocmask(SIG_SETMASK, &oldmask, NULL);
207 int main(int argc, char *argv[]) {
209 bool need_umount = true, need_swapoff = true, need_loop_detach = true;
211 log_parse_environment();
212 log_set_target(LOG_TARGET_KMSG); /* syslog will die if not gone yet */
216 log_error("Not executed by init (pid-1).");
222 log_error("Invalid number of arguments.");
227 if (streq(argv[1], "reboot"))
229 else if (streq(argv[1], "poweroff"))
231 else if (streq(argv[1], "halt"))
232 cmd = RB_HALT_SYSTEM;
233 else if (streq(argv[1], "kexec"))
234 cmd = LINUX_REBOOT_CMD_KEXEC;
236 log_error("Unknown action '%s'.", argv[1]);
241 /* lock us into memory */
242 if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0)
243 log_warning("Cannot lock process memory: %m");
245 log_info("Sending SIGTERM to processes");
246 r = send_signal(SIGTERM);
248 log_warning("Cannot send SIGTERM to all process: %s", strerror(r));
250 log_info("Sending SIGKILL to processes");
251 r = send_signal(SIGKILL);
253 log_warning("Cannot send SIGKILL to all process: %s", strerror(r));
256 /* preventing that we won't block umounts */
258 log_warning("Cannot chdir(\"/\"): %m. Unmounts likely to fail.");
260 /* umount all mountpoints, swaps, and loopback devices */
261 retries = FINALIZE_ATTEMPTS;
262 while (need_umount || need_swapoff || need_loop_detach) {
264 log_info("Unmounting filesystems.");
269 log_warning("Not all filesystems unmounted, %d left.", r);
271 log_error("Error unmounting filesystems: %s", strerror(-r));
275 log_info("Disabling swaps.");
278 need_swapoff = false;
280 log_warning("Not all swaps are off, %d left.", r);
282 log_error("Error turning off swaps: %s", strerror(-r));
285 if (need_loop_detach) {
286 log_info("Detaching loop devices.");
287 r = loopback_detach_all();
289 need_loop_detach = false;
291 log_warning("Not all loop devices detached, %d left.", r);
293 log_error("Error detaching loop devices: %s", strerror(-r));
297 if (need_umount || need_swapoff || need_loop_detach) {
300 if (retries <= FINALIZE_CRITICAL_ATTEMPTS) {
301 log_warning("Approaching critical level to finalize filesystem and devices, try to kill all processes.");
302 rescue_send_signal(SIGTERM);
303 rescue_send_signal(SIGKILL);
307 log_info("Action still required, %d tries left", retries);
309 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);
318 if (cmd == LINUX_REBOOT_CMD_KEXEC) {
319 /* we cheat and exec kexec to avoid doing all its work */
322 log_error("Could not fork: %m. Falling back to reboot.");
324 } else if (pid > 0) {
325 waitpid(pid, NULL, 0);
326 log_warning("Failed %s -e -x -f. Falling back to reboot", KEXEC_BINARY_PATH);
329 const char *args[5] = {KEXEC_BINARY_PATH, "-e", "-f", "-x", NULL};
330 execv(args[0], (char * const *) args);
342 log_error("Critical error while doing system shutdown: %s", strerror(r));