]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/conf-files.c
8e0fb06ad9096bf2a324144da1efb37c6ce97fc3
[thirdparty/systemd.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 "dirent-util.h"
17 #include "fd-util.h"
18 #include "hashmap.h"
19 #include "log.h"
20 #include "macro.h"
21 #include "missing.h"
22 #include "path-util.h"
23 #include "stat-util.h"
24 #include "string-util.h"
25 #include "strv.h"
26 #include "util.h"
27
28 static int files_add(Hashmap *h, const char *suffix, const char *root, unsigned flags, const char *path) {
29 _cleanup_closedir_ DIR *dir = NULL;
30 const char *dirpath;
31 struct dirent *de;
32 int r;
33
34 assert(path);
35
36 dirpath = prefix_roota(root, path);
37
38 dir = opendir(dirpath);
39 if (!dir) {
40 if (errno == ENOENT)
41 return 0;
42 return -errno;
43 }
44
45 FOREACH_DIRENT(de, dir, return -errno) {
46 char *p;
47
48 if (!dirent_is_file_with_suffix(de, suffix)) {
49 log_debug("Ignoring %s/%s, because it's not a regular file with suffix %s.", dirpath, de->d_name, strna(suffix));
50 continue;
51 }
52
53 if (flags & CONF_FILES_EXECUTABLE) {
54 struct stat st;
55
56 /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
57 * here, as we care about whether the file is marked executable at all, and not whether it is
58 * executable for us, because if such errors are stuff we should log about. */
59
60 if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
61 log_debug_errno(errno, "Failed to stat %s/%s, ignoring: %m", dirpath, de->d_name);
62 continue;
63 }
64
65 if (!null_or_empty(&st)) {
66 /* A mask is a symlink to /dev/null or an empty file. It does not even
67 * have to be executable. Other entries must be regular executable files
68 * or symlinks to them. */
69 if (S_ISREG(st.st_mode)) {
70 if ((st.st_mode & 0111) == 0) { /* not executable */
71 log_debug("Ignoring %s/%s, as it is not marked executable.",
72 dirpath, de->d_name);
73 continue;
74 }
75 } else {
76 log_debug("Ignoring %s/%s, as it is neither a regular file nor a mask.",
77 dirpath, de->d_name);
78 continue;
79 }
80 }
81 }
82
83 p = strjoin(dirpath, "/", de->d_name);
84 if (!p)
85 return -ENOMEM;
86
87 r = hashmap_put(h, basename(p), p);
88 if (r == -EEXIST) {
89 log_debug("Skipping overridden file: %s.", p);
90 free(p);
91 } else if (r < 0) {
92 free(p);
93 return r;
94 } else if (r == 0) {
95 log_debug("Duplicate file %s", p);
96 free(p);
97 }
98 }
99
100 return 0;
101 }
102
103 static int base_cmp(const void *a, const void *b) {
104 const char *s1, *s2;
105
106 s1 = *(char * const *)a;
107 s2 = *(char * const *)b;
108 return strcmp(basename(s1), basename(s2));
109 }
110
111 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
112 _cleanup_hashmap_free_ Hashmap *fh = NULL;
113 char **files, **p;
114 int r;
115
116 assert(strv);
117
118 /* This alters the dirs string array */
119 if (!path_strv_resolve_uniq(dirs, root))
120 return -ENOMEM;
121
122 fh = hashmap_new(&string_hash_ops);
123 if (!fh)
124 return -ENOMEM;
125
126 STRV_FOREACH(p, dirs) {
127 r = files_add(fh, suffix, root, flags, *p);
128 if (r == -ENOMEM)
129 return r;
130 if (r < 0)
131 log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
132 }
133
134 files = hashmap_get_strv(fh);
135 if (!files)
136 return -ENOMEM;
137
138 qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
139 *strv = files;
140
141 return 0;
142 }
143
144 int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
145 /* Insert a path into strv, at the place honouring the usual sorting rules:
146 * - we first compare by the basename
147 * - and then we compare by dirname, allowing just one file with the given
148 * basename.
149 * This means that we will
150 * - add a new entry if basename(path) was not on the list,
151 * - do nothing if an entry with higher priority was already present,
152 * - do nothing if our new entry matches the existing entry,
153 * - replace the existing entry if our new entry has higher priority.
154 */
155 char *t;
156 unsigned i;
157 int r;
158
159 for (i = 0; i < strv_length(*strv); i++) {
160 int c;
161
162 c = base_cmp(*strv + i, &path);
163 if (c == 0) {
164 char **dir;
165
166 /* Oh, we found our spot and it already contains something. */
167 STRV_FOREACH(dir, dirs) {
168 char *p1, *p2;
169
170 p1 = path_startswith((*strv)[i], root);
171 if (p1)
172 /* Skip "/" in *dir, because p1 is without "/" too */
173 p1 = path_startswith(p1, *dir + 1);
174 if (p1)
175 /* Existing entry with higher priority
176 * or same priority, no need to do anything. */
177 return 0;
178
179 p2 = path_startswith(path, *dir);
180 if (p2) {
181 /* Our new entry has higher priority */
182 t = path_join(root, path, NULL);
183 if (!t)
184 return log_oom();
185
186 return free_and_replace((*strv)[i], t);
187 }
188 }
189
190 } else if (c > 0)
191 /* Following files have lower priority, let's go insert our
192 * new entry. */
193 break;
194
195 /* … we are not there yet, let's continue */
196 }
197
198 t = path_join(root, path, NULL);
199 if (!t)
200 return log_oom();
201
202 r = strv_insert(strv, i, t);
203 if (r < 0)
204 free(t);
205 return r;
206 }
207
208 int conf_files_insert_nulstr(char ***strv, const char *root, const char *dirs, const char *path) {
209 _cleanup_strv_free_ char **d = NULL;
210
211 assert(strv);
212
213 d = strv_split_nulstr(dirs);
214 if (!d)
215 return -ENOMEM;
216
217 return conf_files_insert(strv, root, d, path);
218 }
219
220 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
221 _cleanup_strv_free_ char **copy = NULL;
222
223 assert(strv);
224
225 copy = strv_copy((char**) dirs);
226 if (!copy)
227 return -ENOMEM;
228
229 return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
230 }
231
232 int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
233 _cleanup_strv_free_ char **dirs = NULL;
234 va_list ap;
235
236 assert(strv);
237
238 va_start(ap, dir);
239 dirs = strv_new_ap(dir, ap);
240 va_end(ap);
241
242 if (!dirs)
243 return -ENOMEM;
244
245 return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
246 }
247
248 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dirs) {
249 _cleanup_strv_free_ char **d = NULL;
250
251 assert(strv);
252
253 d = strv_split_nulstr(dirs);
254 if (!d)
255 return -ENOMEM;
256
257 return conf_files_list_strv_internal(strv, suffix, root, flags, d);
258 }