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