1 /*
2 * This file is part of ioctl_block strace test.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
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 #include <errno.h>
32 #include <inttypes.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/ioctl.h>
36 #include <linux/fs.h>
37 #include <linux/blkpg.h>
38 #ifdef HAVE_STRUCT_BLK_USER_TRACE_SETUP
39 # include <linux/blktrace_api.h>
40 #endif
41 #include "xlat.h"
42
43 static const unsigned int magic = 0xdeadbeef;
44 static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0dedULL;
45
46 static struct xlat block_argless[] = {
47 XLAT(BLKRRPART),
48 XLAT(BLKFLSBUF),
49 #ifdef BLKTRACESTART
50 XLAT(BLKTRACESTART),
51 #endif
52 #ifdef BLKTRACESTOP
53 XLAT(BLKTRACESTOP),
54 #endif
55 #ifdef BLKTRACETEARDOWN
56 XLAT(BLKTRACETEARDOWN),
57 #endif
58 };
59
60 #define TEST_NULL_ARG(cmd) \
61 ioctl(-1, cmd, 0); \
62 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", #cmd)
63
64 int
main(void)65 main(void)
66 {
67 TEST_NULL_ARG(BLKBSZGET);
68 TEST_NULL_ARG(BLKBSZSET);
69 TEST_NULL_ARG(BLKFRAGET);
70 TEST_NULL_ARG(BLKGETSIZE);
71 TEST_NULL_ARG(BLKGETSIZE64);
72 TEST_NULL_ARG(BLKPG);
73 TEST_NULL_ARG(BLKRAGET);
74 TEST_NULL_ARG(BLKROGET);
75 TEST_NULL_ARG(BLKROSET);
76 TEST_NULL_ARG(BLKSECTGET);
77 TEST_NULL_ARG(BLKSECTGET);
78 TEST_NULL_ARG(BLKSSZGET);
79 #ifdef BLKALIGNOFF
80 TEST_NULL_ARG(BLKALIGNOFF);
81 #endif
82 #ifdef BLKDISCARD
83 TEST_NULL_ARG(BLKDISCARD);
84 #endif
85 #ifdef BLKDISCARDZEROES
86 TEST_NULL_ARG(BLKDISCARDZEROES);
87 #endif
88 #ifdef BLKIOMIN
89 TEST_NULL_ARG(BLKIOMIN);
90 #endif
91 #ifdef BLKIOOPT
92 TEST_NULL_ARG(BLKIOOPT);
93 #endif
94 #ifdef BLKPBSZGET
95 TEST_NULL_ARG(BLKPBSZGET);
96 #endif
97 #ifdef BLKROTATIONAL
98 TEST_NULL_ARG(BLKROTATIONAL);
99 #endif
100 #ifdef BLKSECDISCARD
101 TEST_NULL_ARG(BLKSECDISCARD);
102 #endif
103 #ifdef BLKZEROOUT
104 TEST_NULL_ARG(BLKZEROOUT);
105 #endif
106 #if defined BLKTRACESETUP && defined HAVE_STRUCT_BLK_USER_TRACE_SETUP
107 TEST_NULL_ARG(BLKTRACESETUP);
108 #endif
109
110 ioctl(-1, BLKRASET, lmagic);
111 printf("ioctl(-1, BLKRASET, %lu) = -1 EBADF (%m)\n", lmagic);
112
113 ioctl(-1, BLKFRASET, lmagic);
114 printf("ioctl(-1, BLKFRASET, %lu) = -1 EBADF (%m)\n", lmagic);
115
116 int *const val_int = tail_alloc(sizeof(*val_int));
117 *val_int = magic;
118
119 ioctl(-1, BLKROSET, val_int);
120 printf("ioctl(-1, BLKROSET, [%d]) = -1 EBADF (%m)\n", *val_int);
121
122 ioctl(-1, BLKBSZSET, val_int);
123 printf("ioctl(-1, BLKBSZSET, [%d]) = -1 EBADF (%m)\n", *val_int);
124
125 uint64_t *pair_int64 = tail_alloc(sizeof(*pair_int64) * 2);
126 pair_int64[0] = 0xdeadbeefbadc0dedULL;
127 pair_int64[1] = 0xfacefeedcafef00dULL;
128
129 #ifdef BLKDISCARD
130 ioctl(-1, BLKDISCARD, pair_int64);
131 printf("ioctl(-1, BLKDISCARD, [%" PRIu64 ", %" PRIu64 "])"
132 " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
133 #endif
134
135 #ifdef BLKSECDISCARD
136 ioctl(-1, BLKSECDISCARD, pair_int64);
137 printf("ioctl(-1, BLKSECDISCARD, [%" PRIu64 ", %" PRIu64 "])"
138 " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
139 #endif
140
141 #ifdef BLKZEROOUT
142 ioctl(-1, BLKZEROOUT, pair_int64);
143 printf("ioctl(-1, BLKZEROOUT, [%" PRIu64 ", %" PRIu64 "])"
144 " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
145 #endif
146
147 struct blkpg_ioctl_arg *const blkpg = tail_alloc(sizeof(*blkpg));
148 blkpg->op = 3;
149 blkpg->flags = 0xdeadbeef;
150 blkpg->datalen = 0xbadc0ded;
151 blkpg->data = (void *) (unsigned long) 0xcafef00dfffffeedULL;
152
153 ioctl(-1, BLKPG, blkpg);
154 printf("ioctl(-1, BLKPG, {%s, flags=%d, datalen=%d"
155 ", data=%#lx}) = -1 EBADF (%m)\n",
156 "BLKPG_RESIZE_PARTITION", blkpg->flags, blkpg->datalen,
157 (unsigned long) blkpg->data);
158
159 struct blkpg_partition *const bp = tail_alloc(sizeof(*bp));
160 bp->start = 0xfac1fed2dad3bef4ULL;
161 bp->length = 0xfac5fed6dad7bef8ULL;
162 bp->pno = magic;
163 memset(bp->devname, 'A', sizeof(bp->devname));
164 memset(bp->volname, 'B', sizeof(bp->volname));
165 blkpg->op = 1;
166 blkpg->data = bp;
167
168 ioctl(-1, BLKPG, blkpg);
169 printf("ioctl(-1, BLKPG, {%s, flags=%d, datalen=%d"
170 ", data={start=%lld, length=%lld, pno=%d"
171 ", devname=\"%.*s\", volname=\"%.*s\"}})"
172 " = -1 EBADF (%m)\n",
173 "BLKPG_ADD_PARTITION",
174 blkpg->flags, blkpg->datalen,
175 bp->start, bp->length, bp->pno,
176 (int) sizeof(bp->devname) - 1, bp->devname,
177 (int) sizeof(bp->volname) - 1, bp->volname);
178
179 #if defined BLKTRACESETUP && defined HAVE_STRUCT_BLK_USER_TRACE_SETUP
180 struct blk_user_trace_setup *const buts = tail_alloc(sizeof(*buts));
181 fill_memory(buts, sizeof(*buts));
182
183 ioctl(-1, BLKTRACESETUP, buts);
184 printf("ioctl(-1, BLKTRACESETUP, {act_mask=%hu, buf_size=%u, buf_nr=%u"
185 ", start_lba=%" PRI__u64 ", end_lba=%" PRI__u64 ", pid=%u})"
186 " = -1 EBADF (%m)\n",
187 buts->act_mask, buts->buf_size, buts->buf_nr,
188 buts->start_lba, buts->end_lba, buts->pid);
189 #endif
190
191 unsigned int i;
192 for (i = 0; i < ARRAY_SIZE(block_argless); ++i) {
193 ioctl(-1, (unsigned long) block_argless[i].val, lmagic);
194 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", block_argless[i].str);
195 }
196
197 ioctl(-1, _IOC(_IOC_READ, 0x12, 0xfe, 0xff), lmagic);
198 printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
199 "_IOC(_IOC_READ, 0x12, 0xfe, 0xff)", lmagic);
200
201 puts("+++ exited with 0 +++");
202 return 0;
203 }
204