1 /* 2 * Copyright (c) 2014 Fujitsu Ltd. 3 * Author: Zeng Linggang <zenglg.jy (at) cn.fujitsu.com> 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2 of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 * 13 * You should have received a copy of the GNU General Public License along 14 * with this program; if not, write the Free Software Foundation, Inc., 15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 16 */ 17 /* 18 * DESCRIPTION 19 * check mkdirat() with various error conditions that should produce 20 * ELOOP and EROFS. 21 */ 22 23 #define _GNU_SOURCE 24 25 #include <errno.h> 26 #include <sys/types.h> 27 #include <sys/stat.h> 28 #include <sys/mman.h> 29 #include <fcntl.h> 30 #include <sys/mount.h> 31 #include "test.h" 32 #include "safe_macros.h" 33 #include "lapi/mkdirat.h" 34 35 static void setup(void); 36 static void cleanup(void); 37 38 #define TEST_FILE1 "mntpoint/test_file1" 39 #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \ 40 S_IXGRP|S_IROTH|S_IXOTH) 41 42 char *TCID = "mkdirat02"; 43 44 static int dir_fd; 45 static int cur_fd = AT_FDCWD; 46 static char test_file2[PATH_MAX] = "."; 47 static const char *device; 48 static int mount_flag_dir; 49 static int mount_flag_cur; 50 51 static struct test_case_t { 52 int *dirfd; 53 char *pathname; 54 int exp_errno; 55 } TC[] = { 56 {&dir_fd, TEST_FILE1, EROFS}, 57 {&cur_fd, TEST_FILE1, EROFS}, 58 {&dir_fd, test_file2, ELOOP}, 59 {&cur_fd, test_file2, ELOOP}, 60 }; 61 62 int TST_TOTAL = ARRAY_SIZE(TC); 63 static void mkdirat_verify(const struct test_case_t *); 64 65 int main(int ac, char **av) 66 { 67 int lc; 68 int i; 69 70 tst_parse_opts(ac, av, NULL, NULL); 71 72 setup(); 73 74 for (lc = 0; TEST_LOOPING(lc); lc++) { 75 tst_count = 0; 76 for (i = 0; i < TST_TOTAL; i++) 77 mkdirat_verify(&TC[i]); 78 } 79 80 cleanup(); 81 tst_exit(); 82 } 83 84 static void setup(void) 85 { 86 int i; 87 const char *fs_type; 88 89 tst_require_root(); 90 91 tst_sig(NOFORK, DEF_HANDLER, cleanup); 92 93 TEST_PAUSE; 94 95 tst_tmpdir(); 96 97 fs_type = tst_dev_fs_type(); 98 device = tst_acquire_device(cleanup); 99 100 if (!device) 101 tst_brkm(TCONF, cleanup, "Failed to acquire device"); 102 103 SAFE_MKDIR(cleanup, "test_dir", DIR_MODE); 104 dir_fd = SAFE_OPEN(cleanup, "test_dir", O_DIRECTORY); 105 106 SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE); 107 SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop"); 108 109 SAFE_MKDIR(cleanup, "test_dir/test_eloop", DIR_MODE); 110 SAFE_SYMLINK(cleanup, "../test_eloop", 111 "test_dir/test_eloop/test_eloop"); 112 /* 113 * NOTE: the ELOOP test is written based on that the consecutive 114 * symlinks limits in kernel is hardwired to 40. 115 */ 116 for (i = 0; i < 43; i++) 117 strcat(test_file2, "/test_eloop"); 118 119 tst_mkfs(cleanup, device, fs_type, NULL, NULL); 120 121 SAFE_MKDIR(cleanup, "test_dir/mntpoint", DIR_MODE); 122 SAFE_MOUNT(cleanup, device, "test_dir/mntpoint", fs_type, MS_RDONLY, 123 NULL); 124 mount_flag_dir = 1; 125 126 SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE); 127 SAFE_MOUNT(cleanup, device, "mntpoint", fs_type, MS_RDONLY, NULL); 128 mount_flag_cur = 1; 129 } 130 131 static void mkdirat_verify(const struct test_case_t *test) 132 { 133 TEST(mkdirat(*test->dirfd, test->pathname, 0777)); 134 135 if (TEST_RETURN != -1) { 136 tst_resm(TFAIL, "mkdirat() returned %ld, expected -1, errno=%d", 137 TEST_RETURN, test->exp_errno); 138 return; 139 } 140 141 if (TEST_ERRNO == test->exp_errno) { 142 tst_resm(TPASS | TTERRNO, "mkdirat() failed as expected"); 143 } else { 144 tst_resm(TFAIL | TTERRNO, 145 "mkdirat() failed unexpectedly; expected: %d - %s", 146 test->exp_errno, strerror(test->exp_errno)); 147 } 148 } 149 150 static void cleanup(void) 151 { 152 if (mount_flag_dir && tst_umount("mntpoint") < 0) 153 tst_resm(TWARN | TERRNO, "umount device:%s failed", device); 154 155 if (mount_flag_cur && tst_umount("test_dir/mntpoint") < 0) 156 tst_resm(TWARN | TERRNO, "umount device:%s failed", device); 157 158 if (device) 159 tst_release_device(device); 160 161 tst_rmdir(); 162 } 163