]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-dropin.c
99d6b6d8f0a82070c071f6ea99a6b8da40e10336
[thirdparty/systemd.git] / src / core / load-dropin.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2010 Lennart Poettering
4 ***/
5
6 #include "conf-parser.h"
7 #include "fs-util.h"
8 #include "load-dropin.h"
9 #include "load-fragment.h"
10 #include "log.h"
11 #include "stat-util.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "unit-name.h"
15 #include "unit.h"
16
17 static int unit_name_compatible(const char *a, const char *b) {
18 _cleanup_free_ char *template = NULL;
19 int r;
20
21 /* The straightforward case: the symlink name matches the target */
22 if (streq(a, b))
23 return 1;
24
25 r = unit_name_template(a, &template);
26 if (r == -EINVAL)
27 return 0; /* Not a template */
28 if (r < 0)
29 return r; /* OOM, or some other failure. Just skip the warning. */
30
31 /* An instance name points to a target that is just the template name */
32 return streq(template, b);
33 }
34
35 static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) {
36 _cleanup_strv_free_ char **paths = NULL;
37 char **p;
38 int r;
39
40 r = unit_file_find_dropin_paths(NULL,
41 u->manager->lookup_paths.search_path,
42 u->manager->unit_path_cache,
43 dir_suffix,
44 NULL,
45 u->names,
46 &paths);
47 if (r < 0)
48 return r;
49
50 STRV_FOREACH(p, paths) {
51 _cleanup_free_ char *target = NULL;
52 const char *entry;
53
54 entry = basename(*p);
55
56 if (null_or_empty_path(*p) > 0) {
57 /* an error usually means an invalid symlink, which is not a mask */
58 log_unit_debug(u, "%s dependency on %s is masked by %s, ignoring.",
59 unit_dependency_to_string(dependency), entry, *p);
60 continue;
61 }
62
63 r = is_symlink(*p);
64 if (r < 0) {
65 log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m",
66 unit_dependency_to_string(dependency), *p);
67 continue;
68 }
69 if (r == 0) {
70 log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.",
71 unit_dependency_to_string(dependency), *p);
72 continue;
73 }
74
75 if (!unit_name_is_valid(entry, UNIT_NAME_ANY)) {
76 log_unit_warning(u, "%s dependency dropin %s is not a valid unit name, ignoring.",
77 unit_dependency_to_string(dependency), *p);
78 continue;
79 }
80
81 r = readlink_malloc(*p, &target);
82 if (r < 0) {
83 log_unit_warning_errno(u, r, "readlink(\"%s\") failed, ignoring: %m", *p);
84 continue;
85 }
86
87 /* We don't treat this as an error, especially because we didn't check this for a
88 * long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */
89 r = unit_name_compatible(entry, basename(target));
90 if (r < 0) {
91 log_unit_warning_errno(u, r, "Can't check if names %s and %s are compatible, ignoring: %m", entry, basename(target));
92 continue;
93 }
94 if (r == 0)
95 log_unit_warning(u, "%s dependency dropin %s target %s has different name",
96 unit_dependency_to_string(dependency), *p, target);
97
98 r = unit_add_dependency_by_name(u, dependency, entry, *p, true, UNIT_DEPENDENCY_FILE);
99 if (r < 0)
100 log_unit_warning_errno(u, r, "Cannot add %s dependency on %s, ignoring: %m",
101 unit_dependency_to_string(dependency), entry);
102 }
103
104 return 0;
105 }
106
107 int unit_load_dropin(Unit *u) {
108 _cleanup_strv_free_ char **l = NULL;
109 char **f;
110 int r;
111
112 assert(u);
113
114 /* Load dependencies from .wants and .requires directories */
115 r = process_deps(u, UNIT_WANTS, ".wants");
116 if (r < 0)
117 return r;
118
119 r = process_deps(u, UNIT_REQUIRES, ".requires");
120 if (r < 0)
121 return r;
122
123 /* Load .conf dropins */
124 r = unit_find_dropin_paths(u, &l);
125 if (r <= 0)
126 return 0;
127
128 if (!u->dropin_paths)
129 u->dropin_paths = TAKE_PTR(l);
130 else {
131 r = strv_extend_strv(&u->dropin_paths, l, true);
132 if (r < 0)
133 return log_oom();
134 }
135
136 STRV_FOREACH(f, u->dropin_paths)
137 (void) config_parse(u->id, *f, NULL,
138 UNIT_VTABLE(u)->sections,
139 config_item_perf_lookup, load_fragment_gperf_lookup,
140 0, u);
141
142 u->dropin_mtime = now(CLOCK_REALTIME);
143
144 return 0;
145 }