]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/unit-file.c
core: do not touch /run/systemd/systemd-units-load from user session instances
[thirdparty/systemd.git] / src / basic / unit-file.c
CommitLineData
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
18bool 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
28bool 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 37int 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
72int 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
146static 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
194static 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
207bool 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
212 char **dir;
91e0ee5f
ZJS
213 STRV_FOREACH(dir, (char**) lp->search_path) {
214 struct stat st;
215
216 if (lookup_paths_mtime_exclude(lp, *dir))
217 continue;
218
219 /* Determine the latest lookup path modification time */
220 if (stat(*dir, &st) < 0) {
221 if (errno == ENOENT)
222 continue;
223
224 log_debug_errno(errno, "Failed to stat %s, ignoring: %m", *dir);
225 continue;
226 }
227
c2911d48 228 siphash24_compress_usec_t(timespec_load(&st.st_mtim), &state);
91e0ee5f
ZJS
229 }
230
c2911d48
ZJS
231 uint64_t updated = siphash24_finalize(&state);
232 if (ret_new)
233 *ret_new = updated;
234 if (updated != timestamp_hash)
235 log_debug("Modification times have changed, need to update cache.");
236 return updated == timestamp_hash;
91e0ee5f
ZJS
237}
238
e8630e69
ZJS
239int unit_file_build_name_map(
240 const LookupPaths *lp,
c2911d48 241 uint64_t *cache_timestamp_hash,
3fb2326f
ZJS
242 Hashmap **unit_ids_map,
243 Hashmap **unit_names_map,
244 Set **path_cache) {
e8630e69
ZJS
245
246 /* Build two mappings: any name → main unit (i.e. the end result of symlink resolution), unit name →
fa027117 247 * all aliases (i.e. the entry for a given key is a list of all names which point to this key). The
e8630e69
ZJS
248 * key is included in the value iff we saw a file or symlink with that name. In other words, if we
249 * have a key, but it is not present in the value for itself, there was an alias pointing to it, but
250 * the unit itself is not loadable.
251 *
3fb2326f
ZJS
252 * At the same, build a cache of paths where to find units. The non-const parameters are for input
253 * and output. Existing contents will be freed before the new contents are stored.
e8630e69
ZJS
254 */
255
256 _cleanup_hashmap_free_ Hashmap *ids = NULL, *names = NULL;
257 _cleanup_set_free_free_ Set *paths = NULL;
c2911d48 258 uint64_t timestamp_hash;
e8630e69
ZJS
259 char **dir;
260 int r;
91e0ee5f 261
c2911d48
ZJS
262 /* Before doing anything, check if the timestamp hash that was passed is still valid.
263 * If yes, do nothing. */
264 if (cache_timestamp_hash &&
265 lookup_paths_timestamp_hash_same(lp, *cache_timestamp_hash, &timestamp_hash))
f6067186 266 return 0;
c2911d48
ZJS
267
268 /* The timestamp hash is now set based on the mtimes from before when we start reading files.
269 * If anything is modified concurrently, we'll consider the cache outdated. */
e8630e69 270
3fb2326f
ZJS
271 if (path_cache) {
272 paths = set_new(&path_hash_ops_free);
e8630e69
ZJS
273 if (!paths)
274 return log_oom();
275 }
276
277 STRV_FOREACH(dir, (char**) lp->search_path) {
e8630e69
ZJS
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
14bb7295 287 FOREACH_DIRENT_ALL(de, d, log_warning_errno(errno, "Failed to read \"%s\", ignoring: %m", *dir)) {
d7ac0952
FS
288 _unused_ _cleanup_free_ char *_filename_free = NULL;
289 _cleanup_free_ char *simplified = NULL;
95ef0eaf
LP
290 const char *dst = NULL;
291 char *filename;
890befcf
ZJS
292
293 /* We only care about valid units and dirs with certain suffixes, let's ignore the
294 * rest. */
95ef0eaf
LP
295
296 if (IN_SET(de->d_type, DT_REG, DT_LNK)) {
297
298 if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
299 continue;
300
301 /* Accept a regular file or symlink whose name is a valid unit file name. */
302
303 } else if (de->d_type == DT_DIR) {
304 bool valid_dir_name = false;
305 const char *suffix;
306
307 /* Also accept a directory whose name is a valid unit file name ending in
308 * .wants/, .requires/ or .d/ */
309
310 if (!paths) /* Skip directories early unless path_cache is requested */
311 continue;
312
313 FOREACH_STRING(suffix, ".wants", ".requires", ".d") {
314 _cleanup_free_ char *chopped = NULL;
315 const char *e;
316
317 e = endswith(de->d_name, suffix);
318 if (!e)
319 continue;
320
321 chopped = strndup(de->d_name, e - de->d_name);
322 if (!chopped)
323 return log_oom();
324
325 if (unit_name_is_valid(chopped, UNIT_NAME_ANY) ||
326 unit_type_from_string(chopped) >= 0) {
327 valid_dir_name = true;
328 break;
329 }
330 }
331
332 if (!valid_dir_name)
333 continue;
334 } else
890befcf 335 continue;
e8630e69
ZJS
336
337 filename = path_join(*dir, de->d_name);
338 if (!filename)
339 return log_oom();
340
3fb2326f 341 if (paths) {
7f1238bd 342 r = set_put(paths, filename);
e8630e69
ZJS
343 if (r < 0)
344 return log_oom();
7f1238bd
YW
345 if (r == 0)
346 _filename_free = filename; /* Make sure we free the filename. */
e8630e69
ZJS
347 } else
348 _filename_free = filename; /* Make sure we free the filename. */
349
95ef0eaf 350 if (!IN_SET(de->d_type, DT_REG, DT_LNK))
e8630e69 351 continue;
e8630e69
ZJS
352
353 /* search_path is ordered by priority (highest first). If the name is already mapped
354 * to something (incl. itself), it means that we have already seen it, and we should
355 * ignore it here. */
356 if (hashmap_contains(ids, de->d_name))
357 continue;
358
359 if (de->d_type == DT_LNK) {
360 /* We don't explicitly check for alias loops here. unit_ids_map_get() which
361 * limits the number of hops should be used to access the map. */
362
b8239b9c 363 _cleanup_free_ char *target = NULL;
e8630e69
ZJS
364
365 r = readlinkat_malloc(dirfd(d), de->d_name, &target);
366 if (r < 0) {
367 log_warning_errno(r, "Failed to read symlink %s/%s, ignoring: %m",
368 *dir, de->d_name);
369 continue;
370 }
371
b8239b9c
ZJS
372 const bool is_abs = path_is_absolute(target);
373 if (lp->root_dir || !is_abs) {
374 char *target_abs = path_join(is_abs ? lp->root_dir : *dir, target);
e8630e69
ZJS
375 if (!target_abs)
376 return log_oom();
377
378 free_and_replace(target, target_abs);
379 }
380
381 /* Get rid of "." and ".." components in target path */
a5648b80 382 r = chase_symlinks(target, lp->root_dir, CHASE_NOFOLLOW | CHASE_NONEXISTENT, &simplified, NULL);
e8630e69
ZJS
383 if (r < 0) {
384 log_warning_errno(r, "Failed to resolve symlink %s pointing to %s, ignoring: %m",
385 filename, target);
386 continue;
387 }
388
389 /* Check if the symlink goes outside of our search path.
390 * If yes, it's a linked unit file or mask, and we don't care about the target name.
3aa57658 391 * Let's just store the link source directly.
e8630e69
ZJS
392 * If not, let's verify that it's a good symlink. */
393 char *tail = path_startswith_strv(simplified, lp->search_path);
3aa57658
ZJS
394 if (!tail) {
395 log_debug("%s: linked unit file: %s → %s",
396 __func__, filename, simplified);
397
398 dst = filename;
399 } else {
400
e8630e69
ZJS
401 bool self_alias;
402
403 dst = basename(simplified);
404 self_alias = streq(dst, de->d_name);
405
406 if (is_path(tail))
407 log_full(self_alias ? LOG_DEBUG : LOG_WARNING,
408 "Suspicious symlink %s→%s, treating as alias.",
409 filename, simplified);
410
411 r = unit_validate_alias_symlink_and_warn(filename, simplified);
412 if (r < 0)
413 continue;
414
415 if (self_alias) {
416 /* A self-alias that has no effect */
417 log_debug("%s: self-alias: %s/%s → %s, ignoring.",
418 __func__, *dir, de->d_name, dst);
419 continue;
420 }
421
422 log_debug("%s: alias: %s/%s → %s", __func__, *dir, de->d_name, dst);
e8630e69
ZJS
423 }
424
425 } else {
426 dst = filename;
427 log_debug("%s: normal unit file: %s", __func__, dst);
428 }
429
430 r = hashmap_put_strdup(&ids, de->d_name, dst);
431 if (r < 0)
432 return log_warning_errno(r, "Failed to add entry to hashmap (%s→%s): %m",
433 de->d_name, dst);
434 }
435 }
436
437 /* Let's also put the names in the reverse db. */
e8630e69 438 const char *dummy, *src;
90e74a66 439 HASHMAP_FOREACH_KEY(dummy, src, ids) {
fd228387 440 _cleanup_free_ char *inst = NULL, *dst_inst = NULL;
e8630e69
ZJS
441 const char *dst;
442
443 r = unit_ids_map_get(ids, src, &dst);
444 if (r < 0)
445 continue;
446
447 if (null_or_empty_path(dst) != 0)
448 continue;
449
fd228387
ZJS
450 dst = basename(dst);
451
452 /* If we have an symlink from an instance name to a template name, it is an alias just for
453 * this specific instance, foo@id.service ↔ template@id.service. */
454 if (unit_name_is_valid(dst, UNIT_NAME_TEMPLATE)) {
455 UnitNameFlags t = unit_name_to_instance(src, &inst);
456 if (t < 0)
457 return log_error_errno(t, "Failed to extract instance part from %s: %m", src);
458 if (t == UNIT_NAME_INSTANCE) {
459 r = unit_name_replace_instance(dst, inst, &dst_inst);
460 if (r < 0) {
461 /* This might happen e.g. if the combined length is too large.
462 * Let's not make too much of a fuss. */
463 log_debug_errno(r, "Failed to build alias name (%s + %s), ignoring: %m",
464 dst, inst);
465 continue;
466 }
e8630e69 467
fd228387
ZJS
468 dst = dst_inst;
469 }
470 }
471
472 r = string_strv_hashmap_put(&names, dst, src);
e8630e69 473 if (r < 0)
fd228387 474 return log_warning_errno(r, "Failed to add entry to hashmap (%s→%s): %m", dst, src);
e8630e69
ZJS
475 }
476
c2911d48
ZJS
477 if (cache_timestamp_hash)
478 *cache_timestamp_hash = timestamp_hash;
3fb2326f
ZJS
479
480 hashmap_free_and_replace(*unit_ids_map, ids);
481 hashmap_free_and_replace(*unit_names_map, names);
482 if (path_cache)
483 set_free_and_replace(*path_cache, paths);
e8630e69 484
91e0ee5f 485 return 1;
e8630e69
ZJS
486}
487
fd228387
ZJS
488static int add_name(
489 const char *unit_name,
490 Set **names,
491 const char *name) {
492 int r;
493
494 assert(names);
495 assert(name);
496
497 r = set_put_strdup(names, name);
498 if (r < 0)
499 return r;
500 if (r > 0 && !streq(unit_name, name))
501 log_debug("Unit %s has alias %s.", unit_name, name);
502 return r;
503}
504
505static int add_names(
506 Hashmap *unit_ids_map,
507 Hashmap *unit_name_map,
508 const char *unit_name,
509 const char *fragment_basename, /* Only set when adding additional names based on fragment path */
510 UnitNameFlags name_type,
511 const char *instance,
512 Set **names,
513 const char *name) {
514
515 char **aliases, **alias;
516 int r;
517
518 assert(name_type == UNIT_NAME_PLAIN || instance);
519
520 /* The unit has its own name if it's not a template. If we're looking at a fragment, the fragment
521 * name (possibly with instance inserted), is also always one of the unit names. */
522 if (name_type != UNIT_NAME_TEMPLATE) {
523 r = add_name(unit_name, names, name);
524 if (r < 0)
525 return r;
526 }
527
528 /* Add any aliases of the name to the set of names.
529 *
530 * We don't even need to know which fragment we will use. The unit_name_map should return the same
531 * set of names for any of the aliases. */
532 aliases = hashmap_get(unit_name_map, name);
533 STRV_FOREACH(alias, aliases) {
534 if (name_type == UNIT_NAME_INSTANCE && unit_name_is_valid(*alias, UNIT_NAME_TEMPLATE)) {
535 _cleanup_free_ char *inst = NULL;
536 const char *inst_fragment = NULL;
537
538 r = unit_name_replace_instance(*alias, instance, &inst);
539 if (r < 0)
540 return log_debug_errno(r, "Cannot build instance name %s + %s: %m",
541 *alias, instance);
542
543 /* Exclude any aliases that point in some other direction.
544 *
545 * See https://github.com/systemd/systemd/pull/13119#discussion_r308145418. */
546 r = unit_ids_map_get(unit_ids_map, inst, &inst_fragment);
547 if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO))
548 return log_debug_errno(r, "Cannot find instance fragment %s: %m", inst);
549
550 if (inst_fragment &&
551 !streq(basename(inst_fragment), fragment_basename)) {
552 log_debug("Instance %s has fragment %s and is not an alias of %s.",
553 inst, inst_fragment, unit_name);
554 continue;
555 }
556
557 r = set_consume(*names, TAKE_PTR(inst));
558 if (r > 0)
559 log_debug("Unit %s has alias %s.", unit_name, inst);
560 } else
561 r = add_name(unit_name, names, *alias);
562
563 if (r < 0)
564 return r;
565 }
566
567 return 0;
568}
569
e8630e69
ZJS
570int unit_file_find_fragment(
571 Hashmap *unit_ids_map,
572 Hashmap *unit_name_map,
573 const char *unit_name,
574 const char **ret_fragment_path,
575 Set **ret_names) {
576
577 const char *fragment = NULL;
578 _cleanup_free_ char *template = NULL, *instance = NULL;
fd228387
ZJS
579 _cleanup_set_free_ Set *names = NULL;
580 int r;
e8630e69
ZJS
581
582 /* Finds a fragment path, and returns the set of names:
583 * if we have …/foo.service and …/foo-alias.service→foo.service,
584 * and …/foo@.service and …/foo-alias@.service→foo@.service,
585 * and …/foo@inst.service,
586 * this should return:
587 * foo.service → …/foo.service, {foo.service, foo-alias.service},
588 * foo-alias.service → …/foo.service, {foo.service, foo-alias.service},
589 * foo@.service → …/foo@.service, {foo@.service, foo-alias@.service},
590 * foo-alias@.service → …/foo@.service, {foo@.service, foo-alias@.service},
591 * foo@bar.service → …/foo@.service, {foo@bar.service, foo-alias@bar.service},
592 * foo-alias@bar.service → …/foo@.service, {foo@bar.service, foo-alias@bar.service},
593 * foo-alias@inst.service → …/foo@inst.service, {foo@inst.service, foo-alias@inst.service}.
594 */
595
fd228387 596 UnitNameFlags name_type = unit_name_to_instance(unit_name, &instance);
e8630e69
ZJS
597 if (name_type < 0)
598 return name_type;
599
11e9347b
DDM
600 if (ret_names) {
601 r = add_names(unit_ids_map, unit_name_map, unit_name, NULL, name_type, instance, &names, unit_name);
602 if (r < 0)
603 return r;
604 }
e8630e69
ZJS
605
606 /* First try to load fragment under the original name */
607 r = unit_ids_map_get(unit_ids_map, unit_name, &fragment);
608 if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO))
609 return log_debug_errno(r, "Cannot load unit %s: %m", unit_name);
610
e8630e69
ZJS
611 if (!fragment && name_type == UNIT_NAME_INSTANCE) {
612 /* Look for a fragment under the template name */
613
614 r = unit_name_template(unit_name, &template);
615 if (r < 0)
98fac96c 616 return log_debug_errno(r, "Failed to determine template name: %m");
e8630e69
ZJS
617
618 r = unit_ids_map_get(unit_ids_map, template, &fragment);
619 if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO))
771f8aef 620 return log_debug_errno(r, "Cannot load template %s: %m", template);
fd228387 621 }
e8630e69 622
11e9347b 623 if (fragment && ret_names) {
fd228387 624 const char *fragment_basename = basename(fragment);
e8630e69 625
fd228387
ZJS
626 if (!streq(fragment_basename, unit_name)) {
627 /* Add names based on the fragment name to the set of names */
628 r = add_names(unit_ids_map, unit_name_map, unit_name, fragment_basename, name_type, instance, &names, fragment_basename);
629 if (r < 0)
630 return r;
e8630e69
ZJS
631 }
632 }
633
634 *ret_fragment_path = fragment;
11e9347b
DDM
635 if (ret_names)
636 *ret_names = TAKE_PTR(names);
e8630e69 637
e8630e69
ZJS
638 return 0;
639}
da33cba0
ZJS
640
641static const char * const rlmap[] = {
642 "emergency", SPECIAL_EMERGENCY_TARGET,
643 "-b", SPECIAL_EMERGENCY_TARGET,
644 "rescue", SPECIAL_RESCUE_TARGET,
645 "single", SPECIAL_RESCUE_TARGET,
646 "-s", SPECIAL_RESCUE_TARGET,
647 "s", SPECIAL_RESCUE_TARGET,
648 "S", SPECIAL_RESCUE_TARGET,
649 "1", SPECIAL_RESCUE_TARGET,
650 "2", SPECIAL_MULTI_USER_TARGET,
651 "3", SPECIAL_MULTI_USER_TARGET,
652 "4", SPECIAL_MULTI_USER_TARGET,
653 "5", SPECIAL_GRAPHICAL_TARGET,
654 NULL
655};
656
657static const char * const rlmap_initrd[] = {
658 "emergency", SPECIAL_EMERGENCY_TARGET,
659 "rescue", SPECIAL_RESCUE_TARGET,
660 NULL
661};
662
663const char* runlevel_to_target(const char *word) {
664 const char * const *rlmap_ptr;
da33cba0
ZJS
665
666 if (!word)
667 return NULL;
668
669 if (in_initrd()) {
670 word = startswith(word, "rd.");
671 if (!word)
672 return NULL;
673 }
674
675 rlmap_ptr = in_initrd() ? rlmap_initrd : rlmap;
676
fe96c0f8 677 for (size_t i = 0; rlmap_ptr[i]; i += 2)
da33cba0
ZJS
678 if (streq(word, rlmap_ptr[i]))
679 return rlmap_ptr[i+1];
680
681 return NULL;
682}