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