summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Gao2017-08-25 20:00:18 -0500
committerJosh Gao2017-08-28 16:51:07 -0500
commitfdf832dfd3b2f1be9d3ba831ff85b323a61ee8a3 (patch)
tree41f7132b05979b0187f91f264f45911f78015ea6 /debuggerd
parent75a40988c0e7a35f2663b644989ce012b66f4586 (diff)
downloadplatform-system-core-fdf832dfd3b2f1be9d3ba831ff85b323a61ee8a3.tar.gz
platform-system-core-fdf832dfd3b2f1be9d3ba831ff85b323a61ee8a3.tar.xz
platform-system-core-fdf832dfd3b2f1be9d3ba831ff85b323a61ee8a3.zip
base: add Pipe and Socketpair wrappers.
Also, switch debuggerd_handler over to using android::base::unique_fd. Test: treehugger Change-Id: I97b2ce22f1795ce1c4370f95d00d769846cc54b8
Diffstat (limited to 'debuggerd')
-rw-r--r--debuggerd/Android.bp5
-rw-r--r--debuggerd/handler/debuggerd_handler.cpp22
-rw-r--r--debuggerd/util.cpp10
-rw-r--r--debuggerd/util.h2
4 files changed, 16 insertions, 23 deletions
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 {
66 defaults: ["debuggerd_defaults"], 66 defaults: ["debuggerd_defaults"],
67 srcs: ["handler/debuggerd_handler.cpp"], 67 srcs: ["handler/debuggerd_handler.cpp"],
68 68
69 header_libs: ["libdebuggerd_common_headers"], 69 header_libs: [
70 "libbase_headers",
71 "libdebuggerd_common_headers",
72 ],
70 73
71 whole_static_libs: [ 74 whole_static_libs: [
72 "libasync_safe", 75 "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 @@
48#include <sys/wait.h> 48#include <sys/wait.h>
49#include <unistd.h> 49#include <unistd.h>
50 50
51#include <android-base/unique_fd.h>
51#include <async_safe/log.h> 52#include <async_safe/log.h>
52 53
53#include "dump_type.h" 54#include "dump_type.h"
54 55
56using android::base::unique_fd;
57
55// see man(2) prctl, specifically the section about PR_GET_NAME 58// see man(2) prctl, specifically the section about PR_GET_NAME
56#define MAX_TASK_NAME_LEN (16) 59#define MAX_TASK_NAME_LEN (16)
57 60
@@ -117,13 +120,12 @@ static void __noreturn __printflike(1, 2) fatal_errno(const char* fmt, ...) {
117} 120}
118 121
119static bool get_main_thread_name(char* buf, size_t len) { 122static bool get_main_thread_name(char* buf, size_t len) {
120 int fd = open("/proc/self/comm", O_RDONLY | O_CLOEXEC); 123 unique_fd fd(open("/proc/self/comm", O_RDONLY | O_CLOEXEC));
121 if (fd == -1) { 124 if (fd == -1) {
122 return false; 125 return false;
123 } 126 }
124 127
125 ssize_t rc = read(fd, buf, len); 128 ssize_t rc = read(fd, buf, len);
126 close(fd);
127 if (rc == -1) { 129 if (rc == -1) {
128 return false; 130 return false;
129 } else if (rc == 0) { 131 } else if (rc == 0) {
@@ -302,8 +304,8 @@ static int debuggerd_dispatch_pseudothread(void* arg) {
302 TEMP_FAILURE_RETRY(dup2(devnull, STDOUT_FILENO)); 304 TEMP_FAILURE_RETRY(dup2(devnull, STDOUT_FILENO));
303 TEMP_FAILURE_RETRY(dup2(devnull, STDERR_FILENO)); 305 TEMP_FAILURE_RETRY(dup2(devnull, STDERR_FILENO));
304 306
305 int pipefds[2]; 307 unique_fd pipe_read, pipe_write;
306 if (pipe(pipefds) != 0) { 308 if (!android::base::Pipe(&pipe_read, &pipe_write)) {
307 fatal_errno("failed to create pipe"); 309 fatal_errno("failed to create pipe");
308 } 310 }
309 311
@@ -313,9 +315,9 @@ static int debuggerd_dispatch_pseudothread(void* arg) {
313 async_safe_format_log(ANDROID_LOG_FATAL, "libc", 315 async_safe_format_log(ANDROID_LOG_FATAL, "libc",
314 "failed to fork in debuggerd signal handler: %s", strerror(errno)); 316 "failed to fork in debuggerd signal handler: %s", strerror(errno));
315 } else if (forkpid == 0) { 317 } else if (forkpid == 0) {
316 TEMP_FAILURE_RETRY(dup2(pipefds[1], STDOUT_FILENO)); 318 TEMP_FAILURE_RETRY(dup2(pipe_write.get(), STDOUT_FILENO));
317 close(pipefds[0]); 319 pipe_write.reset();
318 close(pipefds[1]); 320 pipe_read.reset();
319 321
320 raise_caps(); 322 raise_caps();
321 323
@@ -333,9 +335,9 @@ static int debuggerd_dispatch_pseudothread(void* arg) {
333 335
334 fatal_errno("exec failed"); 336 fatal_errno("exec failed");
335 } else { 337 } else {
336 close(pipefds[1]); 338 pipe_write.reset();
337 char buf[4]; 339 char buf[4];
338 ssize_t rc = TEMP_FAILURE_RETRY(read(pipefds[0], &buf, sizeof(buf))); 340 ssize_t rc = TEMP_FAILURE_RETRY(read(pipe_read.get(), &buf, sizeof(buf)));
339 if (rc == -1) { 341 if (rc == -1) {
340 async_safe_format_log(ANDROID_LOG_FATAL, "libc", "read of IPC pipe failed: %s", 342 async_safe_format_log(ANDROID_LOG_FATAL, "libc", "read of IPC pipe failed: %s",
341 strerror(errno)); 343 strerror(errno));
@@ -351,7 +353,7 @@ static int debuggerd_dispatch_pseudothread(void* arg) {
351 thread_info->crash_dump_started = true; 353 thread_info->crash_dump_started = true;
352 } 354 }
353 } 355 }
354 close(pipefds[0]); 356 pipe_read.reset();
355 357
356 // Don't leave a zombie child. 358 // Don't leave a zombie child.
357 int status; 359 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
86 86
87 return result; 87 return result;
88} 88}
89
90bool Pipe(unique_fd* read, unique_fd* write) {
91 int pipefds[2];
92 if (pipe(pipefds) != 0) {
93 return false;
94 }
95 read->reset(pipefds[0]);
96 write->reset(pipefds[1]);
97 return true;
98}
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
42// plus any errors returned by the underlying recvmsg. 42// plus any errors returned by the underlying recvmsg.
43ssize_t recv_fd(int sockfd, void* _Nonnull data, size_t len, 43ssize_t recv_fd(int sockfd, void* _Nonnull data, size_t len,
44 android::base::unique_fd* _Nullable out_fd); 44 android::base::unique_fd* _Nullable out_fd);
45
46bool Pipe(android::base::unique_fd* read, android::base::unique_fd* write);