1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
9 #include "proc-cmdline.h"
10 #include "raw-reboot.h"
11 #include "reboot-util.h"
12 #include "string-util.h"
13 #include "umask-util.h"
16 int update_reboot_parameter_and_warn(const char *parameter
, bool keep
) {
19 if (isempty(parameter
)) {
23 if (unlink("/run/systemd/reboot-param") < 0) {
27 return log_warning_errno(errno
, "Failed to unlink reboot parameter file: %m");
33 RUN_WITH_UMASK(0022) {
34 r
= write_string_file("/run/systemd/reboot-param", parameter
,
35 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
);
37 return log_warning_errno(r
, "Failed to write reboot parameter file: %m");
43 int read_reboot_parameter(char **parameter
) {
48 r
= read_one_line_file("/run/systemd/reboot-param", parameter
);
49 if (r
< 0 && r
!= -ENOENT
)
50 return log_debug_errno(r
, "Failed to read /run/systemd/reboot-param: %m");
55 int reboot_with_parameter(RebootFlags flags
) {
58 /* Reboots the system with a parameter that is read from /run/systemd/reboot-param. Returns 0 if
59 * REBOOT_DRY_RUN was set and the actual reboot operation was hence skipped. If REBOOT_FALLBACK is
60 * set and the reboot with parameter doesn't work out a fallback to classic reboot() is attempted. If
61 * REBOOT_FALLBACK is not set, 0 is returned instead, which should be considered indication for the
62 * caller to fall back to reboot() on its own, or somehow else deal with this. If REBOOT_LOG is
63 * specified will log about what it is going to do, as well as all errors. */
65 if (detect_container() == 0) {
66 _cleanup_free_
char *parameter
= NULL
;
68 r
= read_one_line_file("/run/systemd/reboot-param", ¶meter
);
69 if (r
< 0 && r
!= -ENOENT
)
70 log_full_errno(flags
& REBOOT_LOG
? LOG_WARNING
: LOG_DEBUG
, r
,
71 "Failed to read reboot parameter file, ignoring: %m");
73 if (!isempty(parameter
)) {
74 log_full(flags
& REBOOT_LOG
? LOG_INFO
: LOG_DEBUG
,
75 "Rebooting with argument '%s'.", parameter
);
77 if (flags
& REBOOT_DRY_RUN
)
80 (void) raw_reboot(LINUX_REBOOT_CMD_RESTART2
, parameter
);
82 log_full_errno(flags
& REBOOT_LOG
? LOG_WARNING
: LOG_DEBUG
, errno
,
83 "Failed to reboot with parameter, retrying without: %m");
87 if (!(flags
& REBOOT_FALLBACK
))
90 log_full(flags
& REBOOT_LOG
? LOG_INFO
: LOG_DEBUG
, "Rebooting.");
92 if (flags
& REBOOT_DRY_RUN
)
95 (void) reboot(RB_AUTOBOOT
);
97 return log_full_errno(flags
& REBOOT_LOG
? LOG_ERR
: LOG_DEBUG
, errno
, "Failed to reboot: %m");
100 int shall_restore_state(void) {
104 r
= proc_cmdline_get_bool("systemd.restore_state", &ret
);
108 return r
> 0 ? ret
: true;