xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioctl/ioctl04.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 BLKROSET and BLKROGET ioctls.
10  *
11  * - Set the device read only, read the value back.
12  * - Try to mount the device read write, expect failure.
13  * - Try to mount the device read only, expect success.
14  */
15 
16 #include <errno.h>
17 #include <sys/mount.h>
18 #include "tst_test.h"
19 
20 static int fd;
21 
verify_ioctl(void)22 static void verify_ioctl(void)
23 {
24 	int ro = 1;
25 
26 	SAFE_IOCTL(fd, BLKROGET, &ro);
27 
28 	if (ro == 0)
29 		tst_res(TPASS, "BLKROGET returned 0");
30 	else
31 		tst_res(TFAIL, "BLKROGET returned %i", ro);
32 
33 	ro = 1;
34 	SAFE_IOCTL(fd, BLKROSET, &ro);
35 
36 	ro = 0;
37 	SAFE_IOCTL(fd, BLKROGET, &ro);
38 
39 	if (ro == 0)
40 		tst_res(TFAIL, "BLKROGET returned 0");
41 	else
42 		tst_res(TPASS, "BLKROGET returned %i", ro);
43 
44 	TEST(mount(tst_device->dev, "mntpoint", tst_device->fs_type, 0, NULL));
45 
46 	if (TST_RET != -1) {
47 		tst_res(TFAIL, "Mounting RO device RW succeeded");
48 		tst_umount("mntpoint");
49 		goto next;
50 	}
51 
52 	if (TST_ERR == EACCES) {
53 		tst_res(TPASS | TTERRNO, "Mounting RO device RW failed");
54 		goto next;
55 	}
56 
57 	tst_res(TFAIL | TTERRNO,
58 		"Mounting RO device RW failed unexpectedly expected EACCES");
59 
60 next:
61 	TEST(mount(tst_device->dev, "mntpoint", tst_device->fs_type, MS_RDONLY, NULL));
62 
63 	if (TST_RET == 0) {
64 		tst_res(TPASS, "Mounting RO device RO works");
65 		tst_umount("mntpoint");
66 	} else {
67 		tst_res(TFAIL | TTERRNO, "Mounting RO device RO failed");
68 	}
69 
70 	ro = 0;
71 	SAFE_IOCTL(fd, BLKROSET, &ro);
72 }
73 
setup(void)74 static void setup(void)
75 {
76 	SAFE_MKDIR("mntpoint", 0777);
77 	fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
78 }
79 
cleanup(void)80 static void cleanup(void)
81 {
82 	if (fd > 0)
83 		SAFE_CLOSE(fd);
84 }
85 
86 static struct tst_test test = {
87 	.format_device = 1,
88 	.needs_root = 1,
89 	.setup = setup,
90 	.cleanup = cleanup,
91 	.test_all = verify_ioctl,
92 };
93