]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/conf-files.c
tree-wide: drop string.h when string-util.h or friends are included
[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
9 #include "conf-files.h"
10 #include "def.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 "path-util.h"
17 #include "set.h"
18 #include "sort-util.h"
19 #include "stat-util.h"
20 #include "string-util.h"
21 #include "strv.h"
22 #include "terminal-util.h"
23
24 static int files_add(
25 Hashmap *h,
26 Set *masked,
27 const char *suffix,
28 const char *root,
29 unsigned flags,
30 const char *path) {
31
32 _cleanup_closedir_ DIR *dir = NULL;
33 const char *dirpath;
34 struct dirent *de;
35 int r;
36
37 assert(h);
38 assert((flags & CONF_FILES_FILTER_MASKED) == 0 || masked);
39 assert(path);
40
41 dirpath = prefix_roota(root, path);
42
43 dir = opendir(dirpath);
44 if (!dir) {
45 if (errno == ENOENT)
46 return 0;
47
48 return log_debug_errno(errno, "Failed to open directory '%s': %m", dirpath);
49 }
50
51 FOREACH_DIRENT(de, dir, return -errno) {
52 struct stat st;
53 char *p, *key;
54
55 /* Does this match the suffix? */
56 if (suffix && !endswith(de->d_name, suffix))
57 continue;
58
59 /* Has this file already been found in an earlier directory? */
60 if (hashmap_contains(h, de->d_name)) {
61 log_debug("Skipping overridden file '%s/%s'.", dirpath, de->d_name);
62 continue;
63 }
64
65 /* Has this been masked in an earlier directory? */
66 if ((flags & CONF_FILES_FILTER_MASKED) && set_contains(masked, de->d_name)) {
67 log_debug("File '%s/%s' is masked by previous entry.", dirpath, de->d_name);
68 continue;
69 }
70
71 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
72 if (flags & (CONF_FILES_FILTER_MASKED|CONF_FILES_REGULAR|CONF_FILES_DIRECTORY|CONF_FILES_EXECUTABLE))
73 if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
74 log_debug_errno(errno, "Failed to stat '%s/%s', ignoring: %m", dirpath, de->d_name);
75 continue;
76 }
77
78 /* Is this a masking entry? */
79 if ((flags & CONF_FILES_FILTER_MASKED))
80 if (null_or_empty(&st)) {
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 is not a of 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(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
140 _cleanup_hashmap_free_ Hashmap *fh = NULL;
141 _cleanup_set_free_free_ Set *masked = NULL;
142 char **files, **p;
143 int r;
144
145 assert(strv);
146
147 /* This alters the dirs string array */
148 if (!path_strv_resolve_uniq(dirs, root))
149 return -ENOMEM;
150
151 fh = hashmap_new(&path_hash_ops);
152 if (!fh)
153 return -ENOMEM;
154
155 if (flags & CONF_FILES_FILTER_MASKED) {
156 masked = set_new(&path_hash_ops);
157 if (!masked)
158 return -ENOMEM;
159 }
160
161 STRV_FOREACH(p, dirs) {
162 r = files_add(fh, masked, suffix, root, flags, *p);
163 if (r == -ENOMEM)
164 return r;
165 if (r < 0)
166 log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
167 }
168
169 files = hashmap_get_strv(fh);
170 if (!files)
171 return -ENOMEM;
172
173 typesafe_qsort(files, hashmap_size(fh), base_cmp);
174 *strv = files;
175
176 return 0;
177 }
178
179 int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
180 /* Insert a path into strv, at the place honouring the usual sorting rules:
181 * - we first compare by the basename
182 * - and then we compare by dirname, allowing just one file with the given
183 * basename.
184 * This means that we will
185 * - add a new entry if basename(path) was not on the list,
186 * - do nothing if an entry with higher priority was already present,
187 * - do nothing if our new entry matches the existing entry,
188 * - replace the existing entry if our new entry has higher priority.
189 */
190 size_t i, n;
191 char *t;
192 int r;
193
194 n = strv_length(*strv);
195 for (i = 0; i < n; i++) {
196 int c;
197
198 c = base_cmp((char* const*) *strv + i, (char* const*) &path);
199 if (c == 0) {
200 char **dir;
201
202 /* Oh, there already is an entry with a matching name (the last component). */
203
204 STRV_FOREACH(dir, dirs) {
205 _cleanup_free_ char *rdir = NULL;
206 char *p1, *p2;
207
208 rdir = path_join(root, *dir);
209 if (!rdir)
210 return -ENOMEM;
211
212 p1 = path_startswith((*strv)[i], rdir);
213 if (p1)
214 /* Existing entry with higher priority
215 * or same priority, no need to do anything. */
216 return 0;
217
218 p2 = path_startswith(path, *dir);
219 if (p2) {
220 /* Our new entry has higher priority */
221
222 t = path_join(root, path);
223 if (!t)
224 return log_oom();
225
226 return free_and_replace((*strv)[i], t);
227 }
228 }
229
230 } else if (c > 0)
231 /* Following files have lower priority, let's go insert our
232 * new entry. */
233 break;
234
235 /* … we are not there yet, let's continue */
236 }
237
238 /* The new file has lower priority than all the existing entries */
239 t = path_join(root, path);
240 if (!t)
241 return -ENOMEM;
242
243 r = strv_insert(strv, i, t);
244 if (r < 0)
245 free(t);
246
247 return r;
248 }
249
250 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
251 _cleanup_strv_free_ char **copy = NULL;
252
253 assert(strv);
254
255 copy = strv_copy((char**) dirs);
256 if (!copy)
257 return -ENOMEM;
258
259 return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
260 }
261
262 int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir) {
263 _cleanup_strv_free_ char **dirs = NULL;
264
265 assert(strv);
266
267 dirs = strv_new(dir);
268 if (!dirs)
269 return -ENOMEM;
270
271 return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
272 }
273
274 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dirs) {
275 _cleanup_strv_free_ char **d = NULL;
276
277 assert(strv);
278
279 d = strv_split_nulstr(dirs);
280 if (!d)
281 return -ENOMEM;
282
283 return conf_files_list_strv_internal(strv, suffix, root, flags, d);
284 }
285
286 int conf_files_list_with_replacement(
287 const char *root,
288 char **config_dirs,
289 const char *replacement,
290 char ***files,
291 char **replace_file) {
292
293 _cleanup_strv_free_ char **f = NULL;
294 _cleanup_free_ char *p = NULL;
295 int r;
296
297 assert(config_dirs);
298 assert(files);
299 assert(replace_file || !replacement);
300
301 r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs);
302 if (r < 0)
303 return log_error_errno(r, "Failed to enumerate config files: %m");
304
305 if (replacement) {
306 r = conf_files_insert(&f, root, config_dirs, replacement);
307 if (r < 0)
308 return log_error_errno(r, "Failed to extend config file list: %m");
309
310 p = path_join(root, replacement);
311 if (!p)
312 return log_oom();
313 }
314
315 *files = TAKE_PTR(f);
316 if (replace_file)
317 *replace_file = TAKE_PTR(p);
318 return 0;
319 }