xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/recvmmsg/recvmmsg01.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  * Test recvmmsg() errors:
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * - EBADF  Bad socket file descriptor
7*49cdfc7eSAndroid Build Coastguard Worker  * - EFAULT Bad message vector address
8*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL Bad seconds value for the timeout argument
9*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL Bad nanoseconds value for the timeout argument
10*49cdfc7eSAndroid Build Coastguard Worker  * - EFAULT Bad timeout address
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
14*49cdfc7eSAndroid Build Coastguard Worker #include "../sendmmsg/sendmmsg.h"
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker static int send_sockfd;
17*49cdfc7eSAndroid Build Coastguard Worker static int receive_sockfd;
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define VLEN 1
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static struct mmsghdr *msg;
22*49cdfc7eSAndroid Build Coastguard Worker static struct iovec *iov;
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static void *bad_addr;
25*49cdfc7eSAndroid Build Coastguard Worker static int bad_fd = -1;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static struct tst_ts ts;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker struct test_case {
30*49cdfc7eSAndroid Build Coastguard Worker 	const char *desc;
31*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
32*49cdfc7eSAndroid Build Coastguard Worker 	long tv_sec;
33*49cdfc7eSAndroid Build Coastguard Worker 	long tv_nsec;
34*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
35*49cdfc7eSAndroid Build Coastguard Worker 	struct mmsghdr **msg_vec;
36*49cdfc7eSAndroid Build Coastguard Worker 	int bad_ts_addr;
37*49cdfc7eSAndroid Build Coastguard Worker };
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static struct test_case tcase[] = {
40*49cdfc7eSAndroid Build Coastguard Worker 	{
41*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "bad socket file descriptor",
42*49cdfc7eSAndroid Build Coastguard Worker 		.fd = &bad_fd,
43*49cdfc7eSAndroid Build Coastguard Worker 		.exp_errno = EBADF,
44*49cdfc7eSAndroid Build Coastguard Worker 		.msg_vec = &msg,
45*49cdfc7eSAndroid Build Coastguard Worker 	},
46*49cdfc7eSAndroid Build Coastguard Worker 	{
47*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "bad message vector address",
48*49cdfc7eSAndroid Build Coastguard Worker 		.fd = &receive_sockfd,
49*49cdfc7eSAndroid Build Coastguard Worker 		.exp_errno = EFAULT,
50*49cdfc7eSAndroid Build Coastguard Worker 		.msg_vec = (void*)&bad_addr,
51*49cdfc7eSAndroid Build Coastguard Worker 	},
52*49cdfc7eSAndroid Build Coastguard Worker 	{
53*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "negative seconds in timeout",
54*49cdfc7eSAndroid Build Coastguard Worker 		.fd = &receive_sockfd,
55*49cdfc7eSAndroid Build Coastguard Worker 		.tv_sec = -1,
56*49cdfc7eSAndroid Build Coastguard Worker 		.tv_nsec = 0,
57*49cdfc7eSAndroid Build Coastguard Worker 		.exp_errno = EINVAL,
58*49cdfc7eSAndroid Build Coastguard Worker 		.msg_vec = &msg,
59*49cdfc7eSAndroid Build Coastguard Worker 	},
60*49cdfc7eSAndroid Build Coastguard Worker 	{
61*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "overflow in nanoseconds in timeout",
62*49cdfc7eSAndroid Build Coastguard Worker 		.fd = &receive_sockfd,
63*49cdfc7eSAndroid Build Coastguard Worker 		.tv_sec = 1,
64*49cdfc7eSAndroid Build Coastguard Worker 		.tv_nsec = 1000000001,
65*49cdfc7eSAndroid Build Coastguard Worker 		.exp_errno = EINVAL,
66*49cdfc7eSAndroid Build Coastguard Worker 		.msg_vec = &msg,
67*49cdfc7eSAndroid Build Coastguard Worker 	},
68*49cdfc7eSAndroid Build Coastguard Worker 	{
69*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "bad timeout address",
70*49cdfc7eSAndroid Build Coastguard Worker 		.fd = &receive_sockfd,
71*49cdfc7eSAndroid Build Coastguard Worker 		.exp_errno = EFAULT,
72*49cdfc7eSAndroid Build Coastguard Worker 		.msg_vec = &msg,
73*49cdfc7eSAndroid Build Coastguard Worker 		.bad_ts_addr = 1,
74*49cdfc7eSAndroid Build Coastguard Worker 	}
75*49cdfc7eSAndroid Build Coastguard Worker };
76*49cdfc7eSAndroid Build Coastguard Worker 
do_test(unsigned int i)77*49cdfc7eSAndroid Build Coastguard Worker static void do_test(unsigned int i)
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker 	struct time64_variants *tv = &variants[tst_variant];
80*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = &tcase[i];
81*49cdfc7eSAndroid Build Coastguard Worker 	void *timeout;
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	ts.type = tv->ts_type;
84*49cdfc7eSAndroid Build Coastguard Worker 	tst_ts_set_sec(&ts, tc->tv_sec);
85*49cdfc7eSAndroid Build Coastguard Worker 	tst_ts_set_nsec(&ts, tc->tv_nsec);
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->bad_ts_addr)
88*49cdfc7eSAndroid Build Coastguard Worker 		timeout = bad_addr;
89*49cdfc7eSAndroid Build Coastguard Worker 	else
90*49cdfc7eSAndroid Build Coastguard Worker 		timeout = tst_ts_get(&ts);
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(tv->recvmmsg(*tc->fd, *tc->msg_vec, VLEN, 0, timeout),
93*49cdfc7eSAndroid Build Coastguard Worker 	             tc->exp_errno, "recvmmsg() %s", tc->desc);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)96*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in addr;
99*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int port = TST_GET_UNUSED_PORT(AF_INET, SOCK_DGRAM);
100*49cdfc7eSAndroid Build Coastguard Worker 	struct time64_variants *tv = &variants[tst_variant];
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	send_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
105*49cdfc7eSAndroid Build Coastguard Worker 	receive_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	addr.sin_family = AF_INET;
108*49cdfc7eSAndroid Build Coastguard Worker 	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
109*49cdfc7eSAndroid Build Coastguard Worker 	addr.sin_port = port;
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(receive_sockfd, (struct sockaddr *)&addr, sizeof(addr));
112*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CONNECT(send_sockfd, (struct sockaddr *)&addr, sizeof(addr));
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	msg[0].msg_hdr.msg_iov = iov;
115*49cdfc7eSAndroid Build Coastguard Worker 	msg[0].msg_hdr.msg_iovlen = 1;
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tv->sendmmsg(send_sockfd, msg, 1, 0));
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 1) {
120*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "sendmmsg() failed");
121*49cdfc7eSAndroid Build Coastguard Worker 		return;
122*49cdfc7eSAndroid Build Coastguard Worker 	}
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	bad_addr = tst_get_bad_addr(NULL);
125*49cdfc7eSAndroid Build Coastguard Worker }
126*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)127*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
128*49cdfc7eSAndroid Build Coastguard Worker {
129*49cdfc7eSAndroid Build Coastguard Worker 	if (send_sockfd > 0)
130*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(send_sockfd);
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	if (receive_sockfd > 0)
133*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(receive_sockfd);
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
137*49cdfc7eSAndroid Build Coastguard Worker 	.test = do_test,
138*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcase),
139*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
140*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
141*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = ARRAY_SIZE(variants),
142*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
143*49cdfc7eSAndroid Build Coastguard Worker 		{&iov, .iov_sizes = (int[]){1, -1}},
144*49cdfc7eSAndroid Build Coastguard Worker 		{&msg, .size = VLEN * sizeof(*msg)},
145*49cdfc7eSAndroid Build Coastguard Worker 		{},
146*49cdfc7eSAndroid Build Coastguard Worker 	}
147*49cdfc7eSAndroid Build Coastguard Worker };
148