xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/openat2/openat201.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) 2020 Viresh Kumar <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Basic openat2() test.
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
8*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/openat2.h"
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "test_file"
11*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR "test_dir"
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker static struct open_how *how;
14*49cdfc7eSAndroid Build Coastguard Worker static struct open_how_pad *phow;
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker static int dir_fd = -1, fd_atcwd = AT_FDCWD;
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
19*49cdfc7eSAndroid Build Coastguard Worker 	int *dfd;
20*49cdfc7eSAndroid Build Coastguard Worker 	const char *pathname;
21*49cdfc7eSAndroid Build Coastguard Worker 	uint64_t flags;
22*49cdfc7eSAndroid Build Coastguard Worker 	uint64_t mode;
23*49cdfc7eSAndroid Build Coastguard Worker 	uint64_t resolve;
24*49cdfc7eSAndroid Build Coastguard Worker 	struct open_how **how;
25*49cdfc7eSAndroid Build Coastguard Worker 	size_t size;
26*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
27*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, 0, &how, sizeof(*how)},
28*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_FILE, O_RDONLY, S_IRUSR, 0, &how, sizeof(*how)},
29*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_FILE, O_WRONLY, S_IWUSR, 0, &how, sizeof(*how)},
30*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_XDEV, &how, sizeof(*how)},
31*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_MAGICLINKS, &how, sizeof(*how)},
32*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_SYMLINKS, &how, sizeof(*how)},
33*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_BENEATH, &how, sizeof(*how)},
34*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_IN_ROOT, &how, sizeof(*how)},
35*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, 0, &how, sizeof(*how)},
36*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_atcwd, TEST_FILE, O_RDONLY, S_IRUSR, 0, &how, sizeof(*how)},
37*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_atcwd, TEST_FILE, O_WRONLY, S_IWUSR, 0, &how, sizeof(*how)},
38*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_XDEV, &how, sizeof(*how)},
39*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_MAGICLINKS, &how, sizeof(*how)},
40*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_NO_SYMLINKS, &how, sizeof(*how)},
41*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_BENEATH, &how, sizeof(*how)},
42*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_atcwd, TEST_FILE, O_RDWR, S_IRWXU, RESOLVE_IN_ROOT, (struct open_how **)&phow, sizeof(*how) + 8},
43*49cdfc7eSAndroid Build Coastguard Worker };
44*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)45*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker 	if (dir_fd != -1)
48*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(dir_fd);
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)51*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker 	openat2_supported_by_kernel();
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	phow->pad = 0x00;
56*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TEST_DIR, 0700);
57*49cdfc7eSAndroid Build Coastguard Worker 	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)60*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
63*49cdfc7eSAndroid Build Coastguard Worker 	struct stat file_stat;
64*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
65*49cdfc7eSAndroid Build Coastguard Worker 	struct open_how *myhow = *tc->how;
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	myhow->flags = tc->flags | O_CREAT;
68*49cdfc7eSAndroid Build Coastguard Worker 	myhow->mode = tc->mode;
69*49cdfc7eSAndroid Build Coastguard Worker 	myhow->resolve = tc->resolve;
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fd = openat2(*tc->dfd, tc->pathname, myhow, tc->size));
72*49cdfc7eSAndroid Build Coastguard Worker 	if (fd < 0) {
73*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "openat2() failed (%d)", n);
74*49cdfc7eSAndroid Build Coastguard Worker 		return;
75*49cdfc7eSAndroid Build Coastguard Worker 	}
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FSTAT(fd, &file_stat);
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	if (file_stat.st_size == 0)
80*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "openat2() passed (%d)", n);
81*49cdfc7eSAndroid Build Coastguard Worker 	else
82*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "fstat() didn't work as expected (%d)", n);
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
88*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
89*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
90*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
91*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
92*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
93*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
94*49cdfc7eSAndroid Build Coastguard Worker 		{&how, .size = sizeof(*how)},
95*49cdfc7eSAndroid Build Coastguard Worker 		{&phow, .size = sizeof(*phow)},
96*49cdfc7eSAndroid Build Coastguard Worker 		{},
97*49cdfc7eSAndroid Build Coastguard Worker 	},
98*49cdfc7eSAndroid Build Coastguard Worker };
99