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