]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d360705f
LP
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
f4f15635 24#include "fs-util.h"
d360705f 25#include "log.h"
1cec251c 26#include "proc-cmdline.h"
075dafd2 27#include "special.h"
07630cea 28#include "string-util.h"
d360705f 29#include "util.h"
d360705f 30
399c5f96 31/*
075dafd2 32 * Implements the logic described in systemd.offline-updates(7).
399c5f96
LP
33 */
34
d360705f
LP
35static const char *arg_dest = "/tmp";
36
37static int generate_symlink(void) {
9194c8e4 38 const char *p = NULL;
d360705f 39
6b321a79 40 if (laccess("/system-update", F_OK) < 0) {
d360705f
LP
41 if (errno == ENOENT)
42 return 0;
43
56f64d95 44 log_error_errno(errno, "Failed to check for system update: %m");
d360705f
LP
45 return -EINVAL;
46 }
47
075dafd2 48 p = strjoina(arg_dest, "/" SPECIAL_DEFAULT_TARGET);
4a62c710
MS
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);
d360705f 51
1cec251c
ZJS
52 return 1;
53}
54
55static 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
d360705f
LP
68 return 0;
69}
70
71int main(int argc, char *argv[]) {
1cec251c 72 int r, k;
d360705f 73
07719a21
LP
74 if (argc > 1 && argc != 4) {
75 log_error("This program takes three or no arguments.");
d360705f
LP
76 return EXIT_FAILURE;
77 }
78
79 if (argc > 1)
07719a21 80 arg_dest = argv[2];
d360705f 81
a6903061 82 log_set_target(LOG_TARGET_SAFE);
d360705f
LP
83 log_parse_environment();
84 log_open();
85
86 umask(0022);
87
88 r = generate_symlink();
89
1cec251c
ZJS
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
d360705f
LP
96 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
97}