]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dropin.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / shared / dropin.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Zbigniew Jędrzejewski-Szmek
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "alloc-util.h"
23 #include "conf-files.h"
24 #include "dropin.h"
25 #include "escape.h"
26 #include "fd-util.h"
27 #include "fileio-label.h"
28 #include "mkdir.h"
29 #include "path-util.h"
30 #include "string-util.h"
31 #include "strv.h"
32 #include "util.h"
33
34 int drop_in_file(const char *dir, const char *unit, unsigned level,
35 const char *name, char **_p, char **_q) {
36
37 _cleanup_free_ char *b = NULL;
38 char *p, *q;
39
40 char prefix[DECIMAL_STR_MAX(unsigned)];
41
42 assert(unit);
43 assert(name);
44 assert(_p);
45 assert(_q);
46
47 sprintf(prefix, "%u", level);
48
49 b = xescape(name, "/.");
50 if (!b)
51 return -ENOMEM;
52
53 if (!filename_is_valid(b))
54 return -EINVAL;
55
56 p = strjoin(dir, "/", unit, ".d", NULL);
57 if (!p)
58 return -ENOMEM;
59
60 q = strjoin(p, "/", prefix, "-", b, ".conf", NULL);
61 if (!q) {
62 free(p);
63 return -ENOMEM;
64 }
65
66 *_p = p;
67 *_q = q;
68 return 0;
69 }
70
71 int write_drop_in(const char *dir, const char *unit, unsigned level,
72 const char *name, const char *data) {
73
74 _cleanup_free_ char *p = NULL, *q = NULL;
75 int r;
76
77 assert(dir);
78 assert(unit);
79 assert(name);
80 assert(data);
81
82 r = drop_in_file(dir, unit, level, name, &p, &q);
83 if (r < 0)
84 return r;
85
86 (void) mkdir_p(p, 0755);
87 return write_string_file_atomic_label(q, data);
88 }
89
90 int write_drop_in_format(const char *dir, const char *unit, unsigned level,
91 const char *name, const char *format, ...) {
92 _cleanup_free_ char *p = NULL;
93 va_list ap;
94 int r;
95
96 assert(dir);
97 assert(unit);
98 assert(name);
99 assert(format);
100
101 va_start(ap, format);
102 r = vasprintf(&p, format, ap);
103 va_end(ap);
104
105 if (r < 0)
106 return -ENOMEM;
107
108 return write_drop_in(dir, unit, level, name, p);
109 }
110
111 static int iterate_dir(
112 const char *path,
113 UnitDependency dependency,
114 dependency_consumer_t consumer,
115 void *arg,
116 char ***strv) {
117
118 _cleanup_closedir_ DIR *d = NULL;
119 int r;
120
121 assert(path);
122
123 /* The config directories are special, since the order of the
124 * drop-ins matters */
125 if (dependency < 0) {
126 r = strv_extend(strv, path);
127 if (r < 0)
128 return log_oom();
129
130 return 0;
131 }
132
133 assert(consumer);
134
135 d = opendir(path);
136 if (!d) {
137 if (errno == ENOENT)
138 return 0;
139
140 return log_error_errno(errno, "Failed to open directory %s: %m", path);
141 }
142
143 for (;;) {
144 struct dirent *de;
145 _cleanup_free_ char *f = NULL;
146
147 errno = 0;
148 de = readdir(d);
149 if (!de && errno != 0)
150 return log_error_errno(errno, "Failed to read directory %s: %m", path);
151
152 if (!de)
153 break;
154
155 if (hidden_file(de->d_name))
156 continue;
157
158 f = strjoin(path, "/", de->d_name, NULL);
159 if (!f)
160 return log_oom();
161
162 r = consumer(dependency, de->d_name, f, arg);
163 if (r < 0)
164 return r;
165 }
166
167 return 0;
168 }
169
170 int unit_file_process_dir(
171 Set *unit_path_cache,
172 const char *unit_path,
173 const char *name,
174 const char *suffix,
175 UnitDependency dependency,
176 dependency_consumer_t consumer,
177 void *arg,
178 char ***strv) {
179
180 _cleanup_free_ char *path = NULL;
181 int r;
182
183 assert(unit_path);
184 assert(name);
185 assert(suffix);
186
187 path = strjoin(unit_path, "/", name, suffix, NULL);
188 if (!path)
189 return log_oom();
190
191 if (!unit_path_cache || set_get(unit_path_cache, path))
192 (void) iterate_dir(path, dependency, consumer, arg, strv);
193
194 if (unit_name_is_valid(name, UNIT_NAME_INSTANCE)) {
195 _cleanup_free_ char *template = NULL, *p = NULL;
196 /* Also try the template dir */
197
198 r = unit_name_template(name, &template);
199 if (r < 0)
200 return log_error_errno(r, "Failed to generate template from unit name: %m");
201
202 p = strjoin(unit_path, "/", template, suffix, NULL);
203 if (!p)
204 return log_oom();
205
206 if (!unit_path_cache || set_get(unit_path_cache, p))
207 (void) iterate_dir(p, dependency, consumer, arg, strv);
208 }
209
210 return 0;
211 }
212
213 int unit_file_find_dropin_paths(
214 char **lookup_path,
215 Set *unit_path_cache,
216 Set *names,
217 char ***paths) {
218
219 _cleanup_strv_free_ char **strv = NULL, **ans = NULL;
220 Iterator i;
221 char *t;
222 int r;
223
224 assert(paths);
225
226 SET_FOREACH(t, names, i) {
227 char **p;
228
229 STRV_FOREACH(p, lookup_path)
230 unit_file_process_dir(unit_path_cache, *p, t, ".d", _UNIT_DEPENDENCY_INVALID, NULL, NULL, &strv);
231 }
232
233 if (strv_isempty(strv))
234 return 0;
235
236 r = conf_files_list_strv(&ans, ".conf", NULL, (const char**) strv);
237 if (r < 0)
238 return log_warning_errno(r, "Failed to get list of configuration files: %m");
239
240 *paths = ans;
241 ans = NULL;
242 return 1;
243 }