]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dropin.c
util: split out escaping code into escape.[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 "conf-files.h"
23 #include "escape.h"
24 #include "fileio-label.h"
25 #include "mkdir.h"
26 #include "strv.h"
27 #include "util.h"
28 #include "dropin.h"
29
30 int drop_in_file(const char *dir, const char *unit, unsigned level,
31 const char *name, char **_p, char **_q) {
32
33 _cleanup_free_ char *b = NULL;
34 char *p, *q;
35
36 char prefix[DECIMAL_STR_MAX(unsigned)];
37
38 assert(unit);
39 assert(name);
40 assert(_p);
41 assert(_q);
42
43 sprintf(prefix, "%u", level);
44
45 b = xescape(name, "/.");
46 if (!b)
47 return -ENOMEM;
48
49 if (!filename_is_valid(b))
50 return -EINVAL;
51
52 p = strjoin(dir, "/", unit, ".d", NULL);
53 if (!p)
54 return -ENOMEM;
55
56 q = strjoin(p, "/", prefix, "-", b, ".conf", NULL);
57 if (!q) {
58 free(p);
59 return -ENOMEM;
60 }
61
62 *_p = p;
63 *_q = q;
64 return 0;
65 }
66
67 int write_drop_in(const char *dir, const char *unit, unsigned level,
68 const char *name, const char *data) {
69
70 _cleanup_free_ char *p = NULL, *q = NULL;
71 int r;
72
73 assert(dir);
74 assert(unit);
75 assert(name);
76 assert(data);
77
78 r = drop_in_file(dir, unit, level, name, &p, &q);
79 if (r < 0)
80 return r;
81
82 (void) mkdir_p(p, 0755);
83 return write_string_file_atomic_label(q, data);
84 }
85
86 int write_drop_in_format(const char *dir, const char *unit, unsigned level,
87 const char *name, const char *format, ...) {
88 _cleanup_free_ char *p = NULL;
89 va_list ap;
90 int r;
91
92 assert(dir);
93 assert(unit);
94 assert(name);
95 assert(format);
96
97 va_start(ap, format);
98 r = vasprintf(&p, format, ap);
99 va_end(ap);
100
101 if (r < 0)
102 return -ENOMEM;
103
104 return write_drop_in(dir, unit, level, name, p);
105 }
106
107 static int iterate_dir(
108 const char *path,
109 UnitDependency dependency,
110 dependency_consumer_t consumer,
111 void *arg,
112 char ***strv) {
113
114 _cleanup_closedir_ DIR *d = NULL;
115 int r;
116
117 assert(path);
118
119 /* The config directories are special, since the order of the
120 * drop-ins matters */
121 if (dependency < 0) {
122 r = strv_extend(strv, path);
123 if (r < 0)
124 return log_oom();
125
126 return 0;
127 }
128
129 assert(consumer);
130
131 d = opendir(path);
132 if (!d) {
133 if (errno == ENOENT)
134 return 0;
135
136 return log_error_errno(errno, "Failed to open directory %s: %m", path);
137 }
138
139 for (;;) {
140 struct dirent *de;
141 _cleanup_free_ char *f = NULL;
142
143 errno = 0;
144 de = readdir(d);
145 if (!de && errno != 0)
146 return log_error_errno(errno, "Failed to read directory %s: %m", path);
147
148 if (!de)
149 break;
150
151 if (hidden_file(de->d_name))
152 continue;
153
154 f = strjoin(path, "/", de->d_name, NULL);
155 if (!f)
156 return log_oom();
157
158 r = consumer(dependency, de->d_name, f, arg);
159 if (r < 0)
160 return r;
161 }
162
163 return 0;
164 }
165
166 int unit_file_process_dir(
167 Set *unit_path_cache,
168 const char *unit_path,
169 const char *name,
170 const char *suffix,
171 UnitDependency dependency,
172 dependency_consumer_t consumer,
173 void *arg,
174 char ***strv) {
175
176 _cleanup_free_ char *path = NULL;
177 int r;
178
179 assert(unit_path);
180 assert(name);
181 assert(suffix);
182
183 path = strjoin(unit_path, "/", name, suffix, NULL);
184 if (!path)
185 return log_oom();
186
187 if (!unit_path_cache || set_get(unit_path_cache, path))
188 (void) iterate_dir(path, dependency, consumer, arg, strv);
189
190 if (unit_name_is_valid(name, UNIT_NAME_INSTANCE)) {
191 _cleanup_free_ char *template = NULL, *p = NULL;
192 /* Also try the template dir */
193
194 r = unit_name_template(name, &template);
195 if (r < 0)
196 return log_error_errno(r, "Failed to generate template from unit name: %m");
197
198 p = strjoin(unit_path, "/", template, suffix, NULL);
199 if (!p)
200 return log_oom();
201
202 if (!unit_path_cache || set_get(unit_path_cache, p))
203 (void) iterate_dir(p, dependency, consumer, arg, strv);
204 }
205
206 return 0;
207 }
208
209 int unit_file_find_dropin_paths(
210 char **lookup_path,
211 Set *unit_path_cache,
212 Set *names,
213 char ***paths) {
214
215 _cleanup_strv_free_ char **strv = NULL, **ans = NULL;
216 Iterator i;
217 char *t;
218 int r;
219
220 assert(paths);
221
222 SET_FOREACH(t, names, i) {
223 char **p;
224
225 STRV_FOREACH(p, lookup_path)
226 unit_file_process_dir(unit_path_cache, *p, t, ".d", _UNIT_DEPENDENCY_INVALID, NULL, NULL, &strv);
227 }
228
229 if (strv_isempty(strv))
230 return 0;
231
232 r = conf_files_list_strv(&ans, ".conf", NULL, (const char**) strv);
233 if (r < 0)
234 return log_warning_errno(r, "Failed to get list of configuration files: %m");
235
236 *paths = ans;
237 ans = NULL;
238 return 1;
239 }