2 * This file is part of ioctl_block strace test.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
35 #include <sys/ioctl.h>
37 #include <linux/blkpg.h>
38 #ifdef HAVE_STRUCT_BLK_USER_TRACE_SETUP
39 # include <linux/blktrace_api.h>
43 static const unsigned int magic = 0xdeadbeef;
44 static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0ded;
47 init_magic(void *addr, const unsigned int size)
49 unsigned int *p = addr;
50 const unsigned int *end = addr + size - sizeof(int);
53 *(unsigned int *) p = magic;
56 static struct xlat block_argless[] = {
65 #ifdef BLKTRACETEARDOWN
66 XLAT(BLKTRACETEARDOWN),
70 #define TEST_NULL_ARG(cmd) \
72 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", #cmd)
77 TEST_NULL_ARG(BLKBSZGET);
78 TEST_NULL_ARG(BLKBSZSET);
79 TEST_NULL_ARG(BLKFRAGET);
80 TEST_NULL_ARG(BLKGETSIZE);
81 TEST_NULL_ARG(BLKGETSIZE64);
83 TEST_NULL_ARG(BLKRAGET);
84 TEST_NULL_ARG(BLKROGET);
85 TEST_NULL_ARG(BLKROSET);
86 TEST_NULL_ARG(BLKSECTGET);
87 TEST_NULL_ARG(BLKSECTGET);
88 TEST_NULL_ARG(BLKSSZGET);
90 TEST_NULL_ARG(BLKALIGNOFF);
93 TEST_NULL_ARG(BLKDAXGET);
96 TEST_NULL_ARG(BLKDISCARD);
98 #ifdef BLKDISCARDZEROES
99 TEST_NULL_ARG(BLKDISCARDZEROES);
102 TEST_NULL_ARG(BLKIOMIN);
105 TEST_NULL_ARG(BLKIOOPT);
108 TEST_NULL_ARG(BLKPBSZGET);
111 TEST_NULL_ARG(BLKROTATIONAL);
114 TEST_NULL_ARG(BLKSECDISCARD);
117 TEST_NULL_ARG(BLKZEROOUT);
119 #if defined BLKTRACESETUP && defined HAVE_STRUCT_BLK_USER_TRACE_SETUP
120 TEST_NULL_ARG(BLKTRACESETUP);
123 ioctl(-1, BLKRASET, lmagic);
124 printf("ioctl(-1, BLKRASET, %lu) = -1 EBADF (%m)\n", lmagic);
126 ioctl(-1, BLKFRASET, lmagic);
127 printf("ioctl(-1, BLKFRASET, %lu) = -1 EBADF (%m)\n", lmagic);
129 int *const val_int = tail_alloc(sizeof(*val_int));
132 ioctl(-1, BLKROSET, val_int);
133 printf("ioctl(-1, BLKROSET, [%d]) = -1 EBADF (%m)\n", *val_int);
135 ioctl(-1, BLKBSZSET, val_int);
136 printf("ioctl(-1, BLKBSZSET, [%d]) = -1 EBADF (%m)\n", *val_int);
138 uint64_t *pair_int64 = tail_alloc(sizeof(*pair_int64) * 2);
139 pair_int64[0] = 0xdeadbeefbadc0ded;
140 pair_int64[1] = 0xfacefeedcafef00d;
143 ioctl(-1, BLKDISCARD, pair_int64);
144 printf("ioctl(-1, BLKDISCARD, [%" PRIu64 ", %" PRIu64 "])"
145 " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
149 ioctl(-1, BLKSECDISCARD, pair_int64);
150 printf("ioctl(-1, BLKSECDISCARD, [%" PRIu64 ", %" PRIu64 "])"
151 " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
155 ioctl(-1, BLKZEROOUT, pair_int64);
156 printf("ioctl(-1, BLKZEROOUT, [%" PRIu64 ", %" PRIu64 "])"
157 " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
160 struct blkpg_ioctl_arg *const blkpg = tail_alloc(sizeof(*blkpg));
162 blkpg->flags = 0xdeadbeef;
163 blkpg->datalen = 0xbadc0ded;
164 blkpg->data = (void *) (unsigned long) 0xfacefeedcafef00d;
166 ioctl(-1, BLKPG, blkpg);
167 printf("ioctl(-1, BLKPG, {%s, flags=%d, datalen=%d"
168 ", data=%#lx}) = -1 EBADF (%m)\n",
169 "BLKPG_RESIZE_PARTITION", blkpg->flags, blkpg->datalen,
170 (unsigned long) blkpg->data);
172 struct blkpg_partition *const bp = tail_alloc(sizeof(*bp));
173 bp->start = 0xfac1fed2dad3bef4;
174 bp->length = 0xfac5fed6dad7bef8;
176 memset(bp->devname, 'A', sizeof(bp->devname));
177 memset(bp->volname, 'B', sizeof(bp->volname));
181 ioctl(-1, BLKPG, blkpg);
182 printf("ioctl(-1, BLKPG, {%s, flags=%d, datalen=%d"
183 ", data={start=%lld, length=%lld, pno=%d"
184 ", devname=\"%.*s\", volname=\"%.*s\"}})"
185 " = -1 EBADF (%m)\n",
186 "BLKPG_ADD_PARTITION",
187 blkpg->flags, blkpg->datalen,
188 bp->start, bp->length, bp->pno,
189 (int) sizeof(bp->devname) - 1, bp->devname,
190 (int) sizeof(bp->volname) - 1, bp->volname);
192 #if defined BLKTRACESETUP && defined HAVE_STRUCT_BLK_USER_TRACE_SETUP
193 struct blk_user_trace_setup *const buts = tail_alloc(sizeof(*buts));
194 init_magic(buts, sizeof(*buts));
196 ioctl(-1, BLKTRACESETUP, buts);
197 printf("ioctl(-1, BLKTRACESETUP, {act_mask=%hu, buf_size=%u, buf_nr=%u"
198 ", start_lba=%" PRI__u64 ", end_lba=%" PRI__u64 ", pid=%u})"
199 " = -1 EBADF (%m)\n",
200 buts->act_mask, buts->buf_size, buts->buf_nr,
201 buts->start_lba, buts->end_lba, buts->pid);
205 for (i = 0; i < ARRAY_SIZE(block_argless); ++i) {
206 ioctl(-1, (unsigned long) block_argless[i].val, lmagic);
207 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", block_argless[i].str);
210 ioctl(-1, _IOC(_IOC_READ, 0x12, 0xfe, 0xff), lmagic);
211 printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
212 "_IOC(_IOC_READ, 0x12, 0xfe, 0xff)", lmagic);
214 puts("+++ exited with 0 +++");