Merge "base: add Pipe and Socketpair wrappers." am: 287e8348de
am: b8ae24c73b
Change-Id: I9ae44e8ef9ddaf4cf9bf3a8f8a514074c5104bdc
This commit is contained in:
commit
4db97b46fa
|
@ -17,6 +17,13 @@
|
|||
#ifndef ANDROID_BASE_UNIQUE_FD_H
|
||||
#define ANDROID_BASE_UNIQUE_FD_H
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// DO NOT INCLUDE OTHER LIBBASE HEADERS!
|
||||
|
@ -88,6 +95,35 @@ class unique_fd_impl final {
|
|||
|
||||
using unique_fd = unique_fd_impl<DefaultCloser>;
|
||||
|
||||
#if !defined(_WIN32)
|
||||
|
||||
// Inline functions, so that they can be used header-only.
|
||||
inline bool Pipe(unique_fd* read, unique_fd* write) {
|
||||
int pipefd[2];
|
||||
if (pipe2(pipefd, O_CLOEXEC) != 0) {
|
||||
return false;
|
||||
}
|
||||
read->reset(pipefd[0]);
|
||||
write->reset(pipefd[1]);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool Socketpair(int domain, int type, int protocol, unique_fd* left, unique_fd* right) {
|
||||
int sockfd[2];
|
||||
if (socketpair(domain, type, protocol, sockfd) != 0) {
|
||||
return false;
|
||||
}
|
||||
left->reset(sockfd[0]);
|
||||
right->reset(sockfd[1]);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool Socketpair(int type, unique_fd* left, unique_fd* right) {
|
||||
return Socketpair(AF_UNIX, type, 0, left, right);
|
||||
}
|
||||
|
||||
#endif // !defined(_WIN32)
|
||||
|
||||
} // namespace base
|
||||
} // namespace android
|
||||
|
||||
|
|
|
@ -66,7 +66,10 @@ cc_library_static {
|
|||
defaults: ["debuggerd_defaults"],
|
||||
srcs: ["handler/debuggerd_handler.cpp"],
|
||||
|
||||
header_libs: ["libdebuggerd_common_headers"],
|
||||
header_libs: [
|
||||
"libbase_headers",
|
||||
"libdebuggerd_common_headers",
|
||||
],
|
||||
|
||||
whole_static_libs: [
|
||||
"libasync_safe",
|
||||
|
|
|
@ -48,10 +48,13 @@
|
|||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <android-base/unique_fd.h>
|
||||
#include <async_safe/log.h>
|
||||
|
||||
#include "dump_type.h"
|
||||
|
||||
using android::base::unique_fd;
|
||||
|
||||
// see man(2) prctl, specifically the section about PR_GET_NAME
|
||||
#define MAX_TASK_NAME_LEN (16)
|
||||
|
||||
|
@ -117,13 +120,12 @@ static void __noreturn __printflike(1, 2) fatal_errno(const char* fmt, ...) {
|
|||
}
|
||||
|
||||
static bool get_main_thread_name(char* buf, size_t len) {
|
||||
int fd = open("/proc/self/comm", O_RDONLY | O_CLOEXEC);
|
||||
unique_fd fd(open("/proc/self/comm", O_RDONLY | O_CLOEXEC));
|
||||
if (fd == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ssize_t rc = read(fd, buf, len);
|
||||
close(fd);
|
||||
if (rc == -1) {
|
||||
return false;
|
||||
} else if (rc == 0) {
|
||||
|
@ -302,8 +304,8 @@ static int debuggerd_dispatch_pseudothread(void* arg) {
|
|||
TEMP_FAILURE_RETRY(dup2(devnull, STDOUT_FILENO));
|
||||
TEMP_FAILURE_RETRY(dup2(devnull, STDERR_FILENO));
|
||||
|
||||
int pipefds[2];
|
||||
if (pipe(pipefds) != 0) {
|
||||
unique_fd pipe_read, pipe_write;
|
||||
if (!android::base::Pipe(&pipe_read, &pipe_write)) {
|
||||
fatal_errno("failed to create pipe");
|
||||
}
|
||||
|
||||
|
@ -313,9 +315,9 @@ static int debuggerd_dispatch_pseudothread(void* arg) {
|
|||
async_safe_format_log(ANDROID_LOG_FATAL, "libc",
|
||||
"failed to fork in debuggerd signal handler: %s", strerror(errno));
|
||||
} else if (forkpid == 0) {
|
||||
TEMP_FAILURE_RETRY(dup2(pipefds[1], STDOUT_FILENO));
|
||||
close(pipefds[0]);
|
||||
close(pipefds[1]);
|
||||
TEMP_FAILURE_RETRY(dup2(pipe_write.get(), STDOUT_FILENO));
|
||||
pipe_write.reset();
|
||||
pipe_read.reset();
|
||||
|
||||
raise_caps();
|
||||
|
||||
|
@ -333,9 +335,9 @@ static int debuggerd_dispatch_pseudothread(void* arg) {
|
|||
|
||||
fatal_errno("exec failed");
|
||||
} else {
|
||||
close(pipefds[1]);
|
||||
pipe_write.reset();
|
||||
char buf[4];
|
||||
ssize_t rc = TEMP_FAILURE_RETRY(read(pipefds[0], &buf, sizeof(buf)));
|
||||
ssize_t rc = TEMP_FAILURE_RETRY(read(pipe_read.get(), &buf, sizeof(buf)));
|
||||
if (rc == -1) {
|
||||
async_safe_format_log(ANDROID_LOG_FATAL, "libc", "read of IPC pipe failed: %s",
|
||||
strerror(errno));
|
||||
|
@ -351,7 +353,7 @@ static int debuggerd_dispatch_pseudothread(void* arg) {
|
|||
thread_info->crash_dump_started = true;
|
||||
}
|
||||
}
|
||||
close(pipefds[0]);
|
||||
pipe_read.reset();
|
||||
|
||||
// Don't leave a zombie child.
|
||||
int status;
|
||||
|
|
|
@ -86,13 +86,3 @@ ssize_t recv_fd(int sockfd, void* _Nonnull data, size_t len, unique_fd* _Nullabl
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Pipe(unique_fd* read, unique_fd* write) {
|
||||
int pipefds[2];
|
||||
if (pipe(pipefds) != 0) {
|
||||
return false;
|
||||
}
|
||||
read->reset(pipefds[0]);
|
||||
write->reset(pipefds[1]);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -42,5 +42,3 @@ ssize_t send_fd(int sockfd, const void* _Nonnull data, size_t len, android::base
|
|||
// plus any errors returned by the underlying recvmsg.
|
||||
ssize_t recv_fd(int sockfd, void* _Nonnull data, size_t len,
|
||||
android::base::unique_fd* _Nullable out_fd);
|
||||
|
||||
bool Pipe(android::base::unique_fd* read, android::base::unique_fd* write);
|
||||
|
|
Loading…
Reference in New Issue