transport_local.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include "sysdeps.h"
  21. #include <sys/types.h>
  22. #define TRACE_TAG TRACE_TRANSPORT
  23. #include "adb.h"
  24. #ifdef HAVE_BIG_ENDIAN
  25. #define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
  26. static inline void fix_endians(apacket *p)
  27. {
  28. p->msg.command = H4(p->msg.command);
  29. p->msg.arg0 = H4(p->msg.arg0);
  30. p->msg.arg1 = H4(p->msg.arg1);
  31. p->msg.data_length = H4(p->msg.data_length);
  32. p->msg.data_check = H4(p->msg.data_check);
  33. p->msg.magic = H4(p->msg.magic);
  34. }
  35. #else
  36. #define fix_endians(p) do {} while (0)
  37. #endif
  38. #if ADB_HOST
  39. /* we keep a list of opened transports. The atransport struct knows to which
  40. * local transport it is connected. The list is used to detect when we're
  41. * trying to connect twice to a given local transport.
  42. */
  43. #define ADB_LOCAL_TRANSPORT_MAX 16
  44. ADB_MUTEX_DEFINE( local_transports_lock );
  45. static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
  46. #endif /* ADB_HOST */
  47. static int remote_read(apacket *p, atransport *t)
  48. {
  49. if(readx(t->sfd, &p->msg, sizeof(amessage))){
  50. D("remote local: read terminated (message)\n");
  51. return -1;
  52. }
  53. fix_endians(p);
  54. #if 0 && defined HAVE_BIG_ENDIAN
  55. D("read remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
  56. p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
  57. #endif
  58. if(check_header(p)) {
  59. D("bad header: terminated (data)\n");
  60. return -1;
  61. }
  62. if(readx(t->sfd, p->data, p->msg.data_length)){
  63. D("remote local: terminated (data)\n");
  64. return -1;
  65. }
  66. if(check_data(p)) {
  67. D("bad data: terminated (data)\n");
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. static int remote_write(apacket *p, atransport *t)
  73. {
  74. int length = p->msg.data_length;
  75. fix_endians(p);
  76. #if 0 && defined HAVE_BIG_ENDIAN
  77. D("write remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
  78. p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
  79. #endif
  80. if(writex(t->sfd, &p->msg, sizeof(amessage) + length)) {
  81. D("remote local: write terminated\n");
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. int local_connect(int port) {
  87. return local_connect_arbitrary_ports(port-1, port);
  88. }
  89. int local_connect_arbitrary_ports(int console_port, int adb_port)
  90. {
  91. char buf[64];
  92. int fd = -1;
  93. #if ADB_HOST
  94. const char *host = getenv("ADBHOST");
  95. if (host) {
  96. fd = socket_network_client(host, adb_port, SOCK_STREAM);
  97. }
  98. #endif
  99. if (fd < 0) {
  100. fd = socket_loopback_client(adb_port, SOCK_STREAM);
  101. }
  102. if (fd >= 0) {
  103. D("client: connected on remote on fd %d\n", fd);
  104. close_on_exec(fd);
  105. disable_tcp_nagle(fd);
  106. snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, console_port);
  107. register_socket_transport(fd, buf, adb_port, 1);
  108. return 0;
  109. }
  110. return -1;
  111. }
  112. static void *client_socket_thread(void *x)
  113. {
  114. #if ADB_HOST
  115. int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
  116. int count = ADB_LOCAL_TRANSPORT_MAX;
  117. D("transport: client_socket_thread() starting\n");
  118. /* try to connect to any number of running emulator instances */
  119. /* this is only done when ADB starts up. later, each new emulator */
  120. /* will send a message to ADB to indicate that is is starting up */
  121. for ( ; count > 0; count--, port += 2 ) {
  122. (void) local_connect(port);
  123. }
  124. #endif
  125. return 0;
  126. }
  127. static void *server_socket_thread(void * arg)
  128. {
  129. int serverfd, fd;
  130. struct sockaddr addr;
  131. socklen_t alen;
  132. int port = (int)arg;
  133. D("transport: server_socket_thread() starting\n");
  134. serverfd = -1;
  135. for(;;) {
  136. if(serverfd == -1) {
  137. serverfd = socket_inaddr_any_server(port, SOCK_STREAM);
  138. if(serverfd < 0) {
  139. D("server: cannot bind socket yet\n");
  140. adb_sleep_ms(1000);
  141. continue;
  142. }
  143. close_on_exec(serverfd);
  144. }
  145. alen = sizeof(addr);
  146. D("server: trying to get new connection from %d\n", port);
  147. fd = adb_socket_accept(serverfd, &addr, &alen);
  148. if(fd >= 0) {
  149. D("server: new connection on fd %d\n", fd);
  150. close_on_exec(fd);
  151. disable_tcp_nagle(fd);
  152. register_socket_transport(fd, "host", port, 1);
  153. }
  154. }
  155. D("transport: server_socket_thread() exiting\n");
  156. return 0;
  157. }
  158. /* This is relevant only for ADB daemon running inside the emulator. */
  159. #if !ADB_HOST
  160. /*
  161. * Redefine open and write for qemu_pipe.h that contains inlined references
  162. * to those routines. We will redifine them back after qemu_pipe.h inclusion.
  163. */
  164. #undef open
  165. #undef write
  166. #define open adb_open
  167. #define write adb_write
  168. #include <qemu/qemu_pipe.h>
  169. #undef open
  170. #undef write
  171. #define open ___xxx_open
  172. #define write ___xxx_write
  173. /* A worker thread that monitors host connections, and registers a transport for
  174. * every new host connection. This thread replaces server_socket_thread on
  175. * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
  176. * pipe to communicate with adbd daemon inside the guest. This is done in order
  177. * to provide more robust communication channel between ADB host and guest. The
  178. * main issue with server_socket_thread approach is that it runs on top of TCP,
  179. * and thus is sensitive to network disruptions. For instance, the
  180. * ConnectionManager may decide to reset all network connections, in which case
  181. * the connection between ADB host and guest will be lost. To make ADB traffic
  182. * independent from the network, we use here 'adb' QEMUD service to transfer data
  183. * between the host, and the guest. See external/qemu/android/adb-*.* that
  184. * implements the emulator's side of the protocol. Another advantage of using
  185. * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
  186. * anymore on network being set up.
  187. * The guest side of the protocol contains the following phases:
  188. * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
  189. * is opened, and it becomes clear whether or not emulator supports that
  190. * protocol.
  191. * - Wait for the ADB host to create connection with the guest. This is done by
  192. * sending an 'accept' request to the adb QEMUD service, and waiting on
  193. * response.
  194. * - When new ADB host connection is accepted, the connection with adb QEMUD
  195. * service is registered as the transport, and a 'start' request is sent to the
  196. * adb QEMUD service, indicating that the guest is ready to receive messages.
  197. * Note that the guest will ignore messages sent down from the emulator before
  198. * the transport registration is completed. That's why we need to send the
  199. * 'start' request after the transport is registered.
  200. */
  201. static void *qemu_socket_thread(void * arg)
  202. {
  203. /* 'accept' request to the adb QEMUD service. */
  204. static const char _accept_req[] = "accept";
  205. /* 'start' request to the adb QEMUD service. */
  206. static const char _start_req[] = "start";
  207. /* 'ok' reply from the adb QEMUD service. */
  208. static const char _ok_resp[] = "ok";
  209. const int port = (int)arg;
  210. int res, fd;
  211. char tmp[256];
  212. char con_name[32];
  213. D("transport: qemu_socket_thread() starting\n");
  214. /* adb QEMUD service connection request. */
  215. snprintf(con_name, sizeof(con_name), "qemud:adb:%d", port);
  216. /* Connect to the adb QEMUD service. */
  217. fd = qemu_pipe_open(con_name);
  218. if (fd < 0) {
  219. /* This could be an older version of the emulator, that doesn't
  220. * implement adb QEMUD service. Fall back to the old TCP way. */
  221. adb_thread_t thr;
  222. D("adb service is not available. Falling back to TCP socket.\n");
  223. adb_thread_create(&thr, server_socket_thread, arg);
  224. return 0;
  225. }
  226. for(;;) {
  227. /*
  228. * Wait till the host creates a new connection.
  229. */
  230. /* Send the 'accept' request. */
  231. res = adb_write(fd, _accept_req, strlen(_accept_req));
  232. if ((size_t)res == strlen(_accept_req)) {
  233. /* Wait for the response. In the response we expect 'ok' on success,
  234. * or 'ko' on failure. */
  235. res = adb_read(fd, tmp, sizeof(tmp));
  236. if (res != 2 || memcmp(tmp, _ok_resp, 2)) {
  237. D("Accepting ADB host connection has failed.\n");
  238. adb_close(fd);
  239. } else {
  240. /* Host is connected. Register the transport, and start the
  241. * exchange. */
  242. register_socket_transport(fd, "host", port, 1);
  243. adb_write(fd, _start_req, strlen(_start_req));
  244. }
  245. /* Prepare for accepting of the next ADB host connection. */
  246. fd = qemu_pipe_open(con_name);
  247. if (fd < 0) {
  248. D("adb service become unavailable.\n");
  249. return 0;
  250. }
  251. } else {
  252. D("Unable to send the '%s' request to ADB service.\n", _accept_req);
  253. return 0;
  254. }
  255. }
  256. D("transport: qemu_socket_thread() exiting\n");
  257. return 0;
  258. }
  259. #endif // !ADB_HOST
  260. void local_init(int port)
  261. {
  262. adb_thread_t thr;
  263. void* (*func)(void *);
  264. if(HOST) {
  265. func = client_socket_thread;
  266. } else {
  267. #if ADB_HOST
  268. func = server_socket_thread;
  269. #else
  270. /* For the adbd daemon in the system image we need to distinguish
  271. * between the device, and the emulator. */
  272. char is_qemu[PROPERTY_VALUE_MAX];
  273. property_get("ro.kernel.qemu", is_qemu, "");
  274. if (!strcmp(is_qemu, "1")) {
  275. /* Running inside the emulator: use QEMUD pipe as the transport. */
  276. func = qemu_socket_thread;
  277. } else {
  278. /* Running inside the device: use TCP socket as the transport. */
  279. func = server_socket_thread;
  280. }
  281. #endif // !ADB_HOST
  282. }
  283. D("transport: local %s init\n", HOST ? "client" : "server");
  284. if(adb_thread_create(&thr, func, (void *)port)) {
  285. fatal_errno("cannot create local socket %s thread",
  286. HOST ? "client" : "server");
  287. }
  288. }
  289. static void remote_kick(atransport *t)
  290. {
  291. int fd = t->sfd;
  292. t->sfd = -1;
  293. adb_shutdown(fd);
  294. adb_close(fd);
  295. #if ADB_HOST
  296. if(HOST) {
  297. int nn;
  298. adb_mutex_lock( &local_transports_lock );
  299. for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
  300. if (local_transports[nn] == t) {
  301. local_transports[nn] = NULL;
  302. break;
  303. }
  304. }
  305. adb_mutex_unlock( &local_transports_lock );
  306. }
  307. #endif
  308. }
  309. static void remote_close(atransport *t)
  310. {
  311. adb_close(t->fd);
  312. }
  313. #if ADB_HOST
  314. /* Only call this function if you already hold local_transports_lock. */
  315. atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
  316. {
  317. int i;
  318. for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
  319. if (local_transports[i] && local_transports[i]->adb_port == adb_port) {
  320. return local_transports[i];
  321. }
  322. }
  323. return NULL;
  324. }
  325. atransport* find_emulator_transport_by_adb_port(int adb_port)
  326. {
  327. adb_mutex_lock( &local_transports_lock );
  328. atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
  329. adb_mutex_unlock( &local_transports_lock );
  330. return result;
  331. }
  332. /* Only call this function if you already hold local_transports_lock. */
  333. int get_available_local_transport_index_locked()
  334. {
  335. int i;
  336. for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
  337. if (local_transports[i] == NULL) {
  338. return i;
  339. }
  340. }
  341. return -1;
  342. }
  343. int get_available_local_transport_index()
  344. {
  345. adb_mutex_lock( &local_transports_lock );
  346. int result = get_available_local_transport_index_locked();
  347. adb_mutex_unlock( &local_transports_lock );
  348. return result;
  349. }
  350. #endif
  351. int init_socket_transport(atransport *t, int s, int adb_port, int local)
  352. {
  353. int fail = 0;
  354. t->kick = remote_kick;
  355. t->close = remote_close;
  356. t->read_from_remote = remote_read;
  357. t->write_to_remote = remote_write;
  358. t->sfd = s;
  359. t->sync_token = 1;
  360. t->connection_state = CS_OFFLINE;
  361. t->type = kTransportLocal;
  362. t->adb_port = 0;
  363. #if ADB_HOST
  364. if (HOST && local) {
  365. adb_mutex_lock( &local_transports_lock );
  366. {
  367. t->adb_port = adb_port;
  368. atransport* existing_transport =
  369. find_emulator_transport_by_adb_port_locked(adb_port);
  370. int index = get_available_local_transport_index_locked();
  371. if (existing_transport != NULL) {
  372. D("local transport for port %d already registered (%p)?\n",
  373. adb_port, existing_transport);
  374. fail = -1;
  375. } else if (index < 0) {
  376. // Too many emulators.
  377. D("cannot register more emulators. Maximum is %d\n",
  378. ADB_LOCAL_TRANSPORT_MAX);
  379. fail = -1;
  380. } else {
  381. local_transports[index] = t;
  382. }
  383. }
  384. adb_mutex_unlock( &local_transports_lock );
  385. }
  386. #endif
  387. return fail;
  388. }