Debian Codegraph @

src/libsystemd/sd-path/path-lookup.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4
5 #include "alloc-util.h"
6 #include "fs-util.h"
7 #include "log.h"
8 #include "mkdir.h"
9 #include "path-lookup.h"
10 #include "path-util.h"
11 #include "stat-util.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "tmpfile-util.h"
15
16 int user_search_dirs(const char *suffix, char ***ret_config_dirs, char ***ret_data_dirs) {
17 _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
18 int r;
19
20 assert(ret_config_dirs);
21 assert(ret_data_dirs);
22
23 r = sd_path_lookup_strv(SD_PATH_SEARCH_CONFIGURATION, suffix, &config_dirs);
24 if (r < 0)
25 return r;
26
27 r = sd_path_lookup_strv(SD_PATH_SEARCH_SHARED, suffix, &data_dirs);
28 if (r < 0)
29 return r;
30
31 *ret_config_dirs = TAKE_PTR(config_dirs);
32 *ret_data_dirs = TAKE_PTR(data_dirs);
33
34 return 0;
35 }
36
37 int config_directory_generic(RuntimeScope scope, const char *suffix, char **ret) {
38 assert(ret);
39
40 /* This does not bother with $CONFIGURATION_DIRECTORY, and hence can be applied to get other
41 * service's config dir */
42
43 switch (scope) {
44 case RUNTIME_SCOPE_USER:
45 return xdg_user_config_dir(suffix, ret);
46
47 case RUNTIME_SCOPE_SYSTEM: {
48 char *d = path_join("/etc", suffix);
49 if (!d)
50 return -ENOMEM;
51 *ret = d;
52 return 0;
53 }
54
55 default:
56 return -EINVAL;
57 }
58 }
59
60 int runtime_directory_generic(RuntimeScope scope, const char *suffix, char **ret) {
61 assert(ret);
62
63 /* This does not bother with $RUNTIME_DIRECTORY, and hence can be applied to get other service's
64 * runtime dir */
65
66 switch (scope) {
67 case RUNTIME_SCOPE_USER:
68 return xdg_user_runtime_dir(suffix, ret);
69
70 case RUNTIME_SCOPE_SYSTEM: {
71 char *d = path_join("/run", suffix);
72 if (!d)
73 return -ENOMEM;
74 *ret = d;
75 return 0;
76 }
77
78 default:
79 return -EINVAL;
80 }
81 }
82
83 int runtime_directory(RuntimeScope scope, const char *fallback_suffix, char **ret) {
84 int r;
85
86 assert(ret);
87
88 /* Accept $RUNTIME_DIRECTORY as authoritative, i.e. only works for our service's own runtime dir.
89 *
90 * If it's missing, apply the suffix to /run/, or $XDG_RUNTIME_DIR if we are in a user runtime scope.
91 *
92 * Return value indicates whether the suffix was applied or not. */
93
94 const char *e = secure_getenv("RUNTIME_DIRECTORY");
95 if (e)
96 return strdup_to(ret, e);
97
98 r = runtime_directory_generic(scope, fallback_suffix, ret);
99 if (r < 0)
100 return r;
101
102 return 1;
103 }
104
105 int runtime_directory_make(RuntimeScope scope, const char *subdir, char **ret_dir, char **ret_dir_destroy) {
106 _cleanup_free_ char *dir = NULL, *destroy = NULL;
107 int r;
108
109 assert(subdir);
110 assert(ret_dir);
111 assert(ret_dir_destroy);
112
113 /* Use runtime_directory() (not _generic()) so that when we run in a systemd service
114 * with RuntimeDirectory= set, we pick up $RUNTIME_DIRECTORY and place our stuff into the
115 * directory the service manager prepared for us. When the env var is unset, we fall back
116 * to the provided subdirectory under /run (or the $XDG_RUNTIME_DIR equivalent in user scope)
117 * and take care of creation and destruction ourselves. */
118 r = runtime_directory(scope, subdir, &dir);
119 if (r < 0)
120 return r;
121
122 if (r > 0) {
123 /* $RUNTIME_DIRECTORY was not set, so we got the fallback path and need to create and
124 * clean up the directory ourselves. */
125 destroy = strdup(dir);
126 if (!destroy)
127 return -ENOMEM;
128
129 r = mkdir_p(dir, 0755);
130 if (r < 0)
131 return r;
132 }
133
134 /* When $RUNTIME_DIRECTORY is set the service manager created the directory for us and
135 * will destroy it (or preserve it, per RuntimeDirectoryPreserve=) when the service stops. */
136
137 *ret_dir = TAKE_PTR(dir);
138 *ret_dir_destroy = TAKE_PTR(destroy);
139
140 return 0;
141 }
142
143 int state_directory_generic(RuntimeScope scope, const char *suffix, char **ret) {
144 assert(ret);
145
146 /* This does not bother with $STATE_DIRECTORY, and hence can be applied to get other service's state
147 * dir */
148
149 switch (scope) {
150
151 case RUNTIME_SCOPE_USER:
152 return xdg_user_state_dir(suffix, ret);
153
154 case RUNTIME_SCOPE_SYSTEM: {
155 char *d = path_join("/var/lib", suffix);
156 if (!d)
157 return -ENOMEM;
158 *ret = d;
159 return 0;
160 }
161
162 default:
163 return -EINVAL;
164 }
165 }
166
167 static const char* const user_data_unit_paths[] = {
168 "/usr/local/lib/systemd/user",
169 "/usr/local/share/systemd/user",
170 USER_DATA_UNIT_DIR,
171 "/usr/lib/systemd/user",
172 "/usr/share/systemd/user",
173 NULL
174 };
175
176 static const char* const user_config_unit_paths[] = {
177 USER_CONFIG_UNIT_DIR,
178 "/etc/systemd/user",
179 NULL
180 };
181
182 bool path_is_user_data_dir(const char *path) {
183 assert(path);
184
185 return path_strv_contains((char* const*) user_data_unit_paths, path);
186 }
187
188 bool path_is_user_config_dir(const char *path) {
189 assert(path);
190
191 return path_strv_contains((char* const*) user_config_unit_paths, path);
192 }
193
194 static int acquire_generator_dirs(
195 RuntimeScope scope,
196 const char *tempdir,
197 char **ret,
198 char **ret_early,
199 char **ret_late) {
200
201 _cleanup_free_ char *prefix_alloc = NULL, *g = NULL, *early = NULL, *late = NULL;
202 const char *prefix;
203 int r;
204
205 assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
206 assert(ret);
207 assert(ret_early);
208 assert(ret_late);
209
210 if (scope == RUNTIME_SCOPE_GLOBAL)
211 return -EOPNOTSUPP;
212
213 if (tempdir)
214 prefix = tempdir;
215 else if (scope == RUNTIME_SCOPE_SYSTEM)
216 prefix = "/run/systemd";
217 else { /* RUNTIME_SCOPE_USER */
218 r = xdg_user_runtime_dir("/systemd", &prefix_alloc);
219 if (r < 0)
220 return r;
221
222 prefix = prefix_alloc;
223 }
224
225 g = path_join(prefix, "generator");
226 if (!g)
227 return -ENOMEM;
228
229 early = path_join(prefix, "generator.early");
230 if (!early)
231 return -ENOMEM;
232
233 late = path_join(prefix, "generator.late");
234 if (!late)
235 return -ENOMEM;
236
237 *ret = TAKE_PTR(g);
238 *ret_early = TAKE_PTR(early);
239 *ret_late = TAKE_PTR(late);
240
241 return 0;
242 }
243
244 static int acquire_transient_dir(RuntimeScope scope, const char *tempdir, char **ret) {
245 char *transient;
246
247 assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
248 assert(ret);
249
250 if (scope == RUNTIME_SCOPE_GLOBAL)
251 return -EOPNOTSUPP;
252
253 if (tempdir)
254 transient = path_join(tempdir, "transient");
255 else if (scope == RUNTIME_SCOPE_SYSTEM)
256 transient = strdup("/run/systemd/transient");
257 else /* RUNTIME_SCOPE_USER */
258 return xdg_user_runtime_dir("/systemd/transient", ret);
259
260 if (!transient)
261 return -ENOMEM;
262
263 *ret = transient;
264 return 0;
265 }
266
267 typedef enum LookupDirType {
268 LOOKUP_DIR_CONFIG,
269 LOOKUP_DIR_CONTROL,
270 LOOKUP_DIR_ATTACHED,
271 _LOOKUP_DIR_MAX,
272 _LOOKUP_DIR_INVALID = -EINVAL,
273 } LookupDirType;
274
275 static int acquire_lookup_dirs(
276 LookupDirType type,
277 RuntimeScope scope,
278 char **ret_persistent,
279 char **ret_runtime) {
280
281 /* RUNTIME_SCOPE_USER dirs are relative to XDG_CONFIG_DIR and XDG_RUNTIME_DIR, respectively */
282 static const struct {
283 const char *persistent;
284 const char *runtime;
285 } dirs[_LOOKUP_DIR_MAX][_RUNTIME_SCOPE_MAX] = {
286 [LOOKUP_DIR_CONFIG] = {
287 [RUNTIME_SCOPE_SYSTEM] = { SYSTEM_CONFIG_UNIT_DIR, "/run/systemd/system" },
288 [RUNTIME_SCOPE_GLOBAL] = { USER_CONFIG_UNIT_DIR, "/run/systemd/user" },
289 [RUNTIME_SCOPE_USER] = { "systemd/user", "systemd/user" },
290 },
291 [LOOKUP_DIR_CONTROL] = {
292 [RUNTIME_SCOPE_SYSTEM] = { "/etc/systemd/system.control", "/run/systemd/system.control" },
293 [RUNTIME_SCOPE_USER] = { "systemd/user.control", "systemd/user.control" },
294 },
295 [LOOKUP_DIR_ATTACHED] = {
296 [RUNTIME_SCOPE_SYSTEM] = { "/etc/systemd/system.attached", "/run/systemd/system.attached" },
297 [RUNTIME_SCOPE_USER] = { "systemd/user.attached", "systemd/user.attached" },
298 },
299 };
300
301 _cleanup_free_ char *a = NULL, *b = NULL;
302 int r;
303
304 assert(type >= 0 && type < _LOOKUP_DIR_MAX);
305 assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
306 assert(ret_persistent);
307 assert(ret_runtime);
308
309 const char *persistent = dirs[type][scope].persistent;
310 const char *runtime = dirs[type][scope].runtime;
311 assert(!persistent == !runtime);
312
313 if (!persistent)
314 return -EOPNOTSUPP;
315
316 switch (scope) {
317
318 case RUNTIME_SCOPE_SYSTEM:
319 case RUNTIME_SCOPE_GLOBAL:
320 a = strdup(persistent);
321 b = strdup(runtime);
322 if (!a || !b)
323 return -ENOMEM;
324
325 *ret_persistent = TAKE_PTR(a);
326 *ret_runtime = TAKE_PTR(b);
327
328 return 0;
329
330 case RUNTIME_SCOPE_USER:
331 r = xdg_user_config_dir(persistent, &a);
332 if (r < 0)
333 return r;
334
335 r = xdg_user_runtime_dir(runtime, ret_runtime);
336 if (r < 0) {
337 if (r != -ENXIO)
338 return r;
339
340 /* If XDG_RUNTIME_DIR is not set, don't consider that fatal, simply initialize
341 * the runtime directory to NULL. */
342 *ret_runtime = NULL;
343 }
344
345 *ret_persistent = TAKE_PTR(a);
346
347 return 0;
348
349 default:
350 assert_not_reached();
351 }
352 }
353
354 static int patch_root_prefix(char **p, const char *root_dir) {
355 char *c;
356
357 assert(p);
358
359 if (!root_dir)
360 return 0;
361
362 if (!*p)
363 return 0;
364
365 c = path_join(root_dir, *p);
366 if (!c)
367 return -ENOMEM;
368
369 free_and_replace(*p, c);
370 return 0;
371 }
372
373 static int patch_root_prefix_strv(char **l, const char *root_dir) {
374 int r;
375
376 if (!root_dir)
377 return 0;
378
379 STRV_FOREACH(i, l) {
380 r = patch_root_prefix(i, root_dir);
381 if (r < 0)
382 return r;
383 }
384
385 return 0;
386 }
387
388 static int get_paths_from_environ(const char *var, char ***ret) {
389 const char *e;
390 int r;
391
392 assert(var);
393 assert(ret);
394
395 e = getenv(var);
396 if (!e) {
397 *ret = NULL;
398 return 0;
399 }
400
401 bool append = endswith(e, ":"); /* Whether to append the normal search paths after what's obtained
402 from envvar */
403
404 /* FIXME: empty components in other places should be rejected. */
405
406 r = path_split_and_make_absolute(e, ret);
407 if (r < 0)
408 return r;
409
410 return append;
411 }
412
413 static char** user_unit_search_dirs(
414 const char *persistent_config,
415 const char *persistent_attached,
416 const char *runtime_config,
417 const char *runtime_attached,
418 const char *global_persistent_config,
419 const char *global_runtime_config,
420 const char *generator,
421 const char *generator_early,
422 const char *generator_late,
423 const char *transient,
424 const char *persistent_control,
425 const char *runtime_control) {
426
427 _cleanup_strv_free_ char **paths = NULL, **config_dirs = NULL, **data_dirs = NULL;
428
429 /* The returned strv might contain duplicates, and we expect caller to filter them. */
430
431 assert(persistent_config);
432 assert(persistent_attached);
433 assert(global_persistent_config);
434 assert(global_runtime_config);
435 assert(persistent_control);
436
437 if (user_search_dirs("/systemd/user", &config_dirs, &data_dirs) < 0)
438 return NULL;
439
440 paths = strv_new(persistent_control,
441 STRV_IFNOTNULL(runtime_control),
442 STRV_IFNOTNULL(transient),
443 STRV_IFNOTNULL(generator_early),
444 persistent_config,
445 persistent_attached);
446 if (!paths)
447 return NULL;
448
449 if (strv_extend_strv_consume(&paths, TAKE_PTR(config_dirs), /* filter_duplicates= */ false) < 0)
450 return NULL;
451
452 /* global config has lower priority than the user config of the same type */
453 if (strv_extend(&paths, global_persistent_config) < 0)
454 return NULL;
455
456 if (strv_extend_strv(&paths, (char* const*) user_config_unit_paths, /* filter_duplicates= */ false) < 0)
457 return NULL;
458
459 /* strv_extend_many() can deal with NULL-s in arguments */
460 if (strv_extend_many(&paths,
461 runtime_config,
462 runtime_attached,
463 global_runtime_config,
464 generator) < 0)
465 return NULL;
466
467 if (strv_extend_strv_consume(&paths, TAKE_PTR(data_dirs), /* filter_duplicates= */ false) < 0)
468 return NULL;
469
470 if (strv_extend_strv(&paths, (char* const*) user_data_unit_paths, false) < 0)
471 return NULL;
472
473 if (strv_extend(&paths, generator_late) < 0)
474 return NULL;
475
476 if (path_strv_make_absolute_cwd(paths) < 0)
477 return NULL;
478
479 return TAKE_PTR(paths);
480 }
481
482 int lookup_paths_init(
483 LookupPaths *lp,
484 RuntimeScope scope,
485 LookupPathsFlags flags,
486 const char *root_dir) {
487
488 _cleanup_(rmdir_and_freep) char *tempdir = NULL;
489 _cleanup_free_ char
490 *root = NULL,
491 *persistent_config = NULL, *runtime_config = NULL,
492 *global_persistent_config = NULL, *global_runtime_config = NULL,
493 *generator = NULL, *generator_early = NULL, *generator_late = NULL,
494 *transient = NULL,
495 *persistent_control = NULL, *runtime_control = NULL,
496 *persistent_attached = NULL, *runtime_attached = NULL;
497 _cleanup_strv_free_ char **paths = NULL;
498 int r;
499
500 assert(lp);
501 assert(scope >= 0);
502 assert(scope < _RUNTIME_SCOPE_MAX);
503 assert(!FLAGS_SET(flags, LOOKUP_PATHS_EXCLUDE_GENERATED|LOOKUP_PATHS_TEMPORARY_GENERATED));
504
505 if (!empty_or_root(root_dir)) {
506 if (scope == RUNTIME_SCOPE_USER)
507 return -EINVAL;
508
509 r = is_dir(root_dir, true);
510 if (r < 0)
511 return r;
512 if (r == 0)
513 return -ENOTDIR;
514
515 root = strdup(root_dir);
516 if (!root)
517 return -ENOMEM;
518 }
519
520 if (FLAGS_SET(flags, LOOKUP_PATHS_TEMPORARY_GENERATED)) {
521 r = mkdtemp_malloc("/tmp/systemd-temporary-XXXXXX", &tempdir);
522 if (r < 0)
523 return log_debug_errno(r, "Failed to create temporary directory: %m");
524 }
525
526 /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_config to NULL */
527 r = acquire_lookup_dirs(LOOKUP_DIR_CONFIG, scope, &persistent_config, &runtime_config);
528 if (r < 0)
529 return r;
530
531 if (scope == RUNTIME_SCOPE_USER) {
532 r = acquire_lookup_dirs(LOOKUP_DIR_CONFIG, RUNTIME_SCOPE_GLOBAL, &global_persistent_config, &global_runtime_config);
533 if (r < 0)
534 return r;
535 }
536
537 r = acquire_lookup_dirs(LOOKUP_DIR_CONTROL, scope, &persistent_control, &runtime_control);
538 if (r < 0 && r != -EOPNOTSUPP)
539 return r;
540
541 r = acquire_lookup_dirs(LOOKUP_DIR_ATTACHED, scope, &persistent_attached, &runtime_attached);
542 if (r < 0 && r != -EOPNOTSUPP)
543 return r;
544
545 if (!FLAGS_SET(flags, LOOKUP_PATHS_EXCLUDE_GENERATED)) {
546 /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
547 r = acquire_generator_dirs(scope, tempdir,
548 &generator, &generator_early, &generator_late);
549 if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
550 return r;
551 }
552
553 /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
554 r = acquire_transient_dir(scope, tempdir, &transient);
555 if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
556 return r;
557
558 /* First priority is whatever has been passed to us via env vars */
559 r = get_paths_from_environ("SYSTEMD_UNIT_PATH", &paths);
560 if (r < 0)
561 return r;
562
563 if (!paths || r > 0) {
564 /* Let's figure something out. */
565
566 _cleanup_strv_free_ char **add = NULL;
567
568 /* For the user units we include share/ in the search paths in order to comply with
569 * the XDG basedir spec. For the system stuff we avoid such nonsense. OTOH we include (/usr/)lib/
570 * in the search paths for the system stuff but avoid it for user stuff. */
571
572 assert(persistent_config);
573
574 switch (scope) {
575
576 case RUNTIME_SCOPE_SYSTEM:
577 /* If you modify this you also want to modify systemdsystemunitpath= in systemd.pc.in! */
578 add = strv_new(ASSERT_PTR(persistent_control),
579 ASSERT_PTR(runtime_control),
580 ASSERT_PTR(transient),
581 STRV_IFNOTNULL(generator_early),
582 persistent_config,
583 SYSTEM_CONFIG_UNIT_DIR,
584 "/etc/systemd/system",
585 ASSERT_PTR(persistent_attached),
586 ASSERT_PTR(runtime_config),
587 "/run/systemd/system",
588 ASSERT_PTR(runtime_attached),
589 STRV_IFNOTNULL(generator),
590 "/usr/local/lib/systemd/system",
591 SYSTEM_DATA_UNIT_DIR,
592 "/usr/lib/systemd/system",
593 /* To be used ONLY for images which might be legacy split-usr */
594 FLAGS_SET(flags, LOOKUP_PATHS_SPLIT_USR) ? "/lib/systemd/system" : STRV_IGNORE,
595 STRV_IFNOTNULL(generator_late));
596 break;
597
598 case RUNTIME_SCOPE_GLOBAL:
599 /* If you modify this you also want to modify systemduserunitpath= in systemd.pc.in,
600 * and RUNTIME_SCOPE_USER search paths below! */
601
602 assert(!persistent_control);
603 assert(!runtime_control);
604 assert(!transient);
605 assert(!generator_early);
606 assert(!generator);
607 assert(!generator_late);
608
609 add = strv_new(persistent_config,
610 USER_CONFIG_UNIT_DIR,
611 "/etc/systemd/user",
612 ASSERT_PTR(runtime_config),
613 "/run/systemd/user",
614 "/usr/local/share/systemd/user",
615 "/usr/share/systemd/user",
616 "/usr/local/lib/systemd/user",
617 USER_DATA_UNIT_DIR,
618 "/usr/lib/systemd/user");
619 break;
620
621 case RUNTIME_SCOPE_USER:
622 add = user_unit_search_dirs(persistent_config, persistent_attached,
623 runtime_config, runtime_attached,
624 global_persistent_config, global_runtime_config,
625 generator, generator_early, generator_late,
626 transient,
627 persistent_control, runtime_control);
628 break;
629
630 default:
631 assert_not_reached();
632 }
633
634 if (!add)
635 return -ENOMEM;
636
637 /* strv_uniq() below would filter all duplicates against the final strv */
638 r = strv_extend_strv_consume(&paths, TAKE_PTR(add), /* filter_duplicates= */ false);
639 if (r < 0)
640 return r;
641 }
642
643 r = patch_root_prefix(&persistent_config, root);
644 if (r < 0)
645 return r;
646 r = patch_root_prefix(&runtime_config, root);
647 if (r < 0)
648 return r;
649
650 r = patch_root_prefix(&generator, root);
651 if (r < 0)
652 return r;
653 r = patch_root_prefix(&generator_early, root);
654 if (r < 0)
655 return r;
656 r = patch_root_prefix(&generator_late, root);
657 if (r < 0)
658 return r;
659
660 r = patch_root_prefix(&transient, root);
661 if (r < 0)
662 return r;
663
664 r = patch_root_prefix(&persistent_control, root);
665 if (r < 0)
666 return r;
667 r = patch_root_prefix(&runtime_control, root);
668 if (r < 0)
669 return r;
670
671 r = patch_root_prefix(&persistent_attached, root);
672 if (r < 0)
673 return r;
674 r = patch_root_prefix(&runtime_attached, root);
675 if (r < 0)
676 return r;
677
678 r = patch_root_prefix_strv(paths, root);
679 if (r < 0)
680 return -ENOMEM;
681
682 *lp = (LookupPaths) {
683 .search_path = strv_uniq(TAKE_PTR(paths)),
684
685 .persistent_config = TAKE_PTR(persistent_config),
686 .runtime_config = TAKE_PTR(runtime_config),
687
688 .generator = TAKE_PTR(generator),
689 .generator_early = TAKE_PTR(generator_early),
690 .generator_late = TAKE_PTR(generator_late),
691
692 .transient = TAKE_PTR(transient),
693
694 .persistent_control = TAKE_PTR(persistent_control),
695 .runtime_control = TAKE_PTR(runtime_control),
696
697 .persistent_attached = TAKE_PTR(persistent_attached),
698 .runtime_attached = TAKE_PTR(runtime_attached),
699
700 .root_dir = TAKE_PTR(root),
701 .temporary_dir = TAKE_PTR(tempdir),
702 };
703
704 return 0;
705 }
706
707 int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir) {
708 int r;
709
710 r = lookup_paths_init(lp, scope, flags, root_dir);
711 if (r < 0)
712 return log_error_errno(r, "Failed to initialize unit search paths%s%s: %m",
713 isempty(root_dir) ? "" : " for root directory ", strempty(root_dir));
714 return r;
715 }
716
717 void lookup_paths_done(LookupPaths *lp) {
718 assert(lp);
719
720 lp->search_path = strv_free(lp->search_path);
721
722 lp->persistent_config = mfree(lp->persistent_config);
723 lp->runtime_config = mfree(lp->runtime_config);
724
725 lp->persistent_attached = mfree(lp->persistent_attached);
726 lp->runtime_attached = mfree(lp->runtime_attached);
727
728 lp->generator = mfree(lp->generator);
729 lp->generator_early = mfree(lp->generator_early);
730 lp->generator_late = mfree(lp->generator_late);
731
732 lp->transient = mfree(lp->transient);
733
734 lp->persistent_control = mfree(lp->persistent_control);
735 lp->runtime_control = mfree(lp->runtime_control);
736
737 lp->root_dir = mfree(lp->root_dir);
738 lp->temporary_dir = mfree(lp->temporary_dir);
739 }
740
741 void lookup_paths_log(LookupPaths *lp) {
742 assert(lp);
743
744 if (strv_isempty(lp->search_path)) {
745 log_debug("Ignoring unit files.");
746 lp->search_path = strv_free(lp->search_path);
747 } else {
748 _cleanup_free_ char *t = NULL;
749
750 t = strv_join(lp->search_path, "\n\t");
751 log_debug("Looking for unit files in (higher priority first):\n\t%s", strna(t));
752 }
753 }
754
755 static const char* const system_generator_paths[] = {
756 "/run/systemd/system-generators",
757 "/etc/systemd/system-generators",
758 "/usr/local/lib/systemd/system-generators",
759 SYSTEM_GENERATOR_DIR,
760 NULL,
761 };
762
763 static const char* const user_generator_paths[] = {
764 "/run/systemd/user-generators",
765 "/etc/systemd/user-generators",
766 "/usr/local/lib/systemd/user-generators",
767 USER_GENERATOR_DIR,
768 NULL,
769 };
770
771 static const char* const system_env_generator_paths[] = {
772 "/run/systemd/system-environment-generators",
773 "/etc/systemd/system-environment-generators",
774 "/usr/local/lib/systemd/system-environment-generators",
775 SYSTEM_ENV_GENERATOR_DIR,
776 NULL,
777 };
778
779 static const char* const user_env_generator_paths[] = {
780 "/run/systemd/user-environment-generators",
781 "/etc/systemd/user-environment-generators",
782 "/usr/local/lib/systemd/user-environment-generators",
783 USER_ENV_GENERATOR_DIR,
784 NULL,
785 };
786
787 char** generator_binary_paths_internal(RuntimeScope scope, bool env_generator) {
788 static const struct {
789 const char *env_name;
790 const char * const *paths[_RUNTIME_SCOPE_MAX];
791 } unit_generator = {
792 "SYSTEMD_GENERATOR_PATH",
793 {
794 [RUNTIME_SCOPE_SYSTEM] = system_generator_paths,
795 [RUNTIME_SCOPE_USER] = user_generator_paths,
796 }
797 }, environment_generator = {
798 "SYSTEMD_ENVIRONMENT_GENERATOR_PATH",
799 {
800 [RUNTIME_SCOPE_SYSTEM] = system_env_generator_paths,
801 [RUNTIME_SCOPE_USER] = user_env_generator_paths,
802 }
803 };
804
805 _cleanup_strv_free_ char **paths = NULL;
806 int r;
807
808 assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER));
809
810 const char *env_name = ASSERT_PTR((env_generator ? environment_generator : unit_generator).env_name);
811 const char * const *generator_paths = ASSERT_PTR((env_generator ? environment_generator : unit_generator).paths[scope]);
812
813 /* First priority is whatever has been passed to us via env vars */
814 r = get_paths_from_environ(env_name, &paths);
815 if (r < 0)
816 return NULL;
817
818 if (!paths || r > 0) {
819 r = strv_extend_strv(&paths, (char* const*) generator_paths, /* filter_duplicates= */ true);
820 if (r < 0)
821 return NULL;
822 }
823
824 return TAKE_PTR(paths);
825 }