xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/faccessat2/faccessat201.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved.
4  * Copyright (c) Linux Test Project, 2003-2023
5  * Author: Yang Xu <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Check the basic functionality of faccessat2().
12  *
13  * Minimum Linux version required is v5.8.
14  */
15 
16 #include <stdlib.h>
17 
18 #include "tst_test.h"
19 #include "lapi/syscalls.h"
20 #include "lapi/faccessat.h"
21 
22 #define TESTDIR         "faccessat2dir"
23 #define TESTFILE        "faccessat2file"
24 #define RELPATH         "faccessat2dir/faccessat2file"
25 #define TESTSYMLINK     "faccessat2symlink"
26 
27 static int dir_fd, bad_fd = -1;
28 static int atcwd_fd = AT_FDCWD;
29 static char *testfile;
30 static char *abs_path;
31 static char *rel_path;
32 static char *sym_path;
33 
34 static struct tcase {
35 	int *fd;
36 	char **filename;
37 	int flags;
38 } tcases[] = {
39 	{&dir_fd, &testfile, 0},
40 	{&bad_fd, &abs_path, 0},
41 	{&atcwd_fd, &rel_path, 0},
42 	{&dir_fd, &testfile, AT_EACCESS},
43 	{&bad_fd, &abs_path, AT_EACCESS},
44 	{&atcwd_fd, &rel_path, AT_EACCESS},
45 	{&atcwd_fd, &sym_path, AT_SYMLINK_NOFOLLOW},
46 };
47 
verify_faccessat2(unsigned int i)48 static void verify_faccessat2(unsigned int i)
49 {
50 	struct tcase *tc = &tcases[i];
51 
52 	TST_EXP_PASS(faccessat2(*tc->fd, *tc->filename, R_OK, tc->flags),
53 		     "faccessat2(%d, %s, R_OK, %d)",
54 		     *tc->fd, *tc->filename, tc->flags);
55 }
56 
setup(void)57 static void setup(void)
58 {
59 	char *tmpdir_path = tst_get_tmpdir();
60 
61 	abs_path = tst_aprintf("%s/%s", tmpdir_path, RELPATH);
62 	free(tmpdir_path);
63 
64 	SAFE_MKDIR(TESTDIR, 0777);
65 	dir_fd = SAFE_OPEN(TESTDIR, O_DIRECTORY);
66 	SAFE_TOUCH(abs_path, 0444, NULL);
67 	SAFE_SYMLINK(abs_path, TESTSYMLINK);
68 }
69 
cleanup(void)70 static void cleanup(void)
71 {
72 	if (dir_fd > -1)
73 		SAFE_CLOSE(dir_fd);
74 }
75 
76 static struct tst_test test = {
77 	.test = verify_faccessat2,
78 	.tcnt = ARRAY_SIZE(tcases),
79 	.setup = setup,
80 	.cleanup = cleanup,
81 	.bufs = (struct tst_buffers []) {
82 		{&testfile, .str = TESTFILE},
83 		{&rel_path, .str = RELPATH},
84 		{&sym_path, .str = TESTSYMLINK},
85 		{},
86 	},
87 	.needs_tmpdir = 1,
88 };
89