blob: fa5a1a1100d7cb2bfc85146b5b7bef1c666681be [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
Dan Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_SERVICES
18
19#include "sysdeps.h"
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <errno.h>
Dan Albert76649012015-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
Elliott Hughese67f1f82015-04-30 17:32:03 -070034#include <base/file.h>
Elliott Hughes6c34bba2015-04-17 20:11:08 -070035#include <base/stringprintf.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070036#include <base/strings.h>
Elliott Hughes6c34bba2015-04-17 20:11:08 -070037
Dan Albert76649012015-02-24 15:51:19 -080038#if !ADB_HOST
39#include "cutils/android_reboot.h"
40#include "cutils/properties.h"
41#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080044#include "adb_io.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045#include "file_sync_service.h"
Elliott Hughesec7a6672015-03-16 21:58:32 +000046#include "remount_service.h"
Dan Albert76649012015-02-24 15:51:19 -080047#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049struct stinfo {
50 void (*func)(int fd, void *cookie);
51 int fd;
52 void *cookie;
53};
54
55
56void *service_bootstrap_func(void *x)
57{
Dan Albertbac34742015-02-25 17:51:28 -080058 stinfo* sti = reinterpret_cast<stinfo*>(x);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059 sti->func(sti->fd, sti->cookie);
60 free(sti);
61 return 0;
62}
63
Benoit Goby9470c2f2013-02-20 15:04:53 -080064#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070066void restart_root_service(int fd, void *cookie)
67{
68 char buf[100];
69 char value[PROPERTY_VALUE_MAX];
70
71 if (getuid() == 0) {
72 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
Dan Albertcc731cc2015-02-24 21:26:58 -080073 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070074 adb_close(fd);
75 } else {
76 property_get("ro.debuggable", value, "");
77 if (strcmp(value, "1") != 0) {
78 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
Dan Albertcc731cc2015-02-24 21:26:58 -080079 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -070080 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070081 return;
82 }
83
Benoit Goby7941cf82012-03-16 14:44:17 -070084 property_set("service.adb.root", "1");
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070085 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
Dan Albertcc731cc2015-02-24 21:26:58 -080086 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070087 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070088 }
89}
90
Dan Pasanen98858812014-10-06 12:57:20 -050091void restart_unroot_service(int fd, void *cookie)
92{
93 char buf[100];
94
95 if (getuid() != 0) {
96 snprintf(buf, sizeof(buf), "adbd not running as root\n");
Dan Albertcc731cc2015-02-24 21:26:58 -080097 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanen98858812014-10-06 12:57:20 -050098 adb_close(fd);
99 } else {
100 property_set("service.adb.root", "0");
101 snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
Dan Albertcc731cc2015-02-24 21:26:58 -0800102 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanen98858812014-10-06 12:57:20 -0500103 adb_close(fd);
104 }
105}
106
Mike Lockwoodff196702009-08-24 15:58:40 -0700107void restart_tcp_service(int fd, void *cookie)
108{
109 char buf[100];
110 char value[PROPERTY_VALUE_MAX];
Elliott Hughesccecf142014-01-16 10:53:11 -0800111 int port = (int) (uintptr_t) cookie;
Mike Lockwoodff196702009-08-24 15:58:40 -0700112
113 if (port <= 0) {
114 snprintf(buf, sizeof(buf), "invalid port\n");
Dan Albertcc731cc2015-02-24 21:26:58 -0800115 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -0700116 adb_close(fd);
117 return;
118 }
119
120 snprintf(value, sizeof(value), "%d", port);
121 property_set("service.adb.tcp.port", value);
122 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
Dan Albertcc731cc2015-02-24 21:26:58 -0800123 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -0700124 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700125}
126
127void restart_usb_service(int fd, void *cookie)
128{
129 char buf[100];
130
131 property_set("service.adb.tcp.port", "0");
132 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
Dan Albertcc731cc2015-02-24 21:26:58 -0800133 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -0700134 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700135}
136
Tao Bao175b7bb2015-03-29 11:22:34 -0700137static bool reboot_service_impl(int fd, const char* arg) {
138 const char* reboot_arg = arg;
139 bool auto_reboot = false;
140
141 if (strcmp(reboot_arg, "sideload-auto-reboot") == 0) {
142 auto_reboot = true;
143 reboot_arg = "sideload";
144 }
145
Mike Lockwoodee156622009-08-04 20:37:51 -0400146 char buf[100];
Tao Bao175b7bb2015-03-29 11:22:34 -0700147 // It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot"
148 // in the command file.
149 if (strcmp(reboot_arg, "sideload") == 0) {
150 if (getuid() != 0) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700151 WriteFdExactly(fd, "'adb root' is required for 'adb reboot sideload'.\n");
Tao Bao175b7bb2015-03-29 11:22:34 -0700152 return false;
153 }
154
155 const char* const recovery_dir = "/cache/recovery";
156 const char* const command_file = "/cache/recovery/command";
157 // Ensure /cache/recovery exists.
158 if (adb_mkdir(recovery_dir, 0770) == -1 && errno != EEXIST) {
159 D("Failed to create directory '%s': %s\n", recovery_dir, strerror(errno));
160 return false;
161 }
162
163 bool write_status = android::base::WriteStringToFile(
164 auto_reboot ? "--sideload_auto_reboot" : "--sideload", command_file);
165 if (!write_status) {
166 return false;
167 }
168
169 reboot_arg = "recovery";
170 }
Mike Lockwoodee156622009-08-04 20:37:51 -0400171
172 sync();
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500173
Tao Bao175b7bb2015-03-29 11:22:34 -0700174 char property_val[PROPERTY_VALUE_MAX];
175 int ret = snprintf(property_val, sizeof(property_val), "reboot,%s", reboot_arg);
176 if (ret >= static_cast<int>(sizeof(property_val))) {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700177 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700178 WriteFdExactly(fd, buf);
Tao Bao175b7bb2015-03-29 11:22:34 -0700179 return false;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700180 }
181
182 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwoodee156622009-08-04 20:37:51 -0400183 if (ret < 0) {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700184 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700185 WriteFdExactly(fd, buf);
Tao Bao175b7bb2015-03-29 11:22:34 -0700186 return false;
Mike Lockwoodee156622009-08-04 20:37:51 -0400187 }
Tao Bao175b7bb2015-03-29 11:22:34 -0700188
189 return true;
190}
191
192void reboot_service(int fd, void* arg)
193{
194 if (reboot_service_impl(fd, static_cast<const char*>(arg))) {
195 // Don't return early. Give the reboot command time to take effect
196 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
Elliott Hughesa7090b92015-04-17 17:03:59 -0700197 while (true) {
Tao Bao175b7bb2015-03-29 11:22:34 -0700198 pause();
199 }
200 }
201
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400202 free(arg);
Mike Lockwoodee156622009-08-04 20:37:51 -0400203 adb_close(fd);
204}
205
David 'Digit' Turner25258692013-03-21 21:07:42 +0100206void reverse_service(int fd, void* arg)
207{
Dan Albertbac34742015-02-25 17:51:28 -0800208 const char* command = reinterpret_cast<const char*>(arg);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100209
210 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700211 SendFail(fd, "not a reverse forwarding command");
David 'Digit' Turner25258692013-03-21 21:07:42 +0100212 }
213 free(arg);
214 adb_close(fd);
215}
216
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217#endif
218
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219static int create_service_thread(void (*func)(int, void *), void *cookie)
220{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 int s[2];
Dan Albertbac34742015-02-25 17:51:28 -0800222 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 printf("cannot create service socket pair\n");
224 return -1;
225 }
leozwangcbf02672014-08-15 09:51:27 -0700226 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227
Dan Albertbac34742015-02-25 17:51:28 -0800228 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
229 if (sti == nullptr) {
230 fatal("cannot allocate stinfo");
231 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 sti->func = func;
233 sti->cookie = cookie;
234 sti->fd = s[1];
235
Dan Albertbac34742015-02-25 17:51:28 -0800236 adb_thread_t t;
237 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 free(sti);
239 adb_close(s[0]);
240 adb_close(s[1]);
241 printf("cannot create service thread\n");
242 return -1;
243 }
244
245 D("service thread started, %d:%d\n",s[0], s[1]);
246 return s[0];
247}
248
JP Abgrall408fa572011-03-16 15:57:42 -0700249#if !ADB_HOST
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700250
251static void init_subproc_child()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252{
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700253 setsid();
254
Rom Lemarchand07ce7ca2014-06-20 16:30:54 -0700255 // Set OOM score adjustment to prevent killing
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700256 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700257 if (fd >= 0) {
258 adb_write(fd, "0", 1);
259 adb_close(fd);
260 } else {
Rom Lemarchand07ce7ca2014-06-20 16:30:54 -0700261 D("adb: unable to update oom_score_adj\n");
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700262 }
263}
264
265static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
266{
267 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cuie77b6a02014-11-11 09:24:11 -0800268#if defined(_WIN32)
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700269 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
JP Abgrall408fa572011-03-16 15:57:42 -0700270 return -1;
Yabin Cuie77b6a02014-11-11 09:24:11 -0800271#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 int ptm;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700274 ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 if(ptm < 0){
276 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
277 return -1;
278 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279
Elliott Hughesd2352882014-07-29 11:32:16 -0700280 char devname[64];
281 if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700283 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 return -1;
285 }
286
JP Abgrall408fa572011-03-16 15:57:42 -0700287 *pid = fork();
288 if(*pid < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700290 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 return -1;
292 }
293
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700294 if (*pid == 0) {
295 init_subproc_child();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700297 int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700298 if (pts < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -0700299 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
300 exit(-1);
301 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700303 dup2(pts, STDIN_FILENO);
304 dup2(pts, STDOUT_FILENO);
305 dup2(pts, STDERR_FILENO);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306
Benoit Goby95ef8282011-02-01 18:57:41 -0800307 adb_close(pts);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 adb_close(ptm);
309
JP Abgrall408fa572011-03-16 15:57:42 -0700310 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 {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 return ptm;
316 }
Yabin Cuie77b6a02014-11-11 09:24:11 -0800317#endif /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318}
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700319
320static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
321{
322 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cuie77b6a02014-11-11 09:24:11 -0800323#if defined(_WIN32)
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700324 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
325 return -1;
Yabin Cuie77b6a02014-11-11 09:24:11 -0800326#else
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700327
328 // 0 is parent socket, 1 is child socket
329 int sv[2];
leozwangcbf02672014-08-15 09:51:27 -0700330 if (adb_socketpair(sv) < 0) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700331 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
332 return -1;
333 }
leozwangcbf02672014-08-15 09:51:27 -0700334 D("socketpair: (%d,%d)", sv[0], sv[1]);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700335
336 *pid = fork();
337 if (*pid < 0) {
338 printf("- fork failed: %s -\n", strerror(errno));
339 adb_close(sv[0]);
340 adb_close(sv[1]);
341 return -1;
342 }
343
344 if (*pid == 0) {
345 adb_close(sv[0]);
346 init_subproc_child();
347
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700348 dup2(sv[1], STDIN_FILENO);
349 dup2(sv[1], STDOUT_FILENO);
Jeff Sharkey960df972014-06-09 17:30:57 -0700350 dup2(sv[1], STDERR_FILENO);
351
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700352 adb_close(sv[1]);
353
354 execl(cmd, cmd, arg0, arg1, NULL);
355 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
356 cmd, strerror(errno), errno);
357 exit(-1);
358 } else {
359 adb_close(sv[1]);
360 return sv[0];
361 }
Yabin Cuie77b6a02014-11-11 09:24:11 -0800362#endif /* !defined(_WIN32) */
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700363}
JP Abgrall408fa572011-03-16 15:57:42 -0700364#endif /* !ABD_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365
366#if ADB_HOST
367#define SHELL_COMMAND "/bin/sh"
368#else
369#define SHELL_COMMAND "/system/bin/sh"
370#endif
371
JP Abgrall408fa572011-03-16 15:57:42 -0700372#if !ADB_HOST
373static void subproc_waiter_service(int fd, void *cookie)
374{
Elliott Hughesccecf142014-01-16 10:53:11 -0800375 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall408fa572011-03-16 15:57:42 -0700376
377 D("entered. fd=%d of pid=%d\n", fd, pid);
Elliott Hughesa7090b92015-04-17 17:03:59 -0700378 while (true) {
JP Abgrall408fa572011-03-16 15:57:42 -0700379 int status;
380 pid_t p = waitpid(pid, &status, 0);
381 if (p == pid) {
382 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
383 if (WIFSIGNALED(status)) {
384 D("*** Killed by signal %d\n", WTERMSIG(status));
385 break;
386 } else if (!WIFEXITED(status)) {
387 D("*** Didn't exit!!. status %d\n", status);
388 break;
389 } else if (WEXITSTATUS(status) >= 0) {
390 D("*** Exit code %d\n", WEXITSTATUS(status));
391 break;
392 }
393 }
JP Abgrall408fa572011-03-16 15:57:42 -0700394 }
395 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
396 if (SHELL_EXIT_NOTIFY_FD >=0) {
397 int res;
Dan Albertcc731cc2015-02-24 21:26:58 -0800398 res = WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)) ? 0 : -1;
JP Abgrall408fa572011-03-16 15:57:42 -0700399 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
400 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
401 }
402}
403
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700404static int create_subproc_thread(const char *name, const subproc_mode mode)
JP Abgrall408fa572011-03-16 15:57:42 -0700405{
JP Abgrall408fa572011-03-16 15:57:42 -0700406 adb_thread_t t;
407 int ret_fd;
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700408 pid_t pid = -1;
409
410 const char *arg0, *arg1;
411 if (name == 0 || *name == 0) {
412 arg0 = "-"; arg1 = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700413 } else {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700414 arg0 = "-c"; arg1 = name;
JP Abgrall408fa572011-03-16 15:57:42 -0700415 }
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700416
417 switch (mode) {
418 case SUBPROC_PTY:
419 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
420 break;
421 case SUBPROC_RAW:
422 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
423 break;
424 default:
425 fprintf(stderr, "invalid subproc_mode %d\n", mode);
426 return -1;
427 }
428 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall408fa572011-03-16 15:57:42 -0700429
Dan Albertbac34742015-02-25 17:51:28 -0800430 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
JP Abgrall408fa572011-03-16 15:57:42 -0700431 if(sti == 0) fatal("cannot allocate stinfo");
432 sti->func = subproc_waiter_service;
Elliott Hughesccecf142014-01-16 10:53:11 -0800433 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall408fa572011-03-16 15:57:42 -0700434 sti->fd = ret_fd;
435
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700436 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
JP Abgrall408fa572011-03-16 15:57:42 -0700437 free(sti);
438 adb_close(ret_fd);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700439 fprintf(stderr, "cannot create service thread\n");
JP Abgrall408fa572011-03-16 15:57:42 -0700440 return -1;
441 }
442
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700443 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall408fa572011-03-16 15:57:42 -0700444 return ret_fd;
445}
446#endif
447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448int service_to_fd(const char *name)
449{
450 int ret = -1;
451
452 if(!strncmp(name, "tcp:", 4)) {
453 int port = atoi(name + 4);
454 name = strchr(name + 4, ':');
455 if(name == 0) {
456 ret = socket_loopback_client(port, SOCK_STREAM);
457 if (ret >= 0)
458 disable_tcp_nagle(ret);
459 } else {
460#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462#else
463 return -1;
464#endif
465 }
466#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
467 } else if(!strncmp(name, "local:", 6)) {
468 ret = socket_local_client(name + 6,
469 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
470 } else if(!strncmp(name, "localreserved:", 14)) {
471 ret = socket_local_client(name + 14,
472 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
473 } else if(!strncmp(name, "localabstract:", 14)) {
474 ret = socket_local_client(name + 14,
475 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
476 } else if(!strncmp(name, "localfilesystem:", 16)) {
477 ret = socket_local_client(name + 16,
478 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
479#endif
Benoit Goby9470c2f2013-02-20 15:04:53 -0800480#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 } else if(!strncmp("dev:", name, 4)) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700482 ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 } else if(!strncmp(name, "framebuffer:", 12)) {
484 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 } else if (!strncmp(name, "jdwp:", 5)) {
486 ret = create_jdwp_connection_fd(atoi(name+5));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 } else if(!HOST && !strncmp(name, "shell:", 6)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700488 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
489 } else if(!HOST && !strncmp(name, "exec:", 5)) {
490 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 } else if(!strncmp(name, "sync:", 5)) {
492 ret = create_service_thread(file_sync_service, NULL);
493 } else if(!strncmp(name, "remount:", 8)) {
494 ret = create_service_thread(remount_service, NULL);
Mike Lockwoodee156622009-08-04 20:37:51 -0400495 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400496 void* arg = strdup(name + 7);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700497 if (arg == NULL) return -1;
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400498 ret = create_service_thread(reboot_service, arg);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700499 } else if(!strncmp(name, "root:", 5)) {
500 ret = create_service_thread(restart_root_service, NULL);
Dan Pasanen98858812014-10-06 12:57:20 -0500501 } else if(!strncmp(name, "unroot:", 7)) {
502 ret = create_service_thread(restart_unroot_service, NULL);
Christopher Tated2f54152011-04-21 12:53:28 -0700503 } else if(!strncmp(name, "backup:", 7)) {
Elliott Hughes6c34bba2015-04-17 20:11:08 -0700504 ret = create_subproc_thread(android::base::StringPrintf("/system/bin/bu backup %s",
505 (name + 7)).c_str(), SUBPROC_RAW);
Christopher Tate702967a2011-05-17 15:52:54 -0700506 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700507 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwoodff196702009-08-24 15:58:40 -0700508 } else if(!strncmp(name, "tcpip:", 6)) {
509 int port;
Spencer Low943ef232015-01-25 17:38:36 -0800510 if (sscanf(name + 6, "%d", &port) != 1) {
Mike Lockwoodff196702009-08-24 15:58:40 -0700511 port = 0;
512 }
Elliott Hughesccecf142014-01-16 10:53:11 -0800513 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwoodff196702009-08-24 15:58:40 -0700514 } else if(!strncmp(name, "usb:", 4)) {
515 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100516 } else if (!strncmp(name, "reverse:", 8)) {
517 char* cookie = strdup(name + 8);
518 if (cookie == NULL) {
519 ret = -1;
520 } else {
521 ret = create_service_thread(reverse_service, cookie);
522 if (ret < 0) {
523 free(cookie);
524 }
525 }
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000526 } else if(!strncmp(name, "disable-verity:", 15)) {
Paul Lawrence982089d2014-12-03 15:31:57 -0800527 ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
528 } else if(!strncmp(name, "enable-verity:", 15)) {
529 ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 }
532 if (ret >= 0) {
533 close_on_exec(ret);
534 }
535 return ret;
536}
537
538#if ADB_HOST
539struct state_info {
540 transport_type transport;
541 char* serial;
542 int state;
543};
544
545static void wait_for_state(int fd, void* cookie)
546{
Dan Albertbac34742015-02-25 17:51:28 -0800547 state_info* sinfo = reinterpret_cast<state_info*>(cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548
549 D("wait_for_state %d\n", sinfo->state);
550
Elliott Hughes7be29c82015-04-16 22:54:44 -0700551 std::string error_msg = "unknown error";
552 atransport* t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &error_msg);
553 if (t != 0) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700554 SendOkay(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800555 } else {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700556 SendFail(fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 }
558
559 if (sinfo->serial)
560 free(sinfo->serial);
561 free(sinfo);
562 adb_close(fd);
563 D("wait_for_state is done\n");
564}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700565
Elliott Hughese67f1f82015-04-30 17:32:03 -0700566static void connect_device(const std::string& host, std::string* response) {
567 if (host.empty()) {
568 *response = "empty host name";
569 return;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700570 }
571
Elliott Hughese67f1f82015-04-30 17:32:03 -0700572 std::vector<std::string> pieces = android::base::Split(host, ":");
573 const std::string& hostname = pieces[0];
Benoit Goby1c45ee92013-03-29 18:22:36 -0700574
Elliott Hughese67f1f82015-04-30 17:32:03 -0700575 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
576 if (pieces.size() > 1) {
577 if (sscanf(pieces[1].c_str(), "%d", &port) != 1) {
578 *response = android::base::StringPrintf("bad port number %s", pieces[1].c_str());
579 return;
580 }
581 }
582
583 // This may look like we're putting 'host' back together,
584 // but we're actually inserting the default port if necessary.
585 std::string serial = android::base::StringPrintf("%s:%d", hostname.c_str(), port);
586
587 int fd = socket_network_client_timeout(hostname.c_str(), port, SOCK_STREAM, 10);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700588 if (fd < 0) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700589 *response = android::base::StringPrintf("unable to connect to %s:%d",
590 hostname.c_str(), port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700591 return;
592 }
593
594 D("client: connected on remote on fd %d\n", fd);
595 close_on_exec(fd);
596 disable_tcp_nagle(fd);
597
Elliott Hughese67f1f82015-04-30 17:32:03 -0700598 int ret = register_socket_transport(fd, serial.c_str(), port, 0);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700599 if (ret < 0) {
600 adb_close(fd);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700601 *response = android::base::StringPrintf("already connected to %s", serial.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700602 } else {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700603 *response = android::base::StringPrintf("connected to %s", serial.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700604 }
605}
606
Elliott Hughese67f1f82015-04-30 17:32:03 -0700607void connect_emulator(const std::string& port_spec, std::string* response) {
608 std::vector<std::string> pieces = android::base::Split(port_spec, ",");
609 if (pieces.size() != 2) {
610 *response = android::base::StringPrintf("unable to parse '%s' as <console port>,<adb port>",
611 port_spec.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700612 return;
613 }
614
Elliott Hughese67f1f82015-04-30 17:32:03 -0700615 int console_port = strtol(pieces[0].c_str(), NULL, 0);
616 int adb_port = strtol(pieces[1].c_str(), NULL, 0);
617 if (console_port <= 0 || adb_port <= 0) {
618 *response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str());
Benoit Goby1c45ee92013-03-29 18:22:36 -0700619 return;
620 }
621
Elliott Hughese67f1f82015-04-30 17:32:03 -0700622 // Check if the emulator is already known.
623 // Note: There's a small but harmless race condition here: An emulator not
624 // present just yet could be registered by another invocation right
625 // after doing this check here. However, local_connect protects
626 // against double-registration too. From here, a better error message
627 // can be produced. In the case of the race condition, the very specific
628 // error message won't be shown, but the data doesn't get corrupted.
Benoit Goby1c45ee92013-03-29 18:22:36 -0700629 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700630 if (known_emulator != nullptr) {
631 *response = android::base::StringPrintf("Emulator already registered on port %d", adb_port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700632 return;
633 }
634
Elliott Hughese67f1f82015-04-30 17:32:03 -0700635 // Check if more emulators can be registered. Similar unproblematic
636 // race condition as above.
Benoit Goby1c45ee92013-03-29 18:22:36 -0700637 int candidate_slot = get_available_local_transport_index();
638 if (candidate_slot < 0) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700639 *response = "Cannot accept more emulators";
Benoit Goby1c45ee92013-03-29 18:22:36 -0700640 return;
641 }
642
Elliott Hughese67f1f82015-04-30 17:32:03 -0700643 // Preconditions met, try to connect to the emulator.
Benoit Goby1c45ee92013-03-29 18:22:36 -0700644 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700645 *response = android::base::StringPrintf("Connected to emulator on ports %d,%d",
646 console_port, adb_port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700647 } else {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700648 *response = android::base::StringPrintf("Could not connect to emulator on ports %d,%d",
649 console_port, adb_port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700650 }
651}
652
653static void connect_service(int fd, void* cookie)
654{
Dan Albertbac34742015-02-25 17:51:28 -0800655 char *host = reinterpret_cast<char*>(cookie);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700656
Elliott Hughese67f1f82015-04-30 17:32:03 -0700657 std::string response;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700658 if (!strncmp(host, "emu:", 4)) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700659 connect_emulator(host + 4, &response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700660 } else {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700661 connect_device(host, &response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700662 }
663
664 // Send response for emulator and device
Elliott Hughese67f1f82015-04-30 17:32:03 -0700665 SendProtocolString(fd, response);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700666 adb_close(fd);
667}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668#endif
669
670#if ADB_HOST
671asocket* host_service_to_socket(const char* name, const char *serial)
672{
673 if (!strcmp(name,"track-devices")) {
674 return create_device_tracker();
675 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
Dan Albertbac34742015-02-25 17:51:28 -0800676 auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info)));
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700677 if (sinfo == nullptr) {
678 fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno));
679 return NULL;
680 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681
682 if (serial)
683 sinfo->serial = strdup(serial);
684 else
685 sinfo->serial = NULL;
686
687 name += strlen("wait-for-");
688
689 if (!strncmp(name, "local", strlen("local"))) {
690 sinfo->transport = kTransportLocal;
691 sinfo->state = CS_DEVICE;
692 } else if (!strncmp(name, "usb", strlen("usb"))) {
693 sinfo->transport = kTransportUsb;
694 sinfo->state = CS_DEVICE;
695 } else if (!strncmp(name, "any", strlen("any"))) {
696 sinfo->transport = kTransportAny;
697 sinfo->state = CS_DEVICE;
698 } else {
699 free(sinfo);
700 return NULL;
701 }
702
703 int fd = create_service_thread(wait_for_state, sinfo);
704 return create_local_socket(fd);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700705 } else if (!strncmp(name, "connect:", 8)) {
706 const char *host = name + 8;
707 int fd = create_service_thread(connect_service, (void *)host);
708 return create_local_socket(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 }
710 return NULL;
711}
712#endif /* ADB_HOST */