plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * DESCRIPTION |
| 21 | * check mkdir() with various error conditions that should produce |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 22 | * EFAULT, ENAMETOOLONG, EEXIST, ENOENT, ENOTDIR, ELOOP and EROFS |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <errno.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 28 | #include <sys/mman.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 30 | #include <sys/mount.h> |
| 31 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | #include "test.h" |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 33 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 35 | static void setup(void); |
| 36 | struct test_case_t; |
| 37 | static void mkdir_verify(struct test_case_t *tc); |
| 38 | static void bad_addr_setup(struct test_case_t *tc); |
| 39 | static void cleanup(void); |
Cyril Hrubis | 54df83c | 2014-06-19 17:19:21 +0200 | [diff] [blame] | 40 | static int mount_flag; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 42 | #define TST_EEXIST "tst_eexist" |
| 43 | #define TST_ENOENT "tst_enoent/tst" |
| 44 | #define TST_ENOTDIR "tst_enotdir/tst" |
| 45 | #define MODE 0777 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 47 | #define MNT_POINT "mntpoint" |
| 48 | #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \ |
| 49 | S_IXGRP|S_IROTH|S_IXOTH) |
| 50 | #define TST_EROFS "mntpoint/tst_erofs" |
| 51 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 52 | char *TCID = "mkdir03"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 54 | static char long_dir[PATH_MAX+2]; |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 55 | static char loop_dir[PATH_MAX] = "."; |
Cyril Hrubis | 54df83c | 2014-06-19 17:19:21 +0200 | [diff] [blame] | 56 | static const char *device; |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 57 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 58 | static struct test_case_t { |
| 59 | char *pathname; |
| 60 | int mode; |
| 61 | int exp_errno; |
| 62 | void (*setupfunc) (struct test_case_t *tc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | } TC[] = { |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 64 | #if !defined(UCLINUX) |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 65 | {NULL, MODE, EFAULT, bad_addr_setup}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 66 | #endif |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 67 | {long_dir, MODE, ENAMETOOLONG, NULL}, |
| 68 | {TST_EEXIST, MODE, EEXIST, NULL}, |
| 69 | {TST_ENOENT, MODE, ENOENT, NULL}, |
| 70 | {TST_ENOTDIR, MODE, ENOTDIR, NULL}, |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 71 | {loop_dir, MODE, ELOOP, NULL}, |
| 72 | {TST_EROFS, MODE, EROFS, NULL}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 75 | int TST_TOTAL = ARRAY_SIZE(TC); |
vapier | 721ecd8 | 2006-08-06 04:30:48 +0000 | [diff] [blame] | 76 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | { |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 79 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 80 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
Cyril Hrubis | 54df83c | 2014-06-19 17:19:21 +0200 | [diff] [blame] | 82 | msg = parse_opts(ac, av, NULL, NULL); |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 83 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 84 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 89 | tst_count = 0; |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 90 | for (i = 0; i < TST_TOTAL; i++) |
| 91 | mkdir_verify(&TC[i]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 92 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 93 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 95 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 96 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 98 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | { |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 100 | int i; |
Cyril Hrubis | 54df83c | 2014-06-19 17:19:21 +0200 | [diff] [blame] | 101 | const char *fs_type; |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 102 | |
| 103 | tst_require_root(NULL); |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | TEST_PAUSE; |
| 108 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | tst_tmpdir(); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 110 | |
Cyril Hrubis | 54df83c | 2014-06-19 17:19:21 +0200 | [diff] [blame] | 111 | fs_type = tst_dev_fs_type(); |
| 112 | device = tst_acquire_device(cleanup); |
| 113 | |
| 114 | if (!device) |
| 115 | tst_brkm(TCONF, cleanup, "Failed to acquire device"); |
| 116 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 117 | memset(long_dir, 'a', PATH_MAX+1); |
| 118 | |
| 119 | SAFE_TOUCH(cleanup, TST_EEXIST, MODE, NULL); |
| 120 | |
| 121 | SAFE_TOUCH(cleanup, "tst_enotdir", MODE, NULL); |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 122 | |
| 123 | SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE); |
| 124 | SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop"); |
| 125 | for (i = 0; i < 43; i++) |
| 126 | strcat(loop_dir, "/test_eloop"); |
| 127 | |
Cyril Hrubis | 54df83c | 2014-06-19 17:19:21 +0200 | [diff] [blame] | 128 | tst_mkfs(cleanup, device, fs_type, NULL); |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 129 | SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE); |
Cyril Hrubis | 54df83c | 2014-06-19 17:19:21 +0200 | [diff] [blame] | 130 | if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) { |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 131 | tst_brkm(TBROK | TERRNO, cleanup, |
| 132 | "mount device:%s failed", device); |
| 133 | } |
| 134 | mount_flag = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 137 | #if !defined(UCLINUX) |
| 138 | static void bad_addr_setup(struct test_case_t *tc) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | { |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 140 | tc->pathname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, |
| 141 | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
| 142 | } |
| 143 | #endif |
mridge | 106effa | 2004-08-25 15:31:38 +0000 | [diff] [blame] | 144 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 145 | static void mkdir_verify(struct test_case_t *tc) |
| 146 | { |
| 147 | if (tc->setupfunc != NULL) |
| 148 | tc->setupfunc(tc); |
| 149 | |
| 150 | TEST(mkdir(tc->pathname, tc->mode)); |
| 151 | |
| 152 | if (TEST_RETURN != -1) { |
| 153 | tst_resm(TFAIL, "mkdir() returned %ld, expected -1, errno=%d", |
| 154 | TEST_RETURN, tc->exp_errno); |
| 155 | return; |
| 156 | } |
| 157 | |
Zeng Linggang | 96bf1a3 | 2014-02-18 23:18:06 +0800 | [diff] [blame] | 158 | if (TEST_ERRNO == tc->exp_errno) { |
| 159 | tst_resm(TPASS | TTERRNO, "mkdir() failed as expected"); |
| 160 | } else { |
| 161 | tst_resm(TFAIL | TTERRNO, |
| 162 | "mkdir() failed unexpectedly; expected: %d - %s", |
| 163 | tc->exp_errno, strerror(tc->exp_errno)); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void cleanup(void) |
| 168 | { |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 169 | if (mount_flag && tst_umount(MNT_POINT) < 0) |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 170 | tst_resm(TWARN | TERRNO, "umount device:%s failed", device); |
| 171 | |
Cyril Hrubis | 54df83c | 2014-06-19 17:19:21 +0200 | [diff] [blame] | 172 | if (device) |
| 173 | tst_release_device(NULL, device); |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 174 | |
Cyril Hrubis | 54df83c | 2014-06-19 17:19:21 +0200 | [diff] [blame] | 175 | tst_rmdir(); |
Zeng Linggang | d931ebb | 2014-02-18 23:21:31 +0800 | [diff] [blame] | 176 | } |