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