1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-05 15:21:37 +03:00

path-lookup: Split out helper to get paths from environment

This is so that we can use the same pattern for other sets of paths.
This commit is contained in:
Benjamin Berg 2020-02-17 11:53:26 +01:00
parent cccf570355
commit 2909f4dd28

View File

@ -479,6 +479,36 @@ static int patch_root_prefix_strv(char **l, const char *root_dir) {
return 0; return 0;
} }
static int get_paths_from_environ(const char *var, char ***paths, bool *append) {
const char *e;
int r;
assert(var);
assert(paths);
assert(append);
*append = false;
e = getenv(var);
if (e) {
const char *k;
k = endswith(e, ":");
if (k) {
e = strndupa(e, k - e);
*append = true;
}
/* FIXME: empty components in other places should be rejected. */
r = path_split_and_make_absolute(e, paths);
if (r < 0)
return r;
}
return 0;
}
int lookup_paths_init( int lookup_paths_init(
LookupPaths *p, LookupPaths *p,
UnitFileScope scope, UnitFileScope scope,
@ -496,7 +526,6 @@ int lookup_paths_init(
*persistent_attached = NULL, *runtime_attached = NULL; *persistent_attached = NULL, *runtime_attached = NULL;
bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */ bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */
_cleanup_strv_free_ char **paths = NULL; _cleanup_strv_free_ char **paths = NULL;
const char *e;
int r; int r;
assert(p); assert(p);
@ -562,22 +591,9 @@ int lookup_paths_init(
return r; return r;
/* First priority is whatever has been passed to us via env vars */ /* First priority is whatever has been passed to us via env vars */
e = getenv("SYSTEMD_UNIT_PATH"); r = get_paths_from_environ("SYSTEMD_UNIT_PATH", &paths, &append);
if (e) { if (r < 0)
const char *k; return r;
k = endswith(e, ":");
if (k) {
e = strndupa(e, k - e);
append = true;
}
/* FIXME: empty components in other places should be rejected. */
r = path_split_and_make_absolute(e, &paths);
if (r < 0)
return r;
}
if (!paths || append) { if (!paths || append) {
/* Let's figure something out. */ /* Let's figure something out. */