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