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