1 /*
2  * This file is part of ioctl_inotify strace test.
3  *
4  * Copyright (c) 2018 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "tests.h"
31 
32 #include <inttypes.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <asm/unistd.h>
37 #include <linux/ioctl.h>
38 
39 #ifndef INOTIFY_IOC_SETNEXTWD
40 # define INOTIFY_IOC_SETNEXTWD  _IOW('I', 0, int32_t)
41 #endif
42 
43 static long
sys_ioctl(kernel_long_t fd,kernel_ulong_t cmd,kernel_ulong_t arg)44 sys_ioctl(kernel_long_t fd, kernel_ulong_t cmd, kernel_ulong_t arg)
45 {
46 	return syscall(__NR_ioctl, fd, cmd, arg);
47 }
48 
49 int
main(void)50 main(void)
51 {
52 	static const kernel_ulong_t unknown_inotify_cmd =
53 		(kernel_ulong_t) 0xbadc0dedfeed49edULL;
54 	static const kernel_ulong_t magic =
55 		(kernel_ulong_t) 0xdeadbeefbadc0dedULL;
56 
57 	/* Unknown inotify commands */
58 	sys_ioctl(-1, unknown_inotify_cmd, magic);
59 	printf("ioctl(-1, _IOC(%s_IOC_READ|_IOC_WRITE, 0x49, %#x, %#x), "
60 	       "%#lx) = -1 EBADF (%m)\n",
61 	       _IOC_DIR((unsigned int) unknown_inotify_cmd) & _IOC_NONE ?
62 	       "_IOC_NONE|" : "",
63 	       _IOC_NR((unsigned int) unknown_inotify_cmd),
64 	       _IOC_SIZE((unsigned int) unknown_inotify_cmd),
65 	       (unsigned long) magic);
66 
67 	sys_ioctl(-1, INOTIFY_IOC_SETNEXTWD + 1, magic);
68 	printf("ioctl(-1, _IOC(_IOC_WRITE, 0x49, %#x, %#x), %#lx)"
69 	       " = -1 EBADF (%m)\n",
70 	       (unsigned int) _IOC_NR(INOTIFY_IOC_SETNEXTWD + 1),
71 	       (unsigned int) _IOC_SIZE(INOTIFY_IOC_SETNEXTWD + 1),
72 	       (unsigned long) magic);
73 
74 	/* INOTIFY_IOC_SETNEXTWD */
75 	sys_ioctl(-1, INOTIFY_IOC_SETNEXTWD, magic);
76 	printf("ioctl(-1, INOTIFY_IOC_SETNEXTWD, %d) = -1 EBADF (%m)\n",
77 	       (int) magic);
78 
79 	puts("+++ exited with 0 +++");
80 	return 0;
81 }
82