]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
5cfa33e0 | 2 | |
c2911d48 ZJS |
3 | #include "sd-id128.h" |
4 | ||
f4351959 | 5 | #include "chase-symlinks.h" |
e8630e69 ZJS |
6 | #include "dirent-util.h" |
7 | #include "fd-util.h" | |
8 | #include "fs-util.h" | |
5cfa33e0 | 9 | #include "macro.h" |
e8630e69 ZJS |
10 | #include "path-lookup.h" |
11 | #include "set.h" | |
da33cba0 | 12 | #include "special.h" |
e8630e69 | 13 | #include "stat-util.h" |
7d1e91d1 | 14 | #include "string-util.h" |
e8630e69 | 15 | #include "strv.h" |
5cfa33e0 ZJS |
16 | #include "unit-file.h" |
17 | ||
18 | bool unit_type_may_alias(UnitType type) { | |
19 | return IN_SET(type, | |
20 | UNIT_SERVICE, | |
21 | UNIT_SOCKET, | |
22 | UNIT_TARGET, | |
23 | UNIT_DEVICE, | |
24 | UNIT_TIMER, | |
25 | UNIT_PATH); | |
26 | } | |
27 | ||
28 | bool unit_type_may_template(UnitType type) { | |
29 | return IN_SET(type, | |
30 | UNIT_SERVICE, | |
31 | UNIT_SOCKET, | |
32 | UNIT_TARGET, | |
33 | UNIT_TIMER, | |
34 | UNIT_PATH); | |
35 | } | |
7d1e91d1 | 36 | |
1bf15585 | 37 | int unit_symlink_name_compatible(const char *symlink, const char *target, bool instance_propagation) { |
9a4f9e69 | 38 | _cleanup_free_ char *template = NULL; |
1bf15585 | 39 | int r, un_type1, un_type2; |
9a4f9e69 | 40 | |
1bf15585 ZJS |
41 | un_type1 = unit_name_classify(symlink); |
42 | ||
43 | /* The straightforward case: the symlink name matches the target and we have a valid unit */ | |
44 | if (streq(symlink, target) && | |
45 | (un_type1 & (UNIT_NAME_PLAIN | UNIT_NAME_INSTANCE))) | |
9a4f9e69 ZJS |
46 | return 1; |
47 | ||
48 | r = unit_name_template(symlink, &template); | |
49 | if (r == -EINVAL) | |
50 | return 0; /* Not a template */ | |
51 | if (r < 0) | |
52 | return r; | |
53 | ||
1bf15585 ZJS |
54 | un_type2 = unit_name_classify(target); |
55 | ||
9a4f9e69 | 56 | /* An instance name points to a target that is just the template name */ |
1bf15585 ZJS |
57 | if (un_type1 == UNIT_NAME_INSTANCE && |
58 | un_type2 == UNIT_NAME_TEMPLATE && | |
59 | streq(template, target)) | |
60 | return 1; | |
61 | ||
62 | /* foo@.target.requires/bar@.service: instance will be propagated */ | |
63 | if (instance_propagation && | |
64 | un_type1 == UNIT_NAME_TEMPLATE && | |
65 | un_type2 == UNIT_NAME_TEMPLATE && | |
66 | streq(template, target)) | |
67 | return 1; | |
68 | ||
69 | return 0; | |
9a4f9e69 ZJS |
70 | } |
71 | ||
7d1e91d1 ZJS |
72 | int unit_validate_alias_symlink_and_warn(const char *filename, const char *target) { |
73 | const char *src, *dst; | |
74 | _cleanup_free_ char *src_instance = NULL, *dst_instance = NULL; | |
75 | UnitType src_unit_type, dst_unit_type; | |
73ce91a0 | 76 | UnitNameFlags src_name_type, dst_name_type; |
7d1e91d1 ZJS |
77 | |
78 | /* Check if the *alias* symlink is valid. This applies to symlinks like | |
79 | * /etc/systemd/system/dbus.service → dbus-broker.service, but not to .wants or .requires symlinks | |
80 | * and such. Neither does this apply to symlinks which *link* units, i.e. symlinks to outside of the | |
81 | * unit lookup path. | |
82 | * | |
83 | * -EINVAL is returned if the something is wrong with the source filename or the source unit type is | |
84 | * not allowed to symlink, | |
85 | * -EXDEV if the target filename is not a valid unit name or doesn't match the source. | |
86 | */ | |
87 | ||
88 | src = basename(filename); | |
89 | dst = basename(target); | |
90 | ||
91 | /* src checks */ | |
92 | ||
93 | src_name_type = unit_name_to_instance(src, &src_instance); | |
94 | if (src_name_type < 0) | |
95 | return log_notice_errno(src_name_type, | |
96 | "%s: not a valid unit name \"%s\": %m", filename, src); | |
97 | ||
98 | src_unit_type = unit_name_to_type(src); | |
99 | assert(src_unit_type >= 0); /* unit_name_to_instance() checked the suffix already */ | |
100 | ||
101 | if (!unit_type_may_alias(src_unit_type)) | |
102 | return log_notice_errno(SYNTHETIC_ERRNO(EINVAL), | |
103 | "%s: symlinks are not allowed for units of this type, rejecting.", | |
104 | filename); | |
105 | ||
106 | if (src_name_type != UNIT_NAME_PLAIN && | |
107 | !unit_type_may_template(src_unit_type)) | |
108 | return log_notice_errno(SYNTHETIC_ERRNO(EINVAL), | |
109 | "%s: templates not allowed for %s units, rejecting.", | |
110 | filename, unit_type_to_string(src_unit_type)); | |
111 | ||
112 | /* dst checks */ | |
113 | ||
114 | dst_name_type = unit_name_to_instance(dst, &dst_instance); | |
115 | if (dst_name_type < 0) | |
116 | return log_notice_errno(dst_name_type == -EINVAL ? SYNTHETIC_ERRNO(EXDEV) : dst_name_type, | |
117 | "%s points to \"%s\" which is not a valid unit name: %m", | |
118 | filename, dst); | |
119 | ||
120 | if (!(dst_name_type == src_name_type || | |
121 | (src_name_type == UNIT_NAME_INSTANCE && dst_name_type == UNIT_NAME_TEMPLATE))) | |
122 | return log_notice_errno(SYNTHETIC_ERRNO(EXDEV), | |
123 | "%s: symlink target name type \"%s\" does not match source, rejecting.", | |
124 | filename, dst); | |
125 | ||
126 | if (dst_name_type == UNIT_NAME_INSTANCE) { | |
127 | assert(src_instance); | |
128 | assert(dst_instance); | |
129 | if (!streq(src_instance, dst_instance)) | |
130 | return log_notice_errno(SYNTHETIC_ERRNO(EXDEV), | |
131 | "%s: unit symlink target \"%s\" instance name doesn't match, rejecting.", | |
132 | filename, dst); | |
133 | } | |
134 | ||
135 | dst_unit_type = unit_name_to_type(dst); | |
136 | if (dst_unit_type != src_unit_type) | |
137 | return log_notice_errno(SYNTHETIC_ERRNO(EXDEV), | |
138 | "%s: symlink target \"%s\" has incompatible suffix, rejecting.", | |
139 | filename, dst); | |
140 | ||
141 | return 0; | |
142 | } | |
e8630e69 ZJS |
143 | |
144 | #define FOLLOW_MAX 8 | |
145 | ||
146 | static int unit_ids_map_get( | |
147 | Hashmap *unit_ids_map, | |
148 | const char *unit_name, | |
149 | const char **ret_fragment_path) { | |
150 | ||
151 | /* Resolve recursively until we hit an absolute path, i.e. a non-aliased unit. | |
152 | * | |
153 | * We distinguish the case where unit_name was not found in the hashmap at all, and the case where | |
154 | * some symlink was broken. | |
155 | * | |
156 | * If a symlink target points to an instance name, then we also check for the template. */ | |
157 | ||
158 | const char *id = NULL; | |
159 | int r; | |
160 | ||
161 | for (unsigned n = 0; n < FOLLOW_MAX; n++) { | |
162 | const char *t = hashmap_get(unit_ids_map, id ?: unit_name); | |
163 | if (!t) { | |
164 | _cleanup_free_ char *template = NULL; | |
165 | ||
166 | if (!id) | |
167 | return -ENOENT; | |
168 | ||
169 | r = unit_name_template(id, &template); | |
170 | if (r == -EINVAL) | |
171 | return -ENXIO; /* we failed to find the symlink target */ | |
172 | if (r < 0) | |
173 | return log_error_errno(r, "Failed to determine template name for %s: %m", id); | |
174 | ||
175 | t = hashmap_get(unit_ids_map, template); | |
176 | if (!t) | |
177 | return -ENXIO; | |
178 | ||
179 | /* We successfully switched from instanced name to a template, let's continue */ | |
180 | } | |
181 | ||
182 | if (path_is_absolute(t)) { | |
183 | if (ret_fragment_path) | |
184 | *ret_fragment_path = t; | |
185 | return 0; | |
186 | } | |
187 | ||
188 | id = t; | |
189 | } | |
190 | ||
191 | return -ELOOP; | |
192 | } | |
193 | ||
91e0ee5f ZJS |
194 | static bool lookup_paths_mtime_exclude(const LookupPaths *lp, const char *path) { |
195 | /* Paths that are under our exclusive control. Users shall not alter those directly. */ | |
196 | ||
197 | return streq_ptr(path, lp->generator) || | |
198 | streq_ptr(path, lp->generator_early) || | |
199 | streq_ptr(path, lp->generator_late) || | |
200 | streq_ptr(path, lp->transient) || | |
201 | streq_ptr(path, lp->persistent_control) || | |
202 | streq_ptr(path, lp->runtime_control); | |
203 | } | |
204 | ||
c2911d48 ZJS |
205 | #define HASH_KEY SD_ID128_MAKE(4e,86,1b,e3,39,b3,40,46,98,5d,b8,11,34,8f,c3,c1) |
206 | ||
207 | bool lookup_paths_timestamp_hash_same(const LookupPaths *lp, uint64_t timestamp_hash, uint64_t *ret_new) { | |
208 | struct siphash state; | |
91e0ee5f | 209 | |
c2911d48 ZJS |
210 | siphash24_init(&state, HASH_KEY.bytes); |
211 | ||
de010b0b | 212 | STRV_FOREACH(dir, lp->search_path) { |
91e0ee5f ZJS |
213 | struct stat st; |
214 | ||
215 | if (lookup_paths_mtime_exclude(lp, *dir)) | |
216 | continue; | |
217 | ||
218 | /* Determine the latest lookup path modification time */ | |
219 | if (stat(*dir, &st) < 0) { | |
220 | if (errno == ENOENT) | |
221 | continue; | |
222 | ||
223 | log_debug_errno(errno, "Failed to stat %s, ignoring: %m", *dir); | |
224 | continue; | |
225 | } | |
226 | ||
c2911d48 | 227 | siphash24_compress_usec_t(timespec_load(&st.st_mtim), &state); |
91e0ee5f ZJS |
228 | } |
229 | ||
c2911d48 ZJS |
230 | uint64_t updated = siphash24_finalize(&state); |
231 | if (ret_new) | |
232 | *ret_new = updated; | |
233 | if (updated != timestamp_hash) | |
234 | log_debug("Modification times have changed, need to update cache."); | |
235 | return updated == timestamp_hash; | |
91e0ee5f ZJS |
236 | } |
237 | ||
7f304b85 YW |
238 | static int directory_name_is_valid(const char *name) { |
239 | const char *suffix; | |
240 | ||
241 | /* Accept a directory whose name is a valid unit file name ending in .wants/, .requires/ or .d/ */ | |
242 | ||
243 | FOREACH_STRING(suffix, ".wants", ".requires", ".d") { | |
244 | _cleanup_free_ char *chopped = NULL; | |
245 | const char *e; | |
246 | ||
247 | e = endswith(name, suffix); | |
248 | if (!e) | |
249 | continue; | |
250 | ||
251 | chopped = strndup(name, e - name); | |
252 | if (!chopped) | |
253 | return log_oom(); | |
254 | ||
255 | if (unit_name_is_valid(chopped, UNIT_NAME_ANY) || | |
256 | unit_type_from_string(chopped) >= 0) | |
257 | return true; | |
258 | } | |
259 | ||
260 | return false; | |
261 | } | |
262 | ||
e8630e69 ZJS |
263 | int unit_file_build_name_map( |
264 | const LookupPaths *lp, | |
c2911d48 | 265 | uint64_t *cache_timestamp_hash, |
3fb2326f ZJS |
266 | Hashmap **unit_ids_map, |
267 | Hashmap **unit_names_map, | |
268 | Set **path_cache) { | |
e8630e69 ZJS |
269 | |
270 | /* Build two mappings: any name → main unit (i.e. the end result of symlink resolution), unit name → | |
fa027117 | 271 | * all aliases (i.e. the entry for a given key is a list of all names which point to this key). The |
e8630e69 ZJS |
272 | * key is included in the value iff we saw a file or symlink with that name. In other words, if we |
273 | * have a key, but it is not present in the value for itself, there was an alias pointing to it, but | |
274 | * the unit itself is not loadable. | |
275 | * | |
3fb2326f ZJS |
276 | * At the same, build a cache of paths where to find units. The non-const parameters are for input |
277 | * and output. Existing contents will be freed before the new contents are stored. | |
e8630e69 ZJS |
278 | */ |
279 | ||
280 | _cleanup_hashmap_free_ Hashmap *ids = NULL, *names = NULL; | |
281 | _cleanup_set_free_free_ Set *paths = NULL; | |
c2911d48 | 282 | uint64_t timestamp_hash; |
e8630e69 | 283 | int r; |
91e0ee5f | 284 | |
c2911d48 ZJS |
285 | /* Before doing anything, check if the timestamp hash that was passed is still valid. |
286 | * If yes, do nothing. */ | |
287 | if (cache_timestamp_hash && | |
288 | lookup_paths_timestamp_hash_same(lp, *cache_timestamp_hash, ×tamp_hash)) | |
f6067186 | 289 | return 0; |
c2911d48 ZJS |
290 | |
291 | /* The timestamp hash is now set based on the mtimes from before when we start reading files. | |
292 | * If anything is modified concurrently, we'll consider the cache outdated. */ | |
e8630e69 | 293 | |
3fb2326f ZJS |
294 | if (path_cache) { |
295 | paths = set_new(&path_hash_ops_free); | |
e8630e69 ZJS |
296 | if (!paths) |
297 | return log_oom(); | |
298 | } | |
299 | ||
de010b0b | 300 | STRV_FOREACH(dir, lp->search_path) { |
e8630e69 ZJS |
301 | _cleanup_closedir_ DIR *d = NULL; |
302 | ||
303 | d = opendir(*dir); | |
304 | if (!d) { | |
305 | if (errno != ENOENT) | |
306 | log_warning_errno(errno, "Failed to open \"%s\", ignoring: %m", *dir); | |
307 | continue; | |
308 | } | |
309 | ||
14bb7295 | 310 | FOREACH_DIRENT_ALL(de, d, log_warning_errno(errno, "Failed to read \"%s\", ignoring: %m", *dir)) { |
d7ac0952 FS |
311 | _unused_ _cleanup_free_ char *_filename_free = NULL; |
312 | _cleanup_free_ char *simplified = NULL; | |
7f304b85 | 313 | bool symlink_to_dir = false; |
95ef0eaf LP |
314 | const char *dst = NULL; |
315 | char *filename; | |
890befcf ZJS |
316 | |
317 | /* We only care about valid units and dirs with certain suffixes, let's ignore the | |
318 | * rest. */ | |
95ef0eaf | 319 | |
7f304b85 | 320 | if (de->d_type == DT_REG) { |
95ef0eaf | 321 | |
7f304b85 | 322 | /* Accept a regular file whose name is a valid unit file name. */ |
95ef0eaf LP |
323 | if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY)) |
324 | continue; | |
325 | ||
95ef0eaf | 326 | } else if (de->d_type == DT_DIR) { |
95ef0eaf LP |
327 | |
328 | if (!paths) /* Skip directories early unless path_cache is requested */ | |
329 | continue; | |
330 | ||
7f304b85 YW |
331 | r = directory_name_is_valid(de->d_name); |
332 | if (r < 0) | |
333 | return r; | |
334 | if (r == 0) | |
335 | continue; | |
336 | ||
337 | } else if (de->d_type == DT_LNK) { | |
95ef0eaf | 338 | |
7f304b85 YW |
339 | /* Accept a symlink file whose name is a valid unit file name or |
340 | * ending in .wants/, .requires/ or .d/. */ | |
341 | ||
342 | if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY)) { | |
343 | _cleanup_free_ char *target = NULL; | |
344 | ||
345 | if (!paths) /* Skip symlink to a directory early unless path_cache is requested */ | |
95ef0eaf LP |
346 | continue; |
347 | ||
7f304b85 YW |
348 | r = directory_name_is_valid(de->d_name); |
349 | if (r < 0) | |
350 | return r; | |
351 | if (r == 0) | |
352 | continue; | |
95ef0eaf | 353 | |
7f304b85 YW |
354 | r = readlinkat_malloc(dirfd(d), de->d_name, &target); |
355 | if (r < 0) { | |
356 | log_warning_errno(r, "Failed to read symlink %s/%s, ignoring: %m", | |
357 | *dir, de->d_name); | |
358 | continue; | |
95ef0eaf | 359 | } |
7f304b85 YW |
360 | |
361 | r = is_dir(target, /* follow = */ true); | |
362 | if (r <= 0) | |
363 | continue; | |
364 | ||
365 | symlink_to_dir = true; | |
95ef0eaf LP |
366 | } |
367 | ||
95ef0eaf | 368 | } else |
890befcf | 369 | continue; |
e8630e69 ZJS |
370 | |
371 | filename = path_join(*dir, de->d_name); | |
372 | if (!filename) | |
373 | return log_oom(); | |
374 | ||
3fb2326f | 375 | if (paths) { |
7f1238bd | 376 | r = set_put(paths, filename); |
e8630e69 ZJS |
377 | if (r < 0) |
378 | return log_oom(); | |
7f1238bd YW |
379 | if (r == 0) |
380 | _filename_free = filename; /* Make sure we free the filename. */ | |
e8630e69 ZJS |
381 | } else |
382 | _filename_free = filename; /* Make sure we free the filename. */ | |
383 | ||
7f304b85 | 384 | if (de->d_type == DT_DIR || (de->d_type == DT_LNK && symlink_to_dir)) |
e8630e69 | 385 | continue; |
e8630e69 | 386 | |
7f304b85 YW |
387 | assert(IN_SET(de->d_type, DT_REG, DT_LNK)); |
388 | ||
e8630e69 ZJS |
389 | /* search_path is ordered by priority (highest first). If the name is already mapped |
390 | * to something (incl. itself), it means that we have already seen it, and we should | |
391 | * ignore it here. */ | |
392 | if (hashmap_contains(ids, de->d_name)) | |
393 | continue; | |
394 | ||
395 | if (de->d_type == DT_LNK) { | |
396 | /* We don't explicitly check for alias loops here. unit_ids_map_get() which | |
397 | * limits the number of hops should be used to access the map. */ | |
398 | ||
b8239b9c | 399 | _cleanup_free_ char *target = NULL; |
e8630e69 ZJS |
400 | |
401 | r = readlinkat_malloc(dirfd(d), de->d_name, &target); | |
402 | if (r < 0) { | |
403 | log_warning_errno(r, "Failed to read symlink %s/%s, ignoring: %m", | |
404 | *dir, de->d_name); | |
405 | continue; | |
406 | } | |
407 | ||
b8239b9c ZJS |
408 | const bool is_abs = path_is_absolute(target); |
409 | if (lp->root_dir || !is_abs) { | |
410 | char *target_abs = path_join(is_abs ? lp->root_dir : *dir, target); | |
e8630e69 ZJS |
411 | if (!target_abs) |
412 | return log_oom(); | |
413 | ||
414 | free_and_replace(target, target_abs); | |
415 | } | |
416 | ||
417 | /* Get rid of "." and ".." components in target path */ | |
a5648b80 | 418 | r = chase_symlinks(target, lp->root_dir, CHASE_NOFOLLOW | CHASE_NONEXISTENT, &simplified, NULL); |
e8630e69 ZJS |
419 | if (r < 0) { |
420 | log_warning_errno(r, "Failed to resolve symlink %s pointing to %s, ignoring: %m", | |
421 | filename, target); | |
422 | continue; | |
423 | } | |
424 | ||
425 | /* Check if the symlink goes outside of our search path. | |
426 | * If yes, it's a linked unit file or mask, and we don't care about the target name. | |
3aa57658 | 427 | * Let's just store the link source directly. |
e8630e69 ZJS |
428 | * If not, let's verify that it's a good symlink. */ |
429 | char *tail = path_startswith_strv(simplified, lp->search_path); | |
3aa57658 ZJS |
430 | if (!tail) { |
431 | log_debug("%s: linked unit file: %s → %s", | |
432 | __func__, filename, simplified); | |
433 | ||
434 | dst = filename; | |
435 | } else { | |
436 | ||
e8630e69 ZJS |
437 | bool self_alias; |
438 | ||
439 | dst = basename(simplified); | |
440 | self_alias = streq(dst, de->d_name); | |
441 | ||
442 | if (is_path(tail)) | |
443 | log_full(self_alias ? LOG_DEBUG : LOG_WARNING, | |
444 | "Suspicious symlink %s→%s, treating as alias.", | |
445 | filename, simplified); | |
446 | ||
447 | r = unit_validate_alias_symlink_and_warn(filename, simplified); | |
448 | if (r < 0) | |
449 | continue; | |
450 | ||
451 | if (self_alias) { | |
452 | /* A self-alias that has no effect */ | |
453 | log_debug("%s: self-alias: %s/%s → %s, ignoring.", | |
454 | __func__, *dir, de->d_name, dst); | |
455 | continue; | |
456 | } | |
457 | ||
458 | log_debug("%s: alias: %s/%s → %s", __func__, *dir, de->d_name, dst); | |
e8630e69 ZJS |
459 | } |
460 | ||
461 | } else { | |
462 | dst = filename; | |
463 | log_debug("%s: normal unit file: %s", __func__, dst); | |
464 | } | |
465 | ||
466 | r = hashmap_put_strdup(&ids, de->d_name, dst); | |
467 | if (r < 0) | |
468 | return log_warning_errno(r, "Failed to add entry to hashmap (%s→%s): %m", | |
469 | de->d_name, dst); | |
470 | } | |
471 | } | |
472 | ||
473 | /* Let's also put the names in the reverse db. */ | |
e8630e69 | 474 | const char *dummy, *src; |
90e74a66 | 475 | HASHMAP_FOREACH_KEY(dummy, src, ids) { |
fd228387 | 476 | _cleanup_free_ char *inst = NULL, *dst_inst = NULL; |
e8630e69 ZJS |
477 | const char *dst; |
478 | ||
479 | r = unit_ids_map_get(ids, src, &dst); | |
480 | if (r < 0) | |
481 | continue; | |
482 | ||
483 | if (null_or_empty_path(dst) != 0) | |
484 | continue; | |
485 | ||
fd228387 ZJS |
486 | dst = basename(dst); |
487 | ||
488 | /* If we have an symlink from an instance name to a template name, it is an alias just for | |
489 | * this specific instance, foo@id.service ↔ template@id.service. */ | |
490 | if (unit_name_is_valid(dst, UNIT_NAME_TEMPLATE)) { | |
491 | UnitNameFlags t = unit_name_to_instance(src, &inst); | |
492 | if (t < 0) | |
493 | return log_error_errno(t, "Failed to extract instance part from %s: %m", src); | |
494 | if (t == UNIT_NAME_INSTANCE) { | |
495 | r = unit_name_replace_instance(dst, inst, &dst_inst); | |
496 | if (r < 0) { | |
497 | /* This might happen e.g. if the combined length is too large. | |
498 | * Let's not make too much of a fuss. */ | |
499 | log_debug_errno(r, "Failed to build alias name (%s + %s), ignoring: %m", | |
500 | dst, inst); | |
501 | continue; | |
502 | } | |
e8630e69 | 503 | |
fd228387 ZJS |
504 | dst = dst_inst; |
505 | } | |
506 | } | |
507 | ||
508 | r = string_strv_hashmap_put(&names, dst, src); | |
e8630e69 | 509 | if (r < 0) |
fd228387 | 510 | return log_warning_errno(r, "Failed to add entry to hashmap (%s→%s): %m", dst, src); |
e8630e69 ZJS |
511 | } |
512 | ||
c2911d48 ZJS |
513 | if (cache_timestamp_hash) |
514 | *cache_timestamp_hash = timestamp_hash; | |
3fb2326f ZJS |
515 | |
516 | hashmap_free_and_replace(*unit_ids_map, ids); | |
517 | hashmap_free_and_replace(*unit_names_map, names); | |
518 | if (path_cache) | |
519 | set_free_and_replace(*path_cache, paths); | |
e8630e69 | 520 | |
91e0ee5f | 521 | return 1; |
e8630e69 ZJS |
522 | } |
523 | ||
fd228387 ZJS |
524 | static int add_name( |
525 | const char *unit_name, | |
526 | Set **names, | |
527 | const char *name) { | |
528 | int r; | |
529 | ||
530 | assert(names); | |
531 | assert(name); | |
532 | ||
533 | r = set_put_strdup(names, name); | |
534 | if (r < 0) | |
535 | return r; | |
536 | if (r > 0 && !streq(unit_name, name)) | |
537 | log_debug("Unit %s has alias %s.", unit_name, name); | |
538 | return r; | |
539 | } | |
540 | ||
541 | static int add_names( | |
542 | Hashmap *unit_ids_map, | |
543 | Hashmap *unit_name_map, | |
544 | const char *unit_name, | |
545 | const char *fragment_basename, /* Only set when adding additional names based on fragment path */ | |
546 | UnitNameFlags name_type, | |
547 | const char *instance, | |
548 | Set **names, | |
549 | const char *name) { | |
550 | ||
de010b0b | 551 | char **aliases; |
fd228387 ZJS |
552 | int r; |
553 | ||
554 | assert(name_type == UNIT_NAME_PLAIN || instance); | |
555 | ||
556 | /* The unit has its own name if it's not a template. If we're looking at a fragment, the fragment | |
557 | * name (possibly with instance inserted), is also always one of the unit names. */ | |
558 | if (name_type != UNIT_NAME_TEMPLATE) { | |
559 | r = add_name(unit_name, names, name); | |
560 | if (r < 0) | |
561 | return r; | |
562 | } | |
563 | ||
564 | /* Add any aliases of the name to the set of names. | |
565 | * | |
566 | * We don't even need to know which fragment we will use. The unit_name_map should return the same | |
567 | * set of names for any of the aliases. */ | |
568 | aliases = hashmap_get(unit_name_map, name); | |
569 | STRV_FOREACH(alias, aliases) { | |
570 | if (name_type == UNIT_NAME_INSTANCE && unit_name_is_valid(*alias, UNIT_NAME_TEMPLATE)) { | |
571 | _cleanup_free_ char *inst = NULL; | |
572 | const char *inst_fragment = NULL; | |
573 | ||
574 | r = unit_name_replace_instance(*alias, instance, &inst); | |
575 | if (r < 0) | |
576 | return log_debug_errno(r, "Cannot build instance name %s + %s: %m", | |
577 | *alias, instance); | |
578 | ||
579 | /* Exclude any aliases that point in some other direction. | |
580 | * | |
581 | * See https://github.com/systemd/systemd/pull/13119#discussion_r308145418. */ | |
582 | r = unit_ids_map_get(unit_ids_map, inst, &inst_fragment); | |
583 | if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO)) | |
584 | return log_debug_errno(r, "Cannot find instance fragment %s: %m", inst); | |
585 | ||
586 | if (inst_fragment && | |
587 | !streq(basename(inst_fragment), fragment_basename)) { | |
588 | log_debug("Instance %s has fragment %s and is not an alias of %s.", | |
589 | inst, inst_fragment, unit_name); | |
590 | continue; | |
591 | } | |
592 | ||
593 | r = set_consume(*names, TAKE_PTR(inst)); | |
594 | if (r > 0) | |
595 | log_debug("Unit %s has alias %s.", unit_name, inst); | |
596 | } else | |
597 | r = add_name(unit_name, names, *alias); | |
598 | ||
599 | if (r < 0) | |
600 | return r; | |
601 | } | |
602 | ||
603 | return 0; | |
604 | } | |
605 | ||
e8630e69 ZJS |
606 | int unit_file_find_fragment( |
607 | Hashmap *unit_ids_map, | |
608 | Hashmap *unit_name_map, | |
609 | const char *unit_name, | |
610 | const char **ret_fragment_path, | |
611 | Set **ret_names) { | |
612 | ||
613 | const char *fragment = NULL; | |
614 | _cleanup_free_ char *template = NULL, *instance = NULL; | |
fd228387 ZJS |
615 | _cleanup_set_free_ Set *names = NULL; |
616 | int r; | |
e8630e69 ZJS |
617 | |
618 | /* Finds a fragment path, and returns the set of names: | |
619 | * if we have …/foo.service and …/foo-alias.service→foo.service, | |
620 | * and …/foo@.service and …/foo-alias@.service→foo@.service, | |
621 | * and …/foo@inst.service, | |
622 | * this should return: | |
623 | * foo.service → …/foo.service, {foo.service, foo-alias.service}, | |
624 | * foo-alias.service → …/foo.service, {foo.service, foo-alias.service}, | |
625 | * foo@.service → …/foo@.service, {foo@.service, foo-alias@.service}, | |
626 | * foo-alias@.service → …/foo@.service, {foo@.service, foo-alias@.service}, | |
627 | * foo@bar.service → …/foo@.service, {foo@bar.service, foo-alias@bar.service}, | |
628 | * foo-alias@bar.service → …/foo@.service, {foo@bar.service, foo-alias@bar.service}, | |
629 | * foo-alias@inst.service → …/foo@inst.service, {foo@inst.service, foo-alias@inst.service}. | |
630 | */ | |
631 | ||
fd228387 | 632 | UnitNameFlags name_type = unit_name_to_instance(unit_name, &instance); |
e8630e69 ZJS |
633 | if (name_type < 0) |
634 | return name_type; | |
635 | ||
11e9347b DDM |
636 | if (ret_names) { |
637 | r = add_names(unit_ids_map, unit_name_map, unit_name, NULL, name_type, instance, &names, unit_name); | |
638 | if (r < 0) | |
639 | return r; | |
640 | } | |
e8630e69 ZJS |
641 | |
642 | /* First try to load fragment under the original name */ | |
643 | r = unit_ids_map_get(unit_ids_map, unit_name, &fragment); | |
644 | if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO)) | |
645 | return log_debug_errno(r, "Cannot load unit %s: %m", unit_name); | |
646 | ||
e8630e69 ZJS |
647 | if (!fragment && name_type == UNIT_NAME_INSTANCE) { |
648 | /* Look for a fragment under the template name */ | |
649 | ||
650 | r = unit_name_template(unit_name, &template); | |
651 | if (r < 0) | |
98fac96c | 652 | return log_debug_errno(r, "Failed to determine template name: %m"); |
e8630e69 ZJS |
653 | |
654 | r = unit_ids_map_get(unit_ids_map, template, &fragment); | |
655 | if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO)) | |
771f8aef | 656 | return log_debug_errno(r, "Cannot load template %s: %m", template); |
fd228387 | 657 | } |
e8630e69 | 658 | |
11e9347b | 659 | if (fragment && ret_names) { |
fd228387 | 660 | const char *fragment_basename = basename(fragment); |
e8630e69 | 661 | |
fd228387 ZJS |
662 | if (!streq(fragment_basename, unit_name)) { |
663 | /* Add names based on the fragment name to the set of names */ | |
664 | r = add_names(unit_ids_map, unit_name_map, unit_name, fragment_basename, name_type, instance, &names, fragment_basename); | |
665 | if (r < 0) | |
666 | return r; | |
e8630e69 ZJS |
667 | } |
668 | } | |
669 | ||
670 | *ret_fragment_path = fragment; | |
11e9347b DDM |
671 | if (ret_names) |
672 | *ret_names = TAKE_PTR(names); | |
e8630e69 | 673 | |
e8630e69 ZJS |
674 | return 0; |
675 | } | |
da33cba0 ZJS |
676 | |
677 | static const char * const rlmap[] = { | |
678 | "emergency", SPECIAL_EMERGENCY_TARGET, | |
679 | "-b", SPECIAL_EMERGENCY_TARGET, | |
680 | "rescue", SPECIAL_RESCUE_TARGET, | |
681 | "single", SPECIAL_RESCUE_TARGET, | |
682 | "-s", SPECIAL_RESCUE_TARGET, | |
683 | "s", SPECIAL_RESCUE_TARGET, | |
684 | "S", SPECIAL_RESCUE_TARGET, | |
685 | "1", SPECIAL_RESCUE_TARGET, | |
686 | "2", SPECIAL_MULTI_USER_TARGET, | |
687 | "3", SPECIAL_MULTI_USER_TARGET, | |
688 | "4", SPECIAL_MULTI_USER_TARGET, | |
689 | "5", SPECIAL_GRAPHICAL_TARGET, | |
690 | NULL | |
691 | }; | |
692 | ||
693 | static const char * const rlmap_initrd[] = { | |
694 | "emergency", SPECIAL_EMERGENCY_TARGET, | |
695 | "rescue", SPECIAL_RESCUE_TARGET, | |
696 | NULL | |
697 | }; | |
698 | ||
699 | const char* runlevel_to_target(const char *word) { | |
700 | const char * const *rlmap_ptr; | |
da33cba0 ZJS |
701 | |
702 | if (!word) | |
703 | return NULL; | |
704 | ||
705 | if (in_initrd()) { | |
706 | word = startswith(word, "rd."); | |
707 | if (!word) | |
708 | return NULL; | |
709 | } | |
710 | ||
711 | rlmap_ptr = in_initrd() ? rlmap_initrd : rlmap; | |
712 | ||
fe96c0f8 | 713 | for (size_t i = 0; rlmap_ptr[i]; i += 2) |
da33cba0 ZJS |
714 | if (streq(word, rlmap_ptr[i])) |
715 | return rlmap_ptr[i+1]; | |
716 | ||
717 | return NULL; | |
718 | } |