• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 /*
18  * Basic test for the BLKROSET and BLKROGET ioctls.
19  *
20  * - Set the device read only, read the value back.
21  * - Try to mount the device read write, expect failure.
22  * - Try to mount the device read only, expect success.
23  */
24 
25 #include <errno.h>
26 #include <sys/mount.h>
27 #include "tst_test.h"
28 
29 static int fd;
30 
verify_ioctl(void)31 static void verify_ioctl(void)
32 {
33 	int ro = 1;
34 
35 	SAFE_IOCTL(fd, BLKROGET, &ro);
36 
37 	if (ro == 0)
38 		tst_res(TPASS, "BLKROGET returned 0");
39 	else
40 		tst_res(TFAIL, "BLKROGET returned %i", ro);
41 
42 	ro = 1;
43 	SAFE_IOCTL(fd, BLKROSET, &ro);
44 
45 	ro = 0;
46 	SAFE_IOCTL(fd, BLKROGET, &ro);
47 
48 	if (ro == 0)
49 		tst_res(TFAIL, "BLKROGET returned 0");
50 	else
51 		tst_res(TPASS, "BLKROGET returned %i", ro);
52 
53 	TEST(mount(tst_device->dev, "mntpoint", tst_device->fs_type, 0, NULL));
54 
55 	if (TST_RET != -1) {
56 		tst_res(TFAIL, "Mounting RO device RW succeeded");
57 		tst_umount("mntpoint");
58 		goto next;
59 	}
60 
61 	if (TST_ERR == EACCES) {
62 		tst_res(TPASS | TERRNO, "Mounting RO device RW failed");
63 		goto next;
64 	}
65 
66 	tst_res(TFAIL | TERRNO,
67 		"Mounting RO device RW failed unexpectedly expected EACCES");
68 
69 next:
70 	TEST(mount(tst_device->dev, "mntpoint", tst_device->fs_type, MS_RDONLY, NULL));
71 
72 	if (TST_RET == 0) {
73 		tst_res(TPASS, "Mounting RO device RO works");
74 		tst_umount("mntpoint");
75 	} else {
76 		tst_res(TFAIL | TTERRNO, "Mounting RO device RO failed");
77 	}
78 
79 	ro = 0;
80 	SAFE_IOCTL(fd, BLKROSET, &ro);
81 }
82 
setup(void)83 static void setup(void)
84 {
85 	SAFE_MKDIR("mntpoint", 0777);
86 	fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
87 }
88 
cleanup(void)89 static void cleanup(void)
90 {
91 	if (fd > 0)
92 		SAFE_CLOSE(fd);
93 }
94 
95 static struct tst_test test = {
96 	.needs_tmpdir = 1,
97 	.format_device = 1,
98 	.needs_root = 1,
99 	.setup = setup,
100 	.cleanup = cleanup,
101 	.test_all = verify_ioctl,
102 };
103