blob: 0e354cc4e8a29eafceaced7e025c2ec1e55389ff [file] [log] [blame]
Zeng Linggangbe7bf592014-02-18 22:56:32 +08001/*
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 Linggangbe7bf592014-02-18 22:56:32 +080032#include "safe_macros.h"
Cyril Hrubisa08a2752014-03-05 14:03:37 +010033#include "lapi/mkdirat.h"
Zeng Linggangbe7bf592014-02-18 22:56:32 +080034
35static void setup(void);
36static void cleanup(void);
Zeng Linggangbe7bf592014-02-18 22:56:32 +080037
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
42char *TCID = "mkdirat02";
43
44static int dir_fd;
45static int cur_fd = AT_FDCWD;
46static char test_file2[PATH_MAX] = ".";
Cyril Hrubisa1393e22014-06-19 17:23:11 +020047static const char *device;
Zeng Linggangbe7bf592014-02-18 22:56:32 +080048static int mount_flag_dir;
49static int mount_flag_cur;
50
Zeng Linggangbe7bf592014-02-18 22:56:32 +080051static 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
62int TST_TOTAL = ARRAY_SIZE(TC);
63static void mkdirat_verify(const struct test_case_t *);
64
65int main(int ac, char **av)
66{
67 int lc;
68 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020069 const char *msg;
Zeng Linggangbe7bf592014-02-18 22:56:32 +080070
Cyril Hrubisa1393e22014-06-19 17:23:11 +020071 msg = parse_opts(ac, av, NULL, NULL);
Zeng Linggangbe7bf592014-02-18 22:56:32 +080072 if (msg != NULL)
73 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
74
Zeng Linggangbe7bf592014-02-18 22:56:32 +080075 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
87static void setup(void)
88{
89 int i;
Cyril Hrubisa1393e22014-06-19 17:23:11 +020090 const char *fs_type;
Zeng Linggangbe7bf592014-02-18 22:56:32 +080091
92 tst_require_root(NULL);
93
94 tst_sig(NOFORK, DEF_HANDLER, cleanup);
95
96 TEST_PAUSE;
97
98 tst_tmpdir();
99
Cyril Hrubisa1393e22014-06-19 17:23:11 +0200100 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 Linggangbe7bf592014-02-18 22:56:32 +0800106 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 Hrubisa08a2752014-03-05 14:03:37 +0100115 /*
116 * NOTE: the ELOOP test is written based on that the consecutive
117 * symlinks limits in kernel is hardwired to 40.
118 */
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800119 for (i = 0; i < 43; i++)
120 strcat(test_file2, "/test_eloop");
121
Cyril Hrubisa1393e22014-06-19 17:23:11 +0200122 tst_mkfs(cleanup, device, fs_type, NULL);
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800123
124 SAFE_MKDIR(cleanup, "test_dir/mntpoint", DIR_MODE);
Cyril Hrubisa1393e22014-06-19 17:23:11 +0200125 if (mount(device, "test_dir/mntpoint", fs_type, MS_RDONLY, NULL) < 0) {
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800126 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 Hrubisa1393e22014-06-19 17:23:11 +0200132 if (mount(device, "mntpoint", fs_type, MS_RDONLY, NULL) < 0) {
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800133 tst_brkm(TBROK | TERRNO, cleanup,
134 "mount device:%s failed", device);
135 }
136 mount_flag_cur = 1;
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800137}
138
139static void mkdirat_verify(const struct test_case_t *test)
140{
Cyril Hrubisa08a2752014-03-05 14:03:37 +0100141 TEST(mkdirat(*test->dirfd, test->pathname, 0777));
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800142
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 Linggangbe7bf592014-02-18 22:56:32 +0800149 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
158static void cleanup(void)
159{
Cyril Hrubisc60a2302015-01-29 16:28:24 +0100160 if (mount_flag_dir && tst_umount("mntpoint") < 0)
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800161 tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
162
Cyril Hrubisc60a2302015-01-29 16:28:24 +0100163 if (mount_flag_cur && tst_umount("test_dir/mntpoint") < 0)
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800164 tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
165
Cyril Hrubisa1393e22014-06-19 17:23:11 +0200166 if (device)
167 tst_release_device(NULL, device);
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800168
Cyril Hrubisa1393e22014-06-19 17:23:11 +0200169 tst_rmdir();
Zeng Linggangbe7bf592014-02-18 22:56:32 +0800170}