From fdf832dfd3b2f1be9d3ba831ff85b323a61ee8a3 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Fri, 25 Aug 2017 18:00:18 -0700 Subject: base: add Pipe and Socketpair wrappers. Also, switch debuggerd_handler over to using android::base::unique_fd. Test: treehugger Change-Id: I97b2ce22f1795ce1c4370f95d00d769846cc54b8 --- debuggerd/Android.bp | 5 ++++- debuggerd/handler/debuggerd_handler.cpp | 22 ++++++++++++---------- debuggerd/util.cpp | 10 ---------- debuggerd/util.h | 2 -- 4 files changed, 16 insertions(+), 23 deletions(-) (limited to 'debuggerd') diff --git a/debuggerd/Android.bp b/debuggerd/Android.bp index 7d17cd931..2b5f4f646 100644 --- a/debuggerd/Android.bp +++ b/debuggerd/Android.bp @@ -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", diff --git a/debuggerd/handler/debuggerd_handler.cpp b/debuggerd/handler/debuggerd_handler.cpp index 127522926..d41dc67be 100644 --- a/debuggerd/handler/debuggerd_handler.cpp +++ b/debuggerd/handler/debuggerd_handler.cpp @@ -48,10 +48,13 @@ #include #include +#include #include #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; diff --git a/debuggerd/util.cpp b/debuggerd/util.cpp index c6a997bef..0bb07ac1b 100644 --- a/debuggerd/util.cpp +++ b/debuggerd/util.cpp @@ -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; -} diff --git a/debuggerd/util.h b/debuggerd/util.h index 60517146b..171e07a49 100644 --- a/debuggerd/util.h +++ b/debuggerd/util.h @@ -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); -- cgit v1.2.3