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