]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
29686440 | 2 | |
a8fbdf54 TA |
3 | #include <errno.h> |
4 | #include <stdarg.h> | |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | ||
b5efdb8a | 8 | #include "alloc-util.h" |
1a7f1b38 | 9 | #include "conf-files.h" |
8fb3f009 | 10 | #include "dirent-util.h" |
3ffd4af2 | 11 | #include "dropin.h" |
4f5dd394 | 12 | #include "escape.h" |
3ffd4af2 | 13 | #include "fd-util.h" |
4f5dd394 | 14 | #include "fileio-label.h" |
17e78d18 | 15 | #include "fs-util.h" |
a8fbdf54 TA |
16 | #include "hashmap.h" |
17 | #include "log.h" | |
18 | #include "macro.h" | |
4f5dd394 | 19 | #include "mkdir.h" |
bb15fafe | 20 | #include "path-util.h" |
a8fbdf54 | 21 | #include "set.h" |
d2724678 | 22 | #include "special.h" |
07630cea | 23 | #include "string-util.h" |
4f5dd394 | 24 | #include "strv.h" |
a8fbdf54 | 25 | #include "unit-name.h" |
29686440 | 26 | |
8eea8687 | 27 | int drop_in_file(const char *dir, const char *unit, unsigned level, |
f1b4b94c | 28 | const char *name, char **ret_p, char **ret_q) { |
29686440 | 29 | |
09c62487 | 30 | char prefix[DECIMAL_STR_MAX(unsigned)]; |
f1b4b94c | 31 | _cleanup_free_ char *b = NULL, *p = NULL, *q = NULL; |
29686440 ZJS |
32 | |
33 | assert(unit); | |
34 | assert(name); | |
f1b4b94c ZJS |
35 | assert(ret_p); |
36 | assert(ret_q); | |
29686440 | 37 | |
8eea8687 ZJS |
38 | sprintf(prefix, "%u", level); |
39 | ||
29686440 ZJS |
40 | b = xescape(name, "/."); |
41 | if (!b) | |
42 | return -ENOMEM; | |
43 | ||
ae6c3cc0 | 44 | if (!filename_is_valid(b)) |
29686440 ZJS |
45 | return -EINVAL; |
46 | ||
605405c6 | 47 | p = strjoin(dir, "/", unit, ".d"); |
605405c6 | 48 | q = strjoin(p, "/", prefix, "-", b, ".conf"); |
f1b4b94c | 49 | if (!p || !q) |
29686440 | 50 | return -ENOMEM; |
29686440 | 51 | |
f1b4b94c ZJS |
52 | *ret_p = TAKE_PTR(p); |
53 | *ret_q = TAKE_PTR(q); | |
29686440 ZJS |
54 | return 0; |
55 | } | |
56 | ||
8eea8687 | 57 | int write_drop_in(const char *dir, const char *unit, unsigned level, |
29686440 ZJS |
58 | const char *name, const char *data) { |
59 | ||
60 | _cleanup_free_ char *p = NULL, *q = NULL; | |
61 | int r; | |
62 | ||
63 | assert(dir); | |
64 | assert(unit); | |
65 | assert(name); | |
66 | assert(data); | |
67 | ||
8eea8687 | 68 | r = drop_in_file(dir, unit, level, name, &p, &q); |
29686440 ZJS |
69 | if (r < 0) |
70 | return r; | |
71 | ||
45519fd6 | 72 | (void) mkdir_p(p, 0755); |
29686440 ZJS |
73 | return write_string_file_atomic_label(q, data); |
74 | } | |
75 | ||
8eea8687 | 76 | int write_drop_in_format(const char *dir, const char *unit, unsigned level, |
29686440 ZJS |
77 | const char *name, const char *format, ...) { |
78 | _cleanup_free_ char *p = NULL; | |
79 | va_list ap; | |
80 | int r; | |
81 | ||
82 | assert(dir); | |
83 | assert(unit); | |
84 | assert(name); | |
85 | assert(format); | |
86 | ||
87 | va_start(ap, format); | |
88 | r = vasprintf(&p, format, ap); | |
89 | va_end(ap); | |
90 | ||
91 | if (r < 0) | |
92 | return -ENOMEM; | |
93 | ||
8eea8687 | 94 | return write_drop_in(dir, unit, level, name, p); |
29686440 | 95 | } |
1a7f1b38 | 96 | |
47a00111 | 97 | static int unit_file_add_dir( |
17e78d18 | 98 | const char *original_root, |
dcc4f30e ZJS |
99 | const char *path, |
100 | char ***dirs) { | |
1a7f1b38 | 101 | |
17e78d18 | 102 | _cleanup_free_ char *chased = NULL; |
1a7f1b38 ZJS |
103 | int r; |
104 | ||
105 | assert(path); | |
106 | ||
47a00111 ZJS |
107 | /* This adds [original_root]/path to dirs, if it exists. */ |
108 | ||
17e78d18 | 109 | r = chase_symlinks(path, original_root, 0, &chased); |
952713b0 | 110 | if (r == -ENOENT) /* Ignore -ENOENT, after all most units won't have a drop-in dir. */ |
09c62487 | 111 | return 0; |
952713b0 LP |
112 | if (r == -ENAMETOOLONG) { |
113 | /* Also, ignore -ENAMETOOLONG but log about it. After all, users are not even able to create the | |
114 | * drop-in dir in such case. This mostly happens for device units with an overly long /sys path. */ | |
115 | log_debug_errno(r, "Path '%s' too long, couldn't canonicalize, ignoring.", path); | |
116 | return 0; | |
117 | } | |
17e78d18 | 118 | if (r < 0) |
952713b0 | 119 | return log_warning_errno(r, "Failed to canonicalize path '%s': %m", path); |
17e78d18 | 120 | |
f1b4b94c | 121 | if (strv_consume(dirs, TAKE_PTR(chased)) < 0) |
dcc4f30e | 122 | return log_oom(); |
1a7f1b38 ZJS |
123 | |
124 | return 0; | |
125 | } | |
126 | ||
dcc4f30e | 127 | static int unit_file_find_dirs( |
17e78d18 | 128 | const char *original_root, |
7410616c | 129 | Set *unit_path_cache, |
1a7f1b38 ZJS |
130 | const char *unit_path, |
131 | const char *name, | |
132 | const char *suffix, | |
dcc4f30e | 133 | char ***dirs) { |
1a7f1b38 | 134 | |
53966245 LP |
135 | _cleanup_free_ char *prefix = NULL, *instance = NULL, *built = NULL; |
136 | bool is_instance, chopped; | |
137 | const char *dash; | |
138 | UnitType type; | |
96bb2fd8 | 139 | char *path; |
53966245 | 140 | size_t n; |
7410616c | 141 | int r; |
1a7f1b38 ZJS |
142 | |
143 | assert(unit_path); | |
144 | assert(name); | |
145 | assert(suffix); | |
146 | ||
96bb2fd8 | 147 | path = strjoina(unit_path, "/", name, suffix); |
dcc4f30e | 148 | if (!unit_path_cache || set_get(unit_path_cache, path)) { |
47a00111 | 149 | r = unit_file_add_dir(original_root, path, dirs); |
dcc4f30e ZJS |
150 | if (r < 0) |
151 | return r; | |
152 | } | |
1a7f1b38 | 153 | |
53966245 LP |
154 | is_instance = unit_name_is_valid(name, UNIT_NAME_INSTANCE); |
155 | if (is_instance) { /* Also try the template dir */ | |
a09d3eaf LP |
156 | _cleanup_free_ char *template = NULL; |
157 | ||
7410616c LP |
158 | r = unit_name_template(name, &template); |
159 | if (r < 0) | |
160 | return log_error_errno(r, "Failed to generate template from unit name: %m"); | |
1a7f1b38 | 161 | |
53966245 LP |
162 | r = unit_file_find_dirs(original_root, unit_path_cache, unit_path, template, suffix, dirs); |
163 | if (r < 0) | |
164 | return r; | |
1a7f1b38 ZJS |
165 | } |
166 | ||
53966245 LP |
167 | /* Let's see if there's a "-" prefix for this unit name. If so, let's invoke ourselves for it. This will then |
168 | * recursively do the same for all our prefixes. i.e. this means given "foo-bar-waldo.service" we'll also | |
169 | * search "foo-bar-.service" and "foo-.service". | |
170 | * | |
171 | * Note the order in which we do it: we traverse up adding drop-ins on each step. This means the more specific | |
172 | * drop-ins may override the more generic drop-ins, which is the intended behaviour. */ | |
173 | ||
174 | r = unit_name_to_prefix(name, &prefix); | |
175 | if (r < 0) | |
176 | return log_error_errno(r, "Failed to derive unit name prefix from unit name: %m"); | |
177 | ||
178 | chopped = false; | |
179 | for (;;) { | |
180 | dash = strrchr(prefix, '-'); | |
181 | if (!dash) /* No dash? if so we are done */ | |
182 | return 0; | |
183 | ||
184 | n = (size_t) (dash - prefix); | |
185 | if (n == 0) /* Leading dash? If so, we are done */ | |
186 | return 0; | |
187 | ||
188 | if (prefix[n+1] != 0 || chopped) { | |
189 | prefix[n+1] = 0; | |
190 | break; | |
191 | } | |
192 | ||
193 | /* Trailing dash? If so, chop it off and try again, but not more than once. */ | |
194 | prefix[n] = 0; | |
195 | chopped = true; | |
196 | } | |
197 | ||
198 | if (!unit_prefix_is_valid(prefix)) | |
199 | return 0; | |
200 | ||
201 | type = unit_name_to_type(name); | |
baaa35ad ZJS |
202 | if (type < 0) |
203 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
204 | "Failed to to derive unit type from unit name: %s", | |
205 | name); | |
53966245 LP |
206 | |
207 | if (is_instance) { | |
208 | r = unit_name_to_instance(name, &instance); | |
209 | if (r < 0) | |
210 | return log_error_errno(r, "Failed to derive unit name instance from unit name: %m"); | |
211 | } | |
212 | ||
213 | r = unit_name_build_from_type(prefix, instance, type, &built); | |
214 | if (r < 0) | |
215 | return log_error_errno(r, "Failed to build prefix unit name: %m"); | |
216 | ||
217 | return unit_file_find_dirs(original_root, unit_path_cache, unit_path, built, suffix, dirs); | |
1a7f1b38 ZJS |
218 | } |
219 | ||
220 | int unit_file_find_dropin_paths( | |
17e78d18 | 221 | const char *original_root, |
1a7f1b38 ZJS |
222 | char **lookup_path, |
223 | Set *unit_path_cache, | |
95778782 ZJS |
224 | const char *dir_suffix, |
225 | const char *file_suffix, | |
3c4e3031 | 226 | const Set *names, |
058db925 | 227 | char ***ret) { |
1a7f1b38 | 228 | |
3f6de63b | 229 | _cleanup_strv_free_ char **dirs = NULL; |
d2724678 | 230 | UnitType type = _UNIT_TYPE_INVALID; |
f1b4b94c | 231 | char *name, **p; |
53966245 | 232 | Iterator i; |
1a7f1b38 ZJS |
233 | int r; |
234 | ||
058db925 | 235 | assert(ret); |
1a7f1b38 | 236 | |
d2724678 AZ |
237 | /* All the names in the unit are of the same type so just grab one. */ |
238 | name = (char*) set_first(names); | |
239 | if (name) { | |
240 | type = unit_name_to_type(name); | |
241 | if (type < 0) | |
242 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
243 | "Failed to to derive unit type from unit name: %s", | |
244 | name); | |
245 | } | |
246 | ||
247 | /* Special drop in for -.service. Add this first as it's the most generic | |
248 | * and should be able to be overridden by more specific drop-ins. */ | |
249 | if (type == UNIT_SERVICE) | |
250 | STRV_FOREACH(p, lookup_path) | |
251 | (void) unit_file_find_dirs(original_root, | |
252 | unit_path_cache, | |
253 | *p, | |
254 | SPECIAL_ROOT_SERVICE, | |
255 | dir_suffix, | |
256 | &dirs); | |
257 | ||
f1b4b94c | 258 | SET_FOREACH(name, names, i) |
1a7f1b38 | 259 | STRV_FOREACH(p, lookup_path) |
f1b4b94c | 260 | (void) unit_file_find_dirs(original_root, unit_path_cache, *p, name, dir_suffix, &dirs); |
1a7f1b38 | 261 | |
058db925 LP |
262 | if (strv_isempty(dirs)) { |
263 | *ret = NULL; | |
1a7f1b38 | 264 | return 0; |
058db925 | 265 | } |
1a7f1b38 | 266 | |
b5084605 | 267 | r = conf_files_list_strv(ret, file_suffix, NULL, 0, (const char**) dirs); |
1a7f1b38 | 268 | if (r < 0) |
3f6de63b | 269 | return log_warning_errno(r, "Failed to create the list of configuration files: %m"); |
058db925 | 270 | |
1a7f1b38 ZJS |
271 | return 1; |
272 | } |