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

systemd / systemd / 23518499972

24 Mar 2026 09:45PM UTC coverage: 72.567% (-0.01%) from 72.581%
23518499972

push

github

web-flow
resolved: add "static RRs" concept (#41213)

split out of #40980

301 of 337 new or added lines in 9 files covered. (89.32%)

3451 existing lines in 67 files now uncovered.

316989 of 436822 relevant lines covered (72.57%)

1153112.24 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/syscall.h>
5
#include <unistd.h>
6

7
#if HAVE_XENCTRL
8
#include <sys/ioctl.h>
9
#include <sys/mman.h>
10

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

16
#include "errno-util.h"
17
#include "fd-util.h"
18
#endif
19

20
#include "alloc-util.h"
21
#include "fileio.h"
22
#include "log.h"
23
#include "proc-cmdline.h"
24
#include "reboot-util.h"
25
#include "string-util.h"
26
#include "umask-util.h"
27
#include "utf8.h"
28
#include "virt.h"
29

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

UNCOV
34
bool reboot_parameter_is_valid(const char *parameter) {
×
35
        assert(parameter);
×
36

UNCOV
37
        return ascii_is_valid(parameter) && strlen(parameter) <= NAME_MAX;
×
38
}
39

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

43
        if (isempty(parameter)) {
20✔
44
                if (keep)
20✔
45
                        return 0;
46

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

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

54
                return 0;
55
        }
56

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

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

UNCOV
67
        return 0;
×
68
}
69

UNCOV
70
int read_reboot_parameter(char **parameter) {
×
71
        int r;
×
72

73
        assert(parameter);
×
74

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

79
        return 0;
80
}
81

82
int reboot_with_parameter(RebootFlags flags) {
1✔
83
        int r;
1✔
84

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

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

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

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

UNCOV
104
                        if (flags & REBOOT_DRY_RUN)
×
105
                                return 0;
×
106

107
                        (void) raw_reboot(LINUX_REBOOT_CMD_RESTART2, parameter);
×
108

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

114
        if (!(flags & REBOOT_FALLBACK))
1✔
115
                return 0;
116

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

119
        if (flags & REBOOT_DRY_RUN)
1✔
120
                return 0;
121

122
        (void) reboot(RB_AUTOBOOT);
×
123

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

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

132
        if (cached >= 0)
2✔
133
                return cached;
1✔
134

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

139
        return (cached = b);
1✔
140
}
141

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

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

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

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

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

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

171
        *buffer = (xen_kexec_status_t) {
172
                .type = KEXEC_TYPE_DEFAULT,
173
        };
174

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

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

187
        munmap(buffer, size);
188

189
        return r;
190
#else
191
        return -EOPNOTSUPP;
20✔
192
#endif
193
}
194

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

199
       r = xen_kexec_loaded();
20✔
200
       if (r >= 0)
20✔
UNCOV
201
               return r;
×
202

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

210
       return s[0] == '1';
20✔
211
}
212

213
int create_shutdown_run_nologin_or_warn(void) {
109✔
214
        int r;
109✔
215

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

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

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

© 2026 Coveralls, Inc