]> granicus.if.org Git - strace/blob - tests/ioctl_block.c
tests: make magic values in ioctl_block test distinctive
[strace] / tests / ioctl_block.c
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) 0xdeadbeefbadc0ded;
45
46 static void
47 init_magic(void *addr, const unsigned int size)
48 {
49         unsigned int *p = addr;
50         const unsigned int *end = addr + size - sizeof(int);
51
52         for (; p <= end; ++p)
53                 *(unsigned int *) p = magic + (p - (unsigned int *) addr);
54 }
55
56 static struct xlat block_argless[] = {
57         XLAT(BLKRRPART),
58         XLAT(BLKFLSBUF),
59 #ifdef BLKTRACESTART
60         XLAT(BLKTRACESTART),
61 #endif
62 #ifdef BLKTRACESTOP
63         XLAT(BLKTRACESTOP),
64 #endif
65 #ifdef BLKTRACETEARDOWN
66         XLAT(BLKTRACETEARDOWN),
67 #endif
68 };
69
70 #define TEST_NULL_ARG(cmd) \
71         ioctl(-1, cmd, 0); \
72         printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", #cmd)
73
74 int
75 main(void)
76 {
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);
82         TEST_NULL_ARG(BLKPG);
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);
89 #ifdef BLKALIGNOFF
90         TEST_NULL_ARG(BLKALIGNOFF);
91 #endif
92 #ifdef BLKDISCARD
93         TEST_NULL_ARG(BLKDISCARD);
94 #endif
95 #ifdef BLKDISCARDZEROES
96         TEST_NULL_ARG(BLKDISCARDZEROES);
97 #endif
98 #ifdef BLKIOMIN
99         TEST_NULL_ARG(BLKIOMIN);
100 #endif
101 #ifdef BLKIOOPT
102         TEST_NULL_ARG(BLKIOOPT);
103 #endif
104 #ifdef BLKPBSZGET
105         TEST_NULL_ARG(BLKPBSZGET);
106 #endif
107 #ifdef BLKROTATIONAL
108         TEST_NULL_ARG(BLKROTATIONAL);
109 #endif
110 #ifdef BLKSECDISCARD
111         TEST_NULL_ARG(BLKSECDISCARD);
112 #endif
113 #ifdef BLKZEROOUT
114         TEST_NULL_ARG(BLKZEROOUT);
115 #endif
116 #if defined BLKTRACESETUP && defined HAVE_STRUCT_BLK_USER_TRACE_SETUP
117         TEST_NULL_ARG(BLKTRACESETUP);
118 #endif
119
120         ioctl(-1, BLKRASET, lmagic);
121         printf("ioctl(-1, BLKRASET, %lu) = -1 EBADF (%m)\n", lmagic);
122
123         ioctl(-1, BLKFRASET, lmagic);
124         printf("ioctl(-1, BLKFRASET, %lu) = -1 EBADF (%m)\n", lmagic);
125
126         int *const val_int = tail_alloc(sizeof(*val_int));
127         *val_int = magic;
128
129         ioctl(-1, BLKROSET, val_int);
130         printf("ioctl(-1, BLKROSET, [%d]) = -1 EBADF (%m)\n", *val_int);
131
132         ioctl(-1, BLKBSZSET, val_int);
133         printf("ioctl(-1, BLKBSZSET, [%d]) = -1 EBADF (%m)\n", *val_int);
134
135         uint64_t *pair_int64 = tail_alloc(sizeof(*pair_int64) * 2);
136         pair_int64[0] = 0xdeadbeefbadc0ded;
137         pair_int64[1] = 0xfacefeedcafef00d;
138
139 #ifdef BLKDISCARD
140         ioctl(-1, BLKDISCARD, pair_int64);
141         printf("ioctl(-1, BLKDISCARD, [%" PRIu64 ", %" PRIu64 "])"
142                " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
143 #endif
144
145 #ifdef BLKSECDISCARD
146         ioctl(-1, BLKSECDISCARD, pair_int64);
147         printf("ioctl(-1, BLKSECDISCARD, [%" PRIu64 ", %" PRIu64 "])"
148                " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
149 #endif
150
151 #ifdef BLKZEROOUT
152         ioctl(-1, BLKZEROOUT, pair_int64);
153         printf("ioctl(-1, BLKZEROOUT, [%" PRIu64 ", %" PRIu64 "])"
154                " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
155 #endif
156
157         struct blkpg_ioctl_arg *const blkpg = tail_alloc(sizeof(*blkpg));
158         blkpg->op = 3;
159         blkpg->flags = 0xdeadbeef;
160         blkpg->datalen = 0xbadc0ded;
161         blkpg->data = (void *) (unsigned long) 0xcafef00dfffffeed;
162
163         ioctl(-1, BLKPG, blkpg);
164         printf("ioctl(-1, BLKPG, {%s, flags=%d, datalen=%d"
165                ", data=%#lx}) = -1 EBADF (%m)\n",
166                "BLKPG_RESIZE_PARTITION", blkpg->flags, blkpg->datalen,
167                (unsigned long) blkpg->data);
168
169         struct blkpg_partition *const bp = tail_alloc(sizeof(*bp));
170         bp->start = 0xfac1fed2dad3bef4;
171         bp->length = 0xfac5fed6dad7bef8;
172         bp->pno = magic;
173         memset(bp->devname, 'A', sizeof(bp->devname));
174         memset(bp->volname, 'B', sizeof(bp->volname));
175         blkpg->op = 1;
176         blkpg->data = bp;
177
178         ioctl(-1, BLKPG, blkpg);
179         printf("ioctl(-1, BLKPG, {%s, flags=%d, datalen=%d"
180                ", data={start=%lld, length=%lld, pno=%d"
181                ", devname=\"%.*s\", volname=\"%.*s\"}})"
182                " = -1 EBADF (%m)\n",
183                "BLKPG_ADD_PARTITION",
184                blkpg->flags, blkpg->datalen,
185                bp->start, bp->length, bp->pno,
186                (int) sizeof(bp->devname) - 1, bp->devname,
187                (int) sizeof(bp->volname) - 1, bp->volname);
188
189 #if defined BLKTRACESETUP && defined HAVE_STRUCT_BLK_USER_TRACE_SETUP
190         struct blk_user_trace_setup *const buts = tail_alloc(sizeof(*buts));
191         init_magic(buts, sizeof(*buts));
192
193         ioctl(-1, BLKTRACESETUP, buts);
194         printf("ioctl(-1, BLKTRACESETUP, {act_mask=%hu, buf_size=%u, buf_nr=%u"
195                ", start_lba=%" PRI__u64 ", end_lba=%" PRI__u64 ", pid=%u})"
196                " = -1 EBADF (%m)\n",
197                buts->act_mask, buts->buf_size, buts->buf_nr,
198                buts->start_lba, buts->end_lba, buts->pid);
199 #endif
200
201         unsigned int i;
202         for (i = 0; i < ARRAY_SIZE(block_argless); ++i) {
203                 ioctl(-1, (unsigned long) block_argless[i].val, lmagic);
204                 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", block_argless[i].str);
205         }
206
207         ioctl(-1, _IOC(_IOC_READ, 0x12, 0xfe, 0xff), lmagic);
208         printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
209                "_IOC(_IOC_READ, 0x12, 0xfe, 0xff)", lmagic);
210
211         puts("+++ exited with 0 +++");
212         return 0;
213 }