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