udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / basic / conf-files.c
bloba56f82f8a3e8f309e592006ead319626598b13d4
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <stdlib.h>
8 #include "chase.h"
9 #include "conf-files.h"
10 #include "constants.h"
11 #include "dirent-util.h"
12 #include "fd-util.h"
13 #include "hashmap.h"
14 #include "log.h"
15 #include "macro.h"
16 #include "nulstr-util.h"
17 #include "path-util.h"
18 #include "set.h"
19 #include "sort-util.h"
20 #include "stat-util.h"
21 #include "string-util.h"
22 #include "strv.h"
23 #include "terminal-util.h"
25 static int files_add(
26 DIR *dir,
27 const char *dirpath,
28 Hashmap **files,
29 Set **masked,
30 const char *suffix,
31 unsigned flags) {
33 int r;
35 assert(dir);
36 assert(dirpath);
37 assert(files);
38 assert(masked);
40 FOREACH_DIRENT(de, dir, return -errno) {
41 _cleanup_free_ char *n = NULL, *p = NULL;
42 struct stat st;
44 /* Does this match the suffix? */
45 if (suffix && !endswith(de->d_name, suffix))
46 continue;
48 /* Has this file already been found in an earlier directory? */
49 if (hashmap_contains(*files, de->d_name)) {
50 log_debug("Skipping overridden file '%s/%s'.", dirpath, de->d_name);
51 continue;
54 /* Has this been masked in an earlier directory? */
55 if ((flags & CONF_FILES_FILTER_MASKED) && set_contains(*masked, de->d_name)) {
56 log_debug("File '%s/%s' is masked by previous entry.", dirpath, de->d_name);
57 continue;
60 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
61 if (flags & (CONF_FILES_FILTER_MASKED|CONF_FILES_REGULAR|CONF_FILES_DIRECTORY|CONF_FILES_EXECUTABLE))
62 if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
63 log_debug_errno(errno, "Failed to stat '%s/%s', ignoring: %m", dirpath, de->d_name);
64 continue;
67 /* Is this a masking entry? */
68 if ((flags & CONF_FILES_FILTER_MASKED))
69 if (null_or_empty(&st)) {
70 /* Mark this one as masked */
71 r = set_put_strdup(masked, de->d_name);
72 if (r < 0)
73 return r;
75 log_debug("File '%s/%s' is a mask.", dirpath, de->d_name);
76 continue;
79 /* Does this node have the right type? */
80 if (flags & (CONF_FILES_REGULAR|CONF_FILES_DIRECTORY))
81 if (!((flags & CONF_FILES_DIRECTORY) && S_ISDIR(st.st_mode)) &&
82 !((flags & CONF_FILES_REGULAR) && S_ISREG(st.st_mode))) {
83 log_debug("Ignoring '%s/%s', as it does not have the right type.", dirpath, de->d_name);
84 continue;
87 /* Does this node have the executable bit set? */
88 if (flags & CONF_FILES_EXECUTABLE)
89 /* As requested: check if the file is marked executable. Note that we don't check access(X_OK)
90 * here, as we care about whether the file is marked executable at all, and not whether it is
91 * executable for us, because if so, such errors are stuff we should log about. */
93 if ((st.st_mode & 0111) == 0) { /* not executable */
94 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath, de->d_name);
95 continue;
98 n = strdup(de->d_name);
99 if (!n)
100 return -ENOMEM;
102 if ((flags & CONF_FILES_BASENAME))
103 r = hashmap_ensure_put(files, &string_hash_ops_free, n, n);
104 else {
105 p = path_join(dirpath, de->d_name);
106 if (!p)
107 return -ENOMEM;
109 r = hashmap_ensure_put(files, &string_hash_ops_free_free, n, p);
111 if (r < 0)
112 return r;
113 assert(r > 0);
115 TAKE_PTR(n);
116 TAKE_PTR(p);
119 return 0;
122 static int base_cmp(char * const *a, char * const *b) {
123 assert(a);
124 assert(b);
125 return path_compare_filename(*a, *b);
128 static int copy_and_sort_files_from_hashmap(Hashmap *fh, char ***ret) {
129 _cleanup_free_ char **sv = NULL;
130 char **files;
132 assert(ret);
134 sv = hashmap_get_strv(fh);
135 if (!sv)
136 return -ENOMEM;
138 /* The entries in the array given by hashmap_get_strv() are still owned by the hashmap. */
139 files = strv_copy(sv);
140 if (!files)
141 return -ENOMEM;
143 typesafe_qsort(files, strv_length(files), base_cmp);
145 *ret = files;
146 return 0;
149 int conf_files_list_strv(
150 char ***ret,
151 const char *suffix,
152 const char *root,
153 unsigned flags,
154 const char * const *dirs) {
156 _cleanup_hashmap_free_ Hashmap *fh = NULL;
157 _cleanup_set_free_ Set *masked = NULL;
158 int r;
160 assert(ret);
162 STRV_FOREACH(p, dirs) {
163 _cleanup_closedir_ DIR *dir = NULL;
164 _cleanup_free_ char *path = NULL;
166 r = chase_and_opendir(*p, root, CHASE_PREFIX_ROOT, &path, &dir);
167 if (r < 0) {
168 if (r != -ENOENT)
169 log_debug_errno(r, "Failed to chase and open directory '%s', ignoring: %m", *p);
170 continue;
173 r = files_add(dir, path, &fh, &masked, suffix, flags);
174 if (r == -ENOMEM)
175 return r;
176 if (r < 0)
177 log_debug_errno(r, "Failed to search for files in '%s', ignoring: %m", path);
180 return copy_and_sort_files_from_hashmap(fh, ret);
183 int conf_files_list_strv_at(
184 char ***ret,
185 const char *suffix,
186 int rfd,
187 unsigned flags,
188 const char * const *dirs) {
190 _cleanup_hashmap_free_ Hashmap *fh = NULL;
191 _cleanup_set_free_ Set *masked = NULL;
192 int r;
194 assert(rfd >= 0 || rfd == AT_FDCWD);
195 assert(ret);
197 STRV_FOREACH(p, dirs) {
198 _cleanup_closedir_ DIR *dir = NULL;
199 _cleanup_free_ char *path = NULL;
201 r = chase_and_opendirat(rfd, *p, CHASE_AT_RESOLVE_IN_ROOT, &path, &dir);
202 if (r < 0) {
203 if (r != -ENOENT)
204 log_debug_errno(r, "Failed to chase and open directory '%s', ignoring: %m", *p);
205 continue;
208 r = files_add(dir, path, &fh, &masked, suffix, flags);
209 if (r == -ENOMEM)
210 return r;
211 if (r < 0)
212 log_debug_errno(r, "Failed to search for files in '%s', ignoring: %m", path);
215 return copy_and_sort_files_from_hashmap(fh, ret);
218 int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
219 /* Insert a path into strv, at the place honouring the usual sorting rules:
220 * - we first compare by the basename
221 * - and then we compare by dirname, allowing just one file with the given
222 * basename.
223 * This means that we will
224 * - add a new entry if basename(path) was not on the list,
225 * - do nothing if an entry with higher priority was already present,
226 * - do nothing if our new entry matches the existing entry,
227 * - replace the existing entry if our new entry has higher priority.
229 size_t i, n;
230 char *t;
231 int r;
233 n = strv_length(*strv);
234 for (i = 0; i < n; i++) {
235 int c;
237 c = base_cmp((char* const*) *strv + i, (char* const*) &path);
238 if (c == 0)
239 /* Oh, there already is an entry with a matching name (the last component). */
240 STRV_FOREACH(dir, dirs) {
241 _cleanup_free_ char *rdir = NULL;
242 char *p1, *p2;
244 rdir = path_join(root, *dir);
245 if (!rdir)
246 return -ENOMEM;
248 p1 = path_startswith((*strv)[i], rdir);
249 if (p1)
250 /* Existing entry with higher priority
251 * or same priority, no need to do anything. */
252 return 0;
254 p2 = path_startswith(path, *dir);
255 if (p2) {
256 /* Our new entry has higher priority */
258 t = path_join(root, path);
259 if (!t)
260 return log_oom();
262 return free_and_replace((*strv)[i], t);
266 else if (c > 0)
267 /* Following files have lower priority, let's go insert our
268 * new entry. */
269 break;
271 /* … we are not there yet, let's continue */
274 /* The new file has lower priority than all the existing entries */
275 t = path_join(root, path);
276 if (!t)
277 return -ENOMEM;
279 r = strv_insert(strv, i, t);
280 if (r < 0)
281 free(t);
283 return r;
286 int conf_files_list(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dir) {
287 return conf_files_list_strv(ret, suffix, root, flags, STRV_MAKE_CONST(dir));
290 int conf_files_list_at(char ***ret, const char *suffix, int rfd, unsigned flags, const char *dir) {
291 return conf_files_list_strv_at(ret, suffix, rfd, flags, STRV_MAKE_CONST(dir));
294 int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dirs) {
295 _cleanup_strv_free_ char **d = NULL;
297 assert(ret);
299 d = strv_split_nulstr(dirs);
300 if (!d)
301 return -ENOMEM;
303 return conf_files_list_strv(ret, suffix, root, flags, (const char**) d);
306 int conf_files_list_nulstr_at(char ***ret, const char *suffix, int rfd, unsigned flags, const char *dirs) {
307 _cleanup_strv_free_ char **d = NULL;
309 assert(ret);
311 d = strv_split_nulstr(dirs);
312 if (!d)
313 return -ENOMEM;
315 return conf_files_list_strv_at(ret, suffix, rfd, flags, (const char**) d);
318 int conf_files_list_with_replacement(
319 const char *root,
320 char **config_dirs,
321 const char *replacement,
322 char ***ret_files,
323 char **ret_replace_file) {
325 _cleanup_strv_free_ char **f = NULL;
326 _cleanup_free_ char *p = NULL;
327 int r;
329 assert(config_dirs);
330 assert(ret_files);
331 assert(ret_replace_file || !replacement);
333 r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs);
334 if (r < 0)
335 return log_error_errno(r, "Failed to enumerate config files: %m");
337 if (replacement) {
338 r = conf_files_insert(&f, root, config_dirs, replacement);
339 if (r < 0)
340 return log_error_errno(r, "Failed to extend config file list: %m");
342 p = path_join(root, replacement);
343 if (!p)
344 return log_oom();
347 *ret_files = TAKE_PTR(f);
348 if (ret_replace_file)
349 *ret_replace_file = TAKE_PTR(p);
351 return 0;
354 int conf_files_list_dropins(
355 char ***ret,
356 const char *dropin_dirname,
357 const char *root,
358 const char * const *dirs) {
360 _cleanup_strv_free_ char **dropin_dirs = NULL;
361 const char *suffix;
362 int r;
364 assert(ret);
365 assert(dropin_dirname);
366 assert(dirs);
368 suffix = strjoina("/", dropin_dirname);
369 r = strv_extend_strv_concat(&dropin_dirs, (char**) dirs, suffix);
370 if (r < 0)
371 return r;
373 return conf_files_list_strv(ret, ".conf", root, 0, (const char* const*) dropin_dirs);