1 /* include write_fd */
2 #include "unp.h"
3
4 ssize_t
write_fd(int fd,void * ptr,size_t nbytes,int sendfd)5 write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
6 {
7 struct msghdr msg;
8 struct iovec iov[1];
9
10 #ifdef HAVE_MSGHDR_MSG_CONTROL
11 union {
12 struct cmsghdr cm;
13 char control[CMSG_SPACE(sizeof(int))];
14 } control_un;
15 struct cmsghdr *cmptr;
16
17 msg.msg_control = control_un.control;
18 msg.msg_controllen = sizeof(control_un.control);
19
20 cmptr = CMSG_FIRSTHDR(&msg);
21 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
22 cmptr->cmsg_level = SOL_SOCKET;
23 cmptr->cmsg_type = SCM_RIGHTS;
24 *((int *) CMSG_DATA(cmptr)) = sendfd;
25 #else
26 msg.msg_accrights = (caddr_t) &sendfd;
27 msg.msg_accrightslen = sizeof(int);
28 #endif
29
30 msg.msg_name = NULL;
31 msg.msg_namelen = 0;
32
33 iov[0].iov_base = ptr;
34 iov[0].iov_len = nbytes;
35 msg.msg_iov = iov;
36 msg.msg_iovlen = 1;
37
38 return(sendmsg(fd, &msg, 0));
39 }
40 /* end write_fd */
41
42 ssize_t
Write_fd(int fd,void * ptr,size_t nbytes,int sendfd)43 Write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
44 {
45 ssize_t n;
46
47 if ( (n = write_fd(fd, ptr, nbytes, sendfd)) < 0)
48 err_sys("write_fd error");
49
50 return(n);
51 }
52