blob: 94d4aa4866efcfd9d0b309cdbc708994fa2933aa [file] [log] [blame]
Colin Crossab7c9f12016-01-14 15:35:40 -08001/*
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
Colin Cross1fa81f52017-06-21 13:13:00 -070029namespace android {
30
Colin Crossab7c9f12016-01-14 15:35:40 -080031// LeakPipe implements a pipe that can transfer vectors of simple objects
32// between processes. The pipe is created in the sending process and
33// transferred over a socketpair that was created before forking. This ensures
34// that only the sending process can have the send side of the pipe open, so if
35// the sending process dies the pipe will close.
36class LeakPipe {
37 public:
38 LeakPipe() {
Colin Cross401319a2017-06-22 10:50:05 -070039 int ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv_);
Colin Crossab7c9f12016-01-14 15:35:40 -080040 if (ret < 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -070041 MEM_LOG_ALWAYS_FATAL("failed to create socketpair: %s", strerror(errno));
Colin Crossab7c9f12016-01-14 15:35:40 -080042 }
43 }
44
Colin Cross401319a2017-06-22 10:50:05 -070045 ~LeakPipe() { Close(); }
Colin Crossab7c9f12016-01-14 15:35:40 -080046
47 void Close() {
48 close(sv_[0]);
49 close(sv_[1]);
50 sv_[0] = -1;
51 sv_[1] = -1;
52 }
53
54 bool OpenReceiver() {
55 int fd = ReceiveFd(sv_[0]);
56 if (fd < 0) {
57 return false;
58 }
59
60 receiver_.SetFd(fd);
61 return true;
62 }
63
64 bool OpenSender() {
65 ScopedPipe pipe;
66
67 if (!SendFd(sv_[1], pipe.Receiver())) {
68 return false;
69 }
70 pipe.ReleaseReceiver();
71
72 sender_.SetFd(pipe.ReleaseSender());
73 return true;
74 }
75
76 class LeakPipeBase {
77 public:
78 LeakPipeBase() : fd_(-1) {}
79
Colin Cross401319a2017-06-22 10:50:05 -070080 ~LeakPipeBase() { Close(); }
Colin Crossab7c9f12016-01-14 15:35:40 -080081
Colin Cross401319a2017-06-22 10:50:05 -070082 void SetFd(int fd) { fd_ = fd; }
Colin Crossab7c9f12016-01-14 15:35:40 -080083
84 void Close() {
85 close(fd_);
86 fd_ = -1;
87 }
88
89 protected:
90 int fd_;
91
92 private:
93 DISALLOW_COPY_AND_ASSIGN(LeakPipeBase);
94 };
95
96 class LeakPipeSender : public LeakPipeBase {
97 public:
98 using LeakPipeBase::LeakPipeBase;
99
Colin Cross401319a2017-06-22 10:50:05 -0700100 template <typename T>
Colin Crossab7c9f12016-01-14 15:35:40 -0800101 bool Send(const T& value) {
102 ssize_t ret = TEMP_FAILURE_RETRY(write(fd_, &value, sizeof(T)));
103 if (ret < 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -0700104 MEM_ALOGE("failed to send value: %s", strerror(errno));
Colin Crossab7c9f12016-01-14 15:35:40 -0800105 return false;
106 } else if (static_cast<size_t>(ret) != sizeof(T)) {
Christopher Ferris56b8d862017-05-03 17:34:29 -0700107 MEM_ALOGE("eof while writing value");
Colin Crossab7c9f12016-01-14 15:35:40 -0800108 return false;
109 }
110
111 return true;
112 }
113
Colin Cross401319a2017-06-22 10:50:05 -0700114 template <class T, class Alloc = std::allocator<T>>
Colin Crossab7c9f12016-01-14 15:35:40 -0800115 bool SendVector(const std::vector<T, Alloc>& vector) {
116 size_t size = vector.size() * sizeof(T);
117 if (!Send(size)) {
118 return false;
119 }
120
121 ssize_t ret = TEMP_FAILURE_RETRY(write(fd_, vector.data(), size));
122 if (ret < 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -0700123 MEM_ALOGE("failed to send vector: %s", strerror(errno));
Colin Crossab7c9f12016-01-14 15:35:40 -0800124 return false;
125 } else if (static_cast<size_t>(ret) != size) {
Christopher Ferris56b8d862017-05-03 17:34:29 -0700126 MEM_ALOGE("eof while writing vector");
Colin Crossab7c9f12016-01-14 15:35:40 -0800127 return false;
128 }
129
130 return true;
131 }
132 };
133
134 class LeakPipeReceiver : public LeakPipeBase {
135 public:
136 using LeakPipeBase::LeakPipeBase;
137
Colin Cross401319a2017-06-22 10:50:05 -0700138 template <typename T>
Colin Crossab7c9f12016-01-14 15:35:40 -0800139 bool Receive(T* value) {
140 ssize_t ret = TEMP_FAILURE_RETRY(read(fd_, reinterpret_cast<void*>(value), sizeof(T)));
141 if (ret < 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -0700142 MEM_ALOGE("failed to receive value: %s", strerror(errno));
Colin Crossab7c9f12016-01-14 15:35:40 -0800143 return false;
144 } else if (static_cast<size_t>(ret) != sizeof(T)) {
Christopher Ferris56b8d862017-05-03 17:34:29 -0700145 MEM_ALOGE("eof while receiving value");
Colin Crossab7c9f12016-01-14 15:35:40 -0800146 return false;
147 }
148
149 return true;
150 }
151
Colin Cross401319a2017-06-22 10:50:05 -0700152 template <class T, class Alloc = std::allocator<T>>
Colin Crossab7c9f12016-01-14 15:35:40 -0800153 bool ReceiveVector(std::vector<T, Alloc>& vector) {
154 size_t size = 0;
155 if (!Receive(&size)) {
156 return false;
157 }
158
159 vector.resize(size / sizeof(T));
160
161 char* ptr = reinterpret_cast<char*>(vector.data());
162 while (size > 0) {
163 ssize_t ret = TEMP_FAILURE_RETRY(read(fd_, ptr, size));
164 if (ret < 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -0700165 MEM_ALOGE("failed to send vector: %s", strerror(errno));
Colin Crossab7c9f12016-01-14 15:35:40 -0800166 return false;
167 } else if (ret == 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -0700168 MEM_ALOGE("eof while reading vector");
Colin Crossab7c9f12016-01-14 15:35:40 -0800169 return false;
170 }
171 size -= ret;
172 ptr += ret;
173 }
174
175 return true;
176 }
Colin Crossab7c9f12016-01-14 15:35:40 -0800177 };
178
Colin Cross401319a2017-06-22 10:50:05 -0700179 LeakPipeReceiver& Receiver() { return receiver_; }
Colin Crossab7c9f12016-01-14 15:35:40 -0800180
Colin Cross401319a2017-06-22 10:50:05 -0700181 LeakPipeSender& Sender() { return sender_; }
Colin Crossab7c9f12016-01-14 15:35:40 -0800182
183 private:
184 LeakPipeReceiver receiver_;
185 LeakPipeSender sender_;
186 bool SendFd(int sock, int fd);
187 int ReceiveFd(int sock);
188 DISALLOW_COPY_AND_ASSIGN(LeakPipe);
189 int sv_[2];
190};
191
Colin Cross1fa81f52017-06-21 13:13:00 -0700192} // namespace android
193
Colin Cross401319a2017-06-22 10:50:05 -0700194#endif // LIBMEMUNREACHABLE_LEAK_PIPE_H_