acdfdf29247f0f5a48e713e964ea1fc09f47cd7f
[musl] / src / network / sendmsg.c
1 #include <sys/socket.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "syscall.h"
6
7 ssize_t sendmsg(int fd, const struct msghdr *msg, int flags)
8 {
9 #if LONG_MAX > INT_MAX
10         struct msghdr h;
11         /* Kernels before 2.6.38 set SCM_MAX_FD to 255, allocate enough
12          * space to support an SCM_RIGHTS ancillary message with 255 fds.
13          * Kernels since 2.6.38 set SCM_MAX_FD to 253. */
14         struct cmsghdr chbuf[CMSG_SPACE(255*sizeof(int))/sizeof(struct cmsghdr)+1], *c;
15         if (msg) {
16                 h = *msg;
17                 h.__pad1 = h.__pad2 = 0;
18                 msg = &h;
19                 if (h.msg_controllen) {
20                         if (h.msg_controllen > sizeof chbuf) {
21                                 errno = ENOMEM;
22                                 return -1;
23                         }
24                         memcpy(chbuf, h.msg_control, h.msg_controllen);
25                         h.msg_control = chbuf;
26                         for (c=CMSG_FIRSTHDR(&h); c; c=CMSG_NXTHDR(&h,c))
27                                 c->__pad1 = 0;
28                 }
29         }
30 #endif
31         return socketcall_cp(sendmsg, fd, msg, flags, 0, 0, 0);
32 }