[libvirt] [PATCH v2 2/8] tests: Mock realpath()

Andrea Bolognani abologna at redhat.com
Mon Apr 30 16:52:57 UTC 2018


We need to use VIR_MOCK_REAL_INIT_VERSIONED() and ask for a
specific version of the symbol here, because the implementation
dlsym() returns by default on Linux is unsuitable for our use.

Signed-off-by: Andrea Bolognani <abologna at redhat.com>
---
 tests/virpcimock.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/tests/virpcimock.c b/tests/virpcimock.c
index 176c64d654..9a5e6b4cec 100644
--- a/tests/virpcimock.c
+++ b/tests/virpcimock.c
@@ -43,6 +43,7 @@ static char *(*real_canonicalize_file_name)(const char *path);
 static int (*real_open)(const char *path, int flags, ...);
 static int (*real_close)(int fd);
 static DIR * (*real_opendir)(const char *name);
+static char *(*real_realpath)(const char *path, char *resolved);
 
 /* Don't make static, since it causes problems with clang
  * when passed as an arg to virAsprintf()
@@ -814,6 +815,20 @@ init_syms(void)
     VIR_MOCK_REAL_INIT(open);
     VIR_MOCK_REAL_INIT(close);
     VIR_MOCK_REAL_INIT(opendir);
+
+    /* When linking on Linux, the default implementation of realpath() is
+     * realpath at GLIBC_2.3; when using dlsym(), however, we get the older
+     * realpath at GLIBC_2.2.5 instead, which unfortunately doesn't support
+     * passing NULL as the second parameter.
+     *
+     * Ask for a versioned symbol to make sure we get a working realpath()
+     * on Linux; other operating systems such as FreeBSD don't suffer from
+     * the same limitation and can rely on the default dlsym() behavior */
+# ifdef __linux__
+    VIR_MOCK_REAL_INIT_VERSIONED(realpath, "GLIBC_2.3");
+# else
+    VIR_MOCK_REAL_INIT(realpath);
+# endif
 }
 
 static void
@@ -1046,6 +1061,25 @@ close(int fd)
         return -1;
     return real_close(fd);
 }
+
+char *
+realpath(const char *path, char *resolved)
+{
+    char *ret;
+
+    init_syms();
+
+    if (STRPREFIX(path, SYSFS_PCI_PREFIX)) {
+        char *newpath;
+        if (getrealpath(&newpath, path) < 0)
+            return NULL;
+        ret = real_realpath(newpath, resolved);
+        VIR_FREE(newpath);
+    } else {
+        ret = real_realpath(path, resolved);
+    }
+    return ret;
+}
 #else
 /* Nothing to override on non-__linux__ platforms */
 #endif
-- 
2.14.3




More information about the libvir-list mailing list