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