1
0
mirror of https://github.com/systemd/systemd.git synced 2025-02-25 21:57:32 +03:00

Merge pull request #34432 from YHNdnzj/path-lookup-refactor

path-lookup: several cleanups
This commit is contained in:
Yu Watanabe 2024-10-07 09:31:47 +09:00 committed by GitHub
commit ffe967c598
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
26 changed files with 948 additions and 1054 deletions

View File

@ -2092,6 +2092,7 @@ libsystemd_includes = [basic_includes, include_directories(
'src/libsystemd/sd-json',
'src/libsystemd/sd-netlink',
'src/libsystemd/sd-network',
'src/libsystemd/sd-path',
'src/libsystemd/sd-resolve',
'src/libsystemd/sd-varlink')]

View File

@ -26,6 +26,7 @@
#include "nulstr-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "portable-util.h"
#include "pretty-print.h"
#include "seccomp-util.h"
#include "service.h"

View File

@ -70,7 +70,6 @@ basic_sources = files(
'ordered-set.c',
'os-util.c',
'parse-util.c',
'path-lookup.c',
'path-util.c',
'percent-util.c',
'pidref.c',
@ -106,7 +105,6 @@ basic_sources = files(
'uid-classification.c',
'uid-range.c',
'unit-def.c',
'unit-file.c',
'unit-name.c',
'user-util.c',
'utf8.c',

View File

@ -1,931 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "alloc-util.h"
#include "fs-util.h"
#include "log.h"
#include "macro.h"
#include "nulstr-util.h"
#include "path-lookup.h"
#include "path-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "tmpfile-util.h"
#include "user-util.h"
int xdg_user_runtime_dir(char **ret, const char *suffix) {
const char *e;
char *j;
assert(ret);
assert(suffix);
e = getenv("XDG_RUNTIME_DIR");
if (!e)
return -ENXIO;
j = path_join(e, suffix);
if (!j)
return -ENOMEM;
*ret = j;
return 0;
}
int xdg_user_config_dir(char **ret, const char *suffix) {
_cleanup_free_ char *j = NULL;
const char *e;
int r;
assert(ret);
e = getenv("XDG_CONFIG_HOME");
if (e) {
j = path_join(e, suffix);
if (!j)
return -ENOMEM;
} else {
r = get_home_dir(&j);
if (r < 0)
return r;
if (!path_extend(&j, "/.config", suffix))
return -ENOMEM;
}
*ret = TAKE_PTR(j);
return 0;
}
int xdg_user_data_dir(char **ret, const char *suffix) {
_cleanup_free_ char *j = NULL;
const char *e;
int r;
assert(ret);
assert(suffix);
/* We don't treat /etc/xdg/systemd here as the spec
* suggests because we assume that is a link to
* /etc/systemd/ anyway. */
e = getenv("XDG_DATA_HOME");
if (e) {
j = path_join(e, suffix);
if (!j)
return -ENOMEM;
} else {
r = get_home_dir(&j);
if (r < 0)
return r;
if (!path_extend(&j, "/.local/share", suffix))
return -ENOMEM;
}
*ret = TAKE_PTR(j);
return 1;
}
int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) {
int r;
assert(ret);
assert(suffix);
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
/* Accept $RUNTIME_DIRECTORY as authoritative
* If its missing apply the suffix to /run or $XDG_RUNTIME_DIR
* if we are in a user runtime scope.
*
* Return value indicates whether the suffix was applied or not */
const char *e = secure_getenv("RUNTIME_DIRECTORY");
if (e)
return strdup_to(ret, e);
if (scope == RUNTIME_SCOPE_USER) {
r = xdg_user_runtime_dir(ret, suffix);
if (r < 0)
return r;
} else {
char *d = path_join("/run", suffix);
if (!d)
return -ENOMEM;
*ret = d;
}
return true;
}
static const char* const user_data_unit_paths[] = {
"/usr/local/lib/systemd/user",
"/usr/local/share/systemd/user",
USER_DATA_UNIT_DIR,
"/usr/lib/systemd/user",
"/usr/share/systemd/user",
NULL
};
static const char* const user_config_unit_paths[] = {
USER_CONFIG_UNIT_DIR,
"/etc/systemd/user",
NULL
};
int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs) {
/* Implement the mechanisms defined in
*
* https://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
*
* We look in both the config and the data dirs because we
* want to encourage that distributors ship their unit files
* as data, and allow overriding as configuration.
*/
const char *e;
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
e = getenv("XDG_CONFIG_DIRS");
if (e)
config_dirs = strv_split(e, ":");
else
config_dirs = strv_new("/etc/xdg");
if (!config_dirs)
return -ENOMEM;
e = getenv("XDG_DATA_DIRS");
if (e)
data_dirs = strv_split(e, ":");
else
data_dirs = strv_new("/usr/local/share",
"/usr/share");
if (!data_dirs)
return -ENOMEM;
*ret_config_dirs = TAKE_PTR(config_dirs);
*ret_data_dirs = TAKE_PTR(data_dirs);
return 0;
}
static char** 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) {
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
_cleanup_free_ char *data_home = NULL;
_cleanup_strv_free_ char **res = NULL;
int r;
r = xdg_user_dirs(&config_dirs, &data_dirs);
if (r < 0)
return NULL;
r = xdg_user_data_dir(&data_home, "/systemd/user");
if (r < 0 && r != -ENXIO)
return NULL;
/* Now merge everything we found. */
if (strv_extend_many(
&res,
persistent_control,
runtime_control,
transient,
generator_early,
persistent_config) < 0)
return NULL;
if (strv_extend_strv_concat(&res, (const char* const*) config_dirs, "/systemd/user") < 0)
return NULL;
/* global config has lower priority than the user config of the same type */
if (strv_extend(&res, global_persistent_config) < 0)
return NULL;
if (strv_extend_strv(&res, (char**) user_config_unit_paths, false) < 0)
return NULL;
if (strv_extend_many(
&res,
runtime_config,
global_runtime_config,
generator,
data_home) < 0)
return NULL;
if (strv_extend_strv_concat(&res, (const char* const*) data_dirs, "/systemd/user") < 0)
return NULL;
if (strv_extend_strv(&res, (char**) user_data_unit_paths, false) < 0)
return NULL;
if (strv_extend(&res, generator_late) < 0)
return NULL;
if (path_strv_make_absolute_cwd(res) < 0)
return NULL;
return TAKE_PTR(res);
}
bool path_is_user_data_dir(const char *path) {
assert(path);
return strv_contains((char**) user_data_unit_paths, path);
}
bool path_is_user_config_dir(const char *path) {
assert(path);
return strv_contains((char**) user_config_unit_paths, path);
}
static int acquire_generator_dirs(
RuntimeScope scope,
const char *tempdir,
char **generator,
char **generator_early,
char **generator_late) {
_cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *p = NULL;
const char *prefix;
assert(generator);
assert(generator_early);
assert(generator_late);
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
if (scope == RUNTIME_SCOPE_GLOBAL)
return -EOPNOTSUPP;
if (tempdir)
prefix = tempdir;
else if (scope == RUNTIME_SCOPE_SYSTEM)
prefix = "/run/systemd";
else {
/* RUNTIME_SCOPE_USER */
const char *e;
e = getenv("XDG_RUNTIME_DIR");
if (!e)
return -ENXIO;
p = path_join(e, "/systemd");
if (!p)
return -ENOMEM;
prefix = p;
}
x = path_join(prefix, "generator");
if (!x)
return -ENOMEM;
y = path_join(prefix, "generator.early");
if (!y)
return -ENOMEM;
z = path_join(prefix, "generator.late");
if (!z)
return -ENOMEM;
*generator = TAKE_PTR(x);
*generator_early = TAKE_PTR(y);
*generator_late = TAKE_PTR(z);
return 0;
}
static int acquire_transient_dir(
RuntimeScope scope,
const char *tempdir,
char **ret) {
char *transient;
assert(ret);
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
if (scope == RUNTIME_SCOPE_GLOBAL)
return -EOPNOTSUPP;
if (tempdir)
transient = path_join(tempdir, "transient");
else if (scope == RUNTIME_SCOPE_SYSTEM)
transient = strdup("/run/systemd/transient");
else
return xdg_user_runtime_dir(ret, "/systemd/transient");
if (!transient)
return -ENOMEM;
*ret = transient;
return 0;
}
static int acquire_config_dirs(RuntimeScope scope, char **persistent, char **runtime) {
_cleanup_free_ char *a = NULL, *b = NULL;
int r;
assert(persistent);
assert(runtime);
switch (scope) {
case RUNTIME_SCOPE_SYSTEM:
a = strdup(SYSTEM_CONFIG_UNIT_DIR);
b = strdup("/run/systemd/system");
break;
case RUNTIME_SCOPE_GLOBAL:
a = strdup(USER_CONFIG_UNIT_DIR);
b = strdup("/run/systemd/user");
break;
case RUNTIME_SCOPE_USER:
r = xdg_user_config_dir(&a, "/systemd/user");
if (r < 0 && r != -ENXIO)
return r;
r = xdg_user_runtime_dir(runtime, "/systemd/user");
if (r < 0) {
if (r != -ENXIO)
return r;
/* If XDG_RUNTIME_DIR is not set, don't consider that fatal, simply initialize the runtime
* directory to NULL */
*runtime = NULL;
}
*persistent = TAKE_PTR(a);
return 0;
default:
assert_not_reached();
}
if (!a || !b)
return -ENOMEM;
*persistent = TAKE_PTR(a);
*runtime = TAKE_PTR(b);
return 0;
}
static int acquire_control_dirs(RuntimeScope scope, char **persistent, char **runtime) {
_cleanup_free_ char *a = NULL;
int r;
assert(persistent);
assert(runtime);
switch (scope) {
case RUNTIME_SCOPE_SYSTEM: {
_cleanup_free_ char *b = NULL;
a = strdup("/etc/systemd/system.control");
if (!a)
return -ENOMEM;
b = strdup("/run/systemd/system.control");
if (!b)
return -ENOMEM;
*runtime = TAKE_PTR(b);
break;
}
case RUNTIME_SCOPE_USER:
r = xdg_user_config_dir(&a, "/systemd/user.control");
if (r < 0 && r != -ENXIO)
return r;
r = xdg_user_runtime_dir(runtime, "/systemd/user.control");
if (r < 0) {
if (r != -ENXIO)
return r;
/* If XDG_RUNTIME_DIR is not set, don't consider this fatal, simply initialize the directory to
* NULL */
*runtime = NULL;
}
break;
case RUNTIME_SCOPE_GLOBAL:
return -EOPNOTSUPP;
default:
assert_not_reached();
}
*persistent = TAKE_PTR(a);
return 0;
}
static int acquire_attached_dirs(
RuntimeScope scope,
char **ret_persistent,
char **ret_runtime) {
_cleanup_free_ char *a = NULL, *b = NULL;
assert(ret_persistent);
assert(ret_runtime);
/* Portable services are not available to regular users for now. */
if (scope != RUNTIME_SCOPE_SYSTEM)
return -EOPNOTSUPP;
a = strdup("/etc/systemd/system.attached");
if (!a)
return -ENOMEM;
b = strdup("/run/systemd/system.attached");
if (!b)
return -ENOMEM;
*ret_persistent = TAKE_PTR(a);
*ret_runtime = TAKE_PTR(b);
return 0;
}
static int patch_root_prefix(char **p, const char *root_dir) {
char *c;
assert(p);
if (!*p)
return 0;
c = path_join(root_dir, *p);
if (!c)
return -ENOMEM;
free_and_replace(*p, c);
return 0;
}
static int patch_root_prefix_strv(char **l, const char *root_dir) {
int r;
if (!root_dir)
return 0;
STRV_FOREACH(i, l) {
r = patch_root_prefix(i, root_dir);
if (r < 0)
return r;
}
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_safe(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(
LookupPaths *lp,
RuntimeScope scope,
LookupPathsFlags flags,
const char *root_dir) {
_cleanup_(rmdir_and_freep) char *tempdir = NULL;
_cleanup_free_ char
*root = NULL,
*persistent_config = NULL, *runtime_config = NULL,
*global_persistent_config = NULL, *global_runtime_config = NULL,
*generator = NULL, *generator_early = NULL, *generator_late = NULL,
*transient = NULL,
*persistent_control = NULL, *runtime_control = NULL,
*persistent_attached = NULL, *runtime_attached = NULL;
bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */
_cleanup_strv_free_ char **paths = NULL;
int r;
assert(lp);
assert(scope >= 0);
assert(scope < _RUNTIME_SCOPE_MAX);
if (!empty_or_root(root_dir)) {
if (scope == RUNTIME_SCOPE_USER)
return -EINVAL;
r = is_dir(root_dir, true);
if (r < 0)
return r;
if (r == 0)
return -ENOTDIR;
root = strdup(root_dir);
if (!root)
return -ENOMEM;
}
if (flags & LOOKUP_PATHS_TEMPORARY_GENERATED) {
r = mkdtemp_malloc("/tmp/systemd-temporary-XXXXXX", &tempdir);
if (r < 0)
return log_debug_errno(r, "Failed to create temporary directory: %m");
}
/* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_config to NULL */
r = acquire_config_dirs(scope, &persistent_config, &runtime_config);
if (r < 0)
return r;
if (scope == RUNTIME_SCOPE_USER) {
r = acquire_config_dirs(RUNTIME_SCOPE_GLOBAL, &global_persistent_config, &global_runtime_config);
if (r < 0)
return r;
}
if ((flags & LOOKUP_PATHS_EXCLUDE_GENERATED) == 0) {
/* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
r = acquire_generator_dirs(scope, tempdir,
&generator, &generator_early, &generator_late);
if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
return r;
}
/* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
r = acquire_transient_dir(scope, tempdir, &transient);
if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
return r;
/* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_control to NULL */
r = acquire_control_dirs(scope, &persistent_control, &runtime_control);
if (r < 0 && r != -EOPNOTSUPP)
return r;
r = acquire_attached_dirs(scope, &persistent_attached, &runtime_attached);
if (r < 0 && r != -EOPNOTSUPP)
return r;
/* First priority is whatever has been passed to us via env vars */
r = get_paths_from_environ("SYSTEMD_UNIT_PATH", &paths, &append);
if (r < 0)
return r;
if (!paths || append) {
/* Let's figure something out. */
_cleanup_strv_free_ char **add = NULL;
/* For the user units we include share/ in the search
* path in order to comply with the XDG basedir spec.
* For the system stuff we avoid such nonsense. OTOH
* we include /lib in the search path for the system
* stuff but avoid it for user stuff. */
switch (scope) {
case RUNTIME_SCOPE_SYSTEM:
add = strv_new(
/* If you modify this you also want to modify
* systemdsystemunitpath= in systemd.pc.in! */
STRV_IFNOTNULL(persistent_control),
STRV_IFNOTNULL(runtime_control),
STRV_IFNOTNULL(transient),
STRV_IFNOTNULL(generator_early),
persistent_config,
SYSTEM_CONFIG_UNIT_DIR,
"/etc/systemd/system",
STRV_IFNOTNULL(persistent_attached),
runtime_config,
"/run/systemd/system",
STRV_IFNOTNULL(runtime_attached),
STRV_IFNOTNULL(generator),
"/usr/local/lib/systemd/system",
SYSTEM_DATA_UNIT_DIR,
"/usr/lib/systemd/system",
/* To be used ONLY for images which might be legacy split-usr */
STRV_IFNOTNULL(flags & LOOKUP_PATHS_SPLIT_USR ? "/lib/systemd/system" : NULL),
STRV_IFNOTNULL(generator_late));
break;
case RUNTIME_SCOPE_GLOBAL:
add = strv_new(
/* If you modify this you also want to modify
* systemduserunitpath= in systemd.pc.in, and
* the arrays in user_dirs() above! */
STRV_IFNOTNULL(persistent_control),
STRV_IFNOTNULL(runtime_control),
STRV_IFNOTNULL(transient),
STRV_IFNOTNULL(generator_early),
persistent_config,
USER_CONFIG_UNIT_DIR,
"/etc/systemd/user",
runtime_config,
"/run/systemd/user",
STRV_IFNOTNULL(generator),
"/usr/local/share/systemd/user",
"/usr/share/systemd/user",
"/usr/local/lib/systemd/user",
USER_DATA_UNIT_DIR,
"/usr/lib/systemd/user",
STRV_IFNOTNULL(generator_late));
break;
case RUNTIME_SCOPE_USER:
add = user_dirs(persistent_config, runtime_config,
global_persistent_config, global_runtime_config,
generator, generator_early, generator_late,
transient,
persistent_control, runtime_control);
break;
default:
assert_not_reached();
}
if (!add)
return -ENOMEM;
if (paths) {
r = strv_extend_strv(&paths, add, true);
if (r < 0)
return r;
} else
/* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it,
* and don't have to copy anything */
paths = TAKE_PTR(add);
}
r = patch_root_prefix(&persistent_config, root);
if (r < 0)
return r;
r = patch_root_prefix(&runtime_config, root);
if (r < 0)
return r;
r = patch_root_prefix(&generator, root);
if (r < 0)
return r;
r = patch_root_prefix(&generator_early, root);
if (r < 0)
return r;
r = patch_root_prefix(&generator_late, root);
if (r < 0)
return r;
r = patch_root_prefix(&transient, root);
if (r < 0)
return r;
r = patch_root_prefix(&persistent_control, root);
if (r < 0)
return r;
r = patch_root_prefix(&runtime_control, root);
if (r < 0)
return r;
r = patch_root_prefix(&persistent_attached, root);
if (r < 0)
return r;
r = patch_root_prefix(&runtime_attached, root);
if (r < 0)
return r;
r = patch_root_prefix_strv(paths, root);
if (r < 0)
return -ENOMEM;
*lp = (LookupPaths) {
.search_path = strv_uniq(TAKE_PTR(paths)),
.persistent_config = TAKE_PTR(persistent_config),
.runtime_config = TAKE_PTR(runtime_config),
.generator = TAKE_PTR(generator),
.generator_early = TAKE_PTR(generator_early),
.generator_late = TAKE_PTR(generator_late),
.transient = TAKE_PTR(transient),
.persistent_control = TAKE_PTR(persistent_control),
.runtime_control = TAKE_PTR(runtime_control),
.persistent_attached = TAKE_PTR(persistent_attached),
.runtime_attached = TAKE_PTR(runtime_attached),
.root_dir = TAKE_PTR(root),
.temporary_dir = TAKE_PTR(tempdir),
};
return 0;
}
int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir) {
int r;
r = lookup_paths_init(lp, scope, flags, root_dir);
if (r < 0)
return log_error_errno(r, "Failed to initialize unit search paths%s%s: %m",
isempty(root_dir) ? "" : " for root directory ", strempty(root_dir));
return r;
}
void lookup_paths_done(LookupPaths *lp) {
assert(lp);
lp->search_path = strv_free(lp->search_path);
lp->persistent_config = mfree(lp->persistent_config);
lp->runtime_config = mfree(lp->runtime_config);
lp->persistent_attached = mfree(lp->persistent_attached);
lp->runtime_attached = mfree(lp->runtime_attached);
lp->generator = mfree(lp->generator);
lp->generator_early = mfree(lp->generator_early);
lp->generator_late = mfree(lp->generator_late);
lp->transient = mfree(lp->transient);
lp->persistent_control = mfree(lp->persistent_control);
lp->runtime_control = mfree(lp->runtime_control);
lp->root_dir = mfree(lp->root_dir);
lp->temporary_dir = mfree(lp->temporary_dir);
}
void lookup_paths_log(LookupPaths *lp) {
assert(lp);
if (strv_isempty(lp->search_path)) {
log_debug("Ignoring unit files.");
lp->search_path = strv_free(lp->search_path);
} else {
_cleanup_free_ char *t = NULL;
t = strv_join(lp->search_path, "\n\t");
log_debug("Looking for unit files in (higher priority first):\n\t%s", strna(t));
}
}
char **generator_binary_paths(RuntimeScope scope) {
bool append = false; /* Add items from SYSTEMD_GENERATOR_PATH before normal directories */
_cleanup_strv_free_ char **paths = NULL;
int r;
/* First priority is whatever has been passed to us via env vars */
r = get_paths_from_environ("SYSTEMD_GENERATOR_PATH", &paths, &append);
if (r < 0)
return NULL;
if (!paths || append) {
_cleanup_strv_free_ char **add = NULL;
switch (scope) {
case RUNTIME_SCOPE_SYSTEM:
add = strv_new("/run/systemd/system-generators",
"/etc/systemd/system-generators",
"/usr/local/lib/systemd/system-generators",
SYSTEM_GENERATOR_DIR);
break;
case RUNTIME_SCOPE_GLOBAL:
case RUNTIME_SCOPE_USER:
add = strv_new("/run/systemd/user-generators",
"/etc/systemd/user-generators",
"/usr/local/lib/systemd/user-generators",
USER_GENERATOR_DIR);
break;
default:
assert_not_reached();
}
if (!add)
return NULL;
if (paths) {
r = strv_extend_strv(&paths, add, true);
if (r < 0)
return NULL;
} else
/* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it,
* and don't have to copy anything */
paths = TAKE_PTR(add);
}
return TAKE_PTR(paths);
}
char **env_generator_binary_paths(RuntimeScope runtime_scope) {
_cleanup_strv_free_ char **paths = NULL, **add = NULL;
bool append = false; /* Add items from SYSTEMD_ENVIRONMENT_GENERATOR_PATH before normal directories */
int r;
/* First priority is whatever has been passed to us via env vars */
r = get_paths_from_environ("SYSTEMD_ENVIRONMENT_GENERATOR_PATH", &paths, &append);
if (r < 0)
return NULL;
if (!paths || append) {
switch (runtime_scope) {
case RUNTIME_SCOPE_SYSTEM:
add = strv_new("/run/systemd/system-environment-generators",
"/etc/systemd/system-environment-generators",
"/usr/local/lib/systemd/system-environment-generators",
SYSTEM_ENV_GENERATOR_DIR);
break;
case RUNTIME_SCOPE_USER:
add = strv_new("/run/systemd/user-environment-generators",
"/etc/systemd/user-environment-generators",
"/usr/local/lib/systemd/user-environment-generators",
USER_ENV_GENERATOR_DIR);
break;
default:
assert_not_reached();
}
if (!add)
return NULL;
}
if (paths) {
r = strv_extend_strv(&paths, add, true);
if (r < 0)
return NULL;
} else
/* Small optimization: if paths is NULL (and it usually is), we can simply assign 'add' to it,
* and don't have to copy anything */
paths = TAKE_PTR(add);
return TAKE_PTR(paths);
}
int find_portable_profile(const char *name, const char *unit, char **ret_path) {
const char *dot;
int r;
assert(name);
assert(ret_path);
assert_se(dot = strrchr(unit, '.'));
NULSTR_FOREACH(p, PORTABLE_PROFILE_DIRS) {
_cleanup_free_ char *joined = NULL;
joined = strjoin(p, "/", name, "/", dot + 1, ".conf");
if (!joined)
return -ENOMEM;
r = access_nofollow(joined, F_OK);
if (r >= 0) {
*ret_path = TAKE_PTR(joined);
return 0;
}
if (r != -ENOENT)
return r;
}
return -ENOENT;
}

View File

@ -182,7 +182,7 @@ static int manager_find_user_config_paths(char ***ret_files, char ***ret_dirs) {
_cleanup_strv_free_ char **files = NULL, **dirs = NULL;
int r;
r = xdg_user_config_dir(&base, "/systemd");
r = xdg_user_config_dir("/systemd", &base);
if (r < 0)
return r;
@ -2479,7 +2479,7 @@ static int initialize_runtime(
/* Create the runtime directory and place the inaccessible device nodes there, if we run in
* user mode. In system mode mount_setup() already did that. */
r = xdg_user_runtime_dir(&p, "/systemd");
r = xdg_user_runtime_dir("/systemd", &p);
if (r < 0) {
*ret_error_message = "$XDG_RUNTIME_DIR is not set";
return log_struct_errno(LOG_EMERG, r,

View File

@ -1031,7 +1031,7 @@ int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags,
r = mkdir_label("/run/systemd/units", 0755);
else {
_cleanup_free_ char *units_path = NULL;
r = xdg_user_runtime_dir(&units_path, "/systemd/units");
r = xdg_user_runtime_dir("/systemd/units", &units_path);
if (r < 0)
return r;

View File

@ -5576,12 +5576,13 @@ static int unit_get_invocation_path(Unit *u, char **ret) {
p = strjoin("/run/systemd/units/invocation:", u->id);
else {
_cleanup_free_ char *user_path = NULL;
r = xdg_user_runtime_dir(&user_path, "/systemd/units/invocation:");
r = xdg_user_runtime_dir("/systemd/units/invocation:", &user_path);
if (r < 0)
return r;
p = strjoin(user_path, u->id);
}
if (!p)
return -ENOMEM;

View File

@ -44,7 +44,7 @@ sd_journal_sources += [audit_type_to_name]
############################################################
id128_sources = files(
sd_id128_sources = files(
'sd-id128/id128-util.c',
'sd-id128/sd-id128.c',
)
@ -62,6 +62,41 @@ sd_event_sources = files(
############################################################
sd_bus_sources = files(
'sd-bus/bus-common-errors.c',
'sd-bus/bus-container.c',
'sd-bus/bus-control.c',
'sd-bus/bus-convenience.c',
'sd-bus/bus-creds.c',
'sd-bus/bus-dump.c',
'sd-bus/bus-error.c',
'sd-bus/bus-internal.c',
'sd-bus/bus-introspect.c',
'sd-bus/bus-kernel.c',
'sd-bus/bus-match.c',
'sd-bus/bus-message.c',
'sd-bus/bus-objects.c',
'sd-bus/bus-signature.c',
'sd-bus/bus-slot.c',
'sd-bus/bus-socket.c',
'sd-bus/bus-track.c',
'sd-bus/bus-type.c',
'sd-bus/sd-bus.c',
)
############################################################
sd_device_sources = files(
'sd-device/device-enumerator.c',
'sd-device/device-filter.c',
'sd-device/device-monitor.c',
'sd-device/device-private.c',
'sd-device/device-util.c',
'sd-device/sd-device.c',
)
############################################################
sd_login_sources = files('sd-login/sd-login.c')
############################################################
@ -83,33 +118,14 @@ sd_varlink_sources = files(
############################################################
libsystemd_sources = files(
'sd-bus/bus-common-errors.c',
'sd-bus/bus-container.c',
'sd-bus/bus-control.c',
'sd-bus/bus-convenience.c',
'sd-bus/bus-creds.c',
'sd-bus/bus-dump.c',
'sd-bus/bus-error.c',
'sd-bus/bus-internal.c',
'sd-bus/bus-introspect.c',
'sd-bus/bus-kernel.c',
'sd-bus/bus-match.c',
'sd-bus/bus-message.c',
'sd-bus/bus-objects.c',
'sd-bus/bus-signature.c',
'sd-bus/bus-slot.c',
'sd-bus/bus-socket.c',
'sd-bus/bus-track.c',
'sd-bus/bus-type.c',
'sd-bus/sd-bus.c',
'sd-device/device-enumerator.c',
'sd-device/device-filter.c',
'sd-device/device-monitor.c',
'sd-device/device-private.c',
'sd-device/device-util.c',
'sd-device/sd-device.c',
'sd-hwdb/sd-hwdb.c',
sd_path_sources = files(
'sd-path/path-lookup.c',
'sd-path/sd-path.c',
)
############################################################
sd_netlink_sources = files(
'sd-netlink/netlink-genl.c',
'sd-netlink/netlink-message-nfnl.c',
'sd-netlink/netlink-message-rtnl.c',
@ -122,11 +138,24 @@ libsystemd_sources = files(
'sd-netlink/netlink-types.c',
'sd-netlink/netlink-util.c',
'sd-netlink/sd-netlink.c',
)
############################################################
sd_network_sources = files(
'sd-network/network-util.c',
'sd-network/sd-network.c',
'sd-path/sd-path.c',
)
############################################################
libsystemd_sources = files(
'sd-hwdb/sd-hwdb.c',
'sd-resolve/sd-resolve.c',
) + sd_journal_sources + id128_sources + sd_daemon_sources + sd_event_sources + sd_login_sources + sd_json_sources + sd_varlink_sources
) + sd_journal_sources + sd_id128_sources + sd_daemon_sources \
+ sd_event_sources + sd_bus_sources + sd_device_sources \
+ sd_login_sources + sd_json_sources + sd_varlink_sources \
+ sd_path_sources + sd_netlink_sources + sd_network_sources
libsystemd_c_args = ['-fvisibility=default']

View File

@ -4,8 +4,12 @@
#include <errno.h>
#include <stdbool.h>
#include "constants.h"
#include "macro.h"
#define NETWORK_DIRS ((const char* const*) CONF_PATHS_STRV("systemd/network"))
#define NETWORK_DIRS_NULSTR CONF_PATHS_NULSTR("systemd/network")
bool network_is_online(void);
typedef enum AddressFamily {

View File

@ -0,0 +1,724 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "alloc-util.h"
#include "fs-util.h"
#include "log.h"
#include "macro.h"
#include "nulstr-util.h"
#include "path-lookup.h"
#include "path-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "tmpfile-util.h"
#include "user-util.h"
int user_search_dirs(const char *suffix, char ***ret_config_dirs, char ***ret_data_dirs) {
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
int r;
assert(ret_config_dirs);
assert(ret_data_dirs);
r = sd_path_lookup_strv(SD_PATH_SEARCH_CONFIGURATION, suffix, &config_dirs);
if (r < 0)
return r;
r = sd_path_lookup_strv(SD_PATH_SEARCH_SHARED, suffix, &data_dirs);
if (r < 0)
return r;
*ret_config_dirs = TAKE_PTR(config_dirs);
*ret_data_dirs = TAKE_PTR(data_dirs);
return 0;
}
int runtime_directory(RuntimeScope scope, const char *suffix, char **ret) {
int r;
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER));
assert(suffix);
assert(ret);
/* Accept $RUNTIME_DIRECTORY as authoritative
* If it's missing, apply the suffix to /run/, or $XDG_RUNTIME_DIR if we are in a user runtime scope.
*
* Return value indicates whether the suffix was applied or not */
const char *e = secure_getenv("RUNTIME_DIRECTORY");
if (e)
return strdup_to(ret, e);
if (scope == RUNTIME_SCOPE_USER) {
r = xdg_user_runtime_dir(suffix, ret);
if (r < 0)
return r;
} else {
char *d = path_join("/run", suffix);
if (!d)
return -ENOMEM;
*ret = d;
}
return 1;
}
static const char* const user_data_unit_paths[] = {
"/usr/local/lib/systemd/user",
"/usr/local/share/systemd/user",
USER_DATA_UNIT_DIR,
"/usr/lib/systemd/user",
"/usr/share/systemd/user",
NULL
};
static const char* const user_config_unit_paths[] = {
USER_CONFIG_UNIT_DIR,
"/etc/systemd/user",
NULL
};
bool path_is_user_data_dir(const char *path) {
assert(path);
return path_strv_contains((char* const*) user_data_unit_paths, path);
}
bool path_is_user_config_dir(const char *path) {
assert(path);
return path_strv_contains((char* const*) user_config_unit_paths, path);
}
static int acquire_generator_dirs(
RuntimeScope scope,
const char *tempdir,
char **ret,
char **ret_early,
char **ret_late) {
_cleanup_free_ char *prefix_alloc = NULL, *g = NULL, *early = NULL, *late = NULL;
const char *prefix;
int r;
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
assert(ret);
assert(ret_early);
assert(ret_late);
if (scope == RUNTIME_SCOPE_GLOBAL)
return -EOPNOTSUPP;
if (tempdir)
prefix = tempdir;
else if (scope == RUNTIME_SCOPE_SYSTEM)
prefix = "/run/systemd";
else { /* RUNTIME_SCOPE_USER */
r = xdg_user_runtime_dir("/systemd", &prefix_alloc);
if (r < 0)
return r;
prefix = prefix_alloc;
}
g = path_join(prefix, "generator");
if (!g)
return -ENOMEM;
early = path_join(prefix, "generator.early");
if (!early)
return -ENOMEM;
late = path_join(prefix, "generator.late");
if (!late)
return -ENOMEM;
*ret = TAKE_PTR(g);
*ret_early = TAKE_PTR(early);
*ret_late = TAKE_PTR(late);
return 0;
}
static int acquire_transient_dir(RuntimeScope scope, const char *tempdir, char **ret) {
char *transient;
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
assert(ret);
if (scope == RUNTIME_SCOPE_GLOBAL)
return -EOPNOTSUPP;
if (tempdir)
transient = path_join(tempdir, "transient");
else if (scope == RUNTIME_SCOPE_SYSTEM)
transient = strdup("/run/systemd/transient");
else /* RUNTIME_SCOPE_USER */
return xdg_user_runtime_dir("/systemd/transient", ret);
if (!transient)
return -ENOMEM;
*ret = transient;
return 0;
}
typedef enum LookupDirType {
LOOKUP_DIR_CONFIG,
LOOKUP_DIR_CONTROL,
LOOKUP_DIR_ATTACHED,
_LOOKUP_DIR_MAX,
_LOOKUP_DIR_INVALID = -EINVAL,
} LookupDirType;
static int acquire_lookup_dirs(
LookupDirType type,
RuntimeScope scope,
char **ret_persistent,
char **ret_runtime) {
/* RUNTIME_SCOPE_USER dirs are relative to XDG_CONFIG_DIR and XDG_RUNTIME_DIR, respectively */
static const struct {
const char *persistent;
const char *runtime;
} dirs[_LOOKUP_DIR_MAX][_RUNTIME_SCOPE_MAX] = {
[LOOKUP_DIR_CONFIG] = {
[RUNTIME_SCOPE_SYSTEM] = { SYSTEM_CONFIG_UNIT_DIR, "/run/systemd/system" },
[RUNTIME_SCOPE_GLOBAL] = { USER_CONFIG_UNIT_DIR, "/run/systemd/user" },
[RUNTIME_SCOPE_USER] = { "systemd/user", "systemd/user" },
},
[LOOKUP_DIR_CONTROL] = {
[RUNTIME_SCOPE_SYSTEM] = { "/etc/systemd/system.control", "/run/systemd/system.control" },
[RUNTIME_SCOPE_USER] = { "systemd/user.control", "systemd/user.control" },
},
[LOOKUP_DIR_ATTACHED] = {
[RUNTIME_SCOPE_SYSTEM] = { "/etc/systemd/system.attached", "/run/systemd/system.attached" },
/* Portable services are not available to regular users for now. */
},
};
_cleanup_free_ char *a = NULL, *b = NULL;
int r;
assert(type >= 0 && type < _LOOKUP_DIR_MAX);
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
assert(ret_persistent);
assert(ret_runtime);
const char *persistent = dirs[type][scope].persistent;
const char *runtime = dirs[type][scope].runtime;
assert(!persistent == !runtime);
if (!persistent)
return -EOPNOTSUPP;
switch (scope) {
case RUNTIME_SCOPE_SYSTEM:
case RUNTIME_SCOPE_GLOBAL:
a = strdup(persistent);
b = strdup(runtime);
if (!a || !b)
return -ENOMEM;
*ret_persistent = TAKE_PTR(a);
*ret_runtime = TAKE_PTR(b);
return 0;
case RUNTIME_SCOPE_USER:
r = xdg_user_config_dir(persistent, &a);
if (r < 0)
return r;
r = xdg_user_runtime_dir(runtime, ret_runtime);
if (r < 0) {
if (r != -ENXIO)
return r;
/* If XDG_RUNTIME_DIR is not set, don't consider that fatal, simply initialize
* the runtime directory to NULL. */
*ret_runtime = NULL;
}
*ret_persistent = TAKE_PTR(a);
return 0;
default:
assert_not_reached();
}
}
static int patch_root_prefix(char **p, const char *root_dir) {
char *c;
assert(p);
if (!root_dir)
return 0;
if (!*p)
return 0;
c = path_join(root_dir, *p);
if (!c)
return -ENOMEM;
free_and_replace(*p, c);
return 0;
}
static int patch_root_prefix_strv(char **l, const char *root_dir) {
int r;
if (!root_dir)
return 0;
STRV_FOREACH(i, l) {
r = patch_root_prefix(i, root_dir);
if (r < 0)
return r;
}
return 0;
}
static int get_paths_from_environ(const char *var, char ***ret) {
const char *e;
int r;
assert(var);
assert(ret);
e = getenv(var);
if (!e) {
*ret = NULL;
return 0;
}
bool append = endswith(e, ":"); /* Whether to append the normal search paths after what's obtained
from envvar */
/* FIXME: empty components in other places should be rejected. */
r = path_split_and_make_absolute(e, ret);
if (r < 0)
return r;
return append;
}
static char** user_unit_search_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) {
_cleanup_strv_free_ char **paths = NULL, **config_dirs = NULL, **data_dirs = NULL;
/* The returned strv might contain duplicates, and we expect caller to filter them. */
assert(persistent_config);
assert(global_persistent_config);
assert(global_runtime_config);
assert(persistent_control);
if (user_search_dirs("/systemd/user", &config_dirs, &data_dirs) < 0)
return NULL;
paths = strv_new(persistent_control,
STRV_IFNOTNULL(runtime_control),
STRV_IFNOTNULL(transient),
STRV_IFNOTNULL(generator_early),
persistent_config);
if (!paths)
return NULL;
if (strv_extend_strv_consume(&paths, TAKE_PTR(config_dirs), /* filter_duplicates = */ false) < 0)
return NULL;
/* global config has lower priority than the user config of the same type */
if (strv_extend(&paths, global_persistent_config) < 0)
return NULL;
if (strv_extend_strv(&paths, (char* const*) user_config_unit_paths, /* filter_duplicates = */ false) < 0)
return NULL;
/* strv_extend_many() can deal with NULL-s in arguments */
if (strv_extend_many(&paths,
runtime_config,
global_runtime_config,
generator) < 0)
return NULL;
if (strv_extend_strv_consume(&paths, TAKE_PTR(data_dirs), /* filter_duplicates = */ false) < 0)
return NULL;
if (strv_extend_strv(&paths, (char* const*) user_data_unit_paths, false) < 0)
return NULL;
if (strv_extend(&paths, generator_late) < 0)
return NULL;
if (path_strv_make_absolute_cwd(paths) < 0)
return NULL;
return TAKE_PTR(paths);
}
int lookup_paths_init(
LookupPaths *lp,
RuntimeScope scope,
LookupPathsFlags flags,
const char *root_dir) {
_cleanup_(rmdir_and_freep) char *tempdir = NULL;
_cleanup_free_ char
*root = NULL,
*persistent_config = NULL, *runtime_config = NULL,
*global_persistent_config = NULL, *global_runtime_config = NULL,
*generator = NULL, *generator_early = NULL, *generator_late = NULL,
*transient = NULL,
*persistent_control = NULL, *runtime_control = NULL,
*persistent_attached = NULL, *runtime_attached = NULL;
_cleanup_strv_free_ char **paths = NULL;
int r;
assert(lp);
assert(scope >= 0);
assert(scope < _RUNTIME_SCOPE_MAX);
assert(!FLAGS_SET(flags, LOOKUP_PATHS_EXCLUDE_GENERATED|LOOKUP_PATHS_TEMPORARY_GENERATED));
if (!empty_or_root(root_dir)) {
if (scope == RUNTIME_SCOPE_USER)
return -EINVAL;
r = is_dir(root_dir, true);
if (r < 0)
return r;
if (r == 0)
return -ENOTDIR;
root = strdup(root_dir);
if (!root)
return -ENOMEM;
}
if (FLAGS_SET(flags, LOOKUP_PATHS_TEMPORARY_GENERATED)) {
r = mkdtemp_malloc("/tmp/systemd-temporary-XXXXXX", &tempdir);
if (r < 0)
return log_debug_errno(r, "Failed to create temporary directory: %m");
}
/* Note: when XDG_RUNTIME_DIR is not set this will not return -ENXIO, but simply set runtime_config to NULL */
r = acquire_lookup_dirs(LOOKUP_DIR_CONFIG, scope, &persistent_config, &runtime_config);
if (r < 0)
return r;
if (scope == RUNTIME_SCOPE_USER) {
r = acquire_lookup_dirs(LOOKUP_DIR_CONFIG, RUNTIME_SCOPE_GLOBAL, &global_persistent_config, &global_runtime_config);
if (r < 0)
return r;
}
r = acquire_lookup_dirs(LOOKUP_DIR_CONTROL, scope, &persistent_control, &runtime_control);
if (r < 0 && r != -EOPNOTSUPP)
return r;
r = acquire_lookup_dirs(LOOKUP_DIR_ATTACHED, scope, &persistent_attached, &runtime_attached);
if (r < 0 && r != -EOPNOTSUPP)
return r;
if (!FLAGS_SET(flags, LOOKUP_PATHS_EXCLUDE_GENERATED)) {
/* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
r = acquire_generator_dirs(scope, tempdir,
&generator, &generator_early, &generator_late);
if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
return r;
}
/* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
r = acquire_transient_dir(scope, tempdir, &transient);
if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENXIO))
return r;
/* First priority is whatever has been passed to us via env vars */
r = get_paths_from_environ("SYSTEMD_UNIT_PATH", &paths);
if (r < 0)
return r;
if (!paths || r > 0) {
/* Let's figure something out. */
_cleanup_strv_free_ char **add = NULL;
/* For the user units we include share/ in the search paths in order to comply with
* the XDG basedir spec. For the system stuff we avoid such nonsense. OTOH we include (/usr/)lib/
* in the search paths for the system stuff but avoid it for user stuff. */
assert(persistent_config);
switch (scope) {
case RUNTIME_SCOPE_SYSTEM:
/* If you modify this you also want to modify systemdsystemunitpath= in systemd.pc.in! */
add = strv_new(ASSERT_PTR(persistent_control),
ASSERT_PTR(runtime_control),
ASSERT_PTR(transient),
STRV_IFNOTNULL(generator_early),
persistent_config,
SYSTEM_CONFIG_UNIT_DIR,
"/etc/systemd/system",
ASSERT_PTR(persistent_attached),
ASSERT_PTR(runtime_config),
"/run/systemd/system",
ASSERT_PTR(runtime_attached),
STRV_IFNOTNULL(generator),
"/usr/local/lib/systemd/system",
SYSTEM_DATA_UNIT_DIR,
"/usr/lib/systemd/system",
/* To be used ONLY for images which might be legacy split-usr */
FLAGS_SET(flags, LOOKUP_PATHS_SPLIT_USR) ? "/lib/systemd/system" : STRV_IGNORE,
STRV_IFNOTNULL(generator_late));
break;
case RUNTIME_SCOPE_GLOBAL:
/* If you modify this you also want to modify systemduserunitpath= in systemd.pc.in,
* and RUNTIME_SCOPE_USER search paths below! */
assert(!persistent_control);
assert(!runtime_control);
assert(!transient);
assert(!generator_early);
assert(!generator);
assert(!generator_late);
add = strv_new(persistent_config,
USER_CONFIG_UNIT_DIR,
"/etc/systemd/user",
ASSERT_PTR(runtime_config),
"/run/systemd/user",
"/usr/local/share/systemd/user",
"/usr/share/systemd/user",
"/usr/local/lib/systemd/user",
USER_DATA_UNIT_DIR,
"/usr/lib/systemd/user");
break;
case RUNTIME_SCOPE_USER:
add = user_unit_search_dirs(persistent_config, runtime_config,
global_persistent_config, global_runtime_config,
generator, generator_early, generator_late,
transient,
persistent_control, runtime_control);
break;
default:
assert_not_reached();
}
if (!add)
return -ENOMEM;
/* strv_uniq() below would filter all duplicates against the final strv */
r = strv_extend_strv_consume(&paths, TAKE_PTR(add), /* filter_duplicates = */ false);
if (r < 0)
return r;
}
r = patch_root_prefix(&persistent_config, root);
if (r < 0)
return r;
r = patch_root_prefix(&runtime_config, root);
if (r < 0)
return r;
r = patch_root_prefix(&generator, root);
if (r < 0)
return r;
r = patch_root_prefix(&generator_early, root);
if (r < 0)
return r;
r = patch_root_prefix(&generator_late, root);
if (r < 0)
return r;
r = patch_root_prefix(&transient, root);
if (r < 0)
return r;
r = patch_root_prefix(&persistent_control, root);
if (r < 0)
return r;
r = patch_root_prefix(&runtime_control, root);
if (r < 0)
return r;
r = patch_root_prefix(&persistent_attached, root);
if (r < 0)
return r;
r = patch_root_prefix(&runtime_attached, root);
if (r < 0)
return r;
r = patch_root_prefix_strv(paths, root);
if (r < 0)
return -ENOMEM;
*lp = (LookupPaths) {
.search_path = strv_uniq(TAKE_PTR(paths)),
.persistent_config = TAKE_PTR(persistent_config),
.runtime_config = TAKE_PTR(runtime_config),
.generator = TAKE_PTR(generator),
.generator_early = TAKE_PTR(generator_early),
.generator_late = TAKE_PTR(generator_late),
.transient = TAKE_PTR(transient),
.persistent_control = TAKE_PTR(persistent_control),
.runtime_control = TAKE_PTR(runtime_control),
.persistent_attached = TAKE_PTR(persistent_attached),
.runtime_attached = TAKE_PTR(runtime_attached),
.root_dir = TAKE_PTR(root),
.temporary_dir = TAKE_PTR(tempdir),
};
return 0;
}
int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir) {
int r;
r = lookup_paths_init(lp, scope, flags, root_dir);
if (r < 0)
return log_error_errno(r, "Failed to initialize unit search paths%s%s: %m",
isempty(root_dir) ? "" : " for root directory ", strempty(root_dir));
return r;
}
void lookup_paths_done(LookupPaths *lp) {
assert(lp);
lp->search_path = strv_free(lp->search_path);
lp->persistent_config = mfree(lp->persistent_config);
lp->runtime_config = mfree(lp->runtime_config);
lp->persistent_attached = mfree(lp->persistent_attached);
lp->runtime_attached = mfree(lp->runtime_attached);
lp->generator = mfree(lp->generator);
lp->generator_early = mfree(lp->generator_early);
lp->generator_late = mfree(lp->generator_late);
lp->transient = mfree(lp->transient);
lp->persistent_control = mfree(lp->persistent_control);
lp->runtime_control = mfree(lp->runtime_control);
lp->root_dir = mfree(lp->root_dir);
lp->temporary_dir = mfree(lp->temporary_dir);
}
void lookup_paths_log(LookupPaths *lp) {
assert(lp);
if (strv_isempty(lp->search_path)) {
log_debug("Ignoring unit files.");
lp->search_path = strv_free(lp->search_path);
} else {
_cleanup_free_ char *t = NULL;
t = strv_join(lp->search_path, "\n\t");
log_debug("Looking for unit files in (higher priority first):\n\t%s", strna(t));
}
}
static const char* const system_generator_paths[] = {
"/run/systemd/system-generators",
"/etc/systemd/system-generators",
"/usr/local/lib/systemd/system-generators",
SYSTEM_GENERATOR_DIR,
NULL,
};
static const char* const user_generator_paths[] = {
"/run/systemd/user-generators",
"/etc/systemd/user-generators",
"/usr/local/lib/systemd/user-generators",
USER_GENERATOR_DIR,
NULL,
};
static const char* const system_env_generator_paths[] = {
"/run/systemd/system-environment-generators",
"/etc/systemd/system-environment-generators",
"/usr/local/lib/systemd/system-environment-generators",
SYSTEM_ENV_GENERATOR_DIR,
NULL,
};
static const char* const user_env_generator_paths[] = {
"/run/systemd/user-environment-generators",
"/etc/systemd/user-environment-generators",
"/usr/local/lib/systemd/user-environment-generators",
USER_ENV_GENERATOR_DIR,
NULL,
};
char** generator_binary_paths_internal(RuntimeScope scope, bool env_generator) {
static const struct {
const char *env_name;
const char * const *paths[_RUNTIME_SCOPE_MAX];
} unit_generator = {
"SYSTEMD_GENERATOR_PATH",
{
[RUNTIME_SCOPE_SYSTEM] = system_generator_paths,
[RUNTIME_SCOPE_USER] = user_generator_paths,
}
}, environment_generator = {
"SYSTEMD_ENVIRONMENT_GENERATOR_PATH",
{
[RUNTIME_SCOPE_SYSTEM] = system_env_generator_paths,
[RUNTIME_SCOPE_USER] = user_env_generator_paths,
}
};
_cleanup_strv_free_ char **paths = NULL;
int r;
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER));
const char *env_name = ASSERT_PTR((env_generator ? environment_generator : unit_generator).env_name);
const char * const *generator_paths = ASSERT_PTR((env_generator ? environment_generator : unit_generator).paths[scope]);
/* First priority is whatever has been passed to us via env vars */
r = get_paths_from_environ(env_name, &paths);
if (r < 0)
return NULL;
if (!paths || r > 0) {
r = strv_extend_strv(&paths, (char* const*) generator_paths, /* filter_duplicates = */ true);
if (r < 0)
return NULL;
}
return TAKE_PTR(paths);
}

View File

@ -3,8 +3,8 @@
#include <stdbool.h>
#include "constants.h"
#include "macro.h"
#include "sd-path.h"
#include "runtime-scope.h"
typedef enum LookupPathsFlags {
@ -55,23 +55,32 @@ typedef struct LookupPaths {
int lookup_paths_init(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir);
int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir);
int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs);
int xdg_user_runtime_dir(char **ret, const char *suffix);
int xdg_user_config_dir(char **ret, const char *suffix);
int xdg_user_data_dir(char **ret, const char *suffix);
int runtime_directory(char **ret, RuntimeScope scope, const char *suffix);
void lookup_paths_log(LookupPaths *p);
void lookup_paths_done(LookupPaths *p);
int runtime_directory(RuntimeScope scope, const char *suffix, char **ret);
/* We don't treat /etc/xdg/systemd/ in these functions as the xdg base dir spec suggests because we assume
* that is a link to /etc/systemd/ anyway. */
int user_search_dirs(const char *suffix, char ***ret_config_dirs, char ***ret_data_dirs);
static inline int xdg_user_runtime_dir(const char *suffix, char **ret) {
return sd_path_lookup(SD_PATH_USER_RUNTIME, suffix, ret);
}
static inline int xdg_user_config_dir(const char *suffix, char **ret) {
return sd_path_lookup(SD_PATH_USER_CONFIGURATION, suffix, ret);
}
static inline int xdg_user_data_dir(const char *suffix, char **ret) {
return sd_path_lookup(SD_PATH_USER_SHARED, suffix, ret);
}
bool path_is_user_data_dir(const char *path);
bool path_is_user_config_dir(const char *path);
void lookup_paths_log(LookupPaths *p);
void lookup_paths_done(LookupPaths *p);
char **generator_binary_paths(RuntimeScope scope);
char **env_generator_binary_paths(RuntimeScope scope);
#define NETWORK_DIRS ((const char* const*) CONF_PATHS_STRV("systemd/network"))
#define NETWORK_DIRS_NULSTR CONF_PATHS_NULSTR("systemd/network")
#define PORTABLE_PROFILE_DIRS CONF_PATHS_NULSTR("systemd/portable/profile")
int find_portable_profile(const char *name, const char *unit, char **ret_path);
char** generator_binary_paths_internal(RuntimeScope scope, bool env_generator);
static inline char** generator_binary_paths(RuntimeScope runtime_scope) {
return generator_binary_paths_internal(runtime_scope, false);
}
static inline char** env_generator_binary_paths(RuntimeScope runtime_scope) {
return generator_binary_paths_internal(runtime_scope, true);
}

View File

@ -7,6 +7,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "network-util.h"
#include "nulstr-util.h"
#include "path-lookup.h"
#include "path-util.h"
@ -580,25 +581,16 @@ static int get_search(uint64_t type, char ***ret) {
}
case SD_PATH_SYSTEMD_SEARCH_SYSTEM_GENERATOR:
case SD_PATH_SYSTEMD_SEARCH_USER_GENERATOR: {
RuntimeScope scope = type == SD_PATH_SYSTEMD_SEARCH_SYSTEM_GENERATOR ?
RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER;
char **t;
t = generator_binary_paths(scope);
if (!t)
return -ENOMEM;
*ret = t;
return 0;
}
case SD_PATH_SYSTEMD_SEARCH_USER_GENERATOR:
case SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR:
case SD_PATH_SYSTEMD_SEARCH_USER_ENVIRONMENT_GENERATOR: {
char **t;
RuntimeScope scope = IN_SET(type, SD_PATH_SYSTEMD_SEARCH_SYSTEM_GENERATOR,
SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR) ?
RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER;
bool env_generator = IN_SET(type, SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR,
SD_PATH_SYSTEMD_SEARCH_USER_ENVIRONMENT_GENERATOR);
t = env_generator_binary_paths(type == SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR ?
RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER);
char **t = generator_binary_paths_internal(scope, env_generator);
if (!t)
return -ENOMEM;

View File

@ -29,6 +29,7 @@
#include "netdevsim.h"
#include "netif-util.h"
#include "netlink-util.h"
#include "network-util.h"
#include "networkd-manager.h"
#include "networkd-queue.h"
#include "networkd-setlink.h"

View File

@ -15,6 +15,7 @@
#include "edit-util.h"
#include "mkdir-label.h"
#include "netlink-util.h"
#include "network-util.h"
#include "networkctl.h"
#include "networkctl-config-file.h"
#include "networkctl-util.h"

View File

@ -15,6 +15,7 @@
#include "in-addr-util.h"
#include "net-condition.h"
#include "netdev/macvlan.h"
#include "network-util.h"
#include "networkd-address-label.h"
#include "networkd-address.h"
#include "networkd-bridge-fdb.h"

View File

@ -32,6 +32,7 @@
#include "os-util.h"
#include "path-lookup.h"
#include "portable.h"
#include "portable-util.h"
#include "process-util.h"
#include "rm-rf.h"
#include "selinux-util.h"

View File

@ -144,6 +144,7 @@ shared_sources = files(
'pkcs11-util.c',
'plymouth-util.c',
'polkit-agent.c',
'portable-util.c',
'pretty-print.c',
'capsule-util.c',
'ptyfwd.c',
@ -170,6 +171,7 @@ shared_sources = files(
'tpm2-util.c',
'tpm2-event-log.c',
'udev-util.c',
'unit-file.c',
'user-record-nss.c',
'user-record-show.c',
'user-record.c',

View File

@ -0,0 +1,34 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "fs-util.h"
#include "nulstr-util.h"
#include "portable-util.h"
#include "string-util.h"
int find_portable_profile(const char *name, const char *unit, char **ret_path) {
const char *dot;
int r;
assert(name);
assert(ret_path);
assert_se(dot = strrchr(unit, '.'));
NULSTR_FOREACH(p, PORTABLE_PROFILE_DIRS) {
_cleanup_free_ char *joined = NULL;
joined = strjoin(p, "/", name, "/", dot + 1, ".conf");
if (!joined)
return -ENOMEM;
r = access_nofollow(joined, F_OK);
if (r >= 0) {
*ret_path = TAKE_PTR(joined);
return 0;
}
if (r != -ENOENT)
return r;
}
return -ENOENT;
}

View File

@ -0,0 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "constants.h"
#include "macro.h"
#define PORTABLE_PROFILE_DIRS CONF_PATHS_NULSTR("systemd/portable/profile")
int find_portable_profile(const char *name, const char *unit, char **ret_path);

View File

@ -16,25 +16,6 @@
#include "strv.h"
#include "unit-file.h"
bool unit_type_may_alias(UnitType type) {
return IN_SET(type,
UNIT_SERVICE,
UNIT_SOCKET,
UNIT_TARGET,
UNIT_DEVICE,
UNIT_TIMER,
UNIT_PATH);
}
bool unit_type_may_template(UnitType type) {
return IN_SET(type,
UNIT_SERVICE,
UNIT_SOCKET,
UNIT_TARGET,
UNIT_TIMER,
UNIT_PATH);
}
int unit_symlink_name_compatible(const char *symlink, const char *target, bool instance_propagation) {
_cleanup_free_ char *template = NULL;
int r, un_type1, un_type2;

View File

@ -28,8 +28,24 @@ enum UnitFileState {
_UNIT_FILE_STATE_INVALID = -EINVAL,
};
bool unit_type_may_alias(UnitType type) _const_;
bool unit_type_may_template(UnitType type) _const_;
static inline bool unit_type_may_alias(UnitType type) {
return IN_SET(type,
UNIT_SERVICE,
UNIT_SOCKET,
UNIT_TARGET,
UNIT_DEVICE,
UNIT_TIMER,
UNIT_PATH);
}
static inline bool unit_type_may_template(UnitType type) {
return IN_SET(type,
UNIT_SERVICE,
UNIT_SOCKET,
UNIT_TARGET,
UNIT_TIMER,
UNIT_PATH);
}
int unit_symlink_name_compatible(const char *symlink, const char *target, bool instance_propagation);
int unit_validate_alias_symlink_or_warn(int log_level, const char *filename, const char *target);

View File

@ -349,49 +349,35 @@ static int log_unresolvable_specifier(const char *filename, unsigned line) {
arg_dry_run ? (would) : (doing), \
__VA_ARGS__)
static int user_config_paths(char*** ret) {
static int user_config_paths(char ***ret) {
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
_cleanup_free_ char *persistent_config = NULL, *runtime_config = NULL, *data_home = NULL;
_cleanup_strv_free_ char **res = NULL;
_cleanup_free_ char *runtime_config = NULL;
int r;
r = xdg_user_dirs(&config_dirs, &data_dirs);
assert(ret);
/* Combined user-specific and global dirs */
r = user_search_dirs("/user-tmpfiles.d", &config_dirs, &data_dirs);
if (r < 0)
return r;
r = xdg_user_config_dir(&persistent_config, "/user-tmpfiles.d");
r = xdg_user_runtime_dir("/user-tmpfiles.d", &runtime_config);
if (r < 0 && !ERRNO_IS_NEG_NOINFO(r))
return r;
r = xdg_user_runtime_dir(&runtime_config, "/user-tmpfiles.d");
if (r < 0 && !ERRNO_IS_NEG_NOINFO(r))
return r;
r = xdg_user_data_dir(&data_home, "/user-tmpfiles.d");
if (r < 0 && !ERRNO_IS_NEG_NOINFO(r))
return r;
r = strv_extend_strv_concat(&res, (const char* const*) config_dirs, "/user-tmpfiles.d");
r = strv_consume(&config_dirs, TAKE_PTR(runtime_config));
if (r < 0)
return r;
r = strv_extend_many(
&res,
persistent_config,
runtime_config,
data_home);
r = strv_extend_strv_consume(&config_dirs, TAKE_PTR(data_dirs), /* filter_duplicates = */ true);
if (r < 0)
return r;
r = strv_extend_strv_concat(&res, (const char* const*) data_dirs, "/user-tmpfiles.d");
r = path_strv_make_absolute_cwd(config_dirs);
if (r < 0)
return r;
r = path_strv_make_absolute_cwd(res);
if (r < 0)
return r;
*ret = TAKE_PTR(res);
*ret = TAKE_PTR(config_dirs);
return 0;
}

View File

@ -27,6 +27,7 @@
#include "netif-sriov.h"
#include "netif-util.h"
#include "netlink-util.h"
#include "network-util.h"
#include "parse-util.h"
#include "path-lookup.h"
#include "path-util.h"

View File

@ -212,7 +212,7 @@ static int get_firmware_search_dirs(char ***ret) {
* Prioritising entries in "more specific" directories */
_cleanup_free_ char *user_firmware_dir = NULL;
r = xdg_user_config_dir(&user_firmware_dir, "/qemu/firmware");
r = xdg_user_config_dir("/qemu/firmware", &user_firmware_dir);
if (r < 0)
return r;

View File

@ -1488,11 +1488,11 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
/* if we are going to be starting any units with state then create our runtime dir */
if (arg_tpm != 0 || arg_directory || arg_runtime_mounts.n_mounts != 0) {
r = runtime_directory(&arg_runtime_directory, arg_privileged ? RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER, "systemd/vmspawn");
r = runtime_directory(arg_privileged ? RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER, "systemd/vmspawn",
&arg_runtime_directory);
if (r < 0)
return log_error_errno(r, "Failed to lookup runtime directory: %m");
if (r) {
/* r > 0 means we need to create our own runtime dir */
if (r > 0) { /* We need to create our own runtime dir */
r = mkdir_p(arg_runtime_directory, 0755);
if (r < 0)
return log_error_errno(r, "Failed to create runtime directory: %m");

View File

@ -20,6 +20,39 @@
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(xdgautostartservice_hash_ops, char, string_hash_func, string_compare_func, XdgAutostartService, xdg_autostart_service_free);
static int xdg_base_dirs(char ***ret_config_dirs, char ***ret_data_dirs) {
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
const char *e;
/* Implement the mechanisms defined in
* https://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html */
assert(ret_config_dirs);
assert(ret_data_dirs);
e = getenv("XDG_CONFIG_DIRS");
if (e)
config_dirs = strv_split(e, ":");
else
config_dirs = strv_new("/etc/xdg");
if (!config_dirs)
return -ENOMEM;
e = getenv("XDG_DATA_DIRS");
if (e)
data_dirs = strv_split(e, ":");
else
data_dirs = strv_new("/usr/local/share",
"/usr/share");
if (!data_dirs)
return -ENOMEM;
*ret_config_dirs = TAKE_PTR(config_dirs);
*ret_data_dirs = TAKE_PTR(data_dirs);
return 0;
}
static int enumerate_xdg_autostart(Hashmap *all_services) {
_cleanup_strv_free_ char **autostart_dirs = NULL;
_cleanup_strv_free_ char **config_dirs = NULL;
@ -27,14 +60,14 @@ static int enumerate_xdg_autostart(Hashmap *all_services) {
_cleanup_free_ char *user_config_autostart_dir = NULL;
int r;
r = xdg_user_config_dir(&user_config_autostart_dir, "/autostart");
r = xdg_user_config_dir("/autostart", &user_config_autostart_dir);
if (r < 0)
return r;
r = strv_extend(&autostart_dirs, user_config_autostart_dir);
if (r < 0)
return r;
r = xdg_user_dirs(&config_dirs, &data_dirs);
r = xdg_base_dirs(&config_dirs, &data_dirs);
if (r < 0)
return r;
r = strv_extend_strv_concat(&autostart_dirs, (const char* const*) config_dirs, "/autostart");