1570af302Sopenharmony_ci#define _GNU_SOURCE
2570af302Sopenharmony_ci#include <sys/socket.h>
3570af302Sopenharmony_ci#include <limits.h>
4570af302Sopenharmony_ci#include <errno.h>
5570af302Sopenharmony_ci#include "syscall.h"
6570af302Sopenharmony_ci
7570af302Sopenharmony_ciint sendmmsg(int fd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags)
8570af302Sopenharmony_ci{
9570af302Sopenharmony_ci#if LONG_MAX > INT_MAX
10570af302Sopenharmony_ci	/* Can't use the syscall directly because the kernel has the wrong
11570af302Sopenharmony_ci	 * idea for the types of msg_iovlen, msg_controllen, and cmsg_len,
12570af302Sopenharmony_ci	 * and the cmsg blocks cannot be modified in-place. */
13570af302Sopenharmony_ci	int i;
14570af302Sopenharmony_ci	if (vlen > IOV_MAX) vlen = IOV_MAX; /* This matches the kernel. */
15570af302Sopenharmony_ci	if (!vlen) return 0;
16570af302Sopenharmony_ci	for (i=0; i<vlen; i++) {
17570af302Sopenharmony_ci		/* As an unfortunate inconsistency, the sendmmsg API uses
18570af302Sopenharmony_ci		 * unsigned int for the resulting msg_len, despite sendmsg
19570af302Sopenharmony_ci		 * returning ssize_t. However Linux limits the total bytes
20570af302Sopenharmony_ci		 * sent by sendmsg to INT_MAX, so the assignment is safe. */
21570af302Sopenharmony_ci		ssize_t r = sendmsg(fd, &msgvec[i].msg_hdr, flags);
22570af302Sopenharmony_ci		if (r < 0) goto error;
23570af302Sopenharmony_ci		msgvec[i].msg_len = r;
24570af302Sopenharmony_ci	}
25570af302Sopenharmony_cierror:
26570af302Sopenharmony_ci	return i ? i : -1;
27570af302Sopenharmony_ci#else
28570af302Sopenharmony_ci	return syscall_cp(SYS_sendmmsg, fd, msgvec, vlen, flags);
29570af302Sopenharmony_ci#endif
30570af302Sopenharmony_ci}
31