sendmmsg.c 961 B

1234567891011121314151617181920212223242526272829
  1. #define _GNU_SOURCE
  2. #include <sys/socket.h>
  3. #include <limits.h>
  4. #include <errno.h>
  5. #include "syscall.h"
  6. int sendmmsg(int fd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags)
  7. {
  8. #if LONG_MAX > INT_MAX
  9. /* Can't use the syscall directly because the kernel has the wrong
  10. * idea for the types of msg_iovlen, msg_controllen, and cmsg_len,
  11. * and the cmsg blocks cannot be modified in-place. */
  12. int i;
  13. if (vlen > IOV_MAX) vlen = IOV_MAX; /* This matches the kernel. */
  14. for (i=0; i<vlen; i++) {
  15. /* As an unfortunate inconsistency, the sendmmsg API uses
  16. * unsigned int for the resulting msg_len, despite sendmsg
  17. * returning ssize_t. However Linux limits the total bytes
  18. * sent by sendmsg to INT_MAX, so the assignment is safe. */
  19. ssize_t r = sendmsg(fd, &msgvec[i].msg_hdr, flags);
  20. if (r < 0) goto error;
  21. msgvec[i].msg_len = r;
  22. }
  23. error:
  24. return i ? i : -1;
  25. #else
  26. return syscall_cp(SYS_sendmmsg, fd, msgvec, vlen, flags);
  27. #endif
  28. }