summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Gao2018-05-22 19:01:29 -0500
committerGerrit Code Review2018-05-22 19:01:29 -0500
commit7a223584c5c72f7d16e6abc490125faa2349d98a (patch)
tree96942105aba10132fd8e68e517a153b35acbf747
parent2a1c0dd6df7a2cc9817fe9d7fc8ae10a07c96564 (diff)
parent3b37fa256f00abc6a6414984cabbb3dd9e0a0ad1 (diff)
downloadplatform-system-core-7a223584c5c72f7d16e6abc490125faa2349d98a.tar.gz
platform-system-core-7a223584c5c72f7d16e6abc490125faa2349d98a.tar.xz
platform-system-core-7a223584c5c72f7d16e6abc490125faa2349d98a.zip
Merge changes I465804fd,Ib5a684bb,If5e66570,I8471cc00,I8ba0a70a
* changes: adb: convert fdevent over to unique_fd. adb: move towards using unique_fd. adb: delete FDEVENT_DONTCLOSE. adb: remove fdevent_install, fdevent_remove. adb: fix uninitialized variable in AsyncServiceRef.
-rw-r--r--adb/adb.cpp16
-rw-r--r--adb/adb_listeners.cpp10
-rw-r--r--adb/adb_unique_fd.h14
-rw-r--r--adb/client/transport_mdns.cpp29
-rw-r--r--adb/daemon/auth.cpp18
-rw-r--r--adb/daemon/jdwp_service.cpp3
-rw-r--r--adb/fdevent.cpp62
-rw-r--r--adb/fdevent.h30
-rw-r--r--adb/fdevent_test.cpp30
-rw-r--r--adb/socket.h4
-rw-r--r--adb/sockets.cpp26
-rw-r--r--adb/transport.cpp9
12 files changed, 119 insertions, 132 deletions
diff --git a/adb/adb.cpp b/adb/adb.cpp
index 76ca19a79..f8a54c6bf 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_listeners.cpp b/adb/adb_listeners.cpp
index fecf452c1..ea5a44e46 100644
--- a/adb/adb_listeners.cpp
+++ b/adb/adb_listeners.cpp
@@ -42,7 +42,7 @@ class alistener {
42 alistener(const std::string& _local_name, const std::string& _connect_to); 42 alistener(const std::string& _local_name, const std::string& _connect_to);
43 ~alistener(); 43 ~alistener();
44 44
45 fdevent fde; 45 fdevent* fde = nullptr;
46 int fd = -1; 46 int fd = -1;
47 47
48 std::string local_name; 48 std::string local_name;
@@ -60,7 +60,7 @@ alistener::alistener(const std::string& _local_name, const std::string& _connect
60 60
61alistener::~alistener() { 61alistener::~alistener() {
62 // Closes the corresponding fd. 62 // Closes the corresponding fd.
63 fdevent_remove(&fde); 63 fdevent_destroy(fde);
64 64
65 if (transport) { 65 if (transport) {
66 transport->RemoveDisconnect(&disconnect); 66 transport->RemoveDisconnect(&disconnect);
@@ -222,11 +222,11 @@ InstallStatus install_listener(const std::string& local_name, const char* connec
222 222
223 close_on_exec(listener->fd); 223 close_on_exec(listener->fd);
224 if (listener->connect_to == "*smartsocket*") { 224 if (listener->connect_to == "*smartsocket*") {
225 fdevent_install(&listener->fde, listener->fd, ss_listener_event_func, listener.get()); 225 listener->fde = fdevent_create(listener->fd, ss_listener_event_func, listener.get());
226 } else { 226 } else {
227 fdevent_install(&listener->fde, listener->fd, listener_event_func, listener.get()); 227 listener->fde = fdevent_create(listener->fd, listener_event_func, listener.get());
228 } 228 }
229 fdevent_set(&listener->fde, FDE_READ); 229 fdevent_set(listener->fde, FDE_READ);
230 230
231 listener->transport = transport; 231 listener->transport = transport;
232 232
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
diff --git a/adb/client/transport_mdns.cpp b/adb/client/transport_mdns.cpp
index 3603f0974..283fac554 100644
--- a/adb/client/transport_mdns.cpp
+++ b/adb/client/transport_mdns.cpp
@@ -35,7 +35,7 @@
35#include "sysdeps.h" 35#include "sysdeps.h"
36 36
37static DNSServiceRef service_ref; 37static DNSServiceRef service_ref;
38static fdevent service_ref_fde; 38static fdevent* service_ref_fde;
39 39
40// Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD() 40// Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD()
41// directly so that the socket is put through the appropriate compatibility 41// directly so that the socket is put through the appropriate compatibility
@@ -68,27 +68,26 @@ class AsyncServiceRef {
68 } 68 }
69 69
70 virtual ~AsyncServiceRef() { 70 virtual ~AsyncServiceRef() {
71 if (! initialized_) { 71 if (!initialized_) {
72 return; 72 return;
73 } 73 }
74 74
75 DNSServiceRefDeallocate(sdRef_); 75 DNSServiceRefDeallocate(sdRef_);
76 fdevent_remove(&fde_); 76 fdevent_destroy(fde_);
77 } 77 }
78 78
79 protected: 79 protected:
80 DNSServiceRef sdRef_; 80 DNSServiceRef sdRef_;
81 81
82 void Initialize() { 82 void Initialize() {
83 fdevent_install(&fde_, adb_DNSServiceRefSockFD(sdRef_), 83 fde_ = fdevent_create(adb_DNSServiceRefSockFD(sdRef_), pump_service_ref, &sdRef_);
84 pump_service_ref, &sdRef_); 84 fdevent_set(fde_, FDE_READ);
85 fdevent_set(&fde_, FDE_READ);
86 initialized_ = true; 85 initialized_ = true;
87 } 86 }
88 87
89 private: 88 private:
90 bool initialized_; 89 bool initialized_ = false;
91 fdevent fde_; 90 fdevent* fde_;
92}; 91};
93 92
94class ResolvedService : public AsyncServiceRef { 93class ResolvedService : public AsyncServiceRef {
@@ -252,14 +251,12 @@ static void DNSSD_API register_mdns_transport(DNSServiceRef sdRef,
252 if (errorCode != kDNSServiceErr_NoError) { 251 if (errorCode != kDNSServiceErr_NoError) {
253 D("Got error %d during mDNS browse.", errorCode); 252 D("Got error %d during mDNS browse.", errorCode);
254 DNSServiceRefDeallocate(sdRef); 253 DNSServiceRefDeallocate(sdRef);
255 fdevent_remove(&service_ref_fde); 254 fdevent_destroy(service_ref_fde);
256 return; 255 return;
257 } 256 }
258 257
259 auto discovered = new DiscoveredService(interfaceIndex, serviceName, 258 auto discovered = new DiscoveredService(interfaceIndex, serviceName, regtype, domain);
260 regtype, domain); 259 if (!discovered->Initialized()) {
261
262 if (! discovered->Initialized()) {
263 delete discovered; 260 delete discovered;
264 } 261 }
265} 262}
@@ -274,9 +271,9 @@ void init_mdns_transport_discovery_thread(void) {
274 } 271 }
275 272
276 fdevent_run_on_main_thread([]() { 273 fdevent_run_on_main_thread([]() {
277 fdevent_install(&service_ref_fde, adb_DNSServiceRefSockFD(service_ref), pump_service_ref, 274 service_ref_fde =
278 &service_ref); 275 fdevent_create(adb_DNSServiceRefSockFD(service_ref), pump_service_ref, &service_ref);
279 fdevent_set(&service_ref_fde, FDE_READ); 276 fdevent_set(service_ref_fde, FDE_READ);
280 }); 277 });
281} 278}
282 279
diff --git a/adb/daemon/auth.cpp b/adb/daemon/auth.cpp
index 3fd2b3194..f0c36294b 100644
--- a/adb/daemon/auth.cpp
+++ b/adb/daemon/auth.cpp
@@ -35,8 +35,8 @@
35#include <openssl/rsa.h> 35#include <openssl/rsa.h>
36#include <openssl/sha.h> 36#include <openssl/sha.h>
37 37
38static fdevent listener_fde; 38static fdevent* listener_fde = nullptr;
39static fdevent framework_fde; 39static fdevent* framework_fde = nullptr;
40static int framework_fd = -1; 40static int framework_fd = -1;
41 41
42static void usb_disconnected(void* unused, atransport* t); 42static void usb_disconnected(void* unused, atransport* t);
@@ -106,8 +106,10 @@ static void usb_disconnected(void* unused, atransport* t) {
106 106
107static void framework_disconnected() { 107static void framework_disconnected() {
108 LOG(INFO) << "Framework disconnect"; 108 LOG(INFO) << "Framework disconnect";
109 fdevent_remove(&framework_fde); 109 if (framework_fde) {
110 framework_fd = -1; 110 fdevent_destroy(framework_fde);
111 framework_fd = -1;
112 }
111} 113}
112 114
113static void adbd_auth_event(int fd, unsigned events, void*) { 115static void adbd_auth_event(int fd, unsigned events, void*) {
@@ -168,8 +170,8 @@ static void adbd_auth_listener(int fd, unsigned events, void* data) {
168 } 170 }
169 171
170 framework_fd = s; 172 framework_fd = s;
171 fdevent_install(&framework_fde, framework_fd, adbd_auth_event, nullptr); 173 framework_fde = fdevent_create(framework_fd, adbd_auth_event, nullptr);
172 fdevent_add(&framework_fde, FDE_READ); 174 fdevent_add(framework_fde, FDE_READ);
173 175
174 if (needs_retry) { 176 if (needs_retry) {
175 needs_retry = false; 177 needs_retry = false;
@@ -198,8 +200,8 @@ void adbd_auth_init(void) {
198 return; 200 return;
199 } 201 }
200 202
201 fdevent_install(&listener_fde, fd, adbd_auth_listener, NULL); 203 listener_fde = fdevent_create(fd, adbd_auth_listener, NULL);
202 fdevent_add(&listener_fde, FDE_READ); 204 fdevent_add(listener_fde, FDE_READ);
203} 205}
204 206
205void send_auth_request(atransport* t) { 207void send_auth_request(atransport* t) {
diff --git a/adb/daemon/jdwp_service.cpp b/adb/daemon/jdwp_service.cpp
index 3a3f3996d..367695dc6 100644
--- a/adb/daemon/jdwp_service.cpp
+++ b/adb/daemon/jdwp_service.cpp
@@ -139,8 +139,6 @@ struct JdwpProcess {
139 fatal("could not create fdevent for new JDWP process"); 139 fatal("could not create fdevent for new JDWP process");
140 } 140 }
141 141
142 this->fde->state |= FDE_DONT_CLOSE;
143
144 /* start by waiting for the PID */ 142 /* start by waiting for the PID */
145 fdevent_add(this->fde, FDE_READ); 143 fdevent_add(this->fde, FDE_READ);
146 } 144 }
@@ -148,7 +146,6 @@ struct JdwpProcess {
148 ~JdwpProcess() { 146 ~JdwpProcess() {
149 if (this->socket >= 0) { 147 if (this->socket >= 0) {
150 adb_shutdown(this->socket); 148 adb_shutdown(this->socket);
151 adb_close(this->socket);
152 this->socket = -1; 149 this->socket = -1;
153 } 150 }
154 151
diff --git a/adb/fdevent.cpp b/adb/fdevent.cpp
index cf441cf91..1b7758c57 100644
--- a/adb/fdevent.cpp
+++ b/adb/fdevent.cpp
@@ -57,7 +57,7 @@ struct PollNode {
57 57
58 explicit PollNode(fdevent* fde) : fde(fde) { 58 explicit PollNode(fdevent* fde) : fde(fde) {
59 memset(&pollfd, 0, sizeof(pollfd)); 59 memset(&pollfd, 0, sizeof(pollfd));
60 pollfd.fd = fde->fd; 60 pollfd.fd = fde->fd.get();
61 61
62#if defined(__linux__) 62#if defined(__linux__)
63 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect. 63 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
@@ -111,37 +111,22 @@ static std::string dump_fde(const fdevent* fde) {
111 if (fde->state & FDE_ERROR) { 111 if (fde->state & FDE_ERROR) {
112 state += "E"; 112 state += "E";
113 } 113 }
114 if (fde->state & FDE_DONT_CLOSE) { 114 return android::base::StringPrintf("(fdevent %d %s)", fde->fd.get(), state.c_str());
115 state += "D";
116 }
117 return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
118}
119
120fdevent* fdevent_create(int fd, fd_func func, void* arg) {
121 check_main_thread();
122 fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
123 if(fde == 0) return 0;
124 fdevent_install(fde, fd, func, arg);
125 fde->state |= FDE_CREATED;
126 return fde;
127} 115}
128 116
129void fdevent_destroy(fdevent* fde) { 117void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
130 check_main_thread(); 118 check_main_thread();
131 if(fde == 0) return; 119 CHECK_GE(fd, 0);
132 if(!(fde->state & FDE_CREATED)) { 120 memset(fde, 0, sizeof(fdevent));
133 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
134 }
135 fdevent_remove(fde);
136 free(fde);
137} 121}
138 122
139void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) { 123fdevent* fdevent_create(int fd, fd_func func, void* arg) {
140 check_main_thread(); 124 check_main_thread();
141 CHECK_GE(fd, 0); 125 CHECK_GE(fd, 0);
142 memset(fde, 0, sizeof(fdevent)); 126
127 fdevent* fde = new fdevent();
143 fde->state = FDE_ACTIVE; 128 fde->state = FDE_ACTIVE;
144 fde->fd = fd; 129 fde->fd.reset(fd);
145 fde->func = func; 130 fde->func = func;
146 fde->arg = arg; 131 fde->arg = arg;
147 if (!set_file_block_mode(fd, false)) { 132 if (!set_file_block_mode(fd, false)) {
@@ -150,30 +135,35 @@ void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
150 // to handle it. 135 // to handle it.
151 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd; 136 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
152 } 137 }
153 auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde)); 138 auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde));
154 CHECK(pair.second) << "install existing fd " << fd; 139 CHECK(pair.second) << "install existing fd " << fd;
155 D("fdevent_install %s", dump_fde(fde).c_str()); 140
141 fde->state |= FDE_CREATED;
142 return fde;
156} 143}
157 144
158void fdevent_remove(fdevent* fde) { 145void fdevent_destroy(fdevent* fde) {
159 check_main_thread(); 146 check_main_thread();
160 D("fdevent_remove %s", dump_fde(fde).c_str()); 147 if (fde == 0) return;
148 if (!(fde->state & FDE_CREATED)) {
149 LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
150 }
151
161 if (fde->state & FDE_ACTIVE) { 152 if (fde->state & FDE_ACTIVE) {
162 g_poll_node_map.erase(fde->fd); 153 g_poll_node_map.erase(fde->fd.get());
163 if (fde->state & FDE_PENDING) { 154 if (fde->state & FDE_PENDING) {
164 g_pending_list.remove(fde); 155 g_pending_list.remove(fde);
165 } 156 }
166 if (!(fde->state & FDE_DONT_CLOSE)) { 157 fde->fd.reset();
167 adb_close(fde->fd);
168 fde->fd = -1;
169 }
170 fde->state = 0; 158 fde->state = 0;
171 fde->events = 0; 159 fde->events = 0;
172 } 160 }
161
162 delete fde;
173} 163}
174 164
175static void fdevent_update(fdevent* fde, unsigned events) { 165static void fdevent_update(fdevent* fde, unsigned events) {
176 auto it = g_poll_node_map.find(fde->fd); 166 auto it = g_poll_node_map.find(fde->fd.get());
177 CHECK(it != g_poll_node_map.end()); 167 CHECK(it != g_poll_node_map.end());
178 PollNode& node = it->second; 168 PollNode& node = it->second;
179 if (events & FDE_READ) { 169 if (events & FDE_READ) {
@@ -272,7 +262,7 @@ static void fdevent_process() {
272 auto it = g_poll_node_map.find(pollfd.fd); 262 auto it = g_poll_node_map.find(pollfd.fd);
273 CHECK(it != g_poll_node_map.end()); 263 CHECK(it != g_poll_node_map.end());
274 fdevent* fde = it->second.fde; 264 fdevent* fde = it->second.fde;
275 CHECK_EQ(fde->fd, pollfd.fd); 265 CHECK_EQ(fde->fd.get(), pollfd.fd);
276 fde->events |= events; 266 fde->events |= events;
277 D("%s got events %x", dump_fde(fde).c_str(), events); 267 D("%s got events %x", dump_fde(fde).c_str(), events);
278 fde->state |= FDE_PENDING; 268 fde->state |= FDE_PENDING;
@@ -287,7 +277,7 @@ static void fdevent_call_fdfunc(fdevent* fde) {
287 CHECK(fde->state & FDE_PENDING); 277 CHECK(fde->state & FDE_PENDING);
288 fde->state &= (~FDE_PENDING); 278 fde->state &= (~FDE_PENDING);
289 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str()); 279 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
290 fde->func(fde->fd, events, fde->arg); 280 fde->func(fde->fd.get(), events, fde->arg);
291} 281}
292 282
293static void fdevent_run_flush() EXCLUDES(run_queue_mutex) { 283static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
diff --git a/adb/fdevent.h b/adb/fdevent.h
index 896400ad5..69c40721b 100644
--- a/adb/fdevent.h
+++ b/adb/fdevent.h
@@ -22,28 +22,27 @@
22 22
23#include <functional> 23#include <functional>
24 24
25#include "adb_unique_fd.h"
26
25/* events that may be observed */ 27/* events that may be observed */
26#define FDE_READ 0x0001 28#define FDE_READ 0x0001
27#define FDE_WRITE 0x0002 29#define FDE_WRITE 0x0002
28#define FDE_ERROR 0x0004 30#define FDE_ERROR 0x0004
29 31
30/* features that may be set (via the events set/add/del interface) */
31#define FDE_DONT_CLOSE 0x0080
32
33typedef void (*fd_func)(int fd, unsigned events, void *userdata); 32typedef void (*fd_func)(int fd, unsigned events, void *userdata);
34 33
35struct fdevent { 34struct fdevent {
36 fdevent *next; 35 fdevent* next = nullptr;
37 fdevent *prev; 36 fdevent* prev = nullptr;
38 37
39 int fd; 38 unique_fd fd;
40 int force_eof; 39 int force_eof = 0;
41 40
42 uint16_t state; 41 uint16_t state = 0;
43 uint16_t events; 42 uint16_t events = 0;
44 43
45 fd_func func; 44 fd_func func = nullptr;
46 void *arg; 45 void* arg = nullptr;
47}; 46};
48 47
49/* Allocate and initialize a new fdevent object 48/* Allocate and initialize a new fdevent object
@@ -57,15 +56,6 @@ fdevent *fdevent_create(int fd, fd_func func, void *arg);
57*/ 56*/
58void fdevent_destroy(fdevent *fde); 57void fdevent_destroy(fdevent *fde);
59 58
60/* Initialize an fdevent object that was externally allocated
61*/
62void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
63
64/* Uninitialize an fdevent object that was initialized by
65** fdevent_install()
66*/
67void fdevent_remove(fdevent *item);
68
69/* Change which events should cause notifications 59/* Change which events should cause notifications
70*/ 60*/
71void fdevent_set(fdevent *fde, unsigned events); 61void fdevent_set(fdevent *fde, unsigned events);
diff --git a/adb/fdevent_test.cpp b/adb/fdevent_test.cpp
index 2f0ff1805..0cb24390a 100644
--- a/adb/fdevent_test.cpp
+++ b/adb/fdevent_test.cpp
@@ -31,14 +31,14 @@
31class FdHandler { 31class FdHandler {
32 public: 32 public:
33 FdHandler(int read_fd, int write_fd) : read_fd_(read_fd), write_fd_(write_fd) { 33 FdHandler(int read_fd, int write_fd) : read_fd_(read_fd), write_fd_(write_fd) {
34 fdevent_install(&read_fde_, read_fd_, FdEventCallback, this); 34 read_fde_ = fdevent_create(read_fd_, FdEventCallback, this);
35 fdevent_add(&read_fde_, FDE_READ); 35 fdevent_add(read_fde_, FDE_READ);
36 fdevent_install(&write_fde_, write_fd_, FdEventCallback, this); 36 write_fde_ = fdevent_create(write_fd_, FdEventCallback, this);
37 } 37 }
38 38
39 ~FdHandler() { 39 ~FdHandler() {
40 fdevent_remove(&read_fde_); 40 fdevent_destroy(read_fde_);
41 fdevent_remove(&write_fde_); 41 fdevent_destroy(write_fde_);
42 } 42 }
43 43
44 private: 44 private:
@@ -50,7 +50,7 @@ class FdHandler {
50 char c; 50 char c;
51 ASSERT_EQ(1, adb_read(fd, &c, 1)); 51 ASSERT_EQ(1, adb_read(fd, &c, 1));
52 handler->queue_.push(c); 52 handler->queue_.push(c);
53 fdevent_add(&handler->write_fde_, FDE_WRITE); 53 fdevent_add(handler->write_fde_, FDE_WRITE);
54 } 54 }
55 if (events & FDE_WRITE) { 55 if (events & FDE_WRITE) {
56 ASSERT_EQ(fd, handler->write_fd_); 56 ASSERT_EQ(fd, handler->write_fd_);
@@ -59,7 +59,7 @@ class FdHandler {
59 handler->queue_.pop(); 59 handler->queue_.pop();
60 ASSERT_EQ(1, adb_write(fd, &c, 1)); 60 ASSERT_EQ(1, adb_write(fd, &c, 1));
61 if (handler->queue_.empty()) { 61 if (handler->queue_.empty()) {
62 fdevent_del(&handler->write_fde_, FDE_WRITE); 62 fdevent_del(handler->write_fde_, FDE_WRITE);
63 } 63 }
64 } 64 }
65 } 65 }
@@ -67,8 +67,8 @@ class FdHandler {
67 private: 67 private:
68 const int read_fd_; 68 const int read_fd_;
69 const int write_fd_; 69 const int write_fd_;
70 fdevent read_fde_; 70 fdevent* read_fde_;
71 fdevent write_fde_; 71 fdevent* write_fde_;
72 std::queue<char> queue_; 72 std::queue<char> queue_;
73}; 73};
74 74
@@ -137,7 +137,7 @@ TEST_F(FdeventTest, smoke) {
137} 137}
138 138
139struct InvalidFdArg { 139struct InvalidFdArg {
140 fdevent fde; 140 fdevent* fde;
141 unsigned expected_events; 141 unsigned expected_events;
142 size_t* happened_event_count; 142 size_t* happened_event_count;
143}; 143};
@@ -145,7 +145,7 @@ struct InvalidFdArg {
145static void InvalidFdEventCallback(int, unsigned events, void* userdata) { 145static void InvalidFdEventCallback(int, unsigned events, void* userdata) {
146 InvalidFdArg* arg = reinterpret_cast<InvalidFdArg*>(userdata); 146 InvalidFdArg* arg = reinterpret_cast<InvalidFdArg*>(userdata);
147 ASSERT_EQ(arg->expected_events, events); 147 ASSERT_EQ(arg->expected_events, events);
148 fdevent_remove(&arg->fde); 148 fdevent_destroy(arg->fde);
149 if (++*(arg->happened_event_count) == 2) { 149 if (++*(arg->happened_event_count) == 2) {
150 fdevent_terminate_loop(); 150 fdevent_terminate_loop();
151 } 151 }
@@ -157,15 +157,15 @@ static void InvalidFdThreadFunc() {
157 InvalidFdArg read_arg; 157 InvalidFdArg read_arg;
158 read_arg.expected_events = FDE_READ | FDE_ERROR; 158 read_arg.expected_events = FDE_READ | FDE_ERROR;
159 read_arg.happened_event_count = &happened_event_count; 159 read_arg.happened_event_count = &happened_event_count;
160 fdevent_install(&read_arg.fde, INVALID_READ_FD, InvalidFdEventCallback, &read_arg); 160 read_arg.fde = fdevent_create(INVALID_READ_FD, InvalidFdEventCallback, &read_arg);
161 fdevent_add(&read_arg.fde, FDE_READ); 161 fdevent_add(read_arg.fde, FDE_READ);
162 162
163 const int INVALID_WRITE_FD = std::numeric_limits<int>::max(); 163 const int INVALID_WRITE_FD = std::numeric_limits<int>::max();
164 InvalidFdArg write_arg; 164 InvalidFdArg write_arg;
165 write_arg.expected_events = FDE_READ | FDE_ERROR; 165 write_arg.expected_events = FDE_READ | FDE_ERROR;
166 write_arg.happened_event_count = &happened_event_count; 166 write_arg.happened_event_count = &happened_event_count;
167 fdevent_install(&write_arg.fde, INVALID_WRITE_FD, InvalidFdEventCallback, &write_arg); 167 write_arg.fde = fdevent_create(INVALID_WRITE_FD, InvalidFdEventCallback, &write_arg);
168 fdevent_add(&write_arg.fde, FDE_WRITE); 168 fdevent_add(write_arg.fde, FDE_WRITE);
169 fdevent_loop(); 169 fdevent_loop();
170} 170}
171 171
diff --git a/adb/socket.h b/adb/socket.h
index e0fd87f19..27e5b0534 100644
--- a/adb/socket.h
+++ b/adb/socket.h
@@ -58,8 +58,8 @@ struct asocket {
58 * us to our fd event system. For remote asockets 58 * us to our fd event system. For remote asockets
59 * these fields are not used. 59 * these fields are not used.
60 */ 60 */
61 fdevent fde = {}; 61 fdevent* fde = nullptr;
62 int fd = 0; 62 int fd = -1;
63 63
64 // queue of data waiting to be written 64 // queue of data waiting to be written
65 std::deque<Range> packet_queue; 65 std::deque<Range> packet_queue;
diff --git a/adb/sockets.cpp b/adb/sockets.cpp
index e567ff4f9..9a6dcbec5 100644
--- a/adb/sockets.cpp
+++ b/adb/sockets.cpp
@@ -121,10 +121,10 @@ static SocketFlushResult local_socket_flush_incoming(asocket* s) {
121 s->packet_queue.pop_front(); 121 s->packet_queue.pop_front();
122 } else if (rc > 0) { 122 } else if (rc > 0) {
123 r.drop_front(rc); 123 r.drop_front(rc);
124 fdevent_add(&s->fde, FDE_WRITE); 124 fdevent_add(s->fde, FDE_WRITE);
125 return SocketFlushResult::TryAgain; 125 return SocketFlushResult::TryAgain;
126 } else if (rc == -1 && errno == EAGAIN) { 126 } else if (rc == -1 && errno == EAGAIN) {
127 fdevent_add(&s->fde, FDE_WRITE); 127 fdevent_add(s->fde, FDE_WRITE);
128 return SocketFlushResult::TryAgain; 128 return SocketFlushResult::TryAgain;
129 } else { 129 } else {
130 // We failed to write, but it's possible that we can still read from the socket. 130 // We failed to write, but it's possible that we can still read from the socket.
@@ -140,7 +140,7 @@ static SocketFlushResult local_socket_flush_incoming(asocket* s) {
140 return SocketFlushResult::Destroyed; 140 return SocketFlushResult::Destroyed;
141 } 141 }
142 142
143 fdevent_del(&s->fde, FDE_WRITE); 143 fdevent_del(s->fde, FDE_WRITE);
144 return SocketFlushResult::Completed; 144 return SocketFlushResult::Completed;
145} 145}
146 146
@@ -173,7 +173,7 @@ static bool local_socket_flush_outgoing(asocket* s) {
173 break; 173 break;
174 } 174 }
175 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof, 175 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof,
176 s->fde.force_eof); 176 s->fde->force_eof);
177 177
178 if (avail != max_payload && s->peer) { 178 if (avail != max_payload && s->peer) {
179 data.resize(max_payload - avail); 179 data.resize(max_payload - avail);
@@ -200,13 +200,13 @@ static bool local_socket_flush_outgoing(asocket* s) {
200 ** we disable notification of READs. They'll 200 ** we disable notification of READs. They'll
201 ** be enabled again when we get a call to ready() 201 ** be enabled again when we get a call to ready()
202 */ 202 */
203 fdevent_del(&s->fde, FDE_READ); 203 fdevent_del(s->fde, FDE_READ);
204 } 204 }
205 } 205 }
206 206
207 // Don't allow a forced eof if data is still there. 207 // Don't allow a forced eof if data is still there.
208 if ((s->fde.force_eof && !r) || is_eof) { 208 if ((s->fde->force_eof && !r) || is_eof) {
209 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde.force_eof); 209 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde->force_eof);
210 s->close(s); 210 s->close(s);
211 return false; 211 return false;
212 } 212 }
@@ -236,19 +236,19 @@ static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
236static void local_socket_ready(asocket* s) { 236static void local_socket_ready(asocket* s) {
237 /* far side is ready for data, pay attention to 237 /* far side is ready for data, pay attention to
238 readable events */ 238 readable events */
239 fdevent_add(&s->fde, FDE_READ); 239 fdevent_add(s->fde, FDE_READ);
240} 240}
241 241
242// be sure to hold the socket list lock when calling this 242// be sure to hold the socket list lock when calling this
243static void local_socket_destroy(asocket* s) { 243static void local_socket_destroy(asocket* s) {
244 int exit_on_close = s->exit_on_close; 244 int exit_on_close = s->exit_on_close;
245 245
246 D("LS(%d): destroying fde.fd=%d", s->id, s->fde.fd); 246 D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
247 247
248 /* IMPORTANT: the remove closes the fd 248 /* IMPORTANT: the remove closes the fd
249 ** that belongs to this socket 249 ** that belongs to this socket
250 */ 250 */
251 fdevent_remove(&s->fde); 251 fdevent_destroy(s->fde);
252 252
253 remove_socket(s); 253 remove_socket(s);
254 delete s; 254 delete s;
@@ -290,11 +290,11 @@ static void local_socket_close(asocket* s) {
290 */ 290 */
291 D("LS(%d): closing", s->id); 291 D("LS(%d): closing", s->id);
292 s->closing = 1; 292 s->closing = 1;
293 fdevent_del(&s->fde, FDE_READ); 293 fdevent_del(s->fde, FDE_READ);
294 remove_socket(s); 294 remove_socket(s);
295 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd); 295 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
296 local_socket_closing_list.push_back(s); 296 local_socket_closing_list.push_back(s);
297 CHECK_EQ(FDE_WRITE, s->fde.state & FDE_WRITE); 297 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
298} 298}
299 299
300static void local_socket_event_func(int fd, unsigned ev, void* _s) { 300static void local_socket_event_func(int fd, unsigned ev, void* _s) {
@@ -343,7 +343,7 @@ asocket* create_local_socket(int fd) {
343 s->close = local_socket_close; 343 s->close = local_socket_close;
344 install_local_socket(s); 344 install_local_socket(s);
345 345
346 fdevent_install(&s->fde, fd, local_socket_event_func, s); 346 s->fde = fdevent_create(fd, local_socket_event_func, s);
347 D("LS(%d): created (fd=%d)", s->id, s->fd); 347 D("LS(%d): created (fd=%d)", s->id, s->fd);
348 return s; 348 return s;
349} 349}
diff --git a/adb/transport.cpp b/adb/transport.cpp
index beec13a69..7db9bf218 100644
--- a/adb/transport.cpp
+++ b/adb/transport.cpp
@@ -427,7 +427,7 @@ void kick_transport(atransport* t) {
427 427
428static int transport_registration_send = -1; 428static int transport_registration_send = -1;
429static int transport_registration_recv = -1; 429static int transport_registration_recv = -1;
430static fdevent transport_registration_fde; 430static fdevent* transport_registration_fde;
431 431
432#if ADB_HOST 432#if ADB_HOST
433 433
@@ -698,10 +698,9 @@ void init_transport_registration(void) {
698 transport_registration_send = s[0]; 698 transport_registration_send = s[0];
699 transport_registration_recv = s[1]; 699 transport_registration_recv = s[1];
700 700
701 fdevent_install(&transport_registration_fde, transport_registration_recv, 701 transport_registration_fde =
702 transport_registration_func, 0); 702 fdevent_create(transport_registration_recv, transport_registration_func, 0);
703 703 fdevent_set(transport_registration_fde, FDE_READ);
704 fdevent_set(&transport_registration_fde, FDE_READ);
705} 704}
706 705
707void kick_all_transports() { 706void kick_all_transports() {