1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 #include <unistd.h>
5 
6 #include "fs-util.h"
7 #include "generator.h"
8 #include "log.h"
9 #include "proc-cmdline.h"
10 #include "special.h"
11 #include "string-util.h"
12 #include "unit-file.h"
13 #include "util.h"
14 
15 /*
16  * Implements the logic described in systemd.offline-updates(7).
17  */
18 
19 static const char *arg_dest = NULL;
20 
generate_symlink(void)21 static int generate_symlink(void) {
22         const char *p = NULL;
23 
24         if (laccess("/system-update", F_OK) < 0) {
25                 if (errno == ENOENT)
26                         return 0;
27 
28                 log_error_errno(errno, "Failed to check for system update: %m");
29                 return -EINVAL;
30         }
31 
32         p = strjoina(arg_dest, "/" SPECIAL_DEFAULT_TARGET);
33         if (symlink(SYSTEM_DATA_UNIT_DIR "/system-update.target", p) < 0)
34                 return log_error_errno(errno, "Failed to create symlink %s: %m", p);
35 
36         return 1;
37 }
38 
parse_proc_cmdline_item(const char * key,const char * value,void * data)39 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
40         assert(key);
41 
42         /* Check if a run level is specified on the kernel command line. The
43          * command line has higher priority than any on-disk configuration, so
44          * it'll make any symlink we create moot.
45          */
46 
47         if (streq(key, "systemd.unit") && !proc_cmdline_value_missing(key, value))
48                 log_warning("Offline system update overridden by kernel command line systemd.unit= setting");
49         else if (!value && runlevel_to_target(key))
50                 log_warning("Offline system update overridden by runlevel \"%s\" on the kernel command line", key);
51 
52         return 0;
53 }
54 
run(const char * dest,const char * dest_early,const char * dest_late)55 static int run(const char *dest, const char *dest_early, const char *dest_late) {
56         int r;
57 
58         assert_se(arg_dest = dest_early);
59 
60         r = generate_symlink();
61         if (r <= 0)
62                 return r;
63 
64         /* We parse the command line only to emit warnings. */
65         r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
66         if (r < 0)
67                 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
68 
69         return 0;
70 }
71 
72 DEFINE_MAIN_GENERATOR_FUNCTION(run);
73