]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/conf-files.c
analyze: add 'cat-config' verb
[thirdparty/systemd.git] / src / basic / conf-files.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2c21044f
KS
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
2c21044f
KS
6***/
7
07630cea 8#include <dirent.h>
2c21044f 9#include <errno.h>
11c3a366 10#include <stdarg.h>
2c21044f 11#include <stdio.h>
07630cea
LP
12#include <stdlib.h>
13#include <string.h>
2c21044f 14
3ffd4af2 15#include "conf-files.h"
854a42fb 16#include "def.h"
a0956174 17#include "dirent-util.h"
3ffd4af2 18#include "fd-util.h"
07630cea
LP
19#include "hashmap.h"
20#include "log.h"
2c21044f 21#include "macro.h"
2c21044f 22#include "missing.h"
9eb977db 23#include "path-util.h"
b5084605 24#include "stat-util.h"
07630cea
LP
25#include "string-util.h"
26#include "strv.h"
854a42fb 27#include "terminal-util.h"
07630cea 28#include "util.h"
2c21044f 29
b5084605 30static int files_add(Hashmap *h, const char *suffix, const char *root, unsigned flags, const char *path) {
fabe5c0e 31 _cleanup_closedir_ DIR *dir = NULL;
1d13f648 32 const char *dirpath;
31d5192d 33 struct dirent *de;
1d13f648 34 int r;
e02caf30 35
cba2ef02 36 assert(path);
cebed500 37
1d13f648 38 dirpath = prefix_roota(root, path);
cebed500 39
cba2ef02 40 dir = opendir(dirpath);
2c21044f
KS
41 if (!dir) {
42 if (errno == ENOENT)
43 return 0;
44 return -errno;
45 }
46
31d5192d 47 FOREACH_DIRENT(de, dir, return -errno) {
2c21044f
KS
48 char *p;
49
e34aa8ed
LP
50 if (!dirent_is_file_with_suffix(de, suffix)) {
51 log_debug("Ignoring %s/%s, because it's not a regular file with suffix %s.", dirpath, de->d_name, strna(suffix));
2c21044f 52 continue;
e34aa8ed 53 }
2c21044f 54
b5084605
LP
55 if (flags & CONF_FILES_EXECUTABLE) {
56 struct stat st;
57
58 /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
59 * here, as we care about whether the file is marked executable at all, and not whether it is
60 * executable for us, because if such errors are stuff we should log about. */
61
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;
65 }
66
6a464351
ZJS
67 if (!null_or_empty(&st)) {
68 /* A mask is a symlink to /dev/null or an empty file. It does not even
69 * have to be executable. Other entries must be regular executable files
70 * or symlinks to them. */
71 if (S_ISREG(st.st_mode)) {
72 if ((st.st_mode & 0111) == 0) { /* not executable */
73 log_debug("Ignoring %s/%s, as it is not marked executable.",
74 dirpath, de->d_name);
75 continue;
76 }
77 } else {
78 log_debug("Ignoring %s/%s, as it is neither a regular file nor a mask.",
79 dirpath, de->d_name);
b5084605
LP
80 continue;
81 }
b5084605
LP
82 }
83 }
84
605405c6 85 p = strjoin(dirpath, "/", de->d_name);
fabe5c0e 86 if (!p)
e02caf30 87 return -ENOMEM;
2c21044f 88
2b6bf07d 89 r = hashmap_put(h, basename(p), p);
fabe5c0e
LP
90 if (r == -EEXIST) {
91 log_debug("Skipping overridden file: %s.", p);
92 free(p);
93 } else if (r < 0) {
94 free(p);
95 return r;
96 } else if (r == 0) {
97 log_debug("Duplicate file %s", p);
2c21044f
KS
98 free(p);
99 }
100 }
101
e02caf30 102 return 0;
2c21044f
KS
103}
104
105static int base_cmp(const void *a, const void *b) {
106 const char *s1, *s2;
107
108 s1 = *(char * const *)a;
109 s2 = *(char * const *)b;
2b6bf07d 110 return strcmp(basename(s1), basename(s2));
2c21044f
KS
111}
112
b5084605 113static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
e1d75803 114 _cleanup_hashmap_free_ Hashmap *fh = NULL;
fabe5c0e 115 char **files, **p;
578ac060 116 int r;
2c21044f 117
fabe5c0e 118 assert(strv);
fabe5c0e
LP
119
120 /* This alters the dirs string array */
7d8da2c9 121 if (!path_strv_resolve_uniq(dirs, root))
fabe5c0e 122 return -ENOMEM;
2c21044f 123
d5099efc 124 fh = hashmap_new(&string_hash_ops);
fabe5c0e
LP
125 if (!fh)
126 return -ENOMEM;
2c21044f
KS
127
128 STRV_FOREACH(p, dirs) {
b5084605 129 r = files_add(fh, suffix, root, flags, *p);
31d5192d 130 if (r == -ENOMEM)
fabe5c0e 131 return r;
31d5192d
LP
132 if (r < 0)
133 log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
2c21044f
KS
134 }
135
136 files = hashmap_get_strv(fh);
31d5192d 137 if (!files)
fabe5c0e 138 return -ENOMEM;
fabe5c0e 139
7ff7394d 140 qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
fabe5c0e 141 *strv = files;
2c21044f 142
fabe5c0e
LP
143 return 0;
144}
145
a6d8474f 146int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
d16a1c1b
ZJS
147 /* Insert a path into strv, at the place honouring the usual sorting rules:
148 * - we first compare by the basename
149 * - and then we compare by dirname, allowing just one file with the given
150 * basename.
151 * This means that we will
152 * - add a new entry if basename(path) was not on the list,
153 * - do nothing if an entry with higher priority was already present,
154 * - do nothing if our new entry matches the existing entry,
155 * - replace the existing entry if our new entry has higher priority.
156 */
157 char *t;
158 unsigned i;
159 int r;
160
161 for (i = 0; i < strv_length(*strv); i++) {
162 int c;
163
164 c = base_cmp(*strv + i, &path);
165 if (c == 0) {
a6d8474f 166 char **dir;
d16a1c1b
ZJS
167
168 /* Oh, we found our spot and it already contains something. */
a6d8474f 169 STRV_FOREACH(dir, dirs) {
d16a1c1b
ZJS
170 char *p1, *p2;
171
172 p1 = path_startswith((*strv)[i], root);
173 if (p1)
a6d8474f
ZJS
174 /* Skip "/" in *dir, because p1 is without "/" too */
175 p1 = path_startswith(p1, *dir + 1);
d16a1c1b
ZJS
176 if (p1)
177 /* Existing entry with higher priority
178 * or same priority, no need to do anything. */
179 return 0;
180
a6d8474f 181 p2 = path_startswith(path, *dir);
d16a1c1b
ZJS
182 if (p2) {
183 /* Our new entry has higher priority */
184 t = path_join(root, path, NULL);
185 if (!t)
186 return log_oom();
187
188 return free_and_replace((*strv)[i], t);
189 }
190 }
191
192 } else if (c > 0)
193 /* Following files have lower priority, let's go insert our
194 * new entry. */
195 break;
196
197 /* … we are not there yet, let's continue */
198 }
199
200 t = path_join(root, path, NULL);
201 if (!t)
202 return log_oom();
203
204 r = strv_insert(strv, i, t);
205 if (r < 0)
206 free(t);
207 return r;
208}
209
a6d8474f
ZJS
210int conf_files_insert_nulstr(char ***strv, const char *root, const char *dirs, const char *path) {
211 _cleanup_strv_free_ char **d = NULL;
212
213 assert(strv);
214
215 d = strv_split_nulstr(dirs);
216 if (!d)
217 return -ENOMEM;
218
219 return conf_files_insert(strv, root, d, path);
220}
221
b5084605 222int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
fabe5c0e
LP
223 _cleanup_strv_free_ char **copy = NULL;
224
225 assert(strv);
fabe5c0e
LP
226
227 copy = strv_copy((char**) dirs);
228 if (!copy)
229 return -ENOMEM;
230
b5084605 231 return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
2c21044f
KS
232}
233
b5084605 234int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
8201ad81
LP
235 _cleanup_strv_free_ char **dirs = NULL;
236 va_list ap;
fabe5c0e
LP
237
238 assert(strv);
2c21044f 239
8201ad81
LP
240 va_start(ap, dir);
241 dirs = strv_new_ap(dir, ap);
242 va_end(ap);
243
244 if (!dirs)
245 return -ENOMEM;
fabe5c0e 246
b5084605 247 return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
fabe5c0e 248}
2c21044f 249
a6d8474f
ZJS
250int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dirs) {
251 _cleanup_strv_free_ char **d = NULL;
fabe5c0e
LP
252
253 assert(strv);
fabe5c0e 254
a6d8474f
ZJS
255 d = strv_split_nulstr(dirs);
256 if (!d)
fabe5c0e 257 return -ENOMEM;
2c21044f 258
a6d8474f 259 return conf_files_list_strv_internal(strv, suffix, root, flags, d);
2c21044f 260}
854a42fb
ZJS
261
262int conf_files_cat(const char *name) {
263 _cleanup_strv_free_ char **dirs = NULL, **files = NULL;
264 const char *dir;
265 char **t;
266 int r;
267
268 NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
269 assert(endswith(dir, "/"));
270 r = strv_extendf(&dirs, "%s%s.d", dir, name);
271 if (r < 0)
272 return log_error("Failed to build directory list: %m");
273 }
274
275 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dirs);
276 if (r < 0)
277 return log_error_errno(r, "Failed to query file list: %m");
278
279 name = strjoina("/etc/", name);
280
281 if (DEBUG_LOGGING) {
282 log_debug("Looking for configuration in:");
283 log_debug(" %s", name);
284 STRV_FOREACH(t, dirs)
285 log_debug(" %s/*.conf", *t);
286 }
287
288 /* show */
289 return cat_files(name, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);
290}