Merge "base: add Pipe and Socketpair wrappers."
diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h
index 6cfcfcd..fb3dded 100644
--- a/base/include/android-base/unique_fd.h
+++ b/base/include/android-base/unique_fd.h
@@ -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 @@
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
diff --git a/debuggerd/Android.bp b/debuggerd/Android.bp
index 7d17cd9..2b5f4f6 100644
--- a/debuggerd/Android.bp
+++ b/debuggerd/Android.bp
@@ -66,7 +66,10 @@
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 1275229..d41dc67 100644
--- a/debuggerd/handler/debuggerd_handler.cpp
+++ b/debuggerd/handler/debuggerd_handler.cpp
@@ -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 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 @@
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 @@
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 @@
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 @@
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 c6a997b..0bb07ac 100644
--- a/debuggerd/util.cpp
+++ b/debuggerd/util.cpp
@@ -86,13 +86,3 @@
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 6051714..171e07a 100644
--- a/debuggerd/util.h
+++ b/debuggerd/util.h
@@ -42,5 +42,3 @@
// 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);