xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioctl/ioctl05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Cyril Hrubis <[email protected]>
4  */
5 
6 /*\
7  * [Description]
8  *
9  * Basic test for the BLKGETSIZE and BLKGETSIZE64 ioctls.
10  *
11  * - BLKGETSIZE returns size in 512 byte blocks BLKGETSIZE64 in bytes
12  *   compare that they return the same value.
13  * - lseek to the end of the device, this should work
14  * - try to read from the device, read should return 0
15  */
16 
17 #include <stdint.h>
18 #include <errno.h>
19 #include <sys/mount.h>
20 #include "tst_test.h"
21 
22 static int fd;
23 
verify_ioctl(void)24 static void verify_ioctl(void)
25 {
26 	unsigned long size = 0;
27 	uint64_t size64 = 0;
28 	char buf;
29 	int ret;
30 
31 	fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
32 
33 	SAFE_IOCTL(fd, BLKGETSIZE, &size);
34 	SAFE_IOCTL(fd, BLKGETSIZE64, &size64);
35 
36 	if (size == size64/512) {
37 		tst_res(TPASS, "BLKGETSIZE returned %lu, BLKGETSIZE64 %llu",
38 			size, (unsigned long long)size64);
39 	} else {
40 		tst_res(TFAIL,
41 			"BLKGETSIZE returned %lu, BLKGETSIZE64 returned %llu",
42 			size, (unsigned long long)size64);
43 	}
44 
45 	if (lseek(fd, size * 512, SEEK_SET) !=  (off_t)size * 512) {
46 		tst_res(TFAIL | TERRNO,
47 			"Cannot lseek to the end of the device");
48 	} else {
49 		tst_res(TPASS, "Could lseek to the end of the device");
50 	}
51 
52 	ret = read(fd, &buf, 1);
53 
54 	if (ret == 0) {
55 		tst_res(TPASS,
56 			"Got EOF when trying to read after the end of device");
57 	} else {
58 		tst_res(TFAIL | TERRNO,
59 			"Read at the end of device returned %i", ret);
60 	}
61 
62 	SAFE_CLOSE(fd);
63 }
64 
cleanup(void)65 static void cleanup(void)
66 {
67 	if (fd > 0)
68 		SAFE_CLOSE(fd);
69 }
70 
71 static struct tst_test test = {
72 	.needs_device = 1,
73 	.needs_root = 1,
74 	.cleanup = cleanup,
75 	.test_all = verify_ioctl,
76 };
77