blob: f0d587817e40fbc91e476757965937cf92139738 [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];
89 int port = (int)cookie;
90
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];
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500119 int pid, ret;
Mike Lockwoodee156622009-08-04 20:37:51 -0400120
121 sync();
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500122
123 /* Attempt to unmount the SD card first.
124 * No need to bother checking for errors.
125 */
126 pid = fork();
127 if (pid == 0) {
128 /* ask vdc to unmount it */
129 execl("/system/bin/vdc", "/system/bin/vdc", "volume", "unmount",
130 getenv("EXTERNAL_STORAGE"), "force", NULL);
131 } else if (pid > 0) {
132 /* wait until vdc succeeds or fails */
133 waitpid(pid, &ret, 0);
134 }
135
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700136 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
137 if (ret >= (int) sizeof(property_val)) {
138 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
139 writex(fd, buf, strlen(buf));
140 goto cleanup;
141 }
142
143 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwoodee156622009-08-04 20:37:51 -0400144 if (ret < 0) {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700145 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Mike Lockwoodee156622009-08-04 20:37:51 -0400146 writex(fd, buf, strlen(buf));
147 }
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700148cleanup:
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400149 free(arg);
Mike Lockwoodee156622009-08-04 20:37:51 -0400150 adb_close(fd);
151}
152
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153#endif
154
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155static int create_service_thread(void (*func)(int, void *), void *cookie)
156{
157 stinfo *sti;
158 adb_thread_t t;
159 int s[2];
160
161 if(adb_socketpair(s)) {
162 printf("cannot create service socket pair\n");
163 return -1;
164 }
165
166 sti = malloc(sizeof(stinfo));
167 if(sti == 0) fatal("cannot allocate stinfo");
168 sti->func = func;
169 sti->cookie = cookie;
170 sti->fd = s[1];
171
172 if(adb_thread_create( &t, service_bootstrap_func, sti)){
173 free(sti);
174 adb_close(s[0]);
175 adb_close(s[1]);
176 printf("cannot create service thread\n");
177 return -1;
178 }
179
180 D("service thread started, %d:%d\n",s[0], s[1]);
181 return s[0];
182}
183
JP Abgrall408fa572011-03-16 15:57:42 -0700184#if !ADB_HOST
185static 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 -0800186{
Joe Onorato91acb142009-09-03 16:30:43 -0700187#ifdef HAVE_WIN32_PROC
JP Abgrall408fa572011-03-16 15:57:42 -0700188 D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
189 fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
190 return -1;
Joe Onorato91acb142009-09-03 16:30:43 -0700191#else /* !HAVE_WIN32_PROC */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 char *devname;
193 int ptm;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
195 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
196 if(ptm < 0){
197 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
198 return -1;
199 }
200 fcntl(ptm, F_SETFD, FD_CLOEXEC);
201
202 if(grantpt(ptm) || unlockpt(ptm) ||
203 ((devname = (char*) ptsname(ptm)) == 0)){
204 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700205 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 return -1;
207 }
208
JP Abgrall408fa572011-03-16 15:57:42 -0700209 *pid = fork();
210 if(*pid < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700212 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 return -1;
214 }
215
JP Abgrall408fa572011-03-16 15:57:42 -0700216 if(*pid == 0){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 int pts;
218
219 setsid();
220
221 pts = unix_open(devname, O_RDWR);
JP Abgrall408fa572011-03-16 15:57:42 -0700222 if(pts < 0) {
223 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
224 exit(-1);
225 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226
227 dup2(pts, 0);
228 dup2(pts, 1);
229 dup2(pts, 2);
230
Benoit Goby95ef8282011-02-01 18:57:41 -0800231 adb_close(pts);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 adb_close(ptm);
233
JP Abgrall408fa572011-03-16 15:57:42 -0700234 // set OOM adjustment to zero
Mike Lockwood249ad572009-05-20 09:14:30 -0400235 char text[64];
JP Abgrall408fa572011-03-16 15:57:42 -0700236 snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid());
Mike Lockwood249ad572009-05-20 09:14:30 -0400237 int fd = adb_open(text, O_WRONLY);
238 if (fd >= 0) {
239 adb_write(fd, "0", 1);
240 adb_close(fd);
241 } else {
242 D("adb: unable to open %s\n", text);
243 }
JP Abgrall408fa572011-03-16 15:57:42 -0700244 execl(cmd, cmd, arg0, arg1, NULL);
245 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
246 cmd, strerror(errno), errno);
247 exit(-1);
248 } else {
249 // Don't set child's OOM adjustment to zero.
250 // Let the child do it itself, as sometimes the parent starts
251 // running before the child has a /proc/pid/oom_adj.
252 // """adb: unable to open /proc/644/oom_adj""" seen in some logs.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 return ptm;
254 }
Joe Onorato91acb142009-09-03 16:30:43 -0700255#endif /* !HAVE_WIN32_PROC */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256}
JP Abgrall408fa572011-03-16 15:57:42 -0700257#endif /* !ABD_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258
259#if ADB_HOST
260#define SHELL_COMMAND "/bin/sh"
261#else
262#define SHELL_COMMAND "/system/bin/sh"
263#endif
264
JP Abgrall408fa572011-03-16 15:57:42 -0700265#if !ADB_HOST
266static void subproc_waiter_service(int fd, void *cookie)
267{
268 pid_t pid = (pid_t)cookie;
269
270 D("entered. fd=%d of pid=%d\n", fd, pid);
271 for (;;) {
272 int status;
273 pid_t p = waitpid(pid, &status, 0);
274 if (p == pid) {
275 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
276 if (WIFSIGNALED(status)) {
277 D("*** Killed by signal %d\n", WTERMSIG(status));
278 break;
279 } else if (!WIFEXITED(status)) {
280 D("*** Didn't exit!!. status %d\n", status);
281 break;
282 } else if (WEXITSTATUS(status) >= 0) {
283 D("*** Exit code %d\n", WEXITSTATUS(status));
284 break;
285 }
286 }
JP Abgrall408fa572011-03-16 15:57:42 -0700287 }
288 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
289 if (SHELL_EXIT_NOTIFY_FD >=0) {
290 int res;
291 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
292 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
293 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
294 }
295}
296
297static int create_subproc_thread(const char *name)
298{
299 stinfo *sti;
300 adb_thread_t t;
301 int ret_fd;
302 pid_t pid;
303 if(name) {
304 ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid);
305 } else {
306 ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid);
307 }
308 D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
309
310 sti = malloc(sizeof(stinfo));
311 if(sti == 0) fatal("cannot allocate stinfo");
312 sti->func = subproc_waiter_service;
313 sti->cookie = (void*)pid;
314 sti->fd = ret_fd;
315
316 if(adb_thread_create( &t, service_bootstrap_func, sti)){
317 free(sti);
318 adb_close(ret_fd);
319 printf("cannot create service thread\n");
320 return -1;
321 }
322
323 D("service thread started, fd=%d pid=%d\n",ret_fd, pid);
324 return ret_fd;
325}
326#endif
327
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328int service_to_fd(const char *name)
329{
330 int ret = -1;
331
332 if(!strncmp(name, "tcp:", 4)) {
333 int port = atoi(name + 4);
334 name = strchr(name + 4, ':');
335 if(name == 0) {
336 ret = socket_loopback_client(port, SOCK_STREAM);
337 if (ret >= 0)
338 disable_tcp_nagle(ret);
339 } else {
340#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342#else
343 return -1;
344#endif
345 }
346#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
347 } else if(!strncmp(name, "local:", 6)) {
348 ret = socket_local_client(name + 6,
349 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
350 } else if(!strncmp(name, "localreserved:", 14)) {
351 ret = socket_local_client(name + 14,
352 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
353 } else if(!strncmp(name, "localabstract:", 14)) {
354 ret = socket_local_client(name + 14,
355 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
356 } else if(!strncmp(name, "localfilesystem:", 16)) {
357 ret = socket_local_client(name + 16,
358 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
359#endif
Benoit Goby9470c2f2013-02-20 15:04:53 -0800360#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 } else if(!strncmp("dev:", name, 4)) {
362 ret = unix_open(name + 4, O_RDWR);
363 } else if(!strncmp(name, "framebuffer:", 12)) {
364 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 } else if (!strncmp(name, "jdwp:", 5)) {
366 ret = create_jdwp_connection_fd(atoi(name+5));
367 } else if (!strncmp(name, "log:", 4)) {
368 ret = create_service_thread(log_service, get_log_file_path(name + 4));
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 }
396 ret = create_service_thread(restart_tcp_service, (void *)port);
397 } else if(!strncmp(name, "usb:", 4)) {
398 ret = create_service_thread(restart_usb_service, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 }
401 if (ret >= 0) {
402 close_on_exec(ret);
403 }
404 return ret;
405}
406
407#if ADB_HOST
408struct state_info {
409 transport_type transport;
410 char* serial;
411 int state;
412};
413
414static void wait_for_state(int fd, void* cookie)
415{
416 struct state_info* sinfo = cookie;
417 char* err = "unknown error";
418
419 D("wait_for_state %d\n", sinfo->state);
420
421 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
422 if(t != 0) {
423 writex(fd, "OKAY", 4);
424 } else {
425 sendfailmsg(fd, err);
426 }
427
428 if (sinfo->serial)
429 free(sinfo->serial);
430 free(sinfo);
431 adb_close(fd);
432 D("wait_for_state is done\n");
433}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700434
435static void connect_device(char* host, char* buffer, int buffer_size)
436{
437 int port, fd;
438 char* portstr = strchr(host, ':');
439 char hostbuf[100];
440 char serial[100];
441 int ret;
442
443 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
444 if (portstr) {
445 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
446 snprintf(buffer, buffer_size, "bad host name %s", host);
447 return;
448 }
449 // zero terminate the host at the point we found the colon
450 hostbuf[portstr - host] = 0;
451 if (sscanf(portstr + 1, "%d", &port) == 0) {
452 snprintf(buffer, buffer_size, "bad port number %s", portstr);
453 return;
454 }
455 } else {
456 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
457 }
458
459 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
460
461 fd = socket_network_client(hostbuf, port, SOCK_STREAM);
462 if (fd < 0) {
463 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
464 return;
465 }
466
467 D("client: connected on remote on fd %d\n", fd);
468 close_on_exec(fd);
469 disable_tcp_nagle(fd);
470
471 ret = register_socket_transport(fd, serial, port, 0);
472 if (ret < 0) {
473 adb_close(fd);
474 snprintf(buffer, buffer_size, "already connected to %s", serial);
475 } else {
476 snprintf(buffer, buffer_size, "connected to %s", serial);
477 }
478}
479
480void connect_emulator(char* port_spec, char* buffer, int buffer_size)
481{
482 char* port_separator = strchr(port_spec, ',');
483 if (!port_separator) {
484 snprintf(buffer, buffer_size,
485 "unable to parse '%s' as <console port>,<adb port>",
486 port_spec);
487 return;
488 }
489
490 // Zero-terminate console port and make port_separator point to 2nd port.
491 *port_separator++ = 0;
492 int console_port = strtol(port_spec, NULL, 0);
493 int adb_port = strtol(port_separator, NULL, 0);
494 if (!(console_port > 0 && adb_port > 0)) {
495 *(port_separator - 1) = ',';
496 snprintf(buffer, buffer_size,
497 "Invalid port numbers: Expected positive numbers, got '%s'",
498 port_spec);
499 return;
500 }
501
502 /* Check if the emulator is already known.
503 * Note: There's a small but harmless race condition here: An emulator not
504 * present just yet could be registered by another invocation right
505 * after doing this check here. However, local_connect protects
506 * against double-registration too. From here, a better error message
507 * can be produced. In the case of the race condition, the very specific
508 * error message won't be shown, but the data doesn't get corrupted. */
509 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
510 if (known_emulator != NULL) {
511 snprintf(buffer, buffer_size,
512 "Emulator on port %d already registered.", adb_port);
513 return;
514 }
515
516 /* Check if more emulators can be registered. Similar unproblematic
517 * race condition as above. */
518 int candidate_slot = get_available_local_transport_index();
519 if (candidate_slot < 0) {
520 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
521 return;
522 }
523
524 /* Preconditions met, try to connect to the emulator. */
525 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
526 snprintf(buffer, buffer_size,
527 "Connected to emulator on ports %d,%d", console_port, adb_port);
528 } else {
529 snprintf(buffer, buffer_size,
530 "Could not connect to emulator on ports %d,%d",
531 console_port, adb_port);
532 }
533}
534
535static void connect_service(int fd, void* cookie)
536{
537 char buf[4096];
538 char resp[4096];
539 char *host = cookie;
540
541 if (!strncmp(host, "emu:", 4)) {
542 connect_emulator(host + 4, buf, sizeof(buf));
543 } else {
544 connect_device(host, buf, sizeof(buf));
545 }
546
547 // Send response for emulator and device
548 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
549 writex(fd, resp, strlen(resp));
550 adb_close(fd);
551}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800552#endif
553
554#if ADB_HOST
555asocket* host_service_to_socket(const char* name, const char *serial)
556{
557 if (!strcmp(name,"track-devices")) {
558 return create_device_tracker();
559 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
560 struct state_info* sinfo = malloc(sizeof(struct state_info));
561
562 if (serial)
563 sinfo->serial = strdup(serial);
564 else
565 sinfo->serial = NULL;
566
567 name += strlen("wait-for-");
568
569 if (!strncmp(name, "local", strlen("local"))) {
570 sinfo->transport = kTransportLocal;
571 sinfo->state = CS_DEVICE;
572 } else if (!strncmp(name, "usb", strlen("usb"))) {
573 sinfo->transport = kTransportUsb;
574 sinfo->state = CS_DEVICE;
575 } else if (!strncmp(name, "any", strlen("any"))) {
576 sinfo->transport = kTransportAny;
577 sinfo->state = CS_DEVICE;
578 } else {
579 free(sinfo);
580 return NULL;
581 }
582
583 int fd = create_service_thread(wait_for_state, sinfo);
584 return create_local_socket(fd);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700585 } else if (!strncmp(name, "connect:", 8)) {
586 const char *host = name + 8;
587 int fd = create_service_thread(connect_service, (void *)host);
588 return create_local_socket(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589 }
590 return NULL;
591}
592#endif /* ADB_HOST */