diff --git a/src/libsystemd/sd-path/path-lookup.c b/src/libsystemd/sd-path/path-lookup.c index 55d9d4353a..3ae217a7c5 100644 --- a/src/libsystemd/sd-path/path-lookup.c +++ b/src/libsystemd/sd-path/path-lookup.c @@ -33,32 +33,54 @@ 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 + /* This does not bother with $RUNTIME_DIRECTORY, and hence can be applied to get other service's + * runtime dir */ + + switch (scope) { + case RUNTIME_SCOPE_USER: + r = xdg_user_runtime_dir(suffix, ret); + if (r < 0) + return r; + 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 */ + * 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; - } + 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 8457053339..b706e1a6df 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. */