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