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