]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/conf-files.c
Merge pull request #10994 from poettering/sd-bus-tweaks
[thirdparty/systemd.git] / src / basic / conf-files.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <dirent.h>
4 #include <errno.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "conf-files.h"
11 #include "def.h"
12 #include "dirent-util.h"
13 #include "fd-util.h"
14 #include "hashmap.h"
15 #include "log.h"
16 #include "macro.h"
17 #include "missing.h"
18 #include "path-util.h"
19 #include "set.h"
20 #include "stat-util.h"
21 #include "string-util.h"
22 #include "strv.h"
23 #include "terminal-util.h"
24 #include "util.h"
25
26 static int files_add(
27 Hashmap *h,
28 Set *masked,
29 const char *suffix,
30 const char *root,
31 unsigned flags,
32 const char *path) {
33
34 _cleanup_closedir_ DIR *dir = NULL;
35 const char *dirpath;
36 struct dirent *de;
37 int r;
38
39 assert(h);
40 assert((flags & CONF_FILES_FILTER_MASKED) == 0 || masked);
41 assert(path);
42
43 dirpath = prefix_roota(root, path);
44
45 dir = opendir(dirpath);
46 if (!dir) {
47 if (errno == ENOENT)
48 return 0;
49
50 return log_debug_errno(errno, "Failed to open directory '%s': %m", dirpath);
51 }
52
53 FOREACH_DIRENT(de, dir, return -errno) {
54 struct stat st;
55 char *p, *key;
56
57 /* Does this match the suffix? */
58 if (suffix && !endswith(de->d_name, suffix))
59 continue;
60
61 /* Has this file already been found in an earlier directory? */
62 if (hashmap_contains(h, de->d_name)) {
63 log_debug("Skipping overridden file '%s/%s'.", dirpath, de->d_name);
64 continue;
65 }
66
67 /* Has this been masked in an earlier directory? */
68 if ((flags & CONF_FILES_FILTER_MASKED) && set_contains(masked, de->d_name)) {
69 log_debug("File '%s/%s' is masked by previous entry.", dirpath, de->d_name);
70 continue;
71 }
72
73 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
74 if (flags & (CONF_FILES_FILTER_MASKED|CONF_FILES_REGULAR|CONF_FILES_DIRECTORY|CONF_FILES_EXECUTABLE))
75 if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
76 log_debug_errno(errno, "Failed to stat '%s/%s', ignoring: %m", dirpath, de->d_name);
77 continue;
78 }
79
80 /* Is this a masking entry? */
81 if ((flags & CONF_FILES_FILTER_MASKED))
82 if (null_or_empty(&st)) {
83 /* Mark this one as masked */
84 r = set_put_strdup(masked, de->d_name);
85 if (r < 0)
86 return r;
87
88 log_debug("File '%s/%s' is a mask.", dirpath, de->d_name);
89 continue;
90 }
91
92 /* Does this node have the right type? */
93 if (flags & (CONF_FILES_REGULAR|CONF_FILES_DIRECTORY))
94 if (!((flags & CONF_FILES_DIRECTORY) && S_ISDIR(st.st_mode)) &&
95 !((flags & CONF_FILES_REGULAR) && S_ISREG(st.st_mode))) {
96 log_debug("Ignoring '%s/%s', as it is not a of the right type.", dirpath, de->d_name);
97 continue;
98 }
99
100 /* Does this node have the executable bit set? */
101 if (flags & CONF_FILES_EXECUTABLE)
102 /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
103 * here, as we care about whether the file is marked executable at all, and not whether it is
104 * executable for us, because if so, such errors are stuff we should log about. */
105
106 if ((st.st_mode & 0111) == 0) { /* not executable */
107 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath, de->d_name);
108 continue;
109 }
110
111 if (flags & CONF_FILES_BASENAME) {
112 p = strdup(de->d_name);
113 if (!p)
114 return -ENOMEM;
115
116 key = p;
117 } else {
118 p = strjoin(dirpath, "/", de->d_name);
119 if (!p)
120 return -ENOMEM;
121
122 key = basename(p);
123 }
124
125 r = hashmap_put(h, key, p);
126 if (r < 0) {
127 free(p);
128 return log_debug_errno(r, "Failed to add item to hashmap: %m");
129 }
130
131 assert(r > 0);
132 }
133
134 return 0;
135 }
136
137 static int base_cmp(char * const *a, char * const *b) {
138 return strcmp(basename(*a), basename(*b));
139 }
140
141 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
142 _cleanup_hashmap_free_ Hashmap *fh = NULL;
143 _cleanup_set_free_free_ Set *masked = NULL;
144 char **files, **p;
145 int r;
146
147 assert(strv);
148
149 /* This alters the dirs string array */
150 if (!path_strv_resolve_uniq(dirs, root))
151 return -ENOMEM;
152
153 fh = hashmap_new(&path_hash_ops);
154 if (!fh)
155 return -ENOMEM;
156
157 if (flags & CONF_FILES_FILTER_MASKED) {
158 masked = set_new(&path_hash_ops);
159 if (!masked)
160 return -ENOMEM;
161 }
162
163 STRV_FOREACH(p, dirs) {
164 r = files_add(fh, masked, suffix, root, flags, *p);
165 if (r == -ENOMEM)
166 return r;
167 if (r < 0)
168 log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
169 }
170
171 files = hashmap_get_strv(fh);
172 if (!files)
173 return -ENOMEM;
174
175 typesafe_qsort(files, hashmap_size(fh), base_cmp);
176 *strv = files;
177
178 return 0;
179 }
180
181 int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
182 /* Insert a path into strv, at the place honouring the usual sorting rules:
183 * - we first compare by the basename
184 * - and then we compare by dirname, allowing just one file with the given
185 * basename.
186 * This means that we will
187 * - add a new entry if basename(path) was not on the list,
188 * - do nothing if an entry with higher priority was already present,
189 * - do nothing if our new entry matches the existing entry,
190 * - replace the existing entry if our new entry has higher priority.
191 */
192 size_t i, n;
193 char *t;
194 int r;
195
196 n = strv_length(*strv);
197 for (i = 0; i < n; i++) {
198 int c;
199
200 c = base_cmp((char* const*) *strv + i, (char* const*) &path);
201 if (c == 0) {
202 char **dir;
203
204 /* Oh, we found our spot and it already contains something. */
205 STRV_FOREACH(dir, dirs) {
206 char *p1, *p2;
207
208 p1 = path_startswith((*strv)[i], root);
209 if (p1)
210 /* Skip "/" in *dir, because p1 is without "/" too */
211 p1 = path_startswith(p1, *dir + 1);
212 if (p1)
213 /* Existing entry with higher priority
214 * or same priority, no need to do anything. */
215 return 0;
216
217 p2 = path_startswith(path, *dir);
218 if (p2) {
219 /* Our new entry has higher priority */
220 t = path_join(root, path);
221 if (!t)
222 return log_oom();
223
224 return free_and_replace((*strv)[i], t);
225 }
226 }
227
228 } else if (c > 0)
229 /* Following files have lower priority, let's go insert our
230 * new entry. */
231 break;
232
233 /* … we are not there yet, let's continue */
234 }
235
236 t = path_join(root, path);
237 if (!t)
238 return -ENOMEM;
239
240 r = strv_insert(strv, i, t);
241 if (r < 0)
242 free(t);
243
244 return r;
245 }
246
247 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
248 _cleanup_strv_free_ char **copy = NULL;
249
250 assert(strv);
251
252 copy = strv_copy((char**) dirs);
253 if (!copy)
254 return -ENOMEM;
255
256 return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
257 }
258
259 int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
260 _cleanup_strv_free_ char **dirs = NULL;
261 va_list ap;
262
263 assert(strv);
264
265 va_start(ap, dir);
266 dirs = strv_new_ap(dir, ap);
267 va_end(ap);
268
269 if (!dirs)
270 return -ENOMEM;
271
272 return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
273 }
274
275 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dirs) {
276 _cleanup_strv_free_ char **d = NULL;
277
278 assert(strv);
279
280 d = strv_split_nulstr(dirs);
281 if (!d)
282 return -ENOMEM;
283
284 return conf_files_list_strv_internal(strv, suffix, root, flags, d);
285 }
286
287 int conf_files_list_with_replacement(
288 const char *root,
289 char **config_dirs,
290 const char *replacement,
291 char ***files,
292 char **replace_file) {
293
294 _cleanup_strv_free_ char **f = NULL;
295 _cleanup_free_ char *p = NULL;
296 int r;
297
298 assert(config_dirs);
299 assert(files);
300 assert(replace_file || !replacement);
301
302 r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs);
303 if (r < 0)
304 return log_error_errno(r, "Failed to enumerate config files: %m");
305
306 if (replacement) {
307 r = conf_files_insert(&f, root, config_dirs, replacement);
308 if (r < 0)
309 return log_error_errno(r, "Failed to extend config file list: %m");
310
311 p = path_join(root, replacement);
312 if (!p)
313 return log_oom();
314 }
315
316 *files = TAKE_PTR(f);
317 if (replace_file)
318 *replace_file = TAKE_PTR(p);
319 return 0;
320 }