]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/path-lookup.c
path-lookup: rename lookup_paths_free -> _done
[thirdparty/systemd.git] / src / basic / path-lookup.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include "alloc-util.h"
8 #include "fs-util.h"
9 #include "log.h"
10 #include "macro.h"
11 #include "nulstr-util.h"
12 #include "path-lookup.h"
13 #include "path-util.h"
14 #include "stat-util.h"
15 #include "string-util.h"
16 #include "strv.h"
17 #include "tmpfile-util.h"
18 #include "user-util.h"
19
20 int xdg_user_runtime_dir(char **ret, const char *suffix) {
21 const char *e;
22 char *j;
23
24 assert(ret);
25 assert(suffix);
26
27 e = getenv("XDG_RUNTIME_DIR");
28 if (!e)
29 return -ENXIO;
30
31 j = path_join(e, suffix);
32 if (!j)
33 return -ENOMEM;
34
35 *ret = j;
36 return 0;
37 }
38
39 int xdg_user_config_dir(char **ret, const char *suffix) {
40 _cleanup_free_ char *j = NULL;
41 const char *e;
42 int r;
43
44 assert(ret);
45
46 e = getenv("XDG_CONFIG_HOME");
47 if (e) {
48 j = path_join(e, suffix);
49 if (!j)
50 return -ENOMEM;
51 } else {
52 r = get_home_dir(&j);
53 if (r < 0)
54 return r;
55
56 if (!path_extend(&j, "/.config", suffix))
57 return -ENOMEM;
58 }
59
60 *ret = TAKE_PTR(j);
61 return 0;
62 }
63
64 int xdg_user_data_dir(char **ret, const char *suffix) {
65 _cleanup_free_ char *j = NULL;
66 const char *e;
67 int r;
68
69 assert(ret);
70 assert(suffix);
71
72 /* We don't treat /etc/xdg/systemd here as the spec
73 * suggests because we assume that is a link to
74 * /etc/systemd/ anyway. */
75
76 e = getenv("XDG_DATA_HOME");
77 if (e) {
78 j = path_join(e, suffix);
79 if (!j)
80 return -ENOMEM;
81 } else {
82 r = get_home_dir(&j);
83 if (r < 0)
84 return r;
85
86 if (!path_extend(&j, "/.local/share", suffix))
87 return -ENOMEM;
88 }
89
90 *ret = TAKE_PTR(j);
91 return 1;
92 }
93
94 int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) {
95 _cleanup_free_ char *d = NULL;
96 int r;
97
98 assert(ret);
99 assert(suffix);
100 assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
101
102 /* Accept $RUNTIME_DIRECTORY as authoritative
103 * If its missing apply the suffix to /run or $XDG_RUNTIME_DIR
104 * if we are in a user runtime scope.
105 *
106 * Return value indicates whether the suffix was applied or not */
107
108 const char *e = secure_getenv("RUNTIME_DIRECTORY");
109 if (e) {
110 d = strdup(e);
111 if (!d)
112 return -ENOMEM;
113
114 *ret = TAKE_PTR(d);
115 return false;
116 }
117
118 if (scope == RUNTIME_SCOPE_USER) {
119 r = xdg_user_runtime_dir(&d, suffix);
120 if (r < 0)
121 return r;
122 } else {
123 d = path_join("/run", suffix);
124 if (!d)
125 return -ENOMEM;
126 }
127
128 *ret = TAKE_PTR(d);
129 return true;
130 }
131
132 static const char* const user_data_unit_paths[] = {
133 "/usr/local/lib/systemd/user",
134 "/usr/local/share/systemd/user",
135 USER_DATA_UNIT_DIR,
136 "/usr/lib/systemd/user",
137 "/usr/share/systemd/user",
138 NULL
139 };
140
141 static const char* const user_config_unit_paths[] = {
142 USER_CONFIG_UNIT_DIR,
143 "/etc/systemd/user",
144 NULL
145 };
146
147 int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs) {
148 /* Implement the mechanisms defined in
149 *
150 * https://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
151 *
152 * We look in both the config and the data dirs because we
153 * want to encourage that distributors ship their unit files
154 * as data, and allow overriding as configuration.
155 */
156 const char *e;
157 _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
158
159 e = getenv("XDG_CONFIG_DIRS");
160 if (e)
161 config_dirs = strv_split(e, ":");
162 else
163 config_dirs = strv_new("/etc/xdg");
164 if (!config_dirs)
165 return -ENOMEM;
166
167 e = getenv("XDG_DATA_DIRS");
168 if (e)
169 data_dirs = strv_split(e, ":");
170 else
171 data_dirs = strv_new("/usr/local/share",
172 "/usr/share");
173 if (!data_dirs)
174 return -ENOMEM;
175
176 *ret_config_dirs = TAKE_PTR(config_dirs);
177 *ret_data_dirs = TAKE_PTR(data_dirs);
178
179 return 0;
180 }
181
182 static char** user_dirs(
183 const char *persistent_config,
184 const char *runtime_config,
185 const char *global_persistent_config,
186 const char *global_runtime_config,
187 const char *generator,
188 const char *generator_early,
189 const char *generator_late,
190 const char *transient,
191 const char *persistent_control,
192 const char *runtime_control) {
193
194 _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
195 _cleanup_free_ char *data_home = NULL;
196 _cleanup_strv_free_ char **res = NULL;
197 int r;
198
199 r = xdg_user_dirs(&config_dirs, &data_dirs);
200 if (r < 0)
201 return NULL;
202
203 r = xdg_user_data_dir(&data_home, "/systemd/user");
204 if (r < 0 && r != -ENXIO)
205 return NULL;
206
207 /* Now merge everything we found. */
208 if (strv_extend_many(
209 &res,
210 persistent_control,
211 runtime_control,
212 transient,
213 generator_early,
214 persistent_config) < 0)
215 return NULL;
216
217 if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
218 return NULL;
219
220 /* global config has lower priority than the user config of the same type */
221 if (strv_extend(&res, global_persistent_config) < 0)
222 return NULL;
223
224 if (strv_extend_strv(&res, (char**) user_config_unit_paths, false) < 0)
225 return NULL;
226
227 if (strv_extend_many(
228 &res,
229 runtime_config,
230 global_runtime_config,
231 generator,
232 data_home) < 0)
233 return NULL;
234
235 if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
236 return NULL;
237
238 if (strv_extend_strv(&res, (char**) user_data_unit_paths, false) < 0)
239 return NULL;
240
241 if (strv_extend(&res, generator_late) < 0)
242 return NULL;
243
244 if (path_strv_make_absolute_cwd(res) < 0)
245 return NULL;
246
247 return TAKE_PTR(res);
248 }
249
250 bool path_is_user_data_dir(const char *path) {
251 assert(path);
252
253 return strv_contains((char**) user_data_unit_paths, path);
254 }
255
256 bool path_is_user_config_dir(const char *path) {
257 assert(path);
258
259 return strv_contains((char**) user_config_unit_paths, path);
260 }
261
262 static int acquire_generator_dirs(
263 RuntimeScope scope,
264 const char *tempdir,
265 char **generator,
266 char **generator_early,
267 char **generator_late) {
268
269 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *p = NULL;
270 const char *prefix;
271
272 assert(generator);
273 assert(generator_early);
274 assert(generator_late);
275 assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
276
277 if (scope == RUNTIME_SCOPE_GLOBAL)
278 return -EOPNOTSUPP;
279
280 if (tempdir)
281 prefix = tempdir;
282 else if (scope == RUNTIME_SCOPE_SYSTEM)
283 prefix = "/run/systemd";
284 else {
285 /* RUNTIME_SCOPE_USER */
286 const char *e;
287
288 e = getenv("XDG_RUNTIME_DIR");
289 if (!e)
290 return -ENXIO;
291
292 p = path_join(e, "/systemd");
293 if (!p)
294 return -ENOMEM;
295
296 prefix = p;
297 }
298
299 x = path_join(prefix, "generator");
300 if (!x)
301 return -ENOMEM;
302
303 y = path_join(prefix, "generator.early");
304 if (!y)
305 return -ENOMEM;
306
307 z = path_join(prefix, "generator.late");
308 if (!z)
309 return -ENOMEM;
310
311 *generator = TAKE_PTR(x);
312 *generator_early = TAKE_PTR(y);
313 *generator_late = TAKE_PTR(z);
314
315 return 0;
316 }
317
318 static int acquire_transient_dir(
319 RuntimeScope scope,
320 const char *tempdir,
321 char **ret) {
322
323 char *transient;
324
325 assert(ret);
326 assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
327
328 if (scope == RUNTIME_SCOPE_GLOBAL)
329 return -EOPNOTSUPP;
330
331 if (tempdir)
332 transient = path_join(tempdir, "transient");
333 else if (scope == RUNTIME_SCOPE_SYSTEM)
334 transient = strdup("/run/systemd/transient");
335 else
336 return xdg_user_runtime_dir(ret, "/systemd/transient");
337
338 if (!transient)
339 return -ENOMEM;
340 *ret = transient;
341 return 0;
342 }
343
344 static int acquire_config_dirs(RuntimeScope scope, char **persistent, char **runtime) {
345 _cleanup_free_ char *a = NULL, *b = NULL;
346 int r;
347
348 assert(persistent);
349 assert(runtime);
350
351 switch (scope) {
352
353 case RUNTIME_SCOPE_SYSTEM:
354 a = strdup(SYSTEM_CONFIG_UNIT_DIR);
355 b = strdup("/run/systemd/system");
356 break;
357
358 case RUNTIME_SCOPE_GLOBAL:
359 a = strdup(USER_CONFIG_UNIT_DIR);
360 b = strdup("/run/systemd/user");
361 break;
362
363 case RUNTIME_SCOPE_USER:
364 r = xdg_user_config_dir(&a, "/systemd/user");
365 if (r < 0 && r != -ENXIO)
366 return r;
367
368 r = xdg_user_runtime_dir(runtime, "/systemd/user");
369 if (r < 0) {
370 if (r != -ENXIO)
371 return r;
372
373 /* If XDG_RUNTIME_DIR is not set, don't consider that fatal, simply initialize the runtime
374 * directory to NULL */
375 *runtime = NULL;
376 }
377
378 *persistent = TAKE_PTR(a);
379
380 return 0;
381
382 default:
383 assert_not_reached();
384 }
385
386 if (!a || !b)
387 return -ENOMEM;
388
389 *persistent = TAKE_PTR(a);
390 *runtime = TAKE_PTR(b);
391
392 return 0;
393 }
394
395 static int acquire_control_dirs(RuntimeScope scope, char **persistent, char **runtime) {
396 _cleanup_free_ char *a = NULL;
397 int r;
398
399 assert(persistent);
400 assert(runtime);
401
402 switch (scope) {
403
404 case RUNTIME_SCOPE_SYSTEM: {
405 _cleanup_free_ char *b = NULL;
406
407 a = strdup("/etc/systemd/system.control");
408 if (!a)
409 return -ENOMEM;
410
411 b = strdup("/run/systemd/system.control");
412 if (!b)
413 return -ENOMEM;
414
415 *runtime = TAKE_PTR(b);
416
417 break;
418 }
419
420 case RUNTIME_SCOPE_USER:
421 r = xdg_user_config_dir(&a, "/systemd/user.control");
422 if (r < 0 && r != -ENXIO)
423 return r;
424
425 r = xdg_user_runtime_dir(runtime, "/systemd/user.control");
426 if (r < 0) {
427 if (r != -ENXIO)
428 return r;
429
430 /* If XDG_RUNTIME_DIR is not set, don't consider this fatal, simply initialize the directory to
431 * NULL */
432 *runtime = NULL;
433 }
434
435 break;
436
437 case RUNTIME_SCOPE_GLOBAL:
438 return -EOPNOTSUPP;
439
440 default:
441 assert_not_reached();
442 }
443
444 *persistent = TAKE_PTR(a);
445
446 return 0;
447 }
448
449 static int acquire_attached_dirs(
450 RuntimeScope scope,
451 char **ret_persistent,
452 char **ret_runtime) {
453
454 _cleanup_free_ char *a = NULL, *b = NULL;
455
456 assert(ret_persistent);
457 assert(ret_runtime);
458
459 /* Portable services are not available to regular users for now. */
460 if (scope != RUNTIME_SCOPE_SYSTEM)
461 return -EOPNOTSUPP;
462
463 a = strdup("/etc/systemd/system.attached");
464 if (!a)
465 return -ENOMEM;
466
467 b = strdup("/run/systemd/system.attached");
468 if (!b)
469 return -ENOMEM;
470
471 *ret_persistent = TAKE_PTR(a);
472 *ret_runtime = TAKE_PTR(b);
473
474 return 0;
475 }
476
477 static int patch_root_prefix(char **p, const char *root_dir) {
478 char *c;
479
480 assert(p);
481
482 if (!*p)
483 return 0;
484
485 c = path_join(root_dir, *p);
486 if (!c)
487 return -ENOMEM;
488
489 free_and_replace(*p, c);
490 return 0;
491 }
492
493 static int patch_root_prefix_strv(char **l, const char *root_dir) {
494 int r;
495
496 if (!root_dir)
497 return 0;
498
499 STRV_FOREACH(i, l) {
500 r = patch_root_prefix(i, root_dir);
501 if (r < 0)
502 return r;
503 }
504
505 return 0;
506 }
507
508 static int get_paths_from_environ(const char *var, char ***paths, bool *append) {
509 const char *e;
510 int r;
511
512 assert(var);
513 assert(paths);
514 assert(append);
515
516 *append = false;
517
518 e = getenv(var);
519 if (e) {
520 const char *k;
521
522 k = endswith(e, ":");
523 if (k) {
524 e = strndupa_safe(e, k - e);
525 *append = true;
526 }
527
528 /* FIXME: empty components in other places should be rejected. */
529
530 r = path_split_and_make_absolute(e, paths);
531 if (r < 0)
532 return r;
533 }
534
535 return 0;
536 }
537
538 int lookup_paths_init(
539 LookupPaths *lp,
540 RuntimeScope scope,
541 LookupPathsFlags flags,
542 const char *root_dir) {
543
544 _cleanup_(rmdir_and_freep) char *tempdir = NULL;
545 _cleanup_free_ char
546 *root = NULL,
547 *persistent_config = NULL, *runtime_config = NULL,
548 *global_persistent_config = NULL, *global_runtime_config = NULL,
549 *generator = NULL, *generator_early = NULL, *generator_late = NULL,
550 *transient = NULL,
551 *persistent_control = NULL, *runtime_control = NULL,
552 *persistent_attached = NULL, *runtime_attached = NULL;
553 bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */
554 _cleanup_strv_free_ char **paths = NULL;
555 int r;
556
557 assert(lp);
558 assert(scope >= 0);
559 assert(scope < _RUNTIME_SCOPE_MAX);
560
561 if (!empty_or_root(root_dir)) {
562 if (scope == RUNTIME_SCOPE_USER)
563 return -EINVAL;
564
565 r = is_dir(root_dir, true);
566 if (r < 0)
567 return r;
568 if (r == 0)
569 return -ENOTDIR;
570
571 root = strdup(root_dir);
572 if (!root)
573 return -ENOMEM;
574 }
575
576 if (flags & LOOKUP_PATHS_TEMPORARY_GENERATED) {
577 r = mkdtemp_malloc("/tmp/systemd-temporary-XXXXXX", &tempdir);
578 if (r < 0)
579 return log_debug_errno(r, "Failed to create temporary directory: %m");
580 }
581
582 /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_config to NULL */
583 r = acquire_config_dirs(scope, &persistent_config, &runtime_config);
584 if (r < 0)
585 return r;
586
587 if (scope == RUNTIME_SCOPE_USER) {
588 r = acquire_config_dirs(RUNTIME_SCOPE_GLOBAL, &global_persistent_config, &global_runtime_config);
589 if (r < 0)
590 return r;
591 }
592
593 if ((flags & LOOKUP_PATHS_EXCLUDE_GENERATED) == 0) {
594 /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
595 r = acquire_generator_dirs(scope, tempdir,
596 &generator, &generator_early, &generator_late);
597 if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
598 return r;
599 }
600
601 /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
602 r = acquire_transient_dir(scope, tempdir, &transient);
603 if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
604 return r;
605
606 /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_control to NULL */
607 r = acquire_control_dirs(scope, &persistent_control, &runtime_control);
608 if (r < 0 && r != -EOPNOTSUPP)
609 return r;
610
611 r = acquire_attached_dirs(scope, &persistent_attached, &runtime_attached);
612 if (r < 0 && r != -EOPNOTSUPP)
613 return r;
614
615 /* First priority is whatever has been passed to us via env vars */
616 r = get_paths_from_environ("SYSTEMD_UNIT_PATH", &paths, &append);
617 if (r < 0)
618 return r;
619
620 if (!paths || append) {
621 /* Let's figure something out. */
622
623 _cleanup_strv_free_ char **add = NULL;
624
625 /* For the user units we include share/ in the search
626 * path in order to comply with the XDG basedir spec.
627 * For the system stuff we avoid such nonsense. OTOH
628 * we include /lib in the search path for the system
629 * stuff but avoid it for user stuff. */
630
631 switch (scope) {
632
633 case RUNTIME_SCOPE_SYSTEM:
634 add = strv_new(
635 /* If you modify this you also want to modify
636 * systemdsystemunitpath= in systemd.pc.in! */
637 STRV_IFNOTNULL(persistent_control),
638 STRV_IFNOTNULL(runtime_control),
639 STRV_IFNOTNULL(transient),
640 STRV_IFNOTNULL(generator_early),
641 persistent_config,
642 SYSTEM_CONFIG_UNIT_DIR,
643 "/etc/systemd/system",
644 STRV_IFNOTNULL(persistent_attached),
645 runtime_config,
646 "/run/systemd/system",
647 STRV_IFNOTNULL(runtime_attached),
648 STRV_IFNOTNULL(generator),
649 "/usr/local/lib/systemd/system",
650 SYSTEM_DATA_UNIT_DIR,
651 "/usr/lib/systemd/system",
652 /* To be used ONLY for images which might be legacy split-usr */
653 STRV_IFNOTNULL(flags & LOOKUP_PATHS_SPLIT_USR ? "/lib/systemd/system" : NULL),
654 STRV_IFNOTNULL(generator_late));
655 break;
656
657 case RUNTIME_SCOPE_GLOBAL:
658 add = strv_new(
659 /* If you modify this you also want to modify
660 * systemduserunitpath= in systemd.pc.in, and
661 * the arrays in user_dirs() above! */
662 STRV_IFNOTNULL(persistent_control),
663 STRV_IFNOTNULL(runtime_control),
664 STRV_IFNOTNULL(transient),
665 STRV_IFNOTNULL(generator_early),
666 persistent_config,
667 USER_CONFIG_UNIT_DIR,
668 "/etc/systemd/user",
669 runtime_config,
670 "/run/systemd/user",
671 STRV_IFNOTNULL(generator),
672 "/usr/local/share/systemd/user",
673 "/usr/share/systemd/user",
674 "/usr/local/lib/systemd/user",
675 USER_DATA_UNIT_DIR,
676 "/usr/lib/systemd/user",
677 STRV_IFNOTNULL(generator_late));
678 break;
679
680 case RUNTIME_SCOPE_USER:
681 add = user_dirs(persistent_config, runtime_config,
682 global_persistent_config, global_runtime_config,
683 generator, generator_early, generator_late,
684 transient,
685 persistent_control, runtime_control);
686 break;
687
688 default:
689 assert_not_reached();
690 }
691
692 if (!add)
693 return -ENOMEM;
694
695 if (paths) {
696 r = strv_extend_strv(&paths, add, true);
697 if (r < 0)
698 return r;
699 } else
700 /* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it,
701 * and don't have to copy anything */
702 paths = TAKE_PTR(add);
703 }
704
705 r = patch_root_prefix(&persistent_config, root);
706 if (r < 0)
707 return r;
708 r = patch_root_prefix(&runtime_config, root);
709 if (r < 0)
710 return r;
711
712 r = patch_root_prefix(&generator, root);
713 if (r < 0)
714 return r;
715 r = patch_root_prefix(&generator_early, root);
716 if (r < 0)
717 return r;
718 r = patch_root_prefix(&generator_late, root);
719 if (r < 0)
720 return r;
721
722 r = patch_root_prefix(&transient, root);
723 if (r < 0)
724 return r;
725
726 r = patch_root_prefix(&persistent_control, root);
727 if (r < 0)
728 return r;
729 r = patch_root_prefix(&runtime_control, root);
730 if (r < 0)
731 return r;
732
733 r = patch_root_prefix(&persistent_attached, root);
734 if (r < 0)
735 return r;
736 r = patch_root_prefix(&runtime_attached, root);
737 if (r < 0)
738 return r;
739
740 r = patch_root_prefix_strv(paths, root);
741 if (r < 0)
742 return -ENOMEM;
743
744 *lp = (LookupPaths) {
745 .search_path = strv_uniq(TAKE_PTR(paths)),
746
747 .persistent_config = TAKE_PTR(persistent_config),
748 .runtime_config = TAKE_PTR(runtime_config),
749
750 .generator = TAKE_PTR(generator),
751 .generator_early = TAKE_PTR(generator_early),
752 .generator_late = TAKE_PTR(generator_late),
753
754 .transient = TAKE_PTR(transient),
755
756 .persistent_control = TAKE_PTR(persistent_control),
757 .runtime_control = TAKE_PTR(runtime_control),
758
759 .persistent_attached = TAKE_PTR(persistent_attached),
760 .runtime_attached = TAKE_PTR(runtime_attached),
761
762 .root_dir = TAKE_PTR(root),
763 .temporary_dir = TAKE_PTR(tempdir),
764 };
765
766 return 0;
767 }
768
769 int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir) {
770 int r;
771
772 r = lookup_paths_init(lp, scope, flags, root_dir);
773 if (r < 0)
774 return log_error_errno(r, "Failed to initialize unit search paths%s%s: %m",
775 isempty(root_dir) ? "" : " for root directory ", strempty(root_dir));
776 return r;
777 }
778
779 void lookup_paths_done(LookupPaths *lp) {
780 assert(lp);
781
782 lp->search_path = strv_free(lp->search_path);
783
784 lp->persistent_config = mfree(lp->persistent_config);
785 lp->runtime_config = mfree(lp->runtime_config);
786
787 lp->persistent_attached = mfree(lp->persistent_attached);
788 lp->runtime_attached = mfree(lp->runtime_attached);
789
790 lp->generator = mfree(lp->generator);
791 lp->generator_early = mfree(lp->generator_early);
792 lp->generator_late = mfree(lp->generator_late);
793
794 lp->transient = mfree(lp->transient);
795
796 lp->persistent_control = mfree(lp->persistent_control);
797 lp->runtime_control = mfree(lp->runtime_control);
798
799 lp->root_dir = mfree(lp->root_dir);
800 lp->temporary_dir = mfree(lp->temporary_dir);
801 }
802
803 void lookup_paths_log(LookupPaths *lp) {
804 assert(lp);
805
806 if (strv_isempty(lp->search_path)) {
807 log_debug("Ignoring unit files.");
808 lp->search_path = strv_free(lp->search_path);
809 } else {
810 _cleanup_free_ char *t = NULL;
811
812 t = strv_join(lp->search_path, "\n\t");
813 log_debug("Looking for unit files in (higher priority first):\n\t%s", strna(t));
814 }
815 }
816
817 char **generator_binary_paths(RuntimeScope scope) {
818 bool append = false; /* Add items from SYSTEMD_GENERATOR_PATH before normal directories */
819 _cleanup_strv_free_ char **paths = NULL;
820 int r;
821
822 /* First priority is whatever has been passed to us via env vars */
823 r = get_paths_from_environ("SYSTEMD_GENERATOR_PATH", &paths, &append);
824 if (r < 0)
825 return NULL;
826
827 if (!paths || append) {
828 _cleanup_strv_free_ char **add = NULL;
829
830 switch (scope) {
831
832 case RUNTIME_SCOPE_SYSTEM:
833 add = strv_new("/run/systemd/system-generators",
834 "/etc/systemd/system-generators",
835 "/usr/local/lib/systemd/system-generators",
836 SYSTEM_GENERATOR_DIR);
837 break;
838
839 case RUNTIME_SCOPE_GLOBAL:
840 case RUNTIME_SCOPE_USER:
841 add = strv_new("/run/systemd/user-generators",
842 "/etc/systemd/user-generators",
843 "/usr/local/lib/systemd/user-generators",
844 USER_GENERATOR_DIR);
845 break;
846
847 default:
848 assert_not_reached();
849 }
850 if (!add)
851 return NULL;
852
853 if (paths) {
854 r = strv_extend_strv(&paths, add, true);
855 if (r < 0)
856 return NULL;
857 } else
858 /* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it,
859 * and don't have to copy anything */
860 paths = TAKE_PTR(add);
861 }
862
863 return TAKE_PTR(paths);
864 }
865
866 char **env_generator_binary_paths(RuntimeScope runtime_scope) {
867 _cleanup_strv_free_ char **paths = NULL, **add = NULL;
868 bool append = false; /* Add items from SYSTEMD_ENVIRONMENT_GENERATOR_PATH before normal directories */
869 int r;
870
871 /* First priority is whatever has been passed to us via env vars */
872 r = get_paths_from_environ("SYSTEMD_ENVIRONMENT_GENERATOR_PATH", &paths, &append);
873 if (r < 0)
874 return NULL;
875
876 if (!paths || append) {
877 switch (runtime_scope) {
878
879 case RUNTIME_SCOPE_SYSTEM:
880 add = strv_new("/run/systemd/system-environment-generators",
881 "/etc/systemd/system-environment-generators",
882 "/usr/local/lib/systemd/system-environment-generators",
883 SYSTEM_ENV_GENERATOR_DIR);
884 break;
885
886 case RUNTIME_SCOPE_USER:
887 add = strv_new("/run/systemd/user-environment-generators",
888 "/etc/systemd/user-environment-generators",
889 "/usr/local/lib/systemd/user-environment-generators",
890 USER_ENV_GENERATOR_DIR);
891 break;
892
893 default:
894 assert_not_reached();
895 }
896 if (!add)
897 return NULL;
898 }
899
900 if (paths) {
901 r = strv_extend_strv(&paths, add, true);
902 if (r < 0)
903 return NULL;
904 } else
905 /* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it,
906 * and don't have to copy anything */
907 paths = TAKE_PTR(add);
908
909 return TAKE_PTR(paths);
910 }
911
912 int find_portable_profile(const char *name, const char *unit, char **ret_path) {
913 const char *dot;
914
915 assert(name);
916 assert(ret_path);
917
918 assert_se(dot = strrchr(unit, '.'));
919
920 NULSTR_FOREACH(p, PORTABLE_PROFILE_DIRS) {
921 _cleanup_free_ char *joined = NULL;
922
923 joined = strjoin(p, "/", name, "/", dot + 1, ".conf");
924 if (!joined)
925 return -ENOMEM;
926
927 if (laccess(joined, F_OK) >= 0) {
928 *ret_path = TAKE_PTR(joined);
929 return 0;
930 }
931
932 if (errno != ENOENT)
933 return -errno;
934 }
935
936 return -ENOENT;
937 }