blob: bd210a8d75ce6b8d19be179d194d3414fc1f43cb [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Goby3f9f9ce2013-03-29 18:22:36 -070017#include <stddef.h>
The Android Open Source Project9ca14dc2009-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 Abgrall2e5dd6e2011-03-16 15:57:42 -070026#define TRACE_TAG TRACE_SERVICES
The Android Open Source Project9ca14dc2009-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 Abgrall2e5dd6e2011-03-16 15:57:42 -070034# include <sys/ioctl.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080035# endif
Mike Lockwoodfa311c32009-07-30 16:23:56 -070036#else
Ken Sumrall78438c92011-03-07 23:29:42 -080037# include <cutils/android_reboot.h>
Nick Kralevich7472d292013-05-23 09:54:13 -070038# include <cutils/properties.h>
The Android Open Source Project9ca14dc2009-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 Goby12dc3692013-02-20 15:04:53 -080058#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059
The Android Open Source Project9c753402009-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 Lockwood26b88e32009-08-24 15:58:40 -070074 adb_close(fd);
The Android Open Source Project9c753402009-03-13 13:04:37 -070075 return;
76 }
77
Benoit Gobyad926c12012-03-16 14:44:17 -070078 property_set("service.adb.root", "1");
The Android Open Source Project9c753402009-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 Project9c753402009-03-13 13:04:37 -070082 }
83}
84
Dan Pasanenfb787d92014-10-06 12:57:20 -050085void restart_unroot_service(int fd, void *cookie)
86{
87 char buf[100];
88
89 if (getuid() != 0) {
90 snprintf(buf, sizeof(buf), "adbd not running as root\n");
91 writex(fd, buf, strlen(buf));
92 adb_close(fd);
93 } else {
94 property_set("service.adb.root", "0");
95 snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
96 writex(fd, buf, strlen(buf));
97 adb_close(fd);
98 }
99}
100
Mike Lockwood26b88e32009-08-24 15:58:40 -0700101void restart_tcp_service(int fd, void *cookie)
102{
103 char buf[100];
104 char value[PROPERTY_VALUE_MAX];
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800105 int port = (int) (uintptr_t) cookie;
Mike Lockwood26b88e32009-08-24 15:58:40 -0700106
107 if (port <= 0) {
108 snprintf(buf, sizeof(buf), "invalid port\n");
109 writex(fd, buf, strlen(buf));
110 adb_close(fd);
111 return;
112 }
113
114 snprintf(value, sizeof(value), "%d", port);
115 property_set("service.adb.tcp.port", value);
116 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
117 writex(fd, buf, strlen(buf));
118 adb_close(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700119}
120
121void restart_usb_service(int fd, void *cookie)
122{
123 char buf[100];
124
125 property_set("service.adb.tcp.port", "0");
126 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
127 writex(fd, buf, strlen(buf));
128 adb_close(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700129}
130
131void reboot_service(int fd, void *arg)
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400132{
133 char buf[100];
Nick Kralevichb7df0172013-04-18 12:20:02 -0700134 char property_val[PROPERTY_VALUE_MAX];
Nick Kralevich5b5283c2014-01-14 16:34:56 -0800135 int ret;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400136
137 sync();
Mike Lockwoodd49e9eb2010-02-24 16:07:23 -0500138
Nick Kralevichb7df0172013-04-18 12:20:02 -0700139 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
140 if (ret >= (int) sizeof(property_val)) {
141 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
142 writex(fd, buf, strlen(buf));
143 goto cleanup;
144 }
145
146 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400147 if (ret < 0) {
Nick Kralevichb7df0172013-04-18 12:20:02 -0700148 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400149 writex(fd, buf, strlen(buf));
Nick Kralevich51699982013-10-24 08:53:48 -0700150 goto cleanup;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400151 }
Nick Kralevich51699982013-10-24 08:53:48 -0700152 // Don't return early. Give the reboot command time to take effect
153 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
154 while(1) { pause(); }
Nick Kralevichb7df0172013-04-18 12:20:02 -0700155cleanup:
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400156 free(arg);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400157 adb_close(fd);
158}
159
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100160void reverse_service(int fd, void* arg)
161{
162 const char* command = arg;
163
164 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
165 sendfailmsg(fd, "not a reverse forwarding command");
166 }
167 free(arg);
168 adb_close(fd);
169}
170
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800171#endif
172
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800173static int create_service_thread(void (*func)(int, void *), void *cookie)
174{
175 stinfo *sti;
176 adb_thread_t t;
177 int s[2];
178
179 if(adb_socketpair(s)) {
180 printf("cannot create service socket pair\n");
181 return -1;
182 }
leozwang1be54622014-08-15 09:51:27 -0700183 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184
185 sti = malloc(sizeof(stinfo));
186 if(sti == 0) fatal("cannot allocate stinfo");
187 sti->func = func;
188 sti->cookie = cookie;
189 sti->fd = s[1];
190
191 if(adb_thread_create( &t, service_bootstrap_func, sti)){
192 free(sti);
193 adb_close(s[0]);
194 adb_close(s[1]);
195 printf("cannot create service thread\n");
196 return -1;
197 }
198
199 D("service thread started, %d:%d\n",s[0], s[1]);
200 return s[0];
201}
202
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700203#if !ADB_HOST
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700204
205static void init_subproc_child()
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206{
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700207 setsid();
208
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700209 // Set OOM score adjustment to prevent killing
Nick Kralevich777523e2014-07-18 20:57:35 -0700210 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700211 if (fd >= 0) {
212 adb_write(fd, "0", 1);
213 adb_close(fd);
214 } else {
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700215 D("adb: unable to update oom_score_adj\n");
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700216 }
217}
218
219static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
220{
221 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cui2fa43212014-11-11 09:24:11 -0800222#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700223 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700224 return -1;
Yabin Cui2fa43212014-11-11 09:24:11 -0800225#else
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800226 int ptm;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800227
Nick Kralevich777523e2014-07-18 20:57:35 -0700228 ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229 if(ptm < 0){
230 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
231 return -1;
232 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800233
Elliott Hughes1f59fa02014-07-29 11:32:16 -0700234 char devname[64];
235 if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700237 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800238 return -1;
239 }
240
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700241 *pid = fork();
242 if(*pid < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700244 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800245 return -1;
246 }
247
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700248 if (*pid == 0) {
249 init_subproc_child();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800250
Nick Kralevich777523e2014-07-18 20:57:35 -0700251 int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700252 if (pts < 0) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700253 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
254 exit(-1);
255 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800256
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700257 dup2(pts, STDIN_FILENO);
258 dup2(pts, STDOUT_FILENO);
259 dup2(pts, STDERR_FILENO);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800260
Benoit Gobyf4ded742011-02-01 18:57:41 -0800261 adb_close(pts);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800262 adb_close(ptm);
263
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700264 execl(cmd, cmd, arg0, arg1, NULL);
265 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
266 cmd, strerror(errno), errno);
267 exit(-1);
268 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800269 return ptm;
270 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800271#endif /* !defined(_WIN32) */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800272}
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700273
274static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
275{
276 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cui2fa43212014-11-11 09:24:11 -0800277#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700278 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
279 return -1;
Yabin Cui2fa43212014-11-11 09:24:11 -0800280#else
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700281
282 // 0 is parent socket, 1 is child socket
283 int sv[2];
leozwang1be54622014-08-15 09:51:27 -0700284 if (adb_socketpair(sv) < 0) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700285 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
286 return -1;
287 }
leozwang1be54622014-08-15 09:51:27 -0700288 D("socketpair: (%d,%d)", sv[0], sv[1]);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700289
290 *pid = fork();
291 if (*pid < 0) {
292 printf("- fork failed: %s -\n", strerror(errno));
293 adb_close(sv[0]);
294 adb_close(sv[1]);
295 return -1;
296 }
297
298 if (*pid == 0) {
299 adb_close(sv[0]);
300 init_subproc_child();
301
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700302 dup2(sv[1], STDIN_FILENO);
303 dup2(sv[1], STDOUT_FILENO);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -0700304 dup2(sv[1], STDERR_FILENO);
305
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700306 adb_close(sv[1]);
307
308 execl(cmd, cmd, arg0, arg1, NULL);
309 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
310 cmd, strerror(errno), errno);
311 exit(-1);
312 } else {
313 adb_close(sv[1]);
314 return sv[0];
315 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800316#endif /* !defined(_WIN32) */
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700317}
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700318#endif /* !ABD_HOST */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800319
320#if ADB_HOST
321#define SHELL_COMMAND "/bin/sh"
322#else
323#define SHELL_COMMAND "/system/bin/sh"
324#endif
325
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700326#if !ADB_HOST
327static void subproc_waiter_service(int fd, void *cookie)
328{
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800329 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700330
331 D("entered. fd=%d of pid=%d\n", fd, pid);
332 for (;;) {
333 int status;
334 pid_t p = waitpid(pid, &status, 0);
335 if (p == pid) {
336 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
337 if (WIFSIGNALED(status)) {
338 D("*** Killed by signal %d\n", WTERMSIG(status));
339 break;
340 } else if (!WIFEXITED(status)) {
341 D("*** Didn't exit!!. status %d\n", status);
342 break;
343 } else if (WEXITSTATUS(status) >= 0) {
344 D("*** Exit code %d\n", WEXITSTATUS(status));
345 break;
346 }
347 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700348 }
349 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
350 if (SHELL_EXIT_NOTIFY_FD >=0) {
351 int res;
352 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
353 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
354 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
355 }
356}
357
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700358static int create_subproc_thread(const char *name, const subproc_mode mode)
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700359{
360 stinfo *sti;
361 adb_thread_t t;
362 int ret_fd;
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700363 pid_t pid = -1;
364
365 const char *arg0, *arg1;
366 if (name == 0 || *name == 0) {
367 arg0 = "-"; arg1 = 0;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700368 } else {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700369 arg0 = "-c"; arg1 = name;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700370 }
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700371
372 switch (mode) {
373 case SUBPROC_PTY:
374 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
375 break;
376 case SUBPROC_RAW:
377 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
378 break;
379 default:
380 fprintf(stderr, "invalid subproc_mode %d\n", mode);
381 return -1;
382 }
383 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700384
385 sti = malloc(sizeof(stinfo));
386 if(sti == 0) fatal("cannot allocate stinfo");
387 sti->func = subproc_waiter_service;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800388 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700389 sti->fd = ret_fd;
390
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700391 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700392 free(sti);
393 adb_close(ret_fd);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700394 fprintf(stderr, "cannot create service thread\n");
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700395 return -1;
396 }
397
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700398 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700399 return ret_fd;
400}
401#endif
402
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800403int service_to_fd(const char *name)
404{
405 int ret = -1;
406
407 if(!strncmp(name, "tcp:", 4)) {
408 int port = atoi(name + 4);
409 name = strchr(name + 4, ':');
410 if(name == 0) {
411 ret = socket_loopback_client(port, SOCK_STREAM);
412 if (ret >= 0)
413 disable_tcp_nagle(ret);
414 } else {
415#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800416 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417#else
418 return -1;
419#endif
420 }
421#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
422 } else if(!strncmp(name, "local:", 6)) {
423 ret = socket_local_client(name + 6,
424 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
425 } else if(!strncmp(name, "localreserved:", 14)) {
426 ret = socket_local_client(name + 14,
427 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
428 } else if(!strncmp(name, "localabstract:", 14)) {
429 ret = socket_local_client(name + 14,
430 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
431 } else if(!strncmp(name, "localfilesystem:", 16)) {
432 ret = socket_local_client(name + 16,
433 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
434#endif
Benoit Goby12dc3692013-02-20 15:04:53 -0800435#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800436 } else if(!strncmp("dev:", name, 4)) {
Nick Kralevich777523e2014-07-18 20:57:35 -0700437 ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800438 } else if(!strncmp(name, "framebuffer:", 12)) {
439 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800440 } else if (!strncmp(name, "jdwp:", 5)) {
441 ret = create_jdwp_connection_fd(atoi(name+5));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800442 } else if(!HOST && !strncmp(name, "shell:", 6)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700443 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
444 } else if(!HOST && !strncmp(name, "exec:", 5)) {
445 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800446 } else if(!strncmp(name, "sync:", 5)) {
447 ret = create_service_thread(file_sync_service, NULL);
448 } else if(!strncmp(name, "remount:", 8)) {
449 ret = create_service_thread(remount_service, NULL);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400450 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400451 void* arg = strdup(name + 7);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700452 if (arg == NULL) return -1;
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400453 ret = create_service_thread(reboot_service, arg);
The Android Open Source Project9c753402009-03-13 13:04:37 -0700454 } else if(!strncmp(name, "root:", 5)) {
455 ret = create_service_thread(restart_root_service, NULL);
Dan Pasanenfb787d92014-10-06 12:57:20 -0500456 } else if(!strncmp(name, "unroot:", 7)) {
457 ret = create_service_thread(restart_unroot_service, NULL);
Christopher Tate73779122011-04-21 12:53:28 -0700458 } else if(!strncmp(name, "backup:", 7)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700459 char* arg = strdup(name + 7);
Christopher Tate73779122011-04-21 12:53:28 -0700460 if (arg == NULL) return -1;
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700461 char* c = arg;
462 for (; *c != '\0'; c++) {
463 if (*c == ':')
464 *c = ' ';
465 }
466 char* cmd;
467 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
468 ret = create_subproc_thread(cmd, SUBPROC_RAW);
469 free(cmd);
470 }
471 free(arg);
Christopher Tatecf5379b2011-05-17 15:52:54 -0700472 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700473 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700474 } else if(!strncmp(name, "tcpip:", 6)) {
475 int port;
Spencer Low29a029c2015-01-25 17:38:36 -0800476 if (sscanf(name + 6, "%d", &port) != 1) {
Mike Lockwood26b88e32009-08-24 15:58:40 -0700477 port = 0;
478 }
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800479 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700480 } else if(!strncmp(name, "usb:", 4)) {
481 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100482 } else if (!strncmp(name, "reverse:", 8)) {
483 char* cookie = strdup(name + 8);
484 if (cookie == NULL) {
485 ret = -1;
486 } else {
487 ret = create_service_thread(reverse_service, cookie);
488 if (ret < 0) {
489 free(cookie);
490 }
491 }
Paul Lawrence5f356482014-10-09 14:22:49 +0000492 } else if(!strncmp(name, "disable-verity:", 15)) {
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800493 ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
494 } else if(!strncmp(name, "enable-verity:", 15)) {
495 ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800496#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800497 }
498 if (ret >= 0) {
499 close_on_exec(ret);
500 }
501 return ret;
502}
503
504#if ADB_HOST
505struct state_info {
506 transport_type transport;
507 char* serial;
508 int state;
509};
510
511static void wait_for_state(int fd, void* cookie)
512{
513 struct state_info* sinfo = cookie;
514 char* err = "unknown error";
515
516 D("wait_for_state %d\n", sinfo->state);
517
518 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
519 if(t != 0) {
520 writex(fd, "OKAY", 4);
521 } else {
522 sendfailmsg(fd, err);
523 }
524
525 if (sinfo->serial)
526 free(sinfo->serial);
527 free(sinfo);
528 adb_close(fd);
529 D("wait_for_state is done\n");
530}
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700531
532static void connect_device(char* host, char* buffer, int buffer_size)
533{
534 int port, fd;
535 char* portstr = strchr(host, ':');
536 char hostbuf[100];
537 char serial[100];
538 int ret;
539
540 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
541 if (portstr) {
542 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
543 snprintf(buffer, buffer_size, "bad host name %s", host);
544 return;
545 }
546 // zero terminate the host at the point we found the colon
547 hostbuf[portstr - host] = 0;
Spencer Low29a029c2015-01-25 17:38:36 -0800548 if (sscanf(portstr + 1, "%d", &port) != 1) {
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700549 snprintf(buffer, buffer_size, "bad port number %s", portstr);
550 return;
551 }
552 } else {
553 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
554 }
555
556 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
557
Ken Liermane6cd0c82013-06-20 09:27:13 -0700558 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700559 if (fd < 0) {
560 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
561 return;
562 }
563
564 D("client: connected on remote on fd %d\n", fd);
565 close_on_exec(fd);
566 disable_tcp_nagle(fd);
567
568 ret = register_socket_transport(fd, serial, port, 0);
569 if (ret < 0) {
570 adb_close(fd);
571 snprintf(buffer, buffer_size, "already connected to %s", serial);
572 } else {
573 snprintf(buffer, buffer_size, "connected to %s", serial);
574 }
575}
576
577void connect_emulator(char* port_spec, char* buffer, int buffer_size)
578{
579 char* port_separator = strchr(port_spec, ',');
580 if (!port_separator) {
581 snprintf(buffer, buffer_size,
582 "unable to parse '%s' as <console port>,<adb port>",
583 port_spec);
584 return;
585 }
586
587 // Zero-terminate console port and make port_separator point to 2nd port.
588 *port_separator++ = 0;
589 int console_port = strtol(port_spec, NULL, 0);
590 int adb_port = strtol(port_separator, NULL, 0);
591 if (!(console_port > 0 && adb_port > 0)) {
592 *(port_separator - 1) = ',';
593 snprintf(buffer, buffer_size,
594 "Invalid port numbers: Expected positive numbers, got '%s'",
595 port_spec);
596 return;
597 }
598
599 /* Check if the emulator is already known.
600 * Note: There's a small but harmless race condition here: An emulator not
601 * present just yet could be registered by another invocation right
602 * after doing this check here. However, local_connect protects
603 * against double-registration too. From here, a better error message
604 * can be produced. In the case of the race condition, the very specific
605 * error message won't be shown, but the data doesn't get corrupted. */
606 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
607 if (known_emulator != NULL) {
608 snprintf(buffer, buffer_size,
609 "Emulator on port %d already registered.", adb_port);
610 return;
611 }
612
613 /* Check if more emulators can be registered. Similar unproblematic
614 * race condition as above. */
615 int candidate_slot = get_available_local_transport_index();
616 if (candidate_slot < 0) {
617 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
618 return;
619 }
620
621 /* Preconditions met, try to connect to the emulator. */
622 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
623 snprintf(buffer, buffer_size,
624 "Connected to emulator on ports %d,%d", console_port, adb_port);
625 } else {
626 snprintf(buffer, buffer_size,
627 "Could not connect to emulator on ports %d,%d",
628 console_port, adb_port);
629 }
630}
631
632static void connect_service(int fd, void* cookie)
633{
634 char buf[4096];
635 char resp[4096];
636 char *host = cookie;
637
638 if (!strncmp(host, "emu:", 4)) {
639 connect_emulator(host + 4, buf, sizeof(buf));
640 } else {
641 connect_device(host, buf, sizeof(buf));
642 }
643
644 // Send response for emulator and device
645 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
646 writex(fd, resp, strlen(resp));
647 adb_close(fd);
648}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800649#endif
650
651#if ADB_HOST
652asocket* host_service_to_socket(const char* name, const char *serial)
653{
654 if (!strcmp(name,"track-devices")) {
655 return create_device_tracker();
656 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
657 struct state_info* sinfo = malloc(sizeof(struct state_info));
658
659 if (serial)
660 sinfo->serial = strdup(serial);
661 else
662 sinfo->serial = NULL;
663
664 name += strlen("wait-for-");
665
666 if (!strncmp(name, "local", strlen("local"))) {
667 sinfo->transport = kTransportLocal;
668 sinfo->state = CS_DEVICE;
669 } else if (!strncmp(name, "usb", strlen("usb"))) {
670 sinfo->transport = kTransportUsb;
671 sinfo->state = CS_DEVICE;
672 } else if (!strncmp(name, "any", strlen("any"))) {
673 sinfo->transport = kTransportAny;
674 sinfo->state = CS_DEVICE;
675 } else {
676 free(sinfo);
677 return NULL;
678 }
679
680 int fd = create_service_thread(wait_for_state, sinfo);
681 return create_local_socket(fd);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700682 } else if (!strncmp(name, "connect:", 8)) {
683 const char *host = name + 8;
684 int fd = create_service_thread(connect_service, (void *)host);
685 return create_local_socket(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800686 }
687 return NULL;
688}
689#endif /* ADB_HOST */