blob: b57e891e67dce4663236ade66ee97fb6e2e8cb7a [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +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
17#include <stddef.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <unistd.h>
21#include <string.h>
22#include <errno.h>
23
b.liub17525e2025-05-14 17:22:29 +080024#ifdef MBTK_ADB_SEC_SUPPORT
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#endif
29
b.liue9582032025-04-17 19:18:16 +080030#include "sysdeps.h"
31
32#define TRACE_TAG TRACE_SERVICES
33#include "adb.h"
34#include "file_sync_service.h"
35
36#if ADB_HOST
37# ifndef HAVE_WINSOCK
38# include <netinet/in.h>
39# include <netdb.h>
40# include <sys/ioctl.h>
41# endif
42#else
43# include <cutils/android_reboot.h>
44# include <cutils/properties.h>
45#endif
46
47typedef struct stinfo stinfo;
48
49struct stinfo {
50 void (*func)(int fd, void *cookie);
51 int fd;
52 void *cookie;
53};
54
55
56void *service_bootstrap_func(void *x)
57{
58 stinfo *sti = x;
59 sti->func(sti->fd, sti->cookie);
60 free(sti);
61 return 0;
62}
63
64#if !ADB_HOST
65
66void restart_root_service(int fd, void *cookie)
67{
68 char buf[100];
69 char value[PROPERTY_VALUE_MAX];
70
71 if (getuid() == 0) {
72 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
73 writex(fd, buf, strlen(buf));
74 adb_close(fd);
75 } else {
76 property_get("ro.debuggable", value, "");
77 if (strcmp(value, "1") != 0) {
78 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
79 writex(fd, buf, strlen(buf));
80 adb_close(fd);
81 return;
82 }
83
84 property_set("service.adb.root", "1");
85 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
86 writex(fd, buf, strlen(buf));
87 adb_close(fd);
88 }
89}
90
91void restart_tcp_service(int fd, void *cookie)
92{
93 char buf[100];
94 char value[PROPERTY_VALUE_MAX];
95 int port = (int) (uintptr_t) cookie;
96
97 if (port <= 0) {
98 snprintf(buf, sizeof(buf), "invalid port\n");
99 writex(fd, buf, strlen(buf));
100 adb_close(fd);
101 return;
102 }
103
104 snprintf(value, sizeof(value), "%d", port);
105 property_set("service.adb.tcp.port", value);
106 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
107 writex(fd, buf, strlen(buf));
108 adb_close(fd);
109}
110
111void restart_usb_service(int fd, void *cookie)
112{
113 char buf[100];
114
115 property_set("service.adb.tcp.port", "0");
116 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
117 writex(fd, buf, strlen(buf));
118 adb_close(fd);
119}
120
121void reboot_service(int fd, void *arg)
122{
123 char buf[100];
124 char property_val[PROPERTY_VALUE_MAX];
125 int ret;
126
127 sync();
128
129 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
130 if (ret >= (int) sizeof(property_val)) {
131 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
132 writex(fd, buf, strlen(buf));
133 goto cleanup;
134 }
135
136 ret = property_set(ANDROID_RB_PROPERTY, property_val);
137 if (ret < 0) {
138 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
139 writex(fd, buf, strlen(buf));
140 goto cleanup;
141 }
142 // Don't return early. Give the reboot command time to take effect
143 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
144 while(1) { pause(); }
145cleanup:
146 free(arg);
147 adb_close(fd);
148}
149
150void reverse_service(int fd, void* arg)
151{
152 const char* command = arg;
153
154 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
155 sendfailmsg(fd, "not a reverse forwarding command");
156 }
157 free(arg);
158 adb_close(fd);
159}
160
161#endif
162
163static int create_service_thread(void (*func)(int, void *), void *cookie)
164{
165 stinfo *sti;
166 adb_thread_t t;
167 int s[2];
168
169 if(adb_socketpair(s)) {
170 printf("cannot create service socket pair\n");
171 return -1;
172 }
173
174 sti = malloc(sizeof(stinfo));
175 if(sti == 0) fatal("cannot allocate stinfo");
176 sti->func = func;
177 sti->cookie = cookie;
178 sti->fd = s[1];
179
180 if(adb_thread_create( &t, service_bootstrap_func, sti)){
181 free(sti);
182 adb_close(s[0]);
183 adb_close(s[1]);
184 printf("cannot create service thread\n");
185 return -1;
186 }
187
188 D("service thread started, %d:%d\n",s[0], s[1]);
189 return s[0];
190}
191
192#if !ADB_HOST
193
194static void init_subproc_child()
195{
196 setsid();
197
198 // Set OOM score adjustment to prevent killing
199 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY);
200 if (fd >= 0) {
201 adb_write(fd, "0", 1);
202 adb_close(fd);
203 } else {
204 D("adb: unable to update oom_score_adj\n");
205 }
206}
207
208static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
209{
210 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
211#ifdef HAVE_WIN32_PROC
212 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
213 return -1;
214#else /* !HAVE_WIN32_PROC */
215 char *devname;
216 int ptm;
217
218 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
219 if(ptm < 0){
220 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
221 return -1;
222 }
223 fcntl(ptm, F_SETFD, FD_CLOEXEC);
224
225 if(grantpt(ptm) || unlockpt(ptm) ||
226 ((devname = (char*) ptsname(ptm)) == 0)){
227 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
228 adb_close(ptm);
229 return -1;
230 }
231
232 *pid = fork();
233 if(*pid < 0) {
234 printf("- fork failed: %s -\n", strerror(errno));
235 adb_close(ptm);
236 return -1;
237 }
238
239 if (*pid == 0) {
240 init_subproc_child();
241
242 int pts = unix_open(devname, O_RDWR);
243 if (pts < 0) {
244 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
245 exit(-1);
246 }
247
248 dup2(pts, STDIN_FILENO);
249 dup2(pts, STDOUT_FILENO);
250 dup2(pts, STDERR_FILENO);
251
252 adb_close(pts);
253 adb_close(ptm);
254
255 execl(cmd, cmd, arg0, arg1, NULL);
256 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
257 cmd, strerror(errno), errno);
258 exit(-1);
259 } else {
260 return ptm;
261 }
262#endif /* !HAVE_WIN32_PROC */
263}
264
265static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
266{
267 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
268#ifdef HAVE_WIN32_PROC
269 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
270 return -1;
271#else /* !HAVE_WIN32_PROC */
272
273 // 0 is parent socket, 1 is child socket
274 int sv[2];
275 if (unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
276 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
277 return -1;
278 }
279
280 *pid = fork();
281 if (*pid < 0) {
282 printf("- fork failed: %s -\n", strerror(errno));
283 adb_close(sv[0]);
284 adb_close(sv[1]);
285 return -1;
286 }
287
288 if (*pid == 0) {
289 adb_close(sv[0]);
290 init_subproc_child();
291
292 // Only hook up stdin/stdout; drop stderr
293 dup2(sv[1], STDIN_FILENO);
294 dup2(sv[1], STDOUT_FILENO);
295 adb_close(sv[1]);
296
297 execl(cmd, cmd, arg0, arg1, NULL);
298 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
299 cmd, strerror(errno), errno);
300 exit(-1);
301 } else {
302 adb_close(sv[1]);
303 return sv[0];
304 }
305#endif /* !HAVE_WIN32_PROC */
306}
307#endif /* !ABD_HOST */
308
b.liue9582032025-04-17 19:18:16 +0800309#if ADB_HOST
310#define SHELL_COMMAND "/bin/sh"
311#else
b.liub17525e2025-05-14 17:22:29 +0800312
313#ifdef MBTK_ADB_SEC_SUPPORT
314#define SHELL_COMMAND "/bin/adb_shell"
b.liue9582032025-04-17 19:18:16 +0800315#else
316#define SHELL_COMMAND "/bin/sh"
b.liub17525e2025-05-14 17:22:29 +0800317#endif
318
319#endif
320
321#ifdef MBTK_ADB_SEC_SUPPORT
322#define MBTK_LOGIN_STATE_FILE "/tmp/mbtk_login_count"
323
324static int login_conf_read(const char *path, long *value)
325{
326 char buff[64];
327 int fd = adb_open(path, O_RDONLY);
328 if(fd <= 0)
329 return -1;
330 memset(buff, 0, sizeof(buff));
331 if(adb_read(fd, buff, sizeof(buff)) > 0) {
332 *value = atol(buff);
333 }
334 adb_close(fd);
335 return 0;
336}
337
338static int login_conf_write(const char *path, long value)
339{
340 char buff[64];
341 int fd = adb_open(path, O_WRONLY);
342 if(fd <= 0)
343 return -1;
344 memset(buff, 0, sizeof(buff));
345 snprintf(buff, sizeof(buff), "%ld\n", value);
346 if(adb_write(fd, buff, strlen(buff)) > 0) {
347 adb_close(fd);
348 return 0;
349 }
350 adb_close(fd);
351 return -1;
352}
353
354static int login_count_change(int add)
355{
356 long count = 0L;
357 if(login_conf_read(MBTK_LOGIN_STATE_FILE, &count)) {
358 count = 0L;
359 }
360 // printf("old count is %d\n", count);
361 if(add) {
362 count++;
363 } else {
364 count--;
365 }
366
367 if(count < 0) {
368 count = 0L;
369 }
370 return login_conf_write(MBTK_LOGIN_STATE_FILE, count);
371}
372
b.liue9582032025-04-17 19:18:16 +0800373#endif
374
375#if !ADB_HOST
376static void subproc_waiter_service(int fd, void *cookie)
377{
378 pid_t pid = (pid_t) (uintptr_t) cookie;
379
380 D("entered. fd=%d of pid=%d\n", fd, pid);
381 for (;;) {
382 int status;
383 pid_t p = waitpid(pid, &status, 0);
384 if (p == pid) {
385 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
386 if (WIFSIGNALED(status)) {
387 D("*** Killed by signal %d\n", WTERMSIG(status));
388 break;
389 } else if (!WIFEXITED(status)) {
390 D("*** Didn't exit!!. status %d\n", status);
391 break;
392 } else if (WEXITSTATUS(status) >= 0) {
393 D("*** Exit code %d\n", WEXITSTATUS(status));
394 break;
395 }
396 }
397 }
b.liub17525e2025-05-14 17:22:29 +0800398
399#ifdef MBTK_ADB_SEC_SUPPORT
400 login_count_change(0);
401#endif
b.liue9582032025-04-17 19:18:16 +0800402 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
403 if (SHELL_EXIT_NOTIFY_FD >=0) {
404 int res;
405 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
406 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
407 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
408 }
409}
410
411static int create_subproc_thread(const char *name, const subproc_mode mode)
412{
413 stinfo *sti;
414 adb_thread_t t;
415 int ret_fd;
416 pid_t pid = -1;
417
418 const char *arg0, *arg1;
419 if (name == 0 || *name == 0) {
b.liue9582032025-04-17 19:18:16 +0800420 arg0 = "-"; arg1 = 0;
b.liue9582032025-04-17 19:18:16 +0800421 } else {
422 arg0 = "-c"; arg1 = name;
423 }
424
425 switch (mode) {
426 case SUBPROC_PTY:
427 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
428 break;
429 case SUBPROC_RAW:
430 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
431 break;
432 default:
433 fprintf(stderr, "invalid subproc_mode %d\n", mode);
434 return -1;
435 }
436 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
437
438 sti = malloc(sizeof(stinfo));
439 if(sti == 0) fatal("cannot allocate stinfo");
440 sti->func = subproc_waiter_service;
441 sti->cookie = (void*) (uintptr_t) pid;
442 sti->fd = ret_fd;
443
444 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
445 free(sti);
446 adb_close(ret_fd);
447 fprintf(stderr, "cannot create service thread\n");
448 return -1;
449 }
450
451 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
452 return ret_fd;
453}
454#endif
455
456int service_to_fd(const char *name)
457{
458 int ret = -1;
459
460 if(!strncmp(name, "tcp:", 4)) {
461 int port = atoi(name + 4);
462 name = strchr(name + 4, ':');
463 if(name == 0) {
464 ret = socket_loopback_client(port, SOCK_STREAM);
465 if (ret >= 0)
466 disable_tcp_nagle(ret);
467 } else {
468#if ADB_HOST
469 ret = socket_network_client(name + 1, port, SOCK_STREAM);
470#else
471 return -1;
472#endif
473 }
474#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
475 } else if(!strncmp(name, "local:", 6)) {
476 ret = socket_local_client(name + 6,
477 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
478 } else if(!strncmp(name, "localreserved:", 14)) {
479 ret = socket_local_client(name + 14,
480 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
481 } else if(!strncmp(name, "localabstract:", 14)) {
482 ret = socket_local_client(name + 14,
483 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
484 } else if(!strncmp(name, "localfilesystem:", 16)) {
485 ret = socket_local_client(name + 16,
486 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
487#endif
488#if !ADB_HOST
489 } else if(!strncmp("dev:", name, 4)) {
490 ret = unix_open(name + 4, O_RDWR);
491 } else if(!strncmp(name, "framebuffer:", 12)) {
492 ret = create_service_thread(framebuffer_service, 0);
493 } else if (!strncmp(name, "jdwp:", 5)) {
494 ret = create_jdwp_connection_fd(atoi(name+5));
495 } else if(!HOST && !strncmp(name, "shell:", 6)) {
496 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
497 } else if(!HOST && !strncmp(name, "exec:", 5)) {
498 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
499 } else if(!strncmp(name, "sync:", 5)) {
b.liub17525e2025-05-14 17:22:29 +0800500#ifdef MBTK_ADB_SEC_SUPPORT
501 long login_success = 0L;
502 if(login_conf_read(MBTK_LOGIN_STATE_FILE, &login_success) == 0 && login_success > 0) {
503 ret = create_service_thread(file_sync_service, NULL);
504 }
505#else
506 ret = create_service_thread(file_sync_service, NULL);
507#endif
b.liue9582032025-04-17 19:18:16 +0800508 } else if(!strncmp(name, "remount:", 8)) {
509 ret = create_service_thread(remount_service, NULL);
510 } else if(!strncmp(name, "reboot:", 7)) {
511 void* arg = strdup(name + 7);
512 if (arg == NULL) return -1;
513 ret = create_service_thread(reboot_service, arg);
514 } else if(!strncmp(name, "root:", 5)) {
515 ret = create_service_thread(restart_root_service, NULL);
516 } else if(!strncmp(name, "backup:", 7)) {
517 char* arg = strdup(name + 7);
518 if (arg == NULL) return -1;
519 char* c = arg;
520 for (; *c != '\0'; c++) {
521 if (*c == ':')
522 *c = ' ';
523 }
524 char* cmd;
525 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
526 ret = create_subproc_thread(cmd, SUBPROC_RAW);
527 free(cmd);
528 }
529 free(arg);
530 } else if(!strncmp(name, "restore:", 8)) {
531 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
532 } else if(!strncmp(name, "tcpip:", 6)) {
533 int port;
534 if (sscanf(name + 6, "%d", &port) == 0) {
535 port = 0;
536 }
537 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
538 } else if(!strncmp(name, "usb:", 4)) {
539 ret = create_service_thread(restart_usb_service, NULL);
540 } else if (!strncmp(name, "reverse:", 8)) {
541 char* cookie = strdup(name + 8);
542 if (cookie == NULL) {
543 ret = -1;
544 } else {
545 ret = create_service_thread(reverse_service, cookie);
546 if (ret < 0) {
547 free(cookie);
548 }
549 }
550#endif
551 }
552 if (ret >= 0) {
553 close_on_exec(ret);
554 }
555 return ret;
556}
557
558#if ADB_HOST
559struct state_info {
560 transport_type transport;
561 char* serial;
562 int state;
563};
564
565static void wait_for_state(int fd, void* cookie)
566{
567 struct state_info* sinfo = cookie;
568 char* err = "unknown error";
569
570 D("wait_for_state %d\n", sinfo->state);
571
572 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
573 if(t != 0) {
574 writex(fd, "OKAY", 4);
575 } else {
576 sendfailmsg(fd, err);
577 }
578
579 if (sinfo->serial)
580 free(sinfo->serial);
581 free(sinfo);
582 adb_close(fd);
583 D("wait_for_state is done\n");
584}
585
586static void connect_device(char* host, char* buffer, int buffer_size)
587{
588 int port, fd;
589 char* portstr = strchr(host, ':');
590 char hostbuf[100];
591 char serial[100];
592 int ret;
593
594 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
595 if (portstr) {
596 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
597 snprintf(buffer, buffer_size, "bad host name %s", host);
598 return;
599 }
600 // zero terminate the host at the point we found the colon
601 hostbuf[portstr - host] = 0;
602 if (sscanf(portstr + 1, "%d", &port) == 0) {
603 snprintf(buffer, buffer_size, "bad port number %s", portstr);
604 return;
605 }
606 } else {
607 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
608 }
609
610 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
611
612 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
613 if (fd < 0) {
614 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
615 return;
616 }
617
618 D("client: connected on remote on fd %d\n", fd);
619 close_on_exec(fd);
620 disable_tcp_nagle(fd);
621
622 ret = register_socket_transport(fd, serial, port, 0);
623 if (ret < 0) {
624 adb_close(fd);
625 snprintf(buffer, buffer_size, "already connected to %s", serial);
626 } else {
627 snprintf(buffer, buffer_size, "connected to %s", serial);
628 }
629}
630
631void connect_emulator(char* port_spec, char* buffer, int buffer_size)
632{
633 char* port_separator = strchr(port_spec, ',');
634 if (!port_separator) {
635 snprintf(buffer, buffer_size,
636 "unable to parse '%s' as <console port>,<adb port>",
637 port_spec);
638 return;
639 }
640
641 // Zero-terminate console port and make port_separator point to 2nd port.
642 *port_separator++ = 0;
643 int console_port = strtol(port_spec, NULL, 0);
644 int adb_port = strtol(port_separator, NULL, 0);
645 if (!(console_port > 0 && adb_port > 0)) {
646 *(port_separator - 1) = ',';
647 snprintf(buffer, buffer_size,
648 "Invalid port numbers: Expected positive numbers, got '%s'",
649 port_spec);
650 return;
651 }
652
653 /* Check if the emulator is already known.
654 * Note: There's a small but harmless race condition here: An emulator not
655 * present just yet could be registered by another invocation right
656 * after doing this check here. However, local_connect protects
657 * against double-registration too. From here, a better error message
658 * can be produced. In the case of the race condition, the very specific
659 * error message won't be shown, but the data doesn't get corrupted. */
660 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
661 if (known_emulator != NULL) {
662 snprintf(buffer, buffer_size,
663 "Emulator on port %d already registered.", adb_port);
664 return;
665 }
666
667 /* Check if more emulators can be registered. Similar unproblematic
668 * race condition as above. */
669 int candidate_slot = get_available_local_transport_index();
670 if (candidate_slot < 0) {
671 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
672 return;
673 }
674
675 /* Preconditions met, try to connect to the emulator. */
676 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
677 snprintf(buffer, buffer_size,
678 "Connected to emulator on ports %d,%d", console_port, adb_port);
679 } else {
680 snprintf(buffer, buffer_size,
681 "Could not connect to emulator on ports %d,%d",
682 console_port, adb_port);
683 }
684}
685
686static void connect_service(int fd, void* cookie)
687{
688 char buf[4096];
689 char resp[4096];
690 char *host = cookie;
691
692 if (!strncmp(host, "emu:", 4)) {
693 connect_emulator(host + 4, buf, sizeof(buf));
694 } else {
695 connect_device(host, buf, sizeof(buf));
696 }
697
698 // Send response for emulator and device
699 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
700 writex(fd, resp, strlen(resp));
701 adb_close(fd);
702}
703#endif
704
705#if ADB_HOST
706asocket* host_service_to_socket(const char* name, const char *serial)
707{
708 if (!strcmp(name,"track-devices")) {
709 return create_device_tracker();
710 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
711 struct state_info* sinfo = malloc(sizeof(struct state_info));
712
713 if (serial)
714 sinfo->serial = strdup(serial);
715 else
716 sinfo->serial = NULL;
717
718 name += strlen("wait-for-");
719
720 if (!strncmp(name, "local", strlen("local"))) {
721 sinfo->transport = kTransportLocal;
722 sinfo->state = CS_DEVICE;
723 } else if (!strncmp(name, "usb", strlen("usb"))) {
724 sinfo->transport = kTransportUsb;
725 sinfo->state = CS_DEVICE;
726 } else if (!strncmp(name, "any", strlen("any"))) {
727 sinfo->transport = kTransportAny;
728 sinfo->state = CS_DEVICE;
729 } else {
730 free(sinfo);
731 return NULL;
732 }
733
734 int fd = create_service_thread(wait_for_state, sinfo);
735 return create_local_socket(fd);
736 } else if (!strncmp(name, "connect:", 8)) {
737 const char *host = name + 8;
738 int fd = create_service_thread(connect_service, (void *)host);
739 return create_local_socket(fd);
740 }
741 return NULL;
742}
743#endif /* ADB_HOST */