blob: 2325c0f5985097bfd0b141295d6717ab9179ac02 [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017#include <errno.h>
Dan Albert76649012015-02-24 15:51:19 -080018#include <stddef.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#ifndef _WIN32
24#include <netdb.h>
25#include <netinet/in.h>
26#include <sys/ioctl.h>
27#include <unistd.h>
28#endif
29
30#if !ADB_HOST
31#include "cutils/android_reboot.h"
32#include "cutils/properties.h"
33#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35#include "sysdeps.h"
36
JP Abgrall408fa572011-03-16 15:57:42 -070037#define TRACE_TAG TRACE_SERVICES
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#include "adb.h"
39#include "file_sync_service.h"
Dan Albert76649012015-02-24 15:51:19 -080040#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
42typedef struct stinfo stinfo;
43
44struct stinfo {
45 void (*func)(int fd, void *cookie);
46 int fd;
47 void *cookie;
48};
49
50
51void *service_bootstrap_func(void *x)
52{
53 stinfo *sti = x;
54 sti->func(sti->fd, sti->cookie);
55 free(sti);
56 return 0;
57}
58
Benoit Goby9470c2f2013-02-20 15:04:53 -080059#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070061void restart_root_service(int fd, void *cookie)
62{
63 char buf[100];
64 char value[PROPERTY_VALUE_MAX];
65
66 if (getuid() == 0) {
67 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
68 writex(fd, buf, strlen(buf));
69 adb_close(fd);
70 } else {
71 property_get("ro.debuggable", value, "");
72 if (strcmp(value, "1") != 0) {
73 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
74 writex(fd, buf, strlen(buf));
Mike Lockwoodff196702009-08-24 15:58:40 -070075 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070076 return;
77 }
78
Benoit Goby7941cf82012-03-16 14:44:17 -070079 property_set("service.adb.root", "1");
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070080 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
81 writex(fd, buf, strlen(buf));
82 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -070083 }
84}
85
Dan Pasanen98858812014-10-06 12:57:20 -050086void restart_unroot_service(int fd, void *cookie)
87{
88 char buf[100];
89
90 if (getuid() != 0) {
91 snprintf(buf, sizeof(buf), "adbd not running as root\n");
92 writex(fd, buf, strlen(buf));
93 adb_close(fd);
94 } else {
95 property_set("service.adb.root", "0");
96 snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
97 writex(fd, buf, strlen(buf));
98 adb_close(fd);
99 }
100}
101
Mike Lockwoodff196702009-08-24 15:58:40 -0700102void restart_tcp_service(int fd, void *cookie)
103{
104 char buf[100];
105 char value[PROPERTY_VALUE_MAX];
Elliott Hughesccecf142014-01-16 10:53:11 -0800106 int port = (int) (uintptr_t) cookie;
Mike Lockwoodff196702009-08-24 15:58:40 -0700107
108 if (port <= 0) {
109 snprintf(buf, sizeof(buf), "invalid port\n");
110 writex(fd, buf, strlen(buf));
111 adb_close(fd);
112 return;
113 }
114
115 snprintf(value, sizeof(value), "%d", port);
116 property_set("service.adb.tcp.port", value);
117 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
118 writex(fd, buf, strlen(buf));
119 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700120}
121
122void restart_usb_service(int fd, void *cookie)
123{
124 char buf[100];
125
126 property_set("service.adb.tcp.port", "0");
127 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
128 writex(fd, buf, strlen(buf));
129 adb_close(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700130}
131
132void reboot_service(int fd, void *arg)
Mike Lockwoodee156622009-08-04 20:37:51 -0400133{
134 char buf[100];
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700135 char property_val[PROPERTY_VALUE_MAX];
Nick Kralevich02916aa2014-01-14 16:34:56 -0800136 int ret;
Mike Lockwoodee156622009-08-04 20:37:51 -0400137
138 sync();
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500139
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700140 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
141 if (ret >= (int) sizeof(property_val)) {
142 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
143 writex(fd, buf, strlen(buf));
144 goto cleanup;
145 }
146
147 ret = property_set(ANDROID_RB_PROPERTY, property_val);
Mike Lockwoodee156622009-08-04 20:37:51 -0400148 if (ret < 0) {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700149 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
Mike Lockwoodee156622009-08-04 20:37:51 -0400150 writex(fd, buf, strlen(buf));
Nick Kralevich91704522013-10-24 08:53:48 -0700151 goto cleanup;
Mike Lockwoodee156622009-08-04 20:37:51 -0400152 }
Nick Kralevich91704522013-10-24 08:53:48 -0700153 // Don't return early. Give the reboot command time to take effect
154 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
155 while(1) { pause(); }
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700156cleanup:
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400157 free(arg);
Mike Lockwoodee156622009-08-04 20:37:51 -0400158 adb_close(fd);
159}
160
David 'Digit' Turner25258692013-03-21 21:07:42 +0100161void reverse_service(int fd, void* arg)
162{
163 const char* command = arg;
164
165 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
166 sendfailmsg(fd, "not a reverse forwarding command");
167 }
168 free(arg);
169 adb_close(fd);
170}
171
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172#endif
173
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174static int create_service_thread(void (*func)(int, void *), void *cookie)
175{
176 stinfo *sti;
177 adb_thread_t t;
178 int s[2];
179
180 if(adb_socketpair(s)) {
181 printf("cannot create service socket pair\n");
182 return -1;
183 }
leozwangcbf02672014-08-15 09:51:27 -0700184 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
186 sti = malloc(sizeof(stinfo));
187 if(sti == 0) fatal("cannot allocate stinfo");
188 sti->func = func;
189 sti->cookie = cookie;
190 sti->fd = s[1];
191
192 if(adb_thread_create( &t, service_bootstrap_func, sti)){
193 free(sti);
194 adb_close(s[0]);
195 adb_close(s[1]);
196 printf("cannot create service thread\n");
197 return -1;
198 }
199
200 D("service thread started, %d:%d\n",s[0], s[1]);
201 return s[0];
202}
203
JP Abgrall408fa572011-03-16 15:57:42 -0700204#if !ADB_HOST
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700205
206static void init_subproc_child()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207{
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700208 setsid();
209
Rom Lemarchand07ce7ca2014-06-20 16:30:54 -0700210 // Set OOM score adjustment to prevent killing
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700211 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700212 if (fd >= 0) {
213 adb_write(fd, "0", 1);
214 adb_close(fd);
215 } else {
Rom Lemarchand07ce7ca2014-06-20 16:30:54 -0700216 D("adb: unable to update oom_score_adj\n");
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700217 }
218}
219
220static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
221{
222 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cuie77b6a02014-11-11 09:24:11 -0800223#if defined(_WIN32)
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700224 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
JP Abgrall408fa572011-03-16 15:57:42 -0700225 return -1;
Yabin Cuie77b6a02014-11-11 09:24:11 -0800226#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 int ptm;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700229 ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 if(ptm < 0){
231 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
232 return -1;
233 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234
Elliott Hughesd2352882014-07-29 11:32:16 -0700235 char devname[64];
236 if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700238 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 return -1;
240 }
241
JP Abgrall408fa572011-03-16 15:57:42 -0700242 *pid = fork();
243 if(*pid < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 printf("- fork failed: %s -\n", strerror(errno));
JP Abgrall408fa572011-03-16 15:57:42 -0700245 adb_close(ptm);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 return -1;
247 }
248
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700249 if (*pid == 0) {
250 init_subproc_child();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700252 int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700253 if (pts < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -0700254 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
255 exit(-1);
256 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700258 dup2(pts, STDIN_FILENO);
259 dup2(pts, STDOUT_FILENO);
260 dup2(pts, STDERR_FILENO);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261
Benoit Goby95ef8282011-02-01 18:57:41 -0800262 adb_close(pts);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263 adb_close(ptm);
264
JP Abgrall408fa572011-03-16 15:57:42 -0700265 execl(cmd, cmd, arg0, arg1, NULL);
266 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
267 cmd, strerror(errno), errno);
268 exit(-1);
269 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 return ptm;
271 }
Yabin Cuie77b6a02014-11-11 09:24:11 -0800272#endif /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273}
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700274
275static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
276{
277 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Yabin Cuie77b6a02014-11-11 09:24:11 -0800278#if defined(_WIN32)
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700279 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
280 return -1;
Yabin Cuie77b6a02014-11-11 09:24:11 -0800281#else
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700282
283 // 0 is parent socket, 1 is child socket
284 int sv[2];
leozwangcbf02672014-08-15 09:51:27 -0700285 if (adb_socketpair(sv) < 0) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700286 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
287 return -1;
288 }
leozwangcbf02672014-08-15 09:51:27 -0700289 D("socketpair: (%d,%d)", sv[0], sv[1]);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700290
291 *pid = fork();
292 if (*pid < 0) {
293 printf("- fork failed: %s -\n", strerror(errno));
294 adb_close(sv[0]);
295 adb_close(sv[1]);
296 return -1;
297 }
298
299 if (*pid == 0) {
300 adb_close(sv[0]);
301 init_subproc_child();
302
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700303 dup2(sv[1], STDIN_FILENO);
304 dup2(sv[1], STDOUT_FILENO);
Jeff Sharkey960df972014-06-09 17:30:57 -0700305 dup2(sv[1], STDERR_FILENO);
306
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700307 adb_close(sv[1]);
308
309 execl(cmd, cmd, arg0, arg1, NULL);
310 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
311 cmd, strerror(errno), errno);
312 exit(-1);
313 } else {
314 adb_close(sv[1]);
315 return sv[0];
316 }
Yabin Cuie77b6a02014-11-11 09:24:11 -0800317#endif /* !defined(_WIN32) */
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700318}
JP Abgrall408fa572011-03-16 15:57:42 -0700319#endif /* !ABD_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320
321#if ADB_HOST
322#define SHELL_COMMAND "/bin/sh"
323#else
324#define SHELL_COMMAND "/system/bin/sh"
325#endif
326
JP Abgrall408fa572011-03-16 15:57:42 -0700327#if !ADB_HOST
328static void subproc_waiter_service(int fd, void *cookie)
329{
Elliott Hughesccecf142014-01-16 10:53:11 -0800330 pid_t pid = (pid_t) (uintptr_t) cookie;
JP Abgrall408fa572011-03-16 15:57:42 -0700331
332 D("entered. fd=%d of pid=%d\n", fd, pid);
333 for (;;) {
334 int status;
335 pid_t p = waitpid(pid, &status, 0);
336 if (p == pid) {
337 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
338 if (WIFSIGNALED(status)) {
339 D("*** Killed by signal %d\n", WTERMSIG(status));
340 break;
341 } else if (!WIFEXITED(status)) {
342 D("*** Didn't exit!!. status %d\n", status);
343 break;
344 } else if (WEXITSTATUS(status) >= 0) {
345 D("*** Exit code %d\n", WEXITSTATUS(status));
346 break;
347 }
348 }
JP Abgrall408fa572011-03-16 15:57:42 -0700349 }
350 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
351 if (SHELL_EXIT_NOTIFY_FD >=0) {
352 int res;
353 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
354 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
355 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
356 }
357}
358
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700359static int create_subproc_thread(const char *name, const subproc_mode mode)
JP Abgrall408fa572011-03-16 15:57:42 -0700360{
361 stinfo *sti;
362 adb_thread_t t;
363 int ret_fd;
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700364 pid_t pid = -1;
365
366 const char *arg0, *arg1;
367 if (name == 0 || *name == 0) {
368 arg0 = "-"; arg1 = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700369 } else {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700370 arg0 = "-c"; arg1 = name;
JP Abgrall408fa572011-03-16 15:57:42 -0700371 }
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700372
373 switch (mode) {
374 case SUBPROC_PTY:
375 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
376 break;
377 case SUBPROC_RAW:
378 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
379 break;
380 default:
381 fprintf(stderr, "invalid subproc_mode %d\n", mode);
382 return -1;
383 }
384 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall408fa572011-03-16 15:57:42 -0700385
386 sti = malloc(sizeof(stinfo));
387 if(sti == 0) fatal("cannot allocate stinfo");
388 sti->func = subproc_waiter_service;
Elliott Hughesccecf142014-01-16 10:53:11 -0800389 sti->cookie = (void*) (uintptr_t) pid;
JP Abgrall408fa572011-03-16 15:57:42 -0700390 sti->fd = ret_fd;
391
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700392 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
JP Abgrall408fa572011-03-16 15:57:42 -0700393 free(sti);
394 adb_close(ret_fd);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700395 fprintf(stderr, "cannot create service thread\n");
JP Abgrall408fa572011-03-16 15:57:42 -0700396 return -1;
397 }
398
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700399 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
JP Abgrall408fa572011-03-16 15:57:42 -0700400 return ret_fd;
401}
402#endif
403
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404int service_to_fd(const char *name)
405{
406 int ret = -1;
407
408 if(!strncmp(name, "tcp:", 4)) {
409 int port = atoi(name + 4);
410 name = strchr(name + 4, ':');
411 if(name == 0) {
412 ret = socket_loopback_client(port, SOCK_STREAM);
413 if (ret >= 0)
414 disable_tcp_nagle(ret);
415 } else {
416#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 ret = socket_network_client(name + 1, port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418#else
419 return -1;
420#endif
421 }
422#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
423 } else if(!strncmp(name, "local:", 6)) {
424 ret = socket_local_client(name + 6,
425 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
426 } else if(!strncmp(name, "localreserved:", 14)) {
427 ret = socket_local_client(name + 14,
428 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
429 } else if(!strncmp(name, "localabstract:", 14)) {
430 ret = socket_local_client(name + 14,
431 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
432 } else if(!strncmp(name, "localfilesystem:", 16)) {
433 ret = socket_local_client(name + 16,
434 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
435#endif
Benoit Goby9470c2f2013-02-20 15:04:53 -0800436#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 } else if(!strncmp("dev:", name, 4)) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700438 ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 } else if(!strncmp(name, "framebuffer:", 12)) {
440 ret = create_service_thread(framebuffer_service, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 } else if (!strncmp(name, "jdwp:", 5)) {
442 ret = create_jdwp_connection_fd(atoi(name+5));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 } else if(!HOST && !strncmp(name, "shell:", 6)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700444 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
445 } else if(!HOST && !strncmp(name, "exec:", 5)) {
446 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 } else if(!strncmp(name, "sync:", 5)) {
448 ret = create_service_thread(file_sync_service, NULL);
449 } else if(!strncmp(name, "remount:", 8)) {
450 ret = create_service_thread(remount_service, NULL);
Mike Lockwoodee156622009-08-04 20:37:51 -0400451 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400452 void* arg = strdup(name + 7);
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700453 if (arg == NULL) return -1;
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400454 ret = create_service_thread(reboot_service, arg);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700455 } else if(!strncmp(name, "root:", 5)) {
456 ret = create_service_thread(restart_root_service, NULL);
Dan Pasanen98858812014-10-06 12:57:20 -0500457 } else if(!strncmp(name, "unroot:", 7)) {
458 ret = create_service_thread(restart_unroot_service, NULL);
Christopher Tated2f54152011-04-21 12:53:28 -0700459 } else if(!strncmp(name, "backup:", 7)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700460 char* arg = strdup(name + 7);
Christopher Tated2f54152011-04-21 12:53:28 -0700461 if (arg == NULL) return -1;
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700462 char* c = arg;
463 for (; *c != '\0'; c++) {
464 if (*c == ':')
465 *c = ' ';
466 }
467 char* cmd;
468 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
469 ret = create_subproc_thread(cmd, SUBPROC_RAW);
470 free(cmd);
471 }
472 free(arg);
Christopher Tate702967a2011-05-17 15:52:54 -0700473 } else if(!strncmp(name, "restore:", 8)) {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700474 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
Mike Lockwoodff196702009-08-24 15:58:40 -0700475 } else if(!strncmp(name, "tcpip:", 6)) {
476 int port;
Spencer Low943ef232015-01-25 17:38:36 -0800477 if (sscanf(name + 6, "%d", &port) != 1) {
Mike Lockwoodff196702009-08-24 15:58:40 -0700478 port = 0;
479 }
Elliott Hughesccecf142014-01-16 10:53:11 -0800480 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
Mike Lockwoodff196702009-08-24 15:58:40 -0700481 } else if(!strncmp(name, "usb:", 4)) {
482 ret = create_service_thread(restart_usb_service, NULL);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100483 } else if (!strncmp(name, "reverse:", 8)) {
484 char* cookie = strdup(name + 8);
485 if (cookie == NULL) {
486 ret = -1;
487 } else {
488 ret = create_service_thread(reverse_service, cookie);
489 if (ret < 0) {
490 free(cookie);
491 }
492 }
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000493 } else if(!strncmp(name, "disable-verity:", 15)) {
Paul Lawrence982089d2014-12-03 15:31:57 -0800494 ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
495 } else if(!strncmp(name, "enable-verity:", 15)) {
496 ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 }
499 if (ret >= 0) {
500 close_on_exec(ret);
501 }
502 return ret;
503}
504
505#if ADB_HOST
506struct state_info {
507 transport_type transport;
508 char* serial;
509 int state;
510};
511
512static void wait_for_state(int fd, void* cookie)
513{
514 struct state_info* sinfo = cookie;
515 char* err = "unknown error";
516
517 D("wait_for_state %d\n", sinfo->state);
518
519 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
520 if(t != 0) {
521 writex(fd, "OKAY", 4);
522 } else {
523 sendfailmsg(fd, err);
524 }
525
526 if (sinfo->serial)
527 free(sinfo->serial);
528 free(sinfo);
529 adb_close(fd);
530 D("wait_for_state is done\n");
531}
Benoit Goby1c45ee92013-03-29 18:22:36 -0700532
533static void connect_device(char* host, char* buffer, int buffer_size)
534{
535 int port, fd;
536 char* portstr = strchr(host, ':');
537 char hostbuf[100];
538 char serial[100];
539 int ret;
540
541 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
542 if (portstr) {
543 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
544 snprintf(buffer, buffer_size, "bad host name %s", host);
545 return;
546 }
547 // zero terminate the host at the point we found the colon
548 hostbuf[portstr - host] = 0;
Spencer Low943ef232015-01-25 17:38:36 -0800549 if (sscanf(portstr + 1, "%d", &port) != 1) {
Benoit Goby1c45ee92013-03-29 18:22:36 -0700550 snprintf(buffer, buffer_size, "bad port number %s", portstr);
551 return;
552 }
553 } else {
554 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
555 }
556
557 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
558
Ken Liermanaecc6a62013-06-20 09:27:13 -0700559 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700560 if (fd < 0) {
561 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
562 return;
563 }
564
565 D("client: connected on remote on fd %d\n", fd);
566 close_on_exec(fd);
567 disable_tcp_nagle(fd);
568
569 ret = register_socket_transport(fd, serial, port, 0);
570 if (ret < 0) {
571 adb_close(fd);
572 snprintf(buffer, buffer_size, "already connected to %s", serial);
573 } else {
574 snprintf(buffer, buffer_size, "connected to %s", serial);
575 }
576}
577
578void connect_emulator(char* port_spec, char* buffer, int buffer_size)
579{
580 char* port_separator = strchr(port_spec, ',');
581 if (!port_separator) {
582 snprintf(buffer, buffer_size,
583 "unable to parse '%s' as <console port>,<adb port>",
584 port_spec);
585 return;
586 }
587
588 // Zero-terminate console port and make port_separator point to 2nd port.
589 *port_separator++ = 0;
590 int console_port = strtol(port_spec, NULL, 0);
591 int adb_port = strtol(port_separator, NULL, 0);
592 if (!(console_port > 0 && adb_port > 0)) {
593 *(port_separator - 1) = ',';
594 snprintf(buffer, buffer_size,
595 "Invalid port numbers: Expected positive numbers, got '%s'",
596 port_spec);
597 return;
598 }
599
600 /* Check if the emulator is already known.
601 * Note: There's a small but harmless race condition here: An emulator not
602 * present just yet could be registered by another invocation right
603 * after doing this check here. However, local_connect protects
604 * against double-registration too. From here, a better error message
605 * can be produced. In the case of the race condition, the very specific
606 * error message won't be shown, but the data doesn't get corrupted. */
607 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
608 if (known_emulator != NULL) {
609 snprintf(buffer, buffer_size,
610 "Emulator on port %d already registered.", adb_port);
611 return;
612 }
613
614 /* Check if more emulators can be registered. Similar unproblematic
615 * race condition as above. */
616 int candidate_slot = get_available_local_transport_index();
617 if (candidate_slot < 0) {
618 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
619 return;
620 }
621
622 /* Preconditions met, try to connect to the emulator. */
623 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
624 snprintf(buffer, buffer_size,
625 "Connected to emulator on ports %d,%d", console_port, adb_port);
626 } else {
627 snprintf(buffer, buffer_size,
628 "Could not connect to emulator on ports %d,%d",
629 console_port, adb_port);
630 }
631}
632
633static void connect_service(int fd, void* cookie)
634{
635 char buf[4096];
636 char resp[4096];
637 char *host = cookie;
638
639 if (!strncmp(host, "emu:", 4)) {
640 connect_emulator(host + 4, buf, sizeof(buf));
641 } else {
642 connect_device(host, buf, sizeof(buf));
643 }
644
645 // Send response for emulator and device
646 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
647 writex(fd, resp, strlen(resp));
648 adb_close(fd);
649}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650#endif
651
652#if ADB_HOST
653asocket* host_service_to_socket(const char* name, const char *serial)
654{
655 if (!strcmp(name,"track-devices")) {
656 return create_device_tracker();
657 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
658 struct state_info* sinfo = malloc(sizeof(struct state_info));
659
660 if (serial)
661 sinfo->serial = strdup(serial);
662 else
663 sinfo->serial = NULL;
664
665 name += strlen("wait-for-");
666
667 if (!strncmp(name, "local", strlen("local"))) {
668 sinfo->transport = kTransportLocal;
669 sinfo->state = CS_DEVICE;
670 } else if (!strncmp(name, "usb", strlen("usb"))) {
671 sinfo->transport = kTransportUsb;
672 sinfo->state = CS_DEVICE;
673 } else if (!strncmp(name, "any", strlen("any"))) {
674 sinfo->transport = kTransportAny;
675 sinfo->state = CS_DEVICE;
676 } else {
677 free(sinfo);
678 return NULL;
679 }
680
681 int fd = create_service_thread(wait_for_state, sinfo);
682 return create_local_socket(fd);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700683 } else if (!strncmp(name, "connect:", 8)) {
684 const char *host = name + 8;
685 int fd = create_service_thread(connect_service, (void *)host);
686 return create_local_socket(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 }
688 return NULL;
689}
690#endif /* ADB_HOST */