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