]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
b1b2a107 | 2 | /*** |
96b2fb93 | 3 | Copyright © 2010 ProFUSION embedded systems |
b1b2a107 FF |
4 | ***/ |
5 | ||
b1b2a107 | 6 | #include <errno.h> |
07630cea | 7 | #include <getopt.h> |
c01dcddf | 8 | #include <linux/reboot.h> |
b1b2a107 FF |
9 | #include <stdbool.h> |
10 | #include <stdlib.h> | |
07630cea LP |
11 | #include <sys/mman.h> |
12 | #include <sys/mount.h> | |
13 | #include <sys/reboot.h> | |
14 | #include <sys/stat.h> | |
15 | #include <unistd.h> | |
b1b2a107 | 16 | |
b5efdb8a | 17 | #include "alloc-util.h" |
d00c2631 | 18 | #include "async.h" |
0282c028 | 19 | #include "binfmt-util.h" |
fdb3deca | 20 | #include "cgroup-setup.h" |
07630cea | 21 | #include "cgroup-util.h" |
c8715007 | 22 | #include "coredump-util.h" |
28db6fbf | 23 | #include "constants.h" |
731e5cda | 24 | #include "errno-util.h" |
89711996 | 25 | #include "exec-util.h" |
d00c2631 | 26 | #include "fd-util.h" |
ec26be51 | 27 | #include "fileio.h" |
baa6a42d | 28 | #include "initrd-util.h" |
07630cea LP |
29 | #include "killall.h" |
30 | #include "log.h" | |
6bedfcbb | 31 | #include "parse-util.h" |
07630cea | 32 | #include "process-util.h" |
c01dcddf | 33 | #include "reboot-util.h" |
595225af | 34 | #include "rlimit-util.h" |
73ad712f | 35 | #include "signal-util.h" |
07630cea LP |
36 | #include "string-util.h" |
37 | #include "switch-root.h" | |
827156b3 | 38 | #include "sysctl-util.h" |
07630cea | 39 | #include "terminal-util.h" |
b1b2a107 | 40 | #include "umount.h" |
b52aae1d | 41 | #include "virt.h" |
e96d6be7 | 42 | #include "watchdog.h" |
b1b2a107 | 43 | |
73ad712f KW |
44 | #define SYNC_PROGRESS_ATTEMPTS 3 |
45 | #define SYNC_TIMEOUT_USEC (10*USEC_PER_SEC) | |
46 | ||
b1e90ec5 | 47 | static char* arg_verb; |
287419c1 | 48 | static uint8_t arg_exit_code; |
e73c54b8 | 49 | static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC; |
b1e90ec5 ZJS |
50 | |
51 | static int parse_argv(int argc, char *argv[]) { | |
52 | enum { | |
53 | ARG_LOG_LEVEL = 0x100, | |
54 | ARG_LOG_TARGET, | |
55 | ARG_LOG_COLOR, | |
56 | ARG_LOG_LOCATION, | |
c5673ed0 | 57 | ARG_LOG_TIME, |
287419c1 | 58 | ARG_EXIT_CODE, |
e73c54b8 | 59 | ARG_TIMEOUT, |
b1e90ec5 ZJS |
60 | }; |
61 | ||
62 | static const struct option options[] = { | |
63 | { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, | |
64 | { "log-target", required_argument, NULL, ARG_LOG_TARGET }, | |
65 | { "log-color", optional_argument, NULL, ARG_LOG_COLOR }, | |
66 | { "log-location", optional_argument, NULL, ARG_LOG_LOCATION }, | |
c5673ed0 | 67 | { "log-time", optional_argument, NULL, ARG_LOG_TIME }, |
287419c1 | 68 | { "exit-code", required_argument, NULL, ARG_EXIT_CODE }, |
e73c54b8 | 69 | { "timeout", required_argument, NULL, ARG_TIMEOUT }, |
b1e90ec5 ZJS |
70 | {} |
71 | }; | |
72 | ||
73 | int c, r; | |
74 | ||
75 | assert(argc >= 1); | |
76 | assert(argv); | |
77 | ||
4b5d8d0f MS |
78 | /* "-" prevents getopt from permuting argv[] and moving the verb away |
79 | * from argv[1]. Our interface to initrd promises it'll be there. */ | |
80 | while ((c = getopt_long(argc, argv, "-", options, NULL)) >= 0) | |
b1e90ec5 ZJS |
81 | switch (c) { |
82 | ||
83 | case ARG_LOG_LEVEL: | |
84 | r = log_set_max_level_from_string(optarg); | |
85 | if (r < 0) | |
5e1ee764 | 86 | log_error_errno(r, "Failed to parse log level %s, ignoring: %m", optarg); |
b1e90ec5 ZJS |
87 | |
88 | break; | |
89 | ||
90 | case ARG_LOG_TARGET: | |
91 | r = log_set_target_from_string(optarg); | |
92 | if (r < 0) | |
5e1ee764 | 93 | log_error_errno(r, "Failed to parse log target %s, ignoring: %m", optarg); |
b1e90ec5 ZJS |
94 | |
95 | break; | |
96 | ||
97 | case ARG_LOG_COLOR: | |
98 | ||
99 | if (optarg) { | |
100 | r = log_show_color_from_string(optarg); | |
101 | if (r < 0) | |
5e1ee764 | 102 | log_error_errno(r, "Failed to parse log color setting %s, ignoring: %m", optarg); |
b1e90ec5 ZJS |
103 | } else |
104 | log_show_color(true); | |
105 | ||
106 | break; | |
107 | ||
108 | case ARG_LOG_LOCATION: | |
109 | if (optarg) { | |
110 | r = log_show_location_from_string(optarg); | |
111 | if (r < 0) | |
5e1ee764 | 112 | log_error_errno(r, "Failed to parse log location setting %s, ignoring: %m", optarg); |
b1e90ec5 ZJS |
113 | } else |
114 | log_show_location(true); | |
115 | ||
116 | break; | |
117 | ||
c5673ed0 DS |
118 | case ARG_LOG_TIME: |
119 | ||
120 | if (optarg) { | |
121 | r = log_show_time_from_string(optarg); | |
122 | if (r < 0) | |
123 | log_error_errno(r, "Failed to parse log time setting %s, ignoring: %m", optarg); | |
124 | } else | |
125 | log_show_time(true); | |
126 | ||
127 | break; | |
128 | ||
287419c1 AC |
129 | case ARG_EXIT_CODE: |
130 | r = safe_atou8(optarg, &arg_exit_code); | |
131 | if (r < 0) | |
5e1ee764 | 132 | log_error_errno(r, "Failed to parse exit code %s, ignoring: %m", optarg); |
287419c1 AC |
133 | |
134 | break; | |
135 | ||
e73c54b8 JK |
136 | case ARG_TIMEOUT: |
137 | r = parse_sec(optarg, &arg_timeout); | |
138 | if (r < 0) | |
5e1ee764 | 139 | log_error_errno(r, "Failed to parse shutdown timeout %s, ignoring: %m", optarg); |
e73c54b8 JK |
140 | |
141 | break; | |
142 | ||
4b5d8d0f MS |
143 | case '\001': |
144 | if (!arg_verb) | |
145 | arg_verb = optarg; | |
146 | else | |
147 | log_error("Excess arguments, ignoring"); | |
148 | break; | |
149 | ||
b1e90ec5 | 150 | case '?': |
b1e90ec5 ZJS |
151 | return -EINVAL; |
152 | ||
153 | default: | |
04499a70 | 154 | assert_not_reached(); |
b1e90ec5 ZJS |
155 | } |
156 | ||
baaa35ad ZJS |
157 | if (!arg_verb) |
158 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
159 | "Verb argument missing."); | |
b1e90ec5 | 160 | |
b1e90ec5 ZJS |
161 | return 0; |
162 | } | |
163 | ||
5a4bf02f | 164 | static int switch_root_initramfs(void) { |
4a62c710 MS |
165 | if (mount("/run/initramfs", "/run/initramfs", NULL, MS_BIND, NULL) < 0) |
166 | return log_error_errno(errno, "Failed to mount bind /run/initramfs on /run/initramfs: %m"); | |
89d471d5 | 167 | |
4a62c710 MS |
168 | if (mount(NULL, "/run/initramfs", NULL, MS_PRIVATE, NULL) < 0) |
169 | return log_error_errno(errno, "Failed to make /run/initramfs private mount: %m"); | |
89d471d5 | 170 | |
f131770b | 171 | /* switch_root with MS_BIND, because there might still be processes lurking around, which have open file descriptors. |
5a4bf02f HH |
172 | * /run/initramfs/shutdown will take care of these. |
173 | * Also do not detach the old root, because /run/initramfs/shutdown needs to access it. | |
174 | */ | |
175 | return switch_root("/run/initramfs", "/oldroot", false, MS_BIND); | |
7cb1094a HH |
176 | } |
177 | ||
73ad712f KW |
178 | /* Read the following fields from /proc/meminfo: |
179 | * | |
180 | * NFS_Unstable | |
181 | * Writeback | |
182 | * Dirty | |
183 | * | |
184 | * Return true if the sum of these fields is greater than the previous | |
185 | * value input. For all other issues, report the failure and indicate that | |
186 | * the sync is not making progress. | |
187 | */ | |
66034f9c | 188 | static int sync_making_progress(unsigned long long *prev_dirty) { |
73ad712f | 189 | _cleanup_fclose_ FILE *f = NULL; |
73ad712f | 190 | unsigned long long val = 0; |
66034f9c | 191 | int ret; |
73ad712f KW |
192 | |
193 | f = fopen("/proc/meminfo", "re"); | |
194 | if (!f) | |
195 | return log_warning_errno(errno, "Failed to open /proc/meminfo: %m"); | |
196 | ||
a34f0dae LP |
197 | for (;;) { |
198 | _cleanup_free_ char *line = NULL; | |
73ad712f | 199 | unsigned long long ull = 0; |
a34f0dae LP |
200 | int q; |
201 | ||
202 | q = read_line(f, LONG_LINE_MAX, &line); | |
203 | if (q < 0) | |
204 | return log_warning_errno(q, "Failed to parse /proc/meminfo: %m"); | |
205 | if (q == 0) | |
206 | break; | |
73ad712f KW |
207 | |
208 | if (!first_word(line, "NFS_Unstable:") && !first_word(line, "Writeback:") && !first_word(line, "Dirty:")) | |
209 | continue; | |
210 | ||
211 | errno = 0; | |
212 | if (sscanf(line, "%*s %llu %*s", &ull) != 1) { | |
213 | if (errno != 0) | |
214 | log_warning_errno(errno, "Failed to parse /proc/meminfo: %m"); | |
215 | else | |
216 | log_warning("Failed to parse /proc/meminfo"); | |
217 | ||
218 | return false; | |
219 | } | |
220 | ||
221 | val += ull; | |
222 | } | |
223 | ||
66034f9c | 224 | ret = *prev_dirty > val; |
73ad712f | 225 | *prev_dirty = val; |
66034f9c | 226 | return ret; |
73ad712f KW |
227 | } |
228 | ||
229 | static void sync_with_progress(void) { | |
8a8e84d2 | 230 | unsigned long long dirty = ULLONG_MAX; |
73ad712f KW |
231 | unsigned checks; |
232 | pid_t pid; | |
233 | int r; | |
73ad712f KW |
234 | |
235 | BLOCK_SIGNALS(SIGCHLD); | |
236 | ||
273d76f4 YW |
237 | /* Due to the possibility of the sync operation hanging, we fork a child process and monitor |
238 | * the progress. If the timeout lapses, the assumption is that the particular sync stalled. */ | |
d00c2631 LP |
239 | |
240 | r = asynchronous_sync(&pid); | |
4c253ed1 | 241 | if (r < 0) { |
d00c2631 | 242 | log_error_errno(r, "Failed to fork sync(): %m"); |
73ad712f KW |
243 | return; |
244 | } | |
73ad712f KW |
245 | |
246 | log_info("Syncing filesystems and block devices."); | |
247 | ||
248 | /* Start monitoring the sync operation. If more than | |
249 | * SYNC_PROGRESS_ATTEMPTS lapse without progress being made, | |
250 | * we assume that the sync is stalled */ | |
251 | for (checks = 0; checks < SYNC_PROGRESS_ATTEMPTS; checks++) { | |
252 | r = wait_for_terminate_with_timeout(pid, SYNC_TIMEOUT_USEC); | |
253 | if (r == 0) | |
254 | /* Sync finished without error. | |
255 | * (The sync itself does not return an error code) */ | |
256 | return; | |
257 | else if (r == -ETIMEDOUT) { | |
258 | /* Reset the check counter if the "Dirty" value is | |
259 | * decreasing */ | |
66034f9c | 260 | if (sync_making_progress(&dirty) > 0) |
73ad712f KW |
261 | checks = 0; |
262 | } else { | |
263 | log_error_errno(r, "Failed to sync filesystems and block devices: %m"); | |
264 | return; | |
265 | } | |
266 | } | |
267 | ||
268 | /* Only reached in the event of a timeout. We should issue a kill | |
269 | * to the stray process. */ | |
270 | log_error("Syncing filesystems and block devices - timed out, issuing SIGKILL to PID "PID_FMT".", pid); | |
271 | (void) kill(pid, SIGKILL); | |
272 | } | |
273 | ||
827156b3 BR |
274 | static int read_current_sysctl_printk_log_level(void) { |
275 | _cleanup_free_ char *sysctl_printk_vals = NULL, *sysctl_printk_curr = NULL; | |
701f6af6 | 276 | int current_lvl; |
827156b3 BR |
277 | const char *p; |
278 | int r; | |
279 | ||
280 | r = sysctl_read("kernel/printk", &sysctl_printk_vals); | |
281 | if (r < 0) | |
282 | return log_debug_errno(r, "Cannot read sysctl kernel.printk: %m"); | |
283 | ||
284 | p = sysctl_printk_vals; | |
285 | r = extract_first_word(&p, &sysctl_printk_curr, NULL, 0); | |
701f6af6 LP |
286 | if (r < 0) |
287 | return log_debug_errno(r, "Failed to split out kernel printk priority: %m"); | |
288 | if (r == 0) | |
289 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Short read while reading kernel.printk sysctl"); | |
827156b3 | 290 | |
701f6af6 | 291 | r = safe_atoi(sysctl_printk_curr, ¤t_lvl); |
827156b3 | 292 | if (r < 0) |
701f6af6 | 293 | return log_debug_errno(r, "Failed to parse kernel.printk sysctl: %s", sysctl_printk_vals); |
827156b3 BR |
294 | |
295 | return current_lvl; | |
296 | } | |
297 | ||
298 | static void bump_sysctl_printk_log_level(int min_level) { | |
477851f5 LP |
299 | int current_lvl, r; |
300 | ||
827156b3 BR |
301 | /* Set the logging level to be able to see messages with log level smaller or equal to min_level */ |
302 | ||
477851f5 LP |
303 | current_lvl = read_current_sysctl_printk_log_level(); |
304 | if (current_lvl < 0 || current_lvl >= min_level + 1) | |
305 | return; | |
306 | ||
307 | r = sysctl_writef("kernel/printk", "%i", min_level + 1); | |
308 | if (r < 0) | |
309 | log_debug_errno(r, "Failed to bump kernel.printk to %i: %m", min_level + 1); | |
827156b3 BR |
310 | } |
311 | ||
484752e2 FB |
312 | static void init_watchdog(void) { |
313 | const char *s; | |
314 | int r; | |
315 | ||
316 | s = getenv("WATCHDOG_DEVICE"); | |
317 | if (s) { | |
318 | r = watchdog_set_device(s); | |
319 | if (r < 0) | |
320 | log_warning_errno(r, "Failed to set watchdog device to %s, ignoring: %m", s); | |
321 | } | |
322 | ||
323 | s = getenv("WATCHDOG_USEC"); | |
324 | if (s) { | |
325 | usec_t usec; | |
326 | ||
327 | r = safe_atou64(s, &usec); | |
328 | if (r < 0) | |
329 | log_warning_errno(r, "Failed to parse watchdog timeout '%s', ignoring: %m", s); | |
330 | else | |
331 | (void) watchdog_setup(usec); | |
332 | } | |
333 | } | |
334 | ||
b1b2a107 | 335 | int main(int argc, char *argv[]) { |
731e5cda LP |
336 | static const char* const dirs[] = { |
337 | SYSTEM_SHUTDOWN_PATH, | |
338 | NULL | |
339 | }; | |
06beed6d | 340 | _cleanup_free_ char *cgroup = NULL; |
484752e2 | 341 | char *arguments[3]; |
5125b677 | 342 | int cmd, r; |
b1b2a107 | 343 | |
e18805fb LP |
344 | /* The log target defaults to console, but the original systemd process will pass its log target in through a |
345 | * command line argument, which will override this default. Also, ensure we'll never log to the journal or | |
346 | * syslog, as these logging daemons are either already dead or will die very soon. */ | |
347 | ||
348 | log_set_target(LOG_TARGET_CONSOLE); | |
349 | log_set_prohibit_ipc(true); | |
b1e90ec5 | 350 | log_parse_environment(); |
e18805fb | 351 | |
f975f1cc AZ |
352 | if (getpid_cached() == 1) |
353 | log_set_always_reopen_console(true); | |
354 | ||
b1e90ec5 ZJS |
355 | r = parse_argv(argc, argv); |
356 | if (r < 0) | |
357 | goto error; | |
ec26be51 | 358 | |
b1b2a107 FF |
359 | log_open(); |
360 | ||
4c12626c LP |
361 | umask(0022); |
362 | ||
df0ff127 | 363 | if (getpid_cached() != 1) { |
731e5cda | 364 | r = log_error_errno(SYNTHETIC_ERRNO(EPERM), "Not executed by init (PID 1)."); |
b1b2a107 FF |
365 | goto error; |
366 | } | |
367 | ||
b1e90ec5 | 368 | if (streq(arg_verb, "reboot")) |
b1b2a107 | 369 | cmd = RB_AUTOBOOT; |
b1e90ec5 | 370 | else if (streq(arg_verb, "poweroff")) |
b1b2a107 | 371 | cmd = RB_POWER_OFF; |
b1e90ec5 | 372 | else if (streq(arg_verb, "halt")) |
b1b2a107 | 373 | cmd = RB_HALT_SYSTEM; |
b1e90ec5 | 374 | else if (streq(arg_verb, "kexec")) |
b1b2a107 | 375 | cmd = LINUX_REBOOT_CMD_KEXEC; |
287419c1 AC |
376 | else if (streq(arg_verb, "exit")) |
377 | cmd = 0; /* ignored, just checking that arg_verb is valid */ | |
b1b2a107 | 378 | else { |
731e5cda | 379 | r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown action '%s'.", arg_verb); |
b1b2a107 FF |
380 | goto error; |
381 | } | |
382 | ||
0b9aa270 | 383 | (void) cg_get_root_path(&cgroup); |
5125b677 | 384 | bool in_container = detect_container() > 0; |
41f85451 | 385 | |
9a75c652 LP |
386 | /* If the logging messages are going to KMSG, and if we are not running from a container, then try to |
387 | * update the sysctl kernel.printk current value in order to see "info" messages; This current log | |
388 | * level is not updated if already big enough. | |
827156b3 | 389 | */ |
9a75c652 LP |
390 | if (!in_container && |
391 | IN_SET(log_get_target(), | |
392 | LOG_TARGET_AUTO, | |
393 | LOG_TARGET_JOURNAL_OR_KMSG, | |
394 | LOG_TARGET_SYSLOG_OR_KMSG, | |
395 | LOG_TARGET_KMSG)) | |
396 | bump_sysctl_printk_log_level(LOG_WARNING); | |
827156b3 | 397 | |
484752e2 | 398 | init_watchdog(); |
5bbf2db1 | 399 | |
2e79d182 | 400 | /* Lock us into memory */ |
e18805fb | 401 | (void) mlockall(MCL_CURRENT|MCL_FUTURE); |
b1b2a107 | 402 | |
2e79d182 LP |
403 | /* Synchronize everything that is not written to disk yet at this point already. This is a good idea so that |
404 | * slow IO is processed here already and the final process killing spree is not impacted by processes | |
73ad712f KW |
405 | * desperately trying to sync IO to disk within their timeout. Do not remove this sync, data corruption will |
406 | * result. */ | |
2e79d182 | 407 | if (!in_container) |
73ad712f | 408 | sync_with_progress(); |
2e79d182 | 409 | |
e557b1a6 | 410 | disable_coredumps(); |
0282c028 | 411 | disable_binfmt(); |
27b372c1 | 412 | |
ab58e291 | 413 | log_info("Sending SIGTERM to remaining processes..."); |
e73c54b8 | 414 | broadcast_signal(SIGTERM, true, true, arg_timeout); |
b1b2a107 | 415 | |
ab58e291 | 416 | log_info("Sending SIGKILL to remaining processes..."); |
e73c54b8 | 417 | broadcast_signal(SIGKILL, true, false, arg_timeout); |
40e85d00 | 418 | |
5125b677 JJ |
419 | bool need_umount = !in_container, need_swapoff = !in_container, need_loop_detach = !in_container, |
420 | need_dm_detach = !in_container, need_md_detach = !in_container, can_initrd, last_try = false; | |
456b2199 | 421 | can_initrd = !in_container && !in_initrd() && access("/run/initramfs/shutdown", X_OK) == 0; |
b1b2a107 | 422 | |
567ea02a | 423 | /* Unmount all mountpoints, swaps, and loopback devices */ |
ac9cea5b | 424 | for (;;) { |
12aad1d0 LP |
425 | bool changed = false; |
426 | ||
5bbf2db1 | 427 | (void) watchdog_ping(); |
e96d6be7 | 428 | |
731e5cda LP |
429 | /* Let's trim the cgroup tree on each iteration so that we leave an empty cgroup tree around, |
430 | * so that container managers get a nice notify event when we are down */ | |
41f85451 | 431 | if (cgroup) |
3a736a32 | 432 | (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, cgroup, false); |
41f85451 | 433 | |
b1b2a107 | 434 | if (need_umount) { |
ab58e291 | 435 | log_info("Unmounting file systems."); |
5125b677 | 436 | r = umount_all(&changed, last_try); |
bce93b7a | 437 | if (r == 0) { |
b1b2a107 | 438 | need_umount = false; |
bce93b7a MS |
439 | log_info("All filesystems unmounted."); |
440 | } else if (r > 0) | |
ab58e291 | 441 | log_info("Not all file systems unmounted, %d left.", r); |
b1b2a107 | 442 | else |
da927ba9 | 443 | log_error_errno(r, "Failed to unmount file systems: %m"); |
b1b2a107 FF |
444 | } |
445 | ||
446 | if (need_swapoff) { | |
735e0712 | 447 | log_info("Deactivating swaps."); |
12aad1d0 | 448 | r = swapoff_all(&changed); |
bce93b7a | 449 | if (r == 0) { |
b1b2a107 | 450 | need_swapoff = false; |
735e0712 | 451 | log_info("All swaps deactivated."); |
bce93b7a | 452 | } else if (r > 0) |
735e0712 | 453 | log_info("Not all swaps deactivated, %d left.", r); |
b1b2a107 | 454 | else |
da927ba9 | 455 | log_error_errno(r, "Failed to deactivate swaps: %m"); |
b1b2a107 FF |
456 | } |
457 | ||
458 | if (need_loop_detach) { | |
459 | log_info("Detaching loop devices."); | |
5125b677 | 460 | r = loopback_detach_all(&changed, last_try); |
bce93b7a | 461 | if (r == 0) { |
b1b2a107 | 462 | need_loop_detach = false; |
bce93b7a MS |
463 | log_info("All loop devices detached."); |
464 | } else if (r > 0) | |
ab58e291 | 465 | log_info("Not all loop devices detached, %d left.", r); |
b1b2a107 | 466 | else |
da927ba9 | 467 | log_error_errno(r, "Failed to detach loop devices: %m"); |
d48141ba | 468 | } |
b1b2a107 | 469 | |
0b220a5f HK |
470 | if (need_md_detach) { |
471 | log_info("Stopping MD devices."); | |
5125b677 | 472 | r = md_detach_all(&changed, last_try); |
0b220a5f HK |
473 | if (r == 0) { |
474 | need_md_detach = false; | |
475 | log_info("All MD devices stopped."); | |
476 | } else if (r > 0) | |
477 | log_info("Not all MD devices stopped, %d left.", r); | |
478 | else | |
479 | log_error_errno(r, "Failed to stop MD devices: %m"); | |
480 | } | |
481 | ||
d48141ba LP |
482 | if (need_dm_detach) { |
483 | log_info("Detaching DM devices."); | |
5125b677 | 484 | r = dm_detach_all(&changed, last_try); |
bce93b7a | 485 | if (r == 0) { |
d48141ba | 486 | need_dm_detach = false; |
bce93b7a MS |
487 | log_info("All DM devices detached."); |
488 | } else if (r > 0) | |
2569a5ce | 489 | log_info("Not all DM devices detached, %d left.", r); |
d48141ba | 490 | else |
da927ba9 | 491 | log_error_errno(r, "Failed to detach DM devices: %m"); |
b1b2a107 FF |
492 | } |
493 | ||
0b220a5f HK |
494 | if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach |
495 | && !need_md_detach) { | |
496 | log_info("All filesystems, swaps, loop devices, MD devices and DM devices detached."); | |
12aad1d0 | 497 | /* Yay, done */ |
ac9cea5b | 498 | break; |
a27d2184 | 499 | } |
b1b2a107 | 500 | |
13d9616c | 501 | if (!changed && !last_try && !can_initrd) { |
5125b677 JJ |
502 | /* There are things we cannot get rid of. Loop one more time in which we will log |
503 | * with higher priority to inform the user. Note that we don't need to do this if | |
504 | * there is an initrd to switch to, because that one is likely to get rid of the | |
505 | * remaining mounts. If not, it will log about them. */ | |
506 | last_try = true; | |
456b2199 JJ |
507 | continue; |
508 | } | |
509 | ||
731e5cda | 510 | /* If in this iteration we didn't manage to unmount/deactivate anything, we simply give up */ |
12aad1d0 | 511 | if (!changed) { |
0b220a5f | 512 | log_info("Cannot finalize remaining%s%s%s%s%s continuing.", |
8c977838 ZJS |
513 | need_umount ? " file systems," : "", |
514 | need_swapoff ? " swap devices," : "", | |
515 | need_loop_detach ? " loop devices," : "", | |
0b220a5f HK |
516 | need_dm_detach ? " DM devices," : "", |
517 | need_md_detach ? " MD devices," : ""); | |
ac9cea5b | 518 | break; |
12aad1d0 LP |
519 | } |
520 | ||
0b220a5f | 521 | log_debug("Couldn't finalize remaining %s%s%s%s%s trying again.", |
8c977838 ZJS |
522 | need_umount ? " file systems," : "", |
523 | need_swapoff ? " swap devices," : "", | |
524 | need_loop_detach ? " loop devices," : "", | |
0b220a5f HK |
525 | need_dm_detach ? " DM devices," : "", |
526 | need_md_detach ? " MD devices," : ""); | |
b1b2a107 FF |
527 | } |
528 | ||
731e5cda LP |
529 | /* We're done with the watchdog. Note that the watchdog is explicitly not stopped here. It remains |
530 | * active to guard against any issues during the rest of the shutdown sequence. */ | |
8a2c1fbf EJ |
531 | watchdog_free_device(); |
532 | ||
731e5cda | 533 | arguments[0] = NULL; /* Filled in by execute_directories(), when needed */ |
b1e90ec5 | 534 | arguments[1] = arg_verb; |
6edd7d0a | 535 | arguments[2] = NULL; |
3a736a32 | 536 | (void) execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS); |
83cc030f | 537 | |
595225af LP |
538 | (void) rlimit_nofile_safe(); |
539 | ||
456b2199 | 540 | if (can_initrd) { |
5a4bf02f HH |
541 | r = switch_root_initramfs(); |
542 | if (r >= 0) { | |
a2726e5c | 543 | argv[0] = (char*) "/shutdown"; |
30d743f4 | 544 | |
ece0fe12 LP |
545 | (void) setsid(); |
546 | (void) make_console_stdio(); | |
5a4bf02f HH |
547 | |
548 | log_info("Successfully changed into root pivot.\n" | |
549 | "Returning to initrd..."); | |
30d743f4 | 550 | |
a2726e5c | 551 | execv("/shutdown", argv); |
56f64d95 | 552 | log_error_errno(errno, "Failed to execute shutdown binary: %m"); |
5a4bf02f | 553 | } else |
da927ba9 | 554 | log_error_errno(r, "Failed to switch root to \"/run/initramfs\": %m"); |
7cb1094a HH |
555 | } |
556 | ||
0b220a5f HK |
557 | if (need_umount || need_swapoff || need_loop_detach || need_dm_detach || need_md_detach) |
558 | log_error("Failed to finalize%s%s%s%s%s ignoring.", | |
8c977838 ZJS |
559 | need_umount ? " file systems," : "", |
560 | need_swapoff ? " swap devices," : "", | |
561 | need_loop_detach ? " loop devices," : "", | |
0b220a5f HK |
562 | need_dm_detach ? " DM devices," : "", |
563 | need_md_detach ? " MD devices," : ""); | |
8c977838 | 564 | |
731e5cda LP |
565 | /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need |
566 | * to be sync'ed explicitly in advance. So let's do this here, but not needlessly slow down | |
567 | * containers. Note that we sync'ed things already once above, but we did some more work since then | |
568 | * which might have caused IO, hence let's do it once more. Do not remove this sync, data corruption | |
569 | * will result. */ | |
0049f05a | 570 | if (!in_container) |
73ad712f | 571 | sync_with_progress(); |
0049f05a | 572 | |
287419c1 | 573 | if (streq(arg_verb, "exit")) { |
016f36ae AZ |
574 | if (in_container) { |
575 | log_info("Exiting container."); | |
1f409a0c | 576 | return arg_exit_code; |
016f36ae | 577 | } |
1f409a0c LP |
578 | |
579 | cmd = RB_POWER_OFF; /* We cannot exit() on the host, fallback on another method. */ | |
287419c1 AC |
580 | } |
581 | ||
477def80 LP |
582 | switch (cmd) { |
583 | ||
584 | case LINUX_REBOOT_CMD_KEXEC: | |
cb7ec564 LP |
585 | |
586 | if (!in_container) { | |
587 | /* We cheat and exec kexec to avoid doing all its work */ | |
477def80 | 588 | log_info("Rebooting with kexec."); |
cb7ec564 | 589 | |
1f5d1e02 | 590 | r = safe_fork("(sd-kexec)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_WAIT, NULL); |
4c253ed1 | 591 | if (r == 0) { |
477def80 LP |
592 | const char * const args[] = { |
593 | KEXEC, "-e", NULL | |
594 | }; | |
595 | ||
cb7ec564 | 596 | /* Child */ |
477def80 | 597 | |
cb7ec564 | 598 | execv(args[0], (char * const *) args); |
731e5cda | 599 | log_debug_errno(errno, "Failed to execute '" KEXEC "' binary, proceeding with reboot(RB_KEXEC): %m"); |
71180f8e A |
600 | |
601 | /* execv failed (kexec binary missing?), so try simply reboot(RB_KEXEC) */ | |
602 | (void) reboot(cmd); | |
477def80 | 603 | _exit(EXIT_FAILURE); |
4c253ed1 LP |
604 | } |
605 | ||
1f5d1e02 | 606 | /* If we are still running, then the kexec can't have worked, let's fall through */ |
b1b2a107 | 607 | } |
e61cd186 LP |
608 | |
609 | cmd = RB_AUTOBOOT; | |
4831981d | 610 | _fallthrough_; |
477def80 | 611 | |
c01dcddf LP |
612 | case RB_AUTOBOOT: |
613 | (void) reboot_with_parameter(REBOOT_LOG); | |
477def80 LP |
614 | log_info("Rebooting."); |
615 | break; | |
616 | ||
617 | case RB_POWER_OFF: | |
618 | log_info("Powering off."); | |
619 | break; | |
620 | ||
621 | case RB_HALT_SYSTEM: | |
622 | log_info("Halting system."); | |
623 | break; | |
624 | ||
625 | default: | |
04499a70 | 626 | assert_not_reached(); |
477def80 | 627 | } |
cb7ec564 | 628 | |
118cf952 | 629 | (void) reboot(cmd); |
731e5cda LP |
630 | if (ERRNO_IS_PRIVILEGE(errno) && in_container) { |
631 | /* If we are in a container, and we lacked CAP_SYS_BOOT just exit, this will kill our | |
cb7ec564 | 632 | * container for good. */ |
477def80 | 633 | log_info("Exiting container."); |
1f409a0c | 634 | return EXIT_SUCCESS; |
cb7ec564 LP |
635 | } |
636 | ||
76ef789d | 637 | r = log_error_errno(errno, "Failed to invoke reboot(): %m"); |
b1b2a107 FF |
638 | |
639 | error: | |
da927ba9 | 640 | log_emergency_errno(r, "Critical error while doing system shutdown: %m"); |
b1b2a107 | 641 | freeze(); |
b1b2a107 | 642 | } |