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