blob: 0dbe4dbdc9322b3fbe6076cb710d469d938279f1 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +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
24#include "sysdeps.h"
25
26#define TRACE_TAG TRACE_SERVICES
27#include "adb.h"
28#include "file_sync_service.h"
29
30#if ADB_HOST
31# ifndef HAVE_WINSOCK
32# include <netinet/in.h>
33# include <netdb.h>
34# include <sys/ioctl.h>
35# endif
36#else
37# include <cutils/android_reboot.h>
38# include <cutils/properties.h>
39#endif
40
41typedef struct stinfo stinfo;
42
43struct stinfo {
44 void (*func)(int fd, void *cookie);
45 int fd;
46 void *cookie;
47};
48
49
50void *service_bootstrap_func(void *x)
51{
52 stinfo *sti = x;
53 sti->func(sti->fd, sti->cookie);
54 free(sti);
55 return 0;
56}
57
58#if !ADB_HOST
59
60void restart_root_service(int fd, void *cookie)
61{
62 char buf[100];
63 char value[PROPERTY_VALUE_MAX];
64
65 if (getuid() == 0) {
66 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
67 writex(fd, buf, strlen(buf));
68 adb_close(fd);
69 } else {
70 property_get("ro.debuggable", value, "");
71 if (strcmp(value, "1") != 0) {
72 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
73 writex(fd, buf, strlen(buf));
74 adb_close(fd);
75 return;
76 }
77
78 property_set("service.adb.root", "1");
79 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
80 writex(fd, buf, strlen(buf));
81 adb_close(fd);
82 }
83}
84
85void restart_tcp_service(int fd, void *cookie)
86{
87 char buf[100];
88 char value[PROPERTY_VALUE_MAX];
89 int port = (int) (uintptr_t) cookie;
90
91 if (port <= 0) {
92 snprintf(buf, sizeof(buf), "invalid port\n");
93 writex(fd, buf, strlen(buf));
94 adb_close(fd);
95 return;
96 }
97
98 snprintf(value, sizeof(value), "%d", port);
99 property_set("service.adb.tcp.port", value);
100 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
101 writex(fd, buf, strlen(buf));
102 adb_close(fd);
103}
104
105void restart_usb_service(int fd, void *cookie)
106{
107 char buf[100];
108
109 property_set("service.adb.tcp.port", "0");
110 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
111 writex(fd, buf, strlen(buf));
112 adb_close(fd);
113}
114
115void reboot_service(int fd, void *arg)
116{
117 char buf[100];
118 char property_val[PROPERTY_VALUE_MAX];
119 int ret;
120
121 sync();
122
123 ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg);
124 if (ret >= (int) sizeof(property_val)) {
125 snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
126 writex(fd, buf, strlen(buf));
127 goto cleanup;
128 }
129
130 ret = property_set(ANDROID_RB_PROPERTY, property_val);
131 if (ret < 0) {
132 snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
133 writex(fd, buf, strlen(buf));
134 goto cleanup;
135 }
136 // Don't return early. Give the reboot command time to take effect
137 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
138 while(1) { pause(); }
139cleanup:
140 free(arg);
141 adb_close(fd);
142}
143
144void reverse_service(int fd, void* arg)
145{
146 const char* command = arg;
147
148 if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) {
149 sendfailmsg(fd, "not a reverse forwarding command");
150 }
151 free(arg);
152 adb_close(fd);
153}
154
155#endif
156
157static int create_service_thread(void (*func)(int, void *), void *cookie)
158{
159 stinfo *sti;
160 adb_thread_t t;
161 int s[2];
162
163 if(adb_socketpair(s)) {
164 printf("cannot create service socket pair\n");
165 return -1;
166 }
167
168 sti = malloc(sizeof(stinfo));
169 if(sti == 0) fatal("cannot allocate stinfo");
170 sti->func = func;
171 sti->cookie = cookie;
172 sti->fd = s[1];
173
174 if(adb_thread_create( &t, service_bootstrap_func, sti)){
175 free(sti);
176 adb_close(s[0]);
177 adb_close(s[1]);
178 printf("cannot create service thread\n");
179 return -1;
180 }
181
182 D("service thread started, %d:%d\n",s[0], s[1]);
183 return s[0];
184}
185
186#if !ADB_HOST
187
188static void init_subproc_child()
189{
190 setsid();
191
192 // Set OOM score adjustment to prevent killing
193 int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY);
194 if (fd >= 0) {
195 adb_write(fd, "0", 1);
196 adb_close(fd);
197 } else {
198 D("adb: unable to update oom_score_adj\n");
199 }
200}
201
202static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
203{
204 D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
205#ifdef HAVE_WIN32_PROC
206 fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
207 return -1;
208#else /* !HAVE_WIN32_PROC */
209 char *devname;
210 int ptm;
211
212 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
213 if(ptm < 0){
214 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
215 return -1;
216 }
217 fcntl(ptm, F_SETFD, FD_CLOEXEC);
218
219 if(grantpt(ptm) || unlockpt(ptm) ||
220 ((devname = (char*) ptsname(ptm)) == 0)){
221 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
222 adb_close(ptm);
223 return -1;
224 }
225
226 *pid = fork();
227 if(*pid < 0) {
228 printf("- fork failed: %s -\n", strerror(errno));
229 adb_close(ptm);
230 return -1;
231 }
232
233 if (*pid == 0) {
234 init_subproc_child();
235
236 int pts = unix_open(devname, O_RDWR);
237 if (pts < 0) {
238 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
239 exit(-1);
240 }
241
242 dup2(pts, STDIN_FILENO);
243 dup2(pts, STDOUT_FILENO);
244 dup2(pts, STDERR_FILENO);
245
246 adb_close(pts);
247 adb_close(ptm);
248
249 execl(cmd, cmd, arg0, arg1, NULL);
250 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
251 cmd, strerror(errno), errno);
252 exit(-1);
253 } else {
254 return ptm;
255 }
256#endif /* !HAVE_WIN32_PROC */
257}
258
259static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
260{
261 D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
262#ifdef HAVE_WIN32_PROC
263 fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
264 return -1;
265#else /* !HAVE_WIN32_PROC */
266
267 // 0 is parent socket, 1 is child socket
268 int sv[2];
269 if (unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
270 printf("[ cannot create socket pair - %s ]\n", strerror(errno));
271 return -1;
272 }
273
274 *pid = fork();
275 if (*pid < 0) {
276 printf("- fork failed: %s -\n", strerror(errno));
277 adb_close(sv[0]);
278 adb_close(sv[1]);
279 return -1;
280 }
281
282 if (*pid == 0) {
283 adb_close(sv[0]);
284 init_subproc_child();
285
286 // Only hook up stdin/stdout; drop stderr
287 dup2(sv[1], STDIN_FILENO);
288 dup2(sv[1], STDOUT_FILENO);
289 adb_close(sv[1]);
290
291 execl(cmd, cmd, arg0, arg1, NULL);
292 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
293 cmd, strerror(errno), errno);
294 exit(-1);
295 } else {
296 adb_close(sv[1]);
297 return sv[0];
298 }
299#endif /* !HAVE_WIN32_PROC */
300}
301#endif /* !ABD_HOST */
302
303//#define ADB_PASSWD
304#if ADB_HOST
305#define SHELL_COMMAND "/bin/sh"
306#else
307#ifdef ADB_PASSWD
308#define SHELL_COMMAND "/bin/login"
309#else
310#define SHELL_COMMAND "/bin/sh"
311#endif //ADB_PASSWD
312#endif
313
314#if !ADB_HOST
315static void subproc_waiter_service(int fd, void *cookie)
316{
317 pid_t pid = (pid_t) (uintptr_t) cookie;
318
319 D("entered. fd=%d of pid=%d\n", fd, pid);
320 for (;;) {
321 int status;
322 pid_t p = waitpid(pid, &status, 0);
323 if (p == pid) {
324 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
325 if (WIFSIGNALED(status)) {
326 D("*** Killed by signal %d\n", WTERMSIG(status));
327 break;
328 } else if (!WIFEXITED(status)) {
329 D("*** Didn't exit!!. status %d\n", status);
330 break;
331 } else if (WEXITSTATUS(status) >= 0) {
332 D("*** Exit code %d\n", WEXITSTATUS(status));
333 break;
334 }
335 }
336 }
337 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
338 if (SHELL_EXIT_NOTIFY_FD >=0) {
339 int res;
340 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
341 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
342 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
343 }
344}
345
346static int create_subproc_thread(const char *name, const subproc_mode mode)
347{
348 stinfo *sti;
349 adb_thread_t t;
350 int ret_fd;
351 pid_t pid = -1;
352
353 const char *arg0, *arg1;
354 if (name == 0 || *name == 0) {
355#ifdef ADB_PASSWD
356 arg0 = "-p"; arg1 = 0;
357#else
358 arg0 = "-"; arg1 = 0;
359#endif //ADB_PASSWD
360 } else {
361 arg0 = "-c"; arg1 = name;
362 }
363
364 switch (mode) {
365 case SUBPROC_PTY:
366 ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
367 break;
368 case SUBPROC_RAW:
369 ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
370 break;
371 default:
372 fprintf(stderr, "invalid subproc_mode %d\n", mode);
373 return -1;
374 }
375 D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
376
377 sti = malloc(sizeof(stinfo));
378 if(sti == 0) fatal("cannot allocate stinfo");
379 sti->func = subproc_waiter_service;
380 sti->cookie = (void*) (uintptr_t) pid;
381 sti->fd = ret_fd;
382
383 if (adb_thread_create(&t, service_bootstrap_func, sti)) {
384 free(sti);
385 adb_close(ret_fd);
386 fprintf(stderr, "cannot create service thread\n");
387 return -1;
388 }
389
390 D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
391 return ret_fd;
392}
393#endif
394
395int service_to_fd(const char *name)
396{
397 int ret = -1;
398
399 if(!strncmp(name, "tcp:", 4)) {
400 int port = atoi(name + 4);
401 name = strchr(name + 4, ':');
402 if(name == 0) {
403 ret = socket_loopback_client(port, SOCK_STREAM);
404 if (ret >= 0)
405 disable_tcp_nagle(ret);
406 } else {
407#if ADB_HOST
408 ret = socket_network_client(name + 1, port, SOCK_STREAM);
409#else
410 return -1;
411#endif
412 }
413#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
414 } else if(!strncmp(name, "local:", 6)) {
415 ret = socket_local_client(name + 6,
416 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
417 } else if(!strncmp(name, "localreserved:", 14)) {
418 ret = socket_local_client(name + 14,
419 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
420 } else if(!strncmp(name, "localabstract:", 14)) {
421 ret = socket_local_client(name + 14,
422 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
423 } else if(!strncmp(name, "localfilesystem:", 16)) {
424 ret = socket_local_client(name + 16,
425 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
426#endif
427#if !ADB_HOST
428 } else if(!strncmp("dev:", name, 4)) {
429 ret = unix_open(name + 4, O_RDWR);
430 } else if(!strncmp(name, "framebuffer:", 12)) {
431 ret = create_service_thread(framebuffer_service, 0);
432 } else if (!strncmp(name, "jdwp:", 5)) {
433 ret = create_jdwp_connection_fd(atoi(name+5));
434 } else if(!HOST && !strncmp(name, "shell:", 6)) {
435 ret = create_subproc_thread(name + 6, SUBPROC_PTY);
436 } else if(!HOST && !strncmp(name, "exec:", 5)) {
437 ret = create_subproc_thread(name + 5, SUBPROC_RAW);
438 } else if(!strncmp(name, "sync:", 5)) {
439 ret = create_service_thread(file_sync_service, NULL);
440 } else if(!strncmp(name, "remount:", 8)) {
441 ret = create_service_thread(remount_service, NULL);
442 } else if(!strncmp(name, "reboot:", 7)) {
443 void* arg = strdup(name + 7);
444 if (arg == NULL) return -1;
445 ret = create_service_thread(reboot_service, arg);
446 } else if(!strncmp(name, "root:", 5)) {
447 ret = create_service_thread(restart_root_service, NULL);
448 } else if(!strncmp(name, "backup:", 7)) {
449 char* arg = strdup(name + 7);
450 if (arg == NULL) return -1;
451 char* c = arg;
452 for (; *c != '\0'; c++) {
453 if (*c == ':')
454 *c = ' ';
455 }
456 char* cmd;
457 if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
458 ret = create_subproc_thread(cmd, SUBPROC_RAW);
459 free(cmd);
460 }
461 free(arg);
462 } else if(!strncmp(name, "restore:", 8)) {
463 ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
464 } else if(!strncmp(name, "tcpip:", 6)) {
465 int port;
466 if (sscanf(name + 6, "%d", &port) == 0) {
467 port = 0;
468 }
469 ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
470 } else if(!strncmp(name, "usb:", 4)) {
471 ret = create_service_thread(restart_usb_service, NULL);
472 } else if (!strncmp(name, "reverse:", 8)) {
473 char* cookie = strdup(name + 8);
474 if (cookie == NULL) {
475 ret = -1;
476 } else {
477 ret = create_service_thread(reverse_service, cookie);
478 if (ret < 0) {
479 free(cookie);
480 }
481 }
482#endif
483 }
484 if (ret >= 0) {
485 close_on_exec(ret);
486 }
487 return ret;
488}
489
490#if ADB_HOST
491struct state_info {
492 transport_type transport;
493 char* serial;
494 int state;
495};
496
497static void wait_for_state(int fd, void* cookie)
498{
499 struct state_info* sinfo = cookie;
500 char* err = "unknown error";
501
502 D("wait_for_state %d\n", sinfo->state);
503
504 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
505 if(t != 0) {
506 writex(fd, "OKAY", 4);
507 } else {
508 sendfailmsg(fd, err);
509 }
510
511 if (sinfo->serial)
512 free(sinfo->serial);
513 free(sinfo);
514 adb_close(fd);
515 D("wait_for_state is done\n");
516}
517
518static void connect_device(char* host, char* buffer, int buffer_size)
519{
520 int port, fd;
521 char* portstr = strchr(host, ':');
522 char hostbuf[100];
523 char serial[100];
524 int ret;
525
526 strncpy(hostbuf, host, sizeof(hostbuf) - 1);
527 if (portstr) {
528 if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
529 snprintf(buffer, buffer_size, "bad host name %s", host);
530 return;
531 }
532 // zero terminate the host at the point we found the colon
533 hostbuf[portstr - host] = 0;
534 if (sscanf(portstr + 1, "%d", &port) == 0) {
535 snprintf(buffer, buffer_size, "bad port number %s", portstr);
536 return;
537 }
538 } else {
539 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
540 }
541
542 snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port);
543
544 fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10);
545 if (fd < 0) {
546 snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
547 return;
548 }
549
550 D("client: connected on remote on fd %d\n", fd);
551 close_on_exec(fd);
552 disable_tcp_nagle(fd);
553
554 ret = register_socket_transport(fd, serial, port, 0);
555 if (ret < 0) {
556 adb_close(fd);
557 snprintf(buffer, buffer_size, "already connected to %s", serial);
558 } else {
559 snprintf(buffer, buffer_size, "connected to %s", serial);
560 }
561}
562
563void connect_emulator(char* port_spec, char* buffer, int buffer_size)
564{
565 char* port_separator = strchr(port_spec, ',');
566 if (!port_separator) {
567 snprintf(buffer, buffer_size,
568 "unable to parse '%s' as <console port>,<adb port>",
569 port_spec);
570 return;
571 }
572
573 // Zero-terminate console port and make port_separator point to 2nd port.
574 *port_separator++ = 0;
575 int console_port = strtol(port_spec, NULL, 0);
576 int adb_port = strtol(port_separator, NULL, 0);
577 if (!(console_port > 0 && adb_port > 0)) {
578 *(port_separator - 1) = ',';
579 snprintf(buffer, buffer_size,
580 "Invalid port numbers: Expected positive numbers, got '%s'",
581 port_spec);
582 return;
583 }
584
585 /* Check if the emulator is already known.
586 * Note: There's a small but harmless race condition here: An emulator not
587 * present just yet could be registered by another invocation right
588 * after doing this check here. However, local_connect protects
589 * against double-registration too. From here, a better error message
590 * can be produced. In the case of the race condition, the very specific
591 * error message won't be shown, but the data doesn't get corrupted. */
592 atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
593 if (known_emulator != NULL) {
594 snprintf(buffer, buffer_size,
595 "Emulator on port %d already registered.", adb_port);
596 return;
597 }
598
599 /* Check if more emulators can be registered. Similar unproblematic
600 * race condition as above. */
601 int candidate_slot = get_available_local_transport_index();
602 if (candidate_slot < 0) {
603 snprintf(buffer, buffer_size, "Cannot accept more emulators.");
604 return;
605 }
606
607 /* Preconditions met, try to connect to the emulator. */
608 if (!local_connect_arbitrary_ports(console_port, adb_port)) {
609 snprintf(buffer, buffer_size,
610 "Connected to emulator on ports %d,%d", console_port, adb_port);
611 } else {
612 snprintf(buffer, buffer_size,
613 "Could not connect to emulator on ports %d,%d",
614 console_port, adb_port);
615 }
616}
617
618static void connect_service(int fd, void* cookie)
619{
620 char buf[4096];
621 char resp[4096];
622 char *host = cookie;
623
624 if (!strncmp(host, "emu:", 4)) {
625 connect_emulator(host + 4, buf, sizeof(buf));
626 } else {
627 connect_device(host, buf, sizeof(buf));
628 }
629
630 // Send response for emulator and device
631 snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf);
632 writex(fd, resp, strlen(resp));
633 adb_close(fd);
634}
635#endif
636
637#if ADB_HOST
638asocket* host_service_to_socket(const char* name, const char *serial)
639{
640 if (!strcmp(name,"track-devices")) {
641 return create_device_tracker();
642 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
643 struct state_info* sinfo = malloc(sizeof(struct state_info));
644
645 if (serial)
646 sinfo->serial = strdup(serial);
647 else
648 sinfo->serial = NULL;
649
650 name += strlen("wait-for-");
651
652 if (!strncmp(name, "local", strlen("local"))) {
653 sinfo->transport = kTransportLocal;
654 sinfo->state = CS_DEVICE;
655 } else if (!strncmp(name, "usb", strlen("usb"))) {
656 sinfo->transport = kTransportUsb;
657 sinfo->state = CS_DEVICE;
658 } else if (!strncmp(name, "any", strlen("any"))) {
659 sinfo->transport = kTransportAny;
660 sinfo->state = CS_DEVICE;
661 } else {
662 free(sinfo);
663 return NULL;
664 }
665
666 int fd = create_service_thread(wait_for_state, sinfo);
667 return create_local_socket(fd);
668 } else if (!strncmp(name, "connect:", 8)) {
669 const char *host = name + 8;
670 int fd = create_service_thread(connect_service, (void *)host);
671 return create_local_socket(fd);
672 }
673 return NULL;
674}
675#endif /* ADB_HOST */