blob: 136078266c08a7876beb3411132c1fd8360132d7 [file] [log] [blame]
rjw2e8229f2022-02-15 21:08:12 +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
17#include <stddef.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <unistd.h>
21#include <string.h>
22#include <errno.h>
23#include <linux/reboot.h>
24#include <sys/syscall.h>
25
26#include "sysdeps.h"
27
28#define TRACE_TAG TRACE_SERVICES
29#include "adb.h"
30#include "file_sync_service.h"
31
32#if ADB_HOST
33# ifndef HAVE_WINSOCK
34# include <netinet/in.h>
35# include <netdb.h>
36# include <sys/ioctl.h>
37# endif
38#else
39# include <cutils/android_reboot.h>
40# include <cutils/properties.h>
41#endif
42
43typedef struct stinfo stinfo;
44
45struct stinfo {
46 void (*func)(int fd, void *cookie);
47 int fd;
48 void *cookie;
49};
50
51
52void *service_bootstrap_func(void *x)
53{
54 stinfo *sti = x;
55 sti->func(sti->fd, sti->cookie);
56 free(sti);
57 return 0;
58}
59
60#if !ADB_HOST
61
62void restart_root_service(int fd, void *cookie)
63{
64 char buf[100];
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#ifndef ADB_NON_ANDROID
72 char value[PROPERTY_VALUE_MAX];
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");
76 writex(fd, buf, strlen(buf));
77 adb_close(fd);
78 return;
79 }
80#endif /* !ADB_NON_ANDROID */
81
82 property_set("service.adb.root", "1");
83 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
84 writex(fd, buf, strlen(buf));
85 adb_close(fd);
86 }
87}
88
89void restart_tcp_service(int fd, void *cookie)
90{
91 char buf[100];
92 char value[PROPERTY_VALUE_MAX];
93 int port = (int) (uintptr_t) cookie;
94
95 if (port <= 0) {
96 snprintf(buf, sizeof(buf), "invalid port\n");
97 writex(fd, buf, strlen(buf));
98 adb_close(fd);
99 return;
100 }
101
102 snprintf(value, sizeof(value), "%d", port);
103 property_set("service.adb.tcp.port", value);
104 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
105 writex(fd, buf, strlen(buf));
106 adb_close(fd);
107}
108
109void restart_usb_service(int fd, void *cookie)
110{
111 char buf[100];
112
113 property_set("service.adb.tcp.port", "0");
114 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
115 writex(fd, buf, strlen(buf));
116 adb_close(fd);
117}
118
119void restart_adbd_service(int fd, void *cookie)
120{
121 char buf[100];
122
123 snprintf(buf, sizeof(buf), "restarting service\n");
124 writex(fd, buf, strlen(buf));
125 adb_close(fd);
126}
127
128void reboot_service(int fd, void *arg)
129{
130 char buf[100];
131 char property_val[PROPERTY_VALUE_MAX];
132 int ret;
133 const char *param = (const char *)arg;
134 int cmd;
135
136 sync();
137
138 if (!param || strlen(param) == 0)
139 cmd = LINUX_REBOOT_CMD_RESTART;
140 else
141 cmd = LINUX_REBOOT_CMD_RESTART2;
142 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, param);
143
144#if 0
145 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
146 if (ret >= (int) sizeof(property_val)) {
147 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
148 writex(fd, buf, strlen(buf));
149 goto cleanup;
150 }
151
152 ret = property_set(ANDROID_RB_PROPERTY, property_val);
153 if (ret < 0) {
154 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
155 writex(fd, buf, strlen(buf));
156 goto cleanup;
157 }
158 // Don't return early. Give the reboot command time to take effect
159 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
160 while(1) { pause(); }
161#endif
162cleanup:
163 free(arg);
164 adb_close(fd);
165}
166
167void reverse_service(int fd, void* arg)
168{
169 const char* command = arg;
170
171 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
172 sendfailmsg(fd, "not a reverse forwarding command");
173 }
174 free(arg);
175 adb_close(fd);
176}
177
178#endif
179
180static int create_service_thread(void (*func)(int, void *), void *cookie)
181{
182 stinfo *sti;
183 adb_thread_t t;
184 int s[2];
185
186 if(adb_socketpair(s)) {
187 printf("cannot create service socket pair\n");
188 return -1;
189 }
190
191 sti = malloc(sizeof(stinfo));
192 if(sti == 0) fatal("cannot allocate stinfo");
193 sti->func = func;
194 sti->cookie = cookie;
195 sti->fd = s[1];
196
197 if(adb_thread_create( &t, service_bootstrap_func, sti)){
198 free(sti);
199 adb_close(s[0]);
200 adb_close(s[1]);
201 printf("cannot create service thread\n");
202 return -1;
203 }
204
205 D("service thread started, %d:%d\n",s[0], s[1]);
206 return s[0];
207}
208
209#if !ADB_HOST
210
211static void init_subproc_child()
212{
213 setsid();
214
215 // Set OOM score adjustment to prevent killing
216 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
217 if (fd >= 0) {
218 adb_write(fd, "0", 1);
219 adb_close(fd);
220 } else {
221 D("adb: unable to update oom_score_adj\n");
222 }
223}
224
225static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
226{
227 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
228#if defined(_WIN32)
229 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
230 return -1;
231#else
232 int ptm;
233
234 ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
235 if(ptm < 0){
236 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
237 return -1;
238 }
239
240 char devname[64];
241 if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
242 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
243 adb_close(ptm);
244 return -1;
245 }
246
247 *pid = fork();
248 if(*pid < 0) {
249 printf("- fork failed: %s -\n", strerror(errno));
250 adb_close(ptm);
251 return -1;
252 }
253
254 if (*pid == 0) {
255 init_subproc_child();
256
257 int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
258 if (pts < 0) {
259 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
260 exit(-1);
261 }
262
263 dup2(pts, STDIN_FILENO);
264 dup2(pts, STDOUT_FILENO);
265 dup2(pts, STDERR_FILENO);
266
267 adb_close(pts);
268 adb_close(ptm);
269
270 execl(cmd, cmd, arg0, arg1, NULL);
271 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
272 cmd, strerror(errno), errno);
273 exit(-1);
274 } else {
275 return ptm;
276 }
277#endif /* !defined(_WIN32) */
278}
279
280static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
281{
282 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
283#if defined(_WIN32)
284 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
285 return -1;
286#else
287
288 // 0 is parent socket, 1 is child socket
289 int sv[2];
290 if (unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
291 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
292 return -1;
293 }
294
295 *pid = fork();
296 if (*pid < 0) {
297 printf("- fork failed: %s -\n", strerror(errno));
298 adb_close(sv[0]);
299 adb_close(sv[1]);
300 return -1;
301 }
302
303 if (*pid == 0) {
304 adb_close(sv[0]);
305 init_subproc_child();
306
307 dup2(sv[1], STDIN_FILENO);
308 dup2(sv[1], STDOUT_FILENO);
309 dup2(sv[1], STDERR_FILENO);
310
311 adb_close(sv[1]);
312
313 execl(cmd, cmd, arg0, arg1, NULL);
314 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
315 cmd, strerror(errno), errno);
316 exit(-1);
317 } else {
318 adb_close(sv[1]);
319 return sv[0];
320 }
321#endif /* !defined(_WIN32) */
322}
323#endif /* !ABD_HOST */
324
325#if ADB_HOST || ADB_NON_ANDROID
326#define SHELL_COMMAND "/bin/sh"
327#else
328#define SHELL_COMMAND "/system/bin/sh"
329#endif
330
331#if !ADB_HOST
332static void subproc_waiter_service(int fd, void *cookie)
333{
334 pid_t pid = (pid_t) (uintptr_t) cookie;
335
336 D("entered. fd=%d of pid=%d\n", fd, pid);
337 for (;;) {
338 int status;
339 pid_t p = waitpid(pid, &status, 0);
340 if (p == pid) {
341 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
342 if (WIFSIGNALED(status)) {
343 D("*** Killed by signal %d\n", WTERMSIG(status));
344 break;
345 } else if (!WIFEXITED(status)) {
346 D("*** Didn't exit!!. status %d\n", status);
347 break;
348 } else if (WEXITSTATUS(status) >= 0) {
349 D("*** Exit code %d\n", WEXITSTATUS(status));
350 break;
351 }
352 }
353 }
354 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
355 if (SHELL_EXIT_NOTIFY_FD >=0) {
356 int res;
357 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
358 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
359 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
360 }
361}
362
363static int create_subproc_thread(const char *name, const subproc_mode mode)
364{
365 stinfo *sti;
366 adb_thread_t t;
367 int ret_fd;
368 pid_t pid = -1;
369
370 const char *arg0, *arg1;
371 if (name == 0 || *name == 0) {
372 arg0 = "-"; arg1 = 0;
373 } else {
374 arg0 = "-c"; arg1 = name;
375 }
376
377 switch (mode) {
378 case SUBPROC_PTY:
379 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
380 break;
381 case SUBPROC_RAW:
382 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
383 break;
384 default:
385 fprintf(stderr, "invalid subproc_mode %d\n", mode);
386 return -1;
387 }
388 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
389
390 sti = malloc(sizeof(stinfo));
391 if(sti == 0) fatal("cannot allocate stinfo");
392 sti->func = subproc_waiter_service;
393 sti->cookie = (void*) (uintptr_t) pid;
394 sti->fd = ret_fd;
395
396 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
397 free(sti);
398 adb_close(ret_fd);
399 fprintf(stderr, "cannot create service thread\n");
400 return -1;
401 }
402
403 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
404 return ret_fd;
405}
406#endif
407
408int service_to_fd(const char *name)
409{
410 int ret = -1;
411
412 if(!strncmp(name, "tcp:", 4)) {
413 int port = atoi(name + 4);
414 name = strchr(name + 4, ':');
415 if(name == 0) {
416 ret = socket_loopback_client(port, SOCK_STREAM);
417 if (ret >= 0)
418 disable_tcp_nagle(ret);
419 } else {
420#if ADB_HOST
421 ret = socket_network_client(name + 1, port, SOCK_STREAM);
422#else
423 return -1;
424#endif
425 }
426#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
427 } else if(!strncmp(name, "local:", 6)) {
428 ret = socket_local_client(name + 6,
429 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
430 } else if(!strncmp(name, "localreserved:", 14)) {
431 ret = socket_local_client(name + 14,
432 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
433 } else if(!strncmp(name, "localabstract:", 14)) {
434 ret = socket_local_client(name + 14,
435 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
436 } else if(!strncmp(name, "localfilesystem:", 16)) {
437 ret = socket_local_client(name + 16,
438 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
439#endif
440#if !ADB_HOST
441 } else if(!strncmp("dev:", name, 4)) {
442 ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
443 } else if(!strncmp(name, "framebuffer:", 12)) {
444 ret = create_service_thread(framebuffer_service, 0);
445 } else if (!strncmp(name, "jdwp:", 5)) {
446 ret = create_jdwp_connection_fd(atoi(name+5));
447 } else if(!HOST && !strncmp(name, "shell:", 6)) {
448 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
449 } else if(!HOST && !strncmp(name, "exec:", 5)) {
450 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
451 } else if(!strncmp(name, "sync:", 5)) {
452 ret = create_service_thread(file_sync_service, NULL);
453 } else if(!strncmp(name, "remount:", 8)) {
454 ret = create_service_thread(remount_service, NULL);
455 } else if(!strncmp(name, "reboot:", 7)) {
456 void* arg = strdup(name + 7);
457 if (arg == NULL) return -1;
458 ret = create_service_thread(reboot_service, arg);
459 } else if(!strncmp(name, "root:", 5)) {
460 ret = create_service_thread(restart_root_service, NULL);
461 } else if(!strncmp(name, "backup:", 7)) {
462 char* arg = strdup(name + 7);
463 if (arg == NULL) return -1;
464 char* c = arg;
465 for (; *c != '\0'; c++) {
466 if (*c == ':')
467 *c = ' ';
468 }
469 char* cmd;
470 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
471 ret = create_subproc_thread(cmd, SUBPROC_RAW);
472 free(cmd);
473 }
474 free(arg);
475 } else if(!strncmp(name, "restore:", 8)) {
476 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
477 } else if(!strncmp(name, "tcpip:", 6)) {
478 int port;
479 if (sscanf(name + 6, "%d", &port) == 0) {
480 port = 0;
481 }
482 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
483 } else if(!strncmp(name, "usb:", 4)) {
484 ret = create_service_thread(restart_usb_service, NULL);
485 } else if(!strncmp(name, "reconnect:", 10)) {
486 ret = create_service_thread(restart_adbd_service, NULL);
487 } else if (!strncmp(name, "reverse:", 8)) {
488 char* cookie = strdup(name + 8);
489 if (cookie == NULL) {
490 ret = -1;
491 } else {
492 ret = create_service_thread(reverse_service, cookie);
493 if (ret < 0) {
494 free(cookie);
495 }
496 }
497#endif
498 }
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}
532
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;
549 if (sscanf(portstr + 1, "%d", &port) == 0) {
550 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
559 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
560 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}
650#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);
683 } 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);
687 }
688 return NULL;
689}
690#endif /* ADB_HOST */