| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "fs-util.h" |
| 7 |
#include "nulstr-util.h" |
| 8 |
#include "path-lookup.h" |
| 9 |
#include "portable-util.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "strv.h" |
| 12 |
|
| 13 |
int portable_profile_dirs(RuntimeScope scope, char ***ret) { |
| 14 |
_cleanup_strv_free_ char **dirs = NULL; |
| 15 |
int r; |
| 16 |
|
| 17 |
assert(ret); |
| 18 |
|
| 19 |
switch (scope) { |
| 20 |
|
| 21 |
case RUNTIME_SCOPE_SYSTEM: |
| 22 |
r = strv_from_nulstr(&dirs, PORTABLE_PROFILE_DIRS); |
| 23 |
if (r < 0) |
| 24 |
return r; |
| 25 |
|
| 26 |
break; |
| 27 |
|
| 28 |
case RUNTIME_SCOPE_USER: { |
| 29 |
_cleanup_free_ char *d = NULL; |
| 30 |
|
| 31 |
r = xdg_user_config_dir("systemd/portable/profile", &d); |
| 32 |
if (r < 0) |
| 33 |
return r; |
| 34 |
|
| 35 |
r = strv_consume(&dirs, TAKE_PTR(d)); |
| 36 |
if (r < 0) |
| 37 |
return r; |
| 38 |
|
| 39 |
r = xdg_user_runtime_dir("systemd/portable/profile", &d); |
| 40 |
if (r < 0 && r != -ENXIO) |
| 41 |
return r; |
| 42 |
if (r >= 0) { |
| 43 |
r = strv_consume(&dirs, TAKE_PTR(d)); |
| 44 |
if (r < 0) |
| 45 |
return r; |
| 46 |
} |
| 47 |
|
| 48 |
_fallthrough_; |
| 49 |
} |
| 50 |
|
| 51 |
case RUNTIME_SCOPE_GLOBAL: |
| 52 |
r = strv_extend_strv( |
| 53 |
&dirs, |
| 54 |
CONF_PATHS_STRV("systemd/user/portable/profile"), |
| 55 |
/* filter_duplicates= */ false); |
| 56 |
if (r < 0) |
| 57 |
return r; |
| 58 |
|
| 59 |
break; |
| 60 |
|
| 61 |
default: |
| 62 |
return -EINVAL; |
| 63 |
} |
| 64 |
|
| 65 |
*ret = TAKE_PTR(dirs); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
int find_portable_profile(RuntimeScope scope, const char *name, const char *unit, char **ret_path) { |
| 70 |
_cleanup_strv_free_ char **dirs = NULL; |
| 71 |
const char *dot; |
| 72 |
int r; |
| 73 |
|
| 74 |
assert(name); |
| 75 |
assert(ret_path); |
| 76 |
|
| 77 |
assert_se(dot = strrchr(unit, '.')); |
| 78 |
|
| 79 |
r = portable_profile_dirs(scope, &dirs); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
|
| 83 |
STRV_FOREACH(p, dirs) { |
| 84 |
_cleanup_free_ char *joined = NULL; |
| 85 |
|
| 86 |
joined = strjoin(*p, "/", name, "/", dot + 1, ".conf"); |
| 87 |
if (!joined) |
| 88 |
return -ENOMEM; |
| 89 |
|
| 90 |
r = access_nofollow(joined, F_OK); |
| 91 |
if (r >= 0) { |
| 92 |
*ret_path = TAKE_PTR(joined); |
| 93 |
return 0; |
| 94 |
} |
| 95 |
if (r != -ENOENT) |
| 96 |
return r; |
| 97 |
} |
| 98 |
|
| 99 |
return -ENOENT; |
| 100 |
} |