]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/dropin.c
strv: make iterator in STRV_FOREACH() declaread in the loop
[thirdparty/systemd.git] / src / shared / dropin.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
29686440 2
a8fbdf54
TA
3#include <errno.h>
4#include <stdarg.h>
5#include <stdio.h>
6#include <stdlib.h>
7
b5efdb8a 8#include "alloc-util.h"
f4351959 9#include "chase-symlinks.h"
1a7f1b38 10#include "conf-files.h"
8fb3f009 11#include "dirent-util.h"
3ffd4af2 12#include "dropin.h"
4f5dd394 13#include "escape.h"
3ffd4af2 14#include "fd-util.h"
4f5dd394 15#include "fileio-label.h"
a8fbdf54
TA
16#include "hashmap.h"
17#include "log.h"
18#include "macro.h"
4f5dd394 19#include "mkdir.h"
bb15fafe 20#include "path-util.h"
a8fbdf54 21#include "set.h"
07630cea 22#include "string-util.h"
4f5dd394 23#include "strv.h"
a8fbdf54 24#include "unit-name.h"
29686440 25
8eea8687 26int drop_in_file(const char *dir, const char *unit, unsigned level,
f1b4b94c 27 const char *name, char **ret_p, char **ret_q) {
29686440 28
09c62487 29 char prefix[DECIMAL_STR_MAX(unsigned)];
f1b4b94c 30 _cleanup_free_ char *b = NULL, *p = NULL, *q = NULL;
29686440
ZJS
31
32 assert(unit);
33 assert(name);
f1b4b94c
ZJS
34 assert(ret_p);
35 assert(ret_q);
29686440 36
8eea8687
ZJS
37 sprintf(prefix, "%u", level);
38
29686440
ZJS
39 b = xescape(name, "/.");
40 if (!b)
41 return -ENOMEM;
42
ae6c3cc0 43 if (!filename_is_valid(b))
29686440
ZJS
44 return -EINVAL;
45
605405c6 46 p = strjoin(dir, "/", unit, ".d");
605405c6 47 q = strjoin(p, "/", prefix, "-", b, ".conf");
f1b4b94c 48 if (!p || !q)
29686440 49 return -ENOMEM;
29686440 50
f1b4b94c
ZJS
51 *ret_p = TAKE_PTR(p);
52 *ret_q = TAKE_PTR(q);
29686440
ZJS
53 return 0;
54}
55
8eea8687 56int write_drop_in(const char *dir, const char *unit, unsigned level,
29686440
ZJS
57 const char *name, const char *data) {
58
59 _cleanup_free_ char *p = NULL, *q = NULL;
60 int r;
61
62 assert(dir);
63 assert(unit);
64 assert(name);
65 assert(data);
66
8eea8687 67 r = drop_in_file(dir, unit, level, name, &p, &q);
29686440
ZJS
68 if (r < 0)
69 return r;
70
45519fd6 71 (void) mkdir_p(p, 0755);
29686440
ZJS
72 return write_string_file_atomic_label(q, data);
73}
74
8eea8687 75int write_drop_in_format(const char *dir, const char *unit, unsigned level,
29686440
ZJS
76 const char *name, const char *format, ...) {
77 _cleanup_free_ char *p = NULL;
78 va_list ap;
79 int r;
80
81 assert(dir);
82 assert(unit);
83 assert(name);
84 assert(format);
85
86 va_start(ap, format);
87 r = vasprintf(&p, format, ap);
88 va_end(ap);
89
90 if (r < 0)
91 return -ENOMEM;
92
8eea8687 93 return write_drop_in(dir, unit, level, name, p);
29686440 94}
1a7f1b38 95
47a00111 96static int unit_file_add_dir(
17e78d18 97 const char *original_root,
dcc4f30e
ZJS
98 const char *path,
99 char ***dirs) {
1a7f1b38 100
17e78d18 101 _cleanup_free_ char *chased = NULL;
1a7f1b38
ZJS
102 int r;
103
104 assert(path);
105
47a00111
ZJS
106 /* This adds [original_root]/path to dirs, if it exists. */
107
a5648b80 108 r = chase_symlinks(path, original_root, 0, &chased, NULL);
952713b0 109 if (r == -ENOENT) /* Ignore -ENOENT, after all most units won't have a drop-in dir. */
09c62487 110 return 0;
952713b0
LP
111 if (r == -ENAMETOOLONG) {
112 /* Also, ignore -ENAMETOOLONG but log about it. After all, users are not even able to create the
113 * drop-in dir in such case. This mostly happens for device units with an overly long /sys path. */
114 log_debug_errno(r, "Path '%s' too long, couldn't canonicalize, ignoring.", path);
115 return 0;
116 }
17e78d18 117 if (r < 0)
952713b0 118 return log_warning_errno(r, "Failed to canonicalize path '%s': %m", path);
17e78d18 119
f1b4b94c 120 if (strv_consume(dirs, TAKE_PTR(chased)) < 0)
dcc4f30e 121 return log_oom();
1a7f1b38
ZJS
122
123 return 0;
124}
125
dcc4f30e 126static int unit_file_find_dirs(
17e78d18 127 const char *original_root,
7410616c 128 Set *unit_path_cache,
1a7f1b38
ZJS
129 const char *unit_path,
130 const char *name,
131 const char *suffix,
dcc4f30e 132 char ***dirs) {
1a7f1b38 133
53966245
LP
134 _cleanup_free_ char *prefix = NULL, *instance = NULL, *built = NULL;
135 bool is_instance, chopped;
136 const char *dash;
137 UnitType type;
96bb2fd8 138 char *path;
53966245 139 size_t n;
7410616c 140 int r;
1a7f1b38
ZJS
141
142 assert(unit_path);
143 assert(name);
144 assert(suffix);
145
96bb2fd8 146 path = strjoina(unit_path, "/", name, suffix);
dcc4f30e 147 if (!unit_path_cache || set_get(unit_path_cache, path)) {
47a00111 148 r = unit_file_add_dir(original_root, path, dirs);
dcc4f30e
ZJS
149 if (r < 0)
150 return r;
151 }
1a7f1b38 152
53966245
LP
153 is_instance = unit_name_is_valid(name, UNIT_NAME_INSTANCE);
154 if (is_instance) { /* Also try the template dir */
a09d3eaf
LP
155 _cleanup_free_ char *template = NULL;
156
7410616c
LP
157 r = unit_name_template(name, &template);
158 if (r < 0)
159 return log_error_errno(r, "Failed to generate template from unit name: %m");
1a7f1b38 160
53966245
LP
161 r = unit_file_find_dirs(original_root, unit_path_cache, unit_path, template, suffix, dirs);
162 if (r < 0)
163 return r;
1a7f1b38
ZJS
164 }
165
3e1db806
AZ
166 /* Return early for top level drop-ins. */
167 if (unit_type_from_string(name) >= 0)
168 return 0;
169
53966245
LP
170 /* Let's see if there's a "-" prefix for this unit name. If so, let's invoke ourselves for it. This will then
171 * recursively do the same for all our prefixes. i.e. this means given "foo-bar-waldo.service" we'll also
172 * search "foo-bar-.service" and "foo-.service".
173 *
174 * Note the order in which we do it: we traverse up adding drop-ins on each step. This means the more specific
175 * drop-ins may override the more generic drop-ins, which is the intended behaviour. */
176
177 r = unit_name_to_prefix(name, &prefix);
178 if (r < 0)
179 return log_error_errno(r, "Failed to derive unit name prefix from unit name: %m");
180
181 chopped = false;
182 for (;;) {
183 dash = strrchr(prefix, '-');
184 if (!dash) /* No dash? if so we are done */
185 return 0;
186
187 n = (size_t) (dash - prefix);
188 if (n == 0) /* Leading dash? If so, we are done */
189 return 0;
190
191 if (prefix[n+1] != 0 || chopped) {
192 prefix[n+1] = 0;
193 break;
194 }
195
196 /* Trailing dash? If so, chop it off and try again, but not more than once. */
197 prefix[n] = 0;
198 chopped = true;
199 }
200
201 if (!unit_prefix_is_valid(prefix))
202 return 0;
203
204 type = unit_name_to_type(name);
baaa35ad
ZJS
205 if (type < 0)
206 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
cd990847 207 "Failed to derive unit type from unit name: %s",
baaa35ad 208 name);
53966245
LP
209
210 if (is_instance) {
211 r = unit_name_to_instance(name, &instance);
212 if (r < 0)
213 return log_error_errno(r, "Failed to derive unit name instance from unit name: %m");
214 }
215
216 r = unit_name_build_from_type(prefix, instance, type, &built);
217 if (r < 0)
218 return log_error_errno(r, "Failed to build prefix unit name: %m");
219
220 return unit_file_find_dirs(original_root, unit_path_cache, unit_path, built, suffix, dirs);
1a7f1b38
ZJS
221}
222
223int unit_file_find_dropin_paths(
17e78d18 224 const char *original_root,
1a7f1b38
ZJS
225 char **lookup_path,
226 Set *unit_path_cache,
95778782
ZJS
227 const char *dir_suffix,
228 const char *file_suffix,
4562c355
ZJS
229 const char *name,
230 const Set *aliases,
058db925 231 char ***ret) {
1a7f1b38 232
3f6de63b 233 _cleanup_strv_free_ char **dirs = NULL;
4562c355 234 const char *n;
1a7f1b38
ZJS
235 int r;
236
058db925 237 assert(ret);
1a7f1b38 238
4562c355 239 if (name)
e6627f23
GGM
240 STRV_FOREACH(p, lookup_path)
241 (void) unit_file_find_dirs(original_root, unit_path_cache, *p, name, dir_suffix, &dirs);
242
90e74a66 243 SET_FOREACH(n, aliases)
4562c355
ZJS
244 STRV_FOREACH(p, lookup_path)
245 (void) unit_file_find_dirs(original_root, unit_path_cache, *p, n, dir_suffix, &dirs);
246
d2724678 247 /* All the names in the unit are of the same type so just grab one. */
4562c355
ZJS
248 n = name ?: (const char*) set_first(aliases);
249 if (n) {
7a670b1d
TM
250 UnitType type = _UNIT_TYPE_INVALID;
251
4562c355 252 type = unit_name_to_type(n);
d2724678
AZ
253 if (type < 0)
254 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
cd990847 255 "Failed to derive unit type from unit name: %s", n);
d2724678 256
e6627f23 257 /* Special top level drop in for "<unit type>.<suffix>". Add this last as it's the most generic
7a670b1d
TM
258 * and should be able to be overridden by more specific drop-ins. */
259 STRV_FOREACH(p, lookup_path)
260 (void) unit_file_find_dirs(original_root,
261 unit_path_cache,
262 *p,
263 unit_type_to_string(type),
264 dir_suffix,
265 &dirs);
266 }
d2724678 267
058db925
LP
268 if (strv_isempty(dirs)) {
269 *ret = NULL;
1a7f1b38 270 return 0;
058db925 271 }
1a7f1b38 272
b5084605 273 r = conf_files_list_strv(ret, file_suffix, NULL, 0, (const char**) dirs);
1a7f1b38 274 if (r < 0)
3f6de63b 275 return log_warning_errno(r, "Failed to create the list of configuration files: %m");
058db925 276
1a7f1b38
ZJS
277 return 1;
278}