blob: 0bd5978d677358f9387b6d779f6ad9f000960032 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
plars865695b2001-08-27 22:15:12 +00002 * Copyright (c) International Business Machines Corp., 2001
Zeng Linggang96bf1a32014-02-18 23:18:06 +08003 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
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 Linggang96bf1a32014-02-18 23:18:06 +080016 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000018 */
plars865695b2001-08-27 22:15:12 +000019/*
plars865695b2001-08-27 22:15:12 +000020 * DESCRIPTION
21 * check mkdir() with various error conditions that should produce
Zeng Linggangd931ebb2014-02-18 23:21:31 +080022 * EFAULT, ENAMETOOLONG, EEXIST, ENOENT, ENOTDIR, ELOOP and EROFS
plars865695b2001-08-27 22:15:12 +000023 */
24
25#include <errno.h>
plars74948ad2002-11-14 16:16:14 +000026#include <sys/types.h>
plars865695b2001-08-27 22:15:12 +000027#include <sys/stat.h>
plars1ad84512002-07-23 13:11:18 +000028#include <sys/mman.h>
plars74948ad2002-11-14 16:16:14 +000029#include <fcntl.h>
Zeng Linggangd931ebb2014-02-18 23:21:31 +080030#include <sys/mount.h>
31
plars865695b2001-08-27 22:15:12 +000032#include "test.h"
Zeng Linggang96bf1a32014-02-18 23:18:06 +080033#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000034
Zeng Linggang96bf1a32014-02-18 23:18:06 +080035static void setup(void);
36struct test_case_t;
37static void mkdir_verify(struct test_case_t *tc);
38static void bad_addr_setup(struct test_case_t *tc);
39static void cleanup(void);
Cyril Hrubis54df83c2014-06-19 17:19:21 +020040static int mount_flag;
plars865695b2001-08-27 22:15:12 +000041
Zeng Linggang96bf1a32014-02-18 23:18:06 +080042#define TST_EEXIST "tst_eexist"
43#define TST_ENOENT "tst_enoent/tst"
44#define TST_ENOTDIR "tst_enotdir/tst"
45#define MODE 0777
plars865695b2001-08-27 22:15:12 +000046
Zeng Linggangd931ebb2014-02-18 23:21:31 +080047#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 Hrubisfdce7d52013-04-04 18:35:48 +020052char *TCID = "mkdir03";
plars865695b2001-08-27 22:15:12 +000053
Zeng Linggang96bf1a32014-02-18 23:18:06 +080054static char long_dir[PATH_MAX+2];
Zeng Linggangd931ebb2014-02-18 23:21:31 +080055static char loop_dir[PATH_MAX] = ".";
Cyril Hrubis54df83c2014-06-19 17:19:21 +020056static const char *device;
Zeng Linggangd931ebb2014-02-18 23:21:31 +080057
Zeng Linggang96bf1a32014-02-18 23:18:06 +080058static struct test_case_t {
59 char *pathname;
60 int mode;
61 int exp_errno;
62 void (*setupfunc) (struct test_case_t *tc);
plars865695b2001-08-27 22:15:12 +000063} TC[] = {
vapierb5ed1f62006-08-24 04:16:32 +000064#if !defined(UCLINUX)
Zeng Linggang96bf1a32014-02-18 23:18:06 +080065 {NULL, MODE, EFAULT, bad_addr_setup},
vapierb5ed1f62006-08-24 04:16:32 +000066#endif
Zeng Linggang96bf1a32014-02-18 23:18:06 +080067 {long_dir, MODE, ENAMETOOLONG, NULL},
68 {TST_EEXIST, MODE, EEXIST, NULL},
69 {TST_ENOENT, MODE, ENOENT, NULL},
70 {TST_ENOTDIR, MODE, ENOTDIR, NULL},
Zeng Linggangd931ebb2014-02-18 23:21:31 +080071 {loop_dir, MODE, ELOOP, NULL},
72 {TST_EROFS, MODE, EROFS, NULL},
plars865695b2001-08-27 22:15:12 +000073};
74
Zeng Linggang96bf1a32014-02-18 23:18:06 +080075int TST_TOTAL = ARRAY_SIZE(TC);
vapier721ecd82006-08-06 04:30:48 +000076
subrata_modak56207ce2009-03-23 13:35:39 +000077int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000078{
Zeng Linggang96bf1a32014-02-18 23:18:06 +080079 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020080 const char *msg;
plars865695b2001-08-27 22:15:12 +000081
Cyril Hrubis54df83c2014-06-19 17:19:21 +020082 msg = parse_opts(ac, av, NULL, NULL);
Zeng Linggang96bf1a32014-02-18 23:18:06 +080083 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080084 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000085
plars865695b2001-08-27 22:15:12 +000086 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +000087
subrata_modak56207ce2009-03-23 13:35:39 +000088 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080089 tst_count = 0;
Zeng Linggang96bf1a32014-02-18 23:18:06 +080090 for (i = 0; i < TST_TOTAL; i++)
91 mkdir_verify(&TC[i]);
Garrett Cooper2c282152010-12-16 00:55:50 -080092 }
subrata_modakbdbaec52009-02-26 12:14:51 +000093
plars865695b2001-08-27 22:15:12 +000094 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080095 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -080096}
plars865695b2001-08-27 22:15:12 +000097
Zeng Linggang96bf1a32014-02-18 23:18:06 +080098static void setup(void)
plars865695b2001-08-27 22:15:12 +000099{
Zeng Linggangd931ebb2014-02-18 23:21:31 +0800100 int i;
Cyril Hrubis54df83c2014-06-19 17:19:21 +0200101 const char *fs_type;
Zeng Linggangd931ebb2014-02-18 23:21:31 +0800102
103 tst_require_root(NULL);
104
plars865695b2001-08-27 22:15:12 +0000105 tst_sig(NOFORK, DEF_HANDLER, cleanup);
vapierb5ed1f62006-08-24 04:16:32 +0000106
plars865695b2001-08-27 22:15:12 +0000107 TEST_PAUSE;
108
plars865695b2001-08-27 22:15:12 +0000109 tst_tmpdir();
plars1ad84512002-07-23 13:11:18 +0000110
Cyril Hrubis54df83c2014-06-19 17:19:21 +0200111 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 Linggang96bf1a32014-02-18 23:18:06 +0800117 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 Linggangd931ebb2014-02-18 23:21:31 +0800122
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 Hrubis54df83c2014-06-19 17:19:21 +0200128 tst_mkfs(cleanup, device, fs_type, NULL);
Zeng Linggangd931ebb2014-02-18 23:21:31 +0800129 SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
Cyril Hrubis54df83c2014-06-19 17:19:21 +0200130 if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) {
Zeng Linggangd931ebb2014-02-18 23:21:31 +0800131 tst_brkm(TBROK | TERRNO, cleanup,
132 "mount device:%s failed", device);
133 }
134 mount_flag = 1;
plars865695b2001-08-27 22:15:12 +0000135}
136
Zeng Linggang96bf1a32014-02-18 23:18:06 +0800137#if !defined(UCLINUX)
138static void bad_addr_setup(struct test_case_t *tc)
plars865695b2001-08-27 22:15:12 +0000139{
Zeng Linggang96bf1a32014-02-18 23:18:06 +0800140 tc->pathname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
141 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
142}
143#endif
mridge106effa2004-08-25 15:31:38 +0000144
Zeng Linggang96bf1a32014-02-18 23:18:06 +0800145static 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 Linggang96bf1a32014-02-18 23:18:06 +0800158 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
167static void cleanup(void)
168{
Cyril Hrubisc60a2302015-01-29 16:28:24 +0100169 if (mount_flag && tst_umount(MNT_POINT) < 0)
Zeng Linggangd931ebb2014-02-18 23:21:31 +0800170 tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
171
Cyril Hrubis54df83c2014-06-19 17:19:21 +0200172 if (device)
173 tst_release_device(NULL, device);
Zeng Linggangd931ebb2014-02-18 23:21:31 +0800174
Cyril Hrubis54df83c2014-06-19 17:19:21 +0200175 tst_rmdir();
Zeng Linggangd931ebb2014-02-18 23:21:31 +0800176}