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

systemd / systemd / 23672908348

27 Mar 2026 10:16PM UTC coverage: 72.401% (+0.3%) from 72.113%
23672908348

push

github

daandemeyer
boot-entry: add 'auto' keyword to parse_boot_entry_token_type

Add the auto keyword as documented in the help message and man pages of
`kernel-install`, `bootctl` and `systemd-pcrlock`.

0 of 4 new or added lines in 1 file covered. (0.0%)

2759 existing lines in 71 files now uncovered.

318227 of 439535 relevant lines covered (72.4%)

1197709.62 hits per line

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

42.68
/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

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

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

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

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

54
                return 0;
55
        }
56

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

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

67
        return 0;
×
68
}
69

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);
×
76
        if (r < 0 && r != -ENOENT)
×
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);
×
96
                if (r < 0 && r != -ENOENT)
×
97
                        log_full_errno(flags & REBOOT_LOG ? LOG_WARNING : LOG_DEBUG, r,
×
98
                                       "Failed to read reboot parameter file, ignoring: %m");
99

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

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

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

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

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✔
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
#if HAVE_XENCTRL
143
static int xen_kexec_command(uint64_t cmd) {
144
        _cleanup_close_ int privcmd_fd = -EBADF, buf_fd = -EBADF;
145
        void *buffer;
146
        size_t size;
147
        int r;
148

149
        assert(IN_SET(cmd, KEXEC_CMD_kexec, KEXEC_CMD_kexec_status));
150

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

157
        size = page_size();
158
        if ((cmd == KEXEC_CMD_kexec_status && sizeof(xen_kexec_status_t) > size) ||
159
            (cmd == KEXEC_CMD_kexec && sizeof(xen_kexec_exec_t) > size))
160
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "page_size is too small for hypercall");
161

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

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

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

174
        if (cmd == KEXEC_CMD_kexec_status)
175
                *(xen_kexec_status_t *)buffer = (xen_kexec_status_t) {
176
                        .type = KEXEC_TYPE_DEFAULT,
177
                };
178
        else
179
                *(xen_kexec_exec_t *)buffer = (xen_kexec_exec_t) {
180
                        .type = KEXEC_TYPE_DEFAULT,
181
                };
182

183
        privcmd_hypercall_t call = {
184
                .op = __HYPERVISOR_kexec_op,
185
                .arg = {
186
                        cmd,
187
                        PTR_TO_UINT64(buffer),
188
                },
189
        };
190

191
        r = RET_NERRNO(ioctl(privcmd_fd, IOCTL_PRIVCMD_HYPERCALL, &call));
192
        if (r < 0)
193
                log_debug_errno(r, "kexec%s failed: %m", cmd == KEXEC_CMD_kexec_status ? "_status" : "");
194

195
        munmap(buffer, size);
196

197
        return r;
198
}
199
#endif
200

201
static int xen_kexec(void) {
×
202
#if HAVE_XENCTRL
203
        return xen_kexec_command(KEXEC_CMD_kexec);
204
#else
205
        return -EOPNOTSUPP;
×
206
#endif
207
}
208

209
static int xen_kexec_loaded(void) {
20✔
210
#if HAVE_XENCTRL
211
        return xen_kexec_command(KEXEC_CMD_kexec_status);
212
#else
213
        return -EOPNOTSUPP;
20✔
214
#endif
215
}
216

217
bool kexec_loaded(void) {
20✔
218
       _cleanup_free_ char *s = NULL;
20✔
219
       int r;
20✔
220

221
       r = xen_kexec_loaded();
20✔
222
       if (r >= 0)
20✔
UNCOV
223
               return r;
×
224

225
       r = read_one_line_file("/sys/kernel/kexec_loaded", &s);
20✔
226
       if (r < 0) {
20✔
UNCOV
227
               if (r != -ENOENT)
×
UNCOV
228
                       log_debug_errno(r, "Unable to read /sys/kernel/kexec_loaded, ignoring: %m");
×
UNCOV
229
               return false;
×
230
       }
231

232
       return s[0] == '1';
20✔
233
}
234

UNCOV
235
int kexec(void) {
×
UNCOV
236
        int r;
×
237

UNCOV
238
        r = xen_kexec();
×
UNCOV
239
        if (r < 0 && r != -EOPNOTSUPP)
×
UNCOV
240
                return log_error_errno(r, "Failed to call xen kexec: %m");
×
241

UNCOV
242
        r = reboot(LINUX_REBOOT_CMD_KEXEC);
×
UNCOV
243
        if (r < 0)
×
UNCOV
244
                return log_error_errno(errno, "Failed to kexec: %m");
×
245

246
        return 0;
247
}
248

249
int create_shutdown_run_nologin_or_warn(void) {
109✔
250
        int r;
109✔
251

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

262
        r = write_string_file("/run/nologin",
109✔
263
                              "System is going down. Unprivileged users are not permitted to log in anymore. "
264
                              "For technical details, see pam_nologin(8).",
265
                              WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
266
        if (r < 0)
109✔
UNCOV
267
                return log_error_errno(r, "Failed to create /run/nologin: %m");
×
268

269
        return 0;
270
}
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