]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-dropin.c
conf-parser: turn three bool function params into a flags fields
[thirdparty/systemd.git] / src / core / load-dropin.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20
21 #include "conf-parser.h"
22 #include "fs-util.h"
23 #include "load-dropin.h"
24 #include "load-fragment.h"
25 #include "log.h"
26 #include "stat-util.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "unit-name.h"
30 #include "unit.h"
31
32 static int unit_name_compatible(const char *a, const char *b) {
33 _cleanup_free_ char *prefix = NULL;
34 int r;
35
36 /* the straightforward case: the symlink name matches the target */
37 if (streq(a, b))
38 return 1;
39
40 r = unit_name_template(a, &prefix);
41 if (r == -EINVAL)
42 /* not a template */
43 return 0;
44 if (r < 0)
45 /* oom, or some other failure. Just skip the warning. */
46 return r;
47
48 /* an instance name points to a target that is just the template name */
49 if (streq(prefix, b))
50 return 1;
51
52 return 0;
53 }
54
55 static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) {
56 _cleanup_strv_free_ char **paths = NULL;
57 char **p;
58 int r;
59
60 r = unit_file_find_dropin_paths(NULL,
61 u->manager->lookup_paths.search_path,
62 u->manager->unit_path_cache,
63 dir_suffix,
64 NULL,
65 u->names,
66 &paths);
67 if (r < 0)
68 return r;
69
70 STRV_FOREACH(p, paths) {
71 const char *entry;
72 _cleanup_free_ char *target = NULL;
73
74 entry = basename(*p);
75
76 if (null_or_empty_path(*p) > 0) {
77 /* an error usually means an invalid symlink, which is not a mask */
78 log_unit_debug(u, "%s dependency on %s is masked by %s, ignoring.",
79 unit_dependency_to_string(dependency), entry, *p);
80 continue;
81 }
82
83 r = is_symlink(*p);
84 if (r < 0) {
85 log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m",
86 unit_dependency_to_string(dependency), *p);
87 continue;
88 }
89 if (r == 0) {
90 log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.",
91 unit_dependency_to_string(dependency), *p);
92 continue;
93 }
94
95 if (!unit_name_is_valid(entry, UNIT_NAME_ANY)) {
96 log_unit_warning(u, "%s dependency dropin %s is not a valid unit name, ignoring.",
97 unit_dependency_to_string(dependency), *p);
98 continue;
99 }
100
101 r = readlink_malloc(*p, &target);
102 if (r < 0) {
103 log_unit_warning_errno(u, r, "readlink(\"%s\") failed, ignoring: %m", *p);
104 continue;
105 }
106
107 /* We don't treat this as an error, especially because we didn't check this for a
108 * long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */
109 r = unit_name_compatible(entry, basename(target));
110 if (r < 0) {
111 log_unit_warning_errno(u, r, "Can't check if names %s and %s are compatible, ignoring: %m", entry, basename(target));
112 continue;
113 }
114 if (r == 0)
115 log_unit_warning(u, "%s dependency dropin %s target %s has different name",
116 unit_dependency_to_string(dependency), *p, target);
117
118 r = unit_add_dependency_by_name(u, dependency, entry, *p, true, UNIT_DEPENDENCY_FILE);
119 if (r < 0)
120 log_unit_error_errno(u, r, "cannot add %s dependency on %s, ignoring: %m",
121 unit_dependency_to_string(dependency), entry);
122 }
123
124 return 0;
125 }
126
127 int unit_load_dropin(Unit *u) {
128 _cleanup_strv_free_ char **l = NULL;
129 char **f;
130 int r;
131
132 assert(u);
133
134 /* Load dependencies from .wants and .requires directories */
135 r = process_deps(u, UNIT_WANTS, ".wants");
136 if (r < 0)
137 return r;
138
139 r = process_deps(u, UNIT_REQUIRES, ".requires");
140 if (r < 0)
141 return r;
142
143 /* Load .conf dropins */
144 r = unit_find_dropin_paths(u, &l);
145 if (r <= 0)
146 return 0;
147
148 if (!u->dropin_paths) {
149 u->dropin_paths = l;
150 l = NULL;
151 } else {
152 r = strv_extend_strv(&u->dropin_paths, l, true);
153 if (r < 0)
154 return log_oom();
155 }
156
157 STRV_FOREACH(f, u->dropin_paths)
158 (void) config_parse(u->id, *f, NULL,
159 UNIT_VTABLE(u)->sections,
160 config_item_perf_lookup, load_fragment_gperf_lookup,
161 0, u);
162
163 u->dropin_mtime = now(CLOCK_REALTIME);
164
165 return 0;
166 }