]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/system-update-generator/system-update-generator.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / system-update-generator / system-update-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <unistd.h>
5
6 #include "fs-util.h"
7 #include "log.h"
8 #include "proc-cmdline.h"
9 #include "special.h"
10 #include "string-util.h"
11 #include "util.h"
12
13 /*
14 * Implements the logic described in systemd.offline-updates(7).
15 */
16
17 static const char *arg_dest = "/tmp";
18
19 static int generate_symlink(void) {
20 const char *p = NULL;
21
22 if (laccess("/system-update", F_OK) < 0) {
23 if (errno == ENOENT)
24 return 0;
25
26 log_error_errno(errno, "Failed to check for system update: %m");
27 return -EINVAL;
28 }
29
30 p = strjoina(arg_dest, "/" SPECIAL_DEFAULT_TARGET);
31 if (symlink(SYSTEM_DATA_UNIT_PATH "/system-update.target", p) < 0)
32 return log_error_errno(errno, "Failed to create symlink %s: %m", p);
33
34 return 1;
35 }
36
37 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
38 assert(key);
39
40 /* Check if a run level is specified on the kernel command line. The
41 * command line has higher priority than any on-disk configuration, so
42 * it'll make any symlink we create moot.
43 */
44
45 if (streq(key, "systemd.unit") && !proc_cmdline_value_missing(key, value))
46 log_warning("Offline system update overridden by kernel command line systemd.unit= setting");
47 else if (!value && runlevel_to_target(key))
48 log_warning("Offline system update overridden by runlevel \"%s\" on the kernel command line", key);
49
50 return 0;
51 }
52
53 int main(int argc, char *argv[]) {
54 int r, k;
55
56 if (argc > 1 && argc != 4) {
57 log_error("This program takes three or no arguments.");
58 return EXIT_FAILURE;
59 }
60
61 if (argc > 1)
62 arg_dest = argv[2];
63
64 log_set_prohibit_ipc(true);
65 log_set_target(LOG_TARGET_AUTO);
66 log_parse_environment();
67 log_open();
68
69 umask(0022);
70
71 r = generate_symlink();
72
73 if (r > 0) {
74 k = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
75 if (k < 0)
76 log_warning_errno(k, "Failed to parse kernel command line, ignoring: %m");
77 }
78
79 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
80 }