提交 acb7e049 编写于 作者: R Rich Felker

implement sendmmsg and recvmmsg

these are not pure syscall wrappers because they have to work around
kernel API bugs on 64-bit archs. the workarounds could probably be
made somewhat more efficient, but at the cost of more complexity. this
may be revisited later.
上级 39201d07
...@@ -26,6 +26,17 @@ struct ucred ...@@ -26,6 +26,17 @@ struct ucred
uid_t uid; uid_t uid;
gid_t gid; gid_t gid;
}; };
struct mmsghdr
{
struct msghdr msg_hdr;
unsigned int msg_len;
};
struct timespec;
int sendmmsg (int, struct mmsghdr *, unsigned int, unsigned int);
int recvmmsg (int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *);
#endif #endif
struct linger struct linger
......
#define _GNU_SOURCE
#include <sys/socket.h>
#include <limits.h>
#include "syscall.h"
int recvmmsg(int fd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags, struct timespec *timeout)
{
#if LONG_MAX > INT_MAX
struct mmsghdr *mh = msgvec;
unsigned int i;
for (i = vlen; i; i--, mh++)
mh->msg_hdr.__pad1 = mh->msg_hdr.__pad2 = 0;
#endif
return syscall_cp(SYS_recvmmsg, fd, msgvec, vlen, flags, timeout);
}
#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. */
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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册