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