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 * Check the basic functionality of the faccessat() system call.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - faccessat() passes if dir_fd is file descriptor to the directory
14*49cdfc7eSAndroid Build Coastguard Worker * where the file is located and pathname is relative path of the file.
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * - faccessat() passes if dir_fd is a bad file descriptor and pathname is
17*49cdfc7eSAndroid Build Coastguard Worker * absolute path of the file.
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * - faccessat() passes if dir_fd is AT_FDCWD and pathname is interpreted
20*49cdfc7eSAndroid Build Coastguard Worker * relative to the current working directory of the calling process.
21*49cdfc7eSAndroid Build Coastguard Worker */
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR "faccessatdir"
28*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE "faccessatfile"
29*49cdfc7eSAndroid Build Coastguard Worker #define FILEPATH "faccessatdir/faccessatfile"
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker static int dir_fd, file_fd;
32*49cdfc7eSAndroid Build Coastguard Worker static int atcwd_fd = AT_FDCWD;
33*49cdfc7eSAndroid Build Coastguard Worker static char *abs_path;
34*49cdfc7eSAndroid Build Coastguard Worker static char *test_file;
35*49cdfc7eSAndroid Build Coastguard Worker static char *file_path;
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
38*49cdfc7eSAndroid Build Coastguard Worker int *fd;
39*49cdfc7eSAndroid Build Coastguard Worker char **filename;
40*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
41*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
42*49cdfc7eSAndroid Build Coastguard Worker {&dir_fd, &test_file, 0},
43*49cdfc7eSAndroid Build Coastguard Worker {&dir_fd, &abs_path, 0},
44*49cdfc7eSAndroid Build Coastguard Worker {&atcwd_fd, &file_path, 0},
45*49cdfc7eSAndroid Build Coastguard Worker };
46*49cdfc7eSAndroid Build Coastguard Worker
verify_faccessat(unsigned int i)47*49cdfc7eSAndroid Build Coastguard Worker static void verify_faccessat(unsigned int i)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[i];
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS(faccessat(*tc->fd, *tc->filename, R_OK, 0),
52*49cdfc7eSAndroid Build Coastguard Worker "faccessat(%d, %s, R_OK, 0)",
53*49cdfc7eSAndroid Build Coastguard Worker *tc->fd, *tc->filename);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker
setup(void)56*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker char *tmpdir_path = tst_get_tmpdir();
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker abs_path = tst_aprintf("%s/%s", tmpdir_path, FILEPATH);
61*49cdfc7eSAndroid Build Coastguard Worker free(tmpdir_path);
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TESTDIR, 0700);
64*49cdfc7eSAndroid Build Coastguard Worker dir_fd = SAFE_OPEN(TESTDIR, O_DIRECTORY);
65*49cdfc7eSAndroid Build Coastguard Worker file_fd = SAFE_OPEN(FILEPATH, O_CREAT | O_RDWR, 0600);
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)68*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker if (dir_fd > -1)
71*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(dir_fd);
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker if (file_fd > -1)
74*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(file_fd);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
78*49cdfc7eSAndroid Build Coastguard Worker .test = verify_faccessat,
79*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
80*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
81*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
82*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) {
83*49cdfc7eSAndroid Build Coastguard Worker {&test_file, .str = TESTFILE},
84*49cdfc7eSAndroid Build Coastguard Worker {&file_path, .str = FILEPATH},
85*49cdfc7eSAndroid Build Coastguard Worker {},
86*49cdfc7eSAndroid Build Coastguard Worker },
87*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
88*49cdfc7eSAndroid Build Coastguard Worker };
89