Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * Author: Yi Yang <yyangcdl@cn.ibm.com> |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 4 | * |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 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. |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 9 | * |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 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. |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 14 | * |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 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. |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 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. |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 37 | */ |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 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 <error.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <errno.h> |
| 48 | #include <string.h> |
| 49 | #include <signal.h> |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 50 | #include <sys/mount.h> |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 51 | |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 52 | #include "test.h" |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 53 | #include "safe_macros.h" |
| 54 | #include "lapi/fcntl.h" |
| 55 | #include "lapi/renameat.h" |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 56 | |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 57 | #define MNTPOINT "mntpoint" |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 58 | #define TESTDIR "testdir" |
| 59 | #define NEW_TESTDIR "new_testdir" |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 60 | #define TESTDIR2 "/loopdir" |
| 61 | #define NEW_TESTDIR2 "newloopdir" |
| 62 | #define TESTDIR3 "emlinkdir" |
| 63 | #define NEW_TESTDIR3 "testemlinkdir/new_emlinkdir" |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 64 | #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 Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 70 | #define TESTFILE5 "mntpoint/rofile" |
| 71 | #define NEW_TESTFILE5 "mntpoint/newrofile" |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 72 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 73 | #define DIRMODE (S_IRWXU | S_IRWXG | S_IRWXO) |
| 74 | #define FILEMODE (S_IRWXU | S_IRWXG | S_IRWXO) |
| 75 | |
| 76 | static int curfd = AT_FDCWD; |
| 77 | static int olddirfd; |
| 78 | static int newdirfd; |
| 79 | static int badfd = 100; |
| 80 | static int filefd; |
| 81 | static char absoldpath[256]; |
| 82 | static char absnewpath[256]; |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 83 | static char looppathname[sizeof(TESTDIR2) * 43] = "."; |
| 84 | static int max_subdirs; |
| 85 | |
| 86 | static int mount_flag; |
| 87 | static const char *device; |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 88 | |
| 89 | static 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 Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 101 | { &curfd, looppathname, &curfd, NEW_TESTDIR2, ELOOP }, |
| 102 | { &curfd, TESTFILE5, &curfd, NEW_TESTFILE5, EROFS }, |
| 103 | { &curfd, TESTDIR3, &curfd, NEW_TESTDIR3, EMLINK }, |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | static void setup(void); |
| 107 | static void cleanup(void); |
| 108 | static void renameat_verify(const struct test_case_t *); |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 109 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 110 | char *TCID = "renameat01"; |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 111 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 112 | |
| 113 | int main(int ac, char **av) |
| 114 | { |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 115 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 116 | const char *msg; |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 117 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 118 | msg = parse_opts(ac, av, NULL, NULL); |
| 119 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 120 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 121 | |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 122 | setup(); |
| 123 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 124 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 125 | tst_count = 0; |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 126 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 127 | for (i = 0; i < TST_TOTAL; i++) |
| 128 | renameat_verify(&test_cases[i]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 129 | } |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 130 | |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 131 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 132 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 133 | } |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 134 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 135 | static void setup(void) |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 136 | { |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 137 | char *tmpdir; |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 138 | const char *fs_type; |
| 139 | int i; |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 140 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 141 | 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"); |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 147 | tst_require_root(NULL); |
| 148 | |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 149 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 150 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 151 | tst_tmpdir(); |
| 152 | |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 153 | fs_type = tst_dev_fs_type(); |
| 154 | device = tst_acquire_device(cleanup); |
| 155 | |
Cyril Hrubis | b244c1c | 2014-06-24 18:11:17 +0200 | [diff] [blame] | 156 | if (!device) |
| 157 | tst_brkm(TCONF, cleanup, "Failed to obtain block device"); |
| 158 | |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 159 | TEST_PAUSE; |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 160 | |
| 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 Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 178 | |
| 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 Hrubis | b244c1c | 2014-06-24 18:11:17 +0200 | [diff] [blame] | 189 | tst_mkfs(cleanup, device, fs_type, NULL); |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 190 | 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 205 | } |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 206 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 207 | static void renameat_verify(const struct test_case_t *tc) |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 208 | { |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 209 | if (tc->exp_errno == EMLINK && max_subdirs == 0) { |
| 210 | tst_resm(TCONF, "EMLINK test is not appropriate"); |
| 211 | return; |
| 212 | } |
| 213 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 214 | TEST(renameat(*(tc->oldfdptr), tc->oldpath, |
| 215 | *(tc->newfdptr), tc->newpath)); |
vapier | b0493c8 | 2006-09-10 09:57:28 +0000 | [diff] [blame] | 216 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 217 | 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 | |
| 245 | static void cleanup(void) |
| 246 | { |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 247 | if (olddirfd > 0 && close(olddirfd) < 0) |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 248 | tst_resm(TWARN | TERRNO, "close olddirfd failed"); |
| 249 | |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 250 | if (newdirfd > 0 && close(newdirfd) < 0) |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 251 | tst_resm(TWARN | TERRNO, "close newdirfd failed"); |
| 252 | |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 253 | if (filefd > 0 && close(filefd) < 0) |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 254 | tst_resm(TWARN | TERRNO, "close filefd failed"); |
| 255 | |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 256 | if (mount_flag && tst_umount(MNTPOINT) < 0) |
Xing Gu | 241a313 | 2014-06-17 15:26:31 +0800 | [diff] [blame] | 257 | tst_resm(TWARN | TERRNO, "umount %s failed", MNTPOINT); |
| 258 | |
| 259 | if (device) |
| 260 | tst_release_device(NULL, device); |
| 261 | |
Xing Gu | a3bfea2 | 2014-06-17 15:26:30 +0800 | [diff] [blame] | 262 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 263 | } |