1 /*
2 * Copyright (c) 2013 Fujitsu Ltd.
3 * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 /*
19 * Description:
20 * Verify that,
21 * 1. access() fails with -1 return value and sets errno to EROFS
22 * if write permission was requested for files on a read-only file system.
23 */
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <sys/mount.h>
35 #include <pwd.h>
36
37 #include "test.h"
38 #include "safe_macros.h"
39
40 static void setup(void);
41 static void access_verify(int i);
42 static void cleanup(void);
43
44 #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
45 S_IXGRP|S_IROTH|S_IXOTH)
46 #define MNT_POINT "mntpoint"
47
48 static const char *device;
49 static const char *fs_type;
50 static int mount_flag;
51
52 static struct test_case_t {
53 char *pathname;
54 int a_mode;
55 int exp_errno;
56 } test_cases[] = {
57 {MNT_POINT, W_OK, EROFS}
58 };
59
60 char *TCID = "access06";
61 int TST_TOTAL = ARRAY_SIZE(test_cases);
62
main(int ac,char ** av)63 int main(int ac, char **av)
64 {
65 int lc;
66 int i;
67
68 tst_parse_opts(ac, av, NULL, NULL);
69
70 setup();
71
72 for (lc = 0; TEST_LOOPING(lc); lc++) {
73 tst_count = 0;
74
75 for (i = 0; i < TST_TOTAL; i++)
76 access_verify(i);
77 }
78
79 cleanup();
80 tst_exit();
81 }
82
setup(void)83 static void setup(void)
84 {
85 tst_sig(NOFORK, DEF_HANDLER, cleanup);
86
87 tst_require_root();
88 tst_tmpdir();
89
90 fs_type = tst_dev_fs_type();
91 device = tst_acquire_device(cleanup);
92
93 if (!device)
94 tst_brkm(TCONF, cleanup, "Failed to obtain block device");
95
96 tst_mkfs(cleanup, device, fs_type, NULL, NULL);
97 SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
98
99 TEST_PAUSE;
100
101 /*
102 * mount a read-only file system for test EROFS
103 */
104 if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) {
105 tst_brkm(TBROK | TERRNO, cleanup,
106 "mount device:%s failed", device);
107 }
108 mount_flag = 1;
109 }
110
access_verify(int i)111 static void access_verify(int i)
112 {
113 char *file_name;
114 int access_mode;
115
116 file_name = test_cases[i].pathname;
117 access_mode = test_cases[i].a_mode;
118
119 TEST(access(file_name, access_mode));
120
121 if (TEST_RETURN != -1) {
122 tst_resm(TFAIL, "access(%s, %#o) succeeded unexpectedly",
123 file_name, access_mode);
124 return;
125 }
126
127 if (TEST_ERRNO == test_cases[i].exp_errno) {
128 tst_resm(TPASS | TTERRNO, "access failed as expected");
129 } else {
130 tst_resm(TFAIL | TTERRNO,
131 "access failed unexpectedly; expected: "
132 "%d - %s", test_cases[i].exp_errno,
133 strerror(test_cases[i].exp_errno));
134 }
135 }
136
cleanup(void)137 static void cleanup(void)
138 {
139 if (mount_flag && tst_umount(MNT_POINT) < 0) {
140 tst_resm(TWARN | TERRNO,
141 "umount device:%s failed", device);
142 }
143
144 if (device)
145 tst_release_device(device);
146
147 tst_rmdir();
148 }
149