]> granicus.if.org Git - strace/blob - tests/ioctl_block.c
Add copyright headers
[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  * Copyright (c) 2016-2017 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
18  *
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.
29  */
30
31 #include "tests.h"
32 #include <errno.h>
33 #include <inttypes.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/ioctl.h>
37 #include <linux/fs.h>
38 #include <linux/blkpg.h>
39 #ifdef HAVE_STRUCT_BLK_USER_TRACE_SETUP
40 # include <linux/blktrace_api.h>
41 #endif
42 #include "xlat.h"
43
44 static const unsigned int magic = 0xdeadbeef;
45 static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0dedULL;
46
47 static struct xlat block_argless[] = {
48         XLAT(BLKRRPART),
49         XLAT(BLKFLSBUF),
50 #ifdef BLKTRACESTART
51         XLAT(BLKTRACESTART),
52 #endif
53 #ifdef BLKTRACESTOP
54         XLAT(BLKTRACESTOP),
55 #endif
56 #ifdef BLKTRACETEARDOWN
57         XLAT(BLKTRACETEARDOWN),
58 #endif
59 };
60
61 #define TEST_NULL_ARG(cmd) \
62         ioctl(-1, cmd, 0); \
63         printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", #cmd)
64
65 int
66 main(void)
67 {
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);
73         TEST_NULL_ARG(BLKPG);
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);
80 #ifdef BLKALIGNOFF
81         TEST_NULL_ARG(BLKALIGNOFF);
82 #endif
83 #ifdef BLKDISCARD
84         TEST_NULL_ARG(BLKDISCARD);
85 #endif
86 #ifdef BLKDISCARDZEROES
87         TEST_NULL_ARG(BLKDISCARDZEROES);
88 #endif
89 #ifdef BLKIOMIN
90         TEST_NULL_ARG(BLKIOMIN);
91 #endif
92 #ifdef BLKIOOPT
93         TEST_NULL_ARG(BLKIOOPT);
94 #endif
95 #ifdef BLKPBSZGET
96         TEST_NULL_ARG(BLKPBSZGET);
97 #endif
98 #ifdef BLKROTATIONAL
99         TEST_NULL_ARG(BLKROTATIONAL);
100 #endif
101 #ifdef BLKSECDISCARD
102         TEST_NULL_ARG(BLKSECDISCARD);
103 #endif
104 #ifdef BLKZEROOUT
105         TEST_NULL_ARG(BLKZEROOUT);
106 #endif
107 #if defined BLKTRACESETUP && defined HAVE_STRUCT_BLK_USER_TRACE_SETUP
108         TEST_NULL_ARG(BLKTRACESETUP);
109 #endif
110
111         ioctl(-1, BLKRASET, lmagic);
112         printf("ioctl(-1, BLKRASET, %lu) = -1 EBADF (%m)\n", lmagic);
113
114         ioctl(-1, BLKFRASET, lmagic);
115         printf("ioctl(-1, BLKFRASET, %lu) = -1 EBADF (%m)\n", lmagic);
116
117         TAIL_ALLOC_OBJECT_CONST_PTR(int, val_int);
118         *val_int = magic;
119
120         ioctl(-1, BLKROSET, val_int);
121         printf("ioctl(-1, BLKROSET, [%d]) = -1 EBADF (%m)\n", *val_int);
122
123         ioctl(-1, BLKBSZSET, val_int);
124         printf("ioctl(-1, BLKBSZSET, [%d]) = -1 EBADF (%m)\n", *val_int);
125
126         uint64_t *pair_int64 = tail_alloc(sizeof(*pair_int64) * 2);
127         pair_int64[0] = 0xdeadbeefbadc0dedULL;
128         pair_int64[1] = 0xfacefeedcafef00dULL;
129
130 #ifdef BLKDISCARD
131         ioctl(-1, BLKDISCARD, pair_int64);
132         printf("ioctl(-1, BLKDISCARD, [%" PRIu64 ", %" PRIu64 "])"
133                " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
134 #endif
135
136 #ifdef BLKSECDISCARD
137         ioctl(-1, BLKSECDISCARD, pair_int64);
138         printf("ioctl(-1, BLKSECDISCARD, [%" PRIu64 ", %" PRIu64 "])"
139                " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
140 #endif
141
142 #ifdef BLKZEROOUT
143         ioctl(-1, BLKZEROOUT, pair_int64);
144         printf("ioctl(-1, BLKZEROOUT, [%" PRIu64 ", %" PRIu64 "])"
145                " = -1 EBADF (%m)\n", pair_int64[0], pair_int64[1]);
146 #endif
147
148         TAIL_ALLOC_OBJECT_CONST_PTR(struct blkpg_ioctl_arg, blkpg);
149         blkpg->op = 3;
150         blkpg->flags = 0xdeadbeef;
151         blkpg->datalen = 0xbadc0ded;
152         blkpg->data = (void *) (unsigned long) 0xcafef00dfffffeedULL;
153
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);
159
160         TAIL_ALLOC_OBJECT_CONST_PTR(struct blkpg_partition, bp);
161         bp->start = 0xfac1fed2dad3bef4ULL;
162         bp->length = 0xfac5fed6dad7bef8ULL;
163         bp->pno = magic;
164         memset(bp->devname, 'A', sizeof(bp->devname));
165         memset(bp->volname, 'B', sizeof(bp->volname));
166         blkpg->op = 1;
167         blkpg->data = bp;
168
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);
179
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));
183
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);
190 #endif
191
192         unsigned int i;
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);
196         }
197
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);
201
202         puts("+++ exited with 0 +++");
203         return 0;
204 }