blob: e7bf6b029004b2ab0f37471e6accc98a24c7eda6 [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"
Elliott Hughesdedf7672015-03-16 21:58:32 +000041#include "remount_service.h"
Dan Albertb302d122015-02-24 15:51:19 -080042#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043
44typedef struct stinfo stinfo;
45
46struct stinfo {
47 void (*func)(int fd, void *cookie);
48 int fd;
49 void *cookie;
50};
51
52
53void *service_bootstrap_func(void *x)
54{
Dan Albertf30d73c2015-02-25 17:51:28 -080055 stinfo* sti = reinterpret_cast<stinfo*>(x);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080056 sti->func(sti->fd, sti->cookie);
57 free(sti);
58 return 0;
59}
60
Benoit Goby12dc3692013-02-20 15:04:53 -080061#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080062
The Android Open Source Project9c753402009-03-13 13:04:37 -070063void restart_root_service(int fd, void *cookie)
64{
65 char buf[100];
66 char value[PROPERTY_VALUE_MAX];
67
68 if (getuid() == 0) {
69 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
Dan Albert66a91b02015-02-24 21:26:58 -080070 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Project9c753402009-03-13 13:04:37 -070071 adb_close(fd);
72 } else {
73 property_get("ro.debuggable", value, "");
74 if (strcmp(value, "1") != 0) {
75 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
Dan Albert66a91b02015-02-24 21:26:58 -080076 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -070077 adb_close(fd);
The Android Open Source Project9c753402009-03-13 13:04:37 -070078 return;
79 }
80
Benoit Gobyad926c12012-03-16 14:44:17 -070081 property_set("service.adb.root", "1");
The Android Open Source Project9c753402009-03-13 13:04:37 -070082 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
Dan Albert66a91b02015-02-24 21:26:58 -080083 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Project9c753402009-03-13 13:04:37 -070084 adb_close(fd);
The Android Open Source Project9c753402009-03-13 13:04:37 -070085 }
86}
87
Dan Pasanenfb787d92014-10-06 12:57:20 -050088void restart_unroot_service(int fd, void *cookie)
89{
90 char buf[100];
91
92 if (getuid() != 0) {
93 snprintf(buf, sizeof(buf), "adbd not running as root\n");
Dan Albert66a91b02015-02-24 21:26:58 -080094 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanenfb787d92014-10-06 12:57:20 -050095 adb_close(fd);
96 } else {
97 property_set("service.adb.root", "0");
98 snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
Dan Albert66a91b02015-02-24 21:26:58 -080099 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanenfb787d92014-10-06 12:57:20 -0500100 adb_close(fd);
101 }
102}
103
Mike Lockwood26b88e32009-08-24 15:58:40 -0700104void restart_tcp_service(int fd, void *cookie)
105{
106 char buf[100];
107 char value[PROPERTY_VALUE_MAX];
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800108 int port = (int) (uintptr_t) cookie;
Mike Lockwood26b88e32009-08-24 15:58:40 -0700109
110 if (port <= 0) {
111 snprintf(buf, sizeof(buf), "invalid port\n");
Dan Albert66a91b02015-02-24 21:26:58 -0800112 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -0700113 adb_close(fd);
114 return;
115 }
116
117 snprintf(value, sizeof(value), "%d", port);
118 property_set("service.adb.tcp.port", value);
119 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
Dan Albert66a91b02015-02-24 21:26:58 -0800120 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -0700121 adb_close(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700122}
123
124void restart_usb_service(int fd, void *cookie)
125{
126 char buf[100];
127
128 property_set("service.adb.tcp.port", "0");
129 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
Dan Albert66a91b02015-02-24 21:26:58 -0800130 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwood26b88e32009-08-24 15:58:40 -0700131 adb_close(fd);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700132}
133
134void reboot_service(int fd, void *arg)
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400135{
136 char buf[100];
Nick Kralevichb7df0172013-04-18 12:20:02 -0700137 char property_val[PROPERTY_VALUE_MAX];
Nick Kralevich5b5283c2014-01-14 16:34:56 -0800138 int ret;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400139
140 sync();
Mike Lockwoodd49e9eb2010-02-24 16:07:23 -0500141
Nick Kralevichb7df0172013-04-18 12:20:02 -0700142 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
143 if (ret >= (int) sizeof(property_val)) {
144 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
Dan Albert66a91b02015-02-24 21:26:58 -0800145 WriteFdExactly(fd, buf, strlen(buf));
Nick Kralevichb7df0172013-04-18 12:20:02 -0700146 goto cleanup;
147 }
148
149 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400150 if (ret < 0) {
Nick Kralevichb7df0172013-04-18 12:20:02 -0700151 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Dan Albert66a91b02015-02-24 21:26:58 -0800152 WriteFdExactly(fd, buf, strlen(buf));
Nick Kralevich51699982013-10-24 08:53:48 -0700153 goto cleanup;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400154 }
Nick Kralevich51699982013-10-24 08:53:48 -0700155 // Don't return early. Give the reboot command time to take effect
156 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
157 while(1) { pause(); }
Nick Kralevichb7df0172013-04-18 12:20:02 -0700158cleanup:
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400159 free(arg);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400160 adb_close(fd);
161}
162
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100163void reverse_service(int fd, void* arg)
164{
Dan Albertf30d73c2015-02-25 17:51:28 -0800165 const char* command = reinterpret_cast<const char*>(arg);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100166
167 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
168 sendfailmsg(fd, "not a reverse forwarding command");
169 }
170 free(arg);
171 adb_close(fd);
172}
173
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800174#endif
175
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800176static int create_service_thread(void (*func)(int, void *), void *cookie)
177{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800178 int s[2];
Dan Albertf30d73c2015-02-25 17:51:28 -0800179 if (adb_socketpair(s)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800180 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
Dan Albertf30d73c2015-02-25 17:51:28 -0800185 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
186 if (sti == nullptr) {
187 fatal("cannot allocate stinfo");
188 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800189 sti->func = func;
190 sti->cookie = cookie;
191 sti->fd = s[1];
192
Dan Albertf30d73c2015-02-25 17:51:28 -0800193 adb_thread_t t;
194 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800195 free(sti);
196 adb_close(s[0]);
197 adb_close(s[1]);
198 printf("cannot create service thread\n");
199 return -1;
200 }
201
202 D("service thread started, %d:%d\n",s[0], s[1]);
203 return s[0];
204}
205
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700206#if !ADB_HOST
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700207
208static void init_subproc_child()
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800209{
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700210 setsid();
211
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700212 // Set OOM score adjustment to prevent killing
Nick Kralevich777523e2014-07-18 20:57:35 -0700213 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700214 if (fd >= 0) {
215 adb_write(fd, "0", 1);
216 adb_close(fd);
217 } else {
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700218 D("adb: unable to update oom_score_adj\n");
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700219 }
220}
221
222static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
223{
224 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cui2fa43212014-11-11 09:24:11 -0800225#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700226 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700227 return -1;
Yabin Cui2fa43212014-11-11 09:24:11 -0800228#else
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229 int ptm;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800230
Nick Kralevich777523e2014-07-18 20:57:35 -0700231 ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800232 if(ptm < 0){
233 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
234 return -1;
235 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236
Elliott Hughes1f59fa02014-07-29 11:32:16 -0700237 char devname[64];
238 if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800239 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700240 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800241 return -1;
242 }
243
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700244 *pid = fork();
245 if(*pid < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800246 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700247 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800248 return -1;
249 }
250
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700251 if (*pid == 0) {
252 init_subproc_child();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800253
Nick Kralevich777523e2014-07-18 20:57:35 -0700254 int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700255 if (pts < 0) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700256 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
257 exit(-1);
258 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800259
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700260 dup2(pts, STDIN_FILENO);
261 dup2(pts, STDOUT_FILENO);
262 dup2(pts, STDERR_FILENO);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800263
Benoit Gobyf4ded742011-02-01 18:57:41 -0800264 adb_close(pts);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800265 adb_close(ptm);
266
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700267 execl(cmd, cmd, arg0, arg1, NULL);
268 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
269 cmd, strerror(errno), errno);
270 exit(-1);
271 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800272 return ptm;
273 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800274#endif /* !defined(_WIN32) */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800275}
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700276
277static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
278{
279 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cui2fa43212014-11-11 09:24:11 -0800280#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700281 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
282 return -1;
Yabin Cui2fa43212014-11-11 09:24:11 -0800283#else
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700284
285 // 0 is parent socket, 1 is child socket
286 int sv[2];
leozwang1be54622014-08-15 09:51:27 -0700287 if (adb_socketpair(sv) < 0) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700288 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
289 return -1;
290 }
leozwang1be54622014-08-15 09:51:27 -0700291 D("socketpair: (%d,%d)", sv[0], sv[1]);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700292
293 *pid = fork();
294 if (*pid < 0) {
295 printf("- fork failed: %s -\n", strerror(errno));
296 adb_close(sv[0]);
297 adb_close(sv[1]);
298 return -1;
299 }
300
301 if (*pid == 0) {
302 adb_close(sv[0]);
303 init_subproc_child();
304
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700305 dup2(sv[1], STDIN_FILENO);
306 dup2(sv[1], STDOUT_FILENO);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -0700307 dup2(sv[1], STDERR_FILENO);
308
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700309 adb_close(sv[1]);
310
311 execl(cmd, cmd, arg0, arg1, NULL);
312 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
313 cmd, strerror(errno), errno);
314 exit(-1);
315 } else {
316 adb_close(sv[1]);
317 return sv[0];
318 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800319#endif /* !defined(_WIN32) */
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700320}
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700321#endif /* !ABD_HOST */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322
323#if ADB_HOST
324#define SHELL_COMMAND "/bin/sh"
325#else
326#define SHELL_COMMAND "/system/bin/sh"
327#endif
328
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700329#if !ADB_HOST
330static void subproc_waiter_service(int fd, void *cookie)
331{
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800332 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700333
334 D("entered. fd=%d of pid=%d\n", fd, pid);
335 for (;;) {
336 int status;
337 pid_t p = waitpid(pid, &status, 0);
338 if (p == pid) {
339 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
340 if (WIFSIGNALED(status)) {
341 D("*** Killed by signal %d\n", WTERMSIG(status));
342 break;
343 } else if (!WIFEXITED(status)) {
344 D("*** Didn't exit!!. status %d\n", status);
345 break;
346 } else if (WEXITSTATUS(status) >= 0) {
347 D("*** Exit code %d\n", WEXITSTATUS(status));
348 break;
349 }
350 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700351 }
352 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
353 if (SHELL_EXIT_NOTIFY_FD >=0) {
354 int res;
Dan Albert66a91b02015-02-24 21:26:58 -0800355 res = WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)) ? 0 : -1;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700356 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
357 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
358 }
359}
360
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700361static int create_subproc_thread(const char *name, const subproc_mode mode)
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700362{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700363 adb_thread_t t;
364 int ret_fd;
Jeff Sharkeyc52ec1a2014-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 Abgrall2e5dd6e2011-03-16 15:57:42 -0700370 } else {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700371 arg0 = "-c"; arg1 = name;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700372 }
Jeff Sharkeyc52ec1a2014-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 Abgrall2e5dd6e2011-03-16 15:57:42 -0700386
Dan Albertf30d73c2015-02-25 17:51:28 -0800387 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700388 if(sti == 0) fatal("cannot allocate stinfo");
389 sti->func = subproc_waiter_service;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800390 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700391 sti->fd = ret_fd;
392
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700393 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700394 free(sti);
395 adb_close(ret_fd);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700396 fprintf(stderr, "cannot create service thread\n");
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700397 return -1;
398 }
399
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700400 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700401 return ret_fd;
402}
403#endif
404
The Android Open Source Project9ca14dc2009-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 Project9ca14dc2009-03-03 19:32:55 -0800418 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-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 Goby12dc3692013-02-20 15:04:53 -0800437#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800438 } else if(!strncmp("dev:", name, 4)) {
Nick Kralevich777523e2014-07-18 20:57:35 -0700439 ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800440 } else if(!strncmp(name, "framebuffer:", 12)) {
441 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Project9ca14dc2009-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 Project9ca14dc2009-03-03 19:32:55 -0800444 } else if(!HOST && !strncmp(name, "shell:", 6)) {
Jeff Sharkeyc52ec1a2014-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 Project9ca14dc2009-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 Lockwood12a35ea2009-08-04 20:37:51 -0400452 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400453 void* arg = strdup(name + 7);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700454 if (arg == NULL) return -1;
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400455 ret = create_service_thread(reboot_service, arg);
The Android Open Source Project9c753402009-03-13 13:04:37 -0700456 } else if(!strncmp(name, "root:", 5)) {
457 ret = create_service_thread(restart_root_service, NULL);
Dan Pasanenfb787d92014-10-06 12:57:20 -0500458 } else if(!strncmp(name, "unroot:", 7)) {
459 ret = create_service_thread(restart_unroot_service, NULL);
Christopher Tate73779122011-04-21 12:53:28 -0700460 } else if(!strncmp(name, "backup:", 7)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700461 char* arg = strdup(name + 7);
Christopher Tate73779122011-04-21 12:53:28 -0700462 if (arg == NULL) return -1;
Jeff Sharkeyc52ec1a2014-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 Tatecf5379b2011-05-17 15:52:54 -0700474 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700475 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700476 } else if(!strncmp(name, "tcpip:", 6)) {
477 int port;
Spencer Low29a029c2015-01-25 17:38:36 -0800478 if (sscanf(name + 6, "%d", &port) != 1) {
Mike Lockwood26b88e32009-08-24 15:58:40 -0700479 port = 0;
480 }
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800481 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700482 } else if(!strncmp(name, "usb:", 4)) {
483 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner963a4492013-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 Lawrence5f356482014-10-09 14:22:49 +0000494 } else if(!strncmp(name, "disable-verity:", 15)) {
Paul Lawrence8ba2b022014-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 Project9ca14dc2009-03-03 19:32:55 -0800498#endif
The Android Open Source Project9ca14dc2009-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{
Dan Albertf30d73c2015-02-25 17:51:28 -0800515 state_info* sinfo = reinterpret_cast<state_info*>(cookie);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800516
517 D("wait_for_state %d\n", sinfo->state);
518
Dan Albertf30d73c2015-02-25 17:51:28 -0800519 const char* err = "unknown error";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800520 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
521 if(t != 0) {
Dan Albert66a91b02015-02-24 21:26:58 -0800522 WriteFdExactly(fd, "OKAY", 4);
The Android Open Source Project9ca14dc2009-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 Goby3f9f9ce2013-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 Low29a029c2015-01-25 17:38:36 -0800550 if (sscanf(portstr + 1, "%d", &port) != 1) {
Benoit Goby3f9f9ce2013-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 Liermane6cd0c82013-06-20 09:27:13 -0700560 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby3f9f9ce2013-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];
Dan Albertf30d73c2015-02-25 17:51:28 -0800638 char *host = reinterpret_cast<char*>(cookie);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700639
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 Albert66a91b02015-02-24 21:26:58 -0800648 WriteFdExactly(fd, resp, strlen(resp));
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700649 adb_close(fd);
650}
The Android Open Source Project9ca14dc2009-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-"))) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800659 auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800660
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 Goby3f9f9ce2013-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 Project9ca14dc2009-03-03 19:32:55 -0800688 }
689 return NULL;
690}
691#endif /* ADB_HOST */