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