int ForkWrapper::get_fd() { ANCIL_FD_BUFFER(1) buffer; struct msghdr msghdr; char nothing; struct iovec nothing_ptr; struct cmsghdr *cmsg; nothing_ptr.iov_base = ¬hing; nothing_ptr.iov_len = 1; msghdr.msg_name = NULL; msghdr.msg_namelen = 0; msghdr.msg_iov = ¬hing_ptr; msghdr.msg_iovlen = 1; msghdr.msg_flags = 0; msghdr.msg_control = &buffer; msghdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int); cmsg = CMSG_FIRSTHDR(&msghdr); cmsg->cmsg_len = msghdr.msg_controllen; cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; int *ifd = (int *)CMSG_DATA(cmsg); *ifd = -1; if(recvmsg(parent_fd, &msghdr, 0) < 0) return(-1); return *ifd; }
int ancil_send_fd(int sock, int fd) { ANCIL_FD_BUFFER(1) buffer; return(ancil_send_fds_with_buffer(sock, &fd, 1, &buffer)); }
void ForkWrapper::send_fd(int fd) { ANCIL_FD_BUFFER(1) buffer; struct msghdr msghdr; char nothing = '!'; struct iovec nothing_ptr; struct cmsghdr *cmsg; nothing_ptr.iov_base = ¬hing; nothing_ptr.iov_len = 1; msghdr.msg_name = NULL; msghdr.msg_namelen = 0; msghdr.msg_iov = ¬hing_ptr; msghdr.msg_iovlen = 1; msghdr.msg_flags = 0; msghdr.msg_control = &buffer; msghdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int); cmsg = CMSG_FIRSTHDR(&msghdr); cmsg->cmsg_len = msghdr.msg_controllen; cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; int *ifd = (int *)CMSG_DATA(cmsg); *ifd = fd; sendmsg(child_fd, &msghdr, 0); }
int ancil_send_fds(int sock, const int *fds, unsigned n_fds) { ANCIL_FD_BUFFER(ANCIL_MAX_N_FDS) buffer; assert(n_fds <= ANCIL_MAX_N_FDS); return(ancil_send_fds_with_buffer(sock, fds, n_fds, &buffer)); }
/** shortcut for receiving only one \e fd from \e sock */ static int ancil_recv_fd(int sock, int *fd) { ANCIL_FD_BUFFER(1) buffer; return ancil_recv_fds_with_buffer(sock, fd, 1, &buffer) == 1 ? 0 : -1; }