blob: abf8ea5720efb467e4773cc2f44f422b4bd52620 [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
Dan Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_SERVICES
18
19#include "sysdeps.h"
20
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#include <errno.h>
Dan Albertb302d122015-02-24 15:51:19 -080022#include <stddef.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#ifndef _WIN32
28#include <netdb.h>
29#include <netinet/in.h>
30#include <sys/ioctl.h>
31#include <unistd.h>
32#endif
33
34#if !ADB_HOST
35#include "cutils/android_reboot.h"
36#include "cutils/properties.h"
37#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080038
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080039#include "adb.h"
Dan Albert66a91b02015-02-24 21:26:58 -080040#include "adb_io.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080041#include "file_sync_service.h"
Elliott Hughesdedf7672015-03-16 21:58:32 +000042#include "remount_service.h"
Dan Albertb302d122015-02-24 15:51:19 -080043#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080044
45typedef struct stinfo stinfo;
46
47struct stinfo {
48 void (*func)(int fd, void *cookie);
49 int fd;
50 void *cookie;
51};
52
53
54void *service_bootstrap_func(void *x)
55{
Dan Albertf30d73c2015-02-25 17:51:28 -080056 stinfo* sti = reinterpret_cast<stinfo*>(x);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080057 sti->func(sti->fd, sti->cookie);
58 free(sti);
59 return 0;
60}
61
Benoit Goby12dc3692013-02-20 15:04:53 -080062#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080063
The Android Open Source Project9c753402009-03-13 13:04:37 -070064void restart_root_service(int fd, void *cookie)
65{
66 char buf[100];
67 char value[PROPERTY_VALUE_MAX];
68
69 if (getuid() == 0) {
70 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
Dan Albert66a91b02015-02-24 21:26:58 -080071 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Project9c753402009-03-13 13:04:37 -070072 adb_close(fd);
73 } else {
74 property_get("ro.debuggable", value, "");
75 if (strcmp(value, "1") != 0) {
76 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
Dan Albert66a91b02015-02-24 21:26:58 -080077 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -070078 adb_close(fd);
The Android Open Source Project9c753402009-03-13 13:04:37 -070079 return;
80 }
81
Benoit Gobyad926c12012-03-16 14:44:17 -070082 property_set("service.adb.root", "1");
The Android Open Source Project9c753402009-03-13 13:04:37 -070083 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
Dan Albert66a91b02015-02-24 21:26:58 -080084 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Project9c753402009-03-13 13:04:37 -070085 adb_close(fd);
The Android Open Source Project9c753402009-03-13 13:04:37 -070086 }
87}
88
Dan Pasanenfb787d92014-10-06 12:57:20 -050089void restart_unroot_service(int fd, void *cookie)
90{
91 char buf[100];
92
93 if (getuid() != 0) {
94 snprintf(buf, sizeof(buf), "adbd not running as root\n");
Dan Albert66a91b02015-02-24 21:26:58 -080095 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanenfb787d92014-10-06 12:57:20 -050096 adb_close(fd);
97 } else {
98 property_set("service.adb.root", "0");
99 snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
Dan Albert66a91b02015-02-24 21:26:58 -0800100 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanenfb787d92014-10-06 12:57:20 -0500101 adb_close(fd);
102 }
103}
104
Mike Lockwood26b88e32009-08-24 15:58:40 -0700105void restart_tcp_service(int fd, void *cookie)
106{
107 char buf[100];
108 char value[PROPERTY_VALUE_MAX];
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800109 int port = (int) (uintptr_t) cookie;
Mike Lockwood26b88e32009-08-24 15:58:40 -0700110
111 if (port <= 0) {
112 snprintf(buf, sizeof(buf), "invalid port\n");
Dan Albert66a91b02015-02-24 21:26:58 -0800113 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -0700114 adb_close(fd);
115 return;
116 }
117
118 snprintf(value, sizeof(value), "%d", port);
119 property_set("service.adb.tcp.port", value);
120 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
Dan Albert66a91b02015-02-24 21:26:58 -0800121 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -0700122 adb_close(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700123}
124
125void restart_usb_service(int fd, void *cookie)
126{
127 char buf[100];
128
129 property_set("service.adb.tcp.port", "0");
130 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
Dan Albert66a91b02015-02-24 21:26:58 -0800131 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -0700132 adb_close(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700133}
134
135void reboot_service(int fd, void *arg)
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400136{
137 char buf[100];
Nick Kralevichb7df0172013-04-18 12:20:02 -0700138 char property_val[PROPERTY_VALUE_MAX];
Nick Kralevich5b5283c2014-01-14 16:34:56 -0800139 int ret;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400140
141 sync();
Mike Lockwoodd49e9eb2010-02-24 16:07:23 -0500142
Nick Kralevichb7df0172013-04-18 12:20:02 -0700143 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
144 if (ret >= (int) sizeof(property_val)) {
145 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
Dan Albert66a91b02015-02-24 21:26:58 -0800146 WriteFdExactly(fd, buf, strlen(buf));
Nick Kralevichb7df0172013-04-18 12:20:02 -0700147 goto cleanup;
148 }
149
150 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400151 if (ret < 0) {
Nick Kralevichb7df0172013-04-18 12:20:02 -0700152 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Dan Albert66a91b02015-02-24 21:26:58 -0800153 WriteFdExactly(fd, buf, strlen(buf));
Nick Kralevich51699982013-10-24 08:53:48 -0700154 goto cleanup;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400155 }
Nick Kralevich51699982013-10-24 08:53:48 -0700156 // Don't return early. Give the reboot command time to take effect
157 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
158 while(1) { pause(); }
Nick Kralevichb7df0172013-04-18 12:20:02 -0700159cleanup:
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400160 free(arg);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400161 adb_close(fd);
162}
163
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100164void reverse_service(int fd, void* arg)
165{
Dan Albertf30d73c2015-02-25 17:51:28 -0800166 const char* command = reinterpret_cast<const char*>(arg);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100167
168 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
169 sendfailmsg(fd, "not a reverse forwarding command");
170 }
171 free(arg);
172 adb_close(fd);
173}
174
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800175#endif
176
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800177static int create_service_thread(void (*func)(int, void *), void *cookie)
178{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800179 int s[2];
Dan Albertf30d73c2015-02-25 17:51:28 -0800180 if (adb_socketpair(s)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800181 printf("cannot create service socket pair\n");
182 return -1;
183 }
leozwang1be54622014-08-15 09:51:27 -0700184 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800185
Dan Albertf30d73c2015-02-25 17:51:28 -0800186 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
187 if (sti == nullptr) {
188 fatal("cannot allocate stinfo");
189 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800190 sti->func = func;
191 sti->cookie = cookie;
192 sti->fd = s[1];
193
Dan Albertf30d73c2015-02-25 17:51:28 -0800194 adb_thread_t t;
195 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196 free(sti);
197 adb_close(s[0]);
198 adb_close(s[1]);
199 printf("cannot create service thread\n");
200 return -1;
201 }
202
203 D("service thread started, %d:%d\n",s[0], s[1]);
204 return s[0];
205}
206
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700207#if !ADB_HOST
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700208
209static void init_subproc_child()
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210{
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700211 setsid();
212
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700213 // Set OOM score adjustment to prevent killing
Nick Kralevich777523e2014-07-18 20:57:35 -0700214 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700215 if (fd >= 0) {
216 adb_write(fd, "0", 1);
217 adb_close(fd);
218 } else {
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700219 D("adb: unable to update oom_score_adj\n");
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700220 }
221}
222
223static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
224{
225 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cui2fa43212014-11-11 09:24:11 -0800226#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700227 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700228 return -1;
Yabin Cui2fa43212014-11-11 09:24:11 -0800229#else
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800230 int ptm;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800231
Nick Kralevich777523e2014-07-18 20:57:35 -0700232 ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800233 if(ptm < 0){
234 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
235 return -1;
236 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800237
Elliott Hughes1f59fa02014-07-29 11:32:16 -0700238 char devname[64];
239 if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700241 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800242 return -1;
243 }
244
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700245 *pid = fork();
246 if(*pid < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800247 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700248 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800249 return -1;
250 }
251
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700252 if (*pid == 0) {
253 init_subproc_child();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800254
Nick Kralevich777523e2014-07-18 20:57:35 -0700255 int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700256 if (pts < 0) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700257 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
258 exit(-1);
259 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800260
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700261 dup2(pts, STDIN_FILENO);
262 dup2(pts, STDOUT_FILENO);
263 dup2(pts, STDERR_FILENO);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800264
Benoit Gobyf4ded742011-02-01 18:57:41 -0800265 adb_close(pts);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800266 adb_close(ptm);
267
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700268 execl(cmd, cmd, arg0, arg1, NULL);
269 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
270 cmd, strerror(errno), errno);
271 exit(-1);
272 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800273 return ptm;
274 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800275#endif /* !defined(_WIN32) */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800276}
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700277
278static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
279{
280 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cui2fa43212014-11-11 09:24:11 -0800281#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700282 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
283 return -1;
Yabin Cui2fa43212014-11-11 09:24:11 -0800284#else
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700285
286 // 0 is parent socket, 1 is child socket
287 int sv[2];
leozwang1be54622014-08-15 09:51:27 -0700288 if (adb_socketpair(sv) < 0) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700289 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
290 return -1;
291 }
leozwang1be54622014-08-15 09:51:27 -0700292 D("socketpair: (%d,%d)", sv[0], sv[1]);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700293
294 *pid = fork();
295 if (*pid < 0) {
296 printf("- fork failed: %s -\n", strerror(errno));
297 adb_close(sv[0]);
298 adb_close(sv[1]);
299 return -1;
300 }
301
302 if (*pid == 0) {
303 adb_close(sv[0]);
304 init_subproc_child();
305
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700306 dup2(sv[1], STDIN_FILENO);
307 dup2(sv[1], STDOUT_FILENO);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -0700308 dup2(sv[1], STDERR_FILENO);
309
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700310 adb_close(sv[1]);
311
312 execl(cmd, cmd, arg0, arg1, NULL);
313 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
314 cmd, strerror(errno), errno);
315 exit(-1);
316 } else {
317 adb_close(sv[1]);
318 return sv[0];
319 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800320#endif /* !defined(_WIN32) */
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700321}
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700322#endif /* !ABD_HOST */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323
324#if ADB_HOST
325#define SHELL_COMMAND "/bin/sh"
326#else
327#define SHELL_COMMAND "/system/bin/sh"
328#endif
329
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700330#if !ADB_HOST
331static void subproc_waiter_service(int fd, void *cookie)
332{
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800333 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700334
335 D("entered. fd=%d of pid=%d\n", fd, pid);
336 for (;;) {
337 int status;
338 pid_t p = waitpid(pid, &status, 0);
339 if (p == pid) {
340 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
341 if (WIFSIGNALED(status)) {
342 D("*** Killed by signal %d\n", WTERMSIG(status));
343 break;
344 } else if (!WIFEXITED(status)) {
345 D("*** Didn't exit!!. status %d\n", status);
346 break;
347 } else if (WEXITSTATUS(status) >= 0) {
348 D("*** Exit code %d\n", WEXITSTATUS(status));
349 break;
350 }
351 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700352 }
353 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
354 if (SHELL_EXIT_NOTIFY_FD >=0) {
355 int res;
Dan Albert66a91b02015-02-24 21:26:58 -0800356 res = WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)) ? 0 : -1;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700357 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
358 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
359 }
360}
361
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700362static int create_subproc_thread(const char *name, const subproc_mode mode)
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700363{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700364 adb_thread_t t;
365 int ret_fd;
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700366 pid_t pid = -1;
367
368 const char *arg0, *arg1;
369 if (name == 0 || *name == 0) {
370 arg0 = "-"; arg1 = 0;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700371 } else {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700372 arg0 = "-c"; arg1 = name;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700373 }
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700374
375 switch (mode) {
376 case SUBPROC_PTY:
377 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
378 break;
379 case SUBPROC_RAW:
380 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
381 break;
382 default:
383 fprintf(stderr, "invalid subproc_mode %d\n", mode);
384 return -1;
385 }
386 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700387
Dan Albertf30d73c2015-02-25 17:51:28 -0800388 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700389 if(sti == 0) fatal("cannot allocate stinfo");
390 sti->func = subproc_waiter_service;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800391 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700392 sti->fd = ret_fd;
393
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700394 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700395 free(sti);
396 adb_close(ret_fd);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700397 fprintf(stderr, "cannot create service thread\n");
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700398 return -1;
399 }
400
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700401 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700402 return ret_fd;
403}
404#endif
405
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800406int service_to_fd(const char *name)
407{
408 int ret = -1;
409
410 if(!strncmp(name, "tcp:", 4)) {
411 int port = atoi(name + 4);
412 name = strchr(name + 4, ':');
413 if(name == 0) {
414 ret = socket_loopback_client(port, SOCK_STREAM);
415 if (ret >= 0)
416 disable_tcp_nagle(ret);
417 } else {
418#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800419 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800420#else
421 return -1;
422#endif
423 }
424#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
425 } else if(!strncmp(name, "local:", 6)) {
426 ret = socket_local_client(name + 6,
427 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
428 } else if(!strncmp(name, "localreserved:", 14)) {
429 ret = socket_local_client(name + 14,
430 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
431 } else if(!strncmp(name, "localabstract:", 14)) {
432 ret = socket_local_client(name + 14,
433 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
434 } else if(!strncmp(name, "localfilesystem:", 16)) {
435 ret = socket_local_client(name + 16,
436 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
437#endif
Benoit Goby12dc3692013-02-20 15:04:53 -0800438#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800439 } else if(!strncmp("dev:", name, 4)) {
Nick Kralevich777523e2014-07-18 20:57:35 -0700440 ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441 } else if(!strncmp(name, "framebuffer:", 12)) {
442 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800443 } else if (!strncmp(name, "jdwp:", 5)) {
444 ret = create_jdwp_connection_fd(atoi(name+5));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800445 } else if(!HOST && !strncmp(name, "shell:", 6)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700446 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
447 } else if(!HOST && !strncmp(name, "exec:", 5)) {
448 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800449 } else if(!strncmp(name, "sync:", 5)) {
450 ret = create_service_thread(file_sync_service, NULL);
451 } else if(!strncmp(name, "remount:", 8)) {
452 ret = create_service_thread(remount_service, NULL);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400453 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400454 void* arg = strdup(name + 7);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700455 if (arg == NULL) return -1;
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400456 ret = create_service_thread(reboot_service, arg);
The Android Open Source Project9c753402009-03-13 13:04:37 -0700457 } else if(!strncmp(name, "root:", 5)) {
458 ret = create_service_thread(restart_root_service, NULL);
Dan Pasanenfb787d92014-10-06 12:57:20 -0500459 } else if(!strncmp(name, "unroot:", 7)) {
460 ret = create_service_thread(restart_unroot_service, NULL);
Christopher Tate73779122011-04-21 12:53:28 -0700461 } else if(!strncmp(name, "backup:", 7)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700462 char* arg = strdup(name + 7);
Christopher Tate73779122011-04-21 12:53:28 -0700463 if (arg == NULL) return -1;
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700464 char* c = arg;
465 for (; *c != '\0'; c++) {
466 if (*c == ':')
467 *c = ' ';
468 }
469 char* cmd;
470 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
471 ret = create_subproc_thread(cmd, SUBPROC_RAW);
472 free(cmd);
473 }
474 free(arg);
Christopher Tatecf5379b2011-05-17 15:52:54 -0700475 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700476 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700477 } else if(!strncmp(name, "tcpip:", 6)) {
478 int port;
Spencer Low29a029c2015-01-25 17:38:36 -0800479 if (sscanf(name + 6, "%d", &port) != 1) {
Mike Lockwood26b88e32009-08-24 15:58:40 -0700480 port = 0;
481 }
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800482 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700483 } else if(!strncmp(name, "usb:", 4)) {
484 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100485 } else if (!strncmp(name, "reverse:", 8)) {
486 char* cookie = strdup(name + 8);
487 if (cookie == NULL) {
488 ret = -1;
489 } else {
490 ret = create_service_thread(reverse_service, cookie);
491 if (ret < 0) {
492 free(cookie);
493 }
494 }
Paul Lawrence5f356482014-10-09 14:22:49 +0000495 } else if(!strncmp(name, "disable-verity:", 15)) {
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800496 ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
497 } else if(!strncmp(name, "enable-verity:", 15)) {
498 ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800499#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800500 }
501 if (ret >= 0) {
502 close_on_exec(ret);
503 }
504 return ret;
505}
506
507#if ADB_HOST
508struct state_info {
509 transport_type transport;
510 char* serial;
511 int state;
512};
513
514static void wait_for_state(int fd, void* cookie)
515{
Dan Albertf30d73c2015-02-25 17:51:28 -0800516 state_info* sinfo = reinterpret_cast<state_info*>(cookie);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800517
518 D("wait_for_state %d\n", sinfo->state);
519
Dan Albertf30d73c2015-02-25 17:51:28 -0800520 const char* err = "unknown error";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800521 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
522 if(t != 0) {
Dan Albert66a91b02015-02-24 21:26:58 -0800523 WriteFdExactly(fd, "OKAY", 4);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800524 } else {
525 sendfailmsg(fd, err);
526 }
527
528 if (sinfo->serial)
529 free(sinfo->serial);
530 free(sinfo);
531 adb_close(fd);
532 D("wait_for_state is done\n");
533}
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700534
535static void connect_device(char* host, char* buffer, int buffer_size)
536{
537 int port, fd;
538 char* portstr = strchr(host, ':');
539 char hostbuf[100];
540 char serial[100];
541 int ret;
542
543 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
544 if (portstr) {
545 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
546 snprintf(buffer, buffer_size, "bad host name %s", host);
547 return;
548 }
549 // zero terminate the host at the point we found the colon
550 hostbuf[portstr - host] = 0;
Spencer Low29a029c2015-01-25 17:38:36 -0800551 if (sscanf(portstr + 1, "%d", &port) != 1) {
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700552 snprintf(buffer, buffer_size, "bad port number %s", portstr);
553 return;
554 }
555 } else {
556 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
557 }
558
559 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
560
Ken Liermane6cd0c82013-06-20 09:27:13 -0700561 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700562 if (fd < 0) {
563 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
564 return;
565 }
566
567 D("client: connected on remote on fd %d\n", fd);
568 close_on_exec(fd);
569 disable_tcp_nagle(fd);
570
571 ret = register_socket_transport(fd, serial, port, 0);
572 if (ret < 0) {
573 adb_close(fd);
574 snprintf(buffer, buffer_size, "already connected to %s", serial);
575 } else {
576 snprintf(buffer, buffer_size, "connected to %s", serial);
577 }
578}
579
580void connect_emulator(char* port_spec, char* buffer, int buffer_size)
581{
582 char* port_separator = strchr(port_spec, ',');
583 if (!port_separator) {
584 snprintf(buffer, buffer_size,
585 "unable to parse '%s' as <console port>,<adb port>",
586 port_spec);
587 return;
588 }
589
590 // Zero-terminate console port and make port_separator point to 2nd port.
591 *port_separator++ = 0;
592 int console_port = strtol(port_spec, NULL, 0);
593 int adb_port = strtol(port_separator, NULL, 0);
594 if (!(console_port > 0 && adb_port > 0)) {
595 *(port_separator - 1) = ',';
596 snprintf(buffer, buffer_size,
597 "Invalid port numbers: Expected positive numbers, got '%s'",
598 port_spec);
599 return;
600 }
601
602 /* Check if the emulator is already known.
603 * Note: There's a small but harmless race condition here: An emulator not
604 * present just yet could be registered by another invocation right
605 * after doing this check here. However, local_connect protects
606 * against double-registration too. From here, a better error message
607 * can be produced. In the case of the race condition, the very specific
608 * error message won't be shown, but the data doesn't get corrupted. */
609 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
610 if (known_emulator != NULL) {
611 snprintf(buffer, buffer_size,
612 "Emulator on port %d already registered.", adb_port);
613 return;
614 }
615
616 /* Check if more emulators can be registered. Similar unproblematic
617 * race condition as above. */
618 int candidate_slot = get_available_local_transport_index();
619 if (candidate_slot < 0) {
620 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
621 return;
622 }
623
624 /* Preconditions met, try to connect to the emulator. */
625 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
626 snprintf(buffer, buffer_size,
627 "Connected to emulator on ports %d,%d", console_port, adb_port);
628 } else {
629 snprintf(buffer, buffer_size,
630 "Could not connect to emulator on ports %d,%d",
631 console_port, adb_port);
632 }
633}
634
635static void connect_service(int fd, void* cookie)
636{
637 char buf[4096];
638 char resp[4096];
Dan Albertf30d73c2015-02-25 17:51:28 -0800639 char *host = reinterpret_cast<char*>(cookie);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700640
641 if (!strncmp(host, "emu:", 4)) {
642 connect_emulator(host + 4, buf, sizeof(buf));
643 } else {
644 connect_device(host, buf, sizeof(buf));
645 }
646
647 // Send response for emulator and device
648 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
Dan Albert66a91b02015-02-24 21:26:58 -0800649 WriteFdExactly(fd, resp, strlen(resp));
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700650 adb_close(fd);
651}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800652#endif
653
654#if ADB_HOST
655asocket* host_service_to_socket(const char* name, const char *serial)
656{
657 if (!strcmp(name,"track-devices")) {
658 return create_device_tracker();
659 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800660 auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800661
662 if (serial)
663 sinfo->serial = strdup(serial);
664 else
665 sinfo->serial = NULL;
666
667 name += strlen("wait-for-");
668
669 if (!strncmp(name, "local", strlen("local"))) {
670 sinfo->transport = kTransportLocal;
671 sinfo->state = CS_DEVICE;
672 } else if (!strncmp(name, "usb", strlen("usb"))) {
673 sinfo->transport = kTransportUsb;
674 sinfo->state = CS_DEVICE;
675 } else if (!strncmp(name, "any", strlen("any"))) {
676 sinfo->transport = kTransportAny;
677 sinfo->state = CS_DEVICE;
678 } else {
679 free(sinfo);
680 return NULL;
681 }
682
683 int fd = create_service_thread(wait_for_state, sinfo);
684 return create_local_socket(fd);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700685 } else if (!strncmp(name, "connect:", 8)) {
686 const char *host = name + 8;
687 int fd = create_service_thread(connect_service, (void *)host);
688 return create_local_socket(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800689 }
690 return NULL;
691}
692#endif /* ADB_HOST */