xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sendmmsg/sendmmsg01.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  * Basic sendmmsg() test that sends and receives messages.
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This test is based on source contained in the man pages for sendmmsg and
6*49cdfc7eSAndroid Build Coastguard Worker  * recvmmsg in release 4.15 of the Linux man-pages project.
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
10*49cdfc7eSAndroid Build Coastguard Worker #include "sendmmsg.h"
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #define BUFSIZE 16
13*49cdfc7eSAndroid Build Coastguard Worker #define VLEN 2
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker static int send_sockfd;
16*49cdfc7eSAndroid Build Coastguard Worker static int receive_sockfd;
17*49cdfc7eSAndroid Build Coastguard Worker static struct mmsghdr *snd_msg, *rcv_msg;
18*49cdfc7eSAndroid Build Coastguard Worker static struct iovec *snd1, *snd2, *rcv1, *rcv2;
19*49cdfc7eSAndroid Build Coastguard Worker 
run(void)20*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker 	struct time64_variants *tv = &variants[tst_variant];
23*49cdfc7eSAndroid Build Coastguard Worker 	struct tst_ts timeout;
24*49cdfc7eSAndroid Build Coastguard Worker 	int retval;
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	retval = tv->sendmmsg(send_sockfd, snd_msg, VLEN, 0);
27*49cdfc7eSAndroid Build Coastguard Worker 	if (retval < 0 || snd_msg[0].msg_len != 6 || snd_msg[1].msg_len != 6) {
28*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "sendmmsg() failed");
29*49cdfc7eSAndroid Build Coastguard Worker 		return;
30*49cdfc7eSAndroid Build Coastguard Worker 	}
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	memset(rcv1->iov_base, 0, rcv1->iov_len);
33*49cdfc7eSAndroid Build Coastguard Worker 	memset(rcv2->iov_base, 0, rcv2->iov_len);
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	timeout.type = tv->ts_type;
36*49cdfc7eSAndroid Build Coastguard Worker 	tst_ts_set_sec(&timeout, 1);
37*49cdfc7eSAndroid Build Coastguard Worker 	tst_ts_set_nsec(&timeout, 0);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	retval = tv->recvmmsg(receive_sockfd, rcv_msg, VLEN, 0, tst_ts_get(&timeout));
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	if (retval == -1) {
42*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "recvmmsg() failed");
43*49cdfc7eSAndroid Build Coastguard Worker 		return;
44*49cdfc7eSAndroid Build Coastguard Worker 	}
45*49cdfc7eSAndroid Build Coastguard Worker 	if (retval != 2) {
46*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Received unexpected number of messages (%d)",
47*49cdfc7eSAndroid Build Coastguard Worker 			retval);
48*49cdfc7eSAndroid Build Coastguard Worker 		return;
49*49cdfc7eSAndroid Build Coastguard Worker 	}
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	if (memcmp(rcv1->iov_base, "onetwo", 6))
52*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Error in first received message");
53*49cdfc7eSAndroid Build Coastguard Worker 	else
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "First message received successfully");
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	if (memcmp(rcv2->iov_base, "three", 5))
57*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Error in second received message");
58*49cdfc7eSAndroid Build Coastguard Worker 	else
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Second message received successfully");
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)62*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in addr;
65*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int port = TST_GET_UNUSED_PORT(AF_INET, SOCK_DGRAM);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	send_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
68*49cdfc7eSAndroid Build Coastguard Worker 	receive_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	addr.sin_family = AF_INET;
71*49cdfc7eSAndroid Build Coastguard Worker 	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
72*49cdfc7eSAndroid Build Coastguard Worker 	addr.sin_port = port;
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(receive_sockfd, (struct sockaddr *)&addr, sizeof(addr));
75*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CONNECT(send_sockfd, (struct sockaddr *)&addr, sizeof(addr));
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	memcpy(snd1[0].iov_base, "one", snd1[0].iov_len);
78*49cdfc7eSAndroid Build Coastguard Worker 	memcpy(snd1[1].iov_base, "two", snd1[1].iov_len);
79*49cdfc7eSAndroid Build Coastguard Worker 	memcpy(snd2->iov_base, "three3", snd2->iov_len);
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	memset(snd_msg, 0, VLEN * sizeof(*snd_msg));
82*49cdfc7eSAndroid Build Coastguard Worker 	snd_msg[0].msg_hdr.msg_iov = snd1;
83*49cdfc7eSAndroid Build Coastguard Worker 	snd_msg[0].msg_hdr.msg_iovlen = 2;
84*49cdfc7eSAndroid Build Coastguard Worker 	snd_msg[1].msg_hdr.msg_iov = snd2;
85*49cdfc7eSAndroid Build Coastguard Worker 	snd_msg[1].msg_hdr.msg_iovlen = 1;
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	memset(rcv_msg, 0, VLEN * sizeof(*rcv_msg));
88*49cdfc7eSAndroid Build Coastguard Worker 	rcv_msg[0].msg_hdr.msg_iov = rcv1;
89*49cdfc7eSAndroid Build Coastguard Worker 	rcv_msg[0].msg_hdr.msg_iovlen = 1;
90*49cdfc7eSAndroid Build Coastguard Worker 	rcv_msg[1].msg_hdr.msg_iov = rcv2;
91*49cdfc7eSAndroid Build Coastguard Worker 	rcv_msg[1].msg_hdr.msg_iovlen = 1;
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)96*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker 	if (send_sockfd > 0)
99*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(send_sockfd);
100*49cdfc7eSAndroid Build Coastguard Worker 	if (receive_sockfd > 0)
101*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(receive_sockfd);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
105*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
106*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
107*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
108*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = ARRAY_SIZE(variants),
109*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
110*49cdfc7eSAndroid Build Coastguard Worker 		{&snd1, .iov_sizes = (int[]){3, 3, -1}},
111*49cdfc7eSAndroid Build Coastguard Worker 		{&snd2, .iov_sizes = (int[]){6, -1}},
112*49cdfc7eSAndroid Build Coastguard Worker 		{&rcv1, .iov_sizes = (int[]){6, -1}},
113*49cdfc7eSAndroid Build Coastguard Worker 		{&rcv2, .iov_sizes = (int[]){5, -1}},
114*49cdfc7eSAndroid Build Coastguard Worker 		{&snd_msg, .size = VLEN * sizeof(*snd_msg)},
115*49cdfc7eSAndroid Build Coastguard Worker 		{&rcv_msg, .size = VLEN * sizeof(*rcv_msg)},
116*49cdfc7eSAndroid Build Coastguard Worker 		{},
117*49cdfc7eSAndroid Build Coastguard Worker 	}
118*49cdfc7eSAndroid Build Coastguard Worker };
119