1 /*
2 * Copyright (c) 2014 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) mknod(2) returns -1 and sets errno to EROFS if pathname refers to
22 * a file on a read-only file system.
23 * 2) mknod(2) returns -1 and sets errno to ELOOP if Too many symbolic
24 * links were encountered in resolving pathname.
25 */
26
27 #define _GNU_SOURCE
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <signal.h>
35 #include <sys/mount.h>
36
37 #include "test.h"
38 #include "safe_macros.h"
39 #include "lapi/fcntl.h"
40 #include "mknodat.h"
41
42 static void setup(void);
43 static void cleanup(void);
44
45 #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
46 S_IXGRP|S_IROTH|S_IXOTH)
47 #define MNT_POINT "mntpoint"
48
49 #define FIFOMODE (S_IFIFO | S_IRUSR | S_IRGRP | S_IROTH)
50 #define FREGMODE (S_IFREG | S_IRUSR | S_IRGRP | S_IROTH)
51 #define SOCKMODE (S_IFSOCK | S_IRUSR | S_IRGRP | S_IROTH)
52
53 static const char *device;
54 static int mount_flag;
55
56 static int dir_fd;
57 static int curfd = AT_FDCWD;
58
59 #define ELOPFILE "/test_eloop"
60 static char elooppathname[sizeof(ELOPFILE) * 43] = ".";
61
62 static struct test_case_t {
63 int *dir_fd;
64 char *pathname;
65 mode_t mode;
66 int exp_errno;
67 } test_cases[] = {
68 { &curfd, "tnode1", FIFOMODE, 0 },
69 { &curfd, "tnode2", FREGMODE, 0 },
70 { &curfd, "tnode3", SOCKMODE, 0 },
71 { &dir_fd, "tnode4", FIFOMODE, EROFS },
72 { &dir_fd, "tnode5", FREGMODE, EROFS },
73 { &dir_fd, "tnode6", SOCKMODE, EROFS },
74 { &curfd, elooppathname, FIFOMODE, ELOOP },
75 { &curfd, elooppathname, FREGMODE, ELOOP },
76 { &curfd, elooppathname, SOCKMODE, ELOOP },
77 };
78
79 static void mknodat_verify(struct test_case_t *tc);
80
81 char *TCID = "mknodat";
82 int TST_TOTAL = ARRAY_SIZE(test_cases);
83
main(int ac,char ** av)84 int main(int ac, char **av)
85 {
86 int lc, i;
87
88 tst_parse_opts(ac, av, NULL, NULL);
89
90 setup();
91
92 for (lc = 0; TEST_LOOPING(lc); lc++) {
93 tst_count = 0;
94
95 for (i = 0; i < TST_TOTAL; i++)
96 mknodat_verify(&test_cases[i]);
97 }
98
99 cleanup();
100 tst_exit();
101 }
102
setup(void)103 static void setup(void)
104 {
105 int i;
106 const char *fs_type;
107
108 if (tst_kvercmp(2, 6, 16) < 0) {
109 tst_brkm(TCONF, NULL, "This test can only run on kernels "
110 "that are 2.6.16 and higher");
111 }
112
113 tst_require_root();
114
115 tst_sig(NOFORK, DEF_HANDLER, cleanup);
116
117 tst_tmpdir();
118
119 fs_type = tst_dev_fs_type();
120 device = tst_acquire_device(cleanup);
121
122 if (!device)
123 tst_brkm(TCONF, cleanup, "Failed to acquire device");
124
125 tst_mkfs(cleanup, device, fs_type, NULL, NULL);
126
127 TEST_PAUSE;
128
129 /*
130 * mount a read-only file system for EROFS test
131 */
132 SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
133 SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
134 mount_flag = 1;
135 dir_fd = SAFE_OPEN(cleanup, MNT_POINT, O_DIRECTORY);
136
137 /*
138 * NOTE: the ELOOP test is written based on that the consecutive
139 * symlinks limits in kernel is hardwired to 40.
140 */
141 SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
142 SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
143 for (i = 0; i < 43; i++)
144 strcat(elooppathname, ELOPFILE);
145 }
146
mknodat_verify(struct test_case_t * tc)147 static void mknodat_verify(struct test_case_t *tc)
148 {
149 int fd = *(tc->dir_fd);
150 char *pathname = tc->pathname;
151 mode_t mode = tc->mode;
152
153 TEST(mknodat(fd, pathname, mode, 0));
154
155 if (TEST_ERRNO == tc->exp_errno) {
156 tst_resm(TPASS | TTERRNO,
157 "mknodat() returned the expected value");
158 } else {
159 tst_resm(TFAIL | TTERRNO,
160 "mknodat() got unexpected return value; expected: "
161 "%d - %s", tc->exp_errno,
162 strerror(tc->exp_errno));
163 }
164
165 if (TEST_ERRNO == 0 &&
166 ltp_syscall(__NR_unlinkat, fd, pathname, 0) < 0) {
167 tst_brkm(TBROK | TERRNO, cleanup, "unlinkat(%d, %s) "
168 "failed.", fd, pathname);
169 }
170 }
171
cleanup(void)172 static void cleanup(void)
173 {
174 if (dir_fd > 0 && close(dir_fd) < 0)
175 tst_resm(TWARN | TERRNO, "close(%d) failed", dir_fd);
176 if (mount_flag && tst_umount(MNT_POINT) < 0)
177 tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
178
179 if (device)
180 tst_release_device(device);
181
182 tst_rmdir();
183 }
184