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