]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/reboot-util.c
namespace-util: introduce helper for combining unshare() + MS_SLAVE remount
[thirdparty/systemd.git] / src / shared / reboot-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "fileio.h"
8 #include "log.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"
14 #include "virt.h"
15
16 int update_reboot_parameter_and_warn(const char *parameter, bool keep) {
17 int r;
18
19 if (isempty(parameter)) {
20 if (keep)
21 return 0;
22
23 if (unlink("/run/systemd/reboot-param") < 0) {
24 if (errno == ENOENT)
25 return 0;
26
27 return log_warning_errno(errno, "Failed to unlink reboot parameter file: %m");
28 }
29
30 return 0;
31 }
32
33 RUN_WITH_UMASK(0022) {
34 r = write_string_file("/run/systemd/reboot-param", parameter,
35 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
36 if (r < 0)
37 return log_warning_errno(r, "Failed to write reboot parameter file: %m");
38 }
39
40 return 0;
41 }
42
43 int read_reboot_parameter(char **parameter) {
44 int r;
45
46 assert(parameter);
47
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");
51
52 return 0;
53 }
54
55 int reboot_with_parameter(RebootFlags flags) {
56 int r;
57
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. */
64
65 if (detect_container() == 0) {
66 _cleanup_free_ char *parameter = NULL;
67
68 r = read_one_line_file("/run/systemd/reboot-param", &parameter);
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");
72
73 if (!isempty(parameter)) {
74 log_full(flags & REBOOT_LOG ? LOG_INFO : LOG_DEBUG,
75 "Rebooting with argument '%s'.", parameter);
76
77 if (flags & REBOOT_DRY_RUN)
78 return 0;
79
80 (void) raw_reboot(LINUX_REBOOT_CMD_RESTART2, parameter);
81
82 log_full_errno(flags & REBOOT_LOG ? LOG_WARNING : LOG_DEBUG, errno,
83 "Failed to reboot with parameter, retrying without: %m");
84 }
85 }
86
87 if (!(flags & REBOOT_FALLBACK))
88 return 0;
89
90 log_full(flags & REBOOT_LOG ? LOG_INFO : LOG_DEBUG, "Rebooting.");
91
92 if (flags & REBOOT_DRY_RUN)
93 return 0;
94
95 (void) reboot(RB_AUTOBOOT);
96
97 return log_full_errno(flags & REBOOT_LOG ? LOG_ERR : LOG_DEBUG, errno, "Failed to reboot: %m");
98 }
99
100 int shall_restore_state(void) {
101 bool ret;
102 int r;
103
104 r = proc_cmdline_get_bool("systemd.restore_state", &ret);
105 if (r < 0)
106 return r;
107
108 return r > 0 ? ret : true;
109 }