| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -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 |  | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 17 | #include <stddef.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 |  | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 26 | #define  TRACE_TAG  TRACE_SERVICES | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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> | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 34 | #    include <sys/ioctl.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | #  endif | 
| Mike Lockwood | cc1de48 | 2009-07-30 16:23:56 -0700 | [diff] [blame] | 36 | #else | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 37 | #  include <cutils/android_reboot.h> | 
| Nick Kralevich | 893a4a4 | 2013-05-23 09:54:13 -0700 | [diff] [blame] | 38 | #  include <cutils/properties.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | #endif | 
|  | 40 |  | 
|  | 41 | typedef struct stinfo stinfo; | 
|  | 42 |  | 
|  | 43 | struct stinfo { | 
|  | 44 | void (*func)(int fd, void *cookie); | 
|  | 45 | int fd; | 
|  | 46 | void *cookie; | 
|  | 47 | }; | 
|  | 48 |  | 
|  | 49 |  | 
|  | 50 | void *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 |  | 
| Benoit Goby | 9470c2f | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 58 | #if !ADB_HOST | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 |  | 
| The Android Open Source Project | e037fd7 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 60 | void 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)); | 
| Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 74 | adb_close(fd); | 
| The Android Open Source Project | e037fd7 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 75 | return; | 
|  | 76 | } | 
|  | 77 |  | 
| Benoit Goby | 7941cf8 | 2012-03-16 14:44:17 -0700 | [diff] [blame] | 78 | property_set("service.adb.root", "1"); | 
| The Android Open Source Project | e037fd7 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 79 | snprintf(buf, sizeof(buf), "restarting adbd as root\n"); | 
|  | 80 | writex(fd, buf, strlen(buf)); | 
|  | 81 | adb_close(fd); | 
| The Android Open Source Project | e037fd7 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 82 | } | 
|  | 83 | } | 
|  | 84 |  | 
| Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 85 | void restart_tcp_service(int fd, void *cookie) | 
|  | 86 | { | 
|  | 87 | char buf[100]; | 
|  | 88 | char value[PROPERTY_VALUE_MAX]; | 
| Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 89 | int port = (int) (uintptr_t) cookie; | 
| Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 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); | 
| Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | void 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); | 
| Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
|  | 115 | void reboot_service(int fd, void *arg) | 
| Mike Lockwood | ee15662 | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 116 | { | 
|  | 117 | char buf[100]; | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 118 | char property_val[PROPERTY_VALUE_MAX]; | 
| Mike Lockwood | d969faa | 2010-02-24 16:07:23 -0500 | [diff] [blame] | 119 | int pid, ret; | 
| Mike Lockwood | ee15662 | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 120 |  | 
|  | 121 | sync(); | 
| Mike Lockwood | d969faa | 2010-02-24 16:07:23 -0500 | [diff] [blame] | 122 |  | 
|  | 123 | /* Attempt to unmount the SD card first. | 
|  | 124 | * No need to bother checking for errors. | 
|  | 125 | */ | 
|  | 126 | pid = fork(); | 
|  | 127 | if (pid == 0) { | 
|  | 128 | /* ask vdc to unmount it */ | 
|  | 129 | execl("/system/bin/vdc", "/system/bin/vdc", "volume", "unmount", | 
|  | 130 | getenv("EXTERNAL_STORAGE"), "force", NULL); | 
|  | 131 | } else if (pid > 0) { | 
|  | 132 | /* wait until vdc succeeds or fails */ | 
|  | 133 | waitpid(pid, &ret, 0); | 
|  | 134 | } | 
|  | 135 |  | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 136 | ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg); | 
|  | 137 | if (ret >= (int) sizeof(property_val)) { | 
|  | 138 | snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret); | 
|  | 139 | writex(fd, buf, strlen(buf)); | 
|  | 140 | goto cleanup; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | ret = property_set(ANDROID_RB_PROPERTY, property_val); | 
| Mike Lockwood | ee15662 | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 144 | if (ret < 0) { | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 145 | snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret); | 
| Mike Lockwood | ee15662 | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 146 | writex(fd, buf, strlen(buf)); | 
| Nick Kralevich | 9170452 | 2013-10-24 08:53:48 -0700 | [diff] [blame] | 147 | goto cleanup; | 
| Mike Lockwood | ee15662 | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 148 | } | 
| Nick Kralevich | 9170452 | 2013-10-24 08:53:48 -0700 | [diff] [blame] | 149 | // Don't return early. Give the reboot command time to take effect | 
|  | 150 | // to avoid messing up scripts which do "adb reboot && adb wait-for-device" | 
|  | 151 | while(1) { pause(); } | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 152 | cleanup: | 
| Mike Lockwood | b6b4007 | 2009-09-19 16:52:58 -0400 | [diff] [blame] | 153 | free(arg); | 
| Mike Lockwood | ee15662 | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 154 | adb_close(fd); | 
|  | 155 | } | 
|  | 156 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | #endif | 
|  | 158 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | static int create_service_thread(void (*func)(int, void *), void *cookie) | 
|  | 160 | { | 
|  | 161 | stinfo *sti; | 
|  | 162 | adb_thread_t t; | 
|  | 163 | int s[2]; | 
|  | 164 |  | 
|  | 165 | if(adb_socketpair(s)) { | 
|  | 166 | printf("cannot create service socket pair\n"); | 
|  | 167 | return -1; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | sti = malloc(sizeof(stinfo)); | 
|  | 171 | if(sti == 0) fatal("cannot allocate stinfo"); | 
|  | 172 | sti->func = func; | 
|  | 173 | sti->cookie = cookie; | 
|  | 174 | sti->fd = s[1]; | 
|  | 175 |  | 
|  | 176 | if(adb_thread_create( &t, service_bootstrap_func, sti)){ | 
|  | 177 | free(sti); | 
|  | 178 | adb_close(s[0]); | 
|  | 179 | adb_close(s[1]); | 
|  | 180 | printf("cannot create service thread\n"); | 
|  | 181 | return -1; | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | D("service thread started, %d:%d\n",s[0], s[1]); | 
|  | 185 | return s[0]; | 
|  | 186 | } | 
|  | 187 |  | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 188 | #if !ADB_HOST | 
|  | 189 | static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | { | 
| Joe Onorato | 91acb14 | 2009-09-03 16:30:43 -0700 | [diff] [blame] | 191 | #ifdef HAVE_WIN32_PROC | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 192 | D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1); | 
|  | 193 | fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1); | 
|  | 194 | return -1; | 
| Joe Onorato | 91acb14 | 2009-09-03 16:30:43 -0700 | [diff] [blame] | 195 | #else /* !HAVE_WIN32_PROC */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | char *devname; | 
|  | 197 | int ptm; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 |  | 
|  | 199 | ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY); | 
|  | 200 | if(ptm < 0){ | 
|  | 201 | printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno)); | 
|  | 202 | return -1; | 
|  | 203 | } | 
|  | 204 | fcntl(ptm, F_SETFD, FD_CLOEXEC); | 
|  | 205 |  | 
|  | 206 | if(grantpt(ptm) || unlockpt(ptm) || | 
|  | 207 | ((devname = (char*) ptsname(ptm)) == 0)){ | 
|  | 208 | printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno)); | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 209 | adb_close(ptm); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | return -1; | 
|  | 211 | } | 
|  | 212 |  | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 213 | *pid = fork(); | 
|  | 214 | if(*pid < 0) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | printf("- fork failed: %s -\n", strerror(errno)); | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 216 | adb_close(ptm); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 217 | return -1; | 
|  | 218 | } | 
|  | 219 |  | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 220 | if(*pid == 0){ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 221 | int pts; | 
|  | 222 |  | 
|  | 223 | setsid(); | 
|  | 224 |  | 
|  | 225 | pts = unix_open(devname, O_RDWR); | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 226 | if(pts < 0) { | 
|  | 227 | fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname); | 
|  | 228 | exit(-1); | 
|  | 229 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 |  | 
|  | 231 | dup2(pts, 0); | 
|  | 232 | dup2(pts, 1); | 
|  | 233 | dup2(pts, 2); | 
|  | 234 |  | 
| Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 235 | adb_close(pts); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | adb_close(ptm); | 
|  | 237 |  | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 238 | // set OOM adjustment to zero | 
| Mike Lockwood | 249ad57 | 2009-05-20 09:14:30 -0400 | [diff] [blame] | 239 | char text[64]; | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 240 | snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid()); | 
| Mike Lockwood | 249ad57 | 2009-05-20 09:14:30 -0400 | [diff] [blame] | 241 | int fd = adb_open(text, O_WRONLY); | 
|  | 242 | if (fd >= 0) { | 
|  | 243 | adb_write(fd, "0", 1); | 
|  | 244 | adb_close(fd); | 
|  | 245 | } else { | 
|  | 246 | D("adb: unable to open %s\n", text); | 
|  | 247 | } | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 248 | execl(cmd, cmd, arg0, arg1, NULL); | 
|  | 249 | fprintf(stderr, "- exec '%s' failed: %s (%d) -\n", | 
|  | 250 | cmd, strerror(errno), errno); | 
|  | 251 | exit(-1); | 
|  | 252 | } else { | 
|  | 253 | // Don't set child's OOM adjustment to zero. | 
|  | 254 | // Let the child do it itself, as sometimes the parent starts | 
|  | 255 | // running before the child has a /proc/pid/oom_adj. | 
|  | 256 | // """adb: unable to open /proc/644/oom_adj""" seen in some logs. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 257 | return ptm; | 
|  | 258 | } | 
| Joe Onorato | 91acb14 | 2009-09-03 16:30:43 -0700 | [diff] [blame] | 259 | #endif /* !HAVE_WIN32_PROC */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 260 | } | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 261 | #endif  /* !ABD_HOST */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 262 |  | 
|  | 263 | #if ADB_HOST | 
|  | 264 | #define SHELL_COMMAND "/bin/sh" | 
|  | 265 | #else | 
|  | 266 | #define SHELL_COMMAND "/system/bin/sh" | 
|  | 267 | #endif | 
|  | 268 |  | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 269 | #if !ADB_HOST | 
|  | 270 | static void subproc_waiter_service(int fd, void *cookie) | 
|  | 271 | { | 
| Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 272 | pid_t pid = (pid_t) (uintptr_t) cookie; | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 273 |  | 
|  | 274 | D("entered. fd=%d of pid=%d\n", fd, pid); | 
|  | 275 | for (;;) { | 
|  | 276 | int status; | 
|  | 277 | pid_t p = waitpid(pid, &status, 0); | 
|  | 278 | if (p == pid) { | 
|  | 279 | D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status); | 
|  | 280 | if (WIFSIGNALED(status)) { | 
|  | 281 | D("*** Killed by signal %d\n", WTERMSIG(status)); | 
|  | 282 | break; | 
|  | 283 | } else if (!WIFEXITED(status)) { | 
|  | 284 | D("*** Didn't exit!!. status %d\n", status); | 
|  | 285 | break; | 
|  | 286 | } else if (WEXITSTATUS(status) >= 0) { | 
|  | 287 | D("*** Exit code %d\n", WEXITSTATUS(status)); | 
|  | 288 | break; | 
|  | 289 | } | 
|  | 290 | } | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 291 | } | 
|  | 292 | D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno); | 
|  | 293 | if (SHELL_EXIT_NOTIFY_FD >=0) { | 
|  | 294 | int res; | 
|  | 295 | res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)); | 
|  | 296 | D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n", | 
|  | 297 | SHELL_EXIT_NOTIFY_FD, pid, res, errno); | 
|  | 298 | } | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | static int create_subproc_thread(const char *name) | 
|  | 302 | { | 
|  | 303 | stinfo *sti; | 
|  | 304 | adb_thread_t t; | 
|  | 305 | int ret_fd; | 
|  | 306 | pid_t pid; | 
|  | 307 | if(name) { | 
|  | 308 | ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid); | 
|  | 309 | } else { | 
|  | 310 | ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid); | 
|  | 311 | } | 
|  | 312 | D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid); | 
|  | 313 |  | 
|  | 314 | sti = malloc(sizeof(stinfo)); | 
|  | 315 | if(sti == 0) fatal("cannot allocate stinfo"); | 
|  | 316 | sti->func = subproc_waiter_service; | 
| Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 317 | sti->cookie = (void*) (uintptr_t) pid; | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 318 | sti->fd = ret_fd; | 
|  | 319 |  | 
|  | 320 | if(adb_thread_create( &t, service_bootstrap_func, sti)){ | 
|  | 321 | free(sti); | 
|  | 322 | adb_close(ret_fd); | 
|  | 323 | printf("cannot create service thread\n"); | 
|  | 324 | return -1; | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | D("service thread started, fd=%d pid=%d\n",ret_fd, pid); | 
|  | 328 | return ret_fd; | 
|  | 329 | } | 
|  | 330 | #endif | 
|  | 331 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | int service_to_fd(const char *name) | 
|  | 333 | { | 
|  | 334 | int ret = -1; | 
|  | 335 |  | 
|  | 336 | if(!strncmp(name, "tcp:", 4)) { | 
|  | 337 | int port = atoi(name + 4); | 
|  | 338 | name = strchr(name + 4, ':'); | 
|  | 339 | if(name == 0) { | 
|  | 340 | ret = socket_loopback_client(port, SOCK_STREAM); | 
|  | 341 | if (ret >= 0) | 
|  | 342 | disable_tcp_nagle(ret); | 
|  | 343 | } else { | 
|  | 344 | #if ADB_HOST | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 345 | ret = socket_network_client(name + 1, port, SOCK_STREAM); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 346 | #else | 
|  | 347 | return -1; | 
|  | 348 | #endif | 
|  | 349 | } | 
|  | 350 | #ifndef HAVE_WINSOCK   /* winsock doesn't implement unix domain sockets */ | 
|  | 351 | } else if(!strncmp(name, "local:", 6)) { | 
|  | 352 | ret = socket_local_client(name + 6, | 
|  | 353 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); | 
|  | 354 | } else if(!strncmp(name, "localreserved:", 14)) { | 
|  | 355 | ret = socket_local_client(name + 14, | 
|  | 356 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); | 
|  | 357 | } else if(!strncmp(name, "localabstract:", 14)) { | 
|  | 358 | ret = socket_local_client(name + 14, | 
|  | 359 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); | 
|  | 360 | } else if(!strncmp(name, "localfilesystem:", 16)) { | 
|  | 361 | ret = socket_local_client(name + 16, | 
|  | 362 | ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); | 
|  | 363 | #endif | 
| Benoit Goby | 9470c2f | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 364 | #if !ADB_HOST | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | } else if(!strncmp("dev:", name, 4)) { | 
|  | 366 | ret = unix_open(name + 4, O_RDWR); | 
|  | 367 | } else if(!strncmp(name, "framebuffer:", 12)) { | 
|  | 368 | ret = create_service_thread(framebuffer_service, 0); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 369 | } else if (!strncmp(name, "jdwp:", 5)) { | 
|  | 370 | ret = create_jdwp_connection_fd(atoi(name+5)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 371 | } else if(!HOST && !strncmp(name, "shell:", 6)) { | 
|  | 372 | if(name[6]) { | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 373 | ret = create_subproc_thread(name + 6); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | } else { | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 375 | ret = create_subproc_thread(0); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 376 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 377 | } else if(!strncmp(name, "sync:", 5)) { | 
|  | 378 | ret = create_service_thread(file_sync_service, NULL); | 
|  | 379 | } else if(!strncmp(name, "remount:", 8)) { | 
|  | 380 | ret = create_service_thread(remount_service, NULL); | 
| Mike Lockwood | ee15662 | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 381 | } else if(!strncmp(name, "reboot:", 7)) { | 
| Mike Lockwood | b6b4007 | 2009-09-19 16:52:58 -0400 | [diff] [blame] | 382 | void* arg = strdup(name + 7); | 
|  | 383 | if(arg == 0) return -1; | 
|  | 384 | ret = create_service_thread(reboot_service, arg); | 
| The Android Open Source Project | e037fd7 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 385 | } else if(!strncmp(name, "root:", 5)) { | 
|  | 386 | ret = create_service_thread(restart_root_service, NULL); | 
| Christopher Tate | d2f5415 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 387 | } else if(!strncmp(name, "backup:", 7)) { | 
|  | 388 | char* arg = strdup(name+7); | 
|  | 389 | if (arg == NULL) return -1; | 
| Christopher Tate | 702967a | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 390 | ret = backup_service(BACKUP, arg); | 
|  | 391 | } else if(!strncmp(name, "restore:", 8)) { | 
|  | 392 | ret = backup_service(RESTORE, NULL); | 
| Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 393 | } else if(!strncmp(name, "tcpip:", 6)) { | 
|  | 394 | int port; | 
|  | 395 | if (sscanf(name + 6, "%d", &port) == 0) { | 
|  | 396 | port = 0; | 
|  | 397 | } | 
| Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 398 | ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port); | 
| Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 399 | } else if(!strncmp(name, "usb:", 4)) { | 
|  | 400 | ret = create_service_thread(restart_usb_service, NULL); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 401 | #endif | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 402 | } | 
|  | 403 | if (ret >= 0) { | 
|  | 404 | close_on_exec(ret); | 
|  | 405 | } | 
|  | 406 | return ret; | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | #if ADB_HOST | 
|  | 410 | struct state_info { | 
|  | 411 | transport_type transport; | 
|  | 412 | char* serial; | 
|  | 413 | int state; | 
|  | 414 | }; | 
|  | 415 |  | 
|  | 416 | static void wait_for_state(int fd, void* cookie) | 
|  | 417 | { | 
|  | 418 | struct state_info* sinfo = cookie; | 
|  | 419 | char* err = "unknown error"; | 
|  | 420 |  | 
|  | 421 | D("wait_for_state %d\n", sinfo->state); | 
|  | 422 |  | 
|  | 423 | atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err); | 
|  | 424 | if(t != 0) { | 
|  | 425 | writex(fd, "OKAY", 4); | 
|  | 426 | } else { | 
|  | 427 | sendfailmsg(fd, err); | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | if (sinfo->serial) | 
|  | 431 | free(sinfo->serial); | 
|  | 432 | free(sinfo); | 
|  | 433 | adb_close(fd); | 
|  | 434 | D("wait_for_state is done\n"); | 
|  | 435 | } | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 436 |  | 
|  | 437 | static void connect_device(char* host, char* buffer, int buffer_size) | 
|  | 438 | { | 
|  | 439 | int port, fd; | 
|  | 440 | char* portstr = strchr(host, ':'); | 
|  | 441 | char hostbuf[100]; | 
|  | 442 | char serial[100]; | 
|  | 443 | int ret; | 
|  | 444 |  | 
|  | 445 | strncpy(hostbuf, host, sizeof(hostbuf) - 1); | 
|  | 446 | if (portstr) { | 
|  | 447 | if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) { | 
|  | 448 | snprintf(buffer, buffer_size, "bad host name %s", host); | 
|  | 449 | return; | 
|  | 450 | } | 
|  | 451 | // zero terminate the host at the point we found the colon | 
|  | 452 | hostbuf[portstr - host] = 0; | 
|  | 453 | if (sscanf(portstr + 1, "%d", &port) == 0) { | 
|  | 454 | snprintf(buffer, buffer_size, "bad port number %s", portstr); | 
|  | 455 | return; | 
|  | 456 | } | 
|  | 457 | } else { | 
|  | 458 | port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port); | 
|  | 462 |  | 
| Ken Lierman | aecc6a6 | 2013-06-20 09:27:13 -0700 | [diff] [blame] | 463 | fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 464 | if (fd < 0) { | 
|  | 465 | snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port); | 
|  | 466 | return; | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | D("client: connected on remote on fd %d\n", fd); | 
|  | 470 | close_on_exec(fd); | 
|  | 471 | disable_tcp_nagle(fd); | 
|  | 472 |  | 
|  | 473 | ret = register_socket_transport(fd, serial, port, 0); | 
|  | 474 | if (ret < 0) { | 
|  | 475 | adb_close(fd); | 
|  | 476 | snprintf(buffer, buffer_size, "already connected to %s", serial); | 
|  | 477 | } else { | 
|  | 478 | snprintf(buffer, buffer_size, "connected to %s", serial); | 
|  | 479 | } | 
|  | 480 | } | 
|  | 481 |  | 
|  | 482 | void connect_emulator(char* port_spec, char* buffer, int buffer_size) | 
|  | 483 | { | 
|  | 484 | char* port_separator = strchr(port_spec, ','); | 
|  | 485 | if (!port_separator) { | 
|  | 486 | snprintf(buffer, buffer_size, | 
|  | 487 | "unable to parse '%s' as <console port>,<adb port>", | 
|  | 488 | port_spec); | 
|  | 489 | return; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | // Zero-terminate console port and make port_separator point to 2nd port. | 
|  | 493 | *port_separator++ = 0; | 
|  | 494 | int console_port = strtol(port_spec, NULL, 0); | 
|  | 495 | int adb_port = strtol(port_separator, NULL, 0); | 
|  | 496 | if (!(console_port > 0 && adb_port > 0)) { | 
|  | 497 | *(port_separator - 1) = ','; | 
|  | 498 | snprintf(buffer, buffer_size, | 
|  | 499 | "Invalid port numbers: Expected positive numbers, got '%s'", | 
|  | 500 | port_spec); | 
|  | 501 | return; | 
|  | 502 | } | 
|  | 503 |  | 
|  | 504 | /* Check if the emulator is already known. | 
|  | 505 | * Note: There's a small but harmless race condition here: An emulator not | 
|  | 506 | * present just yet could be registered by another invocation right | 
|  | 507 | * after doing this check here. However, local_connect protects | 
|  | 508 | * against double-registration too. From here, a better error message | 
|  | 509 | * can be produced. In the case of the race condition, the very specific | 
|  | 510 | * error message won't be shown, but the data doesn't get corrupted. */ | 
|  | 511 | atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port); | 
|  | 512 | if (known_emulator != NULL) { | 
|  | 513 | snprintf(buffer, buffer_size, | 
|  | 514 | "Emulator on port %d already registered.", adb_port); | 
|  | 515 | return; | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | /* Check if more emulators can be registered. Similar unproblematic | 
|  | 519 | * race condition as above. */ | 
|  | 520 | int candidate_slot = get_available_local_transport_index(); | 
|  | 521 | if (candidate_slot < 0) { | 
|  | 522 | snprintf(buffer, buffer_size, "Cannot accept more emulators."); | 
|  | 523 | return; | 
|  | 524 | } | 
|  | 525 |  | 
|  | 526 | /* Preconditions met, try to connect to the emulator. */ | 
|  | 527 | if (!local_connect_arbitrary_ports(console_port, adb_port)) { | 
|  | 528 | snprintf(buffer, buffer_size, | 
|  | 529 | "Connected to emulator on ports %d,%d", console_port, adb_port); | 
|  | 530 | } else { | 
|  | 531 | snprintf(buffer, buffer_size, | 
|  | 532 | "Could not connect to emulator on ports %d,%d", | 
|  | 533 | console_port, adb_port); | 
|  | 534 | } | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 | static void connect_service(int fd, void* cookie) | 
|  | 538 | { | 
|  | 539 | char buf[4096]; | 
|  | 540 | char resp[4096]; | 
|  | 541 | char *host = cookie; | 
|  | 542 |  | 
|  | 543 | if (!strncmp(host, "emu:", 4)) { | 
|  | 544 | connect_emulator(host + 4, buf, sizeof(buf)); | 
|  | 545 | } else { | 
|  | 546 | connect_device(host, buf, sizeof(buf)); | 
|  | 547 | } | 
|  | 548 |  | 
|  | 549 | // Send response for emulator and device | 
|  | 550 | snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf); | 
|  | 551 | writex(fd, resp, strlen(resp)); | 
|  | 552 | adb_close(fd); | 
|  | 553 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 554 | #endif | 
|  | 555 |  | 
|  | 556 | #if ADB_HOST | 
|  | 557 | asocket*  host_service_to_socket(const char*  name, const char *serial) | 
|  | 558 | { | 
|  | 559 | if (!strcmp(name,"track-devices")) { | 
|  | 560 | return create_device_tracker(); | 
|  | 561 | } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) { | 
|  | 562 | struct state_info* sinfo = malloc(sizeof(struct state_info)); | 
|  | 563 |  | 
|  | 564 | if (serial) | 
|  | 565 | sinfo->serial = strdup(serial); | 
|  | 566 | else | 
|  | 567 | sinfo->serial = NULL; | 
|  | 568 |  | 
|  | 569 | name += strlen("wait-for-"); | 
|  | 570 |  | 
|  | 571 | if (!strncmp(name, "local", strlen("local"))) { | 
|  | 572 | sinfo->transport = kTransportLocal; | 
|  | 573 | sinfo->state = CS_DEVICE; | 
|  | 574 | } else if (!strncmp(name, "usb", strlen("usb"))) { | 
|  | 575 | sinfo->transport = kTransportUsb; | 
|  | 576 | sinfo->state = CS_DEVICE; | 
|  | 577 | } else if (!strncmp(name, "any", strlen("any"))) { | 
|  | 578 | sinfo->transport = kTransportAny; | 
|  | 579 | sinfo->state = CS_DEVICE; | 
|  | 580 | } else { | 
|  | 581 | free(sinfo); | 
|  | 582 | return NULL; | 
|  | 583 | } | 
|  | 584 |  | 
|  | 585 | int fd = create_service_thread(wait_for_state, sinfo); | 
|  | 586 | return create_local_socket(fd); | 
| Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 587 | } else if (!strncmp(name, "connect:", 8)) { | 
|  | 588 | const char *host = name + 8; | 
|  | 589 | int fd = create_service_thread(connect_service, (void *)host); | 
|  | 590 | return create_local_socket(fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 591 | } | 
|  | 592 | return NULL; | 
|  | 593 | } | 
|  | 594 | #endif /* ADB_HOST */ |