|
| 1 | +#define _GNU_SOURCE |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <sys/socket.h> |
| 5 | +#include "ipc.h" |
| 6 | +#include "log.h" |
| 7 | + |
| 8 | +int receive_fd(int sockfd) |
| 9 | +{ |
| 10 | + int bytes_read; |
| 11 | + struct msghdr msg = { }; |
| 12 | + struct cmsghdr *cmsg; |
| 13 | + struct iovec iov = { }; |
| 14 | + char null_byte = '\0'; |
| 15 | + int ret; |
| 16 | + int fd_count; |
| 17 | + |
| 18 | + iov.iov_base = &null_byte; |
| 19 | + iov.iov_len = 1; |
| 20 | + |
| 21 | + msg.msg_iov = &iov; |
| 22 | + msg.msg_iovlen = 1; |
| 23 | + |
| 24 | + msg.msg_controllen = CMSG_SPACE(sizeof(int)); |
| 25 | + msg.msg_control = malloc(msg.msg_controllen); |
| 26 | + if (msg.msg_control == NULL) { |
| 27 | + bail("Can't allocate memory to receive fd."); |
| 28 | + } |
| 29 | + |
| 30 | + memset(msg.msg_control, 0, msg.msg_controllen); |
| 31 | + |
| 32 | + bytes_read = recvmsg(sockfd, &msg, 0); |
| 33 | + if (bytes_read != 1) |
| 34 | + bail("failed to receive fd from unix socket %d", sockfd); |
| 35 | + if (msg.msg_flags & MSG_CTRUNC) |
| 36 | + bail("received truncated control message from unix socket %d", sockfd); |
| 37 | + |
| 38 | + cmsg = CMSG_FIRSTHDR(&msg); |
| 39 | + if (!cmsg) |
| 40 | + bail("received message from unix socket %d without control message", sockfd); |
| 41 | + |
| 42 | + if (cmsg->cmsg_level != SOL_SOCKET) |
| 43 | + bail("received unknown control message from unix socket %d: cmsg_level=%d", sockfd, cmsg->cmsg_level); |
| 44 | + |
| 45 | + if (cmsg->cmsg_type != SCM_RIGHTS) |
| 46 | + bail("received unknown control message from unix socket %d: cmsg_type=%d", sockfd, cmsg->cmsg_type); |
| 47 | + |
| 48 | + fd_count = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); |
| 49 | + if (fd_count != 1) |
| 50 | + bail("received control message from unix socket %d with too many fds: %d", sockfd, fd_count); |
| 51 | + |
| 52 | + ret = *(int *)CMSG_DATA(cmsg); |
| 53 | + free(msg.msg_control); |
| 54 | + return ret; |
| 55 | +} |
| 56 | + |
| 57 | +void send_fd(int sockfd, int fd) |
| 58 | +{ |
| 59 | + int bytes_written; |
| 60 | + struct msghdr msg = { }; |
| 61 | + struct cmsghdr *cmsg; |
| 62 | + struct iovec iov[1] = { }; |
| 63 | + char null_byte = '\0'; |
| 64 | + |
| 65 | + iov[0].iov_base = &null_byte; |
| 66 | + iov[0].iov_len = 1; |
| 67 | + |
| 68 | + msg.msg_iov = iov; |
| 69 | + msg.msg_iovlen = 1; |
| 70 | + |
| 71 | + /* We send only one fd as specified by cmsg->cmsg_len below, even |
| 72 | + * though msg.msg_controllen might have more space due to alignment. */ |
| 73 | + msg.msg_controllen = CMSG_SPACE(sizeof(int)); |
| 74 | + msg.msg_control = malloc(msg.msg_controllen); |
| 75 | + if (msg.msg_control == NULL) { |
| 76 | + bail("Can't allocate memory to send fd."); |
| 77 | + } |
| 78 | + |
| 79 | + memset(msg.msg_control, 0, msg.msg_controllen); |
| 80 | + |
| 81 | + cmsg = CMSG_FIRSTHDR(&msg); |
| 82 | + cmsg->cmsg_level = SOL_SOCKET; |
| 83 | + cmsg->cmsg_type = SCM_RIGHTS; |
| 84 | + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 85 | + memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); |
| 86 | + |
| 87 | + bytes_written = sendmsg(sockfd, &msg, 0); |
| 88 | + |
| 89 | + free(msg.msg_control); |
| 90 | + |
| 91 | + if (bytes_written != 1) |
| 92 | + bail("failed to send fd %d via unix socket %d", fd, sockfd); |
| 93 | +} |
0 commit comments