]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/path-lookup.c
Merge pull request #22791 from keszybz/bootctl-invert-order
[thirdparty/systemd.git] / src / basic / path-lookup.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
84e3543e 2
cf0fbc49 3#include <errno.h>
84e3543e 4#include <stdio.h>
cf0fbc49 5#include <stdlib.h>
84e3543e 6
b5efdb8a 7#include "alloc-util.h"
a1f31f47 8#include "fs-util.h"
a8fbdf54
TA
9#include "log.h"
10#include "macro.h"
13c02e7b 11#include "nulstr-util.h"
07630cea 12#include "path-lookup.h"
cf0fbc49 13#include "path-util.h"
e4bb56c7 14#include "stat-util.h"
cf0fbc49
TA
15#include "string-util.h"
16#include "strv.h"
e4de7287 17#include "tmpfile-util.h"
c4997074 18#include "user-util.h"
84e3543e 19
ca4adeb7 20int xdg_user_runtime_dir(char **ret, const char *suffix) {
10e87ee7 21 const char *e;
205dd21e
LP
22 char *j;
23
24 assert(ret);
80b1ae32 25 assert(suffix);
10e87ee7 26
80b1ae32
LP
27 e = getenv("XDG_RUNTIME_DIR");
28 if (!e)
29 return -ENXIO;
205dd21e 30
df778749 31 j = path_join(e, suffix);
80b1ae32
LP
32 if (!j)
33 return -ENOMEM;
10e87ee7 34
205dd21e 35 *ret = j;
10e87ee7
LP
36 return 0;
37}
38
ca4adeb7 39int xdg_user_config_dir(char **ret, const char *suffix) {
4ede9802 40 _cleanup_free_ char *j = NULL;
718880ba 41 const char *e;
c4997074 42 int r;
205dd21e
LP
43
44 assert(ret);
718880ba 45
80b1ae32 46 e = getenv("XDG_CONFIG_HOME");
4ede9802 47 if (e) {
df778749 48 j = path_join(e, suffix);
4ede9802
LP
49 if (!j)
50 return -ENOMEM;
51 } else {
52 r = get_home_dir(&j);
c4997074
LP
53 if (r < 0)
54 return r;
80b1ae32 55
4ede9802
LP
56 if (!path_extend(&j, "/.config", suffix))
57 return -ENOMEM;
80b1ae32 58 }
718880ba 59
4ede9802 60 *ret = TAKE_PTR(j);
718880ba
SA
61 return 0;
62}
63
ca4adeb7 64int xdg_user_data_dir(char **ret, const char *suffix) {
4ede9802 65 _cleanup_free_ char *j = NULL;
e801700e 66 const char *e;
c4997074 67 int r;
205dd21e
LP
68
69 assert(ret);
a7527131 70 assert(suffix);
e801700e
ZJS
71
72 /* We don't treat /etc/xdg/systemd here as the spec
61233823 73 * suggests because we assume that is a link to
e801700e
ZJS
74 * /etc/systemd/ anyway. */
75
76 e = getenv("XDG_DATA_HOME");
4ede9802 77 if (e) {
df778749 78 j = path_join(e, suffix);
4ede9802
LP
79 if (!j)
80 return -ENOMEM;
81 } else {
82 r = get_home_dir(&j);
c4997074
LP
83 if (r < 0)
84 return r;
205dd21e 85
4ede9802
LP
86 if (!path_extend(&j, "/.local/share", suffix))
87 return -ENOMEM;
e801700e 88 }
e801700e 89
4ede9802 90 *ret = TAKE_PTR(j);
463d0d15 91 return 1;
e801700e
ZJS
92}
93
d2561cfd
ZJS
94static const char* const user_data_unit_paths[] = {
95 "/usr/local/lib/systemd/user",
96 "/usr/local/share/systemd/user",
96d33e4a 97 USER_DATA_UNIT_DIR,
d2561cfd
ZJS
98 "/usr/lib/systemd/user",
99 "/usr/share/systemd/user",
100 NULL
101};
102
103static const char* const user_config_unit_paths[] = {
96d33e4a 104 USER_CONFIG_UNIT_DIR,
d2561cfd
ZJS
105 "/etc/systemd/user",
106 NULL
107};
108
ca4adeb7 109int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs) {
84e3543e
LP
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 */
ca4adeb7
ZJS
118 const char *e;
119 _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
84e3543e 120
07719a21 121 e = getenv("XDG_CONFIG_DIRS");
f0d0698f 122 if (e)
07719a21 123 config_dirs = strv_split(e, ":");
f0d0698f
DE
124 else
125 config_dirs = strv_new("/etc/xdg");
126 if (!config_dirs)
127 return -ENOMEM;
84e3543e 128
07719a21
LP
129 e = getenv("XDG_DATA_DIRS");
130 if (e)
84e3543e
LP
131 data_dirs = strv_split(e, ":");
132 else
ef3102bf 133 data_dirs = strv_new("/usr/local/share",
bea1a013 134 "/usr/share");
84e3543e 135 if (!data_dirs)
ca4adeb7
ZJS
136 return -ENOMEM;
137
61c22b0b
LP
138 *ret_config_dirs = TAKE_PTR(config_dirs);
139 *ret_data_dirs = TAKE_PTR(data_dirs);
140
ca4adeb7
ZJS
141 return 0;
142}
143
144static char** user_dirs(
145 const char *persistent_config,
146 const char *runtime_config,
7b6344d3
ZJS
147 const char *global_persistent_config,
148 const char *global_runtime_config,
ca4adeb7
ZJS
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;
ca4adeb7
ZJS
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)
e801700e 167 return NULL;
84e3543e
LP
168
169 /* Now merge everything we found. */
80b1ae32
LP
170 if (strv_extend(&res, persistent_control) < 0)
171 return NULL;
39591351 172
80b1ae32
LP
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;
07719a21 181
e3820eea 182 if (strv_extend(&res, persistent_config) < 0)
b9418b05 183 return NULL;
718880ba 184
e3820eea 185 if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
a0f84a10
LP
186 return NULL;
187
7b6344d3
ZJS
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
d2561cfd 192 if (strv_extend_strv(&res, (char**) user_config_unit_paths, false) < 0)
e801700e 193 return NULL;
718880ba 194
a0f84a10 195 if (strv_extend(&res, runtime_config) < 0)
e801700e 196 return NULL;
84e3543e 197
7b6344d3
ZJS
198 if (strv_extend(&res, global_runtime_config) < 0)
199 return NULL;
200
80b1ae32
LP
201 if (strv_extend(&res, generator) < 0)
202 return NULL;
07719a21 203
80b1ae32
LP
204 if (strv_extend(&res, data_home) < 0)
205 return NULL;
84e3543e 206
b9418b05
LP
207 if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
208 return NULL;
84e3543e 209
d2561cfd 210 if (strv_extend_strv(&res, (char**) user_data_unit_paths, false) < 0)
e801700e 211 return NULL;
84e3543e 212
80b1ae32
LP
213 if (strv_extend(&res, generator_late) < 0)
214 return NULL;
07719a21 215
0f474365 216 if (path_strv_make_absolute_cwd(res) < 0)
e801700e 217 return NULL;
84e3543e 218
ae2a15bc 219 return TAKE_PTR(res);
e801700e 220}
84e3543e 221
d2561cfd
ZJS
222bool path_is_user_data_dir(const char *path) {
223 assert(path);
224
225 return strv_contains((char**) user_data_unit_paths, path);
226}
227
228bool path_is_user_config_dir(const char *path) {
229 assert(path);
230
231 return strv_contains((char**) user_config_unit_paths, path);
232}
233
a3c4eb07 234static int acquire_generator_dirs(
463d0d15 235 UnitFileScope scope,
a1f31f47 236 const char *tempdir,
a3c4eb07
LP
237 char **generator,
238 char **generator_early,
239 char **generator_late) {
240
1d3b68f6 241 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *p = NULL;
a3c4eb07
LP
242 const char *prefix;
243
244 assert(generator);
245 assert(generator_early);
246 assert(generator_late);
a1f31f47 247 assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER, UNIT_FILE_GLOBAL));
a3c4eb07 248
a1f31f47
ZJS
249 if (scope == UNIT_FILE_GLOBAL)
250 return -EOPNOTSUPP;
463d0d15 251
a1f31f47
ZJS
252 if (tempdir)
253 prefix = tempdir;
a1f31f47
ZJS
254 else if (scope == UNIT_FILE_SYSTEM)
255 prefix = "/run/systemd";
3c47d509
ZJS
256 else {
257 /* UNIT_FILE_USER */
463d0d15 258 const char *e;
a3c4eb07
LP
259
260 e = getenv("XDG_RUNTIME_DIR");
261 if (!e)
463d0d15 262 return -ENXIO;
a3c4eb07 263
1d3b68f6
AZ
264 p = path_join(e, "/systemd");
265 if (!p)
266 return -ENOMEM;
267
268 prefix = p;
a3c4eb07
LP
269 }
270
b910cc72 271 x = path_join(prefix, "generator");
a3c4eb07
LP
272 if (!x)
273 return -ENOMEM;
274
b910cc72 275 y = path_join(prefix, "generator.early");
a3c4eb07
LP
276 if (!y)
277 return -ENOMEM;
278
b910cc72 279 z = path_join(prefix, "generator.late");
a3c4eb07
LP
280 if (!z)
281 return -ENOMEM;
282
1cc6c93a
YW
283 *generator = TAKE_PTR(x);
284 *generator_early = TAKE_PTR(y);
285 *generator_late = TAKE_PTR(z);
a3c4eb07 286
a3c4eb07
LP
287 return 0;
288}
289
a1f31f47
ZJS
290static int acquire_transient_dir(
291 UnitFileScope scope,
292 const char *tempdir,
293 char **ret) {
39591351 294
a1f31f47 295 char *transient;
39591351 296
a1f31f47
ZJS
297 assert(ret);
298 assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER, UNIT_FILE_GLOBAL));
39591351 299
a1f31f47
ZJS
300 if (scope == UNIT_FILE_GLOBAL)
301 return -EOPNOTSUPP;
39591351 302
a1f31f47 303 if (tempdir)
657ee2d8 304 transient = path_join(tempdir, "transient");
a1f31f47
ZJS
305 else if (scope == UNIT_FILE_SYSTEM)
306 transient = strdup("/run/systemd/transient");
307 else
ca4adeb7 308 return xdg_user_runtime_dir(ret, "/systemd/transient");
a7527131 309
a1f31f47
ZJS
310 if (!transient)
311 return -ENOMEM;
312 *ret = transient;
313 return 0;
39591351
LP
314}
315
463d0d15 316static int acquire_config_dirs(UnitFileScope scope, char **persistent, char **runtime) {
a0f84a10
LP
317 _cleanup_free_ char *a = NULL, *b = NULL;
318 int r;
319
320 assert(persistent);
321 assert(runtime);
322
463d0d15
LP
323 switch (scope) {
324
325 case UNIT_FILE_SYSTEM:
96d33e4a 326 a = strdup(SYSTEM_CONFIG_UNIT_DIR);
a0f84a10 327 b = strdup("/run/systemd/system");
463d0d15
LP
328 break;
329
330 case UNIT_FILE_GLOBAL:
96d33e4a 331 a = strdup(USER_CONFIG_UNIT_DIR);
463d0d15
LP
332 b = strdup("/run/systemd/user");
333 break;
a0f84a10 334
463d0d15 335 case UNIT_FILE_USER:
ca4adeb7 336 r = xdg_user_config_dir(&a, "/systemd/user");
c3c7eb7d 337 if (r < 0 && r != -ENXIO)
a0f84a10
LP
338 return r;
339
ca4adeb7 340 r = xdg_user_runtime_dir(runtime, "/systemd/user");
c3c7eb7d
LP
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 }
a0f84a10 349
ae2a15bc 350 *persistent = TAKE_PTR(a);
a0f84a10
LP
351
352 return 0;
a0f84a10 353
463d0d15 354 default:
04499a70 355 assert_not_reached();
a0f84a10
LP
356 }
357
358 if (!a || !b)
359 return -ENOMEM;
360
1cc6c93a
YW
361 *persistent = TAKE_PTR(a);
362 *runtime = TAKE_PTR(b);
a0f84a10
LP
363
364 return 0;
365}
366
80b1ae32
LP
367static int acquire_control_dirs(UnitFileScope 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 UNIT_FILE_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
ae2a15bc 387 *runtime = TAKE_PTR(b);
80b1ae32
LP
388
389 break;
390 }
391
392 case UNIT_FILE_USER:
0d6671b2 393 r = xdg_user_config_dir(&a, "/systemd/user.control");
c3c7eb7d 394 if (r < 0 && r != -ENXIO)
80b1ae32
LP
395 return r;
396
0d6671b2 397 r = xdg_user_runtime_dir(runtime, "/systemd/user.control");
c3c7eb7d
LP
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 }
80b1ae32
LP
406
407 break;
408
409 case UNIT_FILE_GLOBAL:
410 return -EOPNOTSUPP;
411
412 default:
04499a70 413 assert_not_reached();
80b1ae32
LP
414 }
415
ae2a15bc 416 *persistent = TAKE_PTR(a);
80b1ae32
LP
417
418 return 0;
419}
420
0cd4628d
LP
421static int acquire_attached_dirs(
422 UnitFileScope 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 != UNIT_FILE_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
a3c4eb07
LP
449static 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
c6134d3e 457 c = path_join(root_dir, *p);
a3c4eb07
LP
458 if (!c)
459 return -ENOMEM;
460
c6134d3e 461 free_and_replace(*p, c);
a3c4eb07
LP
462 return 0;
463}
464
a1453343 465static int patch_root_prefix_strv(char **l, const char *root_dir) {
a1453343
LP
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
2909f4dd
BB
480static 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) {
2f82562b 496 e = strndupa_safe(e, k - e);
2909f4dd
BB
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
07719a21
LP
510int lookup_paths_init(
511 LookupPaths *p,
463d0d15 512 UnitFileScope scope,
4943d143 513 LookupPathsFlags flags,
a3c4eb07 514 const char *root_dir) {
07719a21 515
a1f31f47 516 _cleanup_(rmdir_and_freep) char *tempdir = NULL;
e4bb56c7
LP
517 _cleanup_free_ char
518 *root = NULL,
39591351 519 *persistent_config = NULL, *runtime_config = NULL,
7b6344d3 520 *global_persistent_config = NULL, *global_runtime_config = NULL,
e4bb56c7 521 *generator = NULL, *generator_early = NULL, *generator_late = NULL,
80b1ae32 522 *transient = NULL,
0cd4628d
LP
523 *persistent_control = NULL, *runtime_control = NULL,
524 *persistent_attached = NULL, *runtime_attached = NULL;
cf7d80a5 525 bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */
e215d211 526 _cleanup_strv_free_ char **paths = NULL;
0f474365 527 int r;
84e3543e
LP
528
529 assert(p);
463d0d15
LP
530 assert(scope >= 0);
531 assert(scope < _UNIT_FILE_SCOPE_MAX);
a3c4eb07 532
799b2102
LP
533#if HAVE_SPLIT_USR
534 flags |= LOOKUP_PATHS_SPLIT_USR;
535#endif
536
57ea45e1 537 if (!empty_or_root(root_dir)) {
e4bb56c7
LP
538 if (scope == UNIT_FILE_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
a1f31f47
ZJS
552 if (flags & LOOKUP_PATHS_TEMPORARY_GENERATED) {
553 r = mkdtemp_malloc("/tmp/systemd-temporary-XXXXXX", &tempdir);
554 if (r < 0)
fc37d875 555 return log_debug_errno(r, "Failed to create temporary directory: %m");
a1f31f47
ZJS
556 }
557
c3c7eb7d 558 /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_config to NULL */
463d0d15 559 r = acquire_config_dirs(scope, &persistent_config, &runtime_config);
c3c7eb7d 560 if (r < 0)
a0f84a10
LP
561 return r;
562
7b6344d3
ZJS
563 if (scope == UNIT_FILE_USER) {
564 r = acquire_config_dirs(UNIT_FILE_GLOBAL, &global_persistent_config, &global_runtime_config);
565 if (r < 0)
566 return r;
567 }
568
4943d143 569 if ((flags & LOOKUP_PATHS_EXCLUDE_GENERATED) == 0) {
c3c7eb7d 570 /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
a1f31f47
ZJS
571 r = acquire_generator_dirs(scope, tempdir,
572 &generator, &generator_early, &generator_late);
4c701096 573 if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
4943d143
LP
574 return r;
575 }
84e3543e 576
c3c7eb7d 577 /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
a1f31f47 578 r = acquire_transient_dir(scope, tempdir, &transient);
4c701096 579 if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
39591351
LP
580 return r;
581
c3c7eb7d 582 /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_control to NULL */
80b1ae32 583 r = acquire_control_dirs(scope, &persistent_control, &runtime_control);
c3c7eb7d 584 if (r < 0 && r != -EOPNOTSUPP)
80b1ae32
LP
585 return r;
586
0cd4628d
LP
587 r = acquire_attached_dirs(scope, &persistent_attached, &runtime_attached);
588 if (r < 0 && r != -EOPNOTSUPP)
589 return r;
590
cd64fd56 591 /* First priority is whatever has been passed to us via env vars */
2909f4dd
BB
592 r = get_paths_from_environ("SYSTEMD_UNIT_PATH", &paths, &append);
593 if (r < 0)
594 return r;
84e3543e 595
e215d211 596 if (!paths || append) {
cf7d80a5
ZJS
597 /* Let's figure something out. */
598
a3c4eb07 599 _cleanup_strv_free_ char **add = NULL;
84e3543e 600
07719a21 601 /* For the user units we include share/ in the search
cf7d80a5
ZJS
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. */
07719a21 606
463d0d15
LP
607 switch (scope) {
608
609 case UNIT_FILE_SYSTEM:
610 add = strv_new(
611 /* If you modify this you also want to modify
612 * systemdsystemunitpath= in systemd.pc.in! */
80b1ae32
LP
613 STRV_IFNOTNULL(persistent_control),
614 STRV_IFNOTNULL(runtime_control),
39591351 615 STRV_IFNOTNULL(transient),
463d0d15
LP
616 STRV_IFNOTNULL(generator_early),
617 persistent_config,
96d33e4a 618 SYSTEM_CONFIG_UNIT_DIR,
463d0d15 619 "/etc/systemd/system",
0cd4628d 620 STRV_IFNOTNULL(persistent_attached),
463d0d15
LP
621 runtime_config,
622 "/run/systemd/system",
0cd4628d 623 STRV_IFNOTNULL(runtime_attached),
463d0d15
LP
624 STRV_IFNOTNULL(generator),
625 "/usr/local/lib/systemd/system",
835cf75a 626 SYSTEM_DATA_UNIT_DIR,
463d0d15 627 "/usr/lib/systemd/system",
799b2102 628 STRV_IFNOTNULL(flags & LOOKUP_PATHS_SPLIT_USR ? "/lib/systemd/system" : NULL),
bea1a013 629 STRV_IFNOTNULL(generator_late));
463d0d15
LP
630 break;
631
632 case UNIT_FILE_GLOBAL:
633 add = strv_new(
07719a21 634 /* If you modify this you also want to modify
cf7d80a5
ZJS
635 * systemduserunitpath= in systemd.pc.in, and
636 * the arrays in user_dirs() above! */
80b1ae32
LP
637 STRV_IFNOTNULL(persistent_control),
638 STRV_IFNOTNULL(runtime_control),
39591351 639 STRV_IFNOTNULL(transient),
463d0d15 640 STRV_IFNOTNULL(generator_early),
a0f84a10 641 persistent_config,
96d33e4a 642 USER_CONFIG_UNIT_DIR,
cf7d80a5 643 "/etc/systemd/user",
a0f84a10 644 runtime_config,
cf7d80a5 645 "/run/systemd/user",
463d0d15 646 STRV_IFNOTNULL(generator),
cf7d80a5 647 "/usr/local/share/systemd/user",
7e684baf
ZJS
648 "/usr/share/systemd/user",
649 "/usr/local/lib/systemd/user",
96d33e4a 650 USER_DATA_UNIT_DIR,
cf7d80a5 651 "/usr/lib/systemd/user",
bea1a013 652 STRV_IFNOTNULL(generator_late));
463d0d15
LP
653 break;
654
655 case UNIT_FILE_USER:
656 add = user_dirs(persistent_config, runtime_config,
7b6344d3 657 global_persistent_config, global_runtime_config,
39591351 658 generator, generator_early, generator_late,
80b1ae32 659 transient,
c59479e7 660 persistent_control, runtime_control);
463d0d15
LP
661 break;
662
663 default:
04499a70 664 assert_not_reached();
463d0d15 665 }
07719a21 666
a3c4eb07 667 if (!add)
cf7d80a5
ZJS
668 return -ENOMEM;
669
8f1e0ad4
LP
670 if (paths) {
671 r = strv_extend_strv(&paths, add, true);
672 if (r < 0)
a3c4eb07 673 return r;
1cc6c93a 674 } else
8f1e0ad4
LP
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 */
1cc6c93a 677 paths = TAKE_PTR(add);
84e3543e
LP
678 }
679
e4bb56c7 680 r = patch_root_prefix(&persistent_config, root);
a0f84a10
LP
681 if (r < 0)
682 return r;
e4bb56c7 683 r = patch_root_prefix(&runtime_config, root);
a0f84a10
LP
684 if (r < 0)
685 return r;
686
e4bb56c7 687 r = patch_root_prefix(&generator, root);
a3c4eb07
LP
688 if (r < 0)
689 return r;
e4bb56c7 690 r = patch_root_prefix(&generator_early, root);
a3c4eb07
LP
691 if (r < 0)
692 return r;
e4bb56c7 693 r = patch_root_prefix(&generator_late, root);
a3c4eb07
LP
694 if (r < 0)
695 return r;
696
39591351
LP
697 r = patch_root_prefix(&transient, root);
698 if (r < 0)
699 return r;
700
80b1ae32
LP
701 r = patch_root_prefix(&persistent_control, root);
702 if (r < 0)
703 return r;
80b1ae32
LP
704 r = patch_root_prefix(&runtime_control, root);
705 if (r < 0)
706 return r;
707
0cd4628d
LP
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
e215d211 715 r = patch_root_prefix_strv(paths, root);
a1453343 716 if (r < 0)
07719a21 717 return -ENOMEM;
07459bb6 718
0cd4628d 719 *p = (LookupPaths) {
581fef8d 720 .search_path = strv_uniq(TAKE_PTR(paths)),
a3c4eb07 721
0cd4628d
LP
722 .persistent_config = TAKE_PTR(persistent_config),
723 .runtime_config = TAKE_PTR(runtime_config),
84e3543e 724
0cd4628d
LP
725 .generator = TAKE_PTR(generator),
726 .generator_early = TAKE_PTR(generator_early),
727 .generator_late = TAKE_PTR(generator_late),
39591351 728
0cd4628d 729 .transient = TAKE_PTR(transient),
80b1ae32 730
0cd4628d
LP
731 .persistent_control = TAKE_PTR(persistent_control),
732 .runtime_control = TAKE_PTR(runtime_control),
e4bb56c7 733
0cd4628d
LP
734 .persistent_attached = TAKE_PTR(persistent_attached),
735 .runtime_attached = TAKE_PTR(runtime_attached),
a1f31f47 736
0cd4628d
LP
737 .root_dir = TAKE_PTR(root),
738 .temporary_dir = TAKE_PTR(tempdir),
739 };
740
84e3543e
LP
741 return 0;
742}
743
744void lookup_paths_free(LookupPaths *p) {
a3c4eb07
LP
745 if (!p)
746 return;
84e3543e 747
a3c4eb07 748 p->search_path = strv_free(p->search_path);
a0f84a10
LP
749
750 p->persistent_config = mfree(p->persistent_config);
751 p->runtime_config = mfree(p->runtime_config);
752
0cd4628d
LP
753 p->persistent_attached = mfree(p->persistent_attached);
754 p->runtime_attached = mfree(p->runtime_attached);
755
a3c4eb07
LP
756 p->generator = mfree(p->generator);
757 p->generator_early = mfree(p->generator_early);
758 p->generator_late = mfree(p->generator_late);
e4bb56c7 759
39591351
LP
760 p->transient = mfree(p->transient);
761
80b1ae32
LP
762 p->persistent_control = mfree(p->persistent_control);
763 p->runtime_control = mfree(p->runtime_control);
764
e4bb56c7 765 p->root_dir = mfree(p->root_dir);
23115a60 766 p->temporary_dir = mfree(p->temporary_dir);
84e3543e 767}
cd64fd56 768
581fef8d 769void lookup_paths_log(LookupPaths *p) {
a1453343
LP
770 assert(p);
771
a1453343
LP
772 if (strv_isempty(p->search_path)) {
773 log_debug("Ignoring unit files.");
774 p->search_path = strv_free(p->search_path);
775 } else {
c2b2df60 776 _cleanup_free_ char *t = NULL;
a1453343
LP
777
778 t = strv_join(p->search_path, "\n\t");
581fef8d 779 log_debug("Looking for unit files in (higher priority first):\n\t%s", strna(t));
a1453343 780 }
a1453343
LP
781}
782
a69b4fb0 783char **generator_binary_paths(UnitFileScope scope) {
39f7d10c
BB
784 bool append = false; /* Add items from SYSTEMD_GENERATOR_PATH before normal directories */
785 _cleanup_strv_free_ char **paths = NULL;
786 int r;
a69b4fb0 787
39f7d10c
BB
788 /* First priority is whatever has been passed to us via env vars */
789 r = get_paths_from_environ("SYSTEMD_GENERATOR_PATH", &paths, &append);
790 if (r < 0)
791 return NULL;
a69b4fb0 792
39f7d10c
BB
793 if (!paths || append) {
794 _cleanup_strv_free_ char **add = NULL;
a69b4fb0 795
39f7d10c 796 switch (scope) {
a69b4fb0 797
39f7d10c
BB
798 case UNIT_FILE_SYSTEM:
799 add = strv_new("/run/systemd/system-generators",
800 "/etc/systemd/system-generators",
801 "/usr/local/lib/systemd/system-generators",
96d33e4a 802 SYSTEM_GENERATOR_DIR);
39f7d10c
BB
803 break;
804
805 case UNIT_FILE_GLOBAL:
806 case UNIT_FILE_USER:
807 add = strv_new("/run/systemd/user-generators",
808 "/etc/systemd/user-generators",
809 "/usr/local/lib/systemd/user-generators",
96d33e4a 810 USER_GENERATOR_DIR);
39f7d10c
BB
811 break;
812
813 default:
04499a70 814 assert_not_reached();
39f7d10c
BB
815 }
816
817 if (!add)
818 return NULL;
819
820 if (paths) {
821 r = strv_extend_strv(&paths, add, true);
822 if (r < 0)
823 return NULL;
824 } else
825 /* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it,
826 * and don't have to copy anything */
827 paths = TAKE_PTR(add);
a69b4fb0 828 }
39f7d10c
BB
829
830 return TAKE_PTR(paths);
a69b4fb0 831}
cccf5703
BB
832
833char **env_generator_binary_paths(bool is_system) {
39f7d10c
BB
834 bool append = false; /* Add items from SYSTEMD_ENVIRONMENT_GENERATOR_PATH before normal directories */
835 _cleanup_strv_free_ char **paths = NULL;
836 _cleanup_strv_free_ char **add = NULL;
837 int r;
cccf5703 838
39f7d10c
BB
839 /* First priority is whatever has been passed to us via env vars */
840 r = get_paths_from_environ("SYSTEMD_ENVIRONMENT_GENERATOR_PATH", &paths, &append);
841 if (r < 0)
842 return NULL;
843
844 if (!paths || append) {
845 if (is_system)
846 add = strv_new("/run/systemd/system-environment-generators",
847 "/etc/systemd/system-environment-generators",
848 "/usr/local/lib/systemd/system-environment-generators",
96d33e4a 849 SYSTEM_ENV_GENERATOR_DIR);
39f7d10c
BB
850 else
851 add = strv_new("/run/systemd/user-environment-generators",
852 "/etc/systemd/user-environment-generators",
853 "/usr/local/lib/systemd/user-environment-generators",
96d33e4a 854 USER_ENV_GENERATOR_DIR);
39f7d10c
BB
855
856 if (!add)
857 return NULL;
858 }
859
860 if (paths) {
861 r = strv_extend_strv(&paths, add, true);
862 if (r < 0)
863 return NULL;
864 } else
865 /* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it,
866 * and don't have to copy anything */
867 paths = TAKE_PTR(add);
868
869 return TAKE_PTR(paths);
cccf5703 870}
13c02e7b
LB
871
872int find_portable_profile(const char *name, const char *unit, char **ret_path) {
873 const char *p, *dot;
874
875 assert(name);
876 assert(ret_path);
877
878 assert_se(dot = strrchr(unit, '.'));
879
880 NULSTR_FOREACH(p, PORTABLE_PROFILE_DIRS) {
881 _cleanup_free_ char *joined = NULL;
882
883 joined = strjoin(p, "/", name, "/", dot + 1, ".conf");
884 if (!joined)
885 return -ENOMEM;
886
887 if (laccess(joined, F_OK) >= 0) {
888 *ret_path = TAKE_PTR(joined);
889 return 0;
890 }
891
892 if (errno != ENOENT)
893 return -errno;
894 }
895
896 return -ENOENT;
897}