| 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 |
#endif |
| 18 |
|
| 19 |
#include "alloc-util.h" |
| 20 |
#include "compress.h" |
| 21 |
#include "copy.h" |
| 22 |
#include "fd-util.h" |
| 23 |
#include "fileio.h" |
| 24 |
#include "io-util.h" |
| 25 |
#include "log.h" |
| 26 |
#include "memfd-util.h" |
| 27 |
#include "pe-binary.h" |
| 28 |
#include "proc-cmdline.h" |
| 29 |
#include "reboot-util.h" |
| 30 |
#include "sparse-endian.h" |
| 31 |
#include "stat-util.h" |
| 32 |
#include "string-util.h" |
| 33 |
#include "umask-util.h" |
| 34 |
#include "utf8.h" |
| 35 |
#include "virt.h" |
| 36 |
|
| 37 |
/* ZBOOT header layout — see linux/drivers/firmware/efi/libstub/zboot-header.S */ |
| 38 |
struct zboot_header { |
| 39 |
le16_t mz_magic; /* 0x00: "MZ" DOS signature */ |
| 40 |
le16_t _pad0; |
| 41 |
uint8_t zimg_magic[4]; /* 0x04: "zimg" identifier */ |
| 42 |
le32_t payload_offset; /* 0x08: offset to compressed payload */ |
| 43 |
le32_t payload_size; /* 0x0C: size of compressed payload */ |
| 44 |
uint8_t _pad1[8]; |
| 45 |
char comp_type[6]; /* 0x18: NUL-terminated compression type (e.g. "gzip", "zstd") */ |
| 46 |
uint8_t _pad2[2]; |
| 47 |
} _packed_; |
| 48 |
assert_cc(sizeof(struct zboot_header) == 0x20); |
| 49 |
assert_cc(offsetof(struct zboot_header, comp_type) == 0x18); |
| 50 |
|
| 51 |
int raw_reboot(int cmd, const void *arg) { |
| 52 |
return syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, arg); |
| 53 |
} |
| 54 |
|
| 55 |
bool reboot_parameter_is_valid(const char *parameter) { |
| 56 |
assert(parameter); |
| 57 |
|
| 58 |
return ascii_is_valid(parameter) && strlen(parameter) <= NAME_MAX; |
| 59 |
} |
| 60 |
|
| 61 |
int update_reboot_parameter_and_warn(const char *parameter, bool keep) { |
| 62 |
int r; |
| 63 |
|
| 64 |
if (isempty(parameter)) { |
| 65 |
if (keep) |
| 66 |
return 0; |
| 67 |
|
| 68 |
if (unlink("/run/systemd/reboot-param") < 0) { |
| 69 |
if (errno == ENOENT) |
| 70 |
return 0; |
| 71 |
|
| 72 |
return log_warning_errno(errno, "Failed to unlink reboot parameter file: %m"); |
| 73 |
} |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
if (!reboot_parameter_is_valid(parameter)) |
| 79 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid reboot parameter '%s'.", parameter); |
| 80 |
|
| 81 |
WITH_UMASK(0022) { |
| 82 |
r = write_string_file("/run/systemd/reboot-param", parameter, |
| 83 |
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC); |
| 84 |
if (r < 0) |
| 85 |
return log_warning_errno(r, "Failed to write reboot parameter file: %m"); |
| 86 |
} |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
int read_reboot_parameter(char **parameter) { |
| 92 |
int r; |
| 93 |
|
| 94 |
assert(parameter); |
| 95 |
|
| 96 |
r = read_one_line_file("/run/systemd/reboot-param", parameter); |
| 97 |
if (r < 0 && r != -ENOENT) |
| 98 |
return log_debug_errno(r, "Failed to read /run/systemd/reboot-param: %m"); |
| 99 |
|
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
int reboot_with_parameter(RebootFlags flags) { |
| 104 |
int r; |
| 105 |
|
| 106 |
/* Reboots the system with a parameter that is read from /run/systemd/reboot-param. Returns 0 if |
| 107 |
* REBOOT_DRY_RUN was set and the actual reboot operation was hence skipped. If REBOOT_FALLBACK is |
| 108 |
* set and the reboot with parameter doesn't work out a fallback to classic reboot() is attempted. If |
| 109 |
* REBOOT_FALLBACK is not set, 0 is returned instead, which should be considered indication for the |
| 110 |
* caller to fall back to reboot() on its own, or somehow else deal with this. If REBOOT_LOG is |
| 111 |
* specified will log about what it is going to do, as well as all errors. */ |
| 112 |
|
| 113 |
if (detect_container() == 0) { |
| 114 |
_cleanup_free_ char *parameter = NULL; |
| 115 |
|
| 116 |
r = read_one_line_file("/run/systemd/reboot-param", ¶meter); |
| 117 |
if (r < 0 && r != -ENOENT) |
| 118 |
log_full_errno(flags & REBOOT_LOG ? LOG_WARNING : LOG_DEBUG, r, |
| 119 |
"Failed to read reboot parameter file, ignoring: %m"); |
| 120 |
|
| 121 |
if (!isempty(parameter)) { |
| 122 |
log_full(flags & REBOOT_LOG ? LOG_INFO : LOG_DEBUG, |
| 123 |
"Rebooting with argument '%s'.", parameter); |
| 124 |
|
| 125 |
if (flags & REBOOT_DRY_RUN) |
| 126 |
return 0; |
| 127 |
|
| 128 |
(void) raw_reboot(LINUX_REBOOT_CMD_RESTART2, parameter); |
| 129 |
|
| 130 |
log_full_errno(flags & REBOOT_LOG ? LOG_WARNING : LOG_DEBUG, errno, |
| 131 |
"Failed to reboot with parameter, retrying without: %m"); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if (!(flags & REBOOT_FALLBACK)) |
| 136 |
return 0; |
| 137 |
|
| 138 |
log_full(flags & REBOOT_LOG ? LOG_INFO : LOG_DEBUG, "Rebooting."); |
| 139 |
|
| 140 |
if (flags & REBOOT_DRY_RUN) |
| 141 |
return 0; |
| 142 |
|
| 143 |
(void) reboot(RB_AUTOBOOT); |
| 144 |
|
| 145 |
return log_full_errno(flags & REBOOT_LOG ? LOG_ERR : LOG_DEBUG, errno, "Failed to reboot: %m"); |
| 146 |
} |
| 147 |
|
| 148 |
bool shall_restore_state(void) { |
| 149 |
static int cached = -1; |
| 150 |
bool b = true; /* If nothing specified or the check fails, then defaults to true. */ |
| 151 |
int r; |
| 152 |
|
| 153 |
if (cached >= 0) |
| 154 |
return cached; |
| 155 |
|
| 156 |
r = proc_cmdline_get_bool("systemd.restore_state", PROC_CMDLINE_TRUE_WHEN_MISSING, &b); |
| 157 |
if (r < 0) |
| 158 |
log_debug_errno(r, "Failed to parse systemd.restore_state= kernel command line option, ignoring: %m"); |
| 159 |
|
| 160 |
return (cached = b); |
| 161 |
} |
| 162 |
|
| 163 |
#if HAVE_XENCTRL |
| 164 |
static int xen_kexec_command(uint64_t cmd) { |
| 165 |
_cleanup_close_ int privcmd_fd = -EBADF, buf_fd = -EBADF; |
| 166 |
void *buffer; |
| 167 |
size_t size; |
| 168 |
int r; |
| 169 |
|
| 170 |
assert(IN_SET(cmd, KEXEC_CMD_kexec, KEXEC_CMD_kexec_status)); |
| 171 |
|
| 172 |
if (access("/proc/xen", F_OK) < 0) { |
| 173 |
if (errno == ENOENT) |
| 174 |
return -EOPNOTSUPP; |
| 175 |
return log_debug_errno(errno, "Unable to test whether /proc/xen exists: %m"); |
| 176 |
} |
| 177 |
|
| 178 |
size = page_size(); |
| 179 |
if ((cmd == KEXEC_CMD_kexec_status && sizeof(xen_kexec_status_t) > size) || |
| 180 |
(cmd == KEXEC_CMD_kexec && sizeof(xen_kexec_exec_t) > size)) |
| 181 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "page_size is too small for hypercall"); |
| 182 |
|
| 183 |
privcmd_fd = open("/dev/xen/privcmd", O_RDWR|O_CLOEXEC); |
| 184 |
if (privcmd_fd < 0) |
| 185 |
return log_debug_errno(errno, "Cannot access /dev/xen/privcmd: %m"); |
| 186 |
|
| 187 |
buf_fd = open("/dev/xen/hypercall", O_RDWR|O_CLOEXEC); |
| 188 |
if (buf_fd < 0) |
| 189 |
return log_debug_errno(errno, "Cannot access /dev/xen/hypercall: %m"); |
| 190 |
|
| 191 |
buffer = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, buf_fd, 0); |
| 192 |
if (buffer == MAP_FAILED) |
| 193 |
return log_debug_errno(errno, "Cannot allocate buffer for hypercall: %m"); |
| 194 |
|
| 195 |
if (cmd == KEXEC_CMD_kexec_status) |
| 196 |
*(xen_kexec_status_t *)buffer = (xen_kexec_status_t) { |
| 197 |
.type = KEXEC_TYPE_DEFAULT, |
| 198 |
}; |
| 199 |
else |
| 200 |
*(xen_kexec_exec_t *)buffer = (xen_kexec_exec_t) { |
| 201 |
.type = KEXEC_TYPE_DEFAULT, |
| 202 |
}; |
| 203 |
|
| 204 |
privcmd_hypercall_t call = { |
| 205 |
.op = __HYPERVISOR_kexec_op, |
| 206 |
.arg = { |
| 207 |
cmd, |
| 208 |
PTR_TO_UINT64(buffer), |
| 209 |
}, |
| 210 |
}; |
| 211 |
|
| 212 |
r = RET_NERRNO(ioctl(privcmd_fd, IOCTL_PRIVCMD_HYPERCALL, &call)); |
| 213 |
if (r < 0) |
| 214 |
log_debug_errno(r, "kexec%s failed: %m", cmd == KEXEC_CMD_kexec_status ? "_status" : ""); |
| 215 |
|
| 216 |
munmap(buffer, size); |
| 217 |
|
| 218 |
return r; |
| 219 |
} |
| 220 |
#endif |
| 221 |
|
| 222 |
static int xen_kexec(void) { |
| 223 |
#if HAVE_XENCTRL |
| 224 |
return xen_kexec_command(KEXEC_CMD_kexec); |
| 225 |
#else |
| 226 |
return -EOPNOTSUPP; |
| 227 |
#endif |
| 228 |
} |
| 229 |
|
| 230 |
static int xen_kexec_loaded(void) { |
| 231 |
#if HAVE_XENCTRL |
| 232 |
return xen_kexec_command(KEXEC_CMD_kexec_status); |
| 233 |
#else |
| 234 |
return -EOPNOTSUPP; |
| 235 |
#endif |
| 236 |
} |
| 237 |
|
| 238 |
bool kexec_loaded(void) { |
| 239 |
_cleanup_free_ char *s = NULL; |
| 240 |
int r; |
| 241 |
|
| 242 |
r = xen_kexec_loaded(); |
| 243 |
if (r >= 0) |
| 244 |
return r; |
| 245 |
|
| 246 |
r = read_one_line_file("/sys/kernel/kexec_loaded", &s); |
| 247 |
if (r < 0) { |
| 248 |
if (r != -ENOENT) |
| 249 |
log_debug_errno(r, "Unable to read /sys/kernel/kexec_loaded, ignoring: %m"); |
| 250 |
return false; |
| 251 |
} |
| 252 |
|
| 253 |
return s[0] == '1'; |
| 254 |
} |
| 255 |
|
| 256 |
int kexec(void) { |
| 257 |
int r; |
| 258 |
|
| 259 |
r = xen_kexec(); |
| 260 |
if (r < 0 && r != -EOPNOTSUPP) |
| 261 |
return log_error_errno(r, "Failed to call xen kexec: %m"); |
| 262 |
|
| 263 |
r = reboot(LINUX_REBOOT_CMD_KEXEC); |
| 264 |
if (r < 0) |
| 265 |
return log_error_errno(errno, "Failed to kexec: %m"); |
| 266 |
|
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
static int decompress_to_memfd(Compression compression, int fd) { |
| 271 |
int r; |
| 272 |
|
| 273 |
_cleanup_close_ int memfd = memfd_new("kexec-kernel"); |
| 274 |
if (memfd < 0) |
| 275 |
return log_error_errno(memfd, "Failed to create memfd: %m"); |
| 276 |
|
| 277 |
r = decompress_stream(compression, fd, memfd, UINT64_MAX); |
| 278 |
if (r < 0) |
| 279 |
return log_error_errno(r, "Failed to decompress kernel: %m"); |
| 280 |
|
| 281 |
if (lseek(memfd, 0, SEEK_SET) < 0) |
| 282 |
return log_error_errno(errno, "Failed to seek memfd: %m"); |
| 283 |
|
| 284 |
return TAKE_FD(memfd); |
| 285 |
} |
| 286 |
|
| 287 |
static int decompress_zboot_to_memfd(int fd, uint32_t payload_offset, uint32_t payload_size, const char *comp_type) { |
| 288 |
int r; |
| 289 |
|
| 290 |
Compression c = compression_from_string(comp_type); |
| 291 |
if (c < 0) |
| 292 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 293 |
"Unsupported ZBOOT compression type '%s'.", comp_type); |
| 294 |
|
| 295 |
struct stat st; |
| 296 |
if (fstat(fd, &st) < 0) |
| 297 |
return log_error_errno(errno, "Failed to stat ZBOOT image: %m"); |
| 298 |
|
| 299 |
r = stat_verify_regular(&st); |
| 300 |
if (r < 0) |
| 301 |
return log_error_errno(r, "Kernel image is not a regular file: %m"); |
| 302 |
|
| 303 |
if (payload_offset < 0x20 || |
| 304 |
payload_size == 0 || |
| 305 |
payload_offset > (uint64_t) st.st_size || |
| 306 |
payload_size > (uint64_t) st.st_size - payload_offset) |
| 307 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "ZBOOT payload offset/size invalid."); |
| 308 |
|
| 309 |
if (payload_size > 256 * 1024 * 1024) /* generous for any compressed kernel */ |
| 310 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "ZBOOT payload unreasonably large."); |
| 311 |
|
| 312 |
_cleanup_free_ void *payload = malloc(payload_size); |
| 313 |
if (!payload) |
| 314 |
return log_oom(); |
| 315 |
|
| 316 |
ssize_t n = pread(fd, payload, payload_size, payload_offset); |
| 317 |
if (n < 0) |
| 318 |
return log_error_errno(errno, "Failed to read ZBOOT payload: %m"); |
| 319 |
if ((uint32_t) n < payload_size) |
| 320 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Short read of ZBOOT payload."); |
| 321 |
|
| 322 |
_cleanup_free_ void *decompressed = NULL; |
| 323 |
size_t decompressed_size; |
| 324 |
r = decompress_blob(c, payload, payload_size, &decompressed, &decompressed_size, /* dst_max= */ 0); |
| 325 |
if (r < 0) |
| 326 |
return log_error_errno(r, "Failed to decompress ZBOOT payload: %m"); |
| 327 |
|
| 328 |
payload = mfree(payload); |
| 329 |
|
| 330 |
_cleanup_close_ int memfd = memfd_new("kexec-kernel"); |
| 331 |
if (memfd < 0) |
| 332 |
return log_error_errno(memfd, "Failed to create memfd: %m"); |
| 333 |
|
| 334 |
r = loop_write(memfd, decompressed, decompressed_size); |
| 335 |
if (r < 0) |
| 336 |
return log_error_errno(r, "Failed to write decompressed kernel to memfd: %m"); |
| 337 |
|
| 338 |
if (lseek(memfd, 0, SEEK_SET) < 0) |
| 339 |
return log_error_errno(errno, "Failed to seek memfd: %m"); |
| 340 |
|
| 341 |
return TAKE_FD(memfd); |
| 342 |
} |
| 343 |
|
| 344 |
static int pe_section_to_memfd(int fd, const IMAGE_SECTION_HEADER *section, const char *name) { |
| 345 |
int r; |
| 346 |
|
| 347 |
assert(fd >= 0); |
| 348 |
assert(section); |
| 349 |
|
| 350 |
uint32_t offset = le32toh(section->PointerToRawData), |
| 351 |
size = MIN(le32toh(section->VirtualSize), le32toh(section->SizeOfRawData)); |
| 352 |
|
| 353 |
_cleanup_close_ int memfd = memfd_new(name); |
| 354 |
if (memfd < 0) |
| 355 |
return log_error_errno(memfd, "Failed to create memfd for PE section '%s': %m", name); |
| 356 |
|
| 357 |
if (lseek(fd, offset, SEEK_SET) < 0) |
| 358 |
return log_error_errno(errno, "Failed to seek to PE section '%s': %m", name); |
| 359 |
|
| 360 |
r = copy_bytes(fd, memfd, size, /* copy_flags= */ 0); |
| 361 |
if (r < 0) |
| 362 |
return log_error_errno(r, "Failed to copy PE section '%s': %m", name); |
| 363 |
|
| 364 |
if (lseek(memfd, 0, SEEK_SET) < 0) |
| 365 |
return log_error_errno(errno, "Failed to seek memfd: %m"); |
| 366 |
|
| 367 |
return TAKE_FD(memfd); |
| 368 |
} |
| 369 |
|
| 370 |
static int extract_uki(const char *path, int fd, int *ret_kernel_fd, int *ret_initrd_fd) { |
| 371 |
int r; |
| 372 |
|
| 373 |
assert(fd >= 0); |
| 374 |
assert(ret_kernel_fd); |
| 375 |
|
| 376 |
_cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL; |
| 377 |
_cleanup_free_ PeHeader *pe_header = NULL; |
| 378 |
r = pe_load_headers(fd, &dos_header, &pe_header); |
| 379 |
if (r < 0) |
| 380 |
return log_debug_errno(r, "Not a valid PE file '%s': %m", path); |
| 381 |
|
| 382 |
_cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL; |
| 383 |
r = pe_load_sections(fd, dos_header, pe_header, §ions); |
| 384 |
if (r < 0) |
| 385 |
return log_debug_errno(r, "Failed to load PE sections from '%s': %m", path); |
| 386 |
|
| 387 |
if (!pe_is_uki(pe_header, sections)) |
| 388 |
return 0; /* Not a UKI */ |
| 389 |
|
| 390 |
/* FIXME: we currently only extract .linux and .initrd, but sd-stub does a lot more: |
| 391 |
* profiles, .cmdline, .dtb/.dtbauto, .ucode, .pcrsig/.pcrpkey, sidecar addons, |
| 392 |
* credentials, sysexts/confexts, and TPM PCR measurements. */ |
| 393 |
|
| 394 |
const IMAGE_SECTION_HEADER *linux_section = pe_header_find_section(pe_header, sections, ".linux"); |
| 395 |
if (!linux_section) |
| 396 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 397 |
"UKI '%s' has no .linux section.", path); |
| 398 |
|
| 399 |
log_debug("Detected UKI image '%s', extracting .linux section.", path); |
| 400 |
|
| 401 |
_cleanup_close_ int kernel_memfd = pe_section_to_memfd(fd, linux_section, "kexec-uki-kernel"); |
| 402 |
if (kernel_memfd < 0) |
| 403 |
return kernel_memfd; |
| 404 |
|
| 405 |
_cleanup_close_ int initrd_memfd = -EBADF; |
| 406 |
if (ret_initrd_fd) { |
| 407 |
const IMAGE_SECTION_HEADER *initrd_section = pe_header_find_section(pe_header, sections, ".initrd"); |
| 408 |
if (initrd_section) { |
| 409 |
log_debug("Extracting .initrd section from UKI '%s'.", path); |
| 410 |
|
| 411 |
initrd_memfd = pe_section_to_memfd(fd, initrd_section, "kexec-uki-initrd"); |
| 412 |
if (initrd_memfd < 0) |
| 413 |
return initrd_memfd; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
*ret_kernel_fd = TAKE_FD(kernel_memfd); |
| 418 |
if (ret_initrd_fd) |
| 419 |
*ret_initrd_fd = TAKE_FD(initrd_memfd); |
| 420 |
|
| 421 |
return 1; |
| 422 |
} |
| 423 |
|
| 424 |
int kexec_maybe_decompress_kernel(const char *path, int fd, int *ret_kernel_fd, int *ret_initrd_fd) { |
| 425 |
uint8_t magic[8]; |
| 426 |
ssize_t n; |
| 427 |
int r; |
| 428 |
|
| 429 |
assert(fd >= 0); |
| 430 |
assert(ret_kernel_fd); |
| 431 |
|
| 432 |
n = pread(fd, magic, sizeof(magic), 0); |
| 433 |
if (n < 0) |
| 434 |
return log_error_errno(errno, "Failed to read kernel magic from '%s': %m", path); |
| 435 |
if ((size_t) n < sizeof(magic)) |
| 436 |
/* Too small to detect, pass through as-is */ |
| 437 |
return 0; |
| 438 |
|
| 439 |
if (magic[0] == 'M' && magic[1] == 'Z') { |
| 440 |
|
| 441 |
if (magic[4] == 'z' && magic[5] == 'i' && magic[6] == 'm' && magic[7] == 'g') { |
| 442 |
struct zboot_header h; |
| 443 |
|
| 444 |
n = pread(fd, &h, sizeof(h), 0); |
| 445 |
if (n < 0) |
| 446 |
return log_error_errno(errno, "Failed to read ZBOOT header from '%s': %m", path); |
| 447 |
if ((size_t) n < sizeof(h)) |
| 448 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 449 |
"Short read of ZBOOT header from '%s'.", path); |
| 450 |
|
| 451 |
char comp_type[sizeof(h.comp_type) + 1]; |
| 452 |
memcpy(comp_type, h.comp_type, sizeof(h.comp_type)); |
| 453 |
comp_type[sizeof(h.comp_type)] = '\0'; |
| 454 |
|
| 455 |
uint32_t payload_offset = le32toh(h.payload_offset), |
| 456 |
payload_size = le32toh(h.payload_size); |
| 457 |
|
| 458 |
log_debug("Detected ZBOOT image '%s' (compression=%s, offset=%"PRIu32", size=%"PRIu32")", |
| 459 |
path, comp_type, payload_offset, payload_size); |
| 460 |
|
| 461 |
r = decompress_zboot_to_memfd(fd, payload_offset, payload_size, comp_type); |
| 462 |
if (r < 0) |
| 463 |
return r; |
| 464 |
|
| 465 |
*ret_kernel_fd = r; |
| 466 |
return 1; |
| 467 |
} |
| 468 |
|
| 469 |
/* MZ but not ZBOOT — check if it's a UKI */ |
| 470 |
return extract_uki(path, fd, ret_kernel_fd, ret_initrd_fd); |
| 471 |
} |
| 472 |
|
| 473 |
Compression c = compression_detect_from_magic(magic); |
| 474 |
if (c < 0) |
| 475 |
/* Not a recognized compressed format, pass through as-is */ |
| 476 |
return 0; |
| 477 |
|
| 478 |
log_debug("Detected %s-compressed kernel '%s', decompressing.", compression_to_string(c), path); |
| 479 |
|
| 480 |
/* Seek back to start before decompression */ |
| 481 |
if (lseek(fd, 0, SEEK_SET) < 0) |
| 482 |
return log_error_errno(errno, "Failed to seek kernel fd: %m"); |
| 483 |
|
| 484 |
r = decompress_to_memfd(c, fd); |
| 485 |
if (r < 0) |
| 486 |
return r; |
| 487 |
|
| 488 |
*ret_kernel_fd = r; |
| 489 |
return 1; |
| 490 |
} |
| 491 |
|
| 492 |
int create_shutdown_run_nologin_or_warn(void) { |
| 493 |
int r; |
| 494 |
|
| 495 |
/* This is used twice: once in systemd-user-sessions.service, in order to block logins when we |
| 496 |
* actually go down, and once in systemd-logind.service when shutdowns are scheduled, and logins are |
| 497 |
* to be turned off a bit in advance. We use the same wording of the message in both cases. |
| 498 |
* |
| 499 |
* Traditionally, there was only /etc/nologin, and we managed that. Then, in PAM 1.1 |
| 500 |
* support for /run/nologin was added as alternative |
| 501 |
* (https://github.com/linux-pam/linux-pam/commit/e9e593f6ddeaf975b7fe8446d184e6bc387d450b). |
| 502 |
* 13 years later we stopped managing /etc/nologin, leaving it for the administrator to manage. |
| 503 |
*/ |
| 504 |
|
| 505 |
r = write_string_file("/run/nologin", |
| 506 |
"System is going down. Unprivileged users are not permitted to log in anymore. " |
| 507 |
"For technical details, see pam_nologin(8).", |
| 508 |
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL); |
| 509 |
if (r < 0) |
| 510 |
return log_error_errno(r, "Failed to create /run/nologin: %m"); |
| 511 |
|
| 512 |
return 0; |
| 513 |
} |