util.cpp revision cbe70cb0a8cb0171f3802273050e851a47b090ed
1/* 2 * Copyright 2016, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "debuggerd/util.h" 18 19#include <sys/socket.h> 20 21#include <utility> 22 23#include <android-base/unique_fd.h> 24#include <cutils/sockets.h> 25 26ssize_t send_fd(int sockfd, const void* data, size_t len, android::base::unique_fd fd) { 27 char cmsg_buf[CMSG_SPACE(sizeof(int))]; 28 29 iovec iov = { .iov_base = const_cast<void*>(data), .iov_len = len }; 30 msghdr msg = { 31 .msg_iov = &iov, .msg_iovlen = 1, .msg_control = cmsg_buf, .msg_controllen = sizeof(cmsg_buf), 32 }; 33 auto cmsg = CMSG_FIRSTHDR(&msg); 34 cmsg->cmsg_level = SOL_SOCKET; 35 cmsg->cmsg_type = SCM_RIGHTS; 36 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); 37 *reinterpret_cast<int*>(CMSG_DATA(cmsg)) = fd.get(); 38 39 return TEMP_FAILURE_RETRY(sendmsg(sockfd, &msg, 0)); 40} 41 42ssize_t recv_fd(int sockfd, void* _Nonnull data, size_t len, 43 android::base::unique_fd* _Nullable out_fd) { 44 char cmsg_buf[CMSG_SPACE(sizeof(int))]; 45 46 iovec iov = { .iov_base = const_cast<void*>(data), .iov_len = len }; 47 msghdr msg = { 48 .msg_iov = &iov, 49 .msg_iovlen = 1, 50 .msg_control = cmsg_buf, 51 .msg_controllen = sizeof(cmsg_buf), 52 .msg_flags = 0, 53 }; 54 auto cmsg = CMSG_FIRSTHDR(&msg); 55 cmsg->cmsg_level = SOL_SOCKET; 56 cmsg->cmsg_type = SCM_RIGHTS; 57 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); 58 59 ssize_t result = TEMP_FAILURE_RETRY(recvmsg(sockfd, &msg, 0)); 60 if (result == -1) { 61 return -1; 62 } 63 64 android::base::unique_fd fd; 65 bool received_fd = msg.msg_controllen == sizeof(cmsg_buf); 66 if (received_fd) { 67 fd.reset(*reinterpret_cast<int*>(CMSG_DATA(cmsg))); 68 } 69 70 if ((msg.msg_flags & MSG_TRUNC) != 0) { 71 errno = EFBIG; 72 return -1; 73 } else if ((msg.msg_flags & MSG_CTRUNC) != 0) { 74 errno = ERANGE; 75 return -1; 76 } 77 78 if (out_fd) { 79 *out_fd = std::move(fd); 80 } else if (received_fd) { 81 errno = ERANGE; 82 return -1; 83 } 84 85 return result; 86} 87 88bool Pipe(android::base::unique_fd* read, android::base::unique_fd* write) { 89 int pipefds[2]; 90 if (pipe(pipefds) != 0) { 91 return false; 92 } 93 read->reset(pipefds[0]); 94 write->reset(pipefds[1]); 95 return true; 96} 97