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