blob: fa0e73f75b8d2385cae54aee6179aee89c576b88 [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
Tao Bao0db67642015-03-29 11:22:34 -070035#include "base/file.h"
Dan Albertb302d122015-02-24 15:51:19 -080036#include "cutils/android_reboot.h"
37#include "cutils/properties.h"
38#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080039
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080040#include "adb.h"
Dan Albert66a91b02015-02-24 21:26:58 -080041#include "adb_io.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042#include "file_sync_service.h"
Elliott Hughesdedf7672015-03-16 21:58:32 +000043#include "remount_service.h"
Dan Albertb302d122015-02-24 15:51:19 -080044#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080045
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046struct 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
Tao Bao0db67642015-03-29 11:22:34 -0700134static bool reboot_service_impl(int fd, const char* arg) {
135 const char* reboot_arg = arg;
136 bool auto_reboot = false;
137
138 if (strcmp(reboot_arg, "sideload-auto-reboot") == 0) {
139 auto_reboot = true;
140 reboot_arg = "sideload";
141 }
142
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400143 char buf[100];
Tao Bao0db67642015-03-29 11:22:34 -0700144 // It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot"
145 // in the command file.
146 if (strcmp(reboot_arg, "sideload") == 0) {
147 if (getuid() != 0) {
148 snprintf(buf, sizeof(buf), "'adb root' is required for 'adb reboot sideload'.\n");
149 WriteStringFully(fd, buf);
150 return false;
151 }
152
153 const char* const recovery_dir = "/cache/recovery";
154 const char* const command_file = "/cache/recovery/command";
155 // Ensure /cache/recovery exists.
156 if (adb_mkdir(recovery_dir, 0770) == -1 && errno != EEXIST) {
157 D("Failed to create directory '%s': %s\n", recovery_dir, strerror(errno));
158 return false;
159 }
160
161 bool write_status = android::base::WriteStringToFile(
162 auto_reboot ? "--sideload_auto_reboot" : "--sideload", command_file);
163 if (!write_status) {
164 return false;
165 }
166
167 reboot_arg = "recovery";
168 }
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400169
170 sync();
Mike Lockwoodd49e9eb2010-02-24 16:07:23 -0500171
Tao Bao0db67642015-03-29 11:22:34 -0700172 char property_val[PROPERTY_VALUE_MAX];
173 int ret = snprintf(property_val, sizeof(property_val), "reboot,%s", reboot_arg);
174 if (ret >= static_cast<int>(sizeof(property_val))) {
Nick Kralevichb7df0172013-04-18 12:20:02 -0700175 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
Tao Bao0db67642015-03-29 11:22:34 -0700176 WriteStringFully(fd, buf);
177 return false;
Nick Kralevichb7df0172013-04-18 12:20:02 -0700178 }
179
180 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400181 if (ret < 0) {
Nick Kralevichb7df0172013-04-18 12:20:02 -0700182 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Tao Bao0db67642015-03-29 11:22:34 -0700183 WriteStringFully(fd, buf);
184 return false;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400185 }
Tao Bao0db67642015-03-29 11:22:34 -0700186
187 return true;
188}
189
190void reboot_service(int fd, void* arg)
191{
192 if (reboot_service_impl(fd, static_cast<const char*>(arg))) {
193 // Don't return early. Give the reboot command time to take effect
194 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
Elliott Hughes7cf35752015-04-17 17:03:59 -0700195 while (true) {
Tao Bao0db67642015-03-29 11:22:34 -0700196 pause();
197 }
198 }
199
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400200 free(arg);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400201 adb_close(fd);
202}
203
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100204void reverse_service(int fd, void* arg)
205{
Dan Albertf30d73c2015-02-25 17:51:28 -0800206 const char* command = reinterpret_cast<const char*>(arg);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100207
208 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
209 sendfailmsg(fd, "not a reverse forwarding command");
210 }
211 free(arg);
212 adb_close(fd);
213}
214
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215#endif
216
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800217static int create_service_thread(void (*func)(int, void *), void *cookie)
218{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800219 int s[2];
Dan Albertf30d73c2015-02-25 17:51:28 -0800220 if (adb_socketpair(s)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800221 printf("cannot create service socket pair\n");
222 return -1;
223 }
leozwang1be54622014-08-15 09:51:27 -0700224 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800225
Dan Albertf30d73c2015-02-25 17:51:28 -0800226 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
227 if (sti == nullptr) {
228 fatal("cannot allocate stinfo");
229 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800230 sti->func = func;
231 sti->cookie = cookie;
232 sti->fd = s[1];
233
Dan Albertf30d73c2015-02-25 17:51:28 -0800234 adb_thread_t t;
235 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236 free(sti);
237 adb_close(s[0]);
238 adb_close(s[1]);
239 printf("cannot create service thread\n");
240 return -1;
241 }
242
243 D("service thread started, %d:%d\n",s[0], s[1]);
244 return s[0];
245}
246
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700247#if !ADB_HOST
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700248
249static void init_subproc_child()
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800250{
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700251 setsid();
252
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700253 // Set OOM score adjustment to prevent killing
Nick Kralevich777523e2014-07-18 20:57:35 -0700254 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700255 if (fd >= 0) {
256 adb_write(fd, "0", 1);
257 adb_close(fd);
258 } else {
Rom Lemarchand9d837e52014-06-20 16:30:54 -0700259 D("adb: unable to update oom_score_adj\n");
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700260 }
261}
262
263static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
264{
265 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cui2fa43212014-11-11 09:24:11 -0800266#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700267 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700268 return -1;
Yabin Cui2fa43212014-11-11 09:24:11 -0800269#else
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800270 int ptm;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800271
Nick Kralevich777523e2014-07-18 20:57:35 -0700272 ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800273 if(ptm < 0){
274 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
275 return -1;
276 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800277
Elliott Hughes1f59fa02014-07-29 11:32:16 -0700278 char devname[64];
279 if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800280 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700281 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800282 return -1;
283 }
284
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700285 *pid = fork();
286 if(*pid < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800287 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700288 adb_close(ptm);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800289 return -1;
290 }
291
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700292 if (*pid == 0) {
293 init_subproc_child();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800294
Nick Kralevich777523e2014-07-18 20:57:35 -0700295 int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700296 if (pts < 0) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700297 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
298 exit(-1);
299 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700301 dup2(pts, STDIN_FILENO);
302 dup2(pts, STDOUT_FILENO);
303 dup2(pts, STDERR_FILENO);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800304
Benoit Gobyf4ded742011-02-01 18:57:41 -0800305 adb_close(pts);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800306 adb_close(ptm);
307
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700308 execl(cmd, cmd, arg0, arg1, NULL);
309 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
310 cmd, strerror(errno), errno);
311 exit(-1);
312 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800313 return ptm;
314 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800315#endif /* !defined(_WIN32) */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800316}
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700317
318static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
319{
320 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cui2fa43212014-11-11 09:24:11 -0800321#if defined(_WIN32)
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700322 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
323 return -1;
Yabin Cui2fa43212014-11-11 09:24:11 -0800324#else
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700325
326 // 0 is parent socket, 1 is child socket
327 int sv[2];
leozwang1be54622014-08-15 09:51:27 -0700328 if (adb_socketpair(sv) < 0) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700329 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
330 return -1;
331 }
leozwang1be54622014-08-15 09:51:27 -0700332 D("socketpair: (%d,%d)", sv[0], sv[1]);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700333
334 *pid = fork();
335 if (*pid < 0) {
336 printf("- fork failed: %s -\n", strerror(errno));
337 adb_close(sv[0]);
338 adb_close(sv[1]);
339 return -1;
340 }
341
342 if (*pid == 0) {
343 adb_close(sv[0]);
344 init_subproc_child();
345
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700346 dup2(sv[1], STDIN_FILENO);
347 dup2(sv[1], STDOUT_FILENO);
Jeff Sharkey0e0d2512014-06-09 17:30:57 -0700348 dup2(sv[1], STDERR_FILENO);
349
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700350 adb_close(sv[1]);
351
352 execl(cmd, cmd, arg0, arg1, NULL);
353 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
354 cmd, strerror(errno), errno);
355 exit(-1);
356 } else {
357 adb_close(sv[1]);
358 return sv[0];
359 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800360#endif /* !defined(_WIN32) */
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700361}
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700362#endif /* !ABD_HOST */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800363
364#if ADB_HOST
365#define SHELL_COMMAND "/bin/sh"
366#else
367#define SHELL_COMMAND "/system/bin/sh"
368#endif
369
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700370#if !ADB_HOST
371static void subproc_waiter_service(int fd, void *cookie)
372{
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800373 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700374
375 D("entered. fd=%d of pid=%d\n", fd, pid);
Elliott Hughes7cf35752015-04-17 17:03:59 -0700376 while (true) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700377 int status;
378 pid_t p = waitpid(pid, &status, 0);
379 if (p == pid) {
380 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
381 if (WIFSIGNALED(status)) {
382 D("*** Killed by signal %d\n", WTERMSIG(status));
383 break;
384 } else if (!WIFEXITED(status)) {
385 D("*** Didn't exit!!. status %d\n", status);
386 break;
387 } else if (WEXITSTATUS(status) >= 0) {
388 D("*** Exit code %d\n", WEXITSTATUS(status));
389 break;
390 }
391 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700392 }
393 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
394 if (SHELL_EXIT_NOTIFY_FD >=0) {
395 int res;
Dan Albert66a91b02015-02-24 21:26:58 -0800396 res = WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)) ? 0 : -1;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700397 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
398 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
399 }
400}
401
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700402static int create_subproc_thread(const char *name, const subproc_mode mode)
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700403{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700404 adb_thread_t t;
405 int ret_fd;
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700406 pid_t pid = -1;
407
408 const char *arg0, *arg1;
409 if (name == 0 || *name == 0) {
410 arg0 = "-"; arg1 = 0;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700411 } else {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700412 arg0 = "-c"; arg1 = name;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700413 }
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700414
415 switch (mode) {
416 case SUBPROC_PTY:
417 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
418 break;
419 case SUBPROC_RAW:
420 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
421 break;
422 default:
423 fprintf(stderr, "invalid subproc_mode %d\n", mode);
424 return -1;
425 }
426 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700427
Dan Albertf30d73c2015-02-25 17:51:28 -0800428 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700429 if(sti == 0) fatal("cannot allocate stinfo");
430 sti->func = subproc_waiter_service;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800431 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700432 sti->fd = ret_fd;
433
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700434 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700435 free(sti);
436 adb_close(ret_fd);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700437 fprintf(stderr, "cannot create service thread\n");
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700438 return -1;
439 }
440
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700441 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700442 return ret_fd;
443}
444#endif
445
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800446int service_to_fd(const char *name)
447{
448 int ret = -1;
449
450 if(!strncmp(name, "tcp:", 4)) {
451 int port = atoi(name + 4);
452 name = strchr(name + 4, ':');
453 if(name == 0) {
454 ret = socket_loopback_client(port, SOCK_STREAM);
455 if (ret >= 0)
456 disable_tcp_nagle(ret);
457 } else {
458#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800459 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800460#else
461 return -1;
462#endif
463 }
464#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
465 } else if(!strncmp(name, "local:", 6)) {
466 ret = socket_local_client(name + 6,
467 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
468 } else if(!strncmp(name, "localreserved:", 14)) {
469 ret = socket_local_client(name + 14,
470 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
471 } else if(!strncmp(name, "localabstract:", 14)) {
472 ret = socket_local_client(name + 14,
473 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
474 } else if(!strncmp(name, "localfilesystem:", 16)) {
475 ret = socket_local_client(name + 16,
476 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
477#endif
Benoit Goby12dc3692013-02-20 15:04:53 -0800478#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800479 } else if(!strncmp("dev:", name, 4)) {
Nick Kralevich777523e2014-07-18 20:57:35 -0700480 ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800481 } else if(!strncmp(name, "framebuffer:", 12)) {
482 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800483 } else if (!strncmp(name, "jdwp:", 5)) {
484 ret = create_jdwp_connection_fd(atoi(name+5));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800485 } else if(!HOST && !strncmp(name, "shell:", 6)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700486 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
487 } else if(!HOST && !strncmp(name, "exec:", 5)) {
488 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800489 } else if(!strncmp(name, "sync:", 5)) {
490 ret = create_service_thread(file_sync_service, NULL);
491 } else if(!strncmp(name, "remount:", 8)) {
492 ret = create_service_thread(remount_service, NULL);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400493 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400494 void* arg = strdup(name + 7);
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700495 if (arg == NULL) return -1;
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400496 ret = create_service_thread(reboot_service, arg);
The Android Open Source Project9c753402009-03-13 13:04:37 -0700497 } else if(!strncmp(name, "root:", 5)) {
498 ret = create_service_thread(restart_root_service, NULL);
Dan Pasanenfb787d92014-10-06 12:57:20 -0500499 } else if(!strncmp(name, "unroot:", 7)) {
500 ret = create_service_thread(restart_unroot_service, NULL);
Christopher Tate73779122011-04-21 12:53:28 -0700501 } else if(!strncmp(name, "backup:", 7)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700502 char* arg = strdup(name + 7);
Christopher Tate73779122011-04-21 12:53:28 -0700503 if (arg == NULL) return -1;
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700504 char* c = arg;
505 for (; *c != '\0'; c++) {
506 if (*c == ':')
507 *c = ' ';
508 }
509 char* cmd;
510 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
511 ret = create_subproc_thread(cmd, SUBPROC_RAW);
512 free(cmd);
513 }
514 free(arg);
Christopher Tatecf5379b2011-05-17 15:52:54 -0700515 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkeyc52ec1a2014-05-26 18:30:43 -0700516 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700517 } else if(!strncmp(name, "tcpip:", 6)) {
518 int port;
Spencer Low29a029c2015-01-25 17:38:36 -0800519 if (sscanf(name + 6, "%d", &port) != 1) {
Mike Lockwood26b88e32009-08-24 15:58:40 -0700520 port = 0;
521 }
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800522 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700523 } else if(!strncmp(name, "usb:", 4)) {
524 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100525 } else if (!strncmp(name, "reverse:", 8)) {
526 char* cookie = strdup(name + 8);
527 if (cookie == NULL) {
528 ret = -1;
529 } else {
530 ret = create_service_thread(reverse_service, cookie);
531 if (ret < 0) {
532 free(cookie);
533 }
534 }
Paul Lawrence5f356482014-10-09 14:22:49 +0000535 } else if(!strncmp(name, "disable-verity:", 15)) {
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800536 ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
537 } else if(!strncmp(name, "enable-verity:", 15)) {
538 ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800539#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800540 }
541 if (ret >= 0) {
542 close_on_exec(ret);
543 }
544 return ret;
545}
546
547#if ADB_HOST
548struct state_info {
549 transport_type transport;
550 char* serial;
551 int state;
552};
553
554static void wait_for_state(int fd, void* cookie)
555{
Dan Albertf30d73c2015-02-25 17:51:28 -0800556 state_info* sinfo = reinterpret_cast<state_info*>(cookie);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800557
558 D("wait_for_state %d\n", sinfo->state);
559
Elliott Hughesab882422015-04-16 22:54:44 -0700560 std::string error_msg = "unknown error";
561 atransport* t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &error_msg);
562 if (t != 0) {
Dan Albert66a91b02015-02-24 21:26:58 -0800563 WriteFdExactly(fd, "OKAY", 4);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800564 } else {
Elliott Hughesab882422015-04-16 22:54:44 -0700565 sendfailmsg(fd, error_msg.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800566 }
567
568 if (sinfo->serial)
569 free(sinfo->serial);
570 free(sinfo);
571 adb_close(fd);
572 D("wait_for_state is done\n");
573}
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700574
575static void connect_device(char* host, char* buffer, int buffer_size)
576{
577 int port, fd;
578 char* portstr = strchr(host, ':');
579 char hostbuf[100];
580 char serial[100];
581 int ret;
582
583 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
584 if (portstr) {
585 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
586 snprintf(buffer, buffer_size, "bad host name %s", host);
587 return;
588 }
589 // zero terminate the host at the point we found the colon
590 hostbuf[portstr - host] = 0;
Spencer Low29a029c2015-01-25 17:38:36 -0800591 if (sscanf(portstr + 1, "%d", &port) != 1) {
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700592 snprintf(buffer, buffer_size, "bad port number %s", portstr);
593 return;
594 }
595 } else {
596 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
597 }
598
599 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
600
Ken Liermane6cd0c82013-06-20 09:27:13 -0700601 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700602 if (fd < 0) {
603 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
604 return;
605 }
606
607 D("client: connected on remote on fd %d\n", fd);
608 close_on_exec(fd);
609 disable_tcp_nagle(fd);
610
611 ret = register_socket_transport(fd, serial, port, 0);
612 if (ret < 0) {
613 adb_close(fd);
614 snprintf(buffer, buffer_size, "already connected to %s", serial);
615 } else {
616 snprintf(buffer, buffer_size, "connected to %s", serial);
617 }
618}
619
620void connect_emulator(char* port_spec, char* buffer, int buffer_size)
621{
622 char* port_separator = strchr(port_spec, ',');
623 if (!port_separator) {
624 snprintf(buffer, buffer_size,
625 "unable to parse '%s' as <console port>,<adb port>",
626 port_spec);
627 return;
628 }
629
630 // Zero-terminate console port and make port_separator point to 2nd port.
631 *port_separator++ = 0;
632 int console_port = strtol(port_spec, NULL, 0);
633 int adb_port = strtol(port_separator, NULL, 0);
634 if (!(console_port > 0 && adb_port > 0)) {
635 *(port_separator - 1) = ',';
636 snprintf(buffer, buffer_size,
637 "Invalid port numbers: Expected positive numbers, got '%s'",
638 port_spec);
639 return;
640 }
641
642 /* Check if the emulator is already known.
643 * Note: There's a small but harmless race condition here: An emulator not
644 * present just yet could be registered by another invocation right
645 * after doing this check here. However, local_connect protects
646 * against double-registration too. From here, a better error message
647 * can be produced. In the case of the race condition, the very specific
648 * error message won't be shown, but the data doesn't get corrupted. */
649 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
650 if (known_emulator != NULL) {
651 snprintf(buffer, buffer_size,
652 "Emulator on port %d already registered.", adb_port);
653 return;
654 }
655
656 /* Check if more emulators can be registered. Similar unproblematic
657 * race condition as above. */
658 int candidate_slot = get_available_local_transport_index();
659 if (candidate_slot < 0) {
660 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
661 return;
662 }
663
664 /* Preconditions met, try to connect to the emulator. */
665 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
666 snprintf(buffer, buffer_size,
667 "Connected to emulator on ports %d,%d", console_port, adb_port);
668 } else {
669 snprintf(buffer, buffer_size,
670 "Could not connect to emulator on ports %d,%d",
671 console_port, adb_port);
672 }
673}
674
675static void connect_service(int fd, void* cookie)
676{
677 char buf[4096];
678 char resp[4096];
Dan Albertf30d73c2015-02-25 17:51:28 -0800679 char *host = reinterpret_cast<char*>(cookie);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700680
681 if (!strncmp(host, "emu:", 4)) {
682 connect_emulator(host + 4, buf, sizeof(buf));
683 } else {
684 connect_device(host, buf, sizeof(buf));
685 }
686
687 // Send response for emulator and device
688 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
Dan Albert66a91b02015-02-24 21:26:58 -0800689 WriteFdExactly(fd, resp, strlen(resp));
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700690 adb_close(fd);
691}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800692#endif
693
694#if ADB_HOST
695asocket* host_service_to_socket(const char* name, const char *serial)
696{
697 if (!strcmp(name,"track-devices")) {
698 return create_device_tracker();
699 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800700 auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800701
702 if (serial)
703 sinfo->serial = strdup(serial);
704 else
705 sinfo->serial = NULL;
706
707 name += strlen("wait-for-");
708
709 if (!strncmp(name, "local", strlen("local"))) {
710 sinfo->transport = kTransportLocal;
711 sinfo->state = CS_DEVICE;
712 } else if (!strncmp(name, "usb", strlen("usb"))) {
713 sinfo->transport = kTransportUsb;
714 sinfo->state = CS_DEVICE;
715 } else if (!strncmp(name, "any", strlen("any"))) {
716 sinfo->transport = kTransportAny;
717 sinfo->state = CS_DEVICE;
718 } else {
719 free(sinfo);
720 return NULL;
721 }
722
723 int fd = create_service_thread(wait_for_state, sinfo);
724 return create_local_socket(fd);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700725 } else if (!strncmp(name, "connect:", 8)) {
726 const char *host = name + 8;
727 int fd = create_service_thread(connect_service, (void *)host);
728 return create_local_socket(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800729 }
730 return NULL;
731}
732#endif /* ADB_HOST */