chiark / gitweb /
util: don't send SIGCONT following a SIGCONT or SIGKILL in kill_and_sigcont()
[elogind.git] / src / basic / conf-files.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <dirent.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "conf-files.h"
28 #include "dirent-util.h"
29 #include "fd-util.h"
30 #include "hashmap.h"
31 #include "log.h"
32 #include "macro.h"
33 #include "missing.h"
34 #include "path-util.h"
35 #include "string-util.h"
36 #include "strv.h"
37 #include "util.h"
38
39 static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
40         _cleanup_closedir_ DIR *dir = NULL;
41         const char *dirpath;
42         struct dirent *de;
43         int r;
44
45         assert(path);
46         assert(suffix);
47
48         dirpath = prefix_roota(root, path);
49
50         dir = opendir(dirpath);
51         if (!dir) {
52                 if (errno == ENOENT)
53                         return 0;
54                 return -errno;
55         }
56
57         FOREACH_DIRENT(de, dir, return -errno) {
58                 char *p;
59
60                 if (!dirent_is_file_with_suffix(de, suffix))
61                         continue;
62
63                 p = strjoin(dirpath, "/", de->d_name, NULL);
64                 if (!p)
65                         return -ENOMEM;
66
67                 r = hashmap_put(h, basename(p), p);
68                 if (r == -EEXIST) {
69                         log_debug("Skipping overridden file: %s.", p);
70                         free(p);
71                 } else if (r < 0) {
72                         free(p);
73                         return r;
74                 } else if (r == 0) {
75                         log_debug("Duplicate file %s", p);
76                         free(p);
77                 }
78         }
79
80         return 0;
81 }
82
83 static int base_cmp(const void *a, const void *b) {
84         const char *s1, *s2;
85
86         s1 = *(char * const *)a;
87         s2 = *(char * const *)b;
88         return strcmp(basename(s1), basename(s2));
89 }
90
91 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
92         _cleanup_hashmap_free_ Hashmap *fh = NULL;
93         char **files, **p;
94         int r;
95
96         assert(strv);
97         assert(suffix);
98
99         /* This alters the dirs string array */
100         if (!path_strv_resolve_uniq(dirs, root))
101                 return -ENOMEM;
102
103         fh = hashmap_new(&string_hash_ops);
104         if (!fh)
105                 return -ENOMEM;
106
107         STRV_FOREACH(p, dirs) {
108                 r = files_add(fh, root, *p, suffix);
109                 if (r == -ENOMEM)
110                         return r;
111                 if (r < 0)
112                         log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
113         }
114
115         files = hashmap_get_strv(fh);
116         if (!files)
117                 return -ENOMEM;
118
119         qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
120         *strv = files;
121
122         return 0;
123 }
124
125 #if 0 /// UNNEEDED by elogind
126 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char* const* dirs) {
127         _cleanup_strv_free_ char **copy = NULL;
128
129         assert(strv);
130         assert(suffix);
131
132         copy = strv_copy((char**) dirs);
133         if (!copy)
134                 return -ENOMEM;
135
136         return conf_files_list_strv_internal(strv, suffix, root, copy);
137 }
138
139 int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...) {
140         _cleanup_strv_free_ char **dirs = NULL;
141         va_list ap;
142
143         assert(strv);
144         assert(suffix);
145
146         va_start(ap, dir);
147         dirs = strv_new_ap(dir, ap);
148         va_end(ap);
149
150         if (!dirs)
151                 return -ENOMEM;
152
153         return conf_files_list_strv_internal(strv, suffix, root, dirs);
154 }
155 #endif // 0
156
157 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, const char *d) {
158         _cleanup_strv_free_ char **dirs = NULL;
159
160         assert(strv);
161         assert(suffix);
162
163         dirs = strv_split_nulstr(d);
164         if (!dirs)
165                 return -ENOMEM;
166
167         return conf_files_list_strv_internal(strv, suffix, root, dirs);
168 }