[libvirt] [PATCH] util: activate directory override when used from library

Daniel P. Berrangé berrange at redhat.com
Thu Aug 29 10:54:37 UTC 2019


The Perl bindings for libvirt use the test driver for unit tests. This
tries to load the cpu_map/index.xml file, and when run from an
uninstalled build will fail.

The problem is that virFileActivateDirOverride is called by our various
binaries like libvirtd, virsh, but is not called when a 3rd party app
uses libvirt.so

To deal with this we allow the LIBVIRT_DIR_OVERRIDE=1 env variable to be
set and make virInitialize look for this. The 'run' script will set it,
so now build using this script to run against an uninstalled tree we
will correctly resolve files to the source tree.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 run.in             |  3 +++
 src/libvirt.c      |  2 ++
 src/util/virfile.c | 21 +++++++++++++--------
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/run.in b/run.in
index 9a1c6ea11a..de7af78f67 100644
--- a/run.in
+++ b/run.in
@@ -60,6 +60,9 @@ else
 fi
 export PKG_CONFIG_PATH
 
+LIBVIRT_DIR_OVERRIDE=1
+export LIBVIRT_DIR_OVERRIDE
+
 # This is a cheap way to find some use-after-free and uninitialized
 # read problems when using glibc.
 random_val="$(awk 'BEGIN{srand(); print 1+int(255*rand())}' < /dev/null)"
diff --git a/src/libvirt.c b/src/libvirt.c
index 956ccdea30..6b1b4a54c3 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -250,6 +250,8 @@ virGlobalInit(void)
         virErrorInitialize() < 0)
         goto error;
 
+    virFileActivateDirOverride(NULL);
+
     if (getuid() != geteuid() ||
         getgid() != getegid()) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 81a3c096eb..391b6a9e6e 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1776,14 +1776,19 @@ virFileFindResource(const char *filename,
 void
 virFileActivateDirOverride(const char *argv0)
 {
-    char *file = strrchr(argv0, '/');
-    if (!file || file[1] == '\0')
-        return;
-    file++;
-    if (STRPREFIX(file, "lt-") ||
-        strstr(argv0, "/.libs/")) {
-        useDirOverride = true;
-        VIR_DEBUG("Activating build dir override for %s", argv0);
+    if (argv0 == NULL) {
+        if (getenv("LIBVIRT_DIR_OVERRIDE") != NULL)
+            useDirOverride = true;
+    } else {
+        char *file = strrchr(argv0, '/');
+        if (!file || file[1] == '\0')
+            return;
+        file++;
+        if (STRPREFIX(file, "lt-") ||
+            strstr(argv0, "/.libs/")) {
+            useDirOverride = true;
+            VIR_DEBUG("Activating build dir override for %s", argv0);
+        }
     }
 }
 
-- 
2.21.0




More information about the libvir-list mailing list