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 | |
| 17 | #define TRACE_TAG TRACE_ADB |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <ctype.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <errno.h> |
Scott Anderson | c7993af | 2012-05-25 13:55:46 -0700 | [diff] [blame] | 24 | #include <stddef.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <time.h> |
Mike Lockwood | 1f546e6 | 2009-05-25 18:17:55 -0400 | [diff] [blame] | 27 | #include <sys/time.h> |
Ray Donnelly | cbb9891 | 2012-11-29 01:36:08 +0000 | [diff] [blame] | 28 | #include <stdint.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
| 30 | #include "sysdeps.h" |
| 31 | #include "adb.h" |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 32 | #include "adb_auth.h" |
Dan Albert | 21c3eaf | 2015-02-18 17:20:44 -0800 | [diff] [blame^] | 33 | #include "qemu_tracing.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 35 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 36 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #if !ADB_HOST |
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 | #include <private/android_filesystem_config.h> |
Nick Kralevich | e2864bf | 2013-02-28 14:12:58 -0800 | [diff] [blame] | 40 | #include <sys/capability.h> |
Jeff Sharkey | 885342a | 2012-08-14 21:00:22 -0700 | [diff] [blame] | 41 | #include <sys/mount.h> |
Elliott Hughes | b4dd6ef | 2014-07-18 16:44:58 -0700 | [diff] [blame] | 42 | #include <sys/prctl.h> |
Nick Kralevich | d49aa25 | 2014-01-18 09:25:04 -0800 | [diff] [blame] | 43 | #include <getopt.h> |
| 44 | #include <selinux/selinux.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | #endif |
| 46 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 47 | #if ADB_TRACE |
| 48 | ADB_MUTEX_DEFINE( D_lock ); |
| 49 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | |
| 51 | int HOST = 0; |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 52 | int gListenAll = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 54 | static int auth_enabled = 0; |
| 55 | |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 56 | #if !ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | static const char *adb_device_banner = "device"; |
Nick Kralevich | d49aa25 | 2014-01-18 09:25:04 -0800 | [diff] [blame] | 58 | static const char *root_seclabel = NULL; |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 59 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | |
| 61 | void fatal(const char *fmt, ...) |
| 62 | { |
| 63 | va_list ap; |
| 64 | va_start(ap, fmt); |
| 65 | fprintf(stderr, "error: "); |
| 66 | vfprintf(stderr, fmt, ap); |
| 67 | fprintf(stderr, "\n"); |
| 68 | va_end(ap); |
| 69 | exit(-1); |
| 70 | } |
| 71 | |
| 72 | void fatal_errno(const char *fmt, ...) |
| 73 | { |
| 74 | va_list ap; |
| 75 | va_start(ap, fmt); |
| 76 | fprintf(stderr, "error: %s: ", strerror(errno)); |
| 77 | vfprintf(stderr, fmt, ap); |
| 78 | fprintf(stderr, "\n"); |
| 79 | va_end(ap); |
| 80 | exit(-1); |
| 81 | } |
| 82 | |
| 83 | int adb_trace_mask; |
| 84 | |
| 85 | /* read a comma/space/colum/semi-column separated list of tags |
| 86 | * from the ADB_TRACE environment variable and build the trace |
| 87 | * mask from it. note that '1' and 'all' are special cases to |
| 88 | * enable all tracing |
| 89 | */ |
| 90 | void adb_trace_init(void) |
| 91 | { |
| 92 | const char* p = getenv("ADB_TRACE"); |
| 93 | const char* q; |
| 94 | |
| 95 | static const struct { |
| 96 | const char* tag; |
| 97 | int flag; |
| 98 | } tags[] = { |
| 99 | { "1", 0 }, |
| 100 | { "all", 0 }, |
| 101 | { "adb", TRACE_ADB }, |
| 102 | { "sockets", TRACE_SOCKETS }, |
| 103 | { "packets", TRACE_PACKETS }, |
| 104 | { "rwx", TRACE_RWX }, |
| 105 | { "usb", TRACE_USB }, |
| 106 | { "sync", TRACE_SYNC }, |
| 107 | { "sysdeps", TRACE_SYSDEPS }, |
| 108 | { "transport", TRACE_TRANSPORT }, |
| 109 | { "jdwp", TRACE_JDWP }, |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 110 | { "services", TRACE_SERVICES }, |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 111 | { "auth", TRACE_AUTH }, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | { NULL, 0 } |
| 113 | }; |
| 114 | |
| 115 | if (p == NULL) |
| 116 | return; |
| 117 | |
| 118 | /* use a comma/column/semi-colum/space separated list */ |
| 119 | while (*p) { |
| 120 | int len, tagn; |
| 121 | |
| 122 | q = strpbrk(p, " ,:;"); |
| 123 | if (q == NULL) { |
| 124 | q = p + strlen(p); |
| 125 | } |
| 126 | len = q - p; |
| 127 | |
| 128 | for (tagn = 0; tags[tagn].tag != NULL; tagn++) |
| 129 | { |
| 130 | int taglen = strlen(tags[tagn].tag); |
| 131 | |
| 132 | if (len == taglen && !memcmp(tags[tagn].tag, p, len) ) |
| 133 | { |
| 134 | int flag = tags[tagn].flag; |
| 135 | if (flag == 0) { |
| 136 | adb_trace_mask = ~0; |
| 137 | return; |
| 138 | } |
| 139 | adb_trace_mask |= (1 << flag); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | p = q; |
| 144 | if (*p) |
| 145 | p++; |
| 146 | } |
| 147 | } |
| 148 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | apacket *get_apacket(void) |
| 150 | { |
| 151 | apacket *p = malloc(sizeof(apacket)); |
| 152 | if(p == 0) fatal("failed to allocate an apacket"); |
| 153 | memset(p, 0, sizeof(apacket) - MAX_PAYLOAD); |
| 154 | return p; |
| 155 | } |
| 156 | |
| 157 | void put_apacket(apacket *p) |
| 158 | { |
| 159 | free(p); |
| 160 | } |
| 161 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 162 | void handle_online(atransport *t) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 163 | { |
| 164 | D("adb: online\n"); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 165 | t->online = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void handle_offline(atransport *t) |
| 169 | { |
| 170 | D("adb: offline\n"); |
| 171 | //Close the associated usb |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 172 | t->online = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | run_transport_disconnects(t); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 176 | #if DEBUG_PACKETS |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | #define DUMPMAX 32 |
| 178 | void print_packet(const char *label, apacket *p) |
| 179 | { |
| 180 | char *tag; |
| 181 | char *x; |
| 182 | unsigned count; |
| 183 | |
| 184 | switch(p->msg.command){ |
| 185 | case A_SYNC: tag = "SYNC"; break; |
| 186 | case A_CNXN: tag = "CNXN" ; break; |
| 187 | case A_OPEN: tag = "OPEN"; break; |
| 188 | case A_OKAY: tag = "OKAY"; break; |
| 189 | case A_CLSE: tag = "CLSE"; break; |
| 190 | case A_WRTE: tag = "WRTE"; break; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 191 | case A_AUTH: tag = "AUTH"; break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | default: tag = "????"; break; |
| 193 | } |
| 194 | |
| 195 | fprintf(stderr, "%s: %s %08x %08x %04x \"", |
| 196 | label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length); |
| 197 | count = p->msg.data_length; |
| 198 | x = (char*) p->data; |
| 199 | if(count > DUMPMAX) { |
| 200 | count = DUMPMAX; |
| 201 | tag = "\n"; |
| 202 | } else { |
| 203 | tag = "\"\n"; |
| 204 | } |
| 205 | while(count-- > 0){ |
| 206 | if((*x >= ' ') && (*x < 127)) { |
| 207 | fputc(*x, stderr); |
| 208 | } else { |
| 209 | fputc('.', stderr); |
| 210 | } |
| 211 | x++; |
| 212 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 213 | fputs(tag, stderr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | } |
| 215 | #endif |
| 216 | |
| 217 | static void send_ready(unsigned local, unsigned remote, atransport *t) |
| 218 | { |
| 219 | D("Calling send_ready \n"); |
| 220 | apacket *p = get_apacket(); |
| 221 | p->msg.command = A_OKAY; |
| 222 | p->msg.arg0 = local; |
| 223 | p->msg.arg1 = remote; |
| 224 | send_packet(p, t); |
| 225 | } |
| 226 | |
| 227 | static void send_close(unsigned local, unsigned remote, atransport *t) |
| 228 | { |
| 229 | D("Calling send_close \n"); |
| 230 | apacket *p = get_apacket(); |
| 231 | p->msg.command = A_CLSE; |
| 232 | p->msg.arg0 = local; |
| 233 | p->msg.arg1 = remote; |
| 234 | send_packet(p, t); |
| 235 | } |
| 236 | |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 237 | static size_t fill_connect_data(char *buf, size_t bufsize) |
| 238 | { |
| 239 | #if ADB_HOST |
| 240 | return snprintf(buf, bufsize, "host::") + 1; |
| 241 | #else |
| 242 | static const char *cnxn_props[] = { |
| 243 | "ro.product.name", |
| 244 | "ro.product.model", |
| 245 | "ro.product.device", |
| 246 | }; |
| 247 | static const int num_cnxn_props = ARRAY_SIZE(cnxn_props); |
| 248 | int i; |
| 249 | size_t remaining = bufsize; |
| 250 | size_t len; |
| 251 | |
| 252 | len = snprintf(buf, remaining, "%s::", adb_device_banner); |
| 253 | remaining -= len; |
| 254 | buf += len; |
| 255 | for (i = 0; i < num_cnxn_props; i++) { |
| 256 | char value[PROPERTY_VALUE_MAX]; |
| 257 | property_get(cnxn_props[i], value, ""); |
| 258 | len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value); |
| 259 | remaining -= len; |
| 260 | buf += len; |
| 261 | } |
| 262 | |
| 263 | return bufsize - remaining + 1; |
| 264 | #endif |
| 265 | } |
| 266 | |
David 'Digit' Turner | 2525869 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 267 | #if !ADB_HOST |
| 268 | static void send_msg_with_header(int fd, const char* msg, size_t msglen) { |
| 269 | char header[5]; |
| 270 | if (msglen > 0xffff) |
| 271 | msglen = 0xffff; |
| 272 | snprintf(header, sizeof(header), "%04x", (unsigned)msglen); |
| 273 | writex(fd, header, 4); |
| 274 | writex(fd, msg, msglen); |
| 275 | } |
| 276 | #endif |
| 277 | |
Chih-Hung Hsieh | f787b38 | 2014-09-05 15:38:15 -0700 | [diff] [blame] | 278 | #if ADB_HOST |
David 'Digit' Turner | 2525869 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 279 | static void send_msg_with_okay(int fd, const char* msg, size_t msglen) { |
Snild Dolkow | 2264e7c | 2014-01-30 10:08:38 +0100 | [diff] [blame] | 280 | char header[9]; |
| 281 | if (msglen > 0xffff) |
| 282 | msglen = 0xffff; |
| 283 | snprintf(header, sizeof(header), "OKAY%04x", (unsigned)msglen); |
| 284 | writex(fd, header, 8); |
| 285 | writex(fd, msg, msglen); |
| 286 | } |
Chih-Hung Hsieh | f787b38 | 2014-09-05 15:38:15 -0700 | [diff] [blame] | 287 | #endif // ADB_HOST |
Snild Dolkow | 2264e7c | 2014-01-30 10:08:38 +0100 | [diff] [blame] | 288 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 289 | static void send_connect(atransport *t) |
| 290 | { |
| 291 | D("Calling send_connect \n"); |
| 292 | apacket *cp = get_apacket(); |
| 293 | cp->msg.command = A_CNXN; |
| 294 | cp->msg.arg0 = A_VERSION; |
| 295 | cp->msg.arg1 = MAX_PAYLOAD; |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 296 | cp->msg.data_length = fill_connect_data((char *)cp->data, |
| 297 | sizeof(cp->data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 298 | send_packet(cp, t); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Benoit Goby | 045a4a9 | 2013-01-15 19:59:14 -0800 | [diff] [blame] | 301 | void send_auth_request(atransport *t) |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 302 | { |
| 303 | D("Calling send_auth_request\n"); |
| 304 | apacket *p; |
| 305 | int ret; |
| 306 | |
| 307 | ret = adb_auth_generate_token(t->token, sizeof(t->token)); |
| 308 | if (ret != sizeof(t->token)) { |
| 309 | D("Error generating token ret=%d\n", ret); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | p = get_apacket(); |
| 314 | memcpy(p->data, t->token, ret); |
| 315 | p->msg.command = A_AUTH; |
| 316 | p->msg.arg0 = ADB_AUTH_TOKEN; |
| 317 | p->msg.data_length = ret; |
| 318 | send_packet(p, t); |
| 319 | } |
| 320 | |
| 321 | static void send_auth_response(uint8_t *token, size_t token_size, atransport *t) |
| 322 | { |
| 323 | D("Calling send_auth_response\n"); |
| 324 | apacket *p = get_apacket(); |
| 325 | int ret; |
| 326 | |
| 327 | ret = adb_auth_sign(t->key, token, token_size, p->data); |
| 328 | if (!ret) { |
| 329 | D("Error signing the token\n"); |
| 330 | put_apacket(p); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | p->msg.command = A_AUTH; |
| 335 | p->msg.arg0 = ADB_AUTH_SIGNATURE; |
| 336 | p->msg.data_length = ret; |
| 337 | send_packet(p, t); |
| 338 | } |
| 339 | |
| 340 | static void send_auth_publickey(atransport *t) |
| 341 | { |
| 342 | D("Calling send_auth_publickey\n"); |
| 343 | apacket *p = get_apacket(); |
| 344 | int ret; |
| 345 | |
| 346 | ret = adb_auth_get_userkey(p->data, sizeof(p->data)); |
| 347 | if (!ret) { |
| 348 | D("Failed to get user public key\n"); |
| 349 | put_apacket(p); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | p->msg.command = A_AUTH; |
| 354 | p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY; |
| 355 | p->msg.data_length = ret; |
| 356 | send_packet(p, t); |
| 357 | } |
| 358 | |
| 359 | void adb_auth_verified(atransport *t) |
| 360 | { |
| 361 | handle_online(t); |
| 362 | send_connect(t); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Chih-Hung Hsieh | f787b38 | 2014-09-05 15:38:15 -0700 | [diff] [blame] | 365 | #if ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 366 | static char *connection_state_name(atransport *t) |
| 367 | { |
| 368 | if (t == NULL) { |
| 369 | return "unknown"; |
| 370 | } |
| 371 | |
| 372 | switch(t->connection_state) { |
| 373 | case CS_BOOTLOADER: |
| 374 | return "bootloader"; |
| 375 | case CS_DEVICE: |
| 376 | return "device"; |
trevd | a5ad539 | 2013-04-17 14:34:23 +0100 | [diff] [blame] | 377 | case CS_RECOVERY: |
| 378 | return "recovery"; |
| 379 | case CS_SIDELOAD: |
| 380 | return "sideload"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 381 | case CS_OFFLINE: |
| 382 | return "offline"; |
Benoit Goby | 77e8e58 | 2013-01-15 12:36:47 -0800 | [diff] [blame] | 383 | case CS_UNAUTHORIZED: |
| 384 | return "unauthorized"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | default: |
| 386 | return "unknown"; |
| 387 | } |
| 388 | } |
Chih-Hung Hsieh | f787b38 | 2014-09-05 15:38:15 -0700 | [diff] [blame] | 389 | #endif // ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 390 | |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 391 | /* qual_overwrite is used to overwrite a qualifier string. dst is a |
| 392 | * pointer to a char pointer. It is assumed that if *dst is non-NULL, it |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 393 | * was malloc'ed and needs to freed. *dst will be set to a dup of src. |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 394 | */ |
| 395 | static void qual_overwrite(char **dst, const char *src) |
| 396 | { |
| 397 | if (!dst) |
| 398 | return; |
| 399 | |
| 400 | free(*dst); |
| 401 | *dst = NULL; |
| 402 | |
| 403 | if (!src || !*src) |
| 404 | return; |
| 405 | |
| 406 | *dst = strdup(src); |
| 407 | } |
| 408 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | void parse_banner(char *banner, atransport *t) |
| 410 | { |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 411 | static const char *prop_seps = ";"; |
| 412 | static const char key_val_sep = '='; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 413 | char *cp; |
| 414 | char *type; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 415 | |
| 416 | D("parse_banner: %s\n", banner); |
| 417 | type = banner; |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 418 | cp = strchr(type, ':'); |
| 419 | if (cp) { |
| 420 | *cp++ = 0; |
| 421 | /* Nothing is done with second field. */ |
| 422 | cp = strchr(cp, ':'); |
| 423 | if (cp) { |
| 424 | char *save; |
| 425 | char *key; |
Scott Anderson | 1b7a7e8 | 2012-06-05 17:54:27 -0700 | [diff] [blame] | 426 | key = adb_strtok_r(cp + 1, prop_seps, &save); |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 427 | while (key) { |
| 428 | cp = strchr(key, key_val_sep); |
| 429 | if (cp) { |
| 430 | *cp++ = '\0'; |
| 431 | if (!strcmp(key, "ro.product.name")) |
| 432 | qual_overwrite(&t->product, cp); |
| 433 | else if (!strcmp(key, "ro.product.model")) |
| 434 | qual_overwrite(&t->model, cp); |
| 435 | else if (!strcmp(key, "ro.product.device")) |
| 436 | qual_overwrite(&t->device, cp); |
| 437 | } |
Scott Anderson | 1b7a7e8 | 2012-06-05 17:54:27 -0700 | [diff] [blame] | 438 | key = adb_strtok_r(NULL, prop_seps, &save); |
Scott Anderson | e82c2db | 2012-05-25 14:10:02 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | if(!strcmp(type, "bootloader")){ |
| 444 | D("setting connection_state to CS_BOOTLOADER\n"); |
| 445 | t->connection_state = CS_BOOTLOADER; |
| 446 | update_transports(); |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | if(!strcmp(type, "device")) { |
| 451 | D("setting connection_state to CS_DEVICE\n"); |
| 452 | t->connection_state = CS_DEVICE; |
| 453 | update_transports(); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | if(!strcmp(type, "recovery")) { |
| 458 | D("setting connection_state to CS_RECOVERY\n"); |
| 459 | t->connection_state = CS_RECOVERY; |
| 460 | update_transports(); |
| 461 | return; |
| 462 | } |
| 463 | |
Doug Zongker | 447f061 | 2012-01-09 14:54:53 -0800 | [diff] [blame] | 464 | if(!strcmp(type, "sideload")) { |
| 465 | D("setting connection_state to CS_SIDELOAD\n"); |
| 466 | t->connection_state = CS_SIDELOAD; |
| 467 | update_transports(); |
| 468 | return; |
| 469 | } |
| 470 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 471 | t->connection_state = CS_HOST; |
| 472 | } |
| 473 | |
| 474 | void handle_packet(apacket *p, atransport *t) |
| 475 | { |
| 476 | asocket *s; |
| 477 | |
Viral Mehta | 899913f | 2010-06-16 18:41:28 +0530 | [diff] [blame] | 478 | D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0], |
| 479 | ((char*) (&(p->msg.command)))[1], |
| 480 | ((char*) (&(p->msg.command)))[2], |
| 481 | ((char*) (&(p->msg.command)))[3]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 482 | print_packet("recv", p); |
| 483 | |
| 484 | switch(p->msg.command){ |
| 485 | case A_SYNC: |
| 486 | if(p->msg.arg0){ |
| 487 | send_packet(p, t); |
| 488 | if(HOST) send_connect(t); |
| 489 | } else { |
| 490 | t->connection_state = CS_OFFLINE; |
| 491 | handle_offline(t); |
| 492 | send_packet(p, t); |
| 493 | } |
| 494 | return; |
| 495 | |
| 496 | case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */ |
| 497 | /* XXX verify version, etc */ |
| 498 | if(t->connection_state != CS_OFFLINE) { |
| 499 | t->connection_state = CS_OFFLINE; |
| 500 | handle_offline(t); |
| 501 | } |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 502 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 503 | parse_banner((char*) p->data, t); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 504 | |
| 505 | if (HOST || !auth_enabled) { |
| 506 | handle_online(t); |
| 507 | if(!HOST) send_connect(t); |
| 508 | } else { |
| 509 | send_auth_request(t); |
| 510 | } |
| 511 | break; |
| 512 | |
| 513 | case A_AUTH: |
| 514 | if (p->msg.arg0 == ADB_AUTH_TOKEN) { |
Benoit Goby | 77e8e58 | 2013-01-15 12:36:47 -0800 | [diff] [blame] | 515 | t->connection_state = CS_UNAUTHORIZED; |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 516 | t->key = adb_auth_nextkey(t->key); |
| 517 | if (t->key) { |
| 518 | send_auth_response(p->data, p->msg.data_length, t); |
| 519 | } else { |
| 520 | /* No more private keys to try, send the public key */ |
| 521 | send_auth_publickey(t); |
| 522 | } |
| 523 | } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) { |
| 524 | if (adb_auth_verify(t->token, p->data, p->msg.data_length)) { |
| 525 | adb_auth_verified(t); |
| 526 | t->failed_auth_attempts = 0; |
| 527 | } else { |
| 528 | if (t->failed_auth_attempts++ > 10) |
| 529 | adb_sleep_ms(1000); |
| 530 | send_auth_request(t); |
| 531 | } |
| 532 | } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) { |
| 533 | adb_auth_confirm_key(p->data, p->msg.data_length, t); |
| 534 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 535 | break; |
| 536 | |
| 537 | case A_OPEN: /* OPEN(local-id, 0, "destination") */ |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 538 | if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 539 | char *name = (char*) p->data; |
| 540 | name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0; |
| 541 | s = create_local_service_socket(name); |
| 542 | if(s == 0) { |
| 543 | send_close(0, p->msg.arg0, t); |
| 544 | } else { |
| 545 | s->peer = create_remote_socket(p->msg.arg0, t); |
| 546 | s->peer->peer = s; |
| 547 | send_ready(s->id, s->peer->id, t); |
| 548 | s->ready(s); |
| 549 | } |
| 550 | } |
| 551 | break; |
| 552 | |
| 553 | case A_OKAY: /* READY(local-id, remote-id, "") */ |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 554 | if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) { |
| 555 | if((s = find_local_socket(p->msg.arg1, 0))) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 556 | if(s->peer == 0) { |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 557 | /* On first READY message, create the connection. */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 558 | s->peer = create_remote_socket(p->msg.arg0, t); |
| 559 | s->peer->peer = s; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 560 | s->ready(s); |
| 561 | } else if (s->peer->id == p->msg.arg0) { |
| 562 | /* Other READY messages must use the same local-id */ |
| 563 | s->ready(s); |
| 564 | } else { |
| 565 | D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s\n", |
| 566 | p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 567 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | break; |
| 571 | |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 572 | case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */ |
| 573 | if (t->online && p->msg.arg1 != 0) { |
| 574 | if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) { |
| 575 | /* According to protocol.txt, p->msg.arg0 might be 0 to indicate |
| 576 | * a failed OPEN only. However, due to a bug in previous ADB |
| 577 | * versions, CLOSE(0, remote-id, "") was also used for normal |
| 578 | * CLOSE() operations. |
| 579 | * |
| 580 | * This is bad because it means a compromised adbd could |
| 581 | * send packets to close connections between the host and |
| 582 | * other devices. To avoid this, only allow this if the local |
| 583 | * socket has a peer on the same transport. |
| 584 | */ |
| 585 | if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) { |
| 586 | D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s\n", |
| 587 | p->msg.arg1, t->serial, s->peer->transport->serial); |
| 588 | } else { |
| 589 | s->close(s); |
| 590 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | break; |
| 594 | |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 595 | case A_WRTE: /* WRITE(local-id, remote-id, <data>) */ |
| 596 | if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) { |
| 597 | if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 598 | unsigned rid = p->msg.arg0; |
| 599 | p->len = p->msg.data_length; |
| 600 | |
| 601 | if(s->enqueue(s, p) == 0) { |
| 602 | D("Enqueue the socket\n"); |
| 603 | send_ready(s->id, rid, t); |
| 604 | } |
| 605 | return; |
| 606 | } |
| 607 | } |
| 608 | break; |
| 609 | |
| 610 | default: |
| 611 | printf("handle_packet: what is %08x?!\n", p->msg.command); |
| 612 | } |
| 613 | |
| 614 | put_apacket(p); |
| 615 | } |
| 616 | |
| 617 | alistener listener_list = { |
| 618 | .next = &listener_list, |
| 619 | .prev = &listener_list, |
| 620 | }; |
| 621 | |
| 622 | static void ss_listener_event_func(int _fd, unsigned ev, void *_l) |
| 623 | { |
| 624 | asocket *s; |
| 625 | |
| 626 | if(ev & FDE_READ) { |
| 627 | struct sockaddr addr; |
| 628 | socklen_t alen; |
| 629 | int fd; |
| 630 | |
| 631 | alen = sizeof(addr); |
| 632 | fd = adb_socket_accept(_fd, &addr, &alen); |
| 633 | if(fd < 0) return; |
| 634 | |
| 635 | adb_socket_setbufsize(fd, CHUNK_SIZE); |
| 636 | |
| 637 | s = create_local_socket(fd); |
| 638 | if(s) { |
| 639 | connect_to_smartsocket(s); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | adb_close(fd); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | static void listener_event_func(int _fd, unsigned ev, void *_l) |
| 648 | { |
| 649 | alistener *l = _l; |
| 650 | asocket *s; |
| 651 | |
| 652 | if(ev & FDE_READ) { |
| 653 | struct sockaddr addr; |
| 654 | socklen_t alen; |
| 655 | int fd; |
| 656 | |
| 657 | alen = sizeof(addr); |
| 658 | fd = adb_socket_accept(_fd, &addr, &alen); |
| 659 | if(fd < 0) return; |
| 660 | |
| 661 | s = create_local_socket(fd); |
| 662 | if(s) { |
| 663 | s->transport = l->transport; |
| 664 | connect_to_remote(s, l->connect_to); |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | adb_close(fd); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | static void free_listener(alistener* l) |
| 673 | { |
| 674 | if (l->next) { |
| 675 | l->next->prev = l->prev; |
| 676 | l->prev->next = l->next; |
| 677 | l->next = l->prev = l; |
| 678 | } |
| 679 | |
| 680 | // closes the corresponding fd |
| 681 | fdevent_remove(&l->fde); |
| 682 | |
| 683 | if (l->local_name) |
| 684 | free((char*)l->local_name); |
| 685 | |
| 686 | if (l->connect_to) |
| 687 | free((char*)l->connect_to); |
| 688 | |
| 689 | if (l->transport) { |
| 690 | remove_transport_disconnect(l->transport, &l->disconnect); |
| 691 | } |
| 692 | free(l); |
| 693 | } |
| 694 | |
| 695 | static void listener_disconnect(void* _l, atransport* t) |
| 696 | { |
| 697 | alistener* l = _l; |
| 698 | |
| 699 | free_listener(l); |
| 700 | } |
| 701 | |
| 702 | int local_name_to_fd(const char *name) |
| 703 | { |
| 704 | int port; |
| 705 | |
| 706 | if(!strncmp("tcp:", name, 4)){ |
| 707 | int ret; |
| 708 | port = atoi(name + 4); |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 709 | |
| 710 | if (gListenAll > 0) { |
| 711 | ret = socket_inaddr_any_server(port, SOCK_STREAM); |
| 712 | } else { |
| 713 | ret = socket_loopback_server(port, SOCK_STREAM); |
| 714 | } |
| 715 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 716 | return ret; |
| 717 | } |
| 718 | #ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */ |
| 719 | // It's non-sensical to support the "reserved" space on the adb host side |
| 720 | if(!strncmp(name, "local:", 6)) { |
| 721 | return socket_local_server(name + 6, |
| 722 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); |
| 723 | } else if(!strncmp(name, "localabstract:", 14)) { |
| 724 | return socket_local_server(name + 14, |
| 725 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); |
| 726 | } else if(!strncmp(name, "localfilesystem:", 16)) { |
| 727 | return socket_local_server(name + 16, |
| 728 | ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); |
| 729 | } |
| 730 | |
| 731 | #endif |
| 732 | printf("unknown local portname '%s'\n", name); |
| 733 | return -1; |
| 734 | } |
| 735 | |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 736 | // Write a single line describing a listener to a user-provided buffer. |
| 737 | // Appends a trailing zero, even in case of truncation, but the function |
| 738 | // returns the full line length. |
| 739 | // If |buffer| is NULL, does not write but returns required size. |
| 740 | static int format_listener(alistener* l, char* buffer, size_t buffer_len) { |
| 741 | // Format is simply: |
| 742 | // |
| 743 | // <device-serial> " " <local-name> " " <remote-name> "\n" |
| 744 | // |
| 745 | int local_len = strlen(l->local_name); |
| 746 | int connect_len = strlen(l->connect_to); |
| 747 | int serial_len = strlen(l->transport->serial); |
| 748 | |
| 749 | if (buffer != NULL) { |
| 750 | snprintf(buffer, buffer_len, "%s %s %s\n", |
| 751 | l->transport->serial, l->local_name, l->connect_to); |
| 752 | } |
| 753 | // NOTE: snprintf() on Windows returns -1 in case of truncation, so |
| 754 | // return the computed line length instead. |
| 755 | return local_len + connect_len + serial_len + 3; |
| 756 | } |
| 757 | |
| 758 | // Write the list of current listeners (network redirections) into a |
| 759 | // user-provided buffer. Appends a trailing zero, even in case of |
| 760 | // trunctaion, but return the full size in bytes. |
| 761 | // If |buffer| is NULL, does not write but returns required size. |
| 762 | static int format_listeners(char* buf, size_t buflen) |
| 763 | { |
| 764 | alistener* l; |
| 765 | int result = 0; |
| 766 | for (l = listener_list.next; l != &listener_list; l = l->next) { |
| 767 | // Ignore special listeners like those for *smartsocket* |
| 768 | if (l->connect_to[0] == '*') |
| 769 | continue; |
| 770 | int len = format_listener(l, buf, buflen); |
| 771 | // Ensure there is space for the trailing zero. |
| 772 | result += len; |
| 773 | if (buf != NULL) { |
| 774 | buf += len; |
| 775 | buflen -= len; |
| 776 | if (buflen <= 0) |
| 777 | break; |
| 778 | } |
| 779 | } |
| 780 | return result; |
| 781 | } |
| 782 | |
| 783 | static int remove_listener(const char *local_name, atransport* transport) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 784 | { |
| 785 | alistener *l; |
| 786 | |
| 787 | for (l = listener_list.next; l != &listener_list; l = l->next) { |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 788 | if (!strcmp(local_name, l->local_name)) { |
| 789 | listener_disconnect(l, l->transport); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 793 | return -1; |
| 794 | } |
| 795 | |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 796 | static void remove_all_listeners(void) |
| 797 | { |
| 798 | alistener *l, *l_next; |
| 799 | for (l = listener_list.next; l != &listener_list; l = l_next) { |
| 800 | l_next = l->next; |
| 801 | // Never remove smart sockets. |
| 802 | if (l->connect_to[0] == '*') |
| 803 | continue; |
| 804 | listener_disconnect(l, l->transport); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | // error/status codes for install_listener. |
| 809 | typedef enum { |
| 810 | INSTALL_STATUS_OK = 0, |
| 811 | INSTALL_STATUS_INTERNAL_ERROR = -1, |
| 812 | INSTALL_STATUS_CANNOT_BIND = -2, |
| 813 | INSTALL_STATUS_CANNOT_REBIND = -3, |
| 814 | } install_status_t; |
| 815 | |
| 816 | static install_status_t install_listener(const char *local_name, |
| 817 | const char *connect_to, |
| 818 | atransport* transport, |
| 819 | int no_rebind) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 820 | { |
| 821 | alistener *l; |
| 822 | |
| 823 | //printf("install_listener('%s','%s')\n", local_name, connect_to); |
| 824 | |
| 825 | for(l = listener_list.next; l != &listener_list; l = l->next){ |
| 826 | if(strcmp(local_name, l->local_name) == 0) { |
| 827 | char *cto; |
| 828 | |
| 829 | /* can't repurpose a smartsocket */ |
| 830 | if(l->connect_to[0] == '*') { |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 831 | return INSTALL_STATUS_INTERNAL_ERROR; |
| 832 | } |
| 833 | |
| 834 | /* can't repurpose a listener if 'no_rebind' is true */ |
| 835 | if (no_rebind) { |
| 836 | return INSTALL_STATUS_CANNOT_REBIND; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | cto = strdup(connect_to); |
| 840 | if(cto == 0) { |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 841 | return INSTALL_STATUS_INTERNAL_ERROR; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | //printf("rebinding '%s' to '%s'\n", local_name, connect_to); |
| 845 | free((void*) l->connect_to); |
| 846 | l->connect_to = cto; |
| 847 | if (l->transport != transport) { |
| 848 | remove_transport_disconnect(l->transport, &l->disconnect); |
| 849 | l->transport = transport; |
| 850 | add_transport_disconnect(l->transport, &l->disconnect); |
| 851 | } |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 852 | return INSTALL_STATUS_OK; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
| 856 | if((l = calloc(1, sizeof(alistener))) == 0) goto nomem; |
| 857 | if((l->local_name = strdup(local_name)) == 0) goto nomem; |
| 858 | if((l->connect_to = strdup(connect_to)) == 0) goto nomem; |
| 859 | |
| 860 | |
| 861 | l->fd = local_name_to_fd(local_name); |
| 862 | if(l->fd < 0) { |
| 863 | free((void*) l->local_name); |
| 864 | free((void*) l->connect_to); |
| 865 | free(l); |
| 866 | printf("cannot bind '%s'\n", local_name); |
| 867 | return -2; |
| 868 | } |
| 869 | |
| 870 | close_on_exec(l->fd); |
| 871 | if(!strcmp(l->connect_to, "*smartsocket*")) { |
| 872 | fdevent_install(&l->fde, l->fd, ss_listener_event_func, l); |
| 873 | } else { |
| 874 | fdevent_install(&l->fde, l->fd, listener_event_func, l); |
| 875 | } |
| 876 | fdevent_set(&l->fde, FDE_READ); |
| 877 | |
| 878 | l->next = &listener_list; |
| 879 | l->prev = listener_list.prev; |
| 880 | l->next->prev = l; |
| 881 | l->prev->next = l; |
| 882 | l->transport = transport; |
| 883 | |
| 884 | if (transport) { |
| 885 | l->disconnect.opaque = l; |
| 886 | l->disconnect.func = listener_disconnect; |
| 887 | add_transport_disconnect(transport, &l->disconnect); |
| 888 | } |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 889 | return INSTALL_STATUS_OK; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 890 | |
| 891 | nomem: |
| 892 | fatal("cannot allocate listener"); |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 893 | return INSTALL_STATUS_INTERNAL_ERROR; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 894 | } |
| 895 | |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 896 | #if defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 897 | static BOOL WINAPI ctrlc_handler(DWORD type) |
| 898 | { |
| 899 | exit(STATUS_CONTROL_C_EXIT); |
| 900 | return TRUE; |
| 901 | } |
| 902 | #endif |
| 903 | |
| 904 | static void adb_cleanup(void) |
| 905 | { |
| 906 | usb_cleanup(); |
| 907 | } |
| 908 | |
| 909 | void start_logging(void) |
| 910 | { |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 911 | #if defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 912 | char temp[ MAX_PATH ]; |
| 913 | FILE* fnul; |
| 914 | FILE* flog; |
| 915 | |
| 916 | GetTempPath( sizeof(temp) - 8, temp ); |
| 917 | strcat( temp, "adb.log" ); |
| 918 | |
| 919 | /* Win32 specific redirections */ |
| 920 | fnul = fopen( "NUL", "rt" ); |
| 921 | if (fnul != NULL) |
| 922 | stdin[0] = fnul[0]; |
| 923 | |
| 924 | flog = fopen( temp, "at" ); |
| 925 | if (flog == NULL) |
| 926 | flog = fnul; |
| 927 | |
| 928 | setvbuf( flog, NULL, _IONBF, 0 ); |
| 929 | |
| 930 | stdout[0] = flog[0]; |
| 931 | stderr[0] = flog[0]; |
| 932 | fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid()); |
| 933 | #else |
| 934 | int fd; |
| 935 | |
| 936 | fd = unix_open("/dev/null", O_RDONLY); |
| 937 | dup2(fd, 0); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 938 | adb_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 939 | |
| 940 | fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640); |
| 941 | if(fd < 0) { |
| 942 | fd = unix_open("/dev/null", O_WRONLY); |
| 943 | } |
| 944 | dup2(fd, 1); |
| 945 | dup2(fd, 2); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 946 | adb_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 947 | fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid()); |
| 948 | #endif |
| 949 | } |
| 950 | |
| 951 | #if !ADB_HOST |
| 952 | void start_device_log(void) |
| 953 | { |
| 954 | int fd; |
Mike Lockwood | 1f546e6 | 2009-05-25 18:17:55 -0400 | [diff] [blame] | 955 | char path[PATH_MAX]; |
| 956 | struct tm now; |
| 957 | time_t t; |
| 958 | char value[PROPERTY_VALUE_MAX]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 959 | |
Mike Lockwood | 1f546e6 | 2009-05-25 18:17:55 -0400 | [diff] [blame] | 960 | // read the trace mask from persistent property persist.adb.trace_mask |
| 961 | // give up if the property is not set or cannot be parsed |
| 962 | property_get("persist.adb.trace_mask", value, ""); |
| 963 | if (sscanf(value, "%x", &adb_trace_mask) != 1) |
| 964 | return; |
| 965 | |
| 966 | adb_mkdir("/data/adb", 0775); |
| 967 | tzset(); |
| 968 | time(&t); |
| 969 | localtime_r(&t, &now); |
| 970 | strftime(path, sizeof(path), |
| 971 | "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt", |
| 972 | &now); |
| 973 | fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 974 | if (fd < 0) |
| 975 | return; |
| 976 | |
| 977 | // redirect stdout and stderr to the log file |
| 978 | dup2(fd, 1); |
| 979 | dup2(fd, 2); |
| 980 | fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid()); |
Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 981 | adb_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 982 | |
| 983 | fd = unix_open("/dev/null", O_RDONLY); |
| 984 | dup2(fd, 0); |
Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 985 | adb_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 986 | } |
| 987 | #endif |
| 988 | |
| 989 | #if ADB_HOST |
JP Abgrall | 571c136 | 2012-12-06 18:18:12 -0800 | [diff] [blame] | 990 | |
| 991 | #ifdef WORKAROUND_BUG6558362 |
| 992 | #include <sched.h> |
| 993 | #define AFFINITY_ENVVAR "ADB_CPU_AFFINITY_BUG6558362" |
| 994 | void adb_set_affinity(void) |
| 995 | { |
| 996 | cpu_set_t cpu_set; |
| 997 | const char* cpunum_str = getenv(AFFINITY_ENVVAR); |
| 998 | char* strtol_res; |
| 999 | int cpu_num; |
| 1000 | |
| 1001 | if (!cpunum_str || !*cpunum_str) |
| 1002 | return; |
| 1003 | cpu_num = strtol(cpunum_str, &strtol_res, 0); |
| 1004 | if (*strtol_res != '\0') |
| 1005 | fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, AFFINITY_ENVVAR); |
| 1006 | |
| 1007 | sched_getaffinity(0, sizeof(cpu_set), &cpu_set); |
| 1008 | D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]); |
| 1009 | CPU_ZERO(&cpu_set); |
| 1010 | CPU_SET(cpu_num, &cpu_set); |
| 1011 | sched_setaffinity(0, sizeof(cpu_set), &cpu_set); |
| 1012 | sched_getaffinity(0, sizeof(cpu_set), &cpu_set); |
| 1013 | D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]); |
| 1014 | } |
| 1015 | #endif |
| 1016 | |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1017 | int launch_server(int server_port) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1018 | { |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1019 | #if defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1020 | /* we need to start the server in the background */ |
| 1021 | /* we create a PIPE that will be used to wait for the server's "OK" */ |
| 1022 | /* message since the pipe handles must be inheritable, we use a */ |
| 1023 | /* security attribute */ |
| 1024 | HANDLE pipe_read, pipe_write; |
Ray Donnelly | 267aa8b | 2012-11-29 01:18:50 +0000 | [diff] [blame] | 1025 | HANDLE stdout_handle, stderr_handle; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1026 | SECURITY_ATTRIBUTES sa; |
| 1027 | STARTUPINFO startup; |
| 1028 | PROCESS_INFORMATION pinfo; |
| 1029 | char program_path[ MAX_PATH ]; |
| 1030 | int ret; |
| 1031 | |
| 1032 | sa.nLength = sizeof(sa); |
| 1033 | sa.lpSecurityDescriptor = NULL; |
| 1034 | sa.bInheritHandle = TRUE; |
| 1035 | |
| 1036 | /* create pipe, and ensure its read handle isn't inheritable */ |
| 1037 | ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 ); |
| 1038 | if (!ret) { |
| 1039 | fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() ); |
| 1040 | return -1; |
| 1041 | } |
| 1042 | |
| 1043 | SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 ); |
| 1044 | |
Ray Donnelly | 267aa8b | 2012-11-29 01:18:50 +0000 | [diff] [blame] | 1045 | /* Some programs want to launch an adb command and collect its output by |
| 1046 | * calling CreateProcess with inheritable stdout/stderr handles, then |
| 1047 | * using read() to get its output. When this happens, the stdout/stderr |
| 1048 | * handles passed to the adb client process will also be inheritable. |
| 1049 | * When starting the adb server here, care must be taken to reset them |
| 1050 | * to non-inheritable. |
| 1051 | * Otherwise, something bad happens: even if the adb command completes, |
| 1052 | * the calling process is stuck while read()-ing from the stdout/stderr |
| 1053 | * descriptors, because they're connected to corresponding handles in the |
| 1054 | * adb server process (even if the latter never uses/writes to them). |
| 1055 | */ |
| 1056 | stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE ); |
| 1057 | stderr_handle = GetStdHandle( STD_ERROR_HANDLE ); |
| 1058 | if (stdout_handle != INVALID_HANDLE_VALUE) { |
| 1059 | SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 ); |
| 1060 | } |
| 1061 | if (stderr_handle != INVALID_HANDLE_VALUE) { |
| 1062 | SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 ); |
| 1063 | } |
| 1064 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1065 | ZeroMemory( &startup, sizeof(startup) ); |
| 1066 | startup.cb = sizeof(startup); |
| 1067 | startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE ); |
| 1068 | startup.hStdOutput = pipe_write; |
| 1069 | startup.hStdError = GetStdHandle( STD_ERROR_HANDLE ); |
| 1070 | startup.dwFlags = STARTF_USESTDHANDLES; |
| 1071 | |
| 1072 | ZeroMemory( &pinfo, sizeof(pinfo) ); |
| 1073 | |
| 1074 | /* get path of current program */ |
| 1075 | GetModuleFileName( NULL, program_path, sizeof(program_path) ); |
Wenhao Li | a09558c | 2013-11-13 16:23:37 +0800 | [diff] [blame] | 1076 | char args[64]; |
| 1077 | snprintf(args, sizeof(args), "adb -P %d fork-server server", server_port); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1078 | ret = CreateProcess( |
| 1079 | program_path, /* program path */ |
Wenhao Li | a09558c | 2013-11-13 16:23:37 +0800 | [diff] [blame] | 1080 | args, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1081 | /* the fork-server argument will set the |
| 1082 | debug = 2 in the child */ |
| 1083 | NULL, /* process handle is not inheritable */ |
| 1084 | NULL, /* thread handle is not inheritable */ |
| 1085 | TRUE, /* yes, inherit some handles */ |
| 1086 | DETACHED_PROCESS, /* the new process doesn't have a console */ |
| 1087 | NULL, /* use parent's environment block */ |
| 1088 | NULL, /* use parent's starting directory */ |
| 1089 | &startup, /* startup info, i.e. std handles */ |
| 1090 | &pinfo ); |
| 1091 | |
| 1092 | CloseHandle( pipe_write ); |
| 1093 | |
| 1094 | if (!ret) { |
| 1095 | fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() ); |
| 1096 | CloseHandle( pipe_read ); |
| 1097 | return -1; |
| 1098 | } |
| 1099 | |
| 1100 | CloseHandle( pinfo.hProcess ); |
| 1101 | CloseHandle( pinfo.hThread ); |
| 1102 | |
| 1103 | /* wait for the "OK\n" message */ |
| 1104 | { |
| 1105 | char temp[3]; |
| 1106 | DWORD count; |
| 1107 | |
| 1108 | ret = ReadFile( pipe_read, temp, 3, &count, NULL ); |
| 1109 | CloseHandle( pipe_read ); |
| 1110 | if ( !ret ) { |
| 1111 | fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() ); |
| 1112 | return -1; |
| 1113 | } |
| 1114 | if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') { |
| 1115 | fprintf(stderr, "ADB server didn't ACK\n" ); |
| 1116 | return -1; |
| 1117 | } |
| 1118 | } |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1119 | #else /* !defined(_WIN32) */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1120 | char path[PATH_MAX]; |
| 1121 | int fd[2]; |
| 1122 | |
| 1123 | // set up a pipe so the child can tell us when it is ready. |
| 1124 | // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child. |
| 1125 | if (pipe(fd)) { |
| 1126 | fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno); |
| 1127 | return -1; |
| 1128 | } |
Alexey Tarasov | 3166410 | 2009-10-22 02:55:00 +1100 | [diff] [blame] | 1129 | get_my_path(path, PATH_MAX); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1130 | pid_t pid = fork(); |
| 1131 | if(pid < 0) return -1; |
| 1132 | |
| 1133 | if (pid == 0) { |
| 1134 | // child side of the fork |
| 1135 | |
| 1136 | // redirect stderr to the pipe |
| 1137 | // we use stderr instead of stdout due to stdout's buffering behavior. |
| 1138 | adb_close(fd[0]); |
| 1139 | dup2(fd[1], STDERR_FILENO); |
| 1140 | adb_close(fd[1]); |
| 1141 | |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 1142 | char str_port[30]; |
| 1143 | snprintf(str_port, sizeof(str_port), "%d", server_port); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1144 | // child process |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 1145 | int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1146 | // this should not return |
| 1147 | fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno); |
| 1148 | } else { |
| 1149 | // parent side of the fork |
| 1150 | |
| 1151 | char temp[3]; |
| 1152 | |
| 1153 | temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C'; |
| 1154 | // wait for the "OK\n" message |
| 1155 | adb_close(fd[1]); |
| 1156 | int ret = adb_read(fd[0], temp, 3); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1157 | int saved_errno = errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1158 | adb_close(fd[0]); |
| 1159 | if (ret < 0) { |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1160 | fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1161 | return -1; |
| 1162 | } |
| 1163 | if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') { |
| 1164 | fprintf(stderr, "ADB server didn't ACK\n" ); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | |
| 1168 | setsid(); |
| 1169 | } |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1170 | #endif /* !defined(_WIN32) */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1171 | return 0; |
| 1172 | } |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1173 | #endif /* ADB_HOST */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1174 | |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1175 | /* Constructs a local name of form tcp:port. |
| 1176 | * target_str points to the target string, it's content will be overwritten. |
| 1177 | * target_size is the capacity of the target string. |
| 1178 | * server_port is the port number to use for the local name. |
| 1179 | */ |
| 1180 | void build_local_name(char* target_str, size_t target_size, int server_port) |
| 1181 | { |
| 1182 | snprintf(target_str, target_size, "tcp:%d", server_port); |
| 1183 | } |
| 1184 | |
Nick Kralevich | bd9206b | 2012-01-19 10:18:59 -0800 | [diff] [blame] | 1185 | #if !ADB_HOST |
Nick Kralevich | 080427e | 2013-02-15 14:39:15 -0800 | [diff] [blame] | 1186 | |
| 1187 | static void drop_capabilities_bounding_set_if_needed() { |
| 1188 | #ifdef ALLOW_ADBD_ROOT |
| 1189 | char value[PROPERTY_VALUE_MAX]; |
| 1190 | property_get("ro.debuggable", value, ""); |
| 1191 | if (strcmp(value, "1") == 0) { |
| 1192 | return; |
| 1193 | } |
| 1194 | #endif |
| 1195 | int i; |
| 1196 | for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) { |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 1197 | if (i == CAP_SETUID || i == CAP_SETGID) { |
Nick Kralevich | 080427e | 2013-02-15 14:39:15 -0800 | [diff] [blame] | 1198 | // CAP_SETUID CAP_SETGID needed by /system/bin/run-as |
| 1199 | continue; |
| 1200 | } |
| 1201 | int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0); |
| 1202 | |
| 1203 | // Some kernels don't have file capabilities compiled in, and |
| 1204 | // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically |
| 1205 | // die when we see such misconfigured kernels. |
| 1206 | if ((err < 0) && (errno != EINVAL)) { |
| 1207 | exit(1); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | |
Nick Kralevich | bd9206b | 2012-01-19 10:18:59 -0800 | [diff] [blame] | 1212 | static int should_drop_privileges() { |
Nick Kralevich | 5890fe3 | 2012-01-19 13:11:35 -0800 | [diff] [blame] | 1213 | #ifndef ALLOW_ADBD_ROOT |
| 1214 | return 1; |
| 1215 | #else /* ALLOW_ADBD_ROOT */ |
Nick Kralevich | bd9206b | 2012-01-19 10:18:59 -0800 | [diff] [blame] | 1216 | int secure = 0; |
| 1217 | char value[PROPERTY_VALUE_MAX]; |
| 1218 | |
| 1219 | /* run adbd in secure mode if ro.secure is set and |
| 1220 | ** we are not in the emulator |
| 1221 | */ |
| 1222 | property_get("ro.kernel.qemu", value, ""); |
| 1223 | if (strcmp(value, "1") != 0) { |
| 1224 | property_get("ro.secure", value, "1"); |
| 1225 | if (strcmp(value, "1") == 0) { |
| 1226 | // don't run as root if ro.secure is set... |
| 1227 | secure = 1; |
| 1228 | |
| 1229 | // ... except we allow running as root in userdebug builds if the |
| 1230 | // service.adb.root property has been set by the "adb root" command |
| 1231 | property_get("ro.debuggable", value, ""); |
| 1232 | if (strcmp(value, "1") == 0) { |
| 1233 | property_get("service.adb.root", value, ""); |
| 1234 | if (strcmp(value, "1") == 0) { |
| 1235 | secure = 0; |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | return secure; |
Nick Kralevich | 5890fe3 | 2012-01-19 13:11:35 -0800 | [diff] [blame] | 1241 | #endif /* ALLOW_ADBD_ROOT */ |
Nick Kralevich | bd9206b | 2012-01-19 10:18:59 -0800 | [diff] [blame] | 1242 | } |
| 1243 | #endif /* !ADB_HOST */ |
| 1244 | |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1245 | int adb_main(int is_daemon, int server_port) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1246 | { |
| 1247 | #if !ADB_HOST |
Mike Lockwood | 2f38b69 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 1248 | int port; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1249 | char value[PROPERTY_VALUE_MAX]; |
Nick Kralevich | eb68fa8 | 2012-04-02 13:00:35 -0700 | [diff] [blame] | 1250 | |
| 1251 | umask(000); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1252 | #endif |
| 1253 | |
| 1254 | atexit(adb_cleanup); |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1255 | #if defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1256 | SetConsoleCtrlHandler( ctrlc_handler, TRUE ); |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1257 | #else |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1258 | // No SIGCHLD. Let the service subproc handle its children. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1259 | signal(SIGPIPE, SIG_IGN); |
| 1260 | #endif |
| 1261 | |
| 1262 | init_transport_registration(); |
| 1263 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1264 | #if ADB_HOST |
| 1265 | HOST = 1; |
JP Abgrall | 571c136 | 2012-12-06 18:18:12 -0800 | [diff] [blame] | 1266 | |
| 1267 | #ifdef WORKAROUND_BUG6558362 |
| 1268 | if(is_daemon) adb_set_affinity(); |
| 1269 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1270 | usb_init(); |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1271 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 1272 | adb_auth_init(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1273 | |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1274 | char local_name[30]; |
| 1275 | build_local_name(local_name, sizeof(local_name), server_port); |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 1276 | if(install_listener(local_name, "*smartsocket*", NULL, 0)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1277 | exit(1); |
| 1278 | } |
| 1279 | #else |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 1280 | property_get("ro.adb.secure", value, "0"); |
| 1281 | auth_enabled = !strcmp(value, "1"); |
| 1282 | if (auth_enabled) |
| 1283 | adb_auth_init(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1284 | |
Jeff Sharkey | d6d4286 | 2012-09-06 13:05:40 -0700 | [diff] [blame] | 1285 | // Our external storage path may be different than apps, since |
| 1286 | // we aren't able to bind mount after dropping root. |
| 1287 | const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE"); |
| 1288 | if (NULL != adb_external_storage) { |
| 1289 | setenv("EXTERNAL_STORAGE", adb_external_storage, 1); |
| 1290 | } else { |
| 1291 | D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE" |
| 1292 | " unchanged.\n"); |
| 1293 | } |
| 1294 | |
Nick Kralevich | e5cbf4e | 2014-06-18 11:24:27 -0700 | [diff] [blame] | 1295 | /* add extra groups: |
| 1296 | ** AID_ADB to access the USB driver |
| 1297 | ** AID_LOG to read system logs (adb logcat) |
| 1298 | ** AID_INPUT to diagnose input issues (getevent) |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 1299 | ** AID_INET to diagnose network issues (ping) |
Nick Kralevich | e5cbf4e | 2014-06-18 11:24:27 -0700 | [diff] [blame] | 1300 | ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump) |
| 1301 | ** AID_SDCARD_R to allow reading from the SD card |
| 1302 | ** AID_SDCARD_RW to allow writing to the SD card |
| 1303 | ** AID_NET_BW_STATS to read out qtaguid statistics |
| 1304 | */ |
Ajay Dudani | 8432ddc | 2014-11-21 11:57:29 -0800 | [diff] [blame] | 1305 | gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_NET_BT, |
| 1306 | AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW, |
Nick Kralevich | e5cbf4e | 2014-06-18 11:24:27 -0700 | [diff] [blame] | 1307 | AID_NET_BW_STATS }; |
| 1308 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { |
| 1309 | exit(1); |
| 1310 | } |
| 1311 | |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1312 | /* don't listen on a port (default 5037) if running in secure mode */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1313 | /* don't run as root if we are running in secure mode */ |
Nick Kralevich | bd9206b | 2012-01-19 10:18:59 -0800 | [diff] [blame] | 1314 | if (should_drop_privileges()) { |
Nick Kralevich | 080427e | 2013-02-15 14:39:15 -0800 | [diff] [blame] | 1315 | drop_capabilities_bounding_set_if_needed(); |
| 1316 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1317 | /* then switch user and group to "shell" */ |
Nick Kralevich | 44db990 | 2010-08-27 14:35:07 -0700 | [diff] [blame] | 1318 | if (setgid(AID_SHELL) != 0) { |
| 1319 | exit(1); |
| 1320 | } |
| 1321 | if (setuid(AID_SHELL) != 0) { |
| 1322 | exit(1); |
| 1323 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1324 | |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1325 | D("Local port disabled\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1326 | } else { |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1327 | char local_name[30]; |
Nick Kralevich | d49aa25 | 2014-01-18 09:25:04 -0800 | [diff] [blame] | 1328 | if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) { |
| 1329 | // b/12587913: fix setcon to allow const pointers |
| 1330 | if (setcon((char *)root_seclabel) < 0) { |
| 1331 | exit(1); |
| 1332 | } |
| 1333 | } |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1334 | build_local_name(local_name, sizeof(local_name), server_port); |
David 'Digit' Turner | 0d82fbf | 2012-11-14 15:01:55 +0100 | [diff] [blame] | 1335 | if(install_listener(local_name, "*smartsocket*", NULL, 0)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1336 | exit(1); |
| 1337 | } |
| 1338 | } |
| 1339 | |
Mike J. Chen | 1dd55c5 | 2012-07-20 18:16:21 -0700 | [diff] [blame] | 1340 | int usb = 0; |
| 1341 | if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) { |
Mike Lockwood | cef31a0 | 2009-08-26 12:50:22 -0700 | [diff] [blame] | 1342 | // listen on USB |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1343 | usb_init(); |
Mike J. Chen | 1dd55c5 | 2012-07-20 18:16:21 -0700 | [diff] [blame] | 1344 | usb = 1; |
| 1345 | } |
| 1346 | |
| 1347 | // If one of these properties is set, also listen on that port |
| 1348 | // If one of the properties isn't set and we couldn't listen on usb, |
| 1349 | // listen on the default port. |
| 1350 | property_get("service.adb.tcp.port", value, ""); |
| 1351 | if (!value[0]) { |
| 1352 | property_get("persist.adb.tcp.port", value, ""); |
| 1353 | } |
| 1354 | if (sscanf(value, "%d", &port) == 1 && port > 0) { |
| 1355 | printf("using port=%d\n", port); |
| 1356 | // listen on TCP port specified by service.adb.tcp.port property |
| 1357 | local_init(port); |
| 1358 | } else if (!usb) { |
Mike Lockwood | cef31a0 | 2009-08-26 12:50:22 -0700 | [diff] [blame] | 1359 | // listen on default port |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1360 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1361 | } |
Mike J. Chen | 1dd55c5 | 2012-07-20 18:16:21 -0700 | [diff] [blame] | 1362 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1363 | D("adb_main(): pre init_jdwp()\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1364 | init_jdwp(); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1365 | D("adb_main(): post init_jdwp()\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1366 | #endif |
| 1367 | |
| 1368 | if (is_daemon) |
| 1369 | { |
| 1370 | // inform our parent that we are up and running. |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1371 | #if defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1372 | DWORD count; |
| 1373 | WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL ); |
Yabin Cui | e77b6a0 | 2014-11-11 09:24:11 -0800 | [diff] [blame] | 1374 | #else |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1375 | fprintf(stderr, "OK\n"); |
| 1376 | #endif |
| 1377 | start_logging(); |
| 1378 | } |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1379 | D("Event loop starting\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1380 | |
| 1381 | fdevent_loop(); |
| 1382 | |
| 1383 | usb_cleanup(); |
| 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
David 'Digit' Turner | 2525869 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 1388 | // Try to handle a network forwarding request. |
| 1389 | // This returns 1 on success, 0 on failure, and -1 to indicate this is not |
| 1390 | // a forwarding-related request. |
| 1391 | int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd) |
| 1392 | { |
| 1393 | if (!strcmp(service, "list-forward")) { |
| 1394 | // Create the list of forward redirections. |
| 1395 | int buffer_size = format_listeners(NULL, 0); |
| 1396 | // Add one byte for the trailing zero. |
| 1397 | char* buffer = malloc(buffer_size + 1); |
| 1398 | if (buffer == NULL) { |
| 1399 | sendfailmsg(reply_fd, "not enough memory"); |
| 1400 | return 1; |
| 1401 | } |
| 1402 | (void) format_listeners(buffer, buffer_size + 1); |
| 1403 | #if ADB_HOST |
| 1404 | send_msg_with_okay(reply_fd, buffer, buffer_size); |
| 1405 | #else |
| 1406 | send_msg_with_header(reply_fd, buffer, buffer_size); |
| 1407 | #endif |
| 1408 | free(buffer); |
| 1409 | return 1; |
| 1410 | } |
| 1411 | |
| 1412 | if (!strcmp(service, "killforward-all")) { |
| 1413 | remove_all_listeners(); |
| 1414 | #if ADB_HOST |
| 1415 | /* On the host: 1st OKAY is connect, 2nd OKAY is status */ |
| 1416 | adb_write(reply_fd, "OKAY", 4); |
| 1417 | #endif |
| 1418 | adb_write(reply_fd, "OKAY", 4); |
| 1419 | return 1; |
| 1420 | } |
| 1421 | |
| 1422 | if (!strncmp(service, "forward:",8) || |
| 1423 | !strncmp(service, "killforward:",12)) { |
| 1424 | char *local, *remote, *err; |
| 1425 | int r; |
| 1426 | atransport *transport; |
| 1427 | |
| 1428 | int createForward = strncmp(service, "kill", 4); |
| 1429 | int no_rebind = 0; |
| 1430 | |
| 1431 | local = strchr(service, ':') + 1; |
| 1432 | |
| 1433 | // Handle forward:norebind:<local>... here |
| 1434 | if (createForward && !strncmp(local, "norebind:", 9)) { |
| 1435 | no_rebind = 1; |
| 1436 | local = strchr(local, ':') + 1; |
| 1437 | } |
| 1438 | |
| 1439 | remote = strchr(local,';'); |
| 1440 | |
| 1441 | if (createForward) { |
| 1442 | // Check forward: parameter format: '<local>;<remote>' |
| 1443 | if(remote == 0) { |
| 1444 | sendfailmsg(reply_fd, "malformed forward spec"); |
| 1445 | return 1; |
| 1446 | } |
| 1447 | |
| 1448 | *remote++ = 0; |
| 1449 | if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')) { |
| 1450 | sendfailmsg(reply_fd, "malformed forward spec"); |
| 1451 | return 1; |
| 1452 | } |
| 1453 | } else { |
| 1454 | // Check killforward: parameter format: '<local>' |
| 1455 | if (local[0] == 0) { |
| 1456 | sendfailmsg(reply_fd, "malformed forward spec"); |
| 1457 | return 1; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | transport = acquire_one_transport(CS_ANY, ttype, serial, &err); |
| 1462 | if (!transport) { |
| 1463 | sendfailmsg(reply_fd, err); |
| 1464 | return 1; |
| 1465 | } |
| 1466 | |
| 1467 | if (createForward) { |
| 1468 | r = install_listener(local, remote, transport, no_rebind); |
| 1469 | } else { |
| 1470 | r = remove_listener(local, transport); |
| 1471 | } |
| 1472 | if(r == 0) { |
| 1473 | #if ADB_HOST |
| 1474 | /* On the host: 1st OKAY is connect, 2nd OKAY is status */ |
| 1475 | writex(reply_fd, "OKAY", 4); |
| 1476 | #endif |
| 1477 | writex(reply_fd, "OKAY", 4); |
| 1478 | return 1; |
| 1479 | } |
| 1480 | |
| 1481 | if (createForward) { |
| 1482 | const char* message; |
| 1483 | switch (r) { |
| 1484 | case INSTALL_STATUS_CANNOT_BIND: |
| 1485 | message = "cannot bind to socket"; |
| 1486 | break; |
| 1487 | case INSTALL_STATUS_CANNOT_REBIND: |
| 1488 | message = "cannot rebind existing socket"; |
| 1489 | break; |
| 1490 | default: |
| 1491 | message = "internal error"; |
| 1492 | } |
| 1493 | sendfailmsg(reply_fd, message); |
| 1494 | } else { |
| 1495 | sendfailmsg(reply_fd, "cannot remove listener"); |
| 1496 | } |
| 1497 | return 1; |
| 1498 | } |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1502 | int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s) |
| 1503 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1504 | if(!strcmp(service, "kill")) { |
| 1505 | fprintf(stderr,"adb server killed by remote request\n"); |
| 1506 | fflush(stdout); |
| 1507 | adb_write(reply_fd, "OKAY", 4); |
| 1508 | usb_cleanup(); |
| 1509 | exit(0); |
| 1510 | } |
| 1511 | |
| 1512 | #if ADB_HOST |
Chih-Hung Hsieh | f787b38 | 2014-09-05 15:38:15 -0700 | [diff] [blame] | 1513 | atransport *transport = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1514 | // "transport:" is used for switching transport with a specified serial number |
| 1515 | // "transport-usb:" is used for switching transport to the only USB transport |
| 1516 | // "transport-local:" is used for switching transport to the only local transport |
| 1517 | // "transport-any:" is used for switching transport to the only transport |
| 1518 | if (!strncmp(service, "transport", strlen("transport"))) { |
| 1519 | char* error_string = "unknown failure"; |
| 1520 | transport_type type = kTransportAny; |
| 1521 | |
| 1522 | if (!strncmp(service, "transport-usb", strlen("transport-usb"))) { |
| 1523 | type = kTransportUsb; |
| 1524 | } else if (!strncmp(service, "transport-local", strlen("transport-local"))) { |
| 1525 | type = kTransportLocal; |
| 1526 | } else if (!strncmp(service, "transport-any", strlen("transport-any"))) { |
| 1527 | type = kTransportAny; |
| 1528 | } else if (!strncmp(service, "transport:", strlen("transport:"))) { |
| 1529 | service += strlen("transport:"); |
Tom Marlin | 3175c8e | 2011-07-27 12:56:14 -0500 | [diff] [blame] | 1530 | serial = service; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | transport = acquire_one_transport(CS_ANY, type, serial, &error_string); |
| 1534 | |
| 1535 | if (transport) { |
| 1536 | s->transport = transport; |
| 1537 | adb_write(reply_fd, "OKAY", 4); |
| 1538 | } else { |
| 1539 | sendfailmsg(reply_fd, error_string); |
| 1540 | } |
| 1541 | return 1; |
| 1542 | } |
| 1543 | |
| 1544 | // return a list of all connected devices |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1545 | if (!strncmp(service, "devices", 7)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1546 | char buffer[4096]; |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1547 | int use_long = !strcmp(service+7, "-l"); |
| 1548 | if (use_long || service[7] == 0) { |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1549 | memset(buffer, 0, sizeof(buffer)); |
| 1550 | D("Getting device list \n"); |
| 1551 | list_transports(buffer, sizeof(buffer), use_long); |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1552 | D("Wrote device list \n"); |
Snild Dolkow | 2264e7c | 2014-01-30 10:08:38 +0100 | [diff] [blame] | 1553 | send_msg_with_okay(reply_fd, buffer, strlen(buffer)); |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1554 | return 0; |
| 1555 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Mike Lockwood | 74d7ff8 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1558 | // remove TCP transport |
| 1559 | if (!strncmp(service, "disconnect:", 11)) { |
| 1560 | char buffer[4096]; |
| 1561 | memset(buffer, 0, sizeof(buffer)); |
| 1562 | char* serial = service + 11; |
Mike Lockwood | cbbe79a | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1563 | if (serial[0] == 0) { |
| 1564 | // disconnect from all TCP devices |
| 1565 | unregister_all_tcp_transports(); |
Mike Lockwood | 74d7ff8 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1566 | } else { |
Mike Lockwood | cbbe79a | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1567 | char hostbuf[100]; |
| 1568 | // assume port 5555 if no port is specified |
| 1569 | if (!strchr(serial, ':')) { |
| 1570 | snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial); |
| 1571 | serial = hostbuf; |
| 1572 | } |
| 1573 | atransport *t = find_transport(serial); |
| 1574 | |
| 1575 | if (t) { |
| 1576 | unregister_transport(t); |
| 1577 | } else { |
| 1578 | snprintf(buffer, sizeof(buffer), "No such device %s", serial); |
| 1579 | } |
Mike Lockwood | 74d7ff8 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1580 | } |
| 1581 | |
Snild Dolkow | 2264e7c | 2014-01-30 10:08:38 +0100 | [diff] [blame] | 1582 | send_msg_with_okay(reply_fd, buffer, strlen(buffer)); |
Mike Lockwood | 2f38b69 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 1583 | return 0; |
| 1584 | } |
| 1585 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1586 | // returns our value for ADB_SERVER_VERSION |
| 1587 | if (!strcmp(service, "version")) { |
| 1588 | char version[12]; |
| 1589 | snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION); |
Snild Dolkow | 2264e7c | 2014-01-30 10:08:38 +0100 | [diff] [blame] | 1590 | send_msg_with_okay(reply_fd, version, strlen(version)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | if(!strncmp(service,"get-serialno",strlen("get-serialno"))) { |
| 1595 | char *out = "unknown"; |
| 1596 | transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); |
| 1597 | if (transport && transport->serial) { |
| 1598 | out = transport->serial; |
| 1599 | } |
Snild Dolkow | 2264e7c | 2014-01-30 10:08:38 +0100 | [diff] [blame] | 1600 | send_msg_with_okay(reply_fd, out, strlen(out)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1601 | return 0; |
| 1602 | } |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1603 | if(!strncmp(service,"get-devpath",strlen("get-devpath"))) { |
| 1604 | char *out = "unknown"; |
| 1605 | transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); |
| 1606 | if (transport && transport->devpath) { |
| 1607 | out = transport->devpath; |
| 1608 | } |
Snild Dolkow | 2264e7c | 2014-01-30 10:08:38 +0100 | [diff] [blame] | 1609 | send_msg_with_okay(reply_fd, out, strlen(out)); |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1610 | return 0; |
| 1611 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1612 | // indicates a new emulator instance has started |
| 1613 | if (!strncmp(service,"emulator:",9)) { |
| 1614 | int port = atoi(service+9); |
| 1615 | local_connect(port); |
| 1616 | /* we don't even need to send a reply */ |
| 1617 | return 0; |
| 1618 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1619 | |
| 1620 | if(!strncmp(service,"get-state",strlen("get-state"))) { |
| 1621 | transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); |
| 1622 | char *state = connection_state_name(transport); |
Snild Dolkow | 2264e7c | 2014-01-30 10:08:38 +0100 | [diff] [blame] | 1623 | send_msg_with_okay(reply_fd, state, strlen(state)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1624 | return 0; |
| 1625 | } |
Simon Ye | dc22c3c | 2014-07-14 17:23:06 -0700 | [diff] [blame] | 1626 | #endif // ADB_HOST |
| 1627 | |
| 1628 | int ret = handle_forward_request(service, ttype, serial, reply_fd); |
| 1629 | if (ret >= 0) |
| 1630 | return ret - 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1631 | return -1; |
| 1632 | } |
| 1633 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1634 | int main(int argc, char **argv) |
| 1635 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1636 | #if ADB_HOST |
| 1637 | adb_sysdeps_init(); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1638 | adb_trace_init(); |
| 1639 | D("Handling commandline()\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1640 | return adb_commandline(argc - 1, argv + 1); |
| 1641 | #else |
Vladimir Chtchetkine | 28781b0 | 2012-02-27 10:41:53 -0800 | [diff] [blame] | 1642 | /* If adbd runs inside the emulator this will enable adb tracing via |
| 1643 | * adb-debug qemud service in the emulator. */ |
| 1644 | adb_qemu_trace_init(); |
Nick Kralevich | d49aa25 | 2014-01-18 09:25:04 -0800 | [diff] [blame] | 1645 | while(1) { |
| 1646 | int c; |
| 1647 | int option_index = 0; |
| 1648 | static struct option opts[] = { |
| 1649 | {"root_seclabel", required_argument, 0, 's' }, |
| 1650 | {"device_banner", required_argument, 0, 'b' } |
| 1651 | }; |
| 1652 | c = getopt_long(argc, argv, "", opts, &option_index); |
| 1653 | if (c == -1) |
| 1654 | break; |
| 1655 | switch (c) { |
| 1656 | case 's': |
| 1657 | root_seclabel = optarg; |
| 1658 | break; |
| 1659 | case 'b': |
| 1660 | adb_device_banner = optarg; |
| 1661 | break; |
| 1662 | default: |
| 1663 | break; |
| 1664 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1665 | } |
Mike Lockwood | 1f546e6 | 2009-05-25 18:17:55 -0400 | [diff] [blame] | 1666 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1667 | start_device_log(); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1668 | D("Handling main()\n"); |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 1669 | return adb_main(0, DEFAULT_ADB_PORT); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1670 | #endif |
| 1671 | } |