summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Gao2018-05-11 14:55:56 -0500
committerJosh Gao2018-05-22 16:55:36 -0500
commitae9c1dc44a7cc6e6eb217ee3284f53fcf5332140 (patch)
tree0d291b852a4f4b174547bd6d9ad11f4e0c976de1
parente5353021baba3aff13e2dea6601aa8f31c5a42dc (diff)
downloadplatform-system-core-ae9c1dc44a7cc6e6eb217ee3284f53fcf5332140.tar.gz
platform-system-core-ae9c1dc44a7cc6e6eb217ee3284f53fcf5332140.tar.xz
platform-system-core-ae9c1dc44a7cc6e6eb217ee3284f53fcf5332140.zip
adb: move towards using unique_fd.
Bug: http://b/79786774 Test: treehugger Change-Id: Ib5a684bba88e87e1aad9da452dcdd5edd11f18f4
-rw-r--r--adb/adb.cpp16
-rw-r--r--adb/adb_unique_fd.h14
2 files changed, 21 insertions, 9 deletions
diff --git a/adb/adb.cpp b/adb/adb.cpp
index 3bf281c6d..7fc4cc2cd 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -885,9 +885,8 @@ int launch_server(const std::string& socket_spec) {
885 } 885 }
886#else /* !defined(_WIN32) */ 886#else /* !defined(_WIN32) */
887 // set up a pipe so the child can tell us when it is ready. 887 // set up a pipe so the child can tell us when it is ready.
888 // fd[0] will be parent's end, and the child will write on fd[1] 888 unique_fd pipe_read, pipe_write;
889 int fd[2]; 889 if (!Pipe(&pipe_read, &pipe_write)) {
890 if (pipe(fd)) {
891 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno); 890 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
892 return -1; 891 return -1;
893 } 892 }
@@ -899,11 +898,10 @@ int launch_server(const std::string& socket_spec) {
899 898
900 if (pid == 0) { 899 if (pid == 0) {
901 // child side of the fork 900 // child side of the fork
902 901 pipe_read.reset();
903 adb_close(fd[0]);
904 902
905 char reply_fd[30]; 903 char reply_fd[30];
906 snprintf(reply_fd, sizeof(reply_fd), "%d", fd[1]); 904 snprintf(reply_fd, sizeof(reply_fd), "%d", pipe_write.get());
907 // child process 905 // child process
908 int result = execl(path.c_str(), "adb", "-L", socket_spec.c_str(), "fork-server", "server", 906 int result = execl(path.c_str(), "adb", "-L", socket_spec.c_str(), "fork-server", "server",
909 "--reply-fd", reply_fd, NULL); 907 "--reply-fd", reply_fd, NULL);
@@ -913,10 +911,10 @@ int launch_server(const std::string& socket_spec) {
913 // parent side of the fork 911 // parent side of the fork
914 char temp[3] = {}; 912 char temp[3] = {};
915 // wait for the "OK\n" message 913 // wait for the "OK\n" message
916 adb_close(fd[1]); 914 pipe_write.reset();
917 int ret = adb_read(fd[0], temp, 3); 915 int ret = adb_read(pipe_read.get(), temp, 3);
918 int saved_errno = errno; 916 int saved_errno = errno;
919 adb_close(fd[0]); 917 pipe_read.reset();
920 if (ret < 0) { 918 if (ret < 0) {
921 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno); 919 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
922 return -1; 920 return -1;
diff --git a/adb/adb_unique_fd.h b/adb/adb_unique_fd.h
index 34c1bbcfa..9c02cbeec 100644
--- a/adb/adb_unique_fd.h
+++ b/adb/adb_unique_fd.h
@@ -16,6 +16,8 @@
16 16
17#pragma once 17#pragma once
18 18
19#include <unistd.h>
20
19#include <android-base/unique_fd.h> 21#include <android-base/unique_fd.h>
20 22
21// Helper to automatically close an FD when it goes out of scope. 23// Helper to automatically close an FD when it goes out of scope.
@@ -24,3 +26,15 @@ struct AdbCloser {
24}; 26};
25 27
26using unique_fd = android::base::unique_fd_impl<AdbCloser>; 28using unique_fd = android::base::unique_fd_impl<AdbCloser>;
29
30#if !defined(_WIN32)
31inline bool Pipe(unique_fd* read, unique_fd* write) {
32 int pipefd[2];
33 if (pipe(pipefd) != 0) {
34 return false;
35 }
36 read->reset(pipefd[0]);
37 write->reset(pipefd[1]);
38 return true;
39}
40#endif