[Libguestfs] [PATCH v2] lib: Convert all drive socket parameters to an absolute path (RHBZ#1588451).

Richard W.M. Jones
2018-06-15T13:50:49Z

Also fix two tests which assumed that you could add a non-existent
socket.
---
 fish/test-add-uri.sh           | 16 ++++++++++++----
 lib/drives.c                   | 14 +++++++++++++-
 tests/disks/test-qemu-drive.sh | 19 ++++++++++++++-----
 3 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/fish/test-add-uri.sh b/fish/test-add-uri.sh
index 756df997b..cb4d40199 100755
--- a/fish/test-add-uri.sh
+++ b/fish/test-add-uri.sh
@@ -26,6 +26,7 @@ skip_if_skipped

 rm -f test-add-uri.out
 rm -f test-add-uri.img
+rm -f test-add-uri.sock

 $VG guestfish sparse test-add-uri.img 10M

@@ -55,11 +56,17 @@ grep -sq 'add_drive ""
"protocol:nbd" "server:tcp:example.com"'
test-add-uri.out
 $VG guestfish -x -a nbd://example.com:3000 </dev/null >test-add-uri.out
2>&1
 grep -sq 'add_drive "" "protocol:nbd"
"server:tcp:example.com:3000"' test-add-uri.out || fail

-$VG guestfish -x -a 'nbd://?socket=/sk' </dev/null
>test-add-uri.out 2>&1
-grep -sq 'add_drive "" "protocol:nbd"
"server:unix:/sk"' test-add-uri.out || fail
+# NBD Unix domain socket tests.
+# These require Perl to create the socket, but don't fail if it's not
+# around.

-$VG guestfish -x -a 'nbd:///export?socket=/sk' </dev/null
>test-add-uri.out 2>&1
-grep -sq 'add_drive "/export" "protocol:nbd"
"server:unix:/sk"' test-add-uri.out || fail
+if perl -MIO::Socket -e 'IO::Socket::UNIX->new (Local =>
"test-add-uri.sock", Type => SOCK_STREAM(), Listen => 1);
1'; then
+    $VG guestfish -x -a 'nbd://?socket=test-add-uri.sock' </dev/null
>test-add-uri.out 2>&1
+    grep -sq 'add_drive "" "protocol:nbd"
"server:unix:test-add-uri.sock"' test-add-uri.out || fail
+
+    $VG guestfish -x -a 'nbd:///export?socket=test-add-uri.sock'
</dev/null >test-add-uri.out 2>&1
+    grep -sq 'add_drive "/export" "protocol:nbd"
"server:unix:test-add-uri.sock"' test-add-uri.out || fail
+fi

 # rbd
 $VG guestfish -x -a rbd://example.com:6789/pool/disk </dev/null
>test-add-uri.out 2>&1
@@ -93,3 +100,4 @@ grep -sq 'add_drive
"iqn.2015-12.com.libguestfs:test2/0" "protocol:iscsi"
"serve

 rm test-add-uri.out
 rm test-add-uri.img
+rm -f test-add-uri.sock
diff --git a/lib/drives.c b/lib/drives.c
index 82ef30093..7697f369a 100644
--- a/lib/drives.c
+++ b/lib/drives.c
@@ -28,6 +28,7 @@
 #include <stdbool.h>
 #include <string.h>
 #include <unistd.h>
+#include <limits.h>
 #include <netdb.h>
 #include <arpa/inet.h>
 #include <assert.h>
@@ -645,7 +646,18 @@ parse_one_server (guestfs_h *g, const char *server, struct
drive_server *ret)
       return -1;
     }
     ret->transport = drive_transport_unix;
-    ret->u.socket = safe_strdup (g, server+5);
+
+    /* libvirt requires sockets to be specified as an absolute path
+     * (see RHBZ#1588451), and it's probably a good idea anyway to
+     * check the socket exists and convert it to an absolute path.
+     */
+    ret->u.socket = realpath (server+5, NULL);
+    if (ret->u.socket == NULL) {
+      perrorf (g, _("realpath: could not convert ‘%s’ to an absolute
path"),
+               server+5);
+      return -1;
+    }
+
     ret->port = 0;
     return 0;
   }
diff --git a/tests/disks/test-qemu-drive.sh b/tests/disks/test-qemu-drive.sh
index 19dd60a2f..181f21401 100755
--- a/tests/disks/test-qemu-drive.sh
+++ b/tests/disks/test-qemu-drive.sh
@@ -43,6 +43,8 @@ function fail ()

 rm -f "$DEBUG_QEMU_FILE"

+rm -f test-qemu-drive.sock
+
 # Ceph (RBD).

 guestfish <<EOF ||:
@@ -111,13 +113,18 @@ check_output
 grep -sq -- '-drive file=nbd:1.2.3.4:1234,'
"$DEBUG_QEMU_FILE" || fail
 rm "$DEBUG_QEMU_FILE"

-guestfish <<EOF ||:
-  add "" "format:raw" "protocol:nbd"
"server:unix:/socket"
+# This test requires Perl to create the socket, but don't fail if
+# it's not around.
+if perl -MIO::Socket -e 'IO::Socket::UNIX->new (Local =>
"test-qemu-drive.sock", Type => SOCK_STREAM(), Listen => 1);
1'; then
+
+    guestfish <<EOF ||:
+  add "" "format:raw" "protocol:nbd"
"server:unix:test-qemu-drive.sock"
   run
 EOF
-check_output
-grep -sq -- '-drive file=nbd:unix:/socket,'
"$DEBUG_QEMU_FILE" || fail
-rm "$DEBUG_QEMU_FILE"
+    check_output
+    grep -sq -- '-drive file=nbd:unix:.*/test-qemu-drive.sock,'
"$DEBUG_QEMU_FILE" || fail
+    rm "$DEBUG_QEMU_FILE"
+fi

 # Sheepdog.

@@ -139,3 +146,5 @@ EOF
 check_output
 grep -sq -- '-drive file=ssh://rich@example.com/disk.img,'
"$DEBUG_QEMU_FILE" || fail
 rm "$DEBUG_QEMU_FILE"
+
+rm -f test-qemu-drive.sock
--
2.16.2