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