blob: e6aee5f6b31be5268f7ca98770a38db9fd22ffa7 [file] [log] [blame]
Colin Crossbcb4ed32016-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
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.
34class LeakPipe {
35 public:
36 LeakPipe() {
Colin Crossa83881e2017-06-22 10:50:05 -070037 int ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv_);
Colin Crossbcb4ed32016-01-14 15:35:40 -080038 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -070039 MEM_LOG_ALWAYS_FATAL("failed to create socketpair: %s", strerror(errno));
Colin Crossbcb4ed32016-01-14 15:35:40 -080040 }
41 }
42
Colin Crossa83881e2017-06-22 10:50:05 -070043 ~LeakPipe() { Close(); }
Colin Crossbcb4ed32016-01-14 15:35:40 -080044
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 Crossa83881e2017-06-22 10:50:05 -070078 ~LeakPipeBase() { Close(); }
Colin Crossbcb4ed32016-01-14 15:35:40 -080079
Colin Crossa83881e2017-06-22 10:50:05 -070080 void SetFd(int fd) { fd_ = fd; }
Colin Crossbcb4ed32016-01-14 15:35:40 -080081
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 Crossa83881e2017-06-22 10:50:05 -070098 template <typename T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080099 bool Send(const T& value) {
100 ssize_t ret = TEMP_FAILURE_RETRY(write(fd_, &value, sizeof(T)));
101 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700102 MEM_ALOGE("failed to send value: %s", strerror(errno));
Colin Crossbcb4ed32016-01-14 15:35:40 -0800103 return false;
104 } else if (static_cast<size_t>(ret) != sizeof(T)) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700105 MEM_ALOGE("eof while writing value");
Colin Crossbcb4ed32016-01-14 15:35:40 -0800106 return false;
107 }
108
109 return true;
110 }
111
Colin Crossa83881e2017-06-22 10:50:05 -0700112 template <class T, class Alloc = std::allocator<T>>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800113 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 Ferris47dea712017-05-03 17:34:29 -0700121 MEM_ALOGE("failed to send vector: %s", strerror(errno));
Colin Crossbcb4ed32016-01-14 15:35:40 -0800122 return false;
123 } else if (static_cast<size_t>(ret) != size) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700124 MEM_ALOGE("eof while writing vector");
Colin Crossbcb4ed32016-01-14 15:35:40 -0800125 return false;
126 }
127
128 return true;
129 }
130 };
131
132 class LeakPipeReceiver : public LeakPipeBase {
133 public:
134 using LeakPipeBase::LeakPipeBase;
135
Colin Crossa83881e2017-06-22 10:50:05 -0700136 template <typename T>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800137 bool Receive(T* value) {
138 ssize_t ret = TEMP_FAILURE_RETRY(read(fd_, reinterpret_cast<void*>(value), sizeof(T)));
139 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700140 MEM_ALOGE("failed to receive value: %s", strerror(errno));
Colin Crossbcb4ed32016-01-14 15:35:40 -0800141 return false;
142 } else if (static_cast<size_t>(ret) != sizeof(T)) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700143 MEM_ALOGE("eof while receiving value");
Colin Crossbcb4ed32016-01-14 15:35:40 -0800144 return false;
145 }
146
147 return true;
148 }
149
Colin Crossa83881e2017-06-22 10:50:05 -0700150 template <class T, class Alloc = std::allocator<T>>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800151 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 Ferris47dea712017-05-03 17:34:29 -0700163 MEM_ALOGE("failed to send vector: %s", strerror(errno));
Colin Crossbcb4ed32016-01-14 15:35:40 -0800164 return false;
165 } else if (ret == 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700166 MEM_ALOGE("eof while reading vector");
Colin Crossbcb4ed32016-01-14 15:35:40 -0800167 return false;
168 }
169 size -= ret;
170 ptr += ret;
171 }
172
173 return true;
174 }
Colin Crossbcb4ed32016-01-14 15:35:40 -0800175 };
176
Colin Crossa83881e2017-06-22 10:50:05 -0700177 LeakPipeReceiver& Receiver() { return receiver_; }
Colin Crossbcb4ed32016-01-14 15:35:40 -0800178
Colin Crossa83881e2017-06-22 10:50:05 -0700179 LeakPipeSender& Sender() { return sender_; }
Colin Crossbcb4ed32016-01-14 15:35:40 -0800180
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 Crossa83881e2017-06-22 10:50:05 -0700190#endif // LIBMEMUNREACHABLE_LEAK_PIPE_H_