1 /*
2 * Copyright (c) International Business Machines Corp., 2006
3 * Author: Yi Yang <[email protected]>
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 <stdlib.h>
45 #include <errno.h>
46 #include <string.h>
47 #include <signal.h>
48 #include <sys/mount.h>
49
50 #include "test.h"
51 #include "safe_macros.h"
52 #include "lapi/fcntl.h"
53
54 #define MNTPOINT "mntpoint"
55 #define TESTDIR "testdir"
56 #define NEW_TESTDIR "new_testdir"
57 #define TESTDIR2 "/loopdir"
58 #define NEW_TESTDIR2 "newloopdir"
59 #define TESTDIR3 "emlinkdir"
60 #define NEW_TESTDIR3 "testemlinkdir/new_emlinkdir"
61 #define TESTFILE "testfile"
62 #define NEW_TESTFILE "new_testfile"
63 #define TESTFILE2 "testfile2"
64 #define NEW_TESTFILE2 "new_testfile2"
65 #define TESTFILE3 "testdir/testfile"
66 #define TESTFILE4 "testfile4"
67 #define TESTFILE5 "mntpoint/rofile"
68 #define NEW_TESTFILE5 "mntpoint/newrofile"
69
70 #define DIRMODE (S_IRWXU | S_IRWXG | S_IRWXO)
71 #define FILEMODE (S_IRWXU | S_IRWXG | S_IRWXO)
72
73 static int curfd = AT_FDCWD;
74 static int olddirfd;
75 static int newdirfd;
76 static int badfd = 100;
77 static int filefd;
78 static char absoldpath[256];
79 static char absnewpath[256];
80 static char looppathname[sizeof(TESTDIR2) * 43] = ".";
81 static int max_subdirs;
82
83 static int mount_flag;
84 static const char *device;
85
86 static struct test_case_t {
87 int *oldfdptr;
88 const char *oldpath;
89 int *newfdptr;
90 const char *newpath;
91 int exp_errno;
92 } test_cases[] = {
93 { &curfd, TESTFILE, &curfd, NEW_TESTFILE, 0 },
94 { &olddirfd, TESTFILE, &newdirfd, NEW_TESTFILE, 0 },
95 { &olddirfd, absoldpath, &newdirfd, absnewpath, 0 },
96 { &badfd, TESTFILE, &badfd, NEW_TESTFILE, EBADF },
97 { &filefd, TESTFILE, &filefd, NEW_TESTFILE, ENOTDIR },
98 { &curfd, looppathname, &curfd, NEW_TESTDIR2, ELOOP },
99 { &curfd, TESTFILE5, &curfd, NEW_TESTFILE5, EROFS },
100 { &curfd, TESTDIR3, &curfd, NEW_TESTDIR3, EMLINK },
101 };
102
103 static void setup(void);
104 static void cleanup(void);
105 static void renameat_verify(const struct test_case_t *);
106
107 char *TCID = "renameat01";
108 int TST_TOTAL = ARRAY_SIZE(test_cases);
109
main(int ac,char ** av)110 int main(int ac, char **av)
111 {
112 int i, lc;
113
114 tst_parse_opts(ac, av, NULL, NULL);
115
116 setup();
117
118 for (lc = 0; TEST_LOOPING(lc); lc++) {
119 tst_count = 0;
120
121 for (i = 0; i < TST_TOTAL; i++)
122 renameat_verify(&test_cases[i]);
123 }
124
125 cleanup();
126 tst_exit();
127 }
128
setup(void)129 static void setup(void)
130 {
131 char *tmpdir;
132 const char *fs_type;
133 int i;
134
135 tst_require_root();
136
137 tst_sig(NOFORK, DEF_HANDLER, cleanup);
138
139 tst_tmpdir();
140
141 fs_type = tst_dev_fs_type();
142 device = tst_acquire_device(cleanup);
143
144 if (!device)
145 tst_brkm(TCONF, cleanup, "Failed to obtain block device");
146
147 TEST_PAUSE;
148
149 SAFE_TOUCH(cleanup, TESTFILE, FILEMODE, NULL);
150
151 SAFE_TOUCH(cleanup, TESTFILE2, FILEMODE, NULL);
152 tmpdir = tst_get_tmpdir();
153 sprintf(absoldpath, "%s/%s", tmpdir, TESTFILE2);
154 sprintf(absnewpath, "%s/%s", tmpdir, NEW_TESTFILE2);
155 free(tmpdir);
156
157 SAFE_MKDIR(cleanup, TESTDIR, DIRMODE);
158 SAFE_TOUCH(cleanup, TESTFILE3, FILEMODE, NULL);
159 SAFE_MKDIR(cleanup, NEW_TESTDIR, DIRMODE);
160
161 olddirfd = SAFE_OPEN(cleanup, TESTDIR, O_DIRECTORY);
162 newdirfd = SAFE_OPEN(cleanup, NEW_TESTDIR, O_DIRECTORY);
163
164 filefd = SAFE_OPEN(cleanup, TESTFILE4,
165 O_RDWR | O_CREAT, FILEMODE);
166
167 /*
168 * NOTE: the ELOOP test is written based on that the
169 * consecutive symlinks limit in kernel is hardwired
170 * to 40.
171 */
172 SAFE_MKDIR(cleanup, "loopdir", DIRMODE);
173 SAFE_SYMLINK(cleanup, "../loopdir", "loopdir/loopdir");
174 for (i = 0; i < 43; i++)
175 strcat(looppathname, TESTDIR2);
176
177 tst_mkfs(cleanup, device, fs_type, NULL, NULL);
178 SAFE_MKDIR(cleanup, MNTPOINT, DIRMODE);
179 SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
180 mount_flag = 1;
181 SAFE_TOUCH(cleanup, TESTFILE5, FILEMODE, NULL);
182 SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY,
183 NULL);
184
185 SAFE_MKDIR(cleanup, TESTDIR3, DIRMODE);
186 max_subdirs = tst_fs_fill_subdirs(cleanup, "testemlinkdir");
187 }
188
renameat_verify(const struct test_case_t * tc)189 static void renameat_verify(const struct test_case_t *tc)
190 {
191 if (tc->exp_errno == EMLINK && max_subdirs == 0) {
192 tst_resm(TCONF, "EMLINK test is not appropriate");
193 return;
194 }
195
196 TEST(renameat(*(tc->oldfdptr), tc->oldpath,
197 *(tc->newfdptr), tc->newpath));
198
199 if (tc->exp_errno && TEST_RETURN != -1) {
200 tst_resm(TFAIL, "renameat() succeeded unexpectedly");
201 return;
202 }
203
204 if (tc->exp_errno == 0 && TEST_RETURN != 0) {
205 tst_resm(TFAIL | TTERRNO, "renameat() failed unexpectedly");
206 return;
207 }
208
209 if (TEST_ERRNO == tc->exp_errno) {
210 tst_resm(TPASS | TTERRNO,
211 "renameat() returned the expected value");
212 } else {
213 tst_resm(TFAIL | TTERRNO,
214 "renameat() got unexpected return value; expected: "
215 "%d - %s", tc->exp_errno,
216 strerror(tc->exp_errno));
217 }
218
219 if (TEST_ERRNO == 0 && renameat(*(tc->newfdptr), tc->newpath,
220 *(tc->oldfdptr), tc->oldpath) < 0) {
221 tst_brkm(TBROK | TERRNO, cleanup, "renameat(%d, %s, "
222 "%d, %s) failed.", *(tc->newfdptr), tc->newpath,
223 *(tc->oldfdptr), tc->oldpath);
224 }
225 }
226
cleanup(void)227 static void cleanup(void)
228 {
229 if (olddirfd > 0 && close(olddirfd) < 0)
230 tst_resm(TWARN | TERRNO, "close olddirfd failed");
231
232 if (newdirfd > 0 && close(newdirfd) < 0)
233 tst_resm(TWARN | TERRNO, "close newdirfd failed");
234
235 if (filefd > 0 && close(filefd) < 0)
236 tst_resm(TWARN | TERRNO, "close filefd failed");
237
238 if (mount_flag && tst_umount(MNTPOINT) < 0)
239 tst_resm(TWARN | TERRNO, "umount %s failed", MNTPOINT);
240
241 if (device)
242 tst_release_device(device);
243
244 tst_rmdir();
245 }
246