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