]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite/path: defer and inline get_rootpath()
authorEmil Velikov <emil.l.velikov@gmail.com>
Sat, 24 May 2025 12:23:09 +0000 (13:23 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Thu, 29 May 2025 22:30:13 +0000 (17:30 -0500)
In a handful of use-cases, we don't need to trap the respective libc
function. Although at that point we have already fetched the rootfs
environment variable.

Defer it until it's needed and in the process inline the function. As
result we have a) less duplication in the wrappers and b) the rootpath
handling is no longer split across multiple functions.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/355
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
testsuite/path.c

index e6a9ef36e2bdaf634b5b6d5b9aca979317c71733..25548f491fe04a4f6f523eb7b6314348a1c60565 100644 (file)
@@ -26,9 +26,6 @@
 
 #include "testsuite.h"
 
-static const char *rootpath;
-static size_t rootpathlen;
-
 static inline bool need_trap(const char *path)
 {
        /*
@@ -45,6 +42,8 @@ static inline bool need_trap(const char *path)
 
 static const char *trap_path(const char *path, char buf[PATH_MAX * 2])
 {
+       static const char *rootpath;
+       static size_t rootpathlen;
        size_t len;
 
        if (!need_trap(path))
@@ -52,6 +51,17 @@ static const char *trap_path(const char *path, char buf[PATH_MAX * 2])
 
        len = strlen(path);
 
+       if (rootpath == NULL) {
+               rootpath = getenv(S_TC_ROOTFS);
+               if (rootpath == NULL) {
+                       ERR("TRAP: missing export %s?\n", S_TC_ROOTFS);
+                       errno = ENOENT;
+                       return NULL;
+               }
+
+               rootpathlen = strlen(rootpath);
+       }
+
        if (len + rootpathlen > PATH_MAX * 2) {
                errno = ENAMETOOLONG;
                return NULL;
@@ -62,23 +72,6 @@ static const char *trap_path(const char *path, char buf[PATH_MAX * 2])
        return buf;
 }
 
-static bool get_rootpath(const char *f)
-{
-       if (rootpath != NULL)
-               return true;
-
-       rootpath = getenv(S_TC_ROOTFS);
-       if (rootpath == NULL) {
-               ERR("TRAP %s(): missing export %s?\n", f, S_TC_ROOTFS);
-               errno = ENOENT;
-               return false;
-       }
-
-       rootpathlen = strlen(rootpath);
-
-       return true;
-}
-
 static void *get_libc_func(const char *f)
 {
        void *fp;
@@ -101,8 +94,6 @@ static void *get_libc_func(const char *f)
                char buf[PATH_MAX * 2];              \
                static rettype (*_fn)(const char *); \
                                                      \
-               if (!get_rootpath(__func__))         \
-                       return failret;              \
                p = trap_path(path, buf);            \
                if (p == NULL)                       \
                        return failret;              \
@@ -120,8 +111,6 @@ static void *get_libc_func(const char *f)
                char buf[PATH_MAX * 2];                          \
                static rettype (*_fn)(const char *, arg2t arg2); \
                                                                  \
-               if (!get_rootpath(__func__))                     \
-                       return failret;                          \
                p = trap_path(path, buf);                        \
                if (p == NULL)                                   \
                        return failret;                          \
@@ -139,8 +128,6 @@ static void *get_libc_func(const char *f)
                char buf[PATH_MAX * 2];                              \
                static int (*_fn)(const char *path, int flags, ...); \
                                                                      \
-               if (!get_rootpath(__func__))                         \
-                       return -1;                                   \
                p = trap_path(path, buf);                            \
                if (p == NULL)                                       \
                        return -1;                                   \
@@ -168,8 +155,6 @@ static void *get_libc_func(const char *f)
                char buf[PATH_MAX * 2];                                              \
                static int (*_fn)(int ver, const char *path, struct stat##suffix *); \
                                                                                      \
-               if (!get_rootpath(__func__))                                         \
-                       return -1;                                                   \
                p = trap_path(path, buf);                                            \
                if (p == NULL)                                                       \
                        return -1;                                                   \