xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/accept/accept03.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 /*
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2023-2024 Cyril Hrubis <[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 accept() returns ENOTSOCK or EBADF for non-socket file
11*49cdfc7eSAndroid Build Coastguard Worker  * descriptors. The EBADF is returned in the case that the file descriptor has
12*49cdfc7eSAndroid Build Coastguard Worker  * not a file associated with it, which is for example in the case of O_PATH
13*49cdfc7eSAndroid Build Coastguard Worker  * opened file.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
check_accept(struct tst_fd * fd)21*49cdfc7eSAndroid Build Coastguard Worker void check_accept(struct tst_fd *fd)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in addr = {
24*49cdfc7eSAndroid Build Coastguard Worker 		.sin_family = AF_INET,
25*49cdfc7eSAndroid Build Coastguard Worker 		.sin_port = 0,
26*49cdfc7eSAndroid Build Coastguard Worker 		.sin_addr = {.s_addr = INADDR_ANY},
27*49cdfc7eSAndroid Build Coastguard Worker 	};
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t size = sizeof(addr);
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno = ENOTSOCK;
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	switch (fd->type) {
34*49cdfc7eSAndroid Build Coastguard Worker 	case TST_FD_UNIX_SOCK:
35*49cdfc7eSAndroid Build Coastguard Worker 	case TST_FD_INET_SOCK:
36*49cdfc7eSAndroid Build Coastguard Worker 		return;
37*49cdfc7eSAndroid Build Coastguard Worker 	/*
38*49cdfc7eSAndroid Build Coastguard Worker 	 * With these two we fail even before we get to the do_accept() because
39*49cdfc7eSAndroid Build Coastguard Worker 	 * the fd does not have a struct file associated.
40*49cdfc7eSAndroid Build Coastguard Worker 	 */
41*49cdfc7eSAndroid Build Coastguard Worker 	case TST_FD_OPEN_TREE:
42*49cdfc7eSAndroid Build Coastguard Worker 	case TST_FD_PATH:
43*49cdfc7eSAndroid Build Coastguard Worker 		exp_errno = EBADF;
44*49cdfc7eSAndroid Build Coastguard Worker 	default:
45*49cdfc7eSAndroid Build Coastguard Worker 		break;
46*49cdfc7eSAndroid Build Coastguard Worker 	}
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(accept(fd->fd, (void*)&addr, &size),
49*49cdfc7eSAndroid Build Coastguard Worker 		exp_errno, "accept() on %s", tst_fd_desc(fd));
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
verify_accept(void)52*49cdfc7eSAndroid Build Coastguard Worker static void verify_accept(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	TST_FD_FOREACH(fd)
55*49cdfc7eSAndroid Build Coastguard Worker 		check_accept(&fd);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
59*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_accept,
60*49cdfc7eSAndroid Build Coastguard Worker };
61