• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	tst_require_root();
109 
110 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
111 
112 	tst_tmpdir();
113 
114 	fs_type = tst_dev_fs_type();
115 	device = tst_acquire_device(cleanup);
116 
117 	if (!device)
118 		tst_brkm(TCONF, cleanup, "Failed to acquire device");
119 
120 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
121 
122 	TEST_PAUSE;
123 
124 	/*
125 	 * mount a read-only file system for EROFS test
126 	 */
127 	SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
128 	SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
129 	mount_flag = 1;
130 	dir_fd = SAFE_OPEN(cleanup, MNT_POINT, O_DIRECTORY);
131 
132 	/*
133 	 * NOTE: the ELOOP test is written based on that the consecutive
134 	 * symlinks limits in kernel is hardwired to 40.
135 	 */
136 	SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
137 	SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
138 	for (i = 0; i < 43; i++)
139 		strcat(elooppathname, ELOPFILE);
140 }
141 
mknodat_verify(struct test_case_t * tc)142 static void mknodat_verify(struct test_case_t *tc)
143 {
144 	int fd = *(tc->dir_fd);
145 	char *pathname = tc->pathname;
146 	mode_t mode = tc->mode;
147 
148 	TEST(mknodat(fd, pathname, mode, 0));
149 
150 	if (TEST_ERRNO == tc->exp_errno) {
151 		tst_resm(TPASS | TTERRNO,
152 			 "mknodat() returned the expected value");
153 	} else {
154 		tst_resm(TFAIL | TTERRNO,
155 			 "mknodat() got unexpected return value; expected: "
156 			 "%d - %s", tc->exp_errno,
157 			 strerror(tc->exp_errno));
158 	}
159 
160 	if (TEST_ERRNO == 0 &&
161 	    tst_syscall(__NR_unlinkat, fd, pathname, 0) < 0) {
162 		tst_brkm(TBROK | TERRNO, cleanup, "unlinkat(%d, %s) "
163 			 "failed.", fd, pathname);
164 	}
165 }
166 
cleanup(void)167 static void cleanup(void)
168 {
169 	if (dir_fd > 0 && close(dir_fd) < 0)
170 		tst_resm(TWARN | TERRNO, "close(%d) failed", dir_fd);
171 	if (mount_flag && tst_umount(MNT_POINT) < 0)
172 		tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
173 
174 	if (device)
175 		tst_release_device(device);
176 
177 	tst_rmdir();
178 }
179