• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
4  */
5 /*
6  * Basic test for the BLKROSET and BLKROGET ioctls.
7  *
8  * - Set the device read only, read the value back.
9  * - Try to mount the device read write, expect failure.
10  * - Try to mount the device read only, expect success.
11  */
12 
13 #include <errno.h>
14 #include <sys/mount.h>
15 #include "tst_test.h"
16 
17 static int fd;
18 
verify_ioctl(void)19 static void verify_ioctl(void)
20 {
21 	int ro = 1;
22 
23 	SAFE_IOCTL(fd, BLKROGET, &ro);
24 
25 	if (ro == 0)
26 		tst_res(TPASS, "BLKROGET returned 0");
27 	else
28 		tst_res(TFAIL, "BLKROGET returned %i", ro);
29 
30 	ro = 1;
31 	SAFE_IOCTL(fd, BLKROSET, &ro);
32 
33 	ro = 0;
34 	SAFE_IOCTL(fd, BLKROGET, &ro);
35 
36 	if (ro == 0)
37 		tst_res(TFAIL, "BLKROGET returned 0");
38 	else
39 		tst_res(TPASS, "BLKROGET returned %i", ro);
40 
41 	TEST(mount(tst_device->dev, "mntpoint", tst_device->fs_type, 0, NULL));
42 
43 	if (TST_RET != -1) {
44 		tst_res(TFAIL, "Mounting RO device RW succeeded");
45 		tst_umount("mntpoint");
46 		goto next;
47 	}
48 
49 	if (TST_ERR == EACCES) {
50 		tst_res(TPASS | TERRNO, "Mounting RO device RW failed");
51 		goto next;
52 	}
53 
54 	tst_res(TFAIL | TERRNO,
55 		"Mounting RO device RW failed unexpectedly expected EACCES");
56 
57 next:
58 	TEST(mount(tst_device->dev, "mntpoint", tst_device->fs_type, MS_RDONLY, NULL));
59 
60 	if (TST_RET == 0) {
61 		tst_res(TPASS, "Mounting RO device RO works");
62 		tst_umount("mntpoint");
63 	} else {
64 		tst_res(TFAIL | TTERRNO, "Mounting RO device RO failed");
65 	}
66 
67 	ro = 0;
68 	SAFE_IOCTL(fd, BLKROSET, &ro);
69 }
70 
setup(void)71 static void setup(void)
72 {
73 	SAFE_MKDIR("mntpoint", 0777);
74 	fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
75 }
76 
cleanup(void)77 static void cleanup(void)
78 {
79 	if (fd > 0)
80 		SAFE_CLOSE(fd);
81 }
82 
83 static struct tst_test test = {
84 	.format_device = 1,
85 	.needs_root = 1,
86 	.setup = setup,
87 	.cleanup = cleanup,
88 	.test_all = verify_ioctl,
89 };
90