]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/bless-boot-generator.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / boot / bless-boot-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7 #include "efivars.h"
8 #include "log.h"
9 #include "mkdir.h"
10 #include "special.h"
11 #include "string-util.h"
12 #include "util.h"
13 #include "virt.h"
14
15 /* This generator pulls systemd-bless-boot.service into the initial transaction if the "LoaderBootCountPath" EFI
16 * variable is set, i.e. the system boots up with boot counting in effect, which means we should mark the boot as
17 * "good" if we manage to boot up far enough. */
18
19 static const char *arg_dest = "/tmp";
20
21 int main(int argc, char *argv[]) {
22 const char *p;
23
24 log_set_prohibit_ipc(true);
25 log_set_target(LOG_TARGET_AUTO);
26 log_parse_environment();
27 log_open();
28
29 umask(0022);
30
31 if (argc > 1 && argc != 4) {
32 log_error("This program takes three or no arguments.");
33 return EXIT_FAILURE;
34 }
35
36 if (argc > 1)
37 arg_dest = argv[2];
38
39 if (in_initrd() > 0) {
40 log_debug("Skipping generator, running in the initrd.");
41 return EXIT_SUCCESS;
42 }
43
44 if (detect_container() > 0) {
45 log_debug("Skipping generator, running in a container.");
46 return EXIT_SUCCESS;
47 }
48
49 if (!is_efi_boot()) {
50 log_debug("Skipping generator, not an EFI boot.");
51 return EXIT_SUCCESS;
52 }
53
54 if (access("/sys/firmware/efi/efivars/LoaderBootCountPath-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) < 0) {
55
56 if (errno == ENOENT) {
57 log_debug_errno(errno, "Skipping generator, not booted with boot counting in effect.");
58 return EXIT_SUCCESS;
59 }
60
61 log_error_errno(errno, "Failed to check if LoaderBootCountPath EFI variable exists: %m");
62 return EXIT_FAILURE;
63 }
64
65 /* We pull this in from basic.target so that it ends up in all "regular" boot ups, but not in rescue.target or
66 * even emergency.target. */
67 p = strjoina(arg_dest, "/" SPECIAL_BASIC_TARGET ".wants/systemd-bless-boot.service");
68 (void) mkdir_parents(p, 0755);
69 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-bless-boot.service", p) < 0) {
70 log_error_errno(errno, "Failed to create symlink '%s': %m", p);
71 return EXIT_FAILURE;
72 }
73
74 return EXIT_SUCCESS;
75 }