blob: 7b809da947e1758fc861664816cf45bda8c83327 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
Benoit Goby1c45ee92013-03-29 18:22:36 -070017#include <stddef.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#include <stdlib.h>
19#include <stdio.h>
20#include <unistd.h>
21#include <string.h>
22#include <errno.h>
23
24#include "sysdeps.h"
25
JP Abgrall408fa572011-03-16 15:57:42 -070026#define TRACE_TAG TRACE_SERVICES
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include "adb.h"
28#include "file_sync_service.h"
29
30#if ADB_HOST
31# ifndef HAVE_WINSOCK
32# include <netinet/in.h>
33# include <netdb.h>
JP Abgrall408fa572011-03-16 15:57:42 -070034# include <sys/ioctl.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035# endif
Mike Lockwoodcc1de482009-07-30 16:23:56 -070036#else
Ken Sumralle3aeeb42011-03-07 23:29:42 -080037# include <cutils/android_reboot.h>
Nick Kralevich893a4a42013-05-23 09:54:13 -070038# include <cutils/properties.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039#endif
40
41typedef struct stinfo stinfo;
42
43struct stinfo {
44 void (*func)(int fd, void *cookie);
45 int fd;
46 void *cookie;
47};
48
49
50void *service_bootstrap_func(void *x)
51{
52 stinfo *sti = x;
53 sti->func(sti->fd, sti->cookie);
54 free(sti);
55 return 0;
56}
57
Benoit Goby9470c2f2013-02-20 15:04:53 -080058#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070060void restart_root_service(int fd, void *cookie)
61{
62 char buf[100];
63 char value[PROPERTY_VALUE_MAX];
64
65 if (getuid() == 0) {
66 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
67 writex(fd, buf, strlen(buf));
68 adb_close(fd);
69 } else {
70 property_get("ro.debuggable", value, "");
71 if (strcmp(value, "1") != 0) {
72 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
73 writex(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -070074 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070075 return;
76 }
77
Benoit Goby7941cf82012-03-16 14:44:17 -070078 property_set("service.adb.root", "1");
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070079 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
80 writex(fd, buf, strlen(buf));
81 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070082 }
83}
84
Mike Lockwoodff196702009-08-24 15:58:40 -070085void restart_tcp_service(int fd, void *cookie)
86{
87 char buf[100];
88 char value[PROPERTY_VALUE_MAX];
Elliott Hughesccecf142014-01-16 10:53:11 -080089 int port = (int) (uintptr_t) cookie;
Mike Lockwoodff196702009-08-24 15:58:40 -070090
91 if (port <= 0) {
92 snprintf(buf, sizeof(buf), "invalid port\n");
93 writex(fd, buf, strlen(buf));
94 adb_close(fd);
95 return;
96 }
97
98 snprintf(value, sizeof(value), "%d", port);
99 property_set("service.adb.tcp.port", value);
100 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
101 writex(fd, buf, strlen(buf));
102 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700103}
104
105void restart_usb_service(int fd, void *cookie)
106{
107 char buf[100];
108
109 property_set("service.adb.tcp.port", "0");
110 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
111 writex(fd, buf, strlen(buf));
112 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700113}
114
115void reboot_service(int fd, void *arg)
Mike Lockwoodee156622009-08-04 20:37:51 -0400116{
117 char buf[100];
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700118 char property_val[PROPERTY_VALUE_MAX];
Nick Kralevich02916aa2014-01-14 16:34:56 -0800119 int ret;
Mike Lockwoodee156622009-08-04 20:37:51 -0400120
121 sync();
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500122
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700123 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
124 if (ret >= (int) sizeof(property_val)) {
125 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
126 writex(fd, buf, strlen(buf));
127 goto cleanup;
128 }
129
130 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwoodee156622009-08-04 20:37:51 -0400131 if (ret < 0) {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700132 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Mike Lockwoodee156622009-08-04 20:37:51 -0400133 writex(fd, buf, strlen(buf));
Nick Kralevich91704522013-10-24 08:53:48 -0700134 goto cleanup;
Mike Lockwoodee156622009-08-04 20:37:51 -0400135 }
Nick Kralevich91704522013-10-24 08:53:48 -0700136 // Don't return early. Give the reboot command time to take effect
137 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
138 while(1) { pause(); }
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700139cleanup:
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400140 free(arg);
Mike Lockwoodee156622009-08-04 20:37:51 -0400141 adb_close(fd);
142}
143
David 'Digit' Turner25258692013-03-21 21:07:42 +0100144void reverse_service(int fd, void* arg)
145{
146 const char* command = arg;
147
148 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
149 sendfailmsg(fd, "not a reverse forwarding command");
150 }
151 free(arg);
152 adb_close(fd);
153}
154
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155#endif
156
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157static int create_service_thread(void (*func)(int, void *), void *cookie)
158{
159 stinfo *sti;
160 adb_thread_t t;
161 int s[2];
162
163 if(adb_socketpair(s)) {
164 printf("cannot create service socket pair\n");
165 return -1;
166 }
167
168 sti = malloc(sizeof(stinfo));
169 if(sti == 0) fatal("cannot allocate stinfo");
170 sti->func = func;
171 sti->cookie = cookie;
172 sti->fd = s[1];
173
174 if(adb_thread_create( &t, service_bootstrap_func, sti)){
175 free(sti);
176 adb_close(s[0]);
177 adb_close(s[1]);
178 printf("cannot create service thread\n");
179 return -1;
180 }
181
182 D("service thread started, %d:%d\n",s[0], s[1]);
183 return s[0];
184}
185
JP Abgrall408fa572011-03-16 15:57:42 -0700186#if !ADB_HOST
187static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188{
Joe Onorato91acb142009-09-03 16:30:43 -0700189#ifdef HAVE_WIN32_PROC
JP Abgrall408fa572011-03-16 15:57:42 -0700190 D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
191 fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
192 return -1;
Joe Onorato91acb142009-09-03 16:30:43 -0700193#else /* !HAVE_WIN32_PROC */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 char *devname;
195 int ptm;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
197 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
198 if(ptm < 0){
199 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
200 return -1;
201 }
202 fcntl(ptm, F_SETFD, FD_CLOEXEC);
203
204 if(grantpt(ptm) || unlockpt(ptm) ||
205 ((devname = (char*) ptsname(ptm)) == 0)){
206 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700207 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 return -1;
209 }
210
JP Abgrall408fa572011-03-16 15:57:42 -0700211 *pid = fork();
212 if(*pid < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700214 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 return -1;
216 }
217
JP Abgrall408fa572011-03-16 15:57:42 -0700218 if(*pid == 0){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 int pts;
220
221 setsid();
222
223 pts = unix_open(devname, O_RDWR);
JP Abgrall408fa572011-03-16 15:57:42 -0700224 if(pts < 0) {
225 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
226 exit(-1);
227 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228
229 dup2(pts, 0);
230 dup2(pts, 1);
231 dup2(pts, 2);
232
Benoit Goby95ef8282011-02-01 18:57:41 -0800233 adb_close(pts);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 adb_close(ptm);
235
JP Abgrall408fa572011-03-16 15:57:42 -0700236 // set OOM adjustment to zero
Mike Lockwood249ad572009-05-20 09:14:30 -0400237 char text[64];
JP Abgrall408fa572011-03-16 15:57:42 -0700238 snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid());
Mike Lockwood249ad572009-05-20 09:14:30 -0400239 int fd = adb_open(text, O_WRONLY);
240 if (fd >= 0) {
241 adb_write(fd, "0", 1);
242 adb_close(fd);
243 } else {
244 D("adb: unable to open %s\n", text);
245 }
JP Abgrall408fa572011-03-16 15:57:42 -0700246 execl(cmd, cmd, arg0, arg1, NULL);
247 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
248 cmd, strerror(errno), errno);
249 exit(-1);
250 } else {
251 // Don't set child's OOM adjustment to zero.
252 // Let the child do it itself, as sometimes the parent starts
253 // running before the child has a /proc/pid/oom_adj.
254 // """adb: unable to open /proc/644/oom_adj""" seen in some logs.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 return ptm;
256 }
Joe Onorato91acb142009-09-03 16:30:43 -0700257#endif /* !HAVE_WIN32_PROC */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258}
JP Abgrall408fa572011-03-16 15:57:42 -0700259#endif /* !ABD_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260
261#if ADB_HOST
262#define SHELL_COMMAND "/bin/sh"
263#else
264#define SHELL_COMMAND "/system/bin/sh"
265#endif
266
JP Abgrall408fa572011-03-16 15:57:42 -0700267#if !ADB_HOST
268static void subproc_waiter_service(int fd, void *cookie)
269{
Elliott Hughesccecf142014-01-16 10:53:11 -0800270 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall408fa572011-03-16 15:57:42 -0700271
272 D("entered. fd=%d of pid=%d\n", fd, pid);
273 for (;;) {
274 int status;
275 pid_t p = waitpid(pid, &status, 0);
276 if (p == pid) {
277 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
278 if (WIFSIGNALED(status)) {
279 D("*** Killed by signal %d\n", WTERMSIG(status));
280 break;
281 } else if (!WIFEXITED(status)) {
282 D("*** Didn't exit!!. status %d\n", status);
283 break;
284 } else if (WEXITSTATUS(status) >= 0) {
285 D("*** Exit code %d\n", WEXITSTATUS(status));
286 break;
287 }
288 }
JP Abgrall408fa572011-03-16 15:57:42 -0700289 }
290 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
291 if (SHELL_EXIT_NOTIFY_FD >=0) {
292 int res;
293 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
294 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
295 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
296 }
297}
298
299static int create_subproc_thread(const char *name)
300{
301 stinfo *sti;
302 adb_thread_t t;
303 int ret_fd;
304 pid_t pid;
305 if(name) {
306 ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid);
307 } else {
308 ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid);
309 }
310 D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
311
312 sti = malloc(sizeof(stinfo));
313 if(sti == 0) fatal("cannot allocate stinfo");
314 sti->func = subproc_waiter_service;
Elliott Hughesccecf142014-01-16 10:53:11 -0800315 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall408fa572011-03-16 15:57:42 -0700316 sti->fd = ret_fd;
317
318 if(adb_thread_create( &t, service_bootstrap_func, sti)){
319 free(sti);
320 adb_close(ret_fd);
321 printf("cannot create service thread\n");
322 return -1;
323 }
324
325 D("service thread started, fd=%d pid=%d\n",ret_fd, pid);
326 return ret_fd;
327}
328#endif
329
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330int service_to_fd(const char *name)
331{
332 int ret = -1;
333
334 if(!strncmp(name, "tcp:", 4)) {
335 int port = atoi(name + 4);
336 name = strchr(name + 4, ':');
337 if(name == 0) {
338 ret = socket_loopback_client(port, SOCK_STREAM);
339 if (ret >= 0)
340 disable_tcp_nagle(ret);
341 } else {
342#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344#else
345 return -1;
346#endif
347 }
348#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
349 } else if(!strncmp(name, "local:", 6)) {
350 ret = socket_local_client(name + 6,
351 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
352 } else if(!strncmp(name, "localreserved:", 14)) {
353 ret = socket_local_client(name + 14,
354 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
355 } else if(!strncmp(name, "localabstract:", 14)) {
356 ret = socket_local_client(name + 14,
357 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
358 } else if(!strncmp(name, "localfilesystem:", 16)) {
359 ret = socket_local_client(name + 16,
360 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
361#endif
Benoit Goby9470c2f2013-02-20 15:04:53 -0800362#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 } else if(!strncmp("dev:", name, 4)) {
364 ret = unix_open(name + 4, O_RDWR);
365 } else if(!strncmp(name, "framebuffer:", 12)) {
366 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 } else if (!strncmp(name, "jdwp:", 5)) {
368 ret = create_jdwp_connection_fd(atoi(name+5));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369 } else if(!HOST && !strncmp(name, "shell:", 6)) {
370 if(name[6]) {
JP Abgrall408fa572011-03-16 15:57:42 -0700371 ret = create_subproc_thread(name + 6);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 } else {
JP Abgrall408fa572011-03-16 15:57:42 -0700373 ret = create_subproc_thread(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 } else if(!strncmp(name, "sync:", 5)) {
376 ret = create_service_thread(file_sync_service, NULL);
377 } else if(!strncmp(name, "remount:", 8)) {
378 ret = create_service_thread(remount_service, NULL);
Mike Lockwoodee156622009-08-04 20:37:51 -0400379 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400380 void* arg = strdup(name + 7);
381 if(arg == 0) return -1;
382 ret = create_service_thread(reboot_service, arg);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700383 } else if(!strncmp(name, "root:", 5)) {
384 ret = create_service_thread(restart_root_service, NULL);
Christopher Tated2f54152011-04-21 12:53:28 -0700385 } else if(!strncmp(name, "backup:", 7)) {
386 char* arg = strdup(name+7);
387 if (arg == NULL) return -1;
Christopher Tate702967a2011-05-17 15:52:54 -0700388 ret = backup_service(BACKUP, arg);
389 } else if(!strncmp(name, "restore:", 8)) {
390 ret = backup_service(RESTORE, NULL);
Mike Lockwoodff196702009-08-24 15:58:40 -0700391 } else if(!strncmp(name, "tcpip:", 6)) {
392 int port;
393 if (sscanf(name + 6, "%d", &port) == 0) {
394 port = 0;
395 }
Elliott Hughesccecf142014-01-16 10:53:11 -0800396 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwoodff196702009-08-24 15:58:40 -0700397 } else if(!strncmp(name, "usb:", 4)) {
398 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100399 } else if (!strncmp(name, "reverse:", 8)) {
400 char* cookie = strdup(name + 8);
401 if (cookie == NULL) {
402 ret = -1;
403 } else {
404 ret = create_service_thread(reverse_service, cookie);
405 if (ret < 0) {
406 free(cookie);
407 }
408 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 }
411 if (ret >= 0) {
412 close_on_exec(ret);
413 }
414 return ret;
415}
416
417#if ADB_HOST
418struct state_info {
419 transport_type transport;
420 char* serial;
421 int state;
422};
423
424static void wait_for_state(int fd, void* cookie)
425{
426 struct state_info* sinfo = cookie;
427 char* err = "unknown error";
428
429 D("wait_for_state %d\n", sinfo->state);
430
431 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
432 if(t != 0) {
433 writex(fd, "OKAY", 4);
434 } else {
435 sendfailmsg(fd, err);
436 }
437
438 if (sinfo->serial)
439 free(sinfo->serial);
440 free(sinfo);
441 adb_close(fd);
442 D("wait_for_state is done\n");
443}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700444
445static void connect_device(char* host, char* buffer, int buffer_size)
446{
447 int port, fd;
448 char* portstr = strchr(host, ':');
449 char hostbuf[100];
450 char serial[100];
451 int ret;
452
453 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
454 if (portstr) {
455 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
456 snprintf(buffer, buffer_size, "bad host name %s", host);
457 return;
458 }
459 // zero terminate the host at the point we found the colon
460 hostbuf[portstr - host] = 0;
461 if (sscanf(portstr + 1, "%d", &port) == 0) {
462 snprintf(buffer, buffer_size, "bad port number %s", portstr);
463 return;
464 }
465 } else {
466 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
467 }
468
469 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
470
Ken Liermanaecc6a62013-06-20 09:27:13 -0700471 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700472 if (fd < 0) {
473 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
474 return;
475 }
476
477 D("client: connected on remote on fd %d\n", fd);
478 close_on_exec(fd);
479 disable_tcp_nagle(fd);
480
481 ret = register_socket_transport(fd, serial, port, 0);
482 if (ret < 0) {
483 adb_close(fd);
484 snprintf(buffer, buffer_size, "already connected to %s", serial);
485 } else {
486 snprintf(buffer, buffer_size, "connected to %s", serial);
487 }
488}
489
490void connect_emulator(char* port_spec, char* buffer, int buffer_size)
491{
492 char* port_separator = strchr(port_spec, ',');
493 if (!port_separator) {
494 snprintf(buffer, buffer_size,
495 "unable to parse '%s' as <console port>,<adb port>",
496 port_spec);
497 return;
498 }
499
500 // Zero-terminate console port and make port_separator point to 2nd port.
501 *port_separator++ = 0;
502 int console_port = strtol(port_spec, NULL, 0);
503 int adb_port = strtol(port_separator, NULL, 0);
504 if (!(console_port > 0 && adb_port > 0)) {
505 *(port_separator - 1) = ',';
506 snprintf(buffer, buffer_size,
507 "Invalid port numbers: Expected positive numbers, got '%s'",
508 port_spec);
509 return;
510 }
511
512 /* Check if the emulator is already known.
513 * Note: There's a small but harmless race condition here: An emulator not
514 * present just yet could be registered by another invocation right
515 * after doing this check here. However, local_connect protects
516 * against double-registration too. From here, a better error message
517 * can be produced. In the case of the race condition, the very specific
518 * error message won't be shown, but the data doesn't get corrupted. */
519 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
520 if (known_emulator != NULL) {
521 snprintf(buffer, buffer_size,
522 "Emulator on port %d already registered.", adb_port);
523 return;
524 }
525
526 /* Check if more emulators can be registered. Similar unproblematic
527 * race condition as above. */
528 int candidate_slot = get_available_local_transport_index();
529 if (candidate_slot < 0) {
530 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
531 return;
532 }
533
534 /* Preconditions met, try to connect to the emulator. */
535 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
536 snprintf(buffer, buffer_size,
537 "Connected to emulator on ports %d,%d", console_port, adb_port);
538 } else {
539 snprintf(buffer, buffer_size,
540 "Could not connect to emulator on ports %d,%d",
541 console_port, adb_port);
542 }
543}
544
545static void connect_service(int fd, void* cookie)
546{
547 char buf[4096];
548 char resp[4096];
549 char *host = cookie;
550
551 if (!strncmp(host, "emu:", 4)) {
552 connect_emulator(host + 4, buf, sizeof(buf));
553 } else {
554 connect_device(host, buf, sizeof(buf));
555 }
556
557 // Send response for emulator and device
558 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
559 writex(fd, resp, strlen(resp));
560 adb_close(fd);
561}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562#endif
563
564#if ADB_HOST
565asocket* host_service_to_socket(const char* name, const char *serial)
566{
567 if (!strcmp(name,"track-devices")) {
568 return create_device_tracker();
569 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
570 struct state_info* sinfo = malloc(sizeof(struct state_info));
571
572 if (serial)
573 sinfo->serial = strdup(serial);
574 else
575 sinfo->serial = NULL;
576
577 name += strlen("wait-for-");
578
579 if (!strncmp(name, "local", strlen("local"))) {
580 sinfo->transport = kTransportLocal;
581 sinfo->state = CS_DEVICE;
582 } else if (!strncmp(name, "usb", strlen("usb"))) {
583 sinfo->transport = kTransportUsb;
584 sinfo->state = CS_DEVICE;
585 } else if (!strncmp(name, "any", strlen("any"))) {
586 sinfo->transport = kTransportAny;
587 sinfo->state = CS_DEVICE;
588 } else {
589 free(sinfo);
590 return NULL;
591 }
592
593 int fd = create_service_thread(wait_for_state, sinfo);
594 return create_local_socket(fd);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700595 } else if (!strncmp(name, "connect:", 8)) {
596 const char *host = name + 8;
597 int fd = create_service_thread(connect_service, (void *)host);
598 return create_local_socket(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 }
600 return NULL;
601}
602#endif /* ADB_HOST */