[PATCH] ioctl: implement NBD_* ioctl decoding

Elvira Khabirova lineprinter at altlinux.org
Fri Sep 21 14:42:50 UTC 2018


* Makefile.am (strace_SOURCES): Add nbd_ioctl.c.
* configure.ac (AC_CHECK_HEADERS): Add linux/nbd.h.
* defs.h (DECL_IOCTL): Add nbd.
* ioctl.c (ioctl_decode): Add 0xab (nbd) case.
* nbd_ioctl.c: New file.
* xlat/nbd_ioctl_cmds.in: Likewise.
* xlat/nbd_ioctl_flags.in: Likewise.
* tests/ioctl_nbd.c: Likewise.
* tests/.gitignore: Add ioctl_nbd.
* tests/gen_tests.in: Likewise.
* tests/pure_executables.list: Likewise.
---
 Makefile.am                 |  1 +
 configure.ac                |  1 +
 defs.h                      |  1 +
 ioctl.c                     |  2 +
 nbd_ioctl.c                 | 76 +++++++++++++++++++++++++++++
 tests/.gitignore            |  1 +
 tests/gen_tests.in          |  1 +
 tests/ioctl_nbd.c           | 96 +++++++++++++++++++++++++++++++++++++
 tests/pure_executables.list |  1 +
 xlat/nbd_ioctl_cmds.in      | 11 +++++
 xlat/nbd_ioctl_flags.in     |  8 ++++
 11 files changed, 199 insertions(+)
 create mode 100644 nbd_ioctl.c
 create mode 100644 tests/ioctl_nbd.c
 create mode 100644 xlat/nbd_ioctl_cmds.in
 create mode 100644 xlat/nbd_ioctl_flags.in

diff --git a/Makefile.am b/Makefile.am
index 9e5eef27..913d26a9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -204,6 +204,7 @@ strace_SOURCES =	\
 	msghdr.h	\
 	mtd.c		\
 	native_defs.h	\
+	nbd_ioctl.c	\
 	negated_errno.h	\
 	net.c		\
 	netlink.c       \
