1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * written by Wayne Boyer
5 * Copyright (c) 2013 Markos Chandras
6 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Verify that:
13 *
14 * - getdents() fails with EBADF if file descriptor fd is invalid
15 * - getdents() fails with EINVAL if result buffer is too small
16 * - getdents() fails with ENOTDIR if file descriptor does not refer to a directory
17 * - getdents() fails with ENOENT if directory was unlinked()
18 */
19
20 #define _GNU_SOURCE
21 #include <errno.h>
22
23 #include "tst_test.h"
24 #include "getdents.h"
25
26 #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
27 S_IXGRP|S_IROTH|S_IXOTH)
28 #define TEST_DIR "test_dir"
29
30 char *TCID = "getdents02";
31
32 static char *dirp;
33 static size_t size;
34
35 static char dirp1_arr[1];
36 static char *dirp1 = dirp1_arr;
37 static size_t size1 = 1;
38
39 static int fd_inv = -5;
40 static int fd;
41 static int fd_file;
42 static int fd_unlinked;
43
44 static struct tcase {
45 int *fd;
46 char **dirp;
47 size_t *size;
48 int exp_errno;
49 } tcases[] = {
50 { &fd_inv, &dirp, &size, EBADF },
51 { &fd, &dirp1, &size1, EINVAL },
52 { &fd_file, &dirp, &size, ENOTDIR },
53 { &fd_unlinked, &dirp, &size, ENOENT },
54 };
55
setup(void)56 static void setup(void)
57 {
58 getdents_info();
59
60 size = tst_dirp_size();
61 dirp = tst_alloc(size);
62
63 fd = SAFE_OPEN(".", O_RDONLY);
64 fd_file = SAFE_OPEN("test", O_CREAT | O_RDWR, 0644);
65
66 SAFE_MKDIR(TEST_DIR, DIR_MODE);
67 fd_unlinked = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
68 SAFE_RMDIR(TEST_DIR);
69 }
70
run(unsigned int i)71 static void run(unsigned int i)
72 {
73 struct tcase *tc = tcases + i;
74
75 TEST(tst_getdents(*tc->fd, *tc->dirp, *tc->size));
76
77 if (TST_RET != -1) {
78 tst_res(TFAIL, "getdents() returned %ld", TST_RET);
79 return;
80 }
81
82 if (TST_ERR == tc->exp_errno) {
83 tst_res(TPASS | TTERRNO, "getdents failed as expected");
84 } else if (errno == ENOSYS) {
85 tst_res(TCONF, "syscall not implemented");
86 } else {
87 tst_res(TFAIL | TTERRNO, "getdents failed unexpectedly");
88 }
89 }
90
91 static struct tst_test test = {
92 .needs_tmpdir = 1,
93 .test = run,
94 .setup = setup,
95 .tcnt = ARRAY_SIZE(tcases),
96 .test_variants = TEST_VARIANTS,
97 };
98