Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | #ifndef LIBMEMUNREACHABLE_LEAK_PIPE_H_ |
| 18 | #define LIBMEMUNREACHABLE_LEAK_PIPE_H_ |
| 19 | |
| 20 | #include <sys/socket.h> |
| 21 | |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "android-base/macros.h" |
| 25 | |
| 26 | #include "ScopedPipe.h" |
| 27 | #include "log.h" |
| 28 | |
| 29 | // LeakPipe implements a pipe that can transfer vectors of simple objects |
| 30 | // between processes. The pipe is created in the sending process and |
| 31 | // transferred over a socketpair that was created before forking. This ensures |
| 32 | // that only the sending process can have the send side of the pipe open, so if |
| 33 | // the sending process dies the pipe will close. |
| 34 | class LeakPipe { |
| 35 | public: |
| 36 | LeakPipe() { |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 37 | int ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv_); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 38 | if (ret < 0) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 39 | MEM_LOG_ALWAYS_FATAL("failed to create socketpair: %s", strerror(errno)); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 43 | ~LeakPipe() { Close(); } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 44 | |
| 45 | void Close() { |
| 46 | close(sv_[0]); |
| 47 | close(sv_[1]); |
| 48 | sv_[0] = -1; |
| 49 | sv_[1] = -1; |
| 50 | } |
| 51 | |
| 52 | bool OpenReceiver() { |
| 53 | int fd = ReceiveFd(sv_[0]); |
| 54 | if (fd < 0) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | receiver_.SetFd(fd); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool OpenSender() { |
| 63 | ScopedPipe pipe; |
| 64 | |
| 65 | if (!SendFd(sv_[1], pipe.Receiver())) { |
| 66 | return false; |
| 67 | } |
| 68 | pipe.ReleaseReceiver(); |
| 69 | |
| 70 | sender_.SetFd(pipe.ReleaseSender()); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | class LeakPipeBase { |
| 75 | public: |
| 76 | LeakPipeBase() : fd_(-1) {} |
| 77 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 78 | ~LeakPipeBase() { Close(); } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 79 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 80 | void SetFd(int fd) { fd_ = fd; } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 81 | |
| 82 | void Close() { |
| 83 | close(fd_); |
| 84 | fd_ = -1; |
| 85 | } |
| 86 | |
| 87 | protected: |
| 88 | int fd_; |
| 89 | |
| 90 | private: |
| 91 | DISALLOW_COPY_AND_ASSIGN(LeakPipeBase); |
| 92 | }; |
| 93 | |
| 94 | class LeakPipeSender : public LeakPipeBase { |
| 95 | public: |
| 96 | using LeakPipeBase::LeakPipeBase; |
| 97 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 98 | template <typename T> |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 99 | bool Send(const T& value) { |
| 100 | ssize_t ret = TEMP_FAILURE_RETRY(write(fd_, &value, sizeof(T))); |
| 101 | if (ret < 0) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 102 | MEM_ALOGE("failed to send value: %s", strerror(errno)); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 103 | return false; |
| 104 | } else if (static_cast<size_t>(ret) != sizeof(T)) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 105 | MEM_ALOGE("eof while writing value"); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 112 | template <class T, class Alloc = std::allocator<T>> |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 113 | bool SendVector(const std::vector<T, Alloc>& vector) { |
| 114 | size_t size = vector.size() * sizeof(T); |
| 115 | if (!Send(size)) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | ssize_t ret = TEMP_FAILURE_RETRY(write(fd_, vector.data(), size)); |
| 120 | if (ret < 0) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 121 | MEM_ALOGE("failed to send vector: %s", strerror(errno)); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 122 | return false; |
| 123 | } else if (static_cast<size_t>(ret) != size) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 124 | MEM_ALOGE("eof while writing vector"); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | class LeakPipeReceiver : public LeakPipeBase { |
| 133 | public: |
| 134 | using LeakPipeBase::LeakPipeBase; |
| 135 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 136 | template <typename T> |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 137 | bool Receive(T* value) { |
| 138 | ssize_t ret = TEMP_FAILURE_RETRY(read(fd_, reinterpret_cast<void*>(value), sizeof(T))); |
| 139 | if (ret < 0) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 140 | MEM_ALOGE("failed to receive value: %s", strerror(errno)); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 141 | return false; |
| 142 | } else if (static_cast<size_t>(ret) != sizeof(T)) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 143 | MEM_ALOGE("eof while receiving value"); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 150 | template <class T, class Alloc = std::allocator<T>> |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 151 | bool ReceiveVector(std::vector<T, Alloc>& vector) { |
| 152 | size_t size = 0; |
| 153 | if (!Receive(&size)) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | vector.resize(size / sizeof(T)); |
| 158 | |
| 159 | char* ptr = reinterpret_cast<char*>(vector.data()); |
| 160 | while (size > 0) { |
| 161 | ssize_t ret = TEMP_FAILURE_RETRY(read(fd_, ptr, size)); |
| 162 | if (ret < 0) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 163 | MEM_ALOGE("failed to send vector: %s", strerror(errno)); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 164 | return false; |
| 165 | } else if (ret == 0) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 166 | MEM_ALOGE("eof while reading vector"); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | size -= ret; |
| 170 | ptr += ret; |
| 171 | } |
| 172 | |
| 173 | return true; |
| 174 | } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 177 | LeakPipeReceiver& Receiver() { return receiver_; } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 178 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 179 | LeakPipeSender& Sender() { return sender_; } |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 180 | |
| 181 | private: |
| 182 | LeakPipeReceiver receiver_; |
| 183 | LeakPipeSender sender_; |
| 184 | bool SendFd(int sock, int fd); |
| 185 | int ReceiveFd(int sock); |
| 186 | DISALLOW_COPY_AND_ASSIGN(LeakPipe); |
| 187 | int sv_[2]; |
| 188 | }; |
| 189 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 190 | #endif // LIBMEMUNREACHABLE_LEAK_PIPE_H_ |