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