• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2006
3  *  Author: Yi Yang <yyangcdl@cn.ibm.com>
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program;  if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 /*
20  * Description:
21  *   Verify that,
22  *   1) renameat(2) returns -1 and sets errno to EBADF if olddirfd
23  *      or newdirfd is not a valid file descriptor.
24  *   2) renameat(2) returns -1 and sets errno to ENOTDIR if oldpath
25  *      is relative and olddirfd is a file descriptor referring to
26  *      a file other than a directory, or similar for newpath and
27  *      newdirfd.
28  *   3) renameat(2) returns -1 and sets errno to ELOOP if too many
29  *      symbolic links were encountered in resolving oldpath or
30  *      newpath.
31  *   4) renameat(2) returns -1 and sets errno to EROFS if the file
32  *      is on a read-only file system.
33  *   5) renameat(2) returns -1 and sets errno to EMLINK if oldpath
34  *      already has the maximum number of links to it, or it is a
35  *      directory and the directory containing newpath has the
36  *      maximum number of links.
37  */
38 
39 #define _GNU_SOURCE
40 
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <fcntl.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <sys/mount.h>
50 
51 #include "test.h"
52 #include "safe_macros.h"
53 #include "lapi/fcntl.h"
54 #include "lapi/renameat.h"
55 
56 #define MNTPOINT "mntpoint"
57 #define TESTDIR "testdir"
58 #define NEW_TESTDIR "new_testdir"
59 #define TESTDIR2 "/loopdir"
60 #define NEW_TESTDIR2 "newloopdir"
61 #define TESTDIR3 "emlinkdir"
62 #define NEW_TESTDIR3 "testemlinkdir/new_emlinkdir"
63 #define TESTFILE "testfile"
64 #define NEW_TESTFILE "new_testfile"
65 #define TESTFILE2 "testfile2"
66 #define NEW_TESTFILE2 "new_testfile2"
67 #define TESTFILE3 "testdir/testfile"
68 #define TESTFILE4 "testfile4"
69 #define TESTFILE5 "mntpoint/rofile"
70 #define NEW_TESTFILE5 "mntpoint/newrofile"
71 
72 #define DIRMODE (S_IRWXU | S_IRWXG | S_IRWXO)
73 #define FILEMODE (S_IRWXU | S_IRWXG | S_IRWXO)
74 
75 static int curfd = AT_FDCWD;
76 static int olddirfd;
77 static int newdirfd;
78 static int badfd = 100;
79 static int filefd;
80 static char absoldpath[256];
81 static char absnewpath[256];
82 static char looppathname[sizeof(TESTDIR2) * 43] = ".";
83 static int max_subdirs;
84 
85 static int mount_flag;
86 static const char *device;
87 
88 static struct test_case_t {
89 	int *oldfdptr;
90 	const char *oldpath;
91 	int *newfdptr;
92 	const char *newpath;
93 	int exp_errno;
94 } test_cases[] = {
95 	{ &curfd, TESTFILE, &curfd, NEW_TESTFILE, 0 },
96 	{ &olddirfd, TESTFILE, &newdirfd, NEW_TESTFILE, 0 },
97 	{ &olddirfd, absoldpath, &newdirfd, absnewpath, 0 },
98 	{ &badfd, TESTFILE, &badfd, NEW_TESTFILE, EBADF },
99 	{ &filefd, TESTFILE, &filefd, NEW_TESTFILE, ENOTDIR },
100 	{ &curfd, looppathname, &curfd, NEW_TESTDIR2, ELOOP },
101 	{ &curfd, TESTFILE5, &curfd, NEW_TESTFILE5, EROFS },
102 	{ &curfd, TESTDIR3, &curfd, NEW_TESTDIR3, EMLINK },
103 };
104 
105 static void setup(void);
106 static void cleanup(void);
107 static void renameat_verify(const struct test_case_t *);
108 
109 char *TCID = "renameat01";
110 int TST_TOTAL = ARRAY_SIZE(test_cases);
111 
main(int ac,char ** av)112 int main(int ac, char **av)
113 {
114 	int i, lc;
115 
116 	tst_parse_opts(ac, av, NULL, NULL);
117 
118 	setup();
119 
120 	for (lc = 0; TEST_LOOPING(lc); lc++) {
121 		tst_count = 0;
122 
123 		for (i = 0; i < TST_TOTAL; i++)
124 			renameat_verify(&test_cases[i]);
125 	}
126 
127 	cleanup();
128 	tst_exit();
129 }
130 
setup(void)131 static void setup(void)
132 {
133 	char *tmpdir;
134 	const char *fs_type;
135 	int i;
136 
137 	if ((tst_kvercmp(2, 6, 16)) < 0) {
138 		tst_brkm(TCONF, NULL,
139 			"This test can only run on kernels that are "
140 			"2.6.16 and higher");
141 	}
142 
143 	tst_require_root();
144 
145 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
146 
147 	tst_tmpdir();
148 
149 	fs_type = tst_dev_fs_type();
150 	device = tst_acquire_device(cleanup);
151 
152 	if (!device)
153 		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
154 
155 	TEST_PAUSE;
156 
157 	SAFE_TOUCH(cleanup, TESTFILE, FILEMODE, NULL);
158 
159 	SAFE_TOUCH(cleanup, TESTFILE2, FILEMODE, NULL);
160 	tmpdir = tst_get_tmpdir();
161 	sprintf(absoldpath, "%s/%s", tmpdir, TESTFILE2);
162 	sprintf(absnewpath, "%s/%s", tmpdir, NEW_TESTFILE2);
163 	free(tmpdir);
164 
165 	SAFE_MKDIR(cleanup, TESTDIR, DIRMODE);
166 	SAFE_TOUCH(cleanup, TESTFILE3, FILEMODE, NULL);
167 	SAFE_MKDIR(cleanup, NEW_TESTDIR, DIRMODE);
168 
169 	olddirfd = SAFE_OPEN(cleanup, TESTDIR, O_DIRECTORY);
170 	newdirfd = SAFE_OPEN(cleanup, NEW_TESTDIR, O_DIRECTORY);
171 
172 	filefd = SAFE_OPEN(cleanup, TESTFILE4,
173 				O_RDWR | O_CREAT, FILEMODE);
174 
175 	/*
176 	 * NOTE: the ELOOP test is written based on that the
177 	 * consecutive symlinks limit in kernel is hardwired
178 	 * to 40.
179 	 */
180 	SAFE_MKDIR(cleanup, "loopdir", DIRMODE);
181 	SAFE_SYMLINK(cleanup, "../loopdir", "loopdir/loopdir");
182 	for (i = 0; i < 43; i++)
183 		strcat(looppathname, TESTDIR2);
184 
185 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
186 	SAFE_MKDIR(cleanup, MNTPOINT, DIRMODE);
187 	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
188 	mount_flag = 1;
189 	SAFE_TOUCH(cleanup, TESTFILE5, FILEMODE, NULL);
190 	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY,
191 		   NULL);
192 
193 	SAFE_MKDIR(cleanup, TESTDIR3, DIRMODE);
194 	max_subdirs = tst_fs_fill_subdirs(cleanup, "testemlinkdir");
195 }
196 
renameat_verify(const struct test_case_t * tc)197 static void renameat_verify(const struct test_case_t *tc)
198 {
199 	if (tc->exp_errno == EMLINK && max_subdirs == 0) {
200 		tst_resm(TCONF, "EMLINK test is not appropriate");
201 		return;
202 	}
203 
204 	TEST(renameat(*(tc->oldfdptr), tc->oldpath,
205 			*(tc->newfdptr), tc->newpath));
206 
207 	if (tc->exp_errno && TEST_RETURN != -1) {
208 		tst_resm(TFAIL, "renameat() succeeded unexpectedly");
209 		return;
210 	}
211 
212 	if (tc->exp_errno == 0 && TEST_RETURN != 0) {
213 		tst_resm(TFAIL | TTERRNO, "renameat() failed unexpectedly");
214 		return;
215 	}
216 
217 	if (TEST_ERRNO == tc->exp_errno) {
218 		tst_resm(TPASS | TTERRNO,
219 		"renameat() returned the expected value");
220 	} else {
221 		tst_resm(TFAIL | TTERRNO,
222 			"renameat() got unexpected return value; expected: "
223 			"%d - %s", tc->exp_errno,
224 			strerror(tc->exp_errno));
225 	}
226 
227 	if (TEST_ERRNO == 0 && renameat(*(tc->newfdptr), tc->newpath,
228 		*(tc->oldfdptr), tc->oldpath) < 0) {
229 		tst_brkm(TBROK | TERRNO, cleanup, "renameat(%d, %s, "
230 			"%d, %s) failed.", *(tc->newfdptr), tc->newpath,
231 			*(tc->oldfdptr), tc->oldpath);
232 	}
233 }
234 
cleanup(void)235 static void cleanup(void)
236 {
237 	if (olddirfd > 0 && close(olddirfd) < 0)
238 		tst_resm(TWARN | TERRNO, "close olddirfd failed");
239 
240 	if (newdirfd > 0 && close(newdirfd) < 0)
241 		tst_resm(TWARN | TERRNO, "close newdirfd failed");
242 
243 	if (filefd > 0 && close(filefd) < 0)
244 		tst_resm(TWARN | TERRNO, "close filefd failed");
245 
246 	if (mount_flag && tst_umount(MNTPOINT) < 0)
247 		tst_resm(TWARN | TERRNO, "umount %s failed", MNTPOINT);
248 
249 	if (device)
250 		tst_release_device(device);
251 
252 	tst_rmdir();
253 }
254