blob: 84f2137da6994484e91cf323bc6e66340ca91e98 [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017#include <errno.h>
Dan Albert9815c9f2015-02-24 15:51:19 -080018#include <stddef.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#ifndef _WIN32
24#include <netdb.h>
25#include <netinet/in.h>
26#include <sys/ioctl.h>
27#include <unistd.h>
28#endif
29
30#if !ADB_HOST
31#include "cutils/android_reboot.h"
32#include "cutils/properties.h"
33#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35#include "sysdeps.h"
36
JP Abgrall408fa572011-03-16 15:57:42 -070037#define TRACE_TAG TRACE_SERVICES
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#include "adb.h"
Dan Albert31c5c4b2015-02-24 21:26:58 -080039#include "adb_io.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include "file_sync_service.h"
Dan Albert9815c9f2015-02-24 15:51:19 -080041#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43typedef struct stinfo stinfo;
44
45struct stinfo {
46 void (*func)(int fd, void *cookie);
47 int fd;
48 void *cookie;
49};
50
51
52void *service_bootstrap_func(void *x)
53{
54 stinfo *sti = x;
55 sti->func(sti->fd, sti->cookie);
56 free(sti);
57 return 0;
58}
59
Benoit Goby9470c2f2013-02-20 15:04:53 -080060#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070062void restart_root_service(int fd, void *cookie)
63{
64 char buf[100];
65 char value[PROPERTY_VALUE_MAX];
66
67 if (getuid() == 0) {
68 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
Dan Albert31c5c4b2015-02-24 21:26:58 -080069 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070070 adb_close(fd);
71 } else {
72 property_get("ro.debuggable", value, "");
73 if (strcmp(value, "1") != 0) {
74 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
Dan Albert31c5c4b2015-02-24 21:26:58 -080075 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -070076 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070077 return;
78 }
79
Benoit Goby7941cf82012-03-16 14:44:17 -070080 property_set("service.adb.root", "1");
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070081 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
Dan Albert31c5c4b2015-02-24 21:26:58 -080082 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070083 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070084 }
85}
86
Dan Pasanenb7d9ef32014-10-06 12:57:20 -050087void restart_unroot_service(int fd, void *cookie)
88{
89 char buf[100];
90
91 if (getuid() != 0) {
92 snprintf(buf, sizeof(buf), "adbd not running as root\n");
Dan Albert31c5c4b2015-02-24 21:26:58 -080093 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanenb7d9ef32014-10-06 12:57:20 -050094 adb_close(fd);
95 } else {
96 property_set("service.adb.root", "0");
97 snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
Dan Albert31c5c4b2015-02-24 21:26:58 -080098 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanenb7d9ef32014-10-06 12:57:20 -050099 adb_close(fd);
100 }
101}
102
Mike Lockwoodff196702009-08-24 15:58:40 -0700103void restart_tcp_service(int fd, void *cookie)
104{
105 char buf[100];
106 char value[PROPERTY_VALUE_MAX];
Elliott Hughesccecf142014-01-16 10:53:11 -0800107 int port = (int) (uintptr_t) cookie;
Mike Lockwoodff196702009-08-24 15:58:40 -0700108
109 if (port <= 0) {
110 snprintf(buf, sizeof(buf), "invalid port\n");
Dan Albert31c5c4b2015-02-24 21:26:58 -0800111 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -0700112 adb_close(fd);
113 return;
114 }
115
116 snprintf(value, sizeof(value), "%d", port);
117 property_set("service.adb.tcp.port", value);
118 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
Dan Albert31c5c4b2015-02-24 21:26:58 -0800119 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -0700120 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700121}
122
123void restart_usb_service(int fd, void *cookie)
124{
125 char buf[100];
126
127 property_set("service.adb.tcp.port", "0");
128 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
Dan Albert31c5c4b2015-02-24 21:26:58 -0800129 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -0700130 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700131}
132
133void reboot_service(int fd, void *arg)
Mike Lockwoodee156622009-08-04 20:37:51 -0400134{
135 char buf[100];
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700136 char property_val[PROPERTY_VALUE_MAX];
Nick Kralevich02916aa2014-01-14 16:34:56 -0800137 int ret;
Mike Lockwoodee156622009-08-04 20:37:51 -0400138
139 sync();
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500140
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700141 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
142 if (ret >= (int) sizeof(property_val)) {
143 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
Dan Albert31c5c4b2015-02-24 21:26:58 -0800144 WriteFdExactly(fd, buf, strlen(buf));
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700145 goto cleanup;
146 }
147
148 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwoodee156622009-08-04 20:37:51 -0400149 if (ret < 0) {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700150 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Dan Albert31c5c4b2015-02-24 21:26:58 -0800151 WriteFdExactly(fd, buf, strlen(buf));
Nick Kralevich91704522013-10-24 08:53:48 -0700152 goto cleanup;
Mike Lockwoodee156622009-08-04 20:37:51 -0400153 }
Nick Kralevich91704522013-10-24 08:53:48 -0700154 // Don't return early. Give the reboot command time to take effect
155 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
156 while(1) { pause(); }
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700157cleanup:
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400158 free(arg);
Mike Lockwoodee156622009-08-04 20:37:51 -0400159 adb_close(fd);
160}
161
David 'Digit' Turner25258692013-03-21 21:07:42 +0100162void reverse_service(int fd, void* arg)
163{
164 const char* command = arg;
165
166 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
167 sendfailmsg(fd, "not a reverse forwarding command");
168 }
169 free(arg);
170 adb_close(fd);
171}
172
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173#endif
174
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175static int create_service_thread(void (*func)(int, void *), void *cookie)
176{
177 stinfo *sti;
178 adb_thread_t t;
179 int s[2];
180
181 if(adb_socketpair(s)) {
182 printf("cannot create service socket pair\n");
183 return -1;
184 }
leozwangcbf02672014-08-15 09:51:27 -0700185 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
187 sti = malloc(sizeof(stinfo));
188 if(sti == 0) fatal("cannot allocate stinfo");
189 sti->func = func;
190 sti->cookie = cookie;
191 sti->fd = s[1];
192
193 if(adb_thread_create( &t, service_bootstrap_func, sti)){
194 free(sti);
195 adb_close(s[0]);
196 adb_close(s[1]);
197 printf("cannot create service thread\n");
198 return -1;
199 }
200
201 D("service thread started, %d:%d\n",s[0], s[1]);
202 return s[0];
203}
204
JP Abgrall408fa572011-03-16 15:57:42 -0700205#if !ADB_HOST
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700206
207static void init_subproc_child()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208{
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700209 setsid();
210
Rom Lemarchand07ce7ca2014-06-20 16:30:54 -0700211 // Set OOM score adjustment to prevent killing
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700212 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700213 if (fd >= 0) {
214 adb_write(fd, "0", 1);
215 adb_close(fd);
216 } else {
Rom Lemarchand07ce7ca2014-06-20 16:30:54 -0700217 D("adb: unable to update oom_score_adj\n");
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700218 }
219}
220
221static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
222{
223 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cuie77b6a02014-11-11 09:24:11 -0800224#if defined(_WIN32)
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700225 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
JP Abgrall408fa572011-03-16 15:57:42 -0700226 return -1;
Yabin Cuie77b6a02014-11-11 09:24:11 -0800227#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 int ptm;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700230 ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 if(ptm < 0){
232 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
233 return -1;
234 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235
Elliott Hughesd2352882014-07-29 11:32:16 -0700236 char devname[64];
237 if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700239 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 return -1;
241 }
242
JP Abgrall408fa572011-03-16 15:57:42 -0700243 *pid = fork();
244 if(*pid < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700246 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 return -1;
248 }
249
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700250 if (*pid == 0) {
251 init_subproc_child();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700253 int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700254 if (pts < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -0700255 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
256 exit(-1);
257 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700259 dup2(pts, STDIN_FILENO);
260 dup2(pts, STDOUT_FILENO);
261 dup2(pts, STDERR_FILENO);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262
Benoit Goby95ef8282011-02-01 18:57:41 -0800263 adb_close(pts);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 adb_close(ptm);
265
JP Abgrall408fa572011-03-16 15:57:42 -0700266 execl(cmd, cmd, arg0, arg1, NULL);
267 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
268 cmd, strerror(errno), errno);
269 exit(-1);
270 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 return ptm;
272 }
Yabin Cuie77b6a02014-11-11 09:24:11 -0800273#endif /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274}
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700275
276static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
277{
278 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cuie77b6a02014-11-11 09:24:11 -0800279#if defined(_WIN32)
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700280 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
281 return -1;
Yabin Cuie77b6a02014-11-11 09:24:11 -0800282#else
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700283
284 // 0 is parent socket, 1 is child socket
285 int sv[2];
leozwangcbf02672014-08-15 09:51:27 -0700286 if (adb_socketpair(sv) < 0) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700287 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
288 return -1;
289 }
leozwangcbf02672014-08-15 09:51:27 -0700290 D("socketpair: (%d,%d)", sv[0], sv[1]);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700291
292 *pid = fork();
293 if (*pid < 0) {
294 printf("- fork failed: %s -\n", strerror(errno));
295 adb_close(sv[0]);
296 adb_close(sv[1]);
297 return -1;
298 }
299
300 if (*pid == 0) {
301 adb_close(sv[0]);
302 init_subproc_child();
303
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700304 dup2(sv[1], STDIN_FILENO);
305 dup2(sv[1], STDOUT_FILENO);
Jeff Sharkey960df972014-06-09 17:30:57 -0700306 dup2(sv[1], STDERR_FILENO);
307
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700308 adb_close(sv[1]);
309
310 execl(cmd, cmd, arg0, arg1, NULL);
311 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
312 cmd, strerror(errno), errno);
313 exit(-1);
314 } else {
315 adb_close(sv[1]);
316 return sv[0];
317 }
Yabin Cuie77b6a02014-11-11 09:24:11 -0800318#endif /* !defined(_WIN32) */
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700319}
JP Abgrall408fa572011-03-16 15:57:42 -0700320#endif /* !ABD_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321
322#if ADB_HOST
323#define SHELL_COMMAND "/bin/sh"
324#else
325#define SHELL_COMMAND "/system/bin/sh"
326#endif
327
JP Abgrall408fa572011-03-16 15:57:42 -0700328#if !ADB_HOST
329static void subproc_waiter_service(int fd, void *cookie)
330{
Elliott Hughesccecf142014-01-16 10:53:11 -0800331 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall408fa572011-03-16 15:57:42 -0700332
333 D("entered. fd=%d of pid=%d\n", fd, pid);
334 for (;;) {
335 int status;
336 pid_t p = waitpid(pid, &status, 0);
337 if (p == pid) {
338 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
339 if (WIFSIGNALED(status)) {
340 D("*** Killed by signal %d\n", WTERMSIG(status));
341 break;
342 } else if (!WIFEXITED(status)) {
343 D("*** Didn't exit!!. status %d\n", status);
344 break;
345 } else if (WEXITSTATUS(status) >= 0) {
346 D("*** Exit code %d\n", WEXITSTATUS(status));
347 break;
348 }
349 }
JP Abgrall408fa572011-03-16 15:57:42 -0700350 }
351 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
352 if (SHELL_EXIT_NOTIFY_FD >=0) {
353 int res;
Dan Albert31c5c4b2015-02-24 21:26:58 -0800354 res = WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)) ? 0 : -1;
JP Abgrall408fa572011-03-16 15:57:42 -0700355 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
356 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
357 }
358}
359
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700360static int create_subproc_thread(const char *name, const subproc_mode mode)
JP Abgrall408fa572011-03-16 15:57:42 -0700361{
362 stinfo *sti;
363 adb_thread_t t;
364 int ret_fd;
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700365 pid_t pid = -1;
366
367 const char *arg0, *arg1;
368 if (name == 0 || *name == 0) {
369 arg0 = "-"; arg1 = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700370 } else {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700371 arg0 = "-c"; arg1 = name;
JP Abgrall408fa572011-03-16 15:57:42 -0700372 }
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700373
374 switch (mode) {
375 case SUBPROC_PTY:
376 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
377 break;
378 case SUBPROC_RAW:
379 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
380 break;
381 default:
382 fprintf(stderr, "invalid subproc_mode %d\n", mode);
383 return -1;
384 }
385 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall408fa572011-03-16 15:57:42 -0700386
387 sti = malloc(sizeof(stinfo));
388 if(sti == 0) fatal("cannot allocate stinfo");
389 sti->func = subproc_waiter_service;
Elliott Hughesccecf142014-01-16 10:53:11 -0800390 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall408fa572011-03-16 15:57:42 -0700391 sti->fd = ret_fd;
392
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700393 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
JP Abgrall408fa572011-03-16 15:57:42 -0700394 free(sti);
395 adb_close(ret_fd);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700396 fprintf(stderr, "cannot create service thread\n");
JP Abgrall408fa572011-03-16 15:57:42 -0700397 return -1;
398 }
399
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700400 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall408fa572011-03-16 15:57:42 -0700401 return ret_fd;
402}
403#endif
404
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405int service_to_fd(const char *name)
406{
407 int ret = -1;
408
409 if(!strncmp(name, "tcp:", 4)) {
410 int port = atoi(name + 4);
411 name = strchr(name + 4, ':');
412 if(name == 0) {
413 ret = socket_loopback_client(port, SOCK_STREAM);
414 if (ret >= 0)
415 disable_tcp_nagle(ret);
416 } else {
417#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419#else
420 return -1;
421#endif
422 }
423#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
424 } else if(!strncmp(name, "local:", 6)) {
425 ret = socket_local_client(name + 6,
426 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
427 } else if(!strncmp(name, "localreserved:", 14)) {
428 ret = socket_local_client(name + 14,
429 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
430 } else if(!strncmp(name, "localabstract:", 14)) {
431 ret = socket_local_client(name + 14,
432 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
433 } else if(!strncmp(name, "localfilesystem:", 16)) {
434 ret = socket_local_client(name + 16,
435 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
436#endif
Benoit Goby9470c2f2013-02-20 15:04:53 -0800437#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 } else if(!strncmp("dev:", name, 4)) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700439 ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 } else if(!strncmp(name, "framebuffer:", 12)) {
441 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 } else if (!strncmp(name, "jdwp:", 5)) {
443 ret = create_jdwp_connection_fd(atoi(name+5));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 } else if(!HOST && !strncmp(name, "shell:", 6)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700445 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
446 } else if(!HOST && !strncmp(name, "exec:", 5)) {
447 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 } else if(!strncmp(name, "sync:", 5)) {
449 ret = create_service_thread(file_sync_service, NULL);
450 } else if(!strncmp(name, "remount:", 8)) {
451 ret = create_service_thread(remount_service, NULL);
Mike Lockwoodee156622009-08-04 20:37:51 -0400452 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400453 void* arg = strdup(name + 7);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700454 if (arg == NULL) return -1;
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400455 ret = create_service_thread(reboot_service, arg);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700456 } else if(!strncmp(name, "root:", 5)) {
457 ret = create_service_thread(restart_root_service, NULL);
Dan Pasanenb7d9ef32014-10-06 12:57:20 -0500458 } else if(!strncmp(name, "unroot:", 7)) {
459 ret = create_service_thread(restart_unroot_service, NULL);
Christopher Tated2f54152011-04-21 12:53:28 -0700460 } else if(!strncmp(name, "backup:", 7)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700461 char* arg = strdup(name + 7);
Christopher Tated2f54152011-04-21 12:53:28 -0700462 if (arg == NULL) return -1;
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700463 char* c = arg;
464 for (; *c != '\0'; c++) {
465 if (*c == ':')
466 *c = ' ';
467 }
468 char* cmd;
469 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
470 ret = create_subproc_thread(cmd, SUBPROC_RAW);
471 free(cmd);
472 }
473 free(arg);
Christopher Tate702967a2011-05-17 15:52:54 -0700474 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700475 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwoodff196702009-08-24 15:58:40 -0700476 } else if(!strncmp(name, "tcpip:", 6)) {
477 int port;
Spencer Low943ef232015-01-25 17:38:36 -0800478 if (sscanf(name + 6, "%d", &port) != 1) {
Mike Lockwoodff196702009-08-24 15:58:40 -0700479 port = 0;
480 }
Elliott Hughesccecf142014-01-16 10:53:11 -0800481 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwoodff196702009-08-24 15:58:40 -0700482 } else if(!strncmp(name, "usb:", 4)) {
483 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100484 } else if (!strncmp(name, "reverse:", 8)) {
485 char* cookie = strdup(name + 8);
486 if (cookie == NULL) {
487 ret = -1;
488 } else {
489 ret = create_service_thread(reverse_service, cookie);
490 if (ret < 0) {
491 free(cookie);
492 }
493 }
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000494 } else if(!strncmp(name, "disable-verity:", 15)) {
Paul Lawrence982089d2014-12-03 15:31:57 -0800495 ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
496 } else if(!strncmp(name, "enable-verity:", 15)) {
497 ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 }
500 if (ret >= 0) {
501 close_on_exec(ret);
502 }
503 return ret;
504}
505
506#if ADB_HOST
507struct state_info {
508 transport_type transport;
509 char* serial;
510 int state;
511};
512
513static void wait_for_state(int fd, void* cookie)
514{
515 struct state_info* sinfo = cookie;
516 char* err = "unknown error";
517
518 D("wait_for_state %d\n", sinfo->state);
519
520 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
521 if(t != 0) {
Dan Albert31c5c4b2015-02-24 21:26:58 -0800522 WriteFdExactly(fd, "OKAY", 4);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 } else {
524 sendfailmsg(fd, err);
525 }
526
527 if (sinfo->serial)
528 free(sinfo->serial);
529 free(sinfo);
530 adb_close(fd);
531 D("wait_for_state is done\n");
532}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700533
534static void connect_device(char* host, char* buffer, int buffer_size)
535{
536 int port, fd;
537 char* portstr = strchr(host, ':');
538 char hostbuf[100];
539 char serial[100];
540 int ret;
541
542 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
543 if (portstr) {
544 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
545 snprintf(buffer, buffer_size, "bad host name %s", host);
546 return;
547 }
548 // zero terminate the host at the point we found the colon
549 hostbuf[portstr - host] = 0;
Spencer Low943ef232015-01-25 17:38:36 -0800550 if (sscanf(portstr + 1, "%d", &port) != 1) {
Benoit Goby1c45ee92013-03-29 18:22:36 -0700551 snprintf(buffer, buffer_size, "bad port number %s", portstr);
552 return;
553 }
554 } else {
555 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
556 }
557
558 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
559
Ken Liermanaecc6a62013-06-20 09:27:13 -0700560 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700561 if (fd < 0) {
562 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
563 return;
564 }
565
566 D("client: connected on remote on fd %d\n", fd);
567 close_on_exec(fd);
568 disable_tcp_nagle(fd);
569
570 ret = register_socket_transport(fd, serial, port, 0);
571 if (ret < 0) {
572 adb_close(fd);
573 snprintf(buffer, buffer_size, "already connected to %s", serial);
574 } else {
575 snprintf(buffer, buffer_size, "connected to %s", serial);
576 }
577}
578
579void connect_emulator(char* port_spec, char* buffer, int buffer_size)
580{
581 char* port_separator = strchr(port_spec, ',');
582 if (!port_separator) {
583 snprintf(buffer, buffer_size,
584 "unable to parse '%s' as <console port>,<adb port>",
585 port_spec);
586 return;
587 }
588
589 // Zero-terminate console port and make port_separator point to 2nd port.
590 *port_separator++ = 0;
591 int console_port = strtol(port_spec, NULL, 0);
592 int adb_port = strtol(port_separator, NULL, 0);
593 if (!(console_port > 0 && adb_port > 0)) {
594 *(port_separator - 1) = ',';
595 snprintf(buffer, buffer_size,
596 "Invalid port numbers: Expected positive numbers, got '%s'",
597 port_spec);
598 return;
599 }
600
601 /* Check if the emulator is already known.
602 * Note: There's a small but harmless race condition here: An emulator not
603 * present just yet could be registered by another invocation right
604 * after doing this check here. However, local_connect protects
605 * against double-registration too. From here, a better error message
606 * can be produced. In the case of the race condition, the very specific
607 * error message won't be shown, but the data doesn't get corrupted. */
608 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
609 if (known_emulator != NULL) {
610 snprintf(buffer, buffer_size,
611 "Emulator on port %d already registered.", adb_port);
612 return;
613 }
614
615 /* Check if more emulators can be registered. Similar unproblematic
616 * race condition as above. */
617 int candidate_slot = get_available_local_transport_index();
618 if (candidate_slot < 0) {
619 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
620 return;
621 }
622
623 /* Preconditions met, try to connect to the emulator. */
624 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
625 snprintf(buffer, buffer_size,
626 "Connected to emulator on ports %d,%d", console_port, adb_port);
627 } else {
628 snprintf(buffer, buffer_size,
629 "Could not connect to emulator on ports %d,%d",
630 console_port, adb_port);
631 }
632}
633
634static void connect_service(int fd, void* cookie)
635{
636 char buf[4096];
637 char resp[4096];
638 char *host = cookie;
639
640 if (!strncmp(host, "emu:", 4)) {
641 connect_emulator(host + 4, buf, sizeof(buf));
642 } else {
643 connect_device(host, buf, sizeof(buf));
644 }
645
646 // Send response for emulator and device
647 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
Dan Albert31c5c4b2015-02-24 21:26:58 -0800648 WriteFdExactly(fd, resp, strlen(resp));
Benoit Goby1c45ee92013-03-29 18:22:36 -0700649 adb_close(fd);
650}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651#endif
652
653#if ADB_HOST
654asocket* host_service_to_socket(const char* name, const char *serial)
655{
656 if (!strcmp(name,"track-devices")) {
657 return create_device_tracker();
658 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
659 struct state_info* sinfo = malloc(sizeof(struct state_info));
660
661 if (serial)
662 sinfo->serial = strdup(serial);
663 else
664 sinfo->serial = NULL;
665
666 name += strlen("wait-for-");
667
668 if (!strncmp(name, "local", strlen("local"))) {
669 sinfo->transport = kTransportLocal;
670 sinfo->state = CS_DEVICE;
671 } else if (!strncmp(name, "usb", strlen("usb"))) {
672 sinfo->transport = kTransportUsb;
673 sinfo->state = CS_DEVICE;
674 } else if (!strncmp(name, "any", strlen("any"))) {
675 sinfo->transport = kTransportAny;
676 sinfo->state = CS_DEVICE;
677 } else {
678 free(sinfo);
679 return NULL;
680 }
681
682 int fd = create_service_thread(wait_for_state, sinfo);
683 return create_local_socket(fd);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700684 } else if (!strncmp(name, "connect:", 8)) {
685 const char *host = name + 8;
686 int fd = create_service_thread(connect_service, (void *)host);
687 return create_local_socket(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 }
689 return NULL;
690}
691#endif /* ADB_HOST */