2 * This file is part of ioctl_block strace test.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2017 The strace developers.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/ioctl.h>
38 #include <linux/blkpg.h>
39 #ifdef HAVE_STRUCT_BLK_USER_TRACE_SETUP
40 # include <linux/blktrace_api.h>
44 static const unsigned int magic = 0xdeadbeef;
45 static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0dedULL;
47 static struct xlat block_argless[] = {
56 #ifdef BLKTRACETEARDOWN
57 XLAT(BLKTRACETEARDOWN),
61 #define TEST_NULL_ARG(cmd) \
63 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", #cmd)
68 TEST_NULL_ARG(BLKBSZGET);
69 TEST_NULL_ARG(BLKBSZSET);
70 TEST_NULL_ARG(BLKFRAGET);
71 TEST_NULL_ARG(BLKGETSIZE);
72 TEST_NULL_ARG(BLKGETSIZE64);
74 TEST_NULL_ARG(BLKRAGET);
75 TEST_NULL_ARG(BLKROGET);
76 TEST_NULL_ARG(BLKROSET);
77 TEST_NULL_ARG(BLKSECTGET);
78 TEST_NULL_ARG(BLKSECTGET);
79 TEST_NULL_ARG(BLKSSZGET);
81 TEST_NULL_ARG(BLKALIGNOFF);
84 TEST_NULL_ARG(BLKDISCARD);
86 #ifdef BLKDISCARDZEROES
87 TEST_NULL_ARG(BLKDISCARDZEROES);
90 TEST_NULL_ARG(BLKIOMIN);
93 TEST_NULL_ARG(BLKIOOPT);
96 TEST_NULL_ARG(BLKPBSZGET);
99 TEST_NULL_ARG(BLKROTATIONAL);
102 TEST_NULL_ARG(BLKSECDISCARD);
105 TEST_NULL_ARG(BLKZEROOUT);
107 #if defined BLKTRACESETUP && defined HAVE_STRUCT_BLK_USER_TRACE_SETUP
108 TEST_NULL_ARG(BLKTRACESETUP);
111 ioctl(-1, BLKRASET, lmagic);
112 printf("ioctl(-1, BLKRASET, %lu) = -1 EBADF (%m)\n", lmagic);
114 ioctl(-1, BLKFRASET, lmagic);
115 printf("ioctl(-1, BLKFRASET, %lu) = -1 EBADF (%m)\n", lmagic);
117 TAIL_ALLOC_OBJECT_CONST_PTR(int, val_int);
120 ioctl(-1, BLKROSET, val_int);
121 printf("ioctl(-1, BLKROSET, [%d]) = -1 EBADF (%m)\n", *val_int);
123 ioctl(-1, BLKBSZSET, val_int);
124 printf("ioctl(-1, BLKBSZSET, [%d]) = -1 EBADF (%m)\n", *val_int);
126 uint64_t *pair_int64 = tail_alloc(sizeof(*pair_int64) * 2);
127 pair_int64[0] = 0xdeadbeefbadc0dedULL;
128 pair_int64[1] = 0xfacefeedcafef00dULL;
131 ioctl(-1, BLKDISCARD, pair_int64);
132 printf("ioctl(-1, BLKDISCARD, [%" PRIu64 ", %" PRIu64 "])"
133 " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
137 ioctl(-1, BLKSECDISCARD, pair_int64);
138 printf("ioctl(-1, BLKSECDISCARD, [%" PRIu64 ", %" PRIu64 "])"
139 " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
143 ioctl(-1, BLKZEROOUT, pair_int64);
144 printf("ioctl(-1, BLKZEROOUT, [%" PRIu64 ", %" PRIu64 "])"
145 " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
148 TAIL_ALLOC_OBJECT_CONST_PTR(struct blkpg_ioctl_arg, blkpg);
150 blkpg->flags = 0xdeadbeef;
151 blkpg->datalen = 0xbadc0ded;
152 blkpg->data = (void *) (unsigned long) 0xcafef00dfffffeedULL;
154 ioctl(-1, BLKPG, blkpg);
155 printf("ioctl(-1, BLKPG, {%s, flags=%d, datalen=%d"
156 ", data=%#lx}) = -1 EBADF (%m)\n",
157 "BLKPG_RESIZE_PARTITION", blkpg->flags, blkpg->datalen,
158 (unsigned long) blkpg->data);
160 TAIL_ALLOC_OBJECT_CONST_PTR(struct blkpg_partition, bp);
161 bp->start = 0xfac1fed2dad3bef4ULL;
162 bp->length = 0xfac5fed6dad7bef8ULL;
164 memset(bp->devname, 'A', sizeof(bp->devname));
165 memset(bp->volname, 'B', sizeof(bp->volname));
169 ioctl(-1, BLKPG, blkpg);
170 printf("ioctl(-1, BLKPG, {%s, flags=%d, datalen=%d"
171 ", data={start=%lld, length=%lld, pno=%d"
172 ", devname=\"%.*s\", volname=\"%.*s\"}})"
173 " = -1 EBADF (%m)\n",
174 "BLKPG_ADD_PARTITION",
175 blkpg->flags, blkpg->datalen,
176 bp->start, bp->length, bp->pno,
177 (int) sizeof(bp->devname) - 1, bp->devname,
178 (int) sizeof(bp->volname) - 1, bp->volname);
180 #if defined BLKTRACESETUP && defined HAVE_STRUCT_BLK_USER_TRACE_SETUP
181 TAIL_ALLOC_OBJECT_CONST_PTR(struct blk_user_trace_setup, buts);
182 fill_memory(buts, sizeof(*buts));
184 ioctl(-1, BLKTRACESETUP, buts);
185 printf("ioctl(-1, BLKTRACESETUP, {act_mask=%hu, buf_size=%u, buf_nr=%u"
186 ", start_lba=%" PRI__u64 ", end_lba=%" PRI__u64 ", pid=%u})"
187 " = -1 EBADF (%m)\n",
188 buts->act_mask, buts->buf_size, buts->buf_nr,
189 buts->start_lba, buts->end_lba, buts->pid);
193 for (i = 0; i < ARRAY_SIZE(block_argless); ++i) {
194 ioctl(-1, (unsigned long) block_argless[i].val, lmagic);
195 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", block_argless[i].str);
198 ioctl(-1, _IOC(_IOC_READ, 0x12, 0xfe, 0xff), lmagic);
199 printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
200 "_IOC(_IOC_READ, 0x12, 0xfe, 0xff)", lmagic);
202 puts("+++ exited with 0 +++");