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