path-lookup: add runtime_directory_generic() helper

This commit is contained in:
Lennart Poettering
2025-07-15 12:34:39 +02:00
parent abf518a8dc
commit 0011ecd6fa
2 changed files with 39 additions and 16 deletions

View File

@@ -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;
}

View File

@@ -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. */