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