Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Fujitsu Ltd. |
| 3 | * Author: Zeng Linggang <zenglg.jy@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" |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 32 | #include "safe_macros.h" |
Cyril Hrubis | a08a275 | 2014-03-05 14:03:37 +0100 | [diff] [blame] | 33 | #include "lapi/mkdirat.h" |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 34 | |
| 35 | static void setup(void); |
| 36 | static void cleanup(void); |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 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] = "."; |
Cyril Hrubis | a1393e2 | 2014-06-19 17:23:11 +0200 | [diff] [blame] | 47 | static const char *device; |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 48 | static int mount_flag_dir; |
| 49 | static int mount_flag_cur; |
| 50 | |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 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; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 69 | const char *msg; |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 70 | |
Cyril Hrubis | a1393e2 | 2014-06-19 17:23:11 +0200 | [diff] [blame] | 71 | msg = parse_opts(ac, av, NULL, NULL); |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 72 | if (msg != NULL) |
| 73 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 74 | |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 75 | setup(); |
| 76 | |
| 77 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 78 | tst_count = 0; |
| 79 | for (i = 0; i < TST_TOTAL; i++) |
| 80 | mkdirat_verify(&TC[i]); |
| 81 | } |
| 82 | |
| 83 | cleanup(); |
| 84 | tst_exit(); |
| 85 | } |
| 86 | |
| 87 | static void setup(void) |
| 88 | { |
| 89 | int i; |
Cyril Hrubis | a1393e2 | 2014-06-19 17:23:11 +0200 | [diff] [blame] | 90 | const char *fs_type; |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 91 | |
| 92 | tst_require_root(NULL); |
| 93 | |
| 94 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 95 | |
| 96 | TEST_PAUSE; |
| 97 | |
| 98 | tst_tmpdir(); |
| 99 | |
Cyril Hrubis | a1393e2 | 2014-06-19 17:23:11 +0200 | [diff] [blame] | 100 | fs_type = tst_dev_fs_type(); |
| 101 | device = tst_acquire_device(cleanup); |
| 102 | |
| 103 | if (!device) |
| 104 | tst_brkm(TCONF, cleanup, "Failed to acquire device"); |
| 105 | |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 106 | SAFE_MKDIR(cleanup, "test_dir", DIR_MODE); |
| 107 | dir_fd = SAFE_OPEN(cleanup, "test_dir", O_DIRECTORY); |
| 108 | |
| 109 | SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE); |
| 110 | SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop"); |
| 111 | |
| 112 | SAFE_MKDIR(cleanup, "test_dir/test_eloop", DIR_MODE); |
| 113 | SAFE_SYMLINK(cleanup, "../test_eloop", |
| 114 | "test_dir/test_eloop/test_eloop"); |
Cyril Hrubis | a08a275 | 2014-03-05 14:03:37 +0100 | [diff] [blame] | 115 | /* |
| 116 | * NOTE: the ELOOP test is written based on that the consecutive |
| 117 | * symlinks limits in kernel is hardwired to 40. |
| 118 | */ |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 119 | for (i = 0; i < 43; i++) |
| 120 | strcat(test_file2, "/test_eloop"); |
| 121 | |
Cyril Hrubis | a1393e2 | 2014-06-19 17:23:11 +0200 | [diff] [blame] | 122 | tst_mkfs(cleanup, device, fs_type, NULL); |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 123 | |
| 124 | SAFE_MKDIR(cleanup, "test_dir/mntpoint", DIR_MODE); |
Cyril Hrubis | a1393e2 | 2014-06-19 17:23:11 +0200 | [diff] [blame] | 125 | if (mount(device, "test_dir/mntpoint", fs_type, MS_RDONLY, NULL) < 0) { |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 126 | tst_brkm(TBROK | TERRNO, cleanup, |
| 127 | "mount device:%s failed", device); |
| 128 | } |
| 129 | mount_flag_dir = 1; |
| 130 | |
| 131 | SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE); |
Cyril Hrubis | a1393e2 | 2014-06-19 17:23:11 +0200 | [diff] [blame] | 132 | if (mount(device, "mntpoint", fs_type, MS_RDONLY, NULL) < 0) { |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 133 | tst_brkm(TBROK | TERRNO, cleanup, |
| 134 | "mount device:%s failed", device); |
| 135 | } |
| 136 | mount_flag_cur = 1; |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void mkdirat_verify(const struct test_case_t *test) |
| 140 | { |
Cyril Hrubis | a08a275 | 2014-03-05 14:03:37 +0100 | [diff] [blame] | 141 | TEST(mkdirat(*test->dirfd, test->pathname, 0777)); |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 142 | |
| 143 | if (TEST_RETURN != -1) { |
| 144 | tst_resm(TFAIL, "mkdirat() returned %ld, expected -1, errno=%d", |
| 145 | TEST_RETURN, test->exp_errno); |
| 146 | return; |
| 147 | } |
| 148 | |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 149 | if (TEST_ERRNO == test->exp_errno) { |
| 150 | tst_resm(TPASS | TTERRNO, "mkdirat() failed as expected"); |
| 151 | } else { |
| 152 | tst_resm(TFAIL | TTERRNO, |
| 153 | "mkdirat() failed unexpectedly; expected: %d - %s", |
| 154 | test->exp_errno, strerror(test->exp_errno)); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static void cleanup(void) |
| 159 | { |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 160 | if (mount_flag_dir && tst_umount("mntpoint") < 0) |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 161 | tst_resm(TWARN | TERRNO, "umount device:%s failed", device); |
| 162 | |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 163 | if (mount_flag_cur && tst_umount("test_dir/mntpoint") < 0) |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 164 | tst_resm(TWARN | TERRNO, "umount device:%s failed", device); |
| 165 | |
Cyril Hrubis | a1393e2 | 2014-06-19 17:23:11 +0200 | [diff] [blame] | 166 | if (device) |
| 167 | tst_release_device(NULL, device); |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 168 | |
Cyril Hrubis | a1393e2 | 2014-06-19 17:23:11 +0200 | [diff] [blame] | 169 | tst_rmdir(); |
Zeng Linggang | be7bf59 | 2014-02-18 22:56:32 +0800 | [diff] [blame] | 170 | } |