summaryrefslogtreecommitdiffstats
path: root/libc-top-half/musl/src/network/sendmmsg.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
commit8c1ab65c0f548d20b7f177bdb736daaf603340e1 (patch)
treedf55b7e75bf43f2bf500845b105afe3ac3a5157e /libc-top-half/musl/src/network/sendmmsg.c
parentInitial commit. (diff)
downloadwasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.tar.xz
wasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.zip
Adding upstream version 0.0~git20221206.8b7148f.upstream/0.0_git20221206.8b7148f
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libc-top-half/musl/src/network/sendmmsg.c')
-rw-r--r--libc-top-half/musl/src/network/sendmmsg.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/network/sendmmsg.c b/libc-top-half/musl/src/network/sendmmsg.c
new file mode 100644
index 0000000..eeae1d0
--- /dev/null
+++ b/libc-top-half/musl/src/network/sendmmsg.c
@@ -0,0 +1,30 @@
+#define _GNU_SOURCE
+#include <sys/socket.h>
+#include <limits.h>
+#include <errno.h>
+#include "syscall.h"
+
+int sendmmsg(int fd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags)
+{
+#if LONG_MAX > INT_MAX
+ /* Can't use the syscall directly because the kernel has the wrong
+ * idea for the types of msg_iovlen, msg_controllen, and cmsg_len,
+ * and the cmsg blocks cannot be modified in-place. */
+ int i;
+ if (vlen > IOV_MAX) vlen = IOV_MAX; /* This matches the kernel. */
+ if (!vlen) return 0;
+ for (i=0; i<vlen; i++) {
+ /* As an unfortunate inconsistency, the sendmmsg API uses
+ * unsigned int for the resulting msg_len, despite sendmsg
+ * returning ssize_t. However Linux limits the total bytes
+ * sent by sendmsg to INT_MAX, so the assignment is safe. */
+ ssize_t r = sendmsg(fd, &msgvec[i].msg_hdr, flags);
+ if (r < 0) goto error;
+ msgvec[i].msg_len = r;
+ }
+error:
+ return i ? i : -1;
+#else
+ return syscall_cp(SYS_sendmmsg, fd, msgvec, vlen, flags);
+#endif
+}