#include #include #include #include #include #include #include static void client(int fd) { int send_fd; struct msghdr msg; struct iovec iov[1]; char buf[1]; union { struct cmsghdr cm; char cntl[CMSG_SPACE(sizeof(int))]; } cntl_u; struct cmsghdr *cmsgptr; send_fd = open("/dev/null", O_RDWR); iov[0].iov_base = buf; iov[0].iov_len = 1; msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = cntl_u.cntl; msg.msg_controllen = sizeof(cntl_u.cntl); cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr->cmsg_len = CMSG_LEN(sizeof(int)); cmsgptr->cmsg_level = SOL_SOCKET; cmsgptr->cmsg_type = SCM_RIGHTS; *((int *)CMSG_DATA(cmsgptr)) = send_fd; if (sendmsg(fd, &msg, 0) <= 0) perror("sendmsg"); } static void server(int fd) { int recv_fd; struct stat statbuf; struct msghdr msg; struct iovec iov[1]; char buf[1]; union { struct cmsghdr cm; char cntl[CMSG_SPACE(sizeof(int))]; } cntl_u; struct cmsghdr *cmsgptr; iov[0].iov_base = buf; iov[0].iov_len = 1; msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = cntl_u.cntl; msg.msg_controllen = sizeof(cntl_u.cntl); if (recvmsg(fd, &msg, 0) <= 0) { perror("recvmsg"); exit(1); } cmsgptr = CMSG_FIRSTHDR(&msg); if (cmsgptr == NULL) { fprintf(stderr, "no control message\n"); exit(1); } if (cmsgptr->cmsg_len != CMSG_LEN(sizeof(int))) fprintf(stderr, "bad length: %d\n", cmsgptr->cmsg_len); if (cmsgptr->cmsg_level != SOL_SOCKET) fprintf(stderr, "not SOL_SOCKET: %d\n", cmsgptr->cmsg_level); if (cmsgptr->cmsg_type != SCM_RIGHTS) fprintf(stderr, "not SCM_RIGHTS: %d\n", cmsgptr->cmsg_type); recv_fd = *((int *)CMSG_DATA(cmsgptr)); if (fstat(recv_fd, &statbuf) == -1) { perror("stat"); exit(1); } printf("fd: %d, dev: %llx, mode: %o\n", recv_fd, statbuf.st_rdev, statbuf.st_mode); } int main() { pid_t pid; int status, fd[2]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) { perror("socketpair"); exit(1); } pid = fork(); if (pid == -1) { perror("fork"); exit(1); } if (pid == 0) { close(fd[1]); client(fd[0]); exit(0); } close(fd[0]); if (waitpid(pid, &status, 0) != pid) { perror("waitpid"); exit(1); } if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { fprintf(stderr, "client abnormal end\n"); exit(1); } server(fd[1]); return 0; }