From: Lennart Poettering Date: Tue, 15 Jul 2025 10:34:39 +0000 (+0200) Subject: path-lookup: add runtime_directory_generic() helper X-Git-Tag: v259-rc1~433^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0011ecd6fac1a7014648ae87975d9723f7fb5e28;p=thirdparty%2Fsystemd.git path-lookup: add runtime_directory_generic() helper --- diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 55d9d4353a1..3ae217a7c5e 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -33,33 +33,55 @@ int user_search_dirs(const char *suffix, char ***ret_config_dirs, char ***ret_da return 0; } -int runtime_directory(RuntimeScope scope, const char *suffix, char **ret) { +int runtime_directory_generic(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 */ + /* This does not bother with $RUNTIME_DIRECTORY, and hence can be applied to get other service's + * runtime dir */ - const char *e = secure_getenv("RUNTIME_DIRECTORY"); - if (e) - return strdup_to(ret, e); - - if (scope == RUNTIME_SCOPE_USER) { + switch (scope) { + case RUNTIME_SCOPE_USER: r = xdg_user_runtime_dir(suffix, ret); if (r < 0) return r; - } else { + break; + + case RUNTIME_SCOPE_SYSTEM: { char *d = path_join("/run", suffix); if (!d) return -ENOMEM; *ret = d; + break; + } + + default: + return -EINVAL; } + return 0; +} + +int runtime_directory(RuntimeScope scope, const char *fallback_suffix, char **ret) { + int r; + + assert(ret); + + /* Accept $RUNTIME_DIRECTORY as authoritative, i.e. only works for our service's own runtime dir. + * + * 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); + + r = runtime_directory_generic(scope, fallback_suffix, ret); + if (r < 0) + return r; + return 1; } diff --git a/src/libsystemd/sd-path/path-lookup.h b/src/libsystemd/sd-path/path-lookup.h index 84570533394..b706e1a6dff 100644 --- a/src/libsystemd/sd-path/path-lookup.h +++ b/src/libsystemd/sd-path/path-lookup.h @@ -57,7 +57,8 @@ int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFl void lookup_paths_log(LookupPaths *p); void lookup_paths_done(LookupPaths *p); -int runtime_directory(RuntimeScope scope, const char *suffix, char **ret); +int runtime_directory_generic(RuntimeScope scope, const char *suffix, char **ret); +int runtime_directory(RuntimeScope scope, const char *fallback_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. */