blob: 50b81abe9546fd08828f8ad2a73c2ea116854c5f [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144#endif
145
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146static int create_service_thread(void (*func)(int, void *), void *cookie)
147{
148 stinfo *sti;
149 adb_thread_t t;
150 int s[2];
151
152 if(adb_socketpair(s)) {
153 printf("cannot create service socket pair\n");
154 return -1;
155 }
156
157 sti = malloc(sizeof(stinfo));
158 if(sti == 0) fatal("cannot allocate stinfo");
159 sti->func = func;
160 sti->cookie = cookie;
161 sti->fd = s[1];
162
163 if(adb_thread_create( &t, service_bootstrap_func, sti)){
164 free(sti);
165 adb_close(s[0]);
166 adb_close(s[1]);
167 printf("cannot create service thread\n");
168 return -1;
169 }
170
171 D("service thread started, %d:%d\n",s[0], s[1]);
172 return s[0];
173}
174
JP Abgrall408fa572011-03-16 15:57:42 -0700175#if !ADB_HOST
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700176
177static void init_subproc_child()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178{
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700179 setsid();
180
181 // Set OOM adjustment to prevent killing
182 int fd = adb_open("/proc/self/oom_adj", O_WRONLY);
183 if (fd >= 0) {
184 adb_write(fd, "0", 1);
185 adb_close(fd);
186 } else {
187 D("adb: unable to update oom_adj\n");
188 }
189}
190
191static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
192{
193 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Joe Onorato91acb142009-09-03 16:30:43 -0700194#ifdef HAVE_WIN32_PROC
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700195 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
JP Abgrall408fa572011-03-16 15:57:42 -0700196 return -1;
Joe Onorato91acb142009-09-03 16:30:43 -0700197#else /* !HAVE_WIN32_PROC */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 char *devname;
199 int ptm;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
201 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
202 if(ptm < 0){
203 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
204 return -1;
205 }
206 fcntl(ptm, F_SETFD, FD_CLOEXEC);
207
208 if(grantpt(ptm) || unlockpt(ptm) ||
209 ((devname = (char*) ptsname(ptm)) == 0)){
210 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700211 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 return -1;
213 }
214
JP Abgrall408fa572011-03-16 15:57:42 -0700215 *pid = fork();
216 if(*pid < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700218 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 return -1;
220 }
221
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700222 if (*pid == 0) {
223 init_subproc_child();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700225 int pts = unix_open(devname, O_RDWR);
226 if (pts < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -0700227 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
228 exit(-1);
229 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700231 dup2(pts, STDIN_FILENO);
232 dup2(pts, STDOUT_FILENO);
233 dup2(pts, STDERR_FILENO);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234
Benoit Goby95ef8282011-02-01 18:57:41 -0800235 adb_close(pts);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 adb_close(ptm);
237
JP Abgrall408fa572011-03-16 15:57:42 -0700238 execl(cmd, cmd, arg0, arg1, NULL);
239 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
240 cmd, strerror(errno), errno);
241 exit(-1);
242 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 return ptm;
244 }
Joe Onorato91acb142009-09-03 16:30:43 -0700245#endif /* !HAVE_WIN32_PROC */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246}
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700247
248static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
249{
250 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
251#ifdef HAVE_WIN32_PROC
252 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
253 return -1;
254#else /* !HAVE_WIN32_PROC */
255
256 // 0 is parent socket, 1 is child socket
257 int sv[2];
258 if (unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
259 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
260 return -1;
261 }
262
263 *pid = fork();
264 if (*pid < 0) {
265 printf("- fork failed: %s -\n", strerror(errno));
266 adb_close(sv[0]);
267 adb_close(sv[1]);
268 return -1;
269 }
270
271 if (*pid == 0) {
272 adb_close(sv[0]);
273 init_subproc_child();
274
275 // Only hook up stdin/stdout; drop stderr
276 dup2(sv[1], STDIN_FILENO);
277 dup2(sv[1], STDOUT_FILENO);
278 adb_close(sv[1]);
279
280 execl(cmd, cmd, arg0, arg1, NULL);
281 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
282 cmd, strerror(errno), errno);
283 exit(-1);
284 } else {
285 adb_close(sv[1]);
286 return sv[0];
287 }
288#endif /* !HAVE_WIN32_PROC */
289}
JP Abgrall408fa572011-03-16 15:57:42 -0700290#endif /* !ABD_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291
292#if ADB_HOST
293#define SHELL_COMMAND "/bin/sh"
294#else
295#define SHELL_COMMAND "/system/bin/sh"
296#endif
297
JP Abgrall408fa572011-03-16 15:57:42 -0700298#if !ADB_HOST
299static void subproc_waiter_service(int fd, void *cookie)
300{
Elliott Hughesccecf142014-01-16 10:53:11 -0800301 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall408fa572011-03-16 15:57:42 -0700302
303 D("entered. fd=%d of pid=%d\n", fd, pid);
304 for (;;) {
305 int status;
306 pid_t p = waitpid(pid, &status, 0);
307 if (p == pid) {
308 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
309 if (WIFSIGNALED(status)) {
310 D("*** Killed by signal %d\n", WTERMSIG(status));
311 break;
312 } else if (!WIFEXITED(status)) {
313 D("*** Didn't exit!!. status %d\n", status);
314 break;
315 } else if (WEXITSTATUS(status) >= 0) {
316 D("*** Exit code %d\n", WEXITSTATUS(status));
317 break;
318 }
319 }
JP Abgrall408fa572011-03-16 15:57:42 -0700320 }
321 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
322 if (SHELL_EXIT_NOTIFY_FD >=0) {
323 int res;
324 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
325 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
326 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
327 }
328}
329
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700330static int create_subproc_thread(const char *name, const subproc_mode mode)
JP Abgrall408fa572011-03-16 15:57:42 -0700331{
332 stinfo *sti;
333 adb_thread_t t;
334 int ret_fd;
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700335 pid_t pid = -1;
336
337 const char *arg0, *arg1;
338 if (name == 0 || *name == 0) {
339 arg0 = "-"; arg1 = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700340 } else {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700341 arg0 = "-c"; arg1 = name;
JP Abgrall408fa572011-03-16 15:57:42 -0700342 }
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700343
344 switch (mode) {
345 case SUBPROC_PTY:
346 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
347 break;
348 case SUBPROC_RAW:
349 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
350 break;
351 default:
352 fprintf(stderr, "invalid subproc_mode %d\n", mode);
353 return -1;
354 }
355 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall408fa572011-03-16 15:57:42 -0700356
357 sti = malloc(sizeof(stinfo));
358 if(sti == 0) fatal("cannot allocate stinfo");
359 sti->func = subproc_waiter_service;
Elliott Hughesccecf142014-01-16 10:53:11 -0800360 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall408fa572011-03-16 15:57:42 -0700361 sti->fd = ret_fd;
362
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700363 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
JP Abgrall408fa572011-03-16 15:57:42 -0700364 free(sti);
365 adb_close(ret_fd);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700366 fprintf(stderr, "cannot create service thread\n");
JP Abgrall408fa572011-03-16 15:57:42 -0700367 return -1;
368 }
369
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700370 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall408fa572011-03-16 15:57:42 -0700371 return ret_fd;
372}
373#endif
374
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375int service_to_fd(const char *name)
376{
377 int ret = -1;
378
379 if(!strncmp(name, "tcp:", 4)) {
380 int port = atoi(name + 4);
381 name = strchr(name + 4, ':');
382 if(name == 0) {
383 ret = socket_loopback_client(port, SOCK_STREAM);
384 if (ret >= 0)
385 disable_tcp_nagle(ret);
386 } else {
387#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389#else
390 return -1;
391#endif
392 }
393#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
394 } else if(!strncmp(name, "local:", 6)) {
395 ret = socket_local_client(name + 6,
396 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
397 } else if(!strncmp(name, "localreserved:", 14)) {
398 ret = socket_local_client(name + 14,
399 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
400 } else if(!strncmp(name, "localabstract:", 14)) {
401 ret = socket_local_client(name + 14,
402 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
403 } else if(!strncmp(name, "localfilesystem:", 16)) {
404 ret = socket_local_client(name + 16,
405 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
406#endif
Benoit Goby9470c2f2013-02-20 15:04:53 -0800407#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 } else if(!strncmp("dev:", name, 4)) {
409 ret = unix_open(name + 4, O_RDWR);
410 } else if(!strncmp(name, "framebuffer:", 12)) {
411 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 } else if (!strncmp(name, "jdwp:", 5)) {
413 ret = create_jdwp_connection_fd(atoi(name+5));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 } else if(!HOST && !strncmp(name, "shell:", 6)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700415 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
416 } else if(!HOST && !strncmp(name, "exec:", 5)) {
417 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 } else if(!strncmp(name, "sync:", 5)) {
419 ret = create_service_thread(file_sync_service, NULL);
420 } else if(!strncmp(name, "remount:", 8)) {
421 ret = create_service_thread(remount_service, NULL);
Mike Lockwoodee156622009-08-04 20:37:51 -0400422 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400423 void* arg = strdup(name + 7);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700424 if (arg == NULL) return -1;
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400425 ret = create_service_thread(reboot_service, arg);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700426 } else if(!strncmp(name, "root:", 5)) {
427 ret = create_service_thread(restart_root_service, NULL);
Christopher Tated2f54152011-04-21 12:53:28 -0700428 } else if(!strncmp(name, "backup:", 7)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700429 char* arg = strdup(name + 7);
Christopher Tated2f54152011-04-21 12:53:28 -0700430 if (arg == NULL) return -1;
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700431 char* c = arg;
432 for (; *c != '\0'; c++) {
433 if (*c == ':')
434 *c = ' ';
435 }
436 char* cmd;
437 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
438 ret = create_subproc_thread(cmd, SUBPROC_RAW);
439 free(cmd);
440 }
441 free(arg);
Christopher Tate702967a2011-05-17 15:52:54 -0700442 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700443 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwoodff196702009-08-24 15:58:40 -0700444 } else if(!strncmp(name, "tcpip:", 6)) {
445 int port;
446 if (sscanf(name + 6, "%d", &port) == 0) {
447 port = 0;
448 }
Elliott Hughesccecf142014-01-16 10:53:11 -0800449 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwoodff196702009-08-24 15:58:40 -0700450 } else if(!strncmp(name, "usb:", 4)) {
451 ret = create_service_thread(restart_usb_service, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 }
454 if (ret >= 0) {
455 close_on_exec(ret);
456 }
457 return ret;
458}
459
460#if ADB_HOST
461struct state_info {
462 transport_type transport;
463 char* serial;
464 int state;
465};
466
467static void wait_for_state(int fd, void* cookie)
468{
469 struct state_info* sinfo = cookie;
470 char* err = "unknown error";
471
472 D("wait_for_state %d\n", sinfo->state);
473
474 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
475 if(t != 0) {
476 writex(fd, "OKAY", 4);
477 } else {
478 sendfailmsg(fd, err);
479 }
480
481 if (sinfo->serial)
482 free(sinfo->serial);
483 free(sinfo);
484 adb_close(fd);
485 D("wait_for_state is done\n");
486}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700487
488static void connect_device(char* host, char* buffer, int buffer_size)
489{
490 int port, fd;
491 char* portstr = strchr(host, ':');
492 char hostbuf[100];
493 char serial[100];
494 int ret;
495
496 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
497 if (portstr) {
498 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
499 snprintf(buffer, buffer_size, "bad host name %s", host);
500 return;
501 }
502 // zero terminate the host at the point we found the colon
503 hostbuf[portstr - host] = 0;
504 if (sscanf(portstr + 1, "%d", &port) == 0) {
505 snprintf(buffer, buffer_size, "bad port number %s", portstr);
506 return;
507 }
508 } else {
509 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
510 }
511
512 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
513
Ken Liermanaecc6a62013-06-20 09:27:13 -0700514 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700515 if (fd < 0) {
516 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
517 return;
518 }
519
520 D("client: connected on remote on fd %d\n", fd);
521 close_on_exec(fd);
522 disable_tcp_nagle(fd);
523
524 ret = register_socket_transport(fd, serial, port, 0);
525 if (ret < 0) {
526 adb_close(fd);
527 snprintf(buffer, buffer_size, "already connected to %s", serial);
528 } else {
529 snprintf(buffer, buffer_size, "connected to %s", serial);
530 }
531}
532
533void connect_emulator(char* port_spec, char* buffer, int buffer_size)
534{
535 char* port_separator = strchr(port_spec, ',');
536 if (!port_separator) {
537 snprintf(buffer, buffer_size,
538 "unable to parse '%s' as <console port>,<adb port>",
539 port_spec);
540 return;
541 }
542
543 // Zero-terminate console port and make port_separator point to 2nd port.
544 *port_separator++ = 0;
545 int console_port = strtol(port_spec, NULL, 0);
546 int adb_port = strtol(port_separator, NULL, 0);
547 if (!(console_port > 0 && adb_port > 0)) {
548 *(port_separator - 1) = ',';
549 snprintf(buffer, buffer_size,
550 "Invalid port numbers: Expected positive numbers, got '%s'",
551 port_spec);
552 return;
553 }
554
555 /* Check if the emulator is already known.
556 * Note: There's a small but harmless race condition here: An emulator not
557 * present just yet could be registered by another invocation right
558 * after doing this check here. However, local_connect protects
559 * against double-registration too. From here, a better error message
560 * can be produced. In the case of the race condition, the very specific
561 * error message won't be shown, but the data doesn't get corrupted. */
562 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
563 if (known_emulator != NULL) {
564 snprintf(buffer, buffer_size,
565 "Emulator on port %d already registered.", adb_port);
566 return;
567 }
568
569 /* Check if more emulators can be registered. Similar unproblematic
570 * race condition as above. */
571 int candidate_slot = get_available_local_transport_index();
572 if (candidate_slot < 0) {
573 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
574 return;
575 }
576
577 /* Preconditions met, try to connect to the emulator. */
578 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
579 snprintf(buffer, buffer_size,
580 "Connected to emulator on ports %d,%d", console_port, adb_port);
581 } else {
582 snprintf(buffer, buffer_size,
583 "Could not connect to emulator on ports %d,%d",
584 console_port, adb_port);
585 }
586}
587
588static void connect_service(int fd, void* cookie)
589{
590 char buf[4096];
591 char resp[4096];
592 char *host = cookie;
593
594 if (!strncmp(host, "emu:", 4)) {
595 connect_emulator(host + 4, buf, sizeof(buf));
596 } else {
597 connect_device(host, buf, sizeof(buf));
598 }
599
600 // Send response for emulator and device
601 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
602 writex(fd, resp, strlen(resp));
603 adb_close(fd);
604}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605#endif
606
607#if ADB_HOST
608asocket* host_service_to_socket(const char* name, const char *serial)
609{
610 if (!strcmp(name,"track-devices")) {
611 return create_device_tracker();
612 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
613 struct state_info* sinfo = malloc(sizeof(struct state_info));
614
615 if (serial)
616 sinfo->serial = strdup(serial);
617 else
618 sinfo->serial = NULL;
619
620 name += strlen("wait-for-");
621
622 if (!strncmp(name, "local", strlen("local"))) {
623 sinfo->transport = kTransportLocal;
624 sinfo->state = CS_DEVICE;
625 } else if (!strncmp(name, "usb", strlen("usb"))) {
626 sinfo->transport = kTransportUsb;
627 sinfo->state = CS_DEVICE;
628 } else if (!strncmp(name, "any", strlen("any"))) {
629 sinfo->transport = kTransportAny;
630 sinfo->state = CS_DEVICE;
631 } else {
632 free(sinfo);
633 return NULL;
634 }
635
636 int fd = create_service_thread(wait_for_state, sinfo);
637 return create_local_socket(fd);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700638 } else if (!strncmp(name, "connect:", 8)) {
639 const char *host = name + 8;
640 int fd = create_service_thread(connect_service, (void *)host);
641 return create_local_socket(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642 }
643 return NULL;
644}
645#endif /* ADB_HOST */