]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/path-lookup.c
build-sys: use #if Y instead of #ifdef Y everywhere
[thirdparty/systemd.git] / src / shared / path-lookup.c
index fead755f8722f740f6459c8106c4dce1a970fe00..68c392e1c0edcc5f9fe1109620e49b1da84cd223 100644 (file)
@@ -23,6 +23,8 @@
 #include <string.h>
 
 #include "alloc-util.h"
+#include "fileio.h"
+#include "fs-util.h"
 #include "install.h"
 #include "log.h"
 #include "macro.h"
@@ -33,6 +35,7 @@
 #include "stat-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "user-util.h"
 #include "util.h"
 
 static int user_runtime_dir(char **ret, const char *suffix) {
@@ -57,6 +60,7 @@ static int user_runtime_dir(char **ret, const char *suffix) {
 static int user_config_dir(char **ret, const char *suffix) {
         const char *e;
         char *j;
+        int r;
 
         assert(ret);
 
@@ -64,11 +68,11 @@ static int user_config_dir(char **ret, const char *suffix) {
         if (e)
                 j = strappend(e, suffix);
         else {
-                const char *home;
+                _cleanup_free_ char *home = NULL;
 
-                home = getenv("HOME");
-                if (!home)
-                        return -ENXIO;
+                r = get_home_dir(&home);
+                if (r < 0)
+                        return r;
 
                 j = strjoin(home, "/.config", suffix);
         }
@@ -83,6 +87,7 @@ static int user_config_dir(char **ret, const char *suffix) {
 static int user_data_dir(char **ret, const char *suffix) {
         const char *e;
         char *j;
+        int r;
 
         assert(ret);
         assert(suffix);
@@ -95,12 +100,11 @@ static int user_data_dir(char **ret, const char *suffix) {
         if (e)
                 j = strappend(e, suffix);
         else {
-                const char *home;
-
-                home = getenv("HOME");
-                if (!home)
-                        return -ENXIO;
+                _cleanup_free_ char *home = NULL;
 
+                r = get_home_dir(&home);
+                if (r < 0)
+                        return r;
 
                 j = strjoin(home, "/.local/share", suffix);
         }
@@ -111,6 +115,21 @@ static int user_data_dir(char **ret, const char *suffix) {
         return 1;
 }
 
+static const char* const user_data_unit_paths[] = {
+        "/usr/local/lib/systemd/user",
+        "/usr/local/share/systemd/user",
+        USER_DATA_UNIT_PATH,
+        "/usr/lib/systemd/user",
+        "/usr/share/systemd/user",
+        NULL
+};
+
+static const char* const user_config_unit_paths[] = {
+        USER_CONFIG_UNIT_PATH,
+        "/etc/systemd/user",
+        NULL
+};
+
 static char** user_dirs(
                 const char *persistent_config,
                 const char *runtime_config,
@@ -121,25 +140,10 @@ static char** user_dirs(
                 const char *persistent_control,
                 const char *runtime_control) {
 
-        const char * const config_unit_paths[] = {
-                USER_CONFIG_UNIT_PATH,
-                "/etc/systemd/user",
-                NULL
-        };
-
-        const char * const data_unit_paths[] = {
-                "/usr/local/lib/systemd/user",
-                "/usr/local/share/systemd/user",
-                USER_DATA_UNIT_PATH,
-                "/usr/lib/systemd/user",
-                "/usr/share/systemd/user",
-                NULL
-        };
-
-        const char *e;
         _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
         _cleanup_free_ char *data_home = NULL;
         _cleanup_strv_free_ char **res = NULL;
+        const char *e;
         char **tmp;
         int r;
 
@@ -186,14 +190,13 @@ static char** user_dirs(
         if (strv_extend(&res, generator_early) < 0)
                 return NULL;
 
-        if (!strv_isempty(config_dirs))
-                if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
-                        return NULL;
+        if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
+                return NULL;
 
         if (strv_extend(&res, persistent_config) < 0)
                 return NULL;
 
-        if (strv_extend_strv(&res, (char**) config_unit_paths, false) < 0)
+        if (strv_extend_strv(&res, (char**) user_config_unit_paths, false) < 0)
                 return NULL;
 
         if (strv_extend(&res, runtime_config) < 0)
@@ -205,11 +208,10 @@ static char** user_dirs(
         if (strv_extend(&res, data_home) < 0)
                 return NULL;
 
-        if (!strv_isempty(data_dirs))
-                if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
-                        return NULL;
+        if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
+                return NULL;
 
-        if (strv_extend_strv(&res, (char**) data_unit_paths, false) < 0)
+        if (strv_extend_strv(&res, (char**) user_data_unit_paths, false) < 0)
                 return NULL;
 
         if (strv_extend(&res, generator_late) < 0)
@@ -220,55 +222,66 @@ static char** user_dirs(
 
         tmp = res;
         res = NULL;
+
         return tmp;
 }
 
+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(
                 UnitFileScope scope,
+                const char *tempdir,
                 char **generator,
                 char **generator_early,
                 char **generator_late) {
 
+        _cleanup_(rmdir_and_freep) char *t = NULL;
         _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL;
         const char *prefix;
 
         assert(generator);
         assert(generator_early);
         assert(generator_late);
+        assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER, UNIT_FILE_GLOBAL));
 
-        switch (scope) {
+        if (scope == UNIT_FILE_GLOBAL)
+                return -EOPNOTSUPP;
 
-        case UNIT_FILE_SYSTEM:
-                prefix = "/run/systemd/";
-                break;
+        if (tempdir)
+                prefix = tempdir;
 
-        case UNIT_FILE_USER: {
+        else if (scope == UNIT_FILE_SYSTEM)
+                prefix = "/run/systemd";
+
+        else if (scope == UNIT_FILE_USER) {
                 const char *e;
 
                 e = getenv("XDG_RUNTIME_DIR");
                 if (!e)
                         return -ENXIO;
 
-                prefix = strjoina(e, "/systemd/");
-                break;
-        }
-
-        case UNIT_FILE_GLOBAL:
-                return -EOPNOTSUPP;
-
-        default:
-                assert_not_reached("Hmm, unexpected scope value.");
+                prefix = strjoina(e, "/systemd");
         }
 
-        x = strappend(prefix, "generator");
+        x = strappend(prefix, "/generator");
         if (!x)
                 return -ENOMEM;
 
-        y = strappend(prefix, "generator.early");
+        y = strappend(prefix, "/generator.early");
         if (!y)
                 return -ENOMEM;
 
-        z = strappend(prefix, "generator.late");
+        z = strappend(prefix, "/generator.late");
         if (!z)
                 return -ENOMEM;
 
@@ -280,31 +293,30 @@ static int acquire_generator_dirs(
         return 0;
 }
 
-static int acquire_transient_dir(UnitFileScope scope, char **ret) {
-        assert(ret);
-
-        switch (scope) {
+static int acquire_transient_dir(
+                UnitFileScope scope,
+                const char *tempdir,
+                char **ret) {
 
-        case UNIT_FILE_SYSTEM: {
-                char *transient;
+        char *transient;
 
-                transient = strdup("/run/systemd/transient");
-                if (!transient)
-                        return -ENOMEM;
+        assert(ret);
+        assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER, UNIT_FILE_GLOBAL));
 
-                *ret = transient;
-                return 0;
-        }
+        if (scope == UNIT_FILE_GLOBAL)
+                return -EOPNOTSUPP;
 
-        case UNIT_FILE_USER:
+        if (tempdir)
+                transient = strjoin(tempdir, "/transient");
+        else if (scope == UNIT_FILE_SYSTEM)
+                transient = strdup("/run/systemd/transient");
+        else
                 return user_runtime_dir(ret, "/systemd/transient");
 
-        case UNIT_FILE_GLOBAL:
-                return -EOPNOTSUPP;
-
-        default:
-                assert_not_reached("Hmm, unexpected scope value.");
-        }
+        if (!transient)
+                return -ENOMEM;
+        *ret = transient;
+        return 0;
 }
 
 static int acquire_config_dirs(UnitFileScope scope, char **persistent, char **runtime) {
@@ -328,12 +340,18 @@ static int acquire_config_dirs(UnitFileScope scope, char **persistent, char **ru
 
         case UNIT_FILE_USER:
                 r = user_config_dir(&a, "/systemd/user");
-                if (r < 0)
+                if (r < 0 && r != -ENXIO)
                         return r;
 
                 r = user_runtime_dir(runtime, "/systemd/user");
-                if (r < 0)
-                        return r;
+                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 = a;
                 a = NULL;
@@ -382,12 +400,18 @@ static int acquire_control_dirs(UnitFileScope scope, char **persistent, char **r
 
         case UNIT_FILE_USER:
                 r = user_config_dir(&a, "/systemd/system.control");
-                if (r < 0)
+                if (r < 0 && r != -ENXIO)
                         return r;
 
                 r = user_runtime_dir(runtime, "/systemd/system.control");
-                if (r < 0)
-                        return r;
+                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;
 
@@ -444,6 +468,7 @@ int lookup_paths_init(
                 LookupPathsFlags flags,
                 const char *root_dir) {
 
+        _cleanup_(rmdir_and_freep) char *tempdir = NULL;
         _cleanup_free_ char
                 *root = NULL,
                 *persistent_config = NULL, *runtime_config = NULL,
@@ -474,22 +499,33 @@ int lookup_paths_init(
                         return -ENOMEM;
         }
 
+        if (flags & LOOKUP_PATHS_TEMPORARY_GENERATED) {
+                r = mkdtemp_malloc("/tmp/systemd-temporary-XXXXXX", &tempdir);
+                if (r < 0)
+                        return log_error_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 && r != -ENXIO)
+        if (r < 0)
                 return r;
 
         if ((flags & LOOKUP_PATHS_EXCLUDE_GENERATED) == 0) {
-                r = acquire_generator_dirs(scope, &generator, &generator_early, &generator_late);
+                /* 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 && r != -EOPNOTSUPP && r != -ENXIO)
                         return r;
         }
 
-        r = acquire_transient_dir(scope, &transient);
+        /* Note: if XDG_RUNTIME_DIR is not set, this will fail completely with ENXIO */
+        r = acquire_transient_dir(scope, tempdir, &transient);
         if (r < 0 && r != -EOPNOTSUPP && r != -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 && r != -ENXIO)
+        if (r < 0 && r != -EOPNOTSUPP)
                 return r;
 
         /* First priority is whatever has been passed to us via env vars */
@@ -540,7 +576,7 @@ int lookup_paths_init(
                                         "/usr/local/lib/systemd/system",
                                         SYSTEM_DATA_UNIT_PATH,
                                         "/usr/lib/systemd/system",
-#ifdef HAVE_SPLIT_USR
+#if HAVE_SPLIT_USR
                                         "/lib/systemd/system",
 #endif
                                         STRV_IFNOTNULL(generator_late),
@@ -652,6 +688,9 @@ int lookup_paths_init(
         p->root_dir = root;
         root = NULL;
 
+        p->temporary_dir = tempdir;
+        tempdir = NULL;
+
         return 0;
 }
 
@@ -674,6 +713,7 @@ void lookup_paths_free(LookupPaths *p) {
         p->runtime_control = mfree(p->runtime_control);
 
         p->root_dir = mfree(p->root_dir);
+        p->temporary_dir = mfree(p->temporary_dir);
 }
 
 int lookup_paths_reduce(LookupPaths *p) {
@@ -794,6 +834,9 @@ void lookup_paths_flush_generator(LookupPaths *p) {
                 (void) rm_rf(p->generator_early, REMOVE_ROOT);
         if (p->generator_late)
                 (void) rm_rf(p->generator_late, REMOVE_ROOT);
+
+        if (p->temporary_dir)
+                (void) rm_rf(p->temporary_dir, REMOVE_ROOT);
 }
 
 char **generator_binary_paths(UnitFileScope scope) {