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

Daniel P. Berrangé posted 1 patch 4 years, 7 months ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20190829105437.11741-1-berrange@redhat.com
There is a newer version of this series
run.in             |  3 +++
src/libvirt.c      |  2 ++
src/util/virfile.c | 21 +++++++++++++--------
3 files changed, 18 insertions(+), 8 deletions(-)
[libvirt] [PATCH] util: activate directory override when used from library
Posted by Daniel P. Berrangé 4 years, 7 months ago
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@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

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] util: activate directory override when used from library
Posted by Peter Krempa 4 years, 7 months ago
On Thu, Aug 29, 2019 at 11:54:37 +0100, Daniel Berrange wrote:
> 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@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

For future reference you should document what this does.

> +
>  # 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;

I'm not a fan that you have different behaviour depending on whether
'argv0' is set or not. Also the comment for the function is now stale
and the point of the env variable is cryptic.

> +    } 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
> 
> --
> libvir-list mailing list
> libvir-list@redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list