blob: 600eb30e187698f40ea76bf54dfb1e8b742299a8 [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
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080017#include <errno.h>
Dan Albertb302d122015-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 Project9ca14dc2009-03-03 19:32:55 -080034
35#include "sysdeps.h"
36
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070037#define TRACE_TAG TRACE_SERVICES
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080038#include "adb.h"
Dan Albert66a91b02015-02-24 21:26:58 -080039#include "adb_io.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080040#include "file_sync_service.h"
Dan Albertb302d122015-02-24 15:51:19 -080041#include "transport.h"
The Android Open Source Project9ca14dc2009-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{
Dan Albertf30d73c2015-02-25 17:51:28 -080054 stinfo* sti = reinterpret_cast<stinfo*>(x);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080055 sti->func(sti->fd, sti->cookie);
56 free(sti);
57 return 0;
58}
59
Benoit Goby12dc3692013-02-20 15:04:53 -080060#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080061
The Android Open Source Project9c753402009-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 Albert66a91b02015-02-24 21:26:58 -080069 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Project9c753402009-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 Albert66a91b02015-02-24 21:26:58 -080075 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -070076 adb_close(fd);
The Android Open Source Project9c753402009-03-13 13:04:37 -070077 return;
78 }
79
Benoit Gobyad926c12012-03-16 14:44:17 -070080 property_set("service.adb.root", "1");
The Android Open Source Project9c753402009-03-13 13:04:37 -070081 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
Dan Albert66a91b02015-02-24 21:26:58 -080082 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Project9c753402009-03-13 13:04:37 -070083 adb_close(fd);
The Android Open Source Project9c753402009-03-13 13:04:37 -070084 }
85}
86
Dan Pasanenfb787d92014-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 Albert66a91b02015-02-24 21:26:58 -080093 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanenfb787d92014-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 Albert66a91b02015-02-24 21:26:58 -080098 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanenfb787d92014-10-06 12:57:20 -050099 adb_close(fd);
100 }
101}
102
Mike Lockwood26b88e32009-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 Hughes9c0d9402014-01-16 10:53:11 -0800107 int port = (int) (uintptr_t) cookie;
Mike Lockwood26b88e32009-08-24 15:58:40 -0700108
109 if (port <= 0) {
110 snprintf(buf, sizeof(buf), "invalid port\n");
Dan Albert66a91b02015-02-24 21:26:58 -0800111 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-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 Albert66a91b02015-02-24 21:26:58 -0800119 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -0700120 adb_close(fd);
Mike Lockwood26b88e32009-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 Albert66a91b02015-02-24 21:26:58 -0800129 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -0700130 adb_close(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700131}
132
133void reboot_service(int fd, void *arg)
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400134{
135 char buf[100];
Nick Kralevichb7df0172013-04-18 12:20:02 -0700136 char property_val[PROPERTY_VALUE_MAX];
Nick Kralevich5b5283c2014-01-14 16:34:56 -0800137 int ret;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400138
139 sync();
Mike Lockwoodd49e9eb2010-02-24 16:07:23 -0500140
Nick Kralevichb7df0172013-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 Albert66a91b02015-02-24 21:26:58 -0800144 WriteFdExactly(fd, buf, strlen(buf));
Nick Kralevichb7df0172013-04-18 12:20:02 -0700145 goto cleanup;
146 }
147
148 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400149 if (ret < 0) {
Nick Kralevichb7df0172013-04-18 12:20:02 -0700150 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Dan Albert66a91b02015-02-24 21:26:58 -0800151 WriteFdExactly(fd, buf, strlen(buf));
Nick Kralevich51699982013-10-24 08:53:48 -0700152 goto cleanup;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400153 }
Nick Kralevich51699982013-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 Kralevichb7df0172013-04-18 12:20:02 -0700157cleanup:
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400158 free(arg);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400159 adb_close(fd);
160}
161
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100162void reverse_service(int fd, void* arg)
163{
Dan Albertf30d73c2015-02-25 17:51:28 -0800164 const char* command = reinterpret_cast<const char*>(arg);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100165
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 Project9ca14dc2009-03-03 19:32:55 -0800173#endif
174
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800175static int create_service_thread(void (*func)(int, void *), void *cookie)
176{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800177 int s[2];
Dan Albertf30d73c2015-02-25 17:51:28 -0800178 if (adb_socketpair(s)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800179 printf("cannot create service socket pair\n");
180 return -1;
181 }
leozwang1be54622014-08-15 09:51:27 -0700182 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183
Dan Albertf30d73c2015-02-25 17:51:28 -0800184 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
185 if (sti == nullptr) {
186 fatal("cannot allocate stinfo");
187 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188 sti->func = func;
189 sti->cookie = cookie;
190 sti->fd = s[1];
191
Dan Albertf30d73c2015-02-25 17:51:28 -0800192 adb_thread_t t;
193 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800194 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 Abgrall2e5dd6e2011-03-16 15:57:42 -0700205#if !ADB_HOST
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700206
207static void init_subproc_child()
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800208{
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700209 setsid();
210
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700211 // Set OOM score adjustment to prevent killing
Nick Kralevich777523e2014-07-18 20:57:35 -0700212 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700213 if (fd >= 0) {
214 adb_write(fd, "0", 1);
215 adb_close(fd);
216 } else {
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700217 D("adb: unable to update oom_score_adj\n");
Jeff Sharkeyc52ec1a2014-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 Cui2fa43212014-11-11 09:24:11 -0800224#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700225 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700226 return -1;
Yabin Cui2fa43212014-11-11 09:24:11 -0800227#else
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800228 int ptm;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229
Nick Kralevich777523e2014-07-18 20:57:35 -0700230 ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
The Android Open Source Project9ca14dc2009-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 Project9ca14dc2009-03-03 19:32:55 -0800235
Elliott Hughes1f59fa02014-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 Project9ca14dc2009-03-03 19:32:55 -0800238 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700239 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 return -1;
241 }
242
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700243 *pid = fork();
244 if(*pid < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800245 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700246 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800247 return -1;
248 }
249
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700250 if (*pid == 0) {
251 init_subproc_child();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252
Nick Kralevich777523e2014-07-18 20:57:35 -0700253 int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700254 if (pts < 0) {
JP Abgrall2e5dd6e2011-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 Project9ca14dc2009-03-03 19:32:55 -0800258
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700259 dup2(pts, STDIN_FILENO);
260 dup2(pts, STDOUT_FILENO);
261 dup2(pts, STDERR_FILENO);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800262
Benoit Gobyf4ded742011-02-01 18:57:41 -0800263 adb_close(pts);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800264 adb_close(ptm);
265
JP Abgrall2e5dd6e2011-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 Project9ca14dc2009-03-03 19:32:55 -0800271 return ptm;
272 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800273#endif /* !defined(_WIN32) */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800274}
Jeff Sharkeyc52ec1a2014-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 Cui2fa43212014-11-11 09:24:11 -0800279#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-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 Cui2fa43212014-11-11 09:24:11 -0800282#else
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700283
284 // 0 is parent socket, 1 is child socket
285 int sv[2];
leozwang1be54622014-08-15 09:51:27 -0700286 if (adb_socketpair(sv) < 0) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700287 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
288 return -1;
289 }
leozwang1be54622014-08-15 09:51:27 -0700290 D("socketpair: (%d,%d)", sv[0], sv[1]);
Jeff Sharkeyc52ec1a2014-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 Sharkeyc52ec1a2014-05-26 18:30:43 -0700304 dup2(sv[1], STDIN_FILENO);
305 dup2(sv[1], STDOUT_FILENO);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -0700306 dup2(sv[1], STDERR_FILENO);
307
Jeff Sharkeyc52ec1a2014-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 Cui2fa43212014-11-11 09:24:11 -0800318#endif /* !defined(_WIN32) */
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700319}
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700320#endif /* !ABD_HOST */
The Android Open Source Project9ca14dc2009-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 Abgrall2e5dd6e2011-03-16 15:57:42 -0700328#if !ADB_HOST
329static void subproc_waiter_service(int fd, void *cookie)
330{
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800331 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall2e5dd6e2011-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 Abgrall2e5dd6e2011-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 Albert66a91b02015-02-24 21:26:58 -0800354 res = WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)) ? 0 : -1;
JP Abgrall2e5dd6e2011-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 Sharkeyc52ec1a2014-05-26 18:30:43 -0700360static int create_subproc_thread(const char *name, const subproc_mode mode)
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700361{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700362 adb_thread_t t;
363 int ret_fd;
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700364 pid_t pid = -1;
365
366 const char *arg0, *arg1;
367 if (name == 0 || *name == 0) {
368 arg0 = "-"; arg1 = 0;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700369 } else {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700370 arg0 = "-c"; arg1 = name;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700371 }
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700372
373 switch (mode) {
374 case SUBPROC_PTY:
375 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
376 break;
377 case SUBPROC_RAW:
378 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
379 break;
380 default:
381 fprintf(stderr, "invalid subproc_mode %d\n", mode);
382 return -1;
383 }
384 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700385
Dan Albertf30d73c2015-02-25 17:51:28 -0800386 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700387 if(sti == 0) fatal("cannot allocate stinfo");
388 sti->func = subproc_waiter_service;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800389 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700390 sti->fd = ret_fd;
391
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700392 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700393 free(sti);
394 adb_close(ret_fd);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700395 fprintf(stderr, "cannot create service thread\n");
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700396 return -1;
397 }
398
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700399 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700400 return ret_fd;
401}
402#endif
403
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404int service_to_fd(const char *name)
405{
406 int ret = -1;
407
408 if(!strncmp(name, "tcp:", 4)) {
409 int port = atoi(name + 4);
410 name = strchr(name + 4, ':');
411 if(name == 0) {
412 ret = socket_loopback_client(port, SOCK_STREAM);
413 if (ret >= 0)
414 disable_tcp_nagle(ret);
415 } else {
416#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800418#else
419 return -1;
420#endif
421 }
422#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
423 } else if(!strncmp(name, "local:", 6)) {
424 ret = socket_local_client(name + 6,
425 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
426 } else if(!strncmp(name, "localreserved:", 14)) {
427 ret = socket_local_client(name + 14,
428 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
429 } else if(!strncmp(name, "localabstract:", 14)) {
430 ret = socket_local_client(name + 14,
431 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
432 } else if(!strncmp(name, "localfilesystem:", 16)) {
433 ret = socket_local_client(name + 16,
434 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
435#endif
Benoit Goby12dc3692013-02-20 15:04:53 -0800436#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800437 } else if(!strncmp("dev:", name, 4)) {
Nick Kralevich777523e2014-07-18 20:57:35 -0700438 ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800439 } else if(!strncmp(name, "framebuffer:", 12)) {
440 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441 } else if (!strncmp(name, "jdwp:", 5)) {
442 ret = create_jdwp_connection_fd(atoi(name+5));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800443 } else if(!HOST && !strncmp(name, "shell:", 6)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700444 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
445 } else if(!HOST && !strncmp(name, "exec:", 5)) {
446 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800447 } else if(!strncmp(name, "sync:", 5)) {
448 ret = create_service_thread(file_sync_service, NULL);
449 } else if(!strncmp(name, "remount:", 8)) {
450 ret = create_service_thread(remount_service, NULL);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400451 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400452 void* arg = strdup(name + 7);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700453 if (arg == NULL) return -1;
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400454 ret = create_service_thread(reboot_service, arg);
The Android Open Source Project9c753402009-03-13 13:04:37 -0700455 } else if(!strncmp(name, "root:", 5)) {
456 ret = create_service_thread(restart_root_service, NULL);
Dan Pasanenfb787d92014-10-06 12:57:20 -0500457 } else if(!strncmp(name, "unroot:", 7)) {
458 ret = create_service_thread(restart_unroot_service, NULL);
Christopher Tate73779122011-04-21 12:53:28 -0700459 } else if(!strncmp(name, "backup:", 7)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700460 char* arg = strdup(name + 7);
Christopher Tate73779122011-04-21 12:53:28 -0700461 if (arg == NULL) return -1;
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700462 char* c = arg;
463 for (; *c != '\0'; c++) {
464 if (*c == ':')
465 *c = ' ';
466 }
467 char* cmd;
468 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
469 ret = create_subproc_thread(cmd, SUBPROC_RAW);
470 free(cmd);
471 }
472 free(arg);
Christopher Tatecf5379b2011-05-17 15:52:54 -0700473 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700474 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700475 } else if(!strncmp(name, "tcpip:", 6)) {
476 int port;
Spencer Low29a029c2015-01-25 17:38:36 -0800477 if (sscanf(name + 6, "%d", &port) != 1) {
Mike Lockwood26b88e32009-08-24 15:58:40 -0700478 port = 0;
479 }
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800480 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700481 } else if(!strncmp(name, "usb:", 4)) {
482 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100483 } else if (!strncmp(name, "reverse:", 8)) {
484 char* cookie = strdup(name + 8);
485 if (cookie == NULL) {
486 ret = -1;
487 } else {
488 ret = create_service_thread(reverse_service, cookie);
489 if (ret < 0) {
490 free(cookie);
491 }
492 }
Paul Lawrence5f356482014-10-09 14:22:49 +0000493 } else if(!strncmp(name, "disable-verity:", 15)) {
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800494 ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
495 } else if(!strncmp(name, "enable-verity:", 15)) {
496 ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800497#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800498 }
499 if (ret >= 0) {
500 close_on_exec(ret);
501 }
502 return ret;
503}
504
505#if ADB_HOST
506struct state_info {
507 transport_type transport;
508 char* serial;
509 int state;
510};
511
512static void wait_for_state(int fd, void* cookie)
513{
Dan Albertf30d73c2015-02-25 17:51:28 -0800514 state_info* sinfo = reinterpret_cast<state_info*>(cookie);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800515
516 D("wait_for_state %d\n", sinfo->state);
517
Dan Albertf30d73c2015-02-25 17:51:28 -0800518 const char* err = "unknown error";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800519 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
520 if(t != 0) {
Dan Albert66a91b02015-02-24 21:26:58 -0800521 WriteFdExactly(fd, "OKAY", 4);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800522 } else {
523 sendfailmsg(fd, err);
524 }
525
526 if (sinfo->serial)
527 free(sinfo->serial);
528 free(sinfo);
529 adb_close(fd);
530 D("wait_for_state is done\n");
531}
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700532
533static void connect_device(char* host, char* buffer, int buffer_size)
534{
535 int port, fd;
536 char* portstr = strchr(host, ':');
537 char hostbuf[100];
538 char serial[100];
539 int ret;
540
541 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
542 if (portstr) {
543 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
544 snprintf(buffer, buffer_size, "bad host name %s", host);
545 return;
546 }
547 // zero terminate the host at the point we found the colon
548 hostbuf[portstr - host] = 0;
Spencer Low29a029c2015-01-25 17:38:36 -0800549 if (sscanf(portstr + 1, "%d", &port) != 1) {
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700550 snprintf(buffer, buffer_size, "bad port number %s", portstr);
551 return;
552 }
553 } else {
554 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
555 }
556
557 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
558
Ken Liermane6cd0c82013-06-20 09:27:13 -0700559 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700560 if (fd < 0) {
561 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
562 return;
563 }
564
565 D("client: connected on remote on fd %d\n", fd);
566 close_on_exec(fd);
567 disable_tcp_nagle(fd);
568
569 ret = register_socket_transport(fd, serial, port, 0);
570 if (ret < 0) {
571 adb_close(fd);
572 snprintf(buffer, buffer_size, "already connected to %s", serial);
573 } else {
574 snprintf(buffer, buffer_size, "connected to %s", serial);
575 }
576}
577
578void connect_emulator(char* port_spec, char* buffer, int buffer_size)
579{
580 char* port_separator = strchr(port_spec, ',');
581 if (!port_separator) {
582 snprintf(buffer, buffer_size,
583 "unable to parse '%s' as <console port>,<adb port>",
584 port_spec);
585 return;
586 }
587
588 // Zero-terminate console port and make port_separator point to 2nd port.
589 *port_separator++ = 0;
590 int console_port = strtol(port_spec, NULL, 0);
591 int adb_port = strtol(port_separator, NULL, 0);
592 if (!(console_port > 0 && adb_port > 0)) {
593 *(port_separator - 1) = ',';
594 snprintf(buffer, buffer_size,
595 "Invalid port numbers: Expected positive numbers, got '%s'",
596 port_spec);
597 return;
598 }
599
600 /* Check if the emulator is already known.
601 * Note: There's a small but harmless race condition here: An emulator not
602 * present just yet could be registered by another invocation right
603 * after doing this check here. However, local_connect protects
604 * against double-registration too. From here, a better error message
605 * can be produced. In the case of the race condition, the very specific
606 * error message won't be shown, but the data doesn't get corrupted. */
607 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
608 if (known_emulator != NULL) {
609 snprintf(buffer, buffer_size,
610 "Emulator on port %d already registered.", adb_port);
611 return;
612 }
613
614 /* Check if more emulators can be registered. Similar unproblematic
615 * race condition as above. */
616 int candidate_slot = get_available_local_transport_index();
617 if (candidate_slot < 0) {
618 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
619 return;
620 }
621
622 /* Preconditions met, try to connect to the emulator. */
623 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
624 snprintf(buffer, buffer_size,
625 "Connected to emulator on ports %d,%d", console_port, adb_port);
626 } else {
627 snprintf(buffer, buffer_size,
628 "Could not connect to emulator on ports %d,%d",
629 console_port, adb_port);
630 }
631}
632
633static void connect_service(int fd, void* cookie)
634{
635 char buf[4096];
636 char resp[4096];
Dan Albertf30d73c2015-02-25 17:51:28 -0800637 char *host = reinterpret_cast<char*>(cookie);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700638
639 if (!strncmp(host, "emu:", 4)) {
640 connect_emulator(host + 4, buf, sizeof(buf));
641 } else {
642 connect_device(host, buf, sizeof(buf));
643 }
644
645 // Send response for emulator and device
646 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
Dan Albert66a91b02015-02-24 21:26:58 -0800647 WriteFdExactly(fd, resp, strlen(resp));
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700648 adb_close(fd);
649}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800650#endif
651
652#if ADB_HOST
653asocket* host_service_to_socket(const char* name, const char *serial)
654{
655 if (!strcmp(name,"track-devices")) {
656 return create_device_tracker();
657 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800658 auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800659
660 if (serial)
661 sinfo->serial = strdup(serial);
662 else
663 sinfo->serial = NULL;
664
665 name += strlen("wait-for-");
666
667 if (!strncmp(name, "local", strlen("local"))) {
668 sinfo->transport = kTransportLocal;
669 sinfo->state = CS_DEVICE;
670 } else if (!strncmp(name, "usb", strlen("usb"))) {
671 sinfo->transport = kTransportUsb;
672 sinfo->state = CS_DEVICE;
673 } else if (!strncmp(name, "any", strlen("any"))) {
674 sinfo->transport = kTransportAny;
675 sinfo->state = CS_DEVICE;
676 } else {
677 free(sinfo);
678 return NULL;
679 }
680
681 int fd = create_service_thread(wait_for_state, sinfo);
682 return create_local_socket(fd);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700683 } else if (!strncmp(name, "connect:", 8)) {
684 const char *host = name + 8;
685 int fd = create_service_thread(connect_service, (void *)host);
686 return create_local_socket(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800687 }
688 return NULL;
689}
690#endif /* ADB_HOST */