From: Emil Velikov Date: Sat, 24 May 2025 12:23:09 +0000 (+0100) Subject: testsuite/path: defer and inline get_rootpath() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fdde586a187a0dcee2e0bfae8f2f907e5018321d;p=thirdparty%2Fkmod.git testsuite/path: defer and inline get_rootpath() 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 Link: https://github.com/kmod-project/kmod/pull/355 Signed-off-by: Lucas De Marchi --- diff --git a/testsuite/path.c b/testsuite/path.c index e6a9ef36..25548f49 100644 --- a/testsuite/path.c +++ b/testsuite/path.c @@ -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; \