udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / system-update-generator / system-update-generator.c
bloba1782d5c059524fd74329811cc02546c377532bb
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <unistd.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 "unit-file.h"
17 * Implements the logic described in systemd.offline-updates(7).
20 static const char *arg_dest = NULL;
22 static int generate_symlink(void) {
23 FOREACH_STRING(p, "/system-update", "/etc/system-update") {
24 if (laccess(p, F_OK) >= 0) {
25 _cleanup_free_ char *j = NULL;
27 j = path_join(arg_dest, SPECIAL_DEFAULT_TARGET);
28 if (!j)
29 return log_oom();
31 if (symlink(SYSTEM_DATA_UNIT_DIR "/system-update.target", j) < 0)
32 return log_error_errno(errno, "Failed to create symlink %s: %m", j);
34 return 1;
37 if (errno != ENOENT)
38 log_warning_errno(errno, "Failed to check if %s symlink exists, ignoring: %m", p);
41 return 0;
44 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
45 assert(key);
47 /* Check if a run level is specified on the kernel command line. The
48 * command line has higher priority than any on-disk configuration, so
49 * it'll make any symlink we create moot.
52 if (streq(key, "systemd.unit") && !proc_cmdline_value_missing(key, value))
53 log_warning("Offline system update overridden by kernel command line systemd.unit= setting");
54 else if (!value && runlevel_to_target(key))
55 log_warning("Offline system update overridden by runlevel \"%s\" on the kernel command line", key);
57 return 0;
60 static int run(const char *dest, const char *dest_early, const char *dest_late) {
61 int r;
63 assert_se(arg_dest = dest_early);
65 if (in_initrd()) {
66 log_debug("Skipping generator, running in the initrd.");
67 return EXIT_SUCCESS;
70 r = generate_symlink();
71 if (r <= 0)
72 return r;
74 /* We parse the command line only to emit warnings. */
75 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
76 if (r < 0)
77 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
79 return 0;
82 DEFINE_MAIN_GENERATOR_FUNCTION(run);