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