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