• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 16280725298

14 Jul 2025 08:16PM UTC coverage: 72.166% (-0.006%) from 72.172%
16280725298

push

github

web-flow
Two fixlets for coverage test (#38183)

302135 of 418667 relevant lines covered (72.17%)

773261.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.61
/src/shared/reboot-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdint.h>
4
#include <sys/ioctl.h>
5
#include <sys/mman.h>
6
#include <sys/syscall.h>
7
#include <unistd.h>
8

9
#if HAVE_XENCTRL
10
#define __XEN_INTERFACE_VERSION__ 0x00040900
11
#include <xen/kexec.h>
12
#include <xen/sys/privcmd.h>
13
#include <xen/xen.h>
14
#endif
15

16
#include "alloc-util.h"
17
#include "errno-util.h"
18
#include "fd-util.h"
19
#include "fileio.h"
20
#include "log.h"
21
#include "proc-cmdline.h"
22
#include "reboot-util.h"
23
#include "string-util.h"
24
#include "umask-util.h"
25
#include "utf8.h"
26
#include "virt.h"
27

28
int raw_reboot(int cmd, const void *arg) {
×
29
        return syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, arg);
×
30
}
31

32
bool reboot_parameter_is_valid(const char *parameter) {
×
33
        assert(parameter);
×
34

35
        return ascii_is_valid(parameter) && strlen(parameter) <= NAME_MAX;
×
36
}
37

38
int update_reboot_parameter_and_warn(const char *parameter, bool keep) {
20✔
39
        int r;
20✔
40

41
        if (isempty(parameter)) {
20✔
42
                if (keep)
20✔
43
                        return 0;
44

45
                if (unlink("/run/systemd/reboot-param") < 0) {
5✔
46
                        if (errno == ENOENT)
5✔
47
                                return 0;
48

49
                        return log_warning_errno(errno, "Failed to unlink reboot parameter file: %m");
×
50
                }
51

52
                return 0;
53
        }
54

55
        if (!reboot_parameter_is_valid(parameter))
×
56
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid reboot parameter '%s'.", parameter);
×
57

58
        WITH_UMASK(0022) {
×
59
                r = write_string_file("/run/systemd/reboot-param", parameter,
×
60
                                      WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
61
                if (r < 0)
×
62
                        return log_warning_errno(r, "Failed to write reboot parameter file: %m");
×
63
        }
64

65
        return 0;
×
66
}
67

68
int read_reboot_parameter(char **parameter) {
×
69
        int r;
×
70

71
        assert(parameter);
×
72

73
        r = read_one_line_file("/run/systemd/reboot-param", parameter);
×
74
        if (r < 0 && r != -ENOENT)
×
75
                return log_debug_errno(r, "Failed to read /run/systemd/reboot-param: %m");
×
76

77
        return 0;
78
}
79

80
int reboot_with_parameter(RebootFlags flags) {
1✔
81
        int r;
1✔
82

83
        /* Reboots the system with a parameter that is read from /run/systemd/reboot-param. Returns 0 if
84
         * REBOOT_DRY_RUN was set and the actual reboot operation was hence skipped. If REBOOT_FALLBACK is
85
         * set and the reboot with parameter doesn't work out a fallback to classic reboot() is attempted. If
86
         * REBOOT_FALLBACK is not set, 0 is returned instead, which should be considered indication for the
87
         * caller to fall back to reboot() on its own, or somehow else deal with this. If REBOOT_LOG is
88
         * specified will log about what it is going to do, as well as all errors. */
89

90
        if (detect_container() == 0) {
1✔
91
                _cleanup_free_ char *parameter = NULL;
×
92

93
                r = read_one_line_file("/run/systemd/reboot-param", &parameter);
×
94
                if (r < 0 && r != -ENOENT)
×
95
                        log_full_errno(flags & REBOOT_LOG ? LOG_WARNING : LOG_DEBUG, r,
×
96
                                       "Failed to read reboot parameter file, ignoring: %m");
97

98
                if (!isempty(parameter)) {
×
99
                        log_full(flags & REBOOT_LOG ? LOG_INFO : LOG_DEBUG,
×
100
                                 "Rebooting with argument '%s'.", parameter);
101

102
                        if (flags & REBOOT_DRY_RUN)
×
103
                                return 0;
×
104

105
                        (void) raw_reboot(LINUX_REBOOT_CMD_RESTART2, parameter);
×
106

107
                        log_full_errno(flags & REBOOT_LOG ? LOG_WARNING : LOG_DEBUG, errno,
×
108
                                       "Failed to reboot with parameter, retrying without: %m");
109
                }
110
        }
111

112
        if (!(flags & REBOOT_FALLBACK))
1✔
113
                return 0;
114

115
        log_full(flags & REBOOT_LOG ? LOG_INFO : LOG_DEBUG, "Rebooting.");
1✔
116

117
        if (flags & REBOOT_DRY_RUN)
1✔
118
                return 0;
119

120
        (void) reboot(RB_AUTOBOOT);
×
121

122
        return log_full_errno(flags & REBOOT_LOG ? LOG_ERR : LOG_DEBUG, errno, "Failed to reboot: %m");
×
123
}
124

125
bool shall_restore_state(void) {
2✔
126
        static int cached = -1;
2✔
127
        bool b = true; /* If nothing specified or the check fails, then defaults to true. */
2✔
128
        int r;
2✔
129

130
        if (cached >= 0)
2✔
131
                return cached;
1✔
132

133
        r = proc_cmdline_get_bool("systemd.restore_state", PROC_CMDLINE_TRUE_WHEN_MISSING, &b);
1✔
134
        if (r < 0)
1✔
135
                log_debug_errno(r, "Failed to parse systemd.restore_state= kernel command line option, ignoring: %m");
×
136

137
        return (cached = b);
1✔
138
}
139

140
static int xen_kexec_loaded(void) {
20✔
141
#if HAVE_XENCTRL
142
        _cleanup_close_ int privcmd_fd = -EBADF, buf_fd = -EBADF;
143
        xen_kexec_status_t *buffer;
144
        size_t size;
145
        int r;
146

147
        if (access("/proc/xen", F_OK) < 0) {
148
                if (errno == ENOENT)
149
                        return -EOPNOTSUPP;
150
                return log_debug_errno(errno, "Unable to test whether /proc/xen exists: %m");
151
        }
152

153
        size = page_size();
154
        if (sizeof(xen_kexec_status_t) > size)
155
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "page_size is too small for hypercall");
156

157
        privcmd_fd = open("/dev/xen/privcmd", O_RDWR|O_CLOEXEC);
158
        if (privcmd_fd < 0)
159
                return log_debug_errno(errno, "Cannot access /dev/xen/privcmd: %m");
160

161
        buf_fd = open("/dev/xen/hypercall", O_RDWR|O_CLOEXEC);
162
        if (buf_fd < 0)
163
                return log_debug_errno(errno, "Cannot access /dev/xen/hypercall: %m");
164

165
        buffer = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, buf_fd, 0);
