4 #include <sys/socket.h>
11 # define CMSG_ALIGN _CMSG_DATA_ALIGN
13 # define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1))
18 # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+CMSG_ALIGN(len))
22 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+(len))
33 char cms[CMSG_SPACE(sizeof(int))];
39 memset(&msg, 0, sizeof msg);
42 msg.msg_control = (caddr_t)cms;
43 msg.msg_controllen = CMSG_LEN(sizeof(int));
45 cmsg = CMSG_FIRSTHDR(&msg);
46 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
47 cmsg->cmsg_level = SOL_SOCKET;
48 cmsg->cmsg_type = SCM_RIGHTS;
49 *(int*)CMSG_DATA(cmsg) = fd;
51 if((n=sendmsg(s, &msg, 0)) != iov.iov_len)
65 char cms[CMSG_SPACE(sizeof(int))];
70 memset(&msg, 0, sizeof msg);
76 msg.msg_control = (caddr_t)cms;
77 msg.msg_controllen = sizeof cms;
79 if((n=recvmsg(s, &msg, 0)) < 0)
82 cmsg = CMSG_FIRSTHDR(&msg);
83 fd = *(int*)CMSG_DATA(cmsg);