The Android Open Source Project | 9ca14dc | 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 | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 17 | #include <stddef.h> |
The Android Open Source Project | 9ca14dc | 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 | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 26 | #define TRACE_TAG TRACE_SERVICES |
The Android Open Source Project | 9ca14dc | 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 | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 34 | # include <sys/ioctl.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | # endif |
Mike Lockwood | fa311c3 | 2009-07-30 16:23:56 -0700 | [diff] [blame] | 36 | #else |
Ken Sumrall | 78438c9 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 37 | # include <cutils/android_reboot.h> |
Nick Kralevich | 7472d29 | 2013-05-23 09:54:13 -0700 | [diff] [blame] | 38 | # include <cutils/properties.h> |
The Android Open Source Project | 9ca14dc | 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 | 12dc369 | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 58 | #if !ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | |
The Android Open Source Project | 9c75340 | 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 | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 74 | adb_close(fd); |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
Benoit Goby | ad926c1 | 2012-03-16 14:44:17 -0700 | [diff] [blame] | 78 | property_set("service.adb.root", "1"); |
The Android Open Source Project | 9c75340 | 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 | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Mike Lockwood | 26b88e3 | 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 | 9c0d940 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 89 | int port = (int) (uintptr_t) cookie; |
Mike Lockwood | 26b88e3 | 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 | 26b88e3 | 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 | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void reboot_service(int fd, void *arg) |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 116 | { |
| 117 | char buf[100]; |
Nick Kralevich | b7df017 | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 118 | char property_val[PROPERTY_VALUE_MAX]; |
Nick Kralevich | 5b5283c | 2014-01-14 16:34:56 -0800 | [diff] [blame] | 119 | int ret; |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 120 | |
| 121 | sync(); |
Mike Lockwood | d49e9eb | 2010-02-24 16:07:23 -0500 | [diff] [blame] | 122 | |
Nick Kralevich | b7df017 | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 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); |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 131 | if (ret < 0) { |
Nick Kralevich | b7df017 | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 132 | snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret); |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 133 | writex(fd, buf, strlen(buf)); |
Nick Kralevich | 5169998 | 2013-10-24 08:53:48 -0700 | [diff] [blame] | 134 | goto cleanup; |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 135 | } |
Nick Kralevich | 5169998 | 2013-10-24 08:53:48 -0700 | [diff] [blame] | 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(); } |
Nick Kralevich | b7df017 | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 139 | cleanup: |
Mike Lockwood | 1a855ae | 2009-09-19 16:52:58 -0400 | [diff] [blame] | 140 | free(arg); |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 141 | adb_close(fd); |
| 142 | } |
| 143 | |
David 'Digit' Turner | 963a449 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 144 | void 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 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 155 | #endif |
| 156 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | static 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 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 186 | #if !ADB_HOST |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 187 | |
| 188 | static void init_subproc_child() |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | { |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 190 | setsid(); |
| 191 | |
Rom Lemarchand | 9d837e5 | 2014-06-20 16:30:54 -0700 | [diff] [blame^] | 192 | // Set OOM score adjustment to prevent killing |
| 193 | int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY); |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 194 | if (fd >= 0) { |
| 195 | adb_write(fd, "0", 1); |
| 196 | adb_close(fd); |
| 197 | } else { |
Rom Lemarchand | 9d837e5 | 2014-06-20 16:30:54 -0700 | [diff] [blame^] | 198 | D("adb: unable to update oom_score_adj\n"); |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | static 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); |
Joe Onorato | 590bca7 | 2009-09-03 16:30:43 -0700 | [diff] [blame] | 205 | #ifdef HAVE_WIN32_PROC |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 206 | fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 207 | return -1; |
Joe Onorato | 590bca7 | 2009-09-03 16:30:43 -0700 | [diff] [blame] | 208 | #else /* !HAVE_WIN32_PROC */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 | char *devname; |
| 210 | int ptm; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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)); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 222 | adb_close(ptm); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | return -1; |
| 224 | } |
| 225 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 226 | *pid = fork(); |
| 227 | if(*pid < 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | printf("- fork failed: %s -\n", strerror(errno)); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 229 | adb_close(ptm); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | return -1; |
| 231 | } |
| 232 | |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 233 | if (*pid == 0) { |
| 234 | init_subproc_child(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 236 | int pts = unix_open(devname, O_RDWR); |
| 237 | if (pts < 0) { |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 238 | fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname); |
| 239 | exit(-1); |
| 240 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 242 | dup2(pts, STDIN_FILENO); |
| 243 | dup2(pts, STDOUT_FILENO); |
| 244 | dup2(pts, STDERR_FILENO); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 245 | |
Benoit Goby | f4ded74 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 246 | adb_close(pts); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 | adb_close(ptm); |
| 248 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 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 { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 254 | return ptm; |
| 255 | } |
Joe Onorato | 590bca7 | 2009-09-03 16:30:43 -0700 | [diff] [blame] | 256 | #endif /* !HAVE_WIN32_PROC */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 257 | } |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 258 | |
| 259 | static 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 | } |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 301 | #endif /* !ABD_HOST */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 302 | |
| 303 | #if ADB_HOST |
| 304 | #define SHELL_COMMAND "/bin/sh" |
| 305 | #else |
| 306 | #define SHELL_COMMAND "/system/bin/sh" |
| 307 | #endif |
| 308 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 309 | #if !ADB_HOST |
| 310 | static void subproc_waiter_service(int fd, void *cookie) |
| 311 | { |
Elliott Hughes | 9c0d940 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 312 | pid_t pid = (pid_t) (uintptr_t) cookie; |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 313 | |
| 314 | D("entered. fd=%d of pid=%d\n", fd, pid); |
| 315 | for (;;) { |
| 316 | int status; |
| 317 | pid_t p = waitpid(pid, &status, 0); |
| 318 | if (p == pid) { |
| 319 | D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status); |
| 320 | if (WIFSIGNALED(status)) { |
| 321 | D("*** Killed by signal %d\n", WTERMSIG(status)); |
| 322 | break; |
| 323 | } else if (!WIFEXITED(status)) { |
| 324 | D("*** Didn't exit!!. status %d\n", status); |
| 325 | break; |
| 326 | } else if (WEXITSTATUS(status) >= 0) { |
| 327 | D("*** Exit code %d\n", WEXITSTATUS(status)); |
| 328 | break; |
| 329 | } |
| 330 | } |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 331 | } |
| 332 | D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno); |
| 333 | if (SHELL_EXIT_NOTIFY_FD >=0) { |
| 334 | int res; |
| 335 | res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)); |
| 336 | D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n", |
| 337 | SHELL_EXIT_NOTIFY_FD, pid, res, errno); |
| 338 | } |
| 339 | } |
| 340 | |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 341 | static int create_subproc_thread(const char *name, const subproc_mode mode) |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 342 | { |
| 343 | stinfo *sti; |
| 344 | adb_thread_t t; |
| 345 | int ret_fd; |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 346 | pid_t pid = -1; |
| 347 | |
| 348 | const char *arg0, *arg1; |
| 349 | if (name == 0 || *name == 0) { |
| 350 | arg0 = "-"; arg1 = 0; |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 351 | } else { |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 352 | arg0 = "-c"; arg1 = name; |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 353 | } |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 354 | |
| 355 | switch (mode) { |
| 356 | case SUBPROC_PTY: |
| 357 | ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid); |
| 358 | break; |
| 359 | case SUBPROC_RAW: |
| 360 | ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid); |
| 361 | break; |
| 362 | default: |
| 363 | fprintf(stderr, "invalid subproc_mode %d\n", mode); |
| 364 | return -1; |
| 365 | } |
| 366 | D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 367 | |
| 368 | sti = malloc(sizeof(stinfo)); |
| 369 | if(sti == 0) fatal("cannot allocate stinfo"); |
| 370 | sti->func = subproc_waiter_service; |
Elliott Hughes | 9c0d940 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 371 | sti->cookie = (void*) (uintptr_t) pid; |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 372 | sti->fd = ret_fd; |
| 373 | |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 374 | if (adb_thread_create(&t, service_bootstrap_func, sti)) { |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 375 | free(sti); |
| 376 | adb_close(ret_fd); |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 377 | fprintf(stderr, "cannot create service thread\n"); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 378 | return -1; |
| 379 | } |
| 380 | |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 381 | D("service thread started, fd=%d pid=%d\n", ret_fd, pid); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 382 | return ret_fd; |
| 383 | } |
| 384 | #endif |
| 385 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | int service_to_fd(const char *name) |
| 387 | { |
| 388 | int ret = -1; |
| 389 | |
| 390 | if(!strncmp(name, "tcp:", 4)) { |
| 391 | int port = atoi(name + 4); |
| 392 | name = strchr(name + 4, ':'); |
| 393 | if(name == 0) { |
| 394 | ret = socket_loopback_client(port, SOCK_STREAM); |
| 395 | if (ret >= 0) |
| 396 | disable_tcp_nagle(ret); |
| 397 | } else { |
| 398 | #if ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 399 | ret = socket_network_client(name + 1, port, SOCK_STREAM); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 400 | #else |
| 401 | return -1; |
| 402 | #endif |
| 403 | } |
| 404 | #ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */ |
| 405 | } else if(!strncmp(name, "local:", 6)) { |
| 406 | ret = socket_local_client(name + 6, |
| 407 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); |
| 408 | } else if(!strncmp(name, "localreserved:", 14)) { |
| 409 | ret = socket_local_client(name + 14, |
| 410 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); |
| 411 | } else if(!strncmp(name, "localabstract:", 14)) { |
| 412 | ret = socket_local_client(name + 14, |
| 413 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); |
| 414 | } else if(!strncmp(name, "localfilesystem:", 16)) { |
| 415 | ret = socket_local_client(name + 16, |
| 416 | ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); |
| 417 | #endif |
Benoit Goby | 12dc369 | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 418 | #if !ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 | } else if(!strncmp("dev:", name, 4)) { |
| 420 | ret = unix_open(name + 4, O_RDWR); |
| 421 | } else if(!strncmp(name, "framebuffer:", 12)) { |
| 422 | ret = create_service_thread(framebuffer_service, 0); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | } else if (!strncmp(name, "jdwp:", 5)) { |
| 424 | ret = create_jdwp_connection_fd(atoi(name+5)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 425 | } else if(!HOST && !strncmp(name, "shell:", 6)) { |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 426 | ret = create_subproc_thread(name + 6, SUBPROC_PTY); |
| 427 | } else if(!HOST && !strncmp(name, "exec:", 5)) { |
| 428 | ret = create_subproc_thread(name + 5, SUBPROC_RAW); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | } else if(!strncmp(name, "sync:", 5)) { |
| 430 | ret = create_service_thread(file_sync_service, NULL); |
| 431 | } else if(!strncmp(name, "remount:", 8)) { |
| 432 | ret = create_service_thread(remount_service, NULL); |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 433 | } else if(!strncmp(name, "reboot:", 7)) { |
Mike Lockwood | 1a855ae | 2009-09-19 16:52:58 -0400 | [diff] [blame] | 434 | void* arg = strdup(name + 7); |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 435 | if (arg == NULL) return -1; |
Mike Lockwood | 1a855ae | 2009-09-19 16:52:58 -0400 | [diff] [blame] | 436 | ret = create_service_thread(reboot_service, arg); |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 437 | } else if(!strncmp(name, "root:", 5)) { |
| 438 | ret = create_service_thread(restart_root_service, NULL); |
Christopher Tate | 7377912 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 439 | } else if(!strncmp(name, "backup:", 7)) { |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 440 | char* arg = strdup(name + 7); |
Christopher Tate | 7377912 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 441 | if (arg == NULL) return -1; |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 442 | char* c = arg; |
| 443 | for (; *c != '\0'; c++) { |
| 444 | if (*c == ':') |
| 445 | *c = ' '; |
| 446 | } |
| 447 | char* cmd; |
| 448 | if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) { |
| 449 | ret = create_subproc_thread(cmd, SUBPROC_RAW); |
| 450 | free(cmd); |
| 451 | } |
| 452 | free(arg); |
Christopher Tate | cf5379b | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 453 | } else if(!strncmp(name, "restore:", 8)) { |
Jeff Sharkey | c52ec1a | 2014-05-26 18:30:43 -0700 | [diff] [blame] | 454 | ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW); |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 455 | } else if(!strncmp(name, "tcpip:", 6)) { |
| 456 | int port; |
| 457 | if (sscanf(name + 6, "%d", &port) == 0) { |
| 458 | port = 0; |
| 459 | } |
Elliott Hughes | 9c0d940 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 460 | ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port); |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 461 | } else if(!strncmp(name, "usb:", 4)) { |
| 462 | ret = create_service_thread(restart_usb_service, NULL); |
David 'Digit' Turner | 963a449 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 463 | } else if (!strncmp(name, "reverse:", 8)) { |
| 464 | char* cookie = strdup(name + 8); |
| 465 | if (cookie == NULL) { |
| 466 | ret = -1; |
| 467 | } else { |
| 468 | ret = create_service_thread(reverse_service, cookie); |
| 469 | if (ret < 0) { |
| 470 | free(cookie); |
| 471 | } |
| 472 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 473 | #endif |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 474 | } |
| 475 | if (ret >= 0) { |
| 476 | close_on_exec(ret); |
| 477 | } |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | #if ADB_HOST |
| 482 | struct state_info { |
| 483 | transport_type transport; |
| 484 | char* serial; |
| 485 | int state; |
| 486 | }; |
| 487 | |
| 488 | static void wait_for_state(int fd, void* cookie) |
| 489 | { |
| 490 | struct state_info* sinfo = cookie; |
| 491 | char* err = "unknown error"; |
| 492 | |
| 493 | D("wait_for_state %d\n", sinfo->state); |
| 494 | |
| 495 | atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err); |
| 496 | if(t != 0) { |
| 497 | writex(fd, "OKAY", 4); |
| 498 | } else { |
| 499 | sendfailmsg(fd, err); |
| 500 | } |
| 501 | |
| 502 | if (sinfo->serial) |
| 503 | free(sinfo->serial); |
| 504 | free(sinfo); |
| 505 | adb_close(fd); |
| 506 | D("wait_for_state is done\n"); |
| 507 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 508 | |
| 509 | static void connect_device(char* host, char* buffer, int buffer_size) |
| 510 | { |
| 511 | int port, fd; |
| 512 | char* portstr = strchr(host, ':'); |
| 513 | char hostbuf[100]; |
| 514 | char serial[100]; |
| 515 | int ret; |
| 516 | |
| 517 | strncpy(hostbuf, host, sizeof(hostbuf) - 1); |
| 518 | if (portstr) { |
| 519 | if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) { |
| 520 | snprintf(buffer, buffer_size, "bad host name %s", host); |
| 521 | return; |
| 522 | } |
| 523 | // zero terminate the host at the point we found the colon |
| 524 | hostbuf[portstr - host] = 0; |
| 525 | if (sscanf(portstr + 1, "%d", &port) == 0) { |
| 526 | snprintf(buffer, buffer_size, "bad port number %s", portstr); |
| 527 | return; |
| 528 | } |
| 529 | } else { |
| 530 | port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; |
| 531 | } |
| 532 | |
| 533 | snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port); |
| 534 | |
Ken Lierman | e6cd0c8 | 2013-06-20 09:27:13 -0700 | [diff] [blame] | 535 | fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 536 | if (fd < 0) { |
| 537 | snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | D("client: connected on remote on fd %d\n", fd); |
| 542 | close_on_exec(fd); |
| 543 | disable_tcp_nagle(fd); |
| 544 | |
| 545 | ret = register_socket_transport(fd, serial, port, 0); |
| 546 | if (ret < 0) { |
| 547 | adb_close(fd); |
| 548 | snprintf(buffer, buffer_size, "already connected to %s", serial); |
| 549 | } else { |
| 550 | snprintf(buffer, buffer_size, "connected to %s", serial); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void connect_emulator(char* port_spec, char* buffer, int buffer_size) |
| 555 | { |
| 556 | char* port_separator = strchr(port_spec, ','); |
| 557 | if (!port_separator) { |
| 558 | snprintf(buffer, buffer_size, |
| 559 | "unable to parse '%s' as <console port>,<adb port>", |
| 560 | port_spec); |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | // Zero-terminate console port and make port_separator point to 2nd port. |
| 565 | *port_separator++ = 0; |
| 566 | int console_port = strtol(port_spec, NULL, 0); |
| 567 | int adb_port = strtol(port_separator, NULL, 0); |
| 568 | if (!(console_port > 0 && adb_port > 0)) { |
| 569 | *(port_separator - 1) = ','; |
| 570 | snprintf(buffer, buffer_size, |
| 571 | "Invalid port numbers: Expected positive numbers, got '%s'", |
| 572 | port_spec); |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | /* Check if the emulator is already known. |
| 577 | * Note: There's a small but harmless race condition here: An emulator not |
| 578 | * present just yet could be registered by another invocation right |
| 579 | * after doing this check here. However, local_connect protects |
| 580 | * against double-registration too. From here, a better error message |
| 581 | * can be produced. In the case of the race condition, the very specific |
| 582 | * error message won't be shown, but the data doesn't get corrupted. */ |
| 583 | atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port); |
| 584 | if (known_emulator != NULL) { |
| 585 | snprintf(buffer, buffer_size, |
| 586 | "Emulator on port %d already registered.", adb_port); |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | /* Check if more emulators can be registered. Similar unproblematic |
| 591 | * race condition as above. */ |
| 592 | int candidate_slot = get_available_local_transport_index(); |
| 593 | if (candidate_slot < 0) { |
| 594 | snprintf(buffer, buffer_size, "Cannot accept more emulators."); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | /* Preconditions met, try to connect to the emulator. */ |
| 599 | if (!local_connect_arbitrary_ports(console_port, adb_port)) { |
| 600 | snprintf(buffer, buffer_size, |
| 601 | "Connected to emulator on ports %d,%d", console_port, adb_port); |
| 602 | } else { |
| 603 | snprintf(buffer, buffer_size, |
| 604 | "Could not connect to emulator on ports %d,%d", |
| 605 | console_port, adb_port); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static void connect_service(int fd, void* cookie) |
| 610 | { |
| 611 | char buf[4096]; |
| 612 | char resp[4096]; |
| 613 | char *host = cookie; |
| 614 | |
| 615 | if (!strncmp(host, "emu:", 4)) { |
| 616 | connect_emulator(host + 4, buf, sizeof(buf)); |
| 617 | } else { |
| 618 | connect_device(host, buf, sizeof(buf)); |
| 619 | } |
| 620 | |
| 621 | // Send response for emulator and device |
| 622 | snprintf(resp, sizeof(resp), "%04x%s",(unsigned)strlen(buf), buf); |
| 623 | writex(fd, resp, strlen(resp)); |
| 624 | adb_close(fd); |
| 625 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 626 | #endif |
| 627 | |
| 628 | #if ADB_HOST |
| 629 | asocket* host_service_to_socket(const char* name, const char *serial) |
| 630 | { |
| 631 | if (!strcmp(name,"track-devices")) { |
| 632 | return create_device_tracker(); |
| 633 | } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) { |
| 634 | struct state_info* sinfo = malloc(sizeof(struct state_info)); |
| 635 | |
| 636 | if (serial) |
| 637 | sinfo->serial = strdup(serial); |
| 638 | else |
| 639 | sinfo->serial = NULL; |
| 640 | |
| 641 | name += strlen("wait-for-"); |
| 642 | |
| 643 | if (!strncmp(name, "local", strlen("local"))) { |
| 644 | sinfo->transport = kTransportLocal; |
| 645 | sinfo->state = CS_DEVICE; |
| 646 | } else if (!strncmp(name, "usb", strlen("usb"))) { |
| 647 | sinfo->transport = kTransportUsb; |
| 648 | sinfo->state = CS_DEVICE; |
| 649 | } else if (!strncmp(name, "any", strlen("any"))) { |
| 650 | sinfo->transport = kTransportAny; |
| 651 | sinfo->state = CS_DEVICE; |
| 652 | } else { |
| 653 | free(sinfo); |
| 654 | return NULL; |
| 655 | } |
| 656 | |
| 657 | int fd = create_service_thread(wait_for_state, sinfo); |
| 658 | return create_local_socket(fd); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 659 | } else if (!strncmp(name, "connect:", 8)) { |
| 660 | const char *host = name + 8; |
| 661 | int fd = create_service_thread(connect_service, (void *)host); |
| 662 | return create_local_socket(fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 663 | } |
| 664 | return NULL; |
| 665 | } |
| 666 | #endif /* ADB_HOST */ |