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