]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/shutdown.c
Merge pull request #3589 from brauner/cgroup_namespace
[thirdparty/systemd.git] / src / core / shutdown.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 ProFUSION embedded systems
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <linux/reboot.h>
23 #include <signal.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <sys/mman.h>
27 #include <sys/mount.h>
28 #include <sys/reboot.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31
32 #include "alloc-util.h"
33 #include "cgroup-util.h"
34 #include "def.h"
35 #include "fileio.h"
36 #include "killall.h"
37 #include "log.h"
38 #include "missing.h"
39 #include "parse-util.h"
40 #include "process-util.h"
41 #include "string-util.h"
42 #include "switch-root.h"
43 #include "terminal-util.h"
44 #include "umount.h"
45 #include "util.h"
46 #include "virt.h"
47 #include "watchdog.h"
48
49 #define FINALIZE_ATTEMPTS 50
50
51 static char* arg_verb;
52 static uint8_t arg_exit_code;
53
54 static int parse_argv(int argc, char *argv[]) {
55 enum {
56 ARG_LOG_LEVEL = 0x100,
57 ARG_LOG_TARGET,
58 ARG_LOG_COLOR,
59 ARG_LOG_LOCATION,
60 ARG_EXIT_CODE,
61 };
62
63 static const struct option options[] = {
64 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
65 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
66 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
67 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
68 { "exit-code", required_argument, NULL, ARG_EXIT_CODE },
69 {}
70 };
71
72 int c, r;
73
74 assert(argc >= 1);
75 assert(argv);
76
77 /* "-" prevents getopt from permuting argv[] and moving the verb away
78 * from argv[1]. Our interface to initrd promises it'll be there. */
79 while ((c = getopt_long(argc, argv, "-", options, NULL)) >= 0)
80 switch (c) {
81
82 case ARG_LOG_LEVEL:
83 r = log_set_max_level_from_string(optarg);
84 if (r < 0)
85 log_error("Failed to parse log level %s, ignoring.", optarg);
86
87 break;
88
89 case ARG_LOG_TARGET:
90 r = log_set_target_from_string(optarg);
91 if (r < 0)
92 log_error("Failed to parse log target %s, ignoring", optarg);
93
94 break;
95
96 case ARG_LOG_COLOR:
97
98 if (optarg) {
99 r = log_show_color_from_string(optarg);
100 if (r < 0)
101 log_error("Failed to parse log color setting %s, ignoring", optarg);
102 } else
103 log_show_color(true);
104
105 break;
106
107 case ARG_LOG_LOCATION:
108 if (optarg) {
109 r = log_show_location_from_string(optarg);
110 if (r < 0)
111 log_error("Failed to parse log location setting %s, ignoring", optarg);
112 } else
113 log_show_location(true);
114
115 break;
116
117 case ARG_EXIT_CODE:
118 r = safe_atou8(optarg, &arg_exit_code);
119 if (r < 0)
120 log_error("Failed to parse exit code %s, ignoring", optarg);
121
122 break;
123
124 case '\001':
125 if (!arg_verb)
126 arg_verb = optarg;
127 else
128 log_error("Excess arguments, ignoring");
129 break;
130
131 case '?':
132 return -EINVAL;
133
134 default:
135 assert_not_reached("Unhandled option code.");
136 }
137
138 if (!arg_verb) {
139 log_error("Verb argument missing.");
140 return -EINVAL;
141 }
142
143 return 0;
144 }
145
146 static int switch_root_initramfs(void) {
147 if (mount("/run/initramfs", "/run/initramfs", NULL, MS_BIND, NULL) < 0)
148 return log_error_errno(errno, "Failed to mount bind /run/initramfs on /run/initramfs: %m");
149
150 if (mount(NULL, "/run/initramfs", NULL, MS_PRIVATE, NULL) < 0)
151 return log_error_errno(errno, "Failed to make /run/initramfs private mount: %m");
152
153 /* switch_root with MS_BIND, because there might still be processes lurking around, which have open file descriptors.
154 * /run/initramfs/shutdown will take care of these.
155 * Also do not detach the old root, because /run/initramfs/shutdown needs to access it.
156 */
157 return switch_root("/run/initramfs", "/oldroot", false, MS_BIND);
158 }
159
160 int main(int argc, char *argv[]) {
161 bool need_umount, need_swapoff, need_loop_detach, need_dm_detach;
162 bool in_container, use_watchdog = false;
163 _cleanup_free_ char *cgroup = NULL;
164 char *arguments[3];
165 unsigned retries;
166 int cmd, r;
167 static const char* const dirs[] = {SYSTEM_SHUTDOWN_PATH, NULL};
168
169 log_parse_environment();
170 r = parse_argv(argc, argv);
171 if (r < 0)
172 goto error;
173
174 /* journald will die if not gone yet. The log target defaults
175 * to console, but may have been changed by command line options. */
176
177 log_close_console(); /* force reopen of /dev/console */
178 log_open();
179
180 umask(0022);
181
182 if (getpid() != 1) {
183 log_error("Not executed by init (PID 1).");
184 r = -EPERM;
185 goto error;
186 }
187
188 if (streq(arg_verb, "reboot"))
189 cmd = RB_AUTOBOOT;
190 else if (streq(arg_verb, "poweroff"))
191 cmd = RB_POWER_OFF;
192 else if (streq(arg_verb, "halt"))
193 cmd = RB_HALT_SYSTEM;
194 else if (streq(arg_verb, "kexec"))
195 cmd = LINUX_REBOOT_CMD_KEXEC;
196 else if (streq(arg_verb, "exit"))
197 cmd = 0; /* ignored, just checking that arg_verb is valid */
198 else {
199 r = -EINVAL;
200 log_error("Unknown action '%s'.", arg_verb);
201 goto error;
202 }
203
204 (void) cg_get_root_path(&cgroup);
205 in_container = detect_container() > 0;
206
207 use_watchdog = !!getenv("WATCHDOG_USEC");
208
209 /* Lock us into memory */
210 mlockall(MCL_CURRENT|MCL_FUTURE);
211
212 /* Synchronize everything that is not written to disk yet at this point already. This is a good idea so that
213 * slow IO is processed here already and the final process killing spree is not impacted by processes
214 * desperately trying to sync IO to disk within their timeout. */
215 if (!in_container)
216 sync();
217
218 log_info("Sending SIGTERM to remaining processes...");
219 broadcast_signal(SIGTERM, true, true);
220
221 log_info("Sending SIGKILL to remaining processes...");
222 broadcast_signal(SIGKILL, true, false);
223
224 need_umount = !in_container;
225 need_swapoff = !in_container;
226 need_loop_detach = !in_container;
227 need_dm_detach = !in_container;
228
229 /* Unmount all mountpoints, swaps, and loopback devices */
230 for (retries = 0; retries < FINALIZE_ATTEMPTS; retries++) {
231 bool changed = false;
232
233 if (use_watchdog)
234 watchdog_ping();
235
236 /* Let's trim the cgroup tree on each iteration so
237 that we leave an empty cgroup tree around, so that
238 container managers get a nice notify event when we
239 are down */
240 if (cgroup)
241 cg_trim(SYSTEMD_CGROUP_CONTROLLER, cgroup, false);
242
243 if (need_umount) {
244 log_info("Unmounting file systems.");
245 r = umount_all(&changed);
246 if (r == 0) {
247 need_umount = false;
248 log_info("All filesystems unmounted.");
249 } else if (r > 0)
250 log_info("Not all file systems unmounted, %d left.", r);
251 else
252 log_error_errno(r, "Failed to unmount file systems: %m");
253 }
254
255 if (need_swapoff) {
256 log_info("Deactivating swaps.");
257 r = swapoff_all(&changed);
258 if (r == 0) {
259 need_swapoff = false;
260 log_info("All swaps deactivated.");
261 } else if (r > 0)
262 log_info("Not all swaps deactivated, %d left.", r);
263 else
264 log_error_errno(r, "Failed to deactivate swaps: %m");
265 }
266
267 if (need_loop_detach) {
268 log_info("Detaching loop devices.");
269 r = loopback_detach_all(&changed);
270 if (r == 0) {
271 need_loop_detach = false;
272 log_info("All loop devices detached.");
273 } else if (r > 0)
274 log_info("Not all loop devices detached, %d left.", r);
275 else
276 log_error_errno(r, "Failed to detach loop devices: %m");
277 }
278
279 if (need_dm_detach) {
280 log_info("Detaching DM devices.");
281 r = dm_detach_all(&changed);
282 if (r == 0) {
283 need_dm_detach = false;
284 log_info("All DM devices detached.");
285 } else if (r > 0)
286 log_info("Not all DM devices detached, %d left.", r);
287 else
288 log_error_errno(r, "Failed to detach DM devices: %m");
289 }
290
291 if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach) {
292 if (retries > 0)
293 log_info("All filesystems, swaps, loop devices, DM devices detached.");
294 /* Yay, done */
295 goto initrd_jump;
296 }
297
298 /* If in this iteration we didn't manage to
299 * unmount/deactivate anything, we simply give up */
300 if (!changed) {
301 log_info("Cannot finalize remaining%s%s%s%s continuing.",
302 need_umount ? " file systems," : "",
303 need_swapoff ? " swap devices," : "",
304 need_loop_detach ? " loop devices," : "",
305 need_dm_detach ? " DM devices," : "");
306 goto initrd_jump;
307 }
308
309 log_debug("After %u retries, couldn't finalize remaining %s%s%s%s trying again.",
310 retries + 1,
311 need_umount ? " file systems," : "",
312 need_swapoff ? " swap devices," : "",
313 need_loop_detach ? " loop devices," : "",
314 need_dm_detach ? " DM devices," : "");
315 }
316
317 log_error("Too many iterations, giving up.");
318
319 initrd_jump:
320
321 arguments[0] = NULL;
322 arguments[1] = arg_verb;
323 arguments[2] = NULL;
324 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
325
326 if (!in_container && !in_initrd() &&
327 access("/run/initramfs/shutdown", X_OK) == 0) {
328 r = switch_root_initramfs();
329 if (r >= 0) {
330 argv[0] = (char*) "/shutdown";
331
332 setsid();
333 make_console_stdio();
334
335 log_info("Successfully changed into root pivot.\n"
336 "Returning to initrd...");
337
338 execv("/shutdown", argv);
339 log_error_errno(errno, "Failed to execute shutdown binary: %m");
340 } else
341 log_error_errno(r, "Failed to switch root to \"/run/initramfs\": %m");
342
343 }
344
345 if (need_umount || need_swapoff || need_loop_detach || need_dm_detach)
346 log_error("Failed to finalize %s%s%s%s ignoring",
347 need_umount ? " file systems," : "",
348 need_swapoff ? " swap devices," : "",
349 need_loop_detach ? " loop devices," : "",
350 need_dm_detach ? " DM devices," : "");
351
352 /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be
353 * sync'ed explicitly in advance. So let's do this here, but not needlessly slow down containers. Note that we
354 * sync'ed things already once above, but we did some more work since then which might have caused IO, hence
355 * let's doit once more. */
356 if (!in_container)
357 sync();
358
359 if (streq(arg_verb, "exit")) {
360 if (in_container)
361 exit(arg_exit_code);
362 else {
363 /* We cannot exit() on the host, fallback on another
364 * method. */
365 cmd = RB_POWER_OFF;
366 }
367 }
368
369 switch (cmd) {
370
371 case LINUX_REBOOT_CMD_KEXEC:
372
373 if (!in_container) {
374 /* We cheat and exec kexec to avoid doing all its work */
375 pid_t pid;
376
377 log_info("Rebooting with kexec.");
378
379 pid = fork();
380 if (pid < 0)
381 log_error_errno(errno, "Failed to fork: %m");
382 else if (pid == 0) {
383
384 const char * const args[] = {
385 KEXEC, "-e", NULL
386 };
387
388 /* Child */
389
390 execv(args[0], (char * const *) args);
391 _exit(EXIT_FAILURE);
392 } else
393 wait_for_terminate_and_warn("kexec", pid, true);
394 }
395
396 cmd = RB_AUTOBOOT;
397 /* Fall through */
398
399 case RB_AUTOBOOT:
400
401 if (!in_container) {
402 _cleanup_free_ char *param = NULL;
403
404 r = read_one_line_file("/run/systemd/reboot-param", &param);
405 if (r < 0)
406 log_warning_errno(r, "Failed to read reboot parameter file: %m");
407
408 if (!isempty(param)) {
409 log_info("Rebooting with argument '%s'.", param);
410 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, param);
411 log_warning_errno(errno, "Failed to reboot with parameter, retrying without: %m");
412 }
413 }
414
415 log_info("Rebooting.");
416 break;
417
418 case RB_POWER_OFF:
419 log_info("Powering off.");
420 break;
421
422 case RB_HALT_SYSTEM:
423 log_info("Halting system.");
424 break;
425
426 default:
427 assert_not_reached("Unknown magic");
428 }
429
430 reboot(cmd);
431 if (errno == EPERM && in_container) {
432 /* If we are in a container, and we lacked
433 * CAP_SYS_BOOT just exit, this will kill our
434 * container for good. */
435 log_info("Exiting container.");
436 exit(0);
437 }
438
439 r = log_error_errno(errno, "Failed to invoke reboot(): %m");
440
441 error:
442 log_emergency_errno(r, "Critical error while doing system shutdown: %m");
443 freeze();
444 }