chiark / gitweb /
b29243ee45f8424fc7a159fd134e54c7211196ef
[elogind.git] / src / basic / conf-files.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <dirent.h>
9 #include <errno.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "conf-files.h"
16 //#include "def.h"
17 #include "dirent-util.h"
18 #include "fd-util.h"
19 #include "hashmap.h"
20 #include "log.h"
21 #include "macro.h"
22 #include "missing.h"
23 #include "path-util.h"
24 //#include "set.h"
25 #include "stat-util.h"
26 #include "string-util.h"
27 #include "strv.h"
28 //#include "terminal-util.h"
29 #include "util.h"
30
31 static int files_add(
32                 Hashmap *h,
33                 Set *masked,
34                 const char *suffix,
35                 const char *root,
36                 unsigned flags,
37                 const char *path) {
38
39         _cleanup_closedir_ DIR *dir = NULL;
40         const char *dirpath;
41         struct dirent *de;
42         int r;
43
44         assert(h);
45         assert((flags & CONF_FILES_FILTER_MASKED) == 0 || masked);
46         assert(path);
47
48         dirpath = prefix_roota(root, path);
49
50         dir = opendir(dirpath);
51         if (!dir) {
52                 if (errno == ENOENT)
53                         return 0;
54
55                 return log_debug_errno(errno, "Failed to open directory '%s': %m", dirpath);
56         }
57
58         FOREACH_DIRENT(de, dir, return -errno) {
59                 struct stat st;
60                 char *p, *key;
61
62                 /* Does this match the suffix? */
63                 if (suffix && !endswith(de->d_name, suffix))
64                         continue;
65
66                 /* Has this file already been found in an earlier directory? */
67                 if (hashmap_contains(h, de->d_name)) {
68                         log_debug("Skipping overridden file '%s/%s'.", dirpath, de->d_name);
69                         continue;
70                 }
71
72                 /* Has this been masked in an earlier directory? */
73                 if ((flags & CONF_FILES_FILTER_MASKED) && set_contains(masked, de->d_name)) {
74                         log_debug("File '%s/%s' is masked by previous entry.", dirpath, de->d_name);
75                         continue;
76                 }
77
78                 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
79                 if (flags & (CONF_FILES_FILTER_MASKED|CONF_FILES_REGULAR|CONF_FILES_DIRECTORY|CONF_FILES_EXECUTABLE))
80                         if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
81                                 log_debug_errno(errno, "Failed to stat '%s/%s', ignoring: %m", dirpath, de->d_name);
82                                 continue;
83                         }
84
85                 /* Is this a masking entry? */
86                 if ((flags & CONF_FILES_FILTER_MASKED))
87                         if (null_or_empty(&st)) {
88                                 /* Mark this one as masked */
89                                 r = set_put_strdup(masked, de->d_name);
90                                 if (r < 0)
91                                         return r;
92
93                                 log_debug("File '%s/%s' is a mask.", dirpath, de->d_name);
94                                 continue;
95                         }
96
97                 /* Does this node have the right type? */
98                 if (flags & (CONF_FILES_REGULAR|CONF_FILES_DIRECTORY))
99                         if (!((flags & CONF_FILES_DIRECTORY) && S_ISDIR(st.st_mode)) &&
100                             !((flags & CONF_FILES_REGULAR) && S_ISREG(st.st_mode))) {
101                                 log_debug("Ignoring '%s/%s', as it is not a of the right type.", dirpath, de->d_name);
102                                 continue;
103                         }
104
105                 /* Does this node have the executable bit set? */
106                 if (flags & CONF_FILES_EXECUTABLE)
107                         /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
108                          * here, as we care about whether the file is marked executable at all, and not whether it is
109                          * executable for us, because if so, such errors are stuff we should log about. */
110
111                         if ((st.st_mode & 0111) == 0) { /* not executable */
112                                 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath, de->d_name);
113                                 continue;
114                         }
115
116                 if (flags & CONF_FILES_BASENAME) {
117                         p = strdup(de->d_name);
118                         if (!p)
119                                 return -ENOMEM;
120
121                         key = p;
122                 } else {
123                         p = strjoin(dirpath, "/", de->d_name);
124                         if (!p)
125                                 return -ENOMEM;
126
127                         key = basename(p);
128                 }
129
130                 r = hashmap_put(h, key, p);
131                 if (r < 0) {
132                         free(p);
133                         return log_debug_errno(r, "Failed to add item to hashmap: %m");
134                 }
135
136                 assert(r > 0);
137         }
138
139         return 0;
140 }
141
142 static int base_cmp(const void *a, const void *b) {
143         const char *s1, *s2;
144
145         s1 = *(char * const *)a;
146         s2 = *(char * const *)b;
147         return strcmp(basename(s1), basename(s2));
148 }
149
150 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
151         _cleanup_hashmap_free_ Hashmap *fh = NULL;
152         _cleanup_set_free_free_ Set *masked = NULL;
153         char **files, **p;
154         int r;
155
156         assert(strv);
157
158         /* This alters the dirs string array */
159         if (!path_strv_resolve_uniq(dirs, root))
160                 return -ENOMEM;
161
162         fh = hashmap_new(&path_hash_ops);
163         if (!fh)
164                 return -ENOMEM;
165
166         if (flags & CONF_FILES_FILTER_MASKED) {
167                 masked = set_new(&path_hash_ops);
168                 if (!masked)
169                         return -ENOMEM;
170         }
171
172         STRV_FOREACH(p, dirs) {
173                 r = files_add(fh, masked, suffix, root, flags, *p);
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", *p);
178         }
179
180         files = hashmap_get_strv(fh);
181         if (!files)
182                 return -ENOMEM;
183
184         qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
185         *strv = files;
186
187         return 0;
188 }
189
190 int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
191         /* Insert a path into strv, at the place honouring the usual sorting rules:
192          * - we first compare by the basename
193          * - and then we compare by dirname, allowing just one file with the given
194          *   basename.
195          * This means that we will
196          * - add a new entry if basename(path) was not on the list,
197          * - do nothing if an entry with higher priority was already present,
198          * - do nothing if our new entry matches the existing entry,
199          * - replace the existing entry if our new entry has higher priority.
200          */
201         size_t i;
202         char *t;
203         int r;
204
205         for (i = 0; i < strv_length(*strv); i++) {
206                 int c;
207
208                 c = base_cmp(*strv + i, &path);
209                 if (c == 0) {
210                         char **dir;
211
212                         /* Oh, we found our spot and it already contains something. */
213                         STRV_FOREACH(dir, dirs) {
214                                 char *p1, *p2;
215
216                                 p1 = path_startswith((*strv)[i], root);
217                                 if (p1)
218                                         /* Skip "/" in *dir, because p1 is without "/" too */
219                                         p1 = path_startswith(p1, *dir + 1);
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                                         t = path_join(root, path, NULL);
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         t = path_join(root, path, NULL);
245         if (!t)
246                 return log_oom();
247
248         r = strv_insert(strv, i, t);
249         if (r < 0)
250                 free(t);
251         return r;
252 }
253
254 int conf_files_insert_nulstr(char ***strv, const char *root, const char *dirs, const char *path) {
255         _cleanup_strv_free_ char **d = NULL;
256
257         assert(strv);
258
259         d = strv_split_nulstr(dirs);
260         if (!d)
261                 return -ENOMEM;
262
263         return conf_files_insert(strv, root, d, path);
264 }
265
266 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
267         _cleanup_strv_free_ char **copy = NULL;
268
269         assert(strv);
270
271         copy = strv_copy((char**) dirs);
272         if (!copy)
273                 return -ENOMEM;
274
275         return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
276 }
277
278 int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
279         _cleanup_strv_free_ char **dirs = NULL;
280         va_list ap;
281
282         assert(strv);
283
284         va_start(ap, dir);
285         dirs = strv_new_ap(dir, ap);
286         va_end(ap);
287
288         if (!dirs)
289                 return -ENOMEM;
290
291         return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
292 }
293
294 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dirs) {
295         _cleanup_strv_free_ char **d = NULL;
296
297         assert(strv);
298
299         d = strv_split_nulstr(dirs);
300         if (!d)
301                 return -ENOMEM;
302
303         return conf_files_list_strv_internal(strv, suffix, root, flags, d);
304 }
305
306 int conf_files_list_with_replacement(
307                 const char *root,
308                 char **config_dirs,
309                 const char *replacement,
310                 char ***files,
311                 char **replace_file) {
312
313         _cleanup_strv_free_ char **f = NULL;
314         _cleanup_free_ char *p = NULL;
315         int r;
316
317         assert(config_dirs);
318         assert(files);
319         assert(replace_file || !replacement);
320
321         r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs);
322         if (r < 0)
323                 return log_error_errno(r, "Failed to enumerate config files: %m");
324
325         if (replacement) {
326                 r = conf_files_insert(&f, root, config_dirs, replacement);
327                 if (r < 0)
328                         return log_error_errno(r, "Failed to extend config file list: %m");
329
330                 p = path_join(root, replacement, NULL);
331                 if (!p)
332                         return log_oom();
333         }
334
335         *files = TAKE_PTR(f);
336         if (replace_file)
337                 *replace_file = TAKE_PTR(p);
338         return 0;
339 }
340
341 int conf_files_cat(const char *root, const char *name) {
342         _cleanup_strv_free_ char **dirs = NULL, **files = NULL;
343         _cleanup_free_ char *path = NULL;
344         const char *dir;
345         char **t;
346         int r;
347
348         NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
349                 assert(endswith(dir, "/"));
350                 r = strv_extendf(&dirs, "%s%s.d", dir, name);
351                 if (r < 0)
352                         return log_error_errno(r, "Failed to build directory list: %m");
353         }
354
355         r = conf_files_list_strv(&files, ".conf", root, 0, (const char* const*) dirs);
356         if (r < 0)
357                 return log_error_errno(r, "Failed to query file list: %m");
358
359         path = path_join(root, "/etc", name);
360         if (!path)
361                 return log_oom();
362
363         if (DEBUG_LOGGING) {
364                 log_debug("Looking for configuration in:");
365                 log_debug("   %s", path);
366                 STRV_FOREACH(t, dirs)
367                         log_debug("   %s/*.conf", *t);
368         }
369
370         /* show */
371         return cat_files(path, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);
372 }