]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/system-update-generator/system-update-generator.c
Add SPDX license identifiers to source files under the LGPL
[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 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <unistd.h>
23
24 #include "fs-util.h"
25 #include "log.h"
26 #include "proc-cmdline.h"
27 #include "special.h"
28 #include "string-util.h"
29 #include "util.h"
30
31 /*
32 * Implements the logic described in systemd.offline-updates(7).
33 */
34
35 static const char *arg_dest = "/tmp";
36
37 static int generate_symlink(void) {
38 const char *p = NULL;
39
40 if (laccess("/system-update", F_OK) < 0) {
41 if (errno == ENOENT)
42 return 0;
43
44 log_error_errno(errno, "Failed to check for system update: %m");
45 return -EINVAL;
46 }
47
48 p = strjoina(arg_dest, "/" SPECIAL_DEFAULT_TARGET);
49 if (symlink(SYSTEM_DATA_UNIT_PATH "/system-update.target", p) < 0)
50 return log_error_errno(errno, "Failed to create symlink %s: %m", p);
51
52 return 1;
53 }
54
55 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
56 assert(key);
57
58 /* Check if a run level is specified on the kernel command line. The
59 * command line has higher priority than any on-disk configuration, so
60 * it'll make any symlink we create moot.
61 */
62
63 if (streq(key, "systemd.unit") && !proc_cmdline_value_missing(key, value))
64 log_warning("Offline system update overriden by kernel command line systemd.unit= setting");
65 else if (!value && runlevel_to_target(key))
66 log_warning("Offline system update overriden by runlevel \"%s\" on the kernel command line", key);
67
68 return 0;
69 }
70
71 int main(int argc, char *argv[]) {
72 int r, k;
73
74 if (argc > 1 && argc != 4) {
75 log_error("This program takes three or no arguments.");
76 return EXIT_FAILURE;
77 }
78
79 if (argc > 1)
80 arg_dest = argv[2];
81
82 log_set_target(LOG_TARGET_SAFE);
83 log_parse_environment();
84 log_open();
85
86 umask(0022);
87
88 r = generate_symlink();
89
90 if (r > 0) {
91 k = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
92 if (k < 0)
93 log_warning_errno(k, "Failed to parse kernel command line, ignoring: %m");
94 }
95
96 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
97 }