Xiaoguang Wang | 986ff2f | 2014-06-17 11:38:41 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Fujitsu Ltd. |
| 3 | * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.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 |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Description: |
| 22 | * Verify that, |
| 23 | * 1. rename() fails with -1 return value and sets errno to ELOOP, if too |
| 24 | * many symbolic links were encountered in resolving oldpath or newpath. |
| 25 | * 2. rename() fails with -1 return value and sets errno to EROFS, |
| 26 | * if the file is on a read-only file system. |
| 27 | * 3. rename() fails with -1 return value and sets errno to EMLINK, |
| 28 | * if the file named by old is a directory and the link count of |
| 29 | * the parent directory of new would exceed {LINK_MAX}. |
| 30 | */ |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <errno.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <sys/mount.h> |
| 38 | |
| 39 | #include "test.h" |
Xiaoguang Wang | 986ff2f | 2014-06-17 11:38:41 +0800 | [diff] [blame] | 40 | #include "safe_macros.h" |
| 41 | |
| 42 | char *TCID = "rename11"; |
| 43 | |
| 44 | #define MNTPOINT "mntpoint" |
| 45 | #define TEST_EROFS "mntpoint/test_erofs" |
| 46 | #define TEST_NEW_EROFS "mntpoint/new_test_erofs" |
| 47 | |
| 48 | #define TEST_EMLINK "test_emlink" |
| 49 | #define TEST_NEW_EMLINK "emlink_dir/testdir" |
| 50 | |
| 51 | #define TEST_NEW_ELOOP "new_test_eloop" |
| 52 | #define ELOPFILE "/test_eloop" |
| 53 | static char elooppathname[sizeof(ELOPFILE) * 43] = "."; |
| 54 | static int max_subdirs; |
| 55 | |
| 56 | static const char *device; |
| 57 | static const char *fs_type; |
| 58 | static int mount_flag; |
| 59 | |
Xiaoguang Wang | 986ff2f | 2014-06-17 11:38:41 +0800 | [diff] [blame] | 60 | static void cleanup(void); |
| 61 | static void setup(void); |
| 62 | static void test_eloop(void); |
| 63 | static void test_erofs(void); |
| 64 | static void test_emlink(void); |
| 65 | |
| 66 | static void (*testfunc[])(void) = { test_eloop, test_erofs, test_emlink }; |
| 67 | |
| 68 | int TST_TOTAL = ARRAY_SIZE(testfunc); |
| 69 | |
| 70 | int main(int ac, char **av) |
| 71 | { |
| 72 | int lc, i; |
| 73 | const char *msg; |
| 74 | |
| 75 | msg = parse_opts(ac, av, NULL, NULL); |
| 76 | if (msg != NULL) |
| 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 78 | |
| 79 | setup(); |
| 80 | |
| 81 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 82 | tst_count = 0; |
| 83 | |
| 84 | for (i = 0; i < TST_TOTAL; i++) |
| 85 | (*testfunc[i])(); |
| 86 | } |
| 87 | |
| 88 | cleanup(); |
| 89 | tst_exit(); |
| 90 | } |
| 91 | |
| 92 | static void setup(void) |
| 93 | { |
| 94 | int i; |
| 95 | |
| 96 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 97 | |
| 98 | tst_require_root(NULL); |
| 99 | |
| 100 | tst_tmpdir(); |
| 101 | |
Xiaoguang Wang | 986ff2f | 2014-06-17 11:38:41 +0800 | [diff] [blame] | 102 | TEST_PAUSE; |
| 103 | |
| 104 | fs_type = tst_dev_fs_type(); |
| 105 | device = tst_acquire_device(cleanup); |
| 106 | |
| 107 | if (!device) |
| 108 | tst_brkm(TCONF, cleanup, "Failed to obtain block device"); |
| 109 | |
| 110 | tst_mkfs(cleanup, device, fs_type, NULL); |
| 111 | |
| 112 | SAFE_MKDIR(cleanup, MNTPOINT, 0755); |
| 113 | if (mount(device, MNTPOINT, fs_type, 0, NULL) < 0) { |
| 114 | tst_brkm(TBROK | TERRNO, cleanup, |
| 115 | "mount device:%s failed", device); |
| 116 | } |
| 117 | mount_flag = 1; |
| 118 | SAFE_TOUCH(cleanup, TEST_EROFS, 0644, NULL); |
| 119 | |
| 120 | SAFE_MKDIR(cleanup, TEST_EMLINK, 0755); |
| 121 | max_subdirs = tst_fs_fill_subdirs(cleanup, "emlink_dir"); |
| 122 | /* |
| 123 | * NOTE: the ELOOP test is written based on that the consecutive |
| 124 | * symlinks limits in kernel is hardwired to 40. |
| 125 | */ |
| 126 | SAFE_MKDIR(cleanup, "test_eloop", 0644); |
| 127 | SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop"); |
| 128 | for (i = 0; i < 43; i++) |
| 129 | strcat(elooppathname, ELOPFILE); |
| 130 | } |
| 131 | |
| 132 | static void check_and_print(int expected_errno) |
| 133 | { |
| 134 | if (TEST_RETURN == -1) { |
| 135 | if (TEST_ERRNO == expected_errno) { |
| 136 | tst_resm(TPASS | TTERRNO, "failed as expected"); |
| 137 | } else { |
| 138 | tst_resm(TFAIL | TTERRNO, |
| 139 | "failed unexpectedly; expected - %d : %s", |
| 140 | expected_errno, strerror(expected_errno)); |
| 141 | } |
| 142 | } else { |
| 143 | tst_resm(TFAIL, "rename succeeded unexpectedly"); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void test_eloop(void) |
| 148 | { |
| 149 | TEST(rename(elooppathname, TEST_NEW_ELOOP)); |
| 150 | check_and_print(ELOOP); |
| 151 | |
| 152 | if (TEST_RETURN == 0) |
| 153 | SAFE_UNLINK(cleanup, TEST_NEW_ELOOP); |
| 154 | } |
| 155 | |
| 156 | static void test_erofs(void) |
| 157 | { |
| 158 | if (mount(device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY, NULL) < 0) { |
| 159 | tst_brkm(TBROK | TERRNO, cleanup, |
| 160 | "mount device:%s failed", device); |
| 161 | } |
| 162 | |
| 163 | TEST(rename(TEST_EROFS, TEST_NEW_EROFS)); |
| 164 | check_and_print(EROFS); |
| 165 | |
| 166 | if (TEST_RETURN == 0) |
| 167 | SAFE_UNLINK(cleanup, TEST_NEW_EROFS); |
| 168 | |
| 169 | if (mount(device, MNTPOINT, fs_type, MS_REMOUNT, NULL) < 0) { |
| 170 | tst_brkm(TBROK | TERRNO, cleanup, |
| 171 | "remount device:%s failed", device); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void test_emlink(void) |
| 176 | { |
| 177 | if (max_subdirs == 0) { |
| 178 | tst_resm(TCONF, "EMLINK test is not appropriate"); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | TEST(rename(TEST_EMLINK, TEST_NEW_EMLINK)); |
| 183 | check_and_print(EMLINK); |
| 184 | |
| 185 | if (TEST_RETURN == 0) |
| 186 | SAFE_RMDIR(cleanup, TEST_NEW_EMLINK); |
| 187 | } |
| 188 | |
| 189 | static void cleanup(void) |
| 190 | { |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 191 | if (mount_flag && tst_umount(MNTPOINT) < 0) |
Xiaoguang Wang | 986ff2f | 2014-06-17 11:38:41 +0800 | [diff] [blame] | 192 | tst_resm(TWARN | TERRNO, "umount device:%s failed", device); |
| 193 | |
| 194 | if (device) |
| 195 | tst_release_device(NULL, device); |
| 196 | |
| 197 | tst_rmdir(); |
| 198 | } |