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