2016-01-15 00:35:40 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LIBMEMUNREACHABLE_LEAK_PIPE_H_
|
|
|
|
#define LIBMEMUNREACHABLE_LEAK_PIPE_H_
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "android-base/macros.h"
|
|
|
|
|
|
|
|
#include "ScopedPipe.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2017-06-21 22:13:00 +02:00
|
|
|
namespace android {
|
|
|
|
|
2016-01-15 00:35:40 +01:00
|
|
|
// LeakPipe implements a pipe that can transfer vectors of simple objects
|
|
|
|
// between processes. The pipe is created in the sending process and
|
|
|
|
// transferred over a socketpair that was created before forking. This ensures
|
|
|
|
// that only the sending process can have the send side of the pipe open, so if
|
|
|
|
// the sending process dies the pipe will close.
|
|
|
|
class LeakPipe {
|
|
|
|
public:
|
|
|
|
LeakPipe() {
|
2017-06-22 19:50:05 +02:00
|
|
|
int ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv_);
|
2016-01-15 00:35:40 +01:00
|
|
|
if (ret < 0) {
|
2017-05-04 02:34:29 +02:00
|
|
|
MEM_LOG_ALWAYS_FATAL("failed to create socketpair: %s", strerror(errno));
|
2016-01-15 00:35:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
~LeakPipe() { Close(); }
|
2016-01-15 00:35:40 +01:00
|
|
|
|
|
|
|
void Close() {
|
|
|
|
close(sv_[0]);
|
|
|
|
close(sv_[1]);
|
|
|
|
sv_[0] = -1;
|
|
|
|
sv_[1] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenReceiver() {
|
|
|
|
int fd = ReceiveFd(sv_[0]);
|
|
|
|
if (fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver_.SetFd(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenSender() {
|
|
|
|
ScopedPipe pipe;
|
|
|
|
|
|
|
|
if (!SendFd(sv_[1], pipe.Receiver())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pipe.ReleaseReceiver();
|
|
|
|
|
|
|
|
sender_.SetFd(pipe.ReleaseSender());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class LeakPipeBase {
|
|
|
|
public:
|
|
|
|
LeakPipeBase() : fd_(-1) {}
|
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
~LeakPipeBase() { Close(); }
|
2016-01-15 00:35:40 +01:00
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
void SetFd(int fd) { fd_ = fd; }
|
2016-01-15 00:35:40 +01:00
|
|
|
|
|
|
|
void Close() {
|
|
|
|
close(fd_);
|
|
|
|
fd_ = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int fd_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(LeakPipeBase);
|
|
|
|
};
|
|
|
|
|
|
|
|
class LeakPipeSender : public LeakPipeBase {
|
|
|
|
public:
|
|
|
|
using LeakPipeBase::LeakPipeBase;
|
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
template <typename T>
|
2016-01-15 00:35:40 +01:00
|
|
|
bool Send(const T& value) {
|
|
|
|
ssize_t ret = TEMP_FAILURE_RETRY(write(fd_, &value, sizeof(T)));
|
|
|
|
if (ret < 0) {
|
2017-05-04 02:34:29 +02:00
|
|
|
MEM_ALOGE("failed to send value: %s", strerror(errno));
|
2016-01-15 00:35:40 +01:00
|
|
|
return false;
|
|
|
|
} else if (static_cast<size_t>(ret) != sizeof(T)) {
|
2017-05-04 02:34:29 +02:00
|
|
|
MEM_ALOGE("eof while writing value");
|
2016-01-15 00:35:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
template <class T, class Alloc = std::allocator<T>>
|
2016-01-15 00:35:40 +01:00
|
|
|
bool SendVector(const std::vector<T, Alloc>& vector) {
|
|
|
|
size_t size = vector.size() * sizeof(T);
|
|
|
|
if (!Send(size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t ret = TEMP_FAILURE_RETRY(write(fd_, vector.data(), size));
|
|
|
|
if (ret < 0) {
|
2017-05-04 02:34:29 +02:00
|
|
|
MEM_ALOGE("failed to send vector: %s", strerror(errno));
|
2016-01-15 00:35:40 +01:00
|
|
|
return false;
|
|
|
|
} else if (static_cast<size_t>(ret) != size) {
|
2017-05-04 02:34:29 +02:00
|
|
|
MEM_ALOGE("eof while writing vector");
|
2016-01-15 00:35:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class LeakPipeReceiver : public LeakPipeBase {
|
|
|
|
public:
|
|
|
|
using LeakPipeBase::LeakPipeBase;
|
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
template <typename T>
|
2016-01-15 00:35:40 +01:00
|
|
|
bool Receive(T* value) {
|
|
|
|
ssize_t ret = TEMP_FAILURE_RETRY(read(fd_, reinterpret_cast<void*>(value), sizeof(T)));
|
|
|
|
if (ret < 0) {
|
2017-05-04 02:34:29 +02:00
|
|
|
MEM_ALOGE("failed to receive value: %s", strerror(errno));
|
2016-01-15 00:35:40 +01:00
|
|
|
return false;
|
|
|
|
} else if (static_cast<size_t>(ret) != sizeof(T)) {
|
2017-05-04 02:34:29 +02:00
|
|
|
MEM_ALOGE("eof while receiving value");
|
2016-01-15 00:35:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
template <class T, class Alloc = std::allocator<T>>
|
2016-01-15 00:35:40 +01:00
|
|
|
bool ReceiveVector(std::vector<T, Alloc>& vector) {
|
|
|
|
size_t size = 0;
|
|
|
|
if (!Receive(&size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector.resize(size / sizeof(T));
|
|
|
|
|
|
|
|
char* ptr = reinterpret_cast<char*>(vector.data());
|
|
|
|
while (size > 0) {
|
|
|
|
ssize_t ret = TEMP_FAILURE_RETRY(read(fd_, ptr, size));
|
|
|
|
if (ret < 0) {
|
2017-05-04 02:34:29 +02:00
|
|
|
MEM_ALOGE("failed to send vector: %s", strerror(errno));
|
2016-01-15 00:35:40 +01:00
|
|
|
return false;
|
|
|
|
} else if (ret == 0) {
|
2017-05-04 02:34:29 +02:00
|
|
|
MEM_ALOGE("eof while reading vector");
|
2016-01-15 00:35:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size -= ret;
|
|
|
|
ptr += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
LeakPipeReceiver& Receiver() { return receiver_; }
|
2016-01-15 00:35:40 +01:00
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
LeakPipeSender& Sender() { return sender_; }
|
2016-01-15 00:35:40 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
LeakPipeReceiver receiver_;
|
|
|
|
LeakPipeSender sender_;
|
|
|
|
bool SendFd(int sock, int fd);
|
|
|
|
int ReceiveFd(int sock);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(LeakPipe);
|
|
|
|
int sv_[2];
|
|
|
|
};
|
|
|
|
|
2017-06-21 22:13:00 +02:00
|
|
|
} // namespace android
|
|
|
|
|
2017-06-22 19:50:05 +02:00
|
|
|
#endif // LIBMEMUNREACHABLE_LEAK_PIPE_H_
|