1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Zeng Linggang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Verify that readdir will fail with:
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * - ENOENT when passed a fd to a deleted directory
13*49cdfc7eSAndroid Build Coastguard Worker * - ENOTDIR when passed fd that does not point to a directory
14*49cdfc7eSAndroid Build Coastguard Worker * - EBADFD when passed an invalid fd
15*49cdfc7eSAndroid Build Coastguard Worker * - EFAULT when passed invalid buffer pointer
16*49cdfc7eSAndroid Build Coastguard Worker */
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/readdir.h"
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR "test_dir"
24*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR4 "test_dir4"
25*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "test_file"
26*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE 0755
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker static unsigned int del_dir_fd, file_fd;
29*49cdfc7eSAndroid Build Coastguard Worker static unsigned int invalid_fd = 999;
30*49cdfc7eSAndroid Build Coastguard Worker static unsigned int dir_fd;
31*49cdfc7eSAndroid Build Coastguard Worker static struct old_linux_dirent dirp;
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
34*49cdfc7eSAndroid Build Coastguard Worker unsigned int *fd;
35*49cdfc7eSAndroid Build Coastguard Worker struct old_linux_dirent *dirp;
36*49cdfc7eSAndroid Build Coastguard Worker unsigned int count;
37*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
38*49cdfc7eSAndroid Build Coastguard Worker char *desc;
39*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
40*49cdfc7eSAndroid Build Coastguard Worker {&del_dir_fd, &dirp, sizeof(struct old_linux_dirent), ENOENT, "directory deleted"},
41*49cdfc7eSAndroid Build Coastguard Worker {&file_fd, &dirp, sizeof(struct old_linux_dirent), ENOTDIR, "not a directory"},
42*49cdfc7eSAndroid Build Coastguard Worker {&invalid_fd, &dirp, sizeof(struct old_linux_dirent), EBADF, "invalid fd"},
43*49cdfc7eSAndroid Build Coastguard Worker {&dir_fd, (struct old_linux_dirent *)-1,
44*49cdfc7eSAndroid Build Coastguard Worker sizeof(struct old_linux_dirent), EFAULT, "invalid buffer pointer"},
45*49cdfc7eSAndroid Build Coastguard Worker };
46*49cdfc7eSAndroid Build Coastguard Worker
setup(void)47*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TEST_DIR, DIR_MODE);
52*49cdfc7eSAndroid Build Coastguard Worker del_dir_fd = SAFE_OPEN(TEST_DIR, O_RDONLY | O_DIRECTORY);
53*49cdfc7eSAndroid Build Coastguard Worker SAFE_RMDIR(TEST_DIR);
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker file_fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0777);
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TEST_DIR4, DIR_MODE);
58*49cdfc7eSAndroid Build Coastguard Worker dir_fd = SAFE_OPEN(TEST_DIR4, O_RDONLY | O_DIRECTORY);
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(tcases); i++) {
61*49cdfc7eSAndroid Build Coastguard Worker if (tcases[i].exp_errno == EFAULT)
62*49cdfc7eSAndroid Build Coastguard Worker tcases[i].dirp = tst_get_bad_addr(NULL);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker
verify_readdir(unsigned int nr)66*49cdfc7eSAndroid Build Coastguard Worker static void verify_readdir(unsigned int nr)
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[nr];
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(tst_syscall(__NR_readdir, *tc->fd, tc->dirp, tc->count),
71*49cdfc7eSAndroid Build Coastguard Worker tc->exp_errno, "readdir() with %s", tc->desc);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
75*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
76*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
77*49cdfc7eSAndroid Build Coastguard Worker .test = verify_readdir,
78*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
79*49cdfc7eSAndroid Build Coastguard Worker };
80