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) Internstional Business Machines Corp., 2006
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Yi Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Cyril Hrubis 2014 <[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 * This test case will verify basic function of openat.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - pathname is relative, then it is interpreted relative to the directory
14*49cdfc7eSAndroid Build Coastguard Worker * referred to by the file descriptor dirfd
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * - pathname is absolute, then dirfd is ignored
17*49cdfc7eSAndroid Build Coastguard Worker *
18*49cdfc7eSAndroid Build Coastguard Worker * - ENODIR pathname is a relative pathname and dirfd is a file
19*49cdfc7eSAndroid Build Coastguard Worker * descriptor referring to a file other than a directory
20*49cdfc7eSAndroid Build Coastguard Worker *
21*49cdfc7eSAndroid Build Coastguard Worker * - EBADF dirfd is not a valid file descriptor
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * - pathname is relative and dirfd is the special value AT_FDCWD, then pathname
24*49cdfc7eSAndroid Build Coastguard Worker * is interpreted relative to the current working directory of the calling
25*49cdfc7eSAndroid Build Coastguard Worker * process
26*49cdfc7eSAndroid Build Coastguard Worker */
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "test_file"
38*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR "test_dir/"
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker static int dir_fd, fd;
41*49cdfc7eSAndroid Build Coastguard Worker static int fd_invalid = 100;
42*49cdfc7eSAndroid Build Coastguard Worker static int fd_atcwd = AT_FDCWD;
43*49cdfc7eSAndroid Build Coastguard Worker static char glob_path[256];
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
46*49cdfc7eSAndroid Build Coastguard Worker int *dir_fd;
47*49cdfc7eSAndroid Build Coastguard Worker const char *pathname;
48*49cdfc7eSAndroid Build Coastguard Worker int exp_ret;
49*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
50*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
51*49cdfc7eSAndroid Build Coastguard Worker {&dir_fd, TEST_FILE, 0, 0},
52*49cdfc7eSAndroid Build Coastguard Worker {&dir_fd, glob_path, 0, 0},
53*49cdfc7eSAndroid Build Coastguard Worker {&fd, TEST_FILE, -1, ENOTDIR},
54*49cdfc7eSAndroid Build Coastguard Worker {&fd_invalid, TEST_FILE, -1, EBADF},
55*49cdfc7eSAndroid Build Coastguard Worker {&fd_atcwd, TEST_DIR TEST_FILE, 0, 0}
56*49cdfc7eSAndroid Build Coastguard Worker };
57*49cdfc7eSAndroid Build Coastguard Worker
verify_openat(unsigned int n)58*49cdfc7eSAndroid Build Coastguard Worker static void verify_openat(unsigned int n)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker struct test_case *tc = &test_cases[n];
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_ret) {
63*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_errno == ENOTDIR) {
64*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL2(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600),
65*49cdfc7eSAndroid Build Coastguard Worker ENOTDIR, "openat with a filefd instead of dirfd");
66*49cdfc7eSAndroid Build Coastguard Worker } else {
67*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL2(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600),
68*49cdfc7eSAndroid Build Coastguard Worker EBADF, "openat with invalid fd");
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker } else {
71*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FD(openat(*tc->dir_fd, tc->pathname, O_RDWR, 0600));
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET > 0)
75*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(TST_RET);
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker
setup(void)78*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker char buf[PATH_MAX];
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETCWD(buf, PATH_MAX);
83*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TEST_DIR, 0700);
84*49cdfc7eSAndroid Build Coastguard Worker dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
85*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TEST_DIR TEST_FILE, O_CREAT | O_RDWR, 0600);
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker snprintf(glob_path, sizeof(glob_path), "%s/" TEST_DIR TEST_FILE, buf);
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)90*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
91*49cdfc7eSAndroid Build Coastguard Worker {
92*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
93*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
94*49cdfc7eSAndroid Build Coastguard Worker if (dir_fd > 0)
95*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(dir_fd);
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
99*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
100*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
101*49cdfc7eSAndroid Build Coastguard Worker .test = verify_openat,
102*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(test_cases),
103*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
104*49cdfc7eSAndroid Build Coastguard Worker };
105