Debian Codegraph @

src/system-update-generator/system-update-generator.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "fs-util.h"
7 #include "generator.h"
8 #include "initrd-util.h"
9 #include "log.h"
10 #include "path-util.h"
11 #include "proc-cmdline.h"
12 #include "special.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "unit-file.h"
16
17 /*
18 * Implements the logic described in systemd.offline-updates(7).
19 */
20
21 static const char *arg_dest = NULL;
22
23 static int generate_symlink(void) {
24 int r;
25
26 FOREACH_STRING(p, "/system-update", "/etc/system-update") {
27 r = access_nofollow(p, F_OK);
28 if (r < 0) {
29 if (r != -ENOENT)
30 log_warning_errno(r, "Failed to check if %s symlink exists, ignoring: %m", p);
31 continue;
32 }
33
34 _cleanup_free_ char *j = NULL;
35
36 j = path_join(arg_dest, SPECIAL_DEFAULT_TARGET);
37 if (!j)
38 return log_oom();
39
40 if (symlink(SYSTEM_DATA_UNIT_DIR "/system-update.target", j) < 0)
41 return log_error_errno(errno, "Failed to create symlink %s: %m", j);
42
43 return 1;
44 }
45
46 return 0;
47 }
48
49 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
50 assert(key);
51
52 /* Check if a run level is specified on the kernel command line. The
53 * command line has higher priority than any on-disk configuration, so
54 * it'll make any symlink we create moot.
55 */
56
57 if (streq(key, "systemd.unit") && !proc_cmdline_value_missing(key, value))
58 log_warning("Offline system update overridden by kernel command line systemd.unit= setting");
59 else if (!value && runlevel_to_target(key))
60 log_warning("Offline system update overridden by runlevel \"%s\" on the kernel command line", key);
61
62 return 0;
63 }
64
65 static int run(const char *dest, const char *dest_early, const char *dest_late) {
66 int r;
67
68 assert_se(arg_dest = dest_early);
69
70 if (in_initrd()) {
71 log_debug("Skipping generator, running in the initrd.");
72 return EXIT_SUCCESS;
73 }
74
75 r = generate_symlink();
76 if (r <= 0)
77 return r;
78
79 /* We parse the command line only to emit warnings. */
80 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
81 if (r < 0)
82 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
83
84 return 0;
85 }
86
87 DEFINE_MAIN_GENERATOR_FUNCTION(run);