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