aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/mkdirat/mkdirat02.c
blob: 2bd8fe9c0353dfbc651614c3993fd4468532cde2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2014 Fujitsu Ltd.
 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
 */
/*
 * DESCRIPTION
 *	check mkdirat() with various error conditions that should produce
 *	ELOOP and EROFS.
 */

#define _GNU_SOURCE
#include "tst_test.h"

#define MNT_POINT	"mntpoint"
#define TEST_DIR	"mntpoint/test_dir"
#define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
			 S_IXGRP|S_IROTH|S_IXOTH)

static int dir_fd;
static int cur_fd = AT_FDCWD;
static char test_dir[PATH_MAX] = ".";

static struct tcase {
	int *dirfd;
	char *pathname;
	int exp_errno;
} tcases[] = {
	{&dir_fd, TEST_DIR, EROFS},
	{&cur_fd, TEST_DIR, EROFS},
	{&dir_fd, test_dir, ELOOP},
	{&cur_fd, test_dir, ELOOP},
};

static void setup(void)
{
	unsigned int i;

	dir_fd = SAFE_OPEN(".", O_DIRECTORY);

	SAFE_MKDIR("test_eloop", DIR_MODE);
	SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");

	/*
	 * NOTE: the ELOOP test is written based on that the consecutive
	 * symlinks limits in kernel is hardwired to 40.
	 */
	for (i = 0; i < 43; i++)
		strcat(test_dir, "/test_eloop");
}

static void mkdirat_verify(unsigned int i)
{
	struct tcase *test = &tcases[i];

	TEST(mkdirat(*test->dirfd, test->pathname, 0777));

	if (TST_RET != -1) {
		tst_res(TFAIL, "mkdirat() succeeded unexpectedly (%li)",
			TST_RET);
		return;
	}

	if (TST_ERR == test->exp_errno) {
		tst_res(TPASS | TTERRNO, "mkdirat() failed as expected");
		return;
	}

	tst_res(TFAIL | TTERRNO,
		"mkdirat() failed unexpectedly; expected: %d - %s",
		test->exp_errno, tst_strerrno(test->exp_errno));
}

static struct tst_test test = {
	.setup = setup,
	.test = mkdirat_verify,
	.tcnt = ARRAY_SIZE(tcases),
	.needs_root = 1,
	.needs_rofs = 1,
	.mntpoint = MNT_POINT,
};