]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
path-lookup: add runtime_directory_generic() helper
authorLennart Poettering <lennart@poettering.net>
Tue, 15 Jul 2025 10:34:39 +0000 (12:34 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 25 Sep 2025 20:43:59 +0000 (22:43 +0200)
src/libsystemd/sd-path/path-lookup.c
src/libsystemd/sd-path/path-lookup.h

index 55d9d4353a1de389f95e6441bc039cb00f02698e..3ae217a7c5e5db44d8c839e5c8d523ab6703def2 100644 (file)
@@ -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;
 }
 
index 84570533394fa91aa0b2c66bf83e594dd2e85eb1..b706e1a6dff918d4f952d696f2ab0cfd6715bd59 100644 (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. */