blob: ea84e8aa3c37d8b0ada1ccf5cff5d46aefbe454d [file] [log] [blame]
Xing Gua3bfea22014-06-17 15:26:30 +08001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 * Author: Yi Yang <yyangcdl@cn.ibm.com>
vapierb0493c82006-09-10 09:57:28 +00004 *
Xing Gua3bfea22014-06-17 15:26:30 +08005 * 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.
vapierb0493c82006-09-10 09:57:28 +00009 *
Xing Gua3bfea22014-06-17 15:26:30 +080010 * 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.
vapierb0493c82006-09-10 09:57:28 +000014 *
Xing Gua3bfea22014-06-17 15:26:30 +080015 * 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.
Xing Gu241a3132014-06-17 15:26:31 +080028 * 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.
Xing Gua3bfea22014-06-17 15:26:30 +080037 */
vapierb0493c82006-09-10 09:57:28 +000038
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 <error.h>
46#include <stdlib.h>
47#include <errno.h>
48#include <string.h>
49#include <signal.h>
Xing Gu241a3132014-06-17 15:26:31 +080050#include <sys/mount.h>
Xing Gua3bfea22014-06-17 15:26:30 +080051
vapierb0493c82006-09-10 09:57:28 +000052#include "test.h"
Xing Gua3bfea22014-06-17 15:26:30 +080053#include "safe_macros.h"
54#include "lapi/fcntl.h"
55#include "lapi/renameat.h"
vapierb0493c82006-09-10 09:57:28 +000056
Xing Gu241a3132014-06-17 15:26:31 +080057#define MNTPOINT "mntpoint"
Xing Gua3bfea22014-06-17 15:26:30 +080058#define TESTDIR "testdir"
59#define NEW_TESTDIR "new_testdir"
Xing Gu241a3132014-06-17 15:26:31 +080060#define TESTDIR2 "/loopdir"
61#define NEW_TESTDIR2 "newloopdir"
62#define TESTDIR3 "emlinkdir"
63#define NEW_TESTDIR3 "testemlinkdir/new_emlinkdir"
Xing Gua3bfea22014-06-17 15:26:30 +080064#define TESTFILE "testfile"
65#define NEW_TESTFILE "new_testfile"
66#define TESTFILE2 "testfile2"
67#define NEW_TESTFILE2 "new_testfile2"
68#define TESTFILE3 "testdir/testfile"
69#define TESTFILE4 "testfile4"
Xing Gu241a3132014-06-17 15:26:31 +080070#define TESTFILE5 "mntpoint/rofile"
71#define NEW_TESTFILE5 "mntpoint/newrofile"
vapierb0493c82006-09-10 09:57:28 +000072
Xing Gua3bfea22014-06-17 15:26:30 +080073#define DIRMODE (S_IRWXU | S_IRWXG | S_IRWXO)
74#define FILEMODE (S_IRWXU | S_IRWXG | S_IRWXO)
75
76static int curfd = AT_FDCWD;
77static int olddirfd;
78static int newdirfd;
79static int badfd = 100;
80static int filefd;
81static char absoldpath[256];
82static char absnewpath[256];
Xing Gu241a3132014-06-17 15:26:31 +080083static char looppathname[sizeof(TESTDIR2) * 43] = ".";
84static int max_subdirs;
85
86static int mount_flag;
87static const char *device;
Xing Gua3bfea22014-06-17 15:26:30 +080088
89static struct test_case_t {
90 int *oldfdptr;
91 const char *oldpath;
92 int *newfdptr;
93 const char *newpath;
94 int exp_errno;
95} test_cases[] = {
96 { &curfd, TESTFILE, &curfd, NEW_TESTFILE, 0 },
97 { &olddirfd, TESTFILE, &newdirfd, NEW_TESTFILE, 0 },
98 { &olddirfd, absoldpath, &newdirfd, absnewpath, 0 },
99 { &badfd, TESTFILE, &badfd, NEW_TESTFILE, EBADF },
100 { &filefd, TESTFILE, &filefd, NEW_TESTFILE, ENOTDIR },
Xing Gu241a3132014-06-17 15:26:31 +0800101 { &curfd, looppathname, &curfd, NEW_TESTDIR2, ELOOP },
102 { &curfd, TESTFILE5, &curfd, NEW_TESTFILE5, EROFS },
103 { &curfd, TESTDIR3, &curfd, NEW_TESTDIR3, EMLINK },
Xing Gua3bfea22014-06-17 15:26:30 +0800104};
105
106static void setup(void);
107static void cleanup(void);
108static void renameat_verify(const struct test_case_t *);
vapierb0493c82006-09-10 09:57:28 +0000109
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200110char *TCID = "renameat01";
Xing Gua3bfea22014-06-17 15:26:30 +0800111int TST_TOTAL = ARRAY_SIZE(test_cases);
vapierb0493c82006-09-10 09:57:28 +0000112
113int main(int ac, char **av)
114{
Xing Gua3bfea22014-06-17 15:26:30 +0800115 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200116 const char *msg;
vapierb0493c82006-09-10 09:57:28 +0000117
Xing Gua3bfea22014-06-17 15:26:30 +0800118 msg = parse_opts(ac, av, NULL, NULL);
119 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800120 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierb0493c82006-09-10 09:57:28 +0000121
vapierb0493c82006-09-10 09:57:28 +0000122 setup();
123
Xing Gua3bfea22014-06-17 15:26:30 +0800124 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800125 tst_count = 0;
vapierb0493c82006-09-10 09:57:28 +0000126
Xing Gua3bfea22014-06-17 15:26:30 +0800127 for (i = 0; i < TST_TOTAL; i++)
128 renameat_verify(&test_cases[i]);
Garrett Cooper2c282152010-12-16 00:55:50 -0800129 }
vapierb0493c82006-09-10 09:57:28 +0000130
vapierb0493c82006-09-10 09:57:28 +0000131 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800132 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800133}
vapierb0493c82006-09-10 09:57:28 +0000134
Xing Gua3bfea22014-06-17 15:26:30 +0800135static void setup(void)
vapierb0493c82006-09-10 09:57:28 +0000136{
Xing Gua3bfea22014-06-17 15:26:30 +0800137 char *tmpdir;
Xing Gu241a3132014-06-17 15:26:31 +0800138 const char *fs_type;
139 int i;
vapierb0493c82006-09-10 09:57:28 +0000140
Xing Gua3bfea22014-06-17 15:26:30 +0800141 if ((tst_kvercmp(2, 6, 16)) < 0) {
142 tst_brkm(TCONF, NULL,
143 "This test can only run on kernels that are "
144 "2.6.16 and higher");
vapierb0493c82006-09-10 09:57:28 +0000145 }
146
Xing Gu241a3132014-06-17 15:26:31 +0800147 tst_require_root(NULL);
148
vapierb0493c82006-09-10 09:57:28 +0000149 tst_sig(NOFORK, DEF_HANDLER, cleanup);
150
Xing Gua3bfea22014-06-17 15:26:30 +0800151 tst_tmpdir();
152
Xing Gu241a3132014-06-17 15:26:31 +0800153 fs_type = tst_dev_fs_type();
154 device = tst_acquire_device(cleanup);
155
Cyril Hrubisb244c1c2014-06-24 18:11:17 +0200156 if (!device)
157 tst_brkm(TCONF, cleanup, "Failed to obtain block device");
158
vapierb0493c82006-09-10 09:57:28 +0000159 TEST_PAUSE;
Xing Gua3bfea22014-06-17 15:26:30 +0800160
161 SAFE_TOUCH(cleanup, TESTFILE, FILEMODE, NULL);
162
163 SAFE_TOUCH(cleanup, TESTFILE2, FILEMODE, NULL);
164 tmpdir = tst_get_tmpdir();
165 sprintf(absoldpath, "%s/%s", tmpdir, TESTFILE2);
166 sprintf(absnewpath, "%s/%s", tmpdir, NEW_TESTFILE2);
167 free(tmpdir);
168
169 SAFE_MKDIR(cleanup, TESTDIR, DIRMODE);
170 SAFE_TOUCH(cleanup, TESTFILE3, FILEMODE, NULL);
171 SAFE_MKDIR(cleanup, NEW_TESTDIR, DIRMODE);
172
173 olddirfd = SAFE_OPEN(cleanup, TESTDIR, O_DIRECTORY);
174 newdirfd = SAFE_OPEN(cleanup, NEW_TESTDIR, O_DIRECTORY);
175
176 filefd = SAFE_OPEN(cleanup, TESTFILE4,
177 O_RDWR | O_CREAT, FILEMODE);
Xing Gu241a3132014-06-17 15:26:31 +0800178
179 /*
180 * NOTE: the ELOOP test is written based on that the
181 * consecutive symlinks limit in kernel is hardwired
182 * to 40.
183 */
184 SAFE_MKDIR(cleanup, "loopdir", DIRMODE);
185 SAFE_SYMLINK(cleanup, "../loopdir", "loopdir/loopdir");
186 for (i = 0; i < 43; i++)
187 strcat(looppathname, TESTDIR2);
188
Cyril Hrubisb244c1c2014-06-24 18:11:17 +0200189 tst_mkfs(cleanup, device, fs_type, NULL);
Xing Gu241a3132014-06-17 15:26:31 +0800190 SAFE_MKDIR(cleanup, MNTPOINT, DIRMODE);
191 if (mount(device, MNTPOINT, fs_type, 0, NULL) < 0) {
192 tst_brkm(TBROK | TERRNO, cleanup,
193 "mount device:%s failed", device);
194 }
195 mount_flag = 1;
196 SAFE_TOUCH(cleanup, TESTFILE5, FILEMODE, NULL);
197 if (mount(device, MNTPOINT, fs_type,
198 MS_REMOUNT | MS_RDONLY, NULL) < 0) {
199 tst_brkm(TBROK | TERRNO, cleanup,
200 "mount device:%s failed", device);
201 }
202
203 SAFE_MKDIR(cleanup, TESTDIR3, DIRMODE);
204 max_subdirs = tst_fs_fill_subdirs(cleanup, "testemlinkdir");
Garrett Cooper2c282152010-12-16 00:55:50 -0800205}
vapierb0493c82006-09-10 09:57:28 +0000206
Xing Gua3bfea22014-06-17 15:26:30 +0800207static void renameat_verify(const struct test_case_t *tc)
vapierb0493c82006-09-10 09:57:28 +0000208{
Xing Gu241a3132014-06-17 15:26:31 +0800209 if (tc->exp_errno == EMLINK && max_subdirs == 0) {
210 tst_resm(TCONF, "EMLINK test is not appropriate");
211 return;
212 }
213
Xing Gua3bfea22014-06-17 15:26:30 +0800214 TEST(renameat(*(tc->oldfdptr), tc->oldpath,
215 *(tc->newfdptr), tc->newpath));
vapierb0493c82006-09-10 09:57:28 +0000216
Xing Gua3bfea22014-06-17 15:26:30 +0800217 if (tc->exp_errno && TEST_RETURN != -1) {
218 tst_resm(TFAIL, "renameat() succeeded unexpectedly");
219 return;
220 }
221
222 if (tc->exp_errno == 0 && TEST_RETURN != 0) {
223 tst_resm(TFAIL | TTERRNO, "renameat() failed unexpectedly");
224 return;
225 }
226
227 if (TEST_ERRNO == tc->exp_errno) {
228 tst_resm(TPASS | TTERRNO,
229 "renameat() returned the expected value");
230 } else {
231 tst_resm(TFAIL | TTERRNO,
232 "renameat() got unexpected return value; expected: "
233 "%d - %s", tc->exp_errno,
234 strerror(tc->exp_errno));
235 }
236
237 if (TEST_ERRNO == 0 && renameat(*(tc->newfdptr), tc->newpath,
238 *(tc->oldfdptr), tc->oldpath) < 0) {
239 tst_brkm(TBROK | TERRNO, cleanup, "renameat(%d, %s, "
240 "%d, %s) failed.", *(tc->newfdptr), tc->newpath,
241 *(tc->oldfdptr), tc->oldpath);
242 }
243}
244
245static void cleanup(void)
246{
Xing Gu241a3132014-06-17 15:26:31 +0800247 if (olddirfd > 0 && close(olddirfd) < 0)
Xing Gua3bfea22014-06-17 15:26:30 +0800248 tst_resm(TWARN | TERRNO, "close olddirfd failed");
249
Xing Gu241a3132014-06-17 15:26:31 +0800250 if (newdirfd > 0 && close(newdirfd) < 0)
Xing Gua3bfea22014-06-17 15:26:30 +0800251 tst_resm(TWARN | TERRNO, "close newdirfd failed");
252
Xing Gu241a3132014-06-17 15:26:31 +0800253 if (filefd > 0 && close(filefd) < 0)
Xing Gua3bfea22014-06-17 15:26:30 +0800254 tst_resm(TWARN | TERRNO, "close filefd failed");
255
Cyril Hrubisc60a2302015-01-29 16:28:24 +0100256 if (mount_flag && tst_umount(MNTPOINT) < 0)
Xing Gu241a3132014-06-17 15:26:31 +0800257 tst_resm(TWARN | TERRNO, "umount %s failed", MNTPOINT);
258
259 if (device)
260 tst_release_device(NULL, device);
261
Xing Gua3bfea22014-06-17 15:26:30 +0800262 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700263}