[PATCH v3] ioctl: implement NBD_* ioctl decoding

Dmitry V. Levin ldv at altlinux.org
Sat Sep 22 22:36:38 UTC 2018


On Sat, Sep 22, 2018 at 03:09:50PM +0200, Elvira Khabirova wrote:
> * 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.

The change looked good so I tried to build it on various platforms.
Here is the result.

[...]
> 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

Old operating systems provide kernel headers without linux kernel commit
v2.6.19-rc2~54, resulting to the following diagnostics:

checking linux/nbd.h usability... no
checking linux/nbd.h presence... yes
configure: WARNING: linux/nbd.h: present but cannot be compiled
configure: WARNING: linux/nbd.h:     check for missing prerequisite headers?
configure: WARNING: linux/nbd.h: see the Autoconf documentation
configure: WARNING: linux/nbd.h:     section "Present But Cannot Be Compiled"
configure: WARNING: linux/nbd.h: proceeding with the compiler's result
checking for linux/nbd.h... no

I suggest to imitate the fix in the configure check, e.g.
AC_CHECK_HEADERS([linux/nbd.h],,, [#include <linux/types.h>])

[...]
> diff --git a/nbd_ioctl.c b/nbd_ioctl.c
> new file mode 100644
> index 00000000..fd666b14
> --- /dev/null
> +++ b/nbd_ioctl.c
> @@ -0,0 +1,75 @@
> +/*
> + * 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 "print_fields.h"
> +#include <linux/ioctl.h>
> +
> +#ifdef HAVE_LINUX_NBD_H
> +# include <linux/nbd.h>
> +#endif

For the same reason I suggest to include <linux/types.h>
before <linux/nbd.h> here and in tests/ioctl_nbd.c file.

[...]
> diff --git a/tests/ioctl_nbd.c b/tests/ioctl_nbd.c
> new file mode 100644
> index 00000000..9f771ee8
> --- /dev/null
> +++ b/tests/ioctl_nbd.c
> @@ -0,0 +1,113 @@
> +/*
> + * 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"
> +
> +
> +#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
> +
> +#define RVAL_EBADF " = -1 EBADF (%m)\n"
> +
> +int
> +main(void)
> +{
> +	static const unsigned long ubeef = (unsigned long) 0xcafef00ddeadbeefULL;
> +	int sock;
> +
> +	skip_if_unavailable("/proc/self/fd/");
> +
> +	ioctl(-1, NBD_DISCONNECT, NULL);
> +	printf("ioctl(-1, NBD_DISCONNECT)" RVAL_EBADF);
> +	ioctl(-1, NBD_CLEAR_SOCK, NULL);
> +	printf("ioctl(-1, NBD_CLEAR_SOCK)" RVAL_EBADF);
> +	ioctl(-1, NBD_DO_IT, NULL);
> +	printf("ioctl(-1, NBD_DO_IT)" RVAL_EBADF);
> +	ioctl(-1, NBD_CLEAR_QUE, NULL);
> +	printf("ioctl(-1, NBD_CLEAR_QUE)" RVAL_EBADF);
> +	ioctl(-1, NBD_PRINT_DEBUG, NULL);
> +	printf("ioctl(-1, NBD_PRINT_DEBUG)" RVAL_EBADF);
> +
> +	sock = socket(AF_UNIX, SOCK_STREAM, 0);
> +	if (sock != -1) {
> +		ioctl(-1, NBD_SET_SOCK, sock);
> +		printf("ioctl(-1, NBD_SET_SOCK, %d<UNIX:[%lu]>)" RVAL_EBADF,
> +		       sock, inode_of_sockfd(sock));
> +	}

Old operating systems do not support this feature.
For example, on el6-test.fedorainfracloud.org you can see the following:

-ioctl(-1, NBD_SET_SOCK, 3<UNIX:[3054577]>) = -1 EBADF (Bad file descriptor)
+ioctl(-1, NBD_SET_SOCK, 3<socket:[3054577]>) = -1 EBADF (Bad file descriptor)

I suggest to skip this part of the test (or the whole test)
on systems where net-yy-unix test is skipped.
For example, on el6-test.fedorainfracloud.org that test is skipped because
netlink_unix_diag prints "NLMSG_ERROR: Invalid argument" and exits with
code 77.

[...]
> +	ioctl(-1, _IO(0xab, 0xb), NULL);
> +	printf("ioctl(-1, _IOC(0, 0xab, 0xb, 0), 0)" RVAL_EBADF);

On alpha, ppc, mips, and sparc this part of the test fails:

-ioctl(-1, _IOC(0, 0xab, 0xb, 0), 0) = -1 EBADF (Bad file descriptor)
+ioctl(-1, _IOC(_IOC_NONE, 0xab, 0xb, 0), 0) = -1 EBADF (Bad file descriptor)

I suppose the reason is that _IOC_NONE == 1 on those architectures.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20180923/ffcfe972/attachment.bin>


More information about the Strace-devel mailing list