1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2006
4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2003-2023
5f08c3bdfSopenharmony_ci * Author: Yi Yang <yyangcdl@cn.ibm.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * - faccessat() fails with ENOTDIR if dir_fd is file descriptor to the file
12f08c3bdfSopenharmony_ci *   and pathname is relative path of the file.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * - faccessat() fails with EBADF if dir_fd is invalid.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <stdlib.h>
18f08c3bdfSopenharmony_ci#include <stdio.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define TESTDIR         "faccessatdir"
22f08c3bdfSopenharmony_ci#define TESTFILE        "faccessatfile"
23f08c3bdfSopenharmony_ci#define FILEPATH        "faccessatdir/faccessatfile"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic int dir_fd, file_fd;
26f08c3bdfSopenharmony_cistatic int bad_fd = -1;
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic struct tcase {
29f08c3bdfSopenharmony_ci	int *fd;
30f08c3bdfSopenharmony_ci	int exp_errno;
31f08c3bdfSopenharmony_ci} tcases[] = {
32f08c3bdfSopenharmony_ci	{&file_fd, ENOTDIR},
33f08c3bdfSopenharmony_ci	{&bad_fd, EBADF},
34f08c3bdfSopenharmony_ci};
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic void verify_faccessat(unsigned int i)
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[i];
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	TST_EXP_FAIL(faccessat(*tc->fd, TESTFILE, R_OK, 0),
41f08c3bdfSopenharmony_ci		     tc->exp_errno, "faccessat(%d, TESTFILE, R_OK, 0)",
42f08c3bdfSopenharmony_ci		     *tc->fd);
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic void setup(void)
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	SAFE_MKDIR(TESTDIR, 0700);
48f08c3bdfSopenharmony_ci	dir_fd = SAFE_OPEN(TESTDIR, O_DIRECTORY);
49f08c3bdfSopenharmony_ci	file_fd = SAFE_OPEN(FILEPATH, O_CREAT | O_RDWR, 0600);
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void cleanup(void)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	if (dir_fd > -1)
55f08c3bdfSopenharmony_ci		SAFE_CLOSE(dir_fd);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (file_fd > -1)
58f08c3bdfSopenharmony_ci		SAFE_CLOSE(file_fd);
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic struct tst_test test = {
62f08c3bdfSopenharmony_ci	.test = verify_faccessat,
63f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
64f08c3bdfSopenharmony_ci	.setup = setup,
65f08c3bdfSopenharmony_ci	.cleanup = cleanup,
66f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
67f08c3bdfSopenharmony_ci};
68