Blob Blame History Raw
From 9e20e2696fdb68008c9b4f1c36298f813320e381 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Sat, 23 Oct 2021 16:16:39 +0100
Subject: [PATCH] vddk: Include VDDK major library version in --dump-plugin
 output

Although it doesn't seem to be possible to get the precise VDDK
version, With a relatively simple change we can at least return the
VDDK major version.  Currently this can be 5, 6 or 7.

(cherry picked from commit 8700649d147948897f3b97810a1dff37924bdd6e)
---
 plugins/vddk/nbdkit-vddk-plugin.pod |  4 ++++
 plugins/vddk/vddk.c                 | 29 +++++++++++++++++++----------
 tests/test-vddk-real-dump-plugin.sh |  2 ++
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/plugins/vddk/nbdkit-vddk-plugin.pod b/plugins/vddk/nbdkit-vddk-plugin.pod
index 8b14eda0..822b96be 100644
--- a/plugins/vddk/nbdkit-vddk-plugin.pod
+++ b/plugins/vddk/nbdkit-vddk-plugin.pod
@@ -417,6 +417,10 @@ at runtime.
 If this is printed then the C<nfchostport=PORT> parameter is supported
 by this build.
 
+=item C<vddk_library_version=...>
+
+The VDDK major library version: 5, 6, 7, ...
+
 =item C<vddk_dll=...>
 
 Prints the full path to the VDDK shared library.  Since this requires
diff --git a/plugins/vddk/vddk.c b/plugins/vddk/vddk.c
index 69193504..291283f4 100644
--- a/plugins/vddk/vddk.c
+++ b/plugins/vddk/vddk.c
@@ -77,6 +77,7 @@ int vddk_debug_datapath = 1;
 static void *dl;                           /* dlopen handle */
 static bool init_called;                   /* was InitEx called */
 static __thread int error_suppression;     /* threadlocal error suppression */
+static int library_version;                /* VDDK major: 5, 6, 7, ... */
 
 static enum { NONE = 0, ZLIB, FASTLZ, SKIPZ } compression; /* compression */
 static char *config;                       /* config */
@@ -297,7 +298,10 @@ vddk_config (const char *key, const char *value)
 static void
 load_library (bool load_error_is_fatal)
 {
-  static const char *sonames[] = {
+  static struct {
+    const char *soname;
+    int library_version;
+  } libs[] = {
     /* Prefer the newest library in case multiple exist.  Check two
      * possible directories: the usual VDDK installation puts .so
      * files in an arch-specific subdirectory of $libdir (our minimum
@@ -305,12 +309,13 @@ load_library (bool load_error_is_fatal)
      * but our testsuite is easier to write if we point libdir
      * directly to a stub .so.
      */
-    "lib64/libvixDiskLib.so.7",
-    "libvixDiskLib.so.7",
-    "lib64/libvixDiskLib.so.6",
-    "libvixDiskLib.so.6",
-    "lib64/libvixDiskLib.so.5",
-    "libvixDiskLib.so.5",
+    { "lib64/libvixDiskLib.so.7", 7 },
+    { "libvixDiskLib.so.7",       7 },
+    { "lib64/libvixDiskLib.so.6", 6 },
+    { "libvixDiskLib.so.6",       6 },
+    { "lib64/libvixDiskLib.so.5", 5 },
+    { "libvixDiskLib.so.5",       5 },
+    { NULL }
   };
   size_t i;
   CLEANUP_FREE char *orig_error = NULL;
@@ -323,19 +328,20 @@ load_library (bool load_error_is_fatal)
     }
   }
 
-  for (i = 0; i < sizeof sonames / sizeof sonames[0]; ++i) {
+  for (i = 0; libs[i].soname != NULL; ++i) {
     CLEANUP_FREE char *path;
 
     /* Set the full path so that dlopen will preferentially load the
      * system libraries from the same directory.
      */
-    if (asprintf (&path, "%s/%s", libdir, sonames[i]) == -1) {
+    if (asprintf (&path, "%s/%s", libdir, libs[i].soname) == -1) {
       nbdkit_error ("asprintf: %m");
       exit (EXIT_FAILURE);
     }
 
     dl = dlopen (path, RTLD_NOW);
     if (dl != NULL) {
+      library_version = libs[i].library_version;
       /* Now that we found the library, ensure that LD_LIBRARY_PATH
        * includes its directory for all future loads.  This may modify
        * path in-place and/or re-exec nbdkit, but that's okay.
@@ -356,10 +362,12 @@ load_library (bool load_error_is_fatal)
                   "If '%s' is located on a non-standard path you may need to\n"
                   "set libdir=/path/to/vmware-vix-disklib-distrib.\n\n"
                   "See nbdkit-vddk-plugin(1) man page section \"LIBRARY LOCATION\" for details.",
-                  orig_error ? : "(unknown error)", sonames[0]);
+                  orig_error ? : "(unknown error)", libs[0].soname);
     exit (EXIT_FAILURE);
   }
 
+  assert (library_version >= 5);
+
   /* Load symbols. */
 #define STUB(fn,ret,args)                                         \
   do {                                                            \
@@ -474,6 +482,7 @@ vddk_dump_plugin (void)
 
   printf ("vddk_default_libdir=%s\n", VDDK_LIBDIR);
   printf ("vddk_has_nfchostport=1\n");
+  printf ("vddk_library_version=%d\n", library_version);
 
 #if defined(HAVE_DLADDR)
   /* It would be nice to print the version of VDDK from the shared
diff --git a/tests/test-vddk-real-dump-plugin.sh b/tests/test-vddk-real-dump-plugin.sh
index 1479e416..59c79693 100755
--- a/tests/test-vddk-real-dump-plugin.sh
+++ b/tests/test-vddk-real-dump-plugin.sh
@@ -51,10 +51,12 @@ rm -f $files
 cleanup_fn rm -f $files
 
 nbdkit -f -v vddk libdir="$vddkdir" --dump-plugin > $out
+cat $out
 
 # Check the vddk_* entries are set.
 grep ^vddk_default_libdir= $out
 grep ^vddk_has_nfchostport= $out
+grep ^vddk_library_version= $out
 grep ^vddk_dll= $out
 
 dll="$(grep ^vddk_dll $out | cut -d= -f2)"
-- 
2.31.1