diff --git a/configure.ac b/configure.ac
index c982ced1..14f6757b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -408,6 +408,7 @@ AC_CHECK_HEADERS(m4_normalize([
 	linux/memfd.h
 	linux/mmtimer.h
 	linux/msg.h
+	linux/nbd.h
 	linux/neighbour.h
 	linux/netfilter/ipset/ip_set.h
 	linux/netfilter/nf_tables.h
diff --git a/defs.h b/defs.h
index 7f1e64da..5ba95f53 100644
--- a/defs.h
+++ b/defs.h
@@ -970,6 +970,7 @@ DECL_IOCTL(file);
 DECL_IOCTL(fs_x);
 DECL_IOCTL(inotify);
 DECL_IOCTL(kvm);
+DECL_IOCTL(nbd);
 DECL_IOCTL(nsfs);
 DECL_IOCTL(ptp);
 DECL_IOCTL(scsi);
diff --git a/ioctl.c b/ioctl.c
index 66b10ec4..bd12da83 100644
--- a/ioctl.c
+++ b/ioctl.c
@@ -327,6 +327,8 @@ ioctl_decode(struct tcb *tcp)
 #endif
 	case 'I':
 		return inotify_ioctl(tcp, code, arg);
+    case 0xab:
+        return nbd_ioctl(tcp, code, arg);
 	default:
 		break;
 	}
diff --git a/nbd_ioctl.c b/nbd_ioctl.c
new file mode 100644
index 00000000..4502edc4
--- /dev/null
+++ b/nbd_ioctl.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2018 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "defs.h"
+
+#include <linux/ioctl.h>
+
+#include "print_fields.h"
+
+#define XLAT_MACROS_ONLY
+# include "xlat/nbd_ioctl_cmds.h"
+#undef XLAT_MACROS_ONLY
+
+#include "xlat/nbd_ioctl_flags.h"
+
+int
+nbd_ioctl(struct tcb *const tcp, const unsigned int code,
+	  const kernel_ulong_t arg)
+{
+	if (!verbose(tcp))
+		return RVAL_DECODED;
+
+	switch (code) {
+	case NBD_DISCONNECT:
+	case NBD_CLEAR_SOCK:
+	case NBD_DO_IT:
+	case NBD_CLEAR_QUE:
+	case NBD_PRINT_DEBUG:
+		return RVAL_IOCTL_DECODED;
+
+	case NBD_SET_SOCK:
+		tprints(", ");
+		printfd(tcp, arg);
+		return RVAL_IOCTL_DECODED;
+
+	case NBD_SET_BLKSIZE:
+	case NBD_SET_SIZE:
+	case NBD_SET_SIZE_BLOCKS:
+	case NBD_SET_TIMEOUT:
+		tprints(", ");
+		tprintf("%" PRI_klu, arg);
+		return RVAL_IOCTL_DECODED;
+
+	case NBD_SET_FLAGS:
+		tprints(", ");
+		printflags(nbd_ioctl_flags, arg, "NBD_IOC_FLAG_???");
+		return RVAL_IOCTL_DECODED;
+
+	default:
+		return RVAL_DECODED;
+	}
+}
diff --git a/tests/.gitignore b/tests/.gitignore
index a7f201ee..577a1316 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -153,6 +153,7 @@ ioctl_loop
 ioctl_loop-nv
 ioctl_loop-v
 ioctl_mtd
+ioctl_nbd
 ioctl_nsfs
 ioctl_perf
 ioctl_perf-success
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index 16638970..e8d6772f 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -146,6 +146,7 @@ ioctl_loop	+ioctl.test
 ioctl_loop-nv	+ioctl.test -a22 -e verbose=none
 ioctl_loop-v	+ioctl.test -v
 ioctl_mtd	+ioctl.test
+ioctl_nbd	+ioctl.test -yy
 ioctl_nsfs	+ioctl.test -esignal=none
 ioctl_perf	+ioctl.test
 ioctl_ptp 	+ioctl.test
diff --git a/tests/ioctl_nbd.c b/tests/ioctl_nbd.c
new file mode 100644
index 00000000..c42a3bcc
--- /dev/null
+++ b/tests/ioctl_nbd.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2018 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests.h"
+
+#ifdef HAVE_LINUX_NBD_H
+
+# include <stdio.h>
+# include <sys/ioctl.h>
+# include <linux/nbd.h>
+# include <sys/socket.h>
+
+# define XLAT_MACROS_ONLY
+#  include "xlat/nbd_ioctl_cmds.h"
+#  include "xlat/nbd_ioctl_flags.h"
+# undef XLAT_MACROS_ONLY
+
+int
+main(void)
+{
+	int64_t ubeef = 0xcafef00ddeadbeefULL;
+	int sock = socket(AF_UNIX, SOCK_STREAM, 0);
+
+	ioctl(-1, NBD_DISCONNECT, NULL);
+	printf("ioctl(-1, NBD_DISCONNECT) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_CLEAR_SOCK, NULL);
+	printf("ioctl(-1, NBD_CLEAR_SOCK) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_DO_IT, NULL);
+	printf("ioctl(-1, NBD_DO_IT) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_CLEAR_QUE, NULL);
+	printf("ioctl(-1, NBD_CLEAR_QUE) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_PRINT_DEBUG, NULL);
+	printf("ioctl(-1, NBD_PRINT_DEBUG) = -1 EBADF (%m)\n");
+
+	ioctl(-1, NBD_SET_SOCK, sock);
+	printf("ioctl(-1, NBD_SET_SOCK, %d<UNIX:[%lu]>) = -1 EBADF (%m)\n",
+		   sock, inode_of_sockfd(sock));
+
+	ioctl(-1, NBD_SET_BLKSIZE, ubeef);
+	printf("ioctl(-1, NBD_SET_BLKSIZE, %lu) = -1 EBADF (%m)\n", (unsigned long) ubeef);
+	ioctl(-1, NBD_SET_SIZE, ubeef);
+	printf("ioctl(-1, NBD_SET_SIZE, %lu) = -1 EBADF (%m)\n", (unsigned long) ubeef);
+	ioctl(-1, NBD_SET_SIZE_BLOCKS, ubeef);
+	printf("ioctl(-1, NBD_SET_SIZE_BLOCKS, %lu) = -1 EBADF (%m)\n", (unsigned long) ubeef);
+
+	ioctl(-1, NBD_SET_TIMEOUT, ubeef);
+	printf("ioctl(-1, NBD_SET_TIMEOUT, %lu) = -1 EBADF (%m)\n", (unsigned long) ubeef);
+
+	ioctl(-1, NBD_SET_FLAGS, 0);
+	printf("ioctl(-1, NBD_SET_FLAGS, 0) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_HAS_FLAGS);
+	printf("ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_HAS_FLAGS) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_READ_ONLY);
+	printf("ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_READ_ONLY) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_SEND_FLUSH);
+	printf("ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_SEND_FLUSH) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_SEND_FUA);
+	printf("ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_SEND_FUA) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_SEND_TRIM);
+	printf("ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_SEND_TRIM) = -1 EBADF (%m)\n");
+	ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_CAN_MULTI_CONN);
+	printf("ioctl(-1, NBD_SET_FLAGS, NBD_FLAG_CAN_MULTI_CONN) = -1 EBADF (%m)\n");
+
+	puts("+++ exited with 0 +++");
+	return 0;
+}
+
+#else
+
+SKIP_MAIN_UNDEFINED("HAVE_LINUX_NBD_H");
+
+#endif
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index 361984aa..69097bfb 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -118,6 +118,7 @@ ioctl_kvm_run-v
 ioctl_kvm_run_auxstr_vcpu
 ioctl_loop
 ioctl_mtd
+ioctl_nbd
 ioctl_perf
 ioctl_ptp
 ioctl_rtc
diff --git a/xlat/nbd_ioctl_cmds.in b/xlat/nbd_ioctl_cmds.in
new file mode 100644
index 00000000..34f891e3
--- /dev/null
+++ b/xlat/nbd_ioctl_cmds.in
@@ -0,0 +1,11 @@
+NBD_SET_SOCK		_IO( 0xab, 0 )
+NBD_SET_BLKSIZE		_IO( 0xab, 1 )
+NBD_SET_SIZE		_IO( 0xab, 2 )
+NBD_DO_IT		_IO( 0xab, 3 )
+NBD_CLEAR_SOCK		_IO( 0xab, 4 )
+NBD_CLEAR_QUE		_IO( 0xab, 5 )
+NBD_PRINT_DEBUG		_IO( 0xab, 6 )
+NBD_SET_SIZE_BLOCKS	_IO( 0xab, 7 )
+NBD_DISCONNECT		_IO( 0xab, 8 )
+NBD_SET_TIMEOUT		_IO( 0xab, 9 )
+NBD_SET_FLAGS		_IO( 0xab, 10)
diff --git a/xlat/nbd_ioctl_flags.in b/xlat/nbd_ioctl_flags.in
new file mode 100644
index 00000000..2040d221
--- /dev/null
+++ b/xlat/nbd_ioctl_flags.in
@@ -0,0 +1,8 @@
+NBD_FLAG_HAS_FLAGS	(1 << 0)
+NBD_FLAG_READ_ONLY	(1 << 1)
+NBD_FLAG_SEND_FLUSH	(1 << 2)
+NBD_FLAG_SEND_FUA	(1 << 3)
+NBD_FLAG_ROTATIONAL	(1 << 4)
+NBD_FLAG_SEND_TRIM	(1 << 5)
+NBD_FLAG_SEND_WRITE_ZEROES	(1 << 6)
+NBD_FLAG_CAN_MULTI_CONN	(1 << 8)
-- 
2.19.0



More information about the Strace-devel mailing list