Debian Codegraph @

src/bless-boot/bless-boot-generator.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "efivars.h"
6 #include "generator.h"
7 #include "initrd-util.h"
8 #include "log.h"
9 #include "special.h"
10 #include "virt.h"
11
12 /* This generator pulls systemd-bless-boot.service into the initial transaction if the "LoaderBootCountPath"
13 * EFI variable is set, i.e. the system boots up with boot counting in effect, which means we should mark the
14 * boot as "good" if we manage to boot up far enough. */
15
16 static int run(const char *dest, const char *dest_early, const char *dest_late) {
17 assert(dest_early);
18
19 if (in_initrd()) {
20 log_debug("Skipping generator, running in the initrd.");
21 return 0;
22 }
23
24 if (detect_container() > 0) {
25 log_debug("Skipping generator, running in a container.");
26 return 0;
27 }
28
29 if (generator_soft_rebooted()) {
30 log_debug("Skipping generator, current system is entered via soft-reboot.");
31 return 0;
32 }
33
34 if (!is_efi_boot()) {
35 log_debug("Skipping generator, not an EFI boot.");
36 return 0;
37 }
38
39 if (access(EFIVAR_PATH(EFI_LOADER_VARIABLE_STR("LoaderBootCountPath")), F_OK) < 0) {
40 if (errno == ENOENT) {
41 log_debug_errno(errno, "Skipping generator, not booted with boot counting in effect.");
42 return 0;
43 }
44
45 return log_error_errno(errno, "Failed to check if LoaderBootCountPath EFI variable exists: %m");
46 }
47
48 /* We pull this in from basic.target so that it ends up in all "regular" boot ups, but not in
49 * rescue.target or even emergency.target. */
50 return generator_add_symlink(dest_early, SPECIAL_BASIC_TARGET, "wants", "systemd-bless-boot.service");
51 }
52
53 DEFINE_MAIN_GENERATOR_FUNCTION(run);