xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mkdirat/mkdirat02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Zeng Linggang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
8*49cdfc7eSAndroid Build Coastguard Worker  *	check mkdirat() with various error conditions that should produce
9*49cdfc7eSAndroid Build Coastguard Worker  *	ELOOP and EROFS.
10*49cdfc7eSAndroid Build Coastguard Worker  */
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT	"mntpoint"
16*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR	"mntpoint/test_dir"
17*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
18*49cdfc7eSAndroid Build Coastguard Worker 			 S_IXGRP|S_IROTH|S_IXOTH)
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static int dir_fd;
21*49cdfc7eSAndroid Build Coastguard Worker static int cur_fd = AT_FDCWD;
22*49cdfc7eSAndroid Build Coastguard Worker static char test_dir[PATH_MAX] = ".";
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
25*49cdfc7eSAndroid Build Coastguard Worker 	int *dirfd;
26*49cdfc7eSAndroid Build Coastguard Worker 	char *pathname;
27*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
28*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
29*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_DIR, EROFS},
30*49cdfc7eSAndroid Build Coastguard Worker 	{&cur_fd, TEST_DIR, EROFS},
31*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, test_dir, ELOOP},
32*49cdfc7eSAndroid Build Coastguard Worker 	{&cur_fd, test_dir, ELOOP},
33*49cdfc7eSAndroid Build Coastguard Worker };
34*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)35*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	dir_fd = SAFE_OPEN(".", O_DIRECTORY);
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR("test_eloop", DIR_MODE);
42*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	/*
45*49cdfc7eSAndroid Build Coastguard Worker 	 * NOTE: the ELOOP test is written based on that the consecutive
46*49cdfc7eSAndroid Build Coastguard Worker 	 * symlinks limits in kernel is hardwired to 40.
47*49cdfc7eSAndroid Build Coastguard Worker 	 */
48*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 43; i++)
49*49cdfc7eSAndroid Build Coastguard Worker 		strcat(test_dir, "/test_eloop");
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
mkdirat_verify(unsigned int i)52*49cdfc7eSAndroid Build Coastguard Worker static void mkdirat_verify(unsigned int i)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *test = &tcases[i];
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	TEST(mkdirat(*test->dirfd, test->pathname, 0777));
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "mkdirat() succeeded unexpectedly (%li)",
60*49cdfc7eSAndroid Build Coastguard Worker 			TST_RET);
61*49cdfc7eSAndroid Build Coastguard Worker 		return;
62*49cdfc7eSAndroid Build Coastguard Worker 	}
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == test->exp_errno) {
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "mkdirat() failed as expected");
66*49cdfc7eSAndroid Build Coastguard Worker 		return;
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TFAIL | TTERRNO,
70*49cdfc7eSAndroid Build Coastguard Worker 		"mkdirat() failed unexpectedly; expected: %d - %s",
71*49cdfc7eSAndroid Build Coastguard Worker 		test->exp_errno, tst_strerrno(test->exp_errno));
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
75*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
76*49cdfc7eSAndroid Build Coastguard Worker 	.test = mkdirat_verify,
77*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
78*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
79*49cdfc7eSAndroid Build Coastguard Worker 	.needs_rofs = 1,
80*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNT_POINT,
81*49cdfc7eSAndroid Build Coastguard Worker };
82