166
        if (buffer == MAP_FAILED)
167
                return log_debug_errno(errno, "Cannot allocate buffer for hypercall: %m");
168

169
        *buffer = (xen_kexec_status_t) {
170
                .type = KEXEC_TYPE_DEFAULT,
171
        };
172

173
        privcmd_hypercall_t call = {
174
                .op = __HYPERVISOR_kexec_op,
175
                .arg = {
176
                        KEXEC_CMD_kexec_status,
177
                        PTR_TO_UINT64(buffer),
178
                },
179
        };
180

181
        r = RET_NERRNO(ioctl(privcmd_fd, IOCTL_PRIVCMD_HYPERCALL, &call));
182
        if (r < 0)
183
                log_debug_errno(r, "kexec_status failed: %m");
184

185
        munmap(buffer, size);
186

187
        return r;
188
#else
189
        return -EOPNOTSUPP;
20✔
190
#endif
191
}
192

193
bool kexec_loaded(void) {
20✔
194
       _cleanup_free_ char *s = NULL;
20✔
195
       int r;
20✔
196

197
       r = xen_kexec_loaded();
20✔
198
       if (r >= 0)
20✔
199
               return r;
×
200

201
       r = read_one_line_file("/sys/kernel/kexec_loaded", &s);
20✔
202
       if (r < 0) {
20✔
203
               if (r != -ENOENT)
×
204
                       log_debug_errno(r, "Unable to read /sys/kernel/kexec_loaded, ignoring: %m");
×
205
               return false;
×
206
       }
207

208
       return s[0] == '1';
20✔
209
}
210

211
int create_shutdown_run_nologin_or_warn(void) {
107✔
212
        int r;
107✔
213

214
        /* This is used twice: once in systemd-user-sessions.service, in order to block logins when we
215
         * actually go down, and once in systemd-logind.service when shutdowns are scheduled, and logins are
216
         * to be turned off a bit in advance. We use the same wording of the message in both cases.
217
         *
218
         * Traditionally, there was only /etc/nologin, and we managed that. Then, in PAM 1.1
219
         * support for /run/nologin was added as alternative
220
         * (https://github.com/linux-pam/linux-pam/commit/e9e593f6ddeaf975b7fe8446d184e6bc387d450b).
221
         * 13 years later we stopped managing /etc/nologin, leaving it for the administrator to manage.
222
         */
223

224
        r = write_string_file("/run/nologin",
107✔
225
                              "System is going down. Unprivileged users are not permitted to log in anymore. "
226
                              "For technical details, see pam_nologin(8).",
227
                              WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
228
        if (r < 0)
107✔
229
                return log_error_errno(r, "Failed to create /run/nologin: %m");
×
230

231
        return 0;
232
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc