]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-path/sd-path.c
systemd-path: allow the default search path to be queried
[thirdparty/systemd.git] / src / libsystemd / sd-path / sd-path.c
index 480f1ad065e3e2d8493f10a1531c641db60abd36..b2e8e63c1b3e594965f5ff0dee5757da504d6f7e 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -23,6 +24,7 @@
 #include "architecture.h"
 #include "fd-util.h"
 #include "fileio.h"
+#include "fs-util.h"
 #include "missing.h"
 #include "path-util.h"
 #include "string-util.h"
@@ -77,7 +79,7 @@ static int from_home_dir(const char *envname, const char *suffix, char **buffer,
         if (endswith(h, "/"))
                 cc = strappend(h, suffix);
         else
-                cc = strjoin(h, "/", suffix, NULL);
+                cc = strjoin(h, "/", suffix);
         if (!cc)
                 return -ENOMEM;
 
@@ -89,7 +91,8 @@ static int from_home_dir(const char *envname, const char *suffix, char **buffer,
 static int from_user_dir(const char *field, char **buffer, const char **ret) {
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *b = NULL;
-        const char *fn = NULL;
+        _cleanup_free_ const char *fn = NULL;
+        const char *c = NULL;
         char line[LINE_MAX];
         size_t n;
         int r;
@@ -98,10 +101,14 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) {
         assert(buffer);
         assert(ret);
 
-        r = from_home_dir(NULL, ".config/user-dirs.dirs", &b, &fn);
+        r = from_home_dir("XDG_CONFIG_HOME", ".config", &b, &c);
         if (r < 0)
                 return r;
 
+        fn = strappend(c, "/user-dirs.dirs");
+        if (!fn)
+                return -ENOMEM;
+
         f = fopen(fn, "re");
         if (!f) {
                 if (errno == ENOENT)
@@ -214,10 +221,10 @@ static int get_path(uint64_t type, char **buffer, const char **ret) {
         switch (type) {
 
         case SD_PATH_TEMPORARY:
-                return from_environment("TMPDIR", "/tmp", ret);
+                return tmp_dir(ret);
 
         case SD_PATH_TEMPORARY_LARGE:
-                return from_environment("TMPDIR", "/var/tmp", ret);
+                return var_tmp_dir(ret);
 
         case SD_PATH_SYSTEM_BINARIES:
                 *ret = "/usr/bin";
@@ -341,6 +348,7 @@ _public_ int sd_path_home(uint64_t type, const char *suffix, char **path) {
 
         if (IN_SET(type,
                    SD_PATH_SEARCH_BINARIES,
+                   SD_PATH_SEARCH_BINARIES_DEFAULT,
                    SD_PATH_SEARCH_LIBRARY_PRIVATE,
                    SD_PATH_SEARCH_LIBRARY_ARCH,
                    SD_PATH_SEARCH_SHARED,
@@ -382,7 +390,7 @@ _public_ int sd_path_home(uint64_t type, const char *suffix, char **path) {
         if (endswith(ret, "/"))
                 cc = strappend(ret, suffix);
         else
-                cc = strjoin(ret, "/", suffix, NULL);
+                cc = strjoin(ret, "/", suffix);
 
         free(buffer);
 
@@ -450,7 +458,7 @@ static int search_from_environment(
                         if (endswith(e, "/"))
                                 h = strappend(e, home_suffix);
                         else
-                                h = strjoin(e, "/", home_suffix, NULL);
+                                h = strjoin(e, "/", home_suffix);
 
                         if (!h) {
                                 strv_free(l);
@@ -471,6 +479,12 @@ static int search_from_environment(
         return 0;
 }
 
+#if HAVE_SPLIT_BIN
+#  define ARRAY_SBIN_BIN(x) x "sbin", x "bin"
+#else
+#  define ARRAY_SBIN_BIN(x) x "bin"
+#endif
+
 static int get_search(uint64_t type, char ***list) {
 
         assert(list);
@@ -483,13 +497,10 @@ static int get_search(uint64_t type, char ***list) {
                                                ".local/bin",
                                                "PATH",
                                                true,
-                                               "/usr/local/sbin",
-                                               "/usr/local/bin",
-                                               "/usr/sbin",
-                                               "/usr/bin",
-#ifdef HAVE_SPLIT_USR
-                                               "/sbin",
-                                               "/bin",
+                                               ARRAY_SBIN_BIN("/usr/local/"),
+                                               ARRAY_SBIN_BIN("/usr/"),
+#if HAVE_SPLIT_USR
+                                               ARRAY_SBIN_BIN("/"),
 #endif
                                                NULL);
 
@@ -501,7 +512,7 @@ static int get_search(uint64_t type, char ***list) {
                                                false,
                                                "/usr/local/lib",
                                                "/usr/lib",
-#ifdef HAVE_SPLIT_USR
+#if HAVE_SPLIT_USR
                                                "/lib",
 #endif
                                                NULL);
@@ -513,7 +524,7 @@ static int get_search(uint64_t type, char ***list) {
                                                "LD_LIBRARY_PATH",
                                                true,
                                                LIBDIR,
-#ifdef HAVE_SPLIT_USR
+#if HAVE_SPLIT_USR
                                                ROOTLIBDIR,
 #endif
                                                NULL);
@@ -556,7 +567,17 @@ static int get_search(uint64_t type, char ***list) {
                                                false,
                                                "/etc",
                                                NULL);
-        }
+
+        case SD_PATH_SEARCH_BINARIES_DEFAULT: {
+                char **t;
+
+                t = strv_split_nulstr(DEFAULT_PATH_NULSTR);
+                if (!t)
+                        return -ENOMEM;
+
+                *list = t;
+                return 0;
+        }}
 
         return -EOPNOTSUPP;
 }
@@ -569,6 +590,7 @@ _public_ int sd_path_search(uint64_t type, const char *suffix, char ***paths) {
 
         if (!IN_SET(type,
                     SD_PATH_SEARCH_BINARIES,
+                    SD_PATH_SEARCH_BINARIES_DEFAULT,
                     SD_PATH_SEARCH_LIBRARY_PRIVATE,
                     SD_PATH_SEARCH_LIBRARY_ARCH,
                     SD_PATH_SEARCH_SHARED,
@@ -616,7 +638,7 @@ _public_ int sd_path_search(uint64_t type, const char *suffix, char ***paths) {
                 if (endswith(*i, "/"))
                         *j = strappend(*i, suffix);
                 else
-                        *j = strjoin(*i, "/", suffix, NULL);
+                        *j = strjoin(*i, "/", suffix);
 
                 if (!*j) {
                         strv_free(l);