]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shutdown.c
util: move virtualization detection into its own files, and extend return codes
[thirdparty/systemd.git] / src / shutdown.c
CommitLineData
b1b2a107
FF
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 ProFUSION embedded systems
7
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.
12
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.
17
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/>.
20***/
21
22#include <sys/mman.h>
23#include <sys/types.h>
24#include <sys/reboot.h>
25#include <linux/reboot.h>
26#include <sys/wait.h>
7cb1094a
HH
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/mount.h>
30#include <sys/syscall.h>
31#include <fcntl.h>
b1b2a107
FF
32#include <dirent.h>
33#include <errno.h>
34#include <unistd.h>
35#include <signal.h>
36#include <stdbool.h>
37#include <stdlib.h>
38#include <string.h>
39
7cb1094a 40#include "missing.h"
b1b2a107
FF
41#include "log.h"
42#include "umount.h"
43#include "util.h"
b52aae1d 44#include "virt.h"
b1b2a107 45
567ea02a 46#define TIMEOUT_USEC (5 * USEC_PER_SEC)
b1b2a107 47#define FINALIZE_ATTEMPTS 50
b1b2a107 48
b1b2a107
FF
49static bool ignore_proc(pid_t pid) {
50 if (pid == 1)
51 return true;
52
53 /* TODO: add more ignore rules here: device-mapper, etc */
54
55 return false;
56}
57
58static bool is_kernel_thread(pid_t pid)
59{
60 char buf[PATH_MAX];
61 FILE *f;
62 char c;
63 size_t count;
64
65 snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long)pid);
66 f = fopen(buf, "re");
67 if (!f)
68 return true; /* not really, but has the desired effect */
69
70 count = fread(&c, 1, 1, f);
71 fclose(f);
72 return count != 1;
73}
74
75static int killall(int sign) {
76 DIR *dir;
77 struct dirent *d;
12aad1d0 78 unsigned int n_processes = 0;
b1b2a107
FF
79
80 if ((dir = opendir("/proc")) == NULL)
81 return -errno;
82
83 while ((d = readdir(dir))) {
84 pid_t pid;
85
86 if (parse_pid(d->d_name, &pid) < 0)
87 continue;
88
89 if (is_kernel_thread(pid))
90 continue;
91
92 if (ignore_proc(pid))
93 continue;
94
95 if (kill(pid, sign) == 0)
12aad1d0 96 n_processes++;
b1b2a107
FF
97 else
98 log_warning("Could not kill %d: %m", pid);
99 }
100
101 closedir(dir);
102
12aad1d0 103 return n_processes;
b1b2a107
FF
104}
105
40e85d00 106static void wait_for_children(int n_processes, sigset_t *mask) {
b1b2a107 107 usec_t until;
b1b2a107 108
40e85d00 109 assert(mask);
b1b2a107
FF
110
111 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
112 for (;;) {
40e85d00 113 struct timespec ts;
ab58e291 114 int k;
40e85d00 115 usec_t n;
ab58e291 116
b1b2a107
FF
117 for (;;) {
118 pid_t pid = waitpid(-1, NULL, WNOHANG);
12aad1d0 119
b1b2a107
FF
120 if (pid == 0)
121 break;
b1b2a107 122
40e85d00
LP
123 if (pid < 0 && errno == ECHILD)
124 return;
125
126 if (n_processes > 0)
127 if (--n_processes == 0)
128 return;
b1b2a107
FF
129 }
130
40e85d00 131 n = now(CLOCK_MONOTONIC);
b1b2a107 132 if (n >= until)
40e85d00 133 return;
b1b2a107
FF
134
135 timespec_store(&ts, until - n);
40e85d00
LP
136
137 if ((k = sigtimedwait(mask, NULL, &ts)) != SIGCHLD) {
138
139 if (k < 0 && errno != EAGAIN) {
140 log_error("sigtimedwait() failed: %m");
141 return;
142 }
143
ab58e291
LP
144 if (k >= 0)
145 log_warning("sigtimedwait() returned unexpected signal.");
ab58e291 146 }
b1b2a107 147 }
40e85d00
LP
148}
149
150static void send_signal(int sign) {
151 sigset_t mask, oldmask;
152 int n_processes;
153
154 assert_se(sigemptyset(&mask) == 0);
155 assert_se(sigaddset(&mask, SIGCHLD) == 0);
156 assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
157
158 if (kill(-1, SIGSTOP) < 0 && errno != ESRCH)
159 log_warning("kill(-1, SIGSTOP) failed: %m");
160
161 n_processes = killall(sign);
162
163 if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
164 log_warning("kill(-1, SIGCONT) failed: %m");
165
166 if (n_processes <= 0)
167 goto finish;
168
169 wait_for_children(n_processes, &mask);
b1b2a107
FF
170
171finish:
172 sigprocmask(SIG_SETMASK, &oldmask, NULL);
b1b2a107
FF
173}
174
40e85d00 175static void ultimate_send_signal(int sign) {
b1b2a107 176 sigset_t mask, oldmask;
b1b2a107
FF
177 int r;
178
40e85d00
LP
179 assert_se(sigemptyset(&mask) == 0);
180 assert_se(sigaddset(&mask, SIGCHLD) == 0);
181 assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
b1b2a107 182
40e85d00 183 if (kill(-1, SIGSTOP) < 0 && errno != ESRCH)
ab58e291 184 log_warning("kill(-1, SIGSTOP) failed: %m");
b1b2a107
FF
185
186 r = kill(-1, sign);
40e85d00
LP
187 if (r < 0 && errno != ESRCH)
188 log_warning("kill(-1, %s) failed: %m", signal_to_string(sign));
b1b2a107 189
40e85d00 190 if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
ab58e291 191 log_warning("kill(-1, SIGCONT) failed: %m");
b1b2a107
FF
192
193 if (r < 0)
194 goto finish;
195
40e85d00 196 wait_for_children(0, &mask);
b1b2a107
FF
197
198finish:
199 sigprocmask(SIG_SETMASK, &oldmask, NULL);
b1b2a107
FF
200}
201
89d471d5
LP
202static int prepare_new_root(void) {
203 static const char dirs[] =
204 "/run/initramfs/oldroot\0"
205 "/run/initramfs/proc\0"
206 "/run/initramfs/sys\0"
207 "/run/initramfs/dev\0"
208 "/run/initramfs/run\0";
209
210 const char *dir;
211
212 if (mount("/run/initramfs", "/run/initramfs", NULL, MS_BIND, NULL) < 0) {
213 log_error("Failed to mount bind /run/initramfs on /run/initramfs: %m");
214 return -errno;
215 }
216
217 if (mount(NULL, "/run/initramfs", NULL, MS_PRIVATE, NULL) < 0) {
218 log_error("Failed to make /run/initramfs private mount: %m");
219 return -errno;
220 }
221
222 NULSTR_FOREACH(dir, dirs)
223 if (mkdir_p(dir, 0755) < 0 && errno != EEXIST) {
224 log_error("Failed to mkdir %s: %m", dir);
225 return -errno;
7cb1094a 226 }
89d471d5
LP
227
228 if (mount("/sys", "/run/initramfs/sys", NULL, MS_BIND, NULL) < 0) {
229 log_error("Failed to mount bind /sys on /run/initramfs/sys: %m");
230 return -errno;
231 }
232
233 if (mount("/proc", "/run/initramfs/proc", NULL, MS_BIND, NULL) < 0) {
234 log_error("Failed to mount bind /proc on /run/initramfs/proc: %m");
235 return -errno;
236 }
237
238 if (mount("/dev", "/run/initramfs/dev", NULL, MS_BIND, NULL) < 0) {
239 log_error("Failed to mount bind /dev on /run/initramfs/dev: %m");
240 return -errno;
7cb1094a
HH
241 }
242
89d471d5
LP
243 if (mount("/run", "/run/initramfs/run", NULL, MS_BIND, NULL) < 0) {
244 log_error("Failed to mount bind /run on /run/initramfs/run: %m");
245 return -errno;
246 }
247
248 return 0;
7cb1094a
HH
249}
250
89d471d5 251static int pivot_to_new_root(void) {
7cb1094a 252 int fd;
89d471d5 253
7cb1094a
HH
254 chdir("/run/initramfs");
255
256 /*
257 In case some evil process made "/" MS_SHARED
258 It works for pivot_root, but the ref count for the root device
259 is not decreasing :-/
260 */
89d471d5
LP
261 if (mount(NULL, "/", NULL, MS_PRIVATE, NULL) < 0) {
262 log_error("Failed to make \"/\" private mount %m");
263 return -errno;
7cb1094a
HH
264 }
265
89d471d5 266 if (pivot_root(".", "oldroot") < 0) {
7cb1094a 267 log_error("pivot failed: %m");
89d471d5
LP
268 /* only chroot if pivot root succeded */
269 return -errno;
7cb1094a 270 }
89d471d5 271
7cb1094a 272 chroot(".");
89d471d5
LP
273 log_info("Successfully changed into root pivot.");
274
275 fd = open("/dev/console", O_RDWR);
276 if (fd < 0)
277 log_error("Failed to open /dev/console: %m");
278 else {
279 make_stdio(fd);
bccc1d88
LP
280
281 /* Initialize the controlling terminal */
282 setsid();
283 ioctl(STDIN_FILENO, TIOCSCTTY, NULL);
89d471d5
LP
284 }
285
286 return 0;
7cb1094a
HH
287}
288
b1b2a107 289int main(int argc, char *argv[]) {
12aad1d0
LP
290 int cmd, r;
291 unsigned retries;
d48141ba 292 bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true;
40e85d00 293 bool killed_everbody = false, in_container;
b1b2a107
FF
294
295 log_parse_environment();
2ca1b422 296 log_set_target(LOG_TARGET_CONSOLE); /* syslog will die if not gone yet */
b1b2a107
FF
297 log_open();
298
4c12626c
LP
299 umask(0022);
300
b1b2a107 301 if (getpid() != 1) {
567ea02a 302 log_error("Not executed by init (pid 1).");
b1b2a107
FF
303 r = -EPERM;
304 goto error;
305 }
306
307 if (argc != 2) {
308 log_error("Invalid number of arguments.");
309 r = -EINVAL;
310 goto error;
311 }
312
40e85d00
LP
313 in_container = detect_container(NULL) > 0;
314
b1b2a107
FF
315 if (streq(argv[1], "reboot"))
316 cmd = RB_AUTOBOOT;
317 else if (streq(argv[1], "poweroff"))
318 cmd = RB_POWER_OFF;
319 else if (streq(argv[1], "halt"))
320 cmd = RB_HALT_SYSTEM;
321 else if (streq(argv[1], "kexec"))
322 cmd = LINUX_REBOOT_CMD_KEXEC;
323 else {
324 log_error("Unknown action '%s'.", argv[1]);
325 r = -EINVAL;
326 goto error;
327 }
328
329 /* lock us into memory */
330 if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0)
331 log_warning("Cannot lock process memory: %m");
332
ab58e291 333 log_info("Sending SIGTERM to remaining processes...");
40e85d00 334 send_signal(SIGTERM);
b1b2a107 335
ab58e291 336 log_info("Sending SIGKILL to remaining processes...");
40e85d00
LP
337 send_signal(SIGKILL);
338
339 if (in_container)
340 need_swapoff = false;
b1b2a107 341
567ea02a 342 /* Unmount all mountpoints, swaps, and loopback devices */
12aad1d0
LP
343 for (retries = 0; retries < FINALIZE_ATTEMPTS; retries++) {
344 bool changed = false;
345
b1b2a107 346 if (need_umount) {
ab58e291 347 log_info("Unmounting file systems.");
12aad1d0 348 r = umount_all(&changed);
b1b2a107
FF
349 if (r == 0)
350 need_umount = false;
351 else if (r > 0)
ab58e291 352 log_info("Not all file systems unmounted, %d left.", r);
b1b2a107 353 else
ab58e291 354 log_error("Failed to unmount file systems: %s", strerror(-r));
b1b2a107
FF
355 }
356
357 if (need_swapoff) {
358 log_info("Disabling swaps.");
12aad1d0 359 r = swapoff_all(&changed);
b1b2a107
FF
360 if (r == 0)
361 need_swapoff = false;
362 else if (r > 0)
ab58e291 363 log_info("Not all swaps are turned off, %d left.", r);
b1b2a107 364 else
ab58e291 365 log_error("Failed to turn off swaps: %s", strerror(-r));
b1b2a107
FF
366 }
367
368 if (need_loop_detach) {
369 log_info("Detaching loop devices.");
12aad1d0 370 r = loopback_detach_all(&changed);
b1b2a107
FF
371 if (r == 0)
372 need_loop_detach = false;
373 else if (r > 0)
ab58e291 374 log_info("Not all loop devices detached, %d left.", r);
b1b2a107 375 else
ab58e291 376 log_error("Failed to detach loop devices: %s", strerror(-r));
d48141ba 377 }
b1b2a107 378
d48141ba
LP
379 if (need_dm_detach) {
380 log_info("Detaching DM devices.");
12aad1d0 381 r = dm_detach_all(&changed);
d48141ba
LP
382 if (r == 0)
383 need_dm_detach = false;
384 else if (r > 0)
ab58e291 385 log_warning("Not all DM devices detached, %d left.", r);
d48141ba 386 else
ab58e291 387 log_error("Failed to detach DM devices: %s", strerror(-r));
b1b2a107
FF
388 }
389
a27d2184
KS
390 if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach) {
391 if (retries > 0)
392 log_info("All filesystems, swaps, loop devices, DM devices detached.");
12aad1d0
LP
393 /* Yay, done */
394 break;
a27d2184 395 }
b1b2a107 396
12aad1d0
LP
397 /* If in this iteration we didn't manage to
398 * unmount/deactivate anything, we either kill more
399 * processes, or simply give up */
400 if (!changed) {
b1b2a107 401
12aad1d0
LP
402 if (killed_everbody) {
403 /* Hmm, we already killed everybody,
404 * let's just give up */
ab58e291 405 log_error("Cannot finalize remaining file systems and devices, giving up.");
d37fb98b 406 break;
b1b2a107 407 }
12aad1d0 408
ab58e291 409 log_warning("Cannot finalize remaining file systems and devices, trying to kill remaining processes.");
40e85d00
LP
410 ultimate_send_signal(SIGTERM);
411 ultimate_send_signal(SIGKILL);
12aad1d0
LP
412 killed_everbody = true;
413 }
414
ab58e291 415 log_debug("Couldn't finalize remaining file systems and devices after %u retries, trying again.", retries+1);
b1b2a107
FF
416 }
417
12aad1d0 418 if (retries >= FINALIZE_ATTEMPTS)
35b8ca3a 419 log_error("Too many iterations, giving up.");
12aad1d0 420
83cc030f
LP
421 execute_directory(SYSTEM_SHUTDOWN_PATH, NULL, NULL);
422
40e85d00
LP
423 /* If we are in a container, just exit, this will kill our
424 * container for good. */
f41de959
LP
425 if (in_container) {
426 log_error("Exiting container.");
40e85d00 427 exit(0);
f41de959 428 }
40e85d00 429
7cb1094a 430 if (access("/run/initramfs/shutdown", X_OK) == 0) {
89d471d5
LP
431
432 if (prepare_new_root() >= 0 &&
433 pivot_to_new_root() >= 0) {
434 execv("/shutdown", argv);
7cb1094a
HH
435 log_error("Failed to execute shutdown binary: %m");
436 }
437 }
438
89d471d5
LP
439 sync();
440
b1b2a107 441 if (cmd == LINUX_REBOOT_CMD_KEXEC) {
12aad1d0 442 /* We cheat and exec kexec to avoid doing all its work */
b1b2a107 443 pid_t pid = fork();
12aad1d0 444
e61cd186
LP
445 if (pid < 0)
446 log_error("Could not fork: %m. Falling back to normal reboot.");
447 else if (pid > 0) {
448 wait_for_terminate_and_warn("kexec", pid);
449 log_warning("kexec failed. Falling back to normal reboot.");
b1b2a107 450 } else {
e61cd186 451 /* Child */
f8d0ceb4 452 const char *args[3] = { "/sbin/kexec", "-e", NULL };
b1b2a107
FF
453 execv(args[0], (char * const *) args);
454 return EXIT_FAILURE;
455 }
e61cd186
LP
456
457 cmd = RB_AUTOBOOT;
b1b2a107
FF
458 }
459
460 reboot(cmd);
e61cd186
LP
461 log_error("Failed to invoke reboot(): %m");
462 r = -errno;
b1b2a107
FF
463
464 error:
e61cd186
LP
465 log_error("Critical error while doing system shutdown: %s", strerror(-r));
466
b1b2a107 467 freeze();
3c14d26c 468 return EXIT_FAILURE;
b1b2a107 469}