| # define CMSG_ALIGN _CMSG_DATA_ALIGN |
| # define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1)) |
| # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+CMSG_ALIGN(len)) |
| # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+(len)) |
| char cms[CMSG_SPACE(sizeof(int))]; |
| memset(&msg, 0, sizeof msg); |
| msg.msg_control = (caddr_t)cms; |
| msg.msg_controllen = CMSG_LEN(sizeof(int)); |
| cmsg = CMSG_FIRSTHDR(&msg); |
| cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| cmsg->cmsg_level = SOL_SOCKET; |
| cmsg->cmsg_type = SCM_RIGHTS; |
| *(int*)CMSG_DATA(cmsg) = fd; |
| if((n=sendmsg(s, &msg, 0)) != iov.iov_len) |
| char cms[CMSG_SPACE(sizeof(int))]; |
| memset(&msg, 0, sizeof msg); |
| msg.msg_control = (caddr_t)cms; |
| msg.msg_controllen = sizeof cms; |
| if((n=recvmsg(s, &msg, 0)) < 0) |
| cmsg = CMSG_FIRSTHDR(&msg); |
| fd = *(int*)CMSG_DATA(cmsg); |