1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 ProFUSION embedded systems
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/reboot.h>
28 #include <sys/mount.h>
29 #include <sys/reboot.h>
33 #include "alloc-util.h"
34 #include "cgroup-util.h"
36 #include "exec-util.h"
41 #include "parse-util.h"
42 #include "process-util.h"
43 #include "string-util.h"
44 #include "switch-root.h"
45 #include "terminal-util.h"
51 #define FINALIZE_ATTEMPTS 50
53 static char* arg_verb
;
54 static uint8_t arg_exit_code
;
56 static int parse_argv(int argc
, char *argv
[]) {
58 ARG_LOG_LEVEL
= 0x100,
65 static const struct option options
[] = {
66 { "log-level", required_argument
, NULL
, ARG_LOG_LEVEL
},
67 { "log-target", required_argument
, NULL
, ARG_LOG_TARGET
},
68 { "log-color", optional_argument
, NULL
, ARG_LOG_COLOR
},
69 { "log-location", optional_argument
, NULL
, ARG_LOG_LOCATION
},
70 { "exit-code", required_argument
, NULL
, ARG_EXIT_CODE
},
79 /* "-" prevents getopt from permuting argv[] and moving the verb away
80 * from argv[1]. Our interface to initrd promises it'll be there. */
81 while ((c
= getopt_long(argc
, argv
, "-", options
, NULL
)) >= 0)
85 r
= log_set_max_level_from_string(optarg
);
87 log_error("Failed to parse log level %s, ignoring.", optarg
);
92 r
= log_set_target_from_string(optarg
);
94 log_error("Failed to parse log target %s, ignoring", optarg
);
101 r
= log_show_color_from_string(optarg
);
103 log_error("Failed to parse log color setting %s, ignoring", optarg
);
105 log_show_color(true);
109 case ARG_LOG_LOCATION
:
111 r
= log_show_location_from_string(optarg
);
113 log_error("Failed to parse log location setting %s, ignoring", optarg
);
115 log_show_location(true);
120 r
= safe_atou8(optarg
, &arg_exit_code
);
122 log_error("Failed to parse exit code %s, ignoring", optarg
);
130 log_error("Excess arguments, ignoring");
137 assert_not_reached("Unhandled option code.");
141 log_error("Verb argument missing.");
148 static int switch_root_initramfs(void) {
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");
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");
155 /* switch_root with MS_BIND, because there might still be processes lurking around, which have open file descriptors.
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.
159 return switch_root("/run/initramfs", "/oldroot", false, MS_BIND
);
162 int main(int argc
, char *argv
[]) {
163 bool need_umount
, need_swapoff
, need_loop_detach
, need_dm_detach
;
164 bool in_container
, use_watchdog
= false;
165 _cleanup_free_
char *cgroup
= NULL
;
169 static const char* const dirs
[] = {SYSTEM_SHUTDOWN_PATH
, NULL
};
170 char *watchdog_device
;
172 log_parse_environment();
173 r
= parse_argv(argc
, argv
);
177 /* journald will die if not gone yet. The log target defaults
178 * to console, but may have been changed by command line options. */
180 log_close_console(); /* force reopen of /dev/console */
185 if (getpid_cached() != 1) {
186 log_error("Not executed by init (PID 1).");
191 if (streq(arg_verb
, "reboot"))
193 else if (streq(arg_verb
, "poweroff"))
195 else if (streq(arg_verb
, "halt"))
196 cmd
= RB_HALT_SYSTEM
;
197 else if (streq(arg_verb
, "kexec"))
198 cmd
= LINUX_REBOOT_CMD_KEXEC
;
199 else if (streq(arg_verb
, "exit"))
200 cmd
= 0; /* ignored, just checking that arg_verb is valid */
203 log_error("Unknown action '%s'.", arg_verb
);
207 (void) cg_get_root_path(&cgroup
);
208 in_container
= detect_container() > 0;
210 use_watchdog
= !!getenv("WATCHDOG_USEC");
211 watchdog_device
= getenv("WATCHDOG_DEVICE");
212 if (watchdog_device
) {
213 r
= watchdog_set_device(watchdog_device
);
215 log_warning_errno(r
, "Failed to set watchdog device to %s, ignoring: %m",
219 /* Lock us into memory */
220 mlockall(MCL_CURRENT
|MCL_FUTURE
);
222 /* Synchronize everything that is not written to disk yet at this point already. This is a good idea so that
223 * slow IO is processed here already and the final process killing spree is not impacted by processes
224 * desperately trying to sync IO to disk within their timeout. */
228 log_info("Sending SIGTERM to remaining processes...");
229 broadcast_signal(SIGTERM
, true, true);
231 log_info("Sending SIGKILL to remaining processes...");
232 broadcast_signal(SIGKILL
, true, false);
234 need_umount
= !in_container
;
235 need_swapoff
= !in_container
;
236 need_loop_detach
= !in_container
;
237 need_dm_detach
= !in_container
;
239 /* Unmount all mountpoints, swaps, and loopback devices */
240 for (retries
= 0; retries
< FINALIZE_ATTEMPTS
; retries
++) {
241 bool changed
= false;
246 /* Let's trim the cgroup tree on each iteration so
247 that we leave an empty cgroup tree around, so that
248 container managers get a nice notify event when we
251 cg_trim(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, false);
254 log_info("Unmounting file systems.");
255 r
= umount_all(&changed
);
258 log_info("All filesystems unmounted.");
260 log_info("Not all file systems unmounted, %d left.", r
);
262 log_error_errno(r
, "Failed to unmount file systems: %m");
266 log_info("Deactivating swaps.");
267 r
= swapoff_all(&changed
);
269 need_swapoff
= false;
270 log_info("All swaps deactivated.");
272 log_info("Not all swaps deactivated, %d left.", r
);
274 log_error_errno(r
, "Failed to deactivate swaps: %m");
277 if (need_loop_detach
) {
278 log_info("Detaching loop devices.");
279 r
= loopback_detach_all(&changed
);
281 need_loop_detach
= false;
282 log_info("All loop devices detached.");
284 log_info("Not all loop devices detached, %d left.", r
);
286 log_error_errno(r
, "Failed to detach loop devices: %m");
289 if (need_dm_detach
) {
290 log_info("Detaching DM devices.");
291 r
= dm_detach_all(&changed
);
293 need_dm_detach
= false;
294 log_info("All DM devices detached.");
296 log_info("Not all DM devices detached, %d left.", r
);
298 log_error_errno(r
, "Failed to detach DM devices: %m");
301 if (!need_umount
&& !need_swapoff
&& !need_loop_detach
&& !need_dm_detach
) {
303 log_info("All filesystems, swaps, loop devices, DM devices detached.");
308 /* If in this iteration we didn't manage to
309 * unmount/deactivate anything, we simply give up */
311 log_info("Cannot finalize remaining%s%s%s%s continuing.",
312 need_umount
? " file systems," : "",
313 need_swapoff
? " swap devices," : "",
314 need_loop_detach
? " loop devices," : "",
315 need_dm_detach
? " DM devices," : "");
319 log_debug("After %u retries, couldn't finalize remaining %s%s%s%s trying again.",
321 need_umount
? " file systems," : "",
322 need_swapoff
? " swap devices," : "",
323 need_loop_detach
? " loop devices," : "",
324 need_dm_detach
? " DM devices," : "");
327 log_error("Too many iterations, giving up.");
331 /* We're done with the watchdog. */
332 watchdog_free_device();
335 arguments
[1] = arg_verb
;
337 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
);
339 if (!in_container
&& !in_initrd() &&
340 access("/run/initramfs/shutdown", X_OK
) == 0) {
341 r
= switch_root_initramfs();
343 argv
[0] = (char*) "/shutdown";
346 make_console_stdio();
348 log_info("Successfully changed into root pivot.\n"
349 "Returning to initrd...");
351 execv("/shutdown", argv
);
352 log_error_errno(errno
, "Failed to execute shutdown binary: %m");
354 log_error_errno(r
, "Failed to switch root to \"/run/initramfs\": %m");
358 if (need_umount
|| need_swapoff
|| need_loop_detach
|| need_dm_detach
)
359 log_error("Failed to finalize %s%s%s%s ignoring",
360 need_umount
? " file systems," : "",
361 need_swapoff
? " swap devices," : "",
362 need_loop_detach
? " loop devices," : "",
363 need_dm_detach
? " DM devices," : "");
365 /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be
366 * sync'ed explicitly in advance. So let's do this here, but not needlessly slow down containers. Note that we
367 * sync'ed things already once above, but we did some more work since then which might have caused IO, hence
368 * let's doit once more. */
372 if (streq(arg_verb
, "exit")) {
376 /* We cannot exit() on the host, fallback on another
384 case LINUX_REBOOT_CMD_KEXEC
:
387 /* We cheat and exec kexec to avoid doing all its work */
390 log_info("Rebooting with kexec.");
394 log_error_errno(errno
, "Failed to fork: %m");
397 const char * const args
[] = {
403 execv(args
[0], (char * const *) args
);
406 wait_for_terminate_and_warn("kexec", pid
, true);
414 _cleanup_free_
char *param
= NULL
;
416 r
= read_one_line_file("/run/systemd/reboot-param", ¶m
);
417 if (r
< 0 && r
!= -ENOENT
)
418 log_warning_errno(r
, "Failed to read reboot parameter file: %m");
420 if (!isempty(param
)) {
421 log_info("Rebooting with argument '%s'.", param
);
422 syscall(SYS_reboot
, LINUX_REBOOT_MAGIC1
, LINUX_REBOOT_MAGIC2
, LINUX_REBOOT_CMD_RESTART2
, param
);
423 log_warning_errno(errno
, "Failed to reboot with parameter, retrying without: %m");
427 log_info("Rebooting.");
431 log_info("Powering off.");
435 log_info("Halting system.");
439 assert_not_reached("Unknown magic");
443 if (errno
== EPERM
&& in_container
) {
444 /* If we are in a container, and we lacked
445 * CAP_SYS_BOOT just exit, this will kill our
446 * container for good. */
447 log_info("Exiting container.");
451 r
= log_error_errno(errno
, "Failed to invoke reboot(): %m");
454 log_emergency_errno(r
, "Critical error while doing system shutdown: %m");