]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
84e3543e | 2 | |
cf0fbc49 | 3 | #include <errno.h> |
84e3543e | 4 | #include <stdio.h> |
cf0fbc49 | 5 | #include <stdlib.h> |
67445f4e | 6 | #include <string.h> |
84e3543e | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
a1f31f47 | 9 | #include "fs-util.h" |
a8ffe6fb | 10 | #include "install.h" |
a8fbdf54 TA |
11 | #include "log.h" |
12 | #include "macro.h" | |
cd64fd56 | 13 | #include "mkdir.h" |
07630cea | 14 | #include "path-lookup.h" |
cf0fbc49 | 15 | #include "path-util.h" |
80b1ae32 | 16 | #include "rm-rf.h" |
e4bb56c7 | 17 | #include "stat-util.h" |
cf0fbc49 TA |
18 | #include "string-util.h" |
19 | #include "strv.h" | |
e4de7287 | 20 | #include "tmpfile-util.h" |
c4997074 | 21 | #include "user-util.h" |
cf0fbc49 | 22 | #include "util.h" |
84e3543e | 23 | |
ca4adeb7 | 24 | int xdg_user_runtime_dir(char **ret, const char *suffix) { |
10e87ee7 | 25 | const char *e; |
205dd21e LP |
26 | char *j; |
27 | ||
28 | assert(ret); | |
80b1ae32 | 29 | assert(suffix); |
10e87ee7 | 30 | |
80b1ae32 LP |
31 | e = getenv("XDG_RUNTIME_DIR"); |
32 | if (!e) | |
33 | return -ENXIO; | |
205dd21e | 34 | |
80b1ae32 LP |
35 | j = strappend(e, suffix); |
36 | if (!j) | |
37 | return -ENOMEM; | |
10e87ee7 | 38 | |
205dd21e | 39 | *ret = j; |
10e87ee7 LP |
40 | return 0; |
41 | } | |
42 | ||
ca4adeb7 | 43 | int xdg_user_config_dir(char **ret, const char *suffix) { |
718880ba | 44 | const char *e; |
205dd21e | 45 | char *j; |
c4997074 | 46 | int r; |
205dd21e LP |
47 | |
48 | assert(ret); | |
718880ba | 49 | |
80b1ae32 LP |
50 | e = getenv("XDG_CONFIG_HOME"); |
51 | if (e) | |
52 | j = strappend(e, suffix); | |
53 | else { | |
c4997074 | 54 | _cleanup_free_ char *home = NULL; |
80b1ae32 | 55 | |
c4997074 LP |
56 | r = get_home_dir(&home); |
57 | if (r < 0) | |
58 | return r; | |
80b1ae32 | 59 | |
605405c6 | 60 | j = strjoin(home, "/.config", suffix); |
80b1ae32 | 61 | } |
718880ba | 62 | |
205dd21e LP |
63 | if (!j) |
64 | return -ENOMEM; | |
718880ba | 65 | |
205dd21e | 66 | *ret = j; |
718880ba SA |
67 | return 0; |
68 | } | |
69 | ||
ca4adeb7 | 70 | int xdg_user_data_dir(char **ret, const char *suffix) { |
e801700e | 71 | const char *e; |
205dd21e | 72 | char *j; |
c4997074 | 73 | int r; |
205dd21e LP |
74 | |
75 | assert(ret); | |
a7527131 | 76 | assert(suffix); |
e801700e ZJS |
77 | |
78 | /* We don't treat /etc/xdg/systemd here as the spec | |
61233823 | 79 | * suggests because we assume that is a link to |
e801700e ZJS |
80 | * /etc/systemd/ anyway. */ |
81 | ||
82 | e = getenv("XDG_DATA_HOME"); | |
83 | if (e) | |
205dd21e | 84 | j = strappend(e, suffix); |
e801700e | 85 | else { |
c4997074 | 86 | _cleanup_free_ char *home = NULL; |
205dd21e | 87 | |
c4997074 LP |
88 | r = get_home_dir(&home); |
89 | if (r < 0) | |
90 | return r; | |
205dd21e | 91 | |
605405c6 | 92 | j = strjoin(home, "/.local/share", suffix); |
e801700e | 93 | } |
205dd21e | 94 | if (!j) |
e801700e ZJS |
95 | return -ENOMEM; |
96 | ||
205dd21e | 97 | *ret = j; |
463d0d15 | 98 | return 1; |
e801700e ZJS |
99 | } |
100 | ||
d2561cfd ZJS |
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 | ||
ca4adeb7 | 116 | int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs) { |
84e3543e LP |
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 | */ | |
ca4adeb7 ZJS |
125 | const char *e; |
126 | _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; | |
84e3543e | 127 | |
07719a21 LP |
128 | e = getenv("XDG_CONFIG_DIRS"); |
129 | if (e) { | |
130 | config_dirs = strv_split(e, ":"); | |
131 | if (!config_dirs) | |
ca4adeb7 | 132 | return -ENOMEM; |
07719a21 | 133 | } |
84e3543e | 134 | |
07719a21 LP |
135 | e = getenv("XDG_DATA_DIRS"); |
136 | if (e) | |
84e3543e LP |
137 | data_dirs = strv_split(e, ":"); |
138 | else | |
ef3102bf | 139 | data_dirs = strv_new("/usr/local/share", |
bea1a013 | 140 | "/usr/share"); |
84e3543e | 141 | if (!data_dirs) |
ca4adeb7 ZJS |
142 | return -ENOMEM; |
143 | ||
61c22b0b LP |
144 | *ret_config_dirs = TAKE_PTR(config_dirs); |
145 | *ret_data_dirs = TAKE_PTR(data_dirs); | |
146 | ||
ca4adeb7 ZJS |
147 | return 0; |
148 | } | |
149 | ||
150 | static char** user_dirs( | |
151 | const char *persistent_config, | |
152 | const char *runtime_config, | |
7b6344d3 ZJS |
153 | const char *global_persistent_config, |
154 | const char *global_runtime_config, | |
ca4adeb7 ZJS |
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; | |
ca4adeb7 ZJS |
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) | |
e801700e | 173 | return NULL; |
84e3543e LP |
174 | |
175 | /* Now merge everything we found. */ | |
80b1ae32 LP |
176 | if (strv_extend(&res, persistent_control) < 0) |
177 | return NULL; | |
39591351 | 178 | |
80b1ae32 LP |
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; | |
07719a21 | 187 | |
b9418b05 LP |
188 | if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0) |
189 | return NULL; | |
718880ba | 190 | |
a0f84a10 LP |
191 | if (strv_extend(&res, persistent_config) < 0) |
192 | return NULL; | |
193 | ||
7b6344d3 ZJS |
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 | ||
d2561cfd | 198 | if (strv_extend_strv(&res, (char**) user_config_unit_paths, false) < 0) |
e801700e | 199 | return NULL; |
718880ba | 200 | |
a0f84a10 | 201 | if (strv_extend(&res, runtime_config) < 0) |
e801700e | 202 | return NULL; |
84e3543e | 203 | |
7b6344d3 ZJS |
204 | if (strv_extend(&res, global_runtime_config) < 0) |
205 | return NULL; | |
206 | ||
80b1ae32 LP |
207 | if (strv_extend(&res, generator) < 0) |
208 | return NULL; | |
07719a21 | 209 | |
80b1ae32 LP |
210 | if (strv_extend(&res, data_home) < 0) |
211 | return NULL; | |
84e3543e | 212 | |
b9418b05 LP |
213 | if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0) |
214 | return NULL; | |
84e3543e | 215 | |
d2561cfd | 216 | if (strv_extend_strv(&res, (char**) user_data_unit_paths, false) < 0) |
e801700e | 217 | return NULL; |
84e3543e | 218 | |
80b1ae32 LP |
219 | if (strv_extend(&res, generator_late) < 0) |
220 | return NULL; | |
07719a21 | 221 | |
0f474365 | 222 | if (path_strv_make_absolute_cwd(res) < 0) |
e801700e | 223 | return NULL; |
84e3543e | 224 | |
ae2a15bc | 225 | return TAKE_PTR(res); |
e801700e | 226 | } |
84e3543e | 227 | |
d2561cfd ZJS |
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 | ||
a3c4eb07 | 240 | static int acquire_generator_dirs( |
463d0d15 | 241 | UnitFileScope scope, |
a1f31f47 | 242 | const char *tempdir, |
a3c4eb07 LP |
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); | |
a1f31f47 | 253 | assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER, UNIT_FILE_GLOBAL)); |
a3c4eb07 | 254 | |
a1f31f47 ZJS |
255 | if (scope == UNIT_FILE_GLOBAL) |
256 | return -EOPNOTSUPP; | |
463d0d15 | 257 | |
a1f31f47 ZJS |
258 | if (tempdir) |
259 | prefix = tempdir; | |
a1f31f47 ZJS |
260 | else if (scope == UNIT_FILE_SYSTEM) |
261 | prefix = "/run/systemd"; | |
3c47d509 ZJS |
262 | else { |
263 | /* UNIT_FILE_USER */ | |
463d0d15 | 264 | const char *e; |
a3c4eb07 LP |
265 | |
266 | e = getenv("XDG_RUNTIME_DIR"); | |
267 | if (!e) | |
463d0d15 | 268 | return -ENXIO; |
a3c4eb07 | 269 | |
a1f31f47 | 270 | prefix = strjoina(e, "/systemd"); |
a3c4eb07 LP |
271 | } |
272 | ||
a1f31f47 | 273 | x = strappend(prefix, "/generator"); |
a3c4eb07 LP |
274 | if (!x) |
275 | return -ENOMEM; | |
276 | ||
a1f31f47 | 277 | y = strappend(prefix, "/generator.early"); |
a3c4eb07 LP |
278 | if (!y) |
279 | return -ENOMEM; | |
280 | ||
a1f31f47 | 281 | z = strappend(prefix, "/generator.late"); |
a3c4eb07 LP |
282 | if (!z) |
283 | return -ENOMEM; | |
284 | ||
1cc6c93a YW |
285 | *generator = TAKE_PTR(x); |
286 | *generator_early = TAKE_PTR(y); | |
287 | *generator_late = TAKE_PTR(z); | |
a3c4eb07 | 288 | |
a3c4eb07 LP |
289 | return 0; |
290 | } | |
291 | ||
a1f31f47 ZJS |
292 | static int acquire_transient_dir( |
293 | UnitFileScope scope, | |
294 | const char *tempdir, | |
295 | char **ret) { | |
39591351 | 296 | |
a1f31f47 | 297 | char *transient; |
39591351 | 298 | |
a1f31f47 ZJS |
299 | assert(ret); |
300 | assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER, UNIT_FILE_GLOBAL)); | |
39591351 | 301 | |
a1f31f47 ZJS |
302 | if (scope == UNIT_FILE_GLOBAL) |
303 | return -EOPNOTSUPP; | |
39591351 | 304 | |
a1f31f47 ZJS |
305 | if (tempdir) |
306 | transient = strjoin(tempdir, "/transient"); | |
307 | else if (scope == UNIT_FILE_SYSTEM) | |
308 | transient = strdup("/run/systemd/transient"); | |
309 | else | |
ca4adeb7 | 310 | return xdg_user_runtime_dir(ret, "/systemd/transient"); |
a7527131 | 311 | |
a1f31f47 ZJS |
312 | if (!transient) |
313 | return -ENOMEM; | |
314 | *ret = transient; | |
315 | return 0; | |
39591351 LP |
316 | } |
317 | ||
463d0d15 | 318 | static int acquire_config_dirs(UnitFileScope scope, char **persistent, char **runtime) { |
a0f84a10 LP |
319 | _cleanup_free_ char *a = NULL, *b = NULL; |
320 | int r; | |
321 | ||
322 | assert(persistent); | |
323 | assert(runtime); | |
324 | ||
463d0d15 LP |
325 | switch (scope) { |
326 | ||
327 | case UNIT_FILE_SYSTEM: | |
a0f84a10 LP |
328 | a = strdup(SYSTEM_CONFIG_UNIT_PATH); |
329 | b = strdup("/run/systemd/system"); | |
463d0d15 LP |
330 | break; |
331 | ||
332 | case UNIT_FILE_GLOBAL: | |
333 | a = strdup(USER_CONFIG_UNIT_PATH); | |
334 | b = strdup("/run/systemd/user"); | |
335 | break; | |
a0f84a10 | 336 | |
463d0d15 | 337 | case UNIT_FILE_USER: |
ca4adeb7 | 338 | r = xdg_user_config_dir(&a, "/systemd/user"); |
c3c7eb7d | 339 | if (r < 0 && r != -ENXIO) |
a0f84a10 LP |
340 | return r; |
341 | ||
ca4adeb7 | 342 | r = xdg_user_runtime_dir(runtime, "/systemd/user"); |
c3c7eb7d LP |
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 | } | |
a0f84a10 | 351 | |
ae2a15bc | 352 | *persistent = TAKE_PTR(a); |
a0f84a10 LP |
353 | |
354 | return 0; | |
a0f84a10 | 355 | |
463d0d15 LP |
356 | default: |
357 | assert_not_reached("Hmm, unexpected scope value."); | |
a0f84a10 LP |
358 | } |
359 | ||
360 | if (!a || !b) | |
361 | return -ENOMEM; | |
362 | ||
1cc6c93a YW |
363 | *persistent = TAKE_PTR(a); |
364 | *runtime = TAKE_PTR(b); | |
a0f84a10 LP |
365 | |
366 | return 0; | |
367 | } | |
368 | ||
80b1ae32 LP |
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 | ||
ae2a15bc | 389 | *runtime = TAKE_PTR(b); |
80b1ae32 LP |
390 | |
391 | break; | |
392 | } | |
393 | ||
394 | case UNIT_FILE_USER: | |
0d6671b2 | 395 | r = xdg_user_config_dir(&a, "/systemd/user.control"); |
c3c7eb7d | 396 | if (r < 0 && r != -ENXIO) |
80b1ae32 LP |
397 | return r; |
398 | ||
0d6671b2 | 399 | r = xdg_user_runtime_dir(runtime, "/systemd/user.control"); |
c3c7eb7d LP |
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 | } | |
80b1ae32 LP |
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 | ||
ae2a15bc | 418 | *persistent = TAKE_PTR(a); |
80b1ae32 LP |
419 | |
420 | return 0; | |
421 | } | |
422 | ||
0cd4628d LP |
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 | ||
a3c4eb07 LP |
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 | ||
a3c4eb07 LP |
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 | ||
a1453343 LP |
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 | ||
07719a21 LP |
485 | int lookup_paths_init( |
486 | LookupPaths *p, | |
463d0d15 | 487 | UnitFileScope scope, |
4943d143 | 488 | LookupPathsFlags flags, |
a3c4eb07 | 489 | const char *root_dir) { |
07719a21 | 490 | |
a1f31f47 | 491 | _cleanup_(rmdir_and_freep) char *tempdir = NULL; |
e4bb56c7 LP |
492 | _cleanup_free_ char |
493 | *root = NULL, | |
39591351 | 494 | *persistent_config = NULL, *runtime_config = NULL, |
7b6344d3 | 495 | *global_persistent_config = NULL, *global_runtime_config = NULL, |
e4bb56c7 | 496 | *generator = NULL, *generator_early = NULL, *generator_late = NULL, |
80b1ae32 | 497 | *transient = NULL, |
0cd4628d LP |
498 | *persistent_control = NULL, *runtime_control = NULL, |
499 | *persistent_attached = NULL, *runtime_attached = NULL; | |
cf7d80a5 | 500 | bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */ |
e215d211 | 501 | _cleanup_strv_free_ char **paths = NULL; |
a3c4eb07 | 502 | const char *e; |
0f474365 | 503 | int r; |
84e3543e LP |
504 | |
505 | assert(p); | |
463d0d15 LP |
506 | assert(scope >= 0); |
507 | assert(scope < _UNIT_FILE_SCOPE_MAX); | |
a3c4eb07 | 508 | |
799b2102 LP |
509 | #if HAVE_SPLIT_USR |
510 | flags |= LOOKUP_PATHS_SPLIT_USR; | |
511 | #endif | |
512 | ||
57ea45e1 | 513 | if (!empty_or_root(root_dir)) { |
e4bb56c7 LP |
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 | ||
a1f31f47 ZJS |
528 | if (flags & LOOKUP_PATHS_TEMPORARY_GENERATED) { |
529 | r = mkdtemp_malloc("/tmp/systemd-temporary-XXXXXX", &tempdir); | |
530 | if (r < 0) | |
fc37d875 | 531 | return log_debug_errno(r, "Failed to create temporary directory: %m"); |
a1f31f47 ZJS |
532 | } |
533 | ||
c3c7eb7d | 534 | /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_config to NULL */ |
463d0d15 | 535 | r = acquire_config_dirs(scope, &persistent_config, &runtime_config); |
c3c7eb7d | 536 | if (r < 0) |
a0f84a10 LP |
537 | return r; |
538 | ||
7b6344d3 ZJS |
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 | ||
4943d143 | 545 | if ((flags & LOOKUP_PATHS_EXCLUDE_GENERATED) == 0) { |
c3c7eb7d | 546 | /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */ |
a1f31f47 ZJS |
547 | r = acquire_generator_dirs(scope, tempdir, |
548 | &generator, &generator_early, &generator_late); | |
4c701096 | 549 | if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO)) |
4943d143 LP |
550 | return r; |
551 | } | |
84e3543e | 552 | |
c3c7eb7d | 553 | /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */ |
a1f31f47 | 554 | r = acquire_transient_dir(scope, tempdir, &transient); |
4c701096 | 555 | if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO)) |
39591351 LP |
556 | return r; |
557 | ||
c3c7eb7d | 558 | /* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_control to NULL */ |
80b1ae32 | 559 | r = acquire_control_dirs(scope, &persistent_control, &runtime_control); |
c3c7eb7d | 560 | if (r < 0 && r != -EOPNOTSUPP) |
80b1ae32 LP |
561 | return r; |
562 | ||
0cd4628d LP |
563 | r = acquire_attached_dirs(scope, &persistent_attached, &runtime_attached); |
564 | if (r < 0 && r != -EOPNOTSUPP) | |
565 | return r; | |
566 | ||
cd64fd56 | 567 | /* First priority is whatever has been passed to us via env vars */ |
07719a21 LP |
568 | e = getenv("SYSTEMD_UNIT_PATH"); |
569 | if (e) { | |
a3c4eb07 LP |
570 | const char *k; |
571 | ||
572 | k = endswith(e, ":"); | |
573 | if (k) { | |
574 | e = strndupa(e, k - e); | |
cf7d80a5 ZJS |
575 | append = true; |
576 | } | |
577 | ||
f63c4aab | 578 | /* FIXME: empty components in other places should be rejected. */ |
cf7d80a5 | 579 | |
e215d211 | 580 | r = path_split_and_make_absolute(e, &paths); |
0f474365 LP |
581 | if (r < 0) |
582 | return r; | |
e215d211 | 583 | } |
84e3543e | 584 | |
e215d211 | 585 | if (!paths || append) { |
cf7d80a5 ZJS |
586 | /* Let's figure something out. */ |
587 | ||
a3c4eb07 | 588 | _cleanup_strv_free_ char **add = NULL; |
84e3543e | 589 | |
07719a21 | 590 | /* For the user units we include share/ in the search |
cf7d80a5 ZJS |
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. */ | |
07719a21 | 595 | |
463d0d15 LP |
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! */ | |
80b1ae32 LP |
602 | STRV_IFNOTNULL(persistent_control), |
603 | STRV_IFNOTNULL(runtime_control), | |
39591351 | 604 | STRV_IFNOTNULL(transient), |
463d0d15 LP |
605 | STRV_IFNOTNULL(generator_early), |
606 | persistent_config, | |
5f0a41da | 607 | SYSTEM_CONFIG_UNIT_PATH, |
463d0d15 | 608 | "/etc/systemd/system", |
0cd4628d | 609 | STRV_IFNOTNULL(persistent_attached), |
463d0d15 LP |
610 | runtime_config, |
611 | "/run/systemd/system", | |
0cd4628d | 612 | STRV_IFNOTNULL(runtime_attached), |
463d0d15 LP |
613 | STRV_IFNOTNULL(generator), |
614 | "/usr/local/lib/systemd/system", | |
615 | SYSTEM_DATA_UNIT_PATH, | |
616 | "/usr/lib/systemd/system", | |
799b2102 | 617 | STRV_IFNOTNULL(flags & LOOKUP_PATHS_SPLIT_USR ? "/lib/systemd/system" : NULL), |
bea1a013 | 618 | STRV_IFNOTNULL(generator_late)); |
463d0d15 LP |
619 | break; |
620 | ||
621 | case UNIT_FILE_GLOBAL: | |
622 | add = strv_new( | |
07719a21 | 623 | /* If you modify this you also want to modify |
cf7d80a5 ZJS |
624 | * systemduserunitpath= in systemd.pc.in, and |
625 | * the arrays in user_dirs() above! */ | |
80b1ae32 LP |
626 | STRV_IFNOTNULL(persistent_control), |
627 | STRV_IFNOTNULL(runtime_control), | |
39591351 | 628 | STRV_IFNOTNULL(transient), |
463d0d15 | 629 | STRV_IFNOTNULL(generator_early), |
a0f84a10 | 630 | persistent_config, |
5f0a41da | 631 | USER_CONFIG_UNIT_PATH, |
cf7d80a5 | 632 | "/etc/systemd/user", |
a0f84a10 | 633 | runtime_config, |
cf7d80a5 | 634 | "/run/systemd/user", |
463d0d15 | 635 | STRV_IFNOTNULL(generator), |
cf7d80a5 | 636 | "/usr/local/share/systemd/user", |
7e684baf ZJS |
637 | "/usr/share/systemd/user", |
638 | "/usr/local/lib/systemd/user", | |
cf7d80a5 ZJS |
639 | USER_DATA_UNIT_PATH, |
640 | "/usr/lib/systemd/user", | |
bea1a013 | 641 | STRV_IFNOTNULL(generator_late)); |
463d0d15 LP |
642 | break; |
643 | ||
644 | case UNIT_FILE_USER: | |
645 | add = user_dirs(persistent_config, runtime_config, | |
7b6344d3 | 646 | global_persistent_config, global_runtime_config, |
39591351 | 647 | generator, generator_early, generator_late, |
80b1ae32 | 648 | transient, |
c59479e7 | 649 | persistent_control, runtime_control); |
463d0d15 LP |
650 | break; |
651 | ||
652 | default: | |
653 | assert_not_reached("Hmm, unexpected scope?"); | |
654 | } | |
07719a21 | 655 | |
a3c4eb07 | 656 | if (!add) |
cf7d80a5 ZJS |
657 | return -ENOMEM; |
658 | ||
8f1e0ad4 LP |
659 | if (paths) { |
660 | r = strv_extend_strv(&paths, add, true); | |
661 | if (r < 0) | |
a3c4eb07 | 662 | return r; |
1cc6c93a | 663 | } else |
8f1e0ad4 LP |
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 */ | |
1cc6c93a | 666 | paths = TAKE_PTR(add); |
84e3543e LP |
667 | } |
668 | ||
e4bb56c7 | 669 | r = patch_root_prefix(&persistent_config, root); |
a0f84a10 LP |
670 | if (r < 0) |
671 | return r; | |
e4bb56c7 | 672 | r = patch_root_prefix(&runtime_config, root); |
a0f84a10 LP |
673 | if (r < 0) |
674 | return r; | |
675 | ||
e4bb56c7 | 676 | r = patch_root_prefix(&generator, root); |
a3c4eb07 LP |
677 | if (r < 0) |
678 | return r; | |
e4bb56c7 | 679 | r = patch_root_prefix(&generator_early, root); |
a3c4eb07 LP |
680 | if (r < 0) |
681 | return r; | |
e4bb56c7 | 682 | r = patch_root_prefix(&generator_late, root); |
a3c4eb07 LP |
683 | if (r < 0) |
684 | return r; | |
685 | ||
39591351 LP |
686 | r = patch_root_prefix(&transient, root); |
687 | if (r < 0) | |
688 | return r; | |
689 | ||
80b1ae32 LP |
690 | r = patch_root_prefix(&persistent_control, root); |
691 | if (r < 0) | |
692 | return r; | |
80b1ae32 LP |
693 | r = patch_root_prefix(&runtime_control, root); |
694 | if (r < 0) | |
695 | return r; | |
696 | ||
0cd4628d LP |
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 | ||
e215d211 | 704 | r = patch_root_prefix_strv(paths, root); |
a1453343 | 705 | if (r < 0) |
07719a21 | 706 | return -ENOMEM; |
07459bb6 | 707 | |
0cd4628d LP |
708 | *p = (LookupPaths) { |
709 | .search_path = strv_uniq(paths), | |
a3c4eb07 | 710 | |
0cd4628d LP |
711 | .persistent_config = TAKE_PTR(persistent_config), |
712 | .runtime_config = TAKE_PTR(runtime_config), | |
84e3543e | 713 | |
0cd4628d LP |
714 | .generator = TAKE_PTR(generator), |
715 | .generator_early = TAKE_PTR(generator_early), | |
716 | .generator_late = TAKE_PTR(generator_late), | |
39591351 | 717 | |
0cd4628d | 718 | .transient = TAKE_PTR(transient), |
80b1ae32 | 719 | |
0cd4628d LP |
720 | .persistent_control = TAKE_PTR(persistent_control), |
721 | .runtime_control = TAKE_PTR(runtime_control), | |
e4bb56c7 | 722 | |
0cd4628d LP |
723 | .persistent_attached = TAKE_PTR(persistent_attached), |
724 | .runtime_attached = TAKE_PTR(runtime_attached), | |
a1f31f47 | 725 | |
0cd4628d LP |
726 | .root_dir = TAKE_PTR(root), |
727 | .temporary_dir = TAKE_PTR(tempdir), | |
728 | }; | |
729 | ||
730 | paths = NULL; | |
84e3543e LP |
731 | return 0; |
732 | } | |
733 | ||
734 | void lookup_paths_free(LookupPaths *p) { | |
a3c4eb07 LP |
735 | if (!p) |
736 | return; | |
84e3543e | 737 | |
a3c4eb07 | 738 | p->search_path = strv_free(p->search_path); |
a0f84a10 LP |
739 | |
740 | p->persistent_config = mfree(p->persistent_config); | |
741 | p->runtime_config = mfree(p->runtime_config); | |
742 | ||
0cd4628d LP |
743 | p->persistent_attached = mfree(p->persistent_attached); |
744 | p->runtime_attached = mfree(p->runtime_attached); | |
745 | ||
a3c4eb07 LP |
746 | p->generator = mfree(p->generator); |
747 | p->generator_early = mfree(p->generator_early); | |
748 | p->generator_late = mfree(p->generator_late); | |
e4bb56c7 | 749 | |
39591351 LP |
750 | p->transient = mfree(p->transient); |
751 | ||
80b1ae32 LP |
752 | p->persistent_control = mfree(p->persistent_control); |
753 | p->runtime_control = mfree(p->runtime_control); | |
754 | ||
e4bb56c7 | 755 | p->root_dir = mfree(p->root_dir); |
23115a60 | 756 | p->temporary_dir = mfree(p->temporary_dir); |
84e3543e | 757 | } |
cd64fd56 | 758 | |
a1453343 LP |
759 | int lookup_paths_reduce(LookupPaths *p) { |
760 | _cleanup_free_ struct stat *stats = NULL; | |
761 | size_t n_stats = 0, allocated = 0; | |
da6053d0 | 762 | size_t c = 0; |
a1453343 LP |
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 | |
d2bcd0ba | 768 | * the same by comparing their device and inode numbers. */ |
a1453343 LP |
769 | |
770 | if (!p->search_path) | |
771 | return 0; | |
772 | ||
773 | while (p->search_path[c]) { | |
774 | struct stat st; | |
da6053d0 | 775 | size_t k; |
a1453343 | 776 | |
45639f1b LP |
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 | ||
d2bcd0ba LP |
785 | r = chase_symlinks_and_stat(p->search_path[c], p->root_dir, 0, NULL, &st); |
786 | if (r == -ENOENT) | |
787 | goto remove_item; | |
a1453343 | 788 | if (r < 0) { |
a1453343 | 789 | /* If something we don't grok happened, let's better leave it in. */ |
d2bcd0ba | 790 | log_debug_errno(r, "Failed to chase and stat %s: %m", p->search_path[c]); |
a1453343 LP |
791 | c++; |
792 | continue; | |
793 | } | |
794 | ||
d2bcd0ba | 795 | for (k = 0; k < n_stats; k++) |
a1453343 LP |
796 | if (stats[k].st_dev == st.st_dev && |
797 | stats[k].st_ino == st.st_ino) | |
798 | break; | |
a1453343 LP |
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 | ||
cd64fd56 LP |
833 | int lookup_paths_mkdir_generator(LookupPaths *p) { |
834 | int r, q; | |
835 | ||
836 | assert(p); | |
837 | ||
4943d143 LP |
838 | if (!p->generator || !p->generator_early || !p->generator_late) |
839 | return -EINVAL; | |
840 | ||
cd64fd56 LP |
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); | |
cd64fd56 LP |
861 | if (p->generator_early) |
862 | (void) rmdir(p->generator_early); | |
cd64fd56 LP |
863 | if (p->generator_late) |
864 | (void) rmdir(p->generator_late); | |
865 | } | |
07a78643 LP |
866 | |
867 | void lookup_paths_flush_generator(LookupPaths *p) { | |
868 | assert(p); | |
869 | ||
80b1ae32 LP |
870 | /* Flush the generated unit files in full */ |
871 | ||
07a78643 | 872 | if (p->generator) |
114110cd | 873 | (void) rm_rf(p->generator, REMOVE_ROOT|REMOVE_PHYSICAL); |
07a78643 | 874 | if (p->generator_early) |
114110cd | 875 | (void) rm_rf(p->generator_early, REMOVE_ROOT|REMOVE_PHYSICAL); |
07a78643 | 876 | if (p->generator_late) |
114110cd | 877 | (void) rm_rf(p->generator_late, REMOVE_ROOT|REMOVE_PHYSICAL); |
a1f31f47 ZJS |
878 | |
879 | if (p->temporary_dir) | |
114110cd | 880 | (void) rm_rf(p->temporary_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
07a78643 | 881 | } |
a69b4fb0 LP |
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", | |
bea1a013 | 891 | SYSTEM_GENERATOR_PATH); |
a69b4fb0 LP |
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", | |
bea1a013 | 898 | USER_GENERATOR_PATH); |
a69b4fb0 LP |
899 | |
900 | default: | |
901 | assert_not_reached("Hmm, unexpected scope."); | |
902 | } | |
903 | } |