blob: 12eb406ff79a4f1f9ae4ce54f370252fa4e2929e [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
34#if !ADB_HOST
Tao Bao175b7bb2015-03-29 11:22:34 -070035#include "base/file.h"
Dan Albert76649012015-02-24 15:51:19 -080036#include "cutils/android_reboot.h"
37#include "cutils/properties.h"
38#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080041#include "adb_io.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include "file_sync_service.h"
Elliott Hughesec7a6672015-03-16 21:58:32 +000043#include "remount_service.h"
Dan Albert76649012015-02-24 15:51:19 -080044#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
46typedef struct stinfo stinfo;
47
48struct stinfo {
49 void (*func)(int fd, void *cookie);
50 int fd;
51 void *cookie;
52};
53
54
55void *service_bootstrap_func(void *x)
56{
Dan Albertbac34742015-02-25 17:51:28 -080057 stinfo* sti = reinterpret_cast<stinfo*>(x);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058 sti->func(sti->fd, sti->cookie);
59 free(sti);
60 return 0;
61}
62
Benoit Goby9470c2f2013-02-20 15:04:53 -080063#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070065void restart_root_service(int fd, void *cookie)
66{
67 char buf[100];
68 char value[PROPERTY_VALUE_MAX];
69
70 if (getuid() == 0) {
71 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
Dan Albertcc731cc2015-02-24 21:26:58 -080072 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070073 adb_close(fd);
74 } else {
75 property_get("ro.debuggable", value, "");
76 if (strcmp(value, "1") != 0) {
77 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
Dan Albertcc731cc2015-02-24 21:26:58 -080078 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -070079 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070080 return;
81 }
82
Benoit Goby7941cf82012-03-16 14:44:17 -070083 property_set("service.adb.root", "1");
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070084 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
Dan Albertcc731cc2015-02-24 21:26:58 -080085 WriteFdExactly(fd, buf, strlen(buf));
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070086 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070087 }
88}
89
Dan Pasanen98858812014-10-06 12:57:20 -050090void restart_unroot_service(int fd, void *cookie)
91{
92 char buf[100];
93
94 if (getuid() != 0) {
95 snprintf(buf, sizeof(buf), "adbd not running as root\n");
Dan Albertcc731cc2015-02-24 21:26:58 -080096 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanen98858812014-10-06 12:57:20 -050097 adb_close(fd);
98 } else {
99 property_set("service.adb.root", "0");
100 snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
Dan Albertcc731cc2015-02-24 21:26:58 -0800101 WriteFdExactly(fd, buf, strlen(buf));
Dan Pasanen98858812014-10-06 12:57:20 -0500102 adb_close(fd);
103 }
104}
105
Mike Lockwoodff196702009-08-24 15:58:40 -0700106void restart_tcp_service(int fd, void *cookie)
107{
108 char buf[100];
109 char value[PROPERTY_VALUE_MAX];
Elliott Hughesccecf142014-01-16 10:53:11 -0800110 int port = (int) (uintptr_t) cookie;
Mike Lockwoodff196702009-08-24 15:58:40 -0700111
112 if (port <= 0) {
113 snprintf(buf, sizeof(buf), "invalid port\n");
Dan Albertcc731cc2015-02-24 21:26:58 -0800114 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -0700115 adb_close(fd);
116 return;
117 }
118
119 snprintf(value, sizeof(value), "%d", port);
120 property_set("service.adb.tcp.port", value);
121 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
Dan Albertcc731cc2015-02-24 21:26:58 -0800122 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -0700123 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700124}
125
126void restart_usb_service(int fd, void *cookie)
127{
128 char buf[100];
129
130 property_set("service.adb.tcp.port", "0");
131 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
Dan Albertcc731cc2015-02-24 21:26:58 -0800132 WriteFdExactly(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -0700133 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700134}
135
Tao Bao175b7bb2015-03-29 11:22:34 -0700136static bool reboot_service_impl(int fd, const char* arg) {
137 const char* reboot_arg = arg;
138 bool auto_reboot = false;
139
140 if (strcmp(reboot_arg, "sideload-auto-reboot") == 0) {
141 auto_reboot = true;
142 reboot_arg = "sideload";
143 }
144
Mike Lockwoodee156622009-08-04 20:37:51 -0400145 char buf[100];
Tao Bao175b7bb2015-03-29 11:22:34 -0700146 // It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot"
147 // in the command file.
148 if (strcmp(reboot_arg, "sideload") == 0) {
149 if (getuid() != 0) {
150 snprintf(buf, sizeof(buf), "'adb root' is required for 'adb reboot sideload'.\n");
151 WriteStringFully(fd, buf);
152 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);
Tao Bao175b7bb2015-03-29 11:22:34 -0700178 WriteStringFully(fd, buf);
179 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);
Tao Bao175b7bb2015-03-29 11:22:34 -0700185 WriteStringFully(fd, buf);
186 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"
197 while (1) {
198 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) {
211 sendfailmsg(fd, "not a reverse forwarding command");
212 }
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);
378 for (;;) {
379 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)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700504 char* arg = strdup(name + 7);
Christopher Tated2f54152011-04-21 12:53:28 -0700505 if (arg == NULL) return -1;
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700506 char* c = arg;
507 for (; *c != '\0'; c++) {
508 if (*c == ':')
509 *c = ' ';
510 }
511 char* cmd;
512 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
513 ret = create_subproc_thread(cmd, SUBPROC_RAW);
514 free(cmd);
515 }
516 free(arg);
Christopher Tate702967a2011-05-17 15:52:54 -0700517 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700518 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwoodff196702009-08-24 15:58:40 -0700519 } else if(!strncmp(name, "tcpip:", 6)) {
520 int port;
Spencer Low943ef232015-01-25 17:38:36 -0800521 if (sscanf(name + 6, "%d", &port) != 1) {
Mike Lockwoodff196702009-08-24 15:58:40 -0700522 port = 0;
523 }
Elliott Hughesccecf142014-01-16 10:53:11 -0800524 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwoodff196702009-08-24 15:58:40 -0700525 } else if(!strncmp(name, "usb:", 4)) {
526 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100527 } else if (!strncmp(name, "reverse:", 8)) {
528 char* cookie = strdup(name + 8);
529 if (cookie == NULL) {
530 ret = -1;
531 } else {
532 ret = create_service_thread(reverse_service, cookie);
533 if (ret < 0) {
534 free(cookie);
535 }
536 }
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000537 } else if(!strncmp(name, "disable-verity:", 15)) {
Paul Lawrence982089d2014-12-03 15:31:57 -0800538 ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
539 } else if(!strncmp(name, "enable-verity:", 15)) {
540 ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 }
543 if (ret >= 0) {
544 close_on_exec(ret);
545 }
546 return ret;
547}
548
549#if ADB_HOST
550struct state_info {
551 transport_type transport;
552 char* serial;
553 int state;
554};
555
556static void wait_for_state(int fd, void* cookie)
557{
Dan Albertbac34742015-02-25 17:51:28 -0800558 state_info* sinfo = reinterpret_cast<state_info*>(cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559
560 D("wait_for_state %d\n", sinfo->state);
561
Dan Albertbac34742015-02-25 17:51:28 -0800562 const char* err = "unknown error";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
564 if(t != 0) {
Dan Albertcc731cc2015-02-24 21:26:58 -0800565 WriteFdExactly(fd, "OKAY", 4);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566 } else {
567 sendfailmsg(fd, err);
568 }
569
570 if (sinfo->serial)
571 free(sinfo->serial);
572 free(sinfo);
573 adb_close(fd);
574 D("wait_for_state is done\n");
575}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700576
577static void connect_device(char* host, char* buffer, int buffer_size)
578{
579 int port, fd;
580 char* portstr = strchr(host, ':');
581 char hostbuf[100];
582 char serial[100];
583 int ret;
584
585 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
586 if (portstr) {
587 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
588 snprintf(buffer, buffer_size, "bad host name %s", host);
589 return;
590 }
591 // zero terminate the host at the point we found the colon
592 hostbuf[portstr - host] = 0;
Spencer Low943ef232015-01-25 17:38:36 -0800593 if (sscanf(portstr + 1, "%d", &port) != 1) {
Benoit Goby1c45ee92013-03-29 18:22:36 -0700594 snprintf(buffer, buffer_size, "bad port number %s", portstr);
595 return;
596 }
597 } else {
598 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
599 }
600
601 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
602
Ken Liermanaecc6a62013-06-20 09:27:13 -0700603 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700604 if (fd < 0) {
605 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
606 return;
607 }
608
609 D("client: connected on remote on fd %d\n", fd);
610 close_on_exec(fd);
611 disable_tcp_nagle(fd);
612
613 ret = register_socket_transport(fd, serial, port, 0);
614 if (ret < 0) {
615 adb_close(fd);
616 snprintf(buffer, buffer_size, "already connected to %s", serial);
617 } else {
618 snprintf(buffer, buffer_size, "connected to %s", serial);
619 }
620}
621
622void connect_emulator(char* port_spec, char* buffer, int buffer_size)
623{
624 char* port_separator = strchr(port_spec, ',');
625 if (!port_separator) {
626 snprintf(buffer, buffer_size,
627 "unable to parse '%s' as <console port>,<adb port>",
628 port_spec);
629 return;
630 }
631
632 // Zero-terminate console port and make port_separator point to 2nd port.
633 *port_separator++ = 0;
634 int console_port = strtol(port_spec, NULL, 0);
635 int adb_port = strtol(port_separator, NULL, 0);
636 if (!(console_port > 0 && adb_port > 0)) {
637 *(port_separator - 1) = ',';
638 snprintf(buffer, buffer_size,
639 "Invalid port numbers: Expected positive numbers, got '%s'",
640 port_spec);
641 return;
642 }
643
644 /* Check if the emulator is already known.
645 * Note: There's a small but harmless race condition here: An emulator not
646 * present just yet could be registered by another invocation right
647 * after doing this check here. However, local_connect protects
648 * against double-registration too. From here, a better error message
649 * can be produced. In the case of the race condition, the very specific
650 * error message won't be shown, but the data doesn't get corrupted. */
651 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
652 if (known_emulator != NULL) {
653 snprintf(buffer, buffer_size,
654 "Emulator on port %d already registered.", adb_port);
655 return;
656 }
657
658 /* Check if more emulators can be registered. Similar unproblematic
659 * race condition as above. */
660 int candidate_slot = get_available_local_transport_index();
661 if (candidate_slot < 0) {
662 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
663 return;
664 }
665
666 /* Preconditions met, try to connect to the emulator. */
667 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
668 snprintf(buffer, buffer_size,
669 "Connected to emulator on ports %d,%d", console_port, adb_port);
670 } else {
671 snprintf(buffer, buffer_size,
672 "Could not connect to emulator on ports %d,%d",
673 console_port, adb_port);
674 }
675}
676
677static void connect_service(int fd, void* cookie)
678{
679 char buf[4096];
680 char resp[4096];
Dan Albertbac34742015-02-25 17:51:28 -0800681 char *host = reinterpret_cast<char*>(cookie);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700682
683 if (!strncmp(host, "emu:", 4)) {
684 connect_emulator(host + 4, buf, sizeof(buf));
685 } else {
686 connect_device(host, buf, sizeof(buf));
687 }
688
689 // Send response for emulator and device
690 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
Dan Albertcc731cc2015-02-24 21:26:58 -0800691 WriteFdExactly(fd, resp, strlen(resp));
Benoit Goby1c45ee92013-03-29 18:22:36 -0700692 adb_close(fd);
693}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694#endif
695
696#if ADB_HOST
697asocket* host_service_to_socket(const char* name, const char *serial)
698{
699 if (!strcmp(name,"track-devices")) {
700 return create_device_tracker();
701 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
Dan Albertbac34742015-02-25 17:51:28 -0800702 auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703
704 if (serial)
705 sinfo->serial = strdup(serial);
706 else
707 sinfo->serial = NULL;
708
709 name += strlen("wait-for-");
710
711 if (!strncmp(name, "local", strlen("local"))) {
712 sinfo->transport = kTransportLocal;
713 sinfo->state = CS_DEVICE;
714 } else if (!strncmp(name, "usb", strlen("usb"))) {
715 sinfo->transport = kTransportUsb;
716 sinfo->state = CS_DEVICE;
717 } else if (!strncmp(name, "any", strlen("any"))) {
718 sinfo->transport = kTransportAny;
719 sinfo->state = CS_DEVICE;
720 } else {
721 free(sinfo);
722 return NULL;
723 }
724
725 int fd = create_service_thread(wait_for_state, sinfo);
726 return create_local_socket(fd);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700727 } else if (!strncmp(name, "connect:", 8)) {
728 const char *host = name + 8;
729 int fd = create_service_thread(connect_service, (void *)host);
730 return create_local_socket(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731 }
732 return NULL;
733}
734#endif /* ADB_HOST */