blob: cf8e3c476a1b26d367947734b6aed84d62f5ecab [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
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 Andersona40781d2012-05-25 13:55:46 -070024#include <stddef.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <string.h>
26#include <time.h>
Mike Lockwood4ac1e282009-05-25 18:17:55 -040027#include <sys/time.h>
Ray Donnellydccbca22012-11-29 01:36:08 +000028#include <stdint.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080029
30#include "sysdeps.h"
31#include "adb.h"
Benoit Goby2cc19e42012-04-12 12:23:49 -070032#include "adb_auth.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080033
Scott Andersonfa020922012-05-25 14:10:02 -070034#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
35
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080036#if !ADB_HOST
Nick Kralevich7472d292013-05-23 09:54:13 -070037#include <cutils/properties.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080038#include <private/android_filesystem_config.h>
Nick Kralevich9de01bd2013-02-28 14:12:58 -080039#include <sys/capability.h>
Jeff Sharkey11987252012-08-14 21:00:22 -070040#include <sys/mount.h>
Elliott Hughes54546bd2014-07-18 16:44:58 -070041#include <sys/prctl.h>
Nick Kralevich0d9c0862014-01-18 09:25:04 -080042#include <getopt.h>
43#include <selinux/selinux.h>
Xavier Ducrohet2ef5fc22009-05-20 17:33:53 -070044#else
45#include "usb_vendors.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046#endif
47
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070048#if ADB_TRACE
49ADB_MUTEX_DEFINE( D_lock );
50#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051
52int HOST = 0;
Matt Gumbel411775c2012-11-14 10:16:17 -080053int gListenAll = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080054
Benoit Goby2cc19e42012-04-12 12:23:49 -070055static int auth_enabled = 0;
56
Scott Andersonfa020922012-05-25 14:10:02 -070057#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080058static const char *adb_device_banner = "device";
Nick Kralevich0d9c0862014-01-18 09:25:04 -080059static const char *root_seclabel = NULL;
Scott Andersonfa020922012-05-25 14:10:02 -070060#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080061
62void 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
73void 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
84int 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 */
91void 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 Abgrall2e5dd6e2011-03-16 15:57:42 -0700111 { "services", TRACE_SERVICES },
Benoit Goby2cc19e42012-04-12 12:23:49 -0700112 { "auth", TRACE_AUTH },
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800113 { 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 Chtchetkine32a15712012-02-27 10:41:53 -0800150#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. */
173int adb_debug_qemu = -1;
174
175/* Initializes connection with the adb-debug qemud service in the emulator. */
176static 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
190void 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 Project9ca14dc2009-03-03 19:32:55 -0800202
203apacket *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
211void put_apacket(apacket *p)
212{
213 free(p);
214}
215
Benoit Goby2cc19e42012-04-12 12:23:49 -0700216void handle_online(atransport *t)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800217{
218 D("adb: online\n");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700219 t->online = 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800220}
221
222void handle_offline(atransport *t)
223{
224 D("adb: offline\n");
225 //Close the associated usb
Benoit Goby2cc19e42012-04-12 12:23:49 -0700226 t->online = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800227 run_transport_disconnects(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800228}
229
Benoit Goby2cc19e42012-04-12 12:23:49 -0700230#if DEBUG_PACKETS
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800231#define DUMPMAX 32
232void 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 Goby2cc19e42012-04-12 12:23:49 -0700245 case A_AUTH: tag = "AUTH"; break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800246 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 Goby2cc19e42012-04-12 12:23:49 -0700267 fputs(tag, stderr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800268}
269#endif
270
271static 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
281static 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 Andersonfa020922012-05-25 14:10:02 -0700291static 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
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100321#if !ADB_HOST
322static void send_msg_with_header(int fd, const char* msg, size_t msglen) {
323 char header[5];
324 if (msglen > 0xffff)
325 msglen = 0xffff;
326 snprintf(header, sizeof(header), "%04x", (unsigned)msglen);
327 writex(fd, header, 4);
328 writex(fd, msg, msglen);
329}
330#endif
331
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -0700332#if ADB_HOST
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100333static void send_msg_with_okay(int fd, const char* msg, size_t msglen) {
Snild Dolkow4516a872014-01-30 10:08:38 +0100334 char header[9];
335 if (msglen > 0xffff)
336 msglen = 0xffff;
337 snprintf(header, sizeof(header), "OKAY%04x", (unsigned)msglen);
338 writex(fd, header, 8);
339 writex(fd, msg, msglen);
340}
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -0700341#endif // ADB_HOST
Snild Dolkow4516a872014-01-30 10:08:38 +0100342
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800343static void send_connect(atransport *t)
344{
345 D("Calling send_connect \n");
346 apacket *cp = get_apacket();
347 cp->msg.command = A_CNXN;
348 cp->msg.arg0 = A_VERSION;
349 cp->msg.arg1 = MAX_PAYLOAD;
Scott Andersonfa020922012-05-25 14:10:02 -0700350 cp->msg.data_length = fill_connect_data((char *)cp->data,
351 sizeof(cp->data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800352 send_packet(cp, t);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700353}
354
Benoit Gobyd592d6c2013-01-15 19:59:14 -0800355void send_auth_request(atransport *t)
Benoit Goby2cc19e42012-04-12 12:23:49 -0700356{
357 D("Calling send_auth_request\n");
358 apacket *p;
359 int ret;
360
361 ret = adb_auth_generate_token(t->token, sizeof(t->token));
362 if (ret != sizeof(t->token)) {
363 D("Error generating token ret=%d\n", ret);
364 return;
365 }
366
367 p = get_apacket();
368 memcpy(p->data, t->token, ret);
369 p->msg.command = A_AUTH;
370 p->msg.arg0 = ADB_AUTH_TOKEN;
371 p->msg.data_length = ret;
372 send_packet(p, t);
373}
374
375static void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
376{
377 D("Calling send_auth_response\n");
378 apacket *p = get_apacket();
379 int ret;
380
381 ret = adb_auth_sign(t->key, token, token_size, p->data);
382 if (!ret) {
383 D("Error signing the token\n");
384 put_apacket(p);
385 return;
386 }
387
388 p->msg.command = A_AUTH;
389 p->msg.arg0 = ADB_AUTH_SIGNATURE;
390 p->msg.data_length = ret;
391 send_packet(p, t);
392}
393
394static void send_auth_publickey(atransport *t)
395{
396 D("Calling send_auth_publickey\n");
397 apacket *p = get_apacket();
398 int ret;
399
400 ret = adb_auth_get_userkey(p->data, sizeof(p->data));
401 if (!ret) {
402 D("Failed to get user public key\n");
403 put_apacket(p);
404 return;
405 }
406
407 p->msg.command = A_AUTH;
408 p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
409 p->msg.data_length = ret;
410 send_packet(p, t);
411}
412
413void adb_auth_verified(atransport *t)
414{
415 handle_online(t);
416 send_connect(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417}
418
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -0700419#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800420static char *connection_state_name(atransport *t)
421{
422 if (t == NULL) {
423 return "unknown";
424 }
425
426 switch(t->connection_state) {
427 case CS_BOOTLOADER:
428 return "bootloader";
429 case CS_DEVICE:
430 return "device";
trevdd11cfc52013-04-17 14:34:23 +0100431 case CS_RECOVERY:
432 return "recovery";
433 case CS_SIDELOAD:
434 return "sideload";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800435 case CS_OFFLINE:
436 return "offline";
Nick Kralevich23b39f02013-02-15 10:22:08 -0800437 case CS_UNAUTHORIZED:
438 return "unauthorized";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800439 default:
440 return "unknown";
441 }
442}
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -0700443#endif // ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800444
Scott Andersonfa020922012-05-25 14:10:02 -0700445/* qual_overwrite is used to overwrite a qualifier string. dst is a
446 * pointer to a char pointer. It is assumed that if *dst is non-NULL, it
Scott Anderson27042382012-05-30 18:11:27 -0700447 * was malloc'ed and needs to freed. *dst will be set to a dup of src.
Scott Andersonfa020922012-05-25 14:10:02 -0700448 */
449static void qual_overwrite(char **dst, const char *src)
450{
451 if (!dst)
452 return;
453
454 free(*dst);
455 *dst = NULL;
456
457 if (!src || !*src)
458 return;
459
460 *dst = strdup(src);
461}
462
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800463void parse_banner(char *banner, atransport *t)
464{
Scott Andersonfa020922012-05-25 14:10:02 -0700465 static const char *prop_seps = ";";
466 static const char key_val_sep = '=';
Scott Anderson27042382012-05-30 18:11:27 -0700467 char *cp;
468 char *type;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800469
470 D("parse_banner: %s\n", banner);
471 type = banner;
Scott Andersonfa020922012-05-25 14:10:02 -0700472 cp = strchr(type, ':');
473 if (cp) {
474 *cp++ = 0;
475 /* Nothing is done with second field. */
476 cp = strchr(cp, ':');
477 if (cp) {
478 char *save;
479 char *key;
Scott Anderson0fa59782012-06-05 17:54:27 -0700480 key = adb_strtok_r(cp + 1, prop_seps, &save);
Scott Andersonfa020922012-05-25 14:10:02 -0700481 while (key) {
482 cp = strchr(key, key_val_sep);
483 if (cp) {
484 *cp++ = '\0';
485 if (!strcmp(key, "ro.product.name"))
486 qual_overwrite(&t->product, cp);
487 else if (!strcmp(key, "ro.product.model"))
488 qual_overwrite(&t->model, cp);
489 else if (!strcmp(key, "ro.product.device"))
490 qual_overwrite(&t->device, cp);
491 }
Scott Anderson0fa59782012-06-05 17:54:27 -0700492 key = adb_strtok_r(NULL, prop_seps, &save);
Scott Andersonfa020922012-05-25 14:10:02 -0700493 }
494 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800495 }
496
497 if(!strcmp(type, "bootloader")){
498 D("setting connection_state to CS_BOOTLOADER\n");
499 t->connection_state = CS_BOOTLOADER;
500 update_transports();
501 return;
502 }
503
504 if(!strcmp(type, "device")) {
505 D("setting connection_state to CS_DEVICE\n");
506 t->connection_state = CS_DEVICE;
507 update_transports();
508 return;
509 }
510
511 if(!strcmp(type, "recovery")) {
512 D("setting connection_state to CS_RECOVERY\n");
513 t->connection_state = CS_RECOVERY;
514 update_transports();
515 return;
516 }
517
Doug Zongker6b217ed2012-01-09 14:54:53 -0800518 if(!strcmp(type, "sideload")) {
519 D("setting connection_state to CS_SIDELOAD\n");
520 t->connection_state = CS_SIDELOAD;
521 update_transports();
522 return;
523 }
524
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800525 t->connection_state = CS_HOST;
526}
527
528void handle_packet(apacket *p, atransport *t)
529{
530 asocket *s;
531
Viral Mehta841f9a72010-06-16 18:41:28 +0530532 D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
533 ((char*) (&(p->msg.command)))[1],
534 ((char*) (&(p->msg.command)))[2],
535 ((char*) (&(p->msg.command)))[3]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800536 print_packet("recv", p);
537
538 switch(p->msg.command){
539 case A_SYNC:
540 if(p->msg.arg0){
541 send_packet(p, t);
542 if(HOST) send_connect(t);
543 } else {
544 t->connection_state = CS_OFFLINE;
545 handle_offline(t);
546 send_packet(p, t);
547 }
548 return;
549
550 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
551 /* XXX verify version, etc */
552 if(t->connection_state != CS_OFFLINE) {
553 t->connection_state = CS_OFFLINE;
554 handle_offline(t);
555 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700556
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800557 parse_banner((char*) p->data, t);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700558
559 if (HOST || !auth_enabled) {
560 handle_online(t);
561 if(!HOST) send_connect(t);
562 } else {
563 send_auth_request(t);
564 }
565 break;
566
567 case A_AUTH:
568 if (p->msg.arg0 == ADB_AUTH_TOKEN) {
Nick Kralevich23b39f02013-02-15 10:22:08 -0800569 t->connection_state = CS_UNAUTHORIZED;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700570 t->key = adb_auth_nextkey(t->key);
571 if (t->key) {
572 send_auth_response(p->data, p->msg.data_length, t);
573 } else {
574 /* No more private keys to try, send the public key */
575 send_auth_publickey(t);
576 }
577 } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
578 if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
579 adb_auth_verified(t);
580 t->failed_auth_attempts = 0;
581 } else {
582 if (t->failed_auth_attempts++ > 10)
583 adb_sleep_ms(1000);
584 send_auth_request(t);
585 }
586 } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
587 adb_auth_confirm_key(p->data, p->msg.data_length, t);
588 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800589 break;
590
591 case A_OPEN: /* OPEN(local-id, 0, "destination") */
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100592 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800593 char *name = (char*) p->data;
594 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
595 s = create_local_service_socket(name);
596 if(s == 0) {
597 send_close(0, p->msg.arg0, t);
598 } else {
599 s->peer = create_remote_socket(p->msg.arg0, t);
600 s->peer->peer = s;
601 send_ready(s->id, s->peer->id, t);
602 s->ready(s);
603 }
604 }
605 break;
606
607 case A_OKAY: /* READY(local-id, remote-id, "") */
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100608 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
609 if((s = find_local_socket(p->msg.arg1, 0))) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800610 if(s->peer == 0) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100611 /* On first READY message, create the connection. */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800612 s->peer = create_remote_socket(p->msg.arg0, t);
613 s->peer->peer = s;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100614 s->ready(s);
615 } else if (s->peer->id == p->msg.arg0) {
616 /* Other READY messages must use the same local-id */
617 s->ready(s);
618 } else {
619 D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s\n",
620 p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800621 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800622 }
623 }
624 break;
625
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100626 case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */
627 if (t->online && p->msg.arg1 != 0) {
628 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
629 /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
630 * a failed OPEN only. However, due to a bug in previous ADB
631 * versions, CLOSE(0, remote-id, "") was also used for normal
632 * CLOSE() operations.
633 *
634 * This is bad because it means a compromised adbd could
635 * send packets to close connections between the host and
636 * other devices. To avoid this, only allow this if the local
637 * socket has a peer on the same transport.
638 */
639 if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
640 D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s\n",
641 p->msg.arg1, t->serial, s->peer->transport->serial);
642 } else {
643 s->close(s);
644 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800645 }
646 }
647 break;
648
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100649 case A_WRTE: /* WRITE(local-id, remote-id, <data>) */
650 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
651 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800652 unsigned rid = p->msg.arg0;
653 p->len = p->msg.data_length;
654
655 if(s->enqueue(s, p) == 0) {
656 D("Enqueue the socket\n");
657 send_ready(s->id, rid, t);
658 }
659 return;
660 }
661 }
662 break;
663
664 default:
665 printf("handle_packet: what is %08x?!\n", p->msg.command);
666 }
667
668 put_apacket(p);
669}
670
671alistener listener_list = {
672 .next = &listener_list,
673 .prev = &listener_list,
674};
675
676static void ss_listener_event_func(int _fd, unsigned ev, void *_l)
677{
678 asocket *s;
679
680 if(ev & FDE_READ) {
681 struct sockaddr addr;
682 socklen_t alen;
683 int fd;
684
685 alen = sizeof(addr);
686 fd = adb_socket_accept(_fd, &addr, &alen);
687 if(fd < 0) return;
688
689 adb_socket_setbufsize(fd, CHUNK_SIZE);
690
691 s = create_local_socket(fd);
692 if(s) {
693 connect_to_smartsocket(s);
694 return;
695 }
696
697 adb_close(fd);
698 }
699}
700
701static void listener_event_func(int _fd, unsigned ev, void *_l)
702{
703 alistener *l = _l;
704 asocket *s;
705
706 if(ev & FDE_READ) {
707 struct sockaddr addr;
708 socklen_t alen;
709 int fd;
710
711 alen = sizeof(addr);
712 fd = adb_socket_accept(_fd, &addr, &alen);
713 if(fd < 0) return;
714
715 s = create_local_socket(fd);
716 if(s) {
717 s->transport = l->transport;
718 connect_to_remote(s, l->connect_to);
719 return;
720 }
721
722 adb_close(fd);
723 }
724}
725
726static void free_listener(alistener* l)
727{
728 if (l->next) {
729 l->next->prev = l->prev;
730 l->prev->next = l->next;
731 l->next = l->prev = l;
732 }
733
734 // closes the corresponding fd
735 fdevent_remove(&l->fde);
736
737 if (l->local_name)
738 free((char*)l->local_name);
739
740 if (l->connect_to)
741 free((char*)l->connect_to);
742
743 if (l->transport) {
744 remove_transport_disconnect(l->transport, &l->disconnect);
745 }
746 free(l);
747}
748
749static void listener_disconnect(void* _l, atransport* t)
750{
751 alistener* l = _l;
752
753 free_listener(l);
754}
755
756int local_name_to_fd(const char *name)
757{
758 int port;
759
760 if(!strncmp("tcp:", name, 4)){
761 int ret;
762 port = atoi(name + 4);
Matt Gumbel411775c2012-11-14 10:16:17 -0800763
764 if (gListenAll > 0) {
765 ret = socket_inaddr_any_server(port, SOCK_STREAM);
766 } else {
767 ret = socket_loopback_server(port, SOCK_STREAM);
768 }
769
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800770 return ret;
771 }
772#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
773 // It's non-sensical to support the "reserved" space on the adb host side
774 if(!strncmp(name, "local:", 6)) {
775 return socket_local_server(name + 6,
776 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
777 } else if(!strncmp(name, "localabstract:", 14)) {
778 return socket_local_server(name + 14,
779 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
780 } else if(!strncmp(name, "localfilesystem:", 16)) {
781 return socket_local_server(name + 16,
782 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
783 }
784
785#endif
786 printf("unknown local portname '%s'\n", name);
787 return -1;
788}
789
David 'Digit' Turner6c489802012-11-14 15:01:55 +0100790// Write a single line describing a listener to a user-provided buffer.
791// Appends a trailing zero, even in case of truncation, but the function
792// returns the full line length.
793// If |buffer| is NULL, does not write but returns required size.
794static int format_listener(alistener* l, char* buffer, size_t buffer_len) {
795 // Format is simply:
796 //
797 // <device-serial> " " <local-name> " " <remote-name> "\n"
798 //
799 int local_len = strlen(l->local_name);
800 int connect_len = strlen(l->connect_to);
801 int serial_len = strlen(l->transport->serial);
802
803 if (buffer != NULL) {
804 snprintf(buffer, buffer_len, "%s %s %s\n",
805 l->transport->serial, l->local_name, l->connect_to);
806 }
807 // NOTE: snprintf() on Windows returns -1 in case of truncation, so
808 // return the computed line length instead.
809 return local_len + connect_len + serial_len + 3;
810}
811
812// Write the list of current listeners (network redirections) into a
813// user-provided buffer. Appends a trailing zero, even in case of
814// trunctaion, but return the full size in bytes.
815// If |buffer| is NULL, does not write but returns required size.
816static int format_listeners(char* buf, size_t buflen)
817{
818 alistener* l;
819 int result = 0;
820 for (l = listener_list.next; l != &listener_list; l = l->next) {
821 // Ignore special listeners like those for *smartsocket*
822 if (l->connect_to[0] == '*')
823 continue;
824 int len = format_listener(l, buf, buflen);
825 // Ensure there is space for the trailing zero.
826 result += len;
827 if (buf != NULL) {
828 buf += len;
829 buflen -= len;
830 if (buflen <= 0)
831 break;
832 }
833 }
834 return result;
835}
836
837static int remove_listener(const char *local_name, atransport* transport)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800838{
839 alistener *l;
840
841 for (l = listener_list.next; l != &listener_list; l = l->next) {
David 'Digit' Turner6c489802012-11-14 15:01:55 +0100842 if (!strcmp(local_name, l->local_name)) {
843 listener_disconnect(l, l->transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800844 return 0;
845 }
846 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800847 return -1;
848}
849
David 'Digit' Turner6c489802012-11-14 15:01:55 +0100850static void remove_all_listeners(void)
851{
852 alistener *l, *l_next;
853 for (l = listener_list.next; l != &listener_list; l = l_next) {
854 l_next = l->next;
855 // Never remove smart sockets.
856 if (l->connect_to[0] == '*')
857 continue;
858 listener_disconnect(l, l->transport);
859 }
860}
861
862// error/status codes for install_listener.
863typedef enum {
864 INSTALL_STATUS_OK = 0,
865 INSTALL_STATUS_INTERNAL_ERROR = -1,
866 INSTALL_STATUS_CANNOT_BIND = -2,
867 INSTALL_STATUS_CANNOT_REBIND = -3,
868} install_status_t;
869
870static install_status_t install_listener(const char *local_name,
871 const char *connect_to,
872 atransport* transport,
873 int no_rebind)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800874{
875 alistener *l;
876
877 //printf("install_listener('%s','%s')\n", local_name, connect_to);
878
879 for(l = listener_list.next; l != &listener_list; l = l->next){
880 if(strcmp(local_name, l->local_name) == 0) {
881 char *cto;
882
883 /* can't repurpose a smartsocket */
884 if(l->connect_to[0] == '*') {
David 'Digit' Turner6c489802012-11-14 15:01:55 +0100885 return INSTALL_STATUS_INTERNAL_ERROR;
886 }
887
888 /* can't repurpose a listener if 'no_rebind' is true */
889 if (no_rebind) {
890 return INSTALL_STATUS_CANNOT_REBIND;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800891 }
892
893 cto = strdup(connect_to);
894 if(cto == 0) {
David 'Digit' Turner6c489802012-11-14 15:01:55 +0100895 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800896 }
897
898 //printf("rebinding '%s' to '%s'\n", local_name, connect_to);
899 free((void*) l->connect_to);
900 l->connect_to = cto;
901 if (l->transport != transport) {
902 remove_transport_disconnect(l->transport, &l->disconnect);
903 l->transport = transport;
904 add_transport_disconnect(l->transport, &l->disconnect);
905 }
David 'Digit' Turner6c489802012-11-14 15:01:55 +0100906 return INSTALL_STATUS_OK;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800907 }
908 }
909
910 if((l = calloc(1, sizeof(alistener))) == 0) goto nomem;
911 if((l->local_name = strdup(local_name)) == 0) goto nomem;
912 if((l->connect_to = strdup(connect_to)) == 0) goto nomem;
913
914
915 l->fd = local_name_to_fd(local_name);
916 if(l->fd < 0) {
917 free((void*) l->local_name);
918 free((void*) l->connect_to);
919 free(l);
920 printf("cannot bind '%s'\n", local_name);
921 return -2;
922 }
923
924 close_on_exec(l->fd);
925 if(!strcmp(l->connect_to, "*smartsocket*")) {
926 fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);
927 } else {
928 fdevent_install(&l->fde, l->fd, listener_event_func, l);
929 }
930 fdevent_set(&l->fde, FDE_READ);
931
932 l->next = &listener_list;
933 l->prev = listener_list.prev;
934 l->next->prev = l;
935 l->prev->next = l;
936 l->transport = transport;
937
938 if (transport) {
939 l->disconnect.opaque = l;
940 l->disconnect.func = listener_disconnect;
941 add_transport_disconnect(transport, &l->disconnect);
942 }
David 'Digit' Turner6c489802012-11-14 15:01:55 +0100943 return INSTALL_STATUS_OK;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800944
945nomem:
946 fatal("cannot allocate listener");
David 'Digit' Turner6c489802012-11-14 15:01:55 +0100947 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800948}
949
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800950#ifdef HAVE_WIN32_PROC
951static BOOL WINAPI ctrlc_handler(DWORD type)
952{
953 exit(STATUS_CONTROL_C_EXIT);
954 return TRUE;
955}
956#endif
957
958static void adb_cleanup(void)
959{
960 usb_cleanup();
961}
962
963void start_logging(void)
964{
965#ifdef HAVE_WIN32_PROC
966 char temp[ MAX_PATH ];
967 FILE* fnul;
968 FILE* flog;
969
970 GetTempPath( sizeof(temp) - 8, temp );
971 strcat( temp, "adb.log" );
972
973 /* Win32 specific redirections */
974 fnul = fopen( "NUL", "rt" );
975 if (fnul != NULL)
976 stdin[0] = fnul[0];
977
978 flog = fopen( temp, "at" );
979 if (flog == NULL)
980 flog = fnul;
981
982 setvbuf( flog, NULL, _IONBF, 0 );
983
984 stdout[0] = flog[0];
985 stderr[0] = flog[0];
986 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
987#else
988 int fd;
989
990 fd = unix_open("/dev/null", O_RDONLY);
991 dup2(fd, 0);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700992 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800993
994 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
995 if(fd < 0) {
996 fd = unix_open("/dev/null", O_WRONLY);
997 }
998 dup2(fd, 1);
999 dup2(fd, 2);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001000 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001001 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
1002#endif
1003}
1004
1005#if !ADB_HOST
1006void start_device_log(void)
1007{
1008 int fd;
Mike Lockwood4ac1e282009-05-25 18:17:55 -04001009 char path[PATH_MAX];
1010 struct tm now;
1011 time_t t;
1012 char value[PROPERTY_VALUE_MAX];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001013
Mike Lockwood4ac1e282009-05-25 18:17:55 -04001014 // read the trace mask from persistent property persist.adb.trace_mask
1015 // give up if the property is not set or cannot be parsed
1016 property_get("persist.adb.trace_mask", value, "");
1017 if (sscanf(value, "%x", &adb_trace_mask) != 1)
1018 return;
1019
1020 adb_mkdir("/data/adb", 0775);
1021 tzset();
1022 time(&t);
1023 localtime_r(&t, &now);
1024 strftime(path, sizeof(path),
1025 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
1026 &now);
1027 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001028 if (fd < 0)
1029 return;
1030
1031 // redirect stdout and stderr to the log file
1032 dup2(fd, 1);
1033 dup2(fd, 2);
1034 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
Benoit Gobyf4ded742011-02-01 18:57:41 -08001035 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001036
1037 fd = unix_open("/dev/null", O_RDONLY);
1038 dup2(fd, 0);
Benoit Gobyf4ded742011-02-01 18:57:41 -08001039 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001040}
1041#endif
1042
1043#if ADB_HOST
Nick Kralevich23b39f02013-02-15 10:22:08 -08001044
1045#ifdef WORKAROUND_BUG6558362
1046#include <sched.h>
1047#define AFFINITY_ENVVAR "ADB_CPU_AFFINITY_BUG6558362"
1048void adb_set_affinity(void)
1049{
1050 cpu_set_t cpu_set;
1051 const char* cpunum_str = getenv(AFFINITY_ENVVAR);
1052 char* strtol_res;
1053 int cpu_num;
1054
1055 if (!cpunum_str || !*cpunum_str)
1056 return;
1057 cpu_num = strtol(cpunum_str, &strtol_res, 0);
1058 if (*strtol_res != '\0')
1059 fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, AFFINITY_ENVVAR);
1060
1061 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
1062 D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
1063 CPU_ZERO(&cpu_set);
1064 CPU_SET(cpu_num, &cpu_set);
1065 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
1066 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
1067 D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
1068}
1069#endif
1070
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001071int launch_server(int server_port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001072{
1073#ifdef HAVE_WIN32_PROC
1074 /* we need to start the server in the background */
1075 /* we create a PIPE that will be used to wait for the server's "OK" */
1076 /* message since the pipe handles must be inheritable, we use a */
1077 /* security attribute */
1078 HANDLE pipe_read, pipe_write;
Ray Donnellyc0324702012-11-29 01:18:50 +00001079 HANDLE stdout_handle, stderr_handle;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001080 SECURITY_ATTRIBUTES sa;
1081 STARTUPINFO startup;
1082 PROCESS_INFORMATION pinfo;
1083 char program_path[ MAX_PATH ];
1084 int ret;
1085
1086 sa.nLength = sizeof(sa);
1087 sa.lpSecurityDescriptor = NULL;
1088 sa.bInheritHandle = TRUE;
1089
1090 /* create pipe, and ensure its read handle isn't inheritable */
1091 ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
1092 if (!ret) {
1093 fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
1094 return -1;
1095 }
1096
1097 SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
1098
Ray Donnellyc0324702012-11-29 01:18:50 +00001099 /* Some programs want to launch an adb command and collect its output by
1100 * calling CreateProcess with inheritable stdout/stderr handles, then
1101 * using read() to get its output. When this happens, the stdout/stderr
1102 * handles passed to the adb client process will also be inheritable.
1103 * When starting the adb server here, care must be taken to reset them
1104 * to non-inheritable.
1105 * Otherwise, something bad happens: even if the adb command completes,
1106 * the calling process is stuck while read()-ing from the stdout/stderr
1107 * descriptors, because they're connected to corresponding handles in the
1108 * adb server process (even if the latter never uses/writes to them).
1109 */
1110 stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
1111 stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
1112 if (stdout_handle != INVALID_HANDLE_VALUE) {
1113 SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
1114 }
1115 if (stderr_handle != INVALID_HANDLE_VALUE) {
1116 SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
1117 }
1118
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001119 ZeroMemory( &startup, sizeof(startup) );
1120 startup.cb = sizeof(startup);
1121 startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
1122 startup.hStdOutput = pipe_write;
1123 startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
1124 startup.dwFlags = STARTF_USESTDHANDLES;
1125
1126 ZeroMemory( &pinfo, sizeof(pinfo) );
1127
1128 /* get path of current program */
1129 GetModuleFileName( NULL, program_path, sizeof(program_path) );
1130
1131 ret = CreateProcess(
1132 program_path, /* program path */
1133 "adb fork-server server",
1134 /* the fork-server argument will set the
1135 debug = 2 in the child */
1136 NULL, /* process handle is not inheritable */
1137 NULL, /* thread handle is not inheritable */
1138 TRUE, /* yes, inherit some handles */
1139 DETACHED_PROCESS, /* the new process doesn't have a console */
1140 NULL, /* use parent's environment block */
1141 NULL, /* use parent's starting directory */
1142 &startup, /* startup info, i.e. std handles */
1143 &pinfo );
1144
1145 CloseHandle( pipe_write );
1146
1147 if (!ret) {
1148 fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
1149 CloseHandle( pipe_read );
1150 return -1;
1151 }
1152
1153 CloseHandle( pinfo.hProcess );
1154 CloseHandle( pinfo.hThread );
1155
1156 /* wait for the "OK\n" message */
1157 {
1158 char temp[3];
1159 DWORD count;
1160
1161 ret = ReadFile( pipe_read, temp, 3, &count, NULL );
1162 CloseHandle( pipe_read );
1163 if ( !ret ) {
1164 fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
1165 return -1;
1166 }
1167 if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1168 fprintf(stderr, "ADB server didn't ACK\n" );
1169 return -1;
1170 }
1171 }
1172#elif defined(HAVE_FORKEXEC)
1173 char path[PATH_MAX];
1174 int fd[2];
1175
1176 // set up a pipe so the child can tell us when it is ready.
1177 // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
1178 if (pipe(fd)) {
1179 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
1180 return -1;
1181 }
Alexey Tarasov857f17a2009-10-22 02:55:00 +11001182 get_my_path(path, PATH_MAX);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001183 pid_t pid = fork();
1184 if(pid < 0) return -1;
1185
1186 if (pid == 0) {
1187 // child side of the fork
1188
1189 // redirect stderr to the pipe
1190 // we use stderr instead of stdout due to stdout's buffering behavior.
1191 adb_close(fd[0]);
1192 dup2(fd[1], STDERR_FILENO);
1193 adb_close(fd[1]);
1194
Matt Gumbel411775c2012-11-14 10:16:17 -08001195 char str_port[30];
1196 snprintf(str_port, sizeof(str_port), "%d", server_port);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001197 // child process
Matt Gumbel411775c2012-11-14 10:16:17 -08001198 int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001199 // this should not return
1200 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
1201 } else {
1202 // parent side of the fork
1203
1204 char temp[3];
1205
1206 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
1207 // wait for the "OK\n" message
1208 adb_close(fd[1]);
1209 int ret = adb_read(fd[0], temp, 3);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001210 int saved_errno = errno;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001211 adb_close(fd[0]);
1212 if (ret < 0) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001213 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001214 return -1;
1215 }
1216 if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1217 fprintf(stderr, "ADB server didn't ACK\n" );
1218 return -1;
1219 }
1220
1221 setsid();
1222 }
1223#else
1224#error "cannot implement background server start on this platform"
1225#endif
1226 return 0;
1227}
1228#endif
1229
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001230/* Constructs a local name of form tcp:port.
1231 * target_str points to the target string, it's content will be overwritten.
1232 * target_size is the capacity of the target string.
1233 * server_port is the port number to use for the local name.
1234 */
1235void build_local_name(char* target_str, size_t target_size, int server_port)
1236{
1237 snprintf(target_str, target_size, "tcp:%d", server_port);
1238}
1239
Nick Kralevichcdcc02e2012-01-19 10:18:59 -08001240#if !ADB_HOST
Nick Kralevich62160b62013-02-15 14:39:15 -08001241
1242static void drop_capabilities_bounding_set_if_needed() {
1243#ifdef ALLOW_ADBD_ROOT
1244 char value[PROPERTY_VALUE_MAX];
1245 property_get("ro.debuggable", value, "");
1246 if (strcmp(value, "1") == 0) {
1247 return;
1248 }
1249#endif
1250 int i;
1251 for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
Nick Kralevichb7df0172013-04-18 12:20:02 -07001252 if (i == CAP_SETUID || i == CAP_SETGID) {
Nick Kralevich62160b62013-02-15 14:39:15 -08001253 // CAP_SETUID CAP_SETGID needed by /system/bin/run-as
1254 continue;
1255 }
1256 int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
1257
1258 // Some kernels don't have file capabilities compiled in, and
1259 // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically
1260 // die when we see such misconfigured kernels.
1261 if ((err < 0) && (errno != EINVAL)) {
1262 exit(1);
1263 }
1264 }
1265}
1266
Nick Kralevichcdcc02e2012-01-19 10:18:59 -08001267static int should_drop_privileges() {
Nick Kralevich09264032012-01-19 13:11:35 -08001268#ifndef ALLOW_ADBD_ROOT
1269 return 1;
1270#else /* ALLOW_ADBD_ROOT */
Nick Kralevichcdcc02e2012-01-19 10:18:59 -08001271 int secure = 0;
1272 char value[PROPERTY_VALUE_MAX];
1273
1274 /* run adbd in secure mode if ro.secure is set and
1275 ** we are not in the emulator
1276 */
1277 property_get("ro.kernel.qemu", value, "");
1278 if (strcmp(value, "1") != 0) {
1279 property_get("ro.secure", value, "1");
1280 if (strcmp(value, "1") == 0) {
1281 // don't run as root if ro.secure is set...
1282 secure = 1;
1283
1284 // ... except we allow running as root in userdebug builds if the
1285 // service.adb.root property has been set by the "adb root" command
1286 property_get("ro.debuggable", value, "");
1287 if (strcmp(value, "1") == 0) {
1288 property_get("service.adb.root", value, "");
1289 if (strcmp(value, "1") == 0) {
1290 secure = 0;
1291 }
1292 }
1293 }
1294 }
1295 return secure;
Nick Kralevich09264032012-01-19 13:11:35 -08001296#endif /* ALLOW_ADBD_ROOT */
Nick Kralevichcdcc02e2012-01-19 10:18:59 -08001297}
1298#endif /* !ADB_HOST */
1299
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001300int adb_main(int is_daemon, int server_port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001301{
1302#if !ADB_HOST
Mike Lockwoodb51ae572009-08-24 15:58:40 -07001303 int port;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001304 char value[PROPERTY_VALUE_MAX];
Nick Kralevichb024cab2012-04-02 13:00:35 -07001305
1306 umask(000);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001307#endif
1308
1309 atexit(adb_cleanup);
1310#ifdef HAVE_WIN32_PROC
1311 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
1312#elif defined(HAVE_FORKEXEC)
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001313 // No SIGCHLD. Let the service subproc handle its children.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001314 signal(SIGPIPE, SIG_IGN);
1315#endif
1316
1317 init_transport_registration();
1318
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001319#if ADB_HOST
1320 HOST = 1;
Nick Kralevich23b39f02013-02-15 10:22:08 -08001321
1322#ifdef WORKAROUND_BUG6558362
1323 if(is_daemon) adb_set_affinity();
1324#endif
Xavier Ducrohet2ef5fc22009-05-20 17:33:53 -07001325 usb_vendors_init();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001326 usb_init();
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001327 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Benoit Goby2cc19e42012-04-12 12:23:49 -07001328 adb_auth_init();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001329
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001330 char local_name[30];
1331 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner6c489802012-11-14 15:01:55 +01001332 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001333 exit(1);
1334 }
1335#else
Benoit Goby2cc19e42012-04-12 12:23:49 -07001336 property_get("ro.adb.secure", value, "0");
1337 auth_enabled = !strcmp(value, "1");
1338 if (auth_enabled)
1339 adb_auth_init();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001340
Jeff Sharkey41f55bb2012-09-06 13:05:40 -07001341 // Our external storage path may be different than apps, since
1342 // we aren't able to bind mount after dropping root.
1343 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
1344 if (NULL != adb_external_storage) {
1345 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
1346 } else {
1347 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
1348 " unchanged.\n");
1349 }
1350
Nick Kralevich4638dea2014-06-18 11:24:27 -07001351 /* add extra groups:
1352 ** AID_ADB to access the USB driver
1353 ** AID_LOG to read system logs (adb logcat)
1354 ** AID_INPUT to diagnose input issues (getevent)
1355 ** AID_INET to diagnose network issues (netcfg, ping)
1356 ** AID_GRAPHICS to access the frame buffer
1357 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
1358 ** AID_SDCARD_R to allow reading from the SD card
1359 ** AID_SDCARD_RW to allow writing to the SD card
1360 ** AID_NET_BW_STATS to read out qtaguid statistics
1361 */
1362 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
1363 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
1364 AID_NET_BW_STATS };
1365 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
1366 exit(1);
1367 }
1368
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001369 /* don't listen on a port (default 5037) if running in secure mode */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001370 /* don't run as root if we are running in secure mode */
Nick Kralevichcdcc02e2012-01-19 10:18:59 -08001371 if (should_drop_privileges()) {
Nick Kralevich62160b62013-02-15 14:39:15 -08001372 drop_capabilities_bounding_set_if_needed();
1373
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001374 /* then switch user and group to "shell" */
Nick Kralevich4ffc2eb2010-08-27 14:35:07 -07001375 if (setgid(AID_SHELL) != 0) {
1376 exit(1);
1377 }
1378 if (setuid(AID_SHELL) != 0) {
1379 exit(1);
1380 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001381
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001382 D("Local port disabled\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001383 } else {
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001384 char local_name[30];
Nick Kralevich0d9c0862014-01-18 09:25:04 -08001385 if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
1386 // b/12587913: fix setcon to allow const pointers
1387 if (setcon((char *)root_seclabel) < 0) {
1388 exit(1);
1389 }
1390 }
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001391 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner6c489802012-11-14 15:01:55 +01001392 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001393 exit(1);
1394 }
1395 }
1396
Mike J. Chen9acc2ee2012-07-20 18:16:21 -07001397 int usb = 0;
1398 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
Mike Lockwooda8b38752009-08-26 12:50:22 -07001399 // listen on USB
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001400 usb_init();
Mike J. Chen9acc2ee2012-07-20 18:16:21 -07001401 usb = 1;
1402 }
1403
1404 // If one of these properties is set, also listen on that port
1405 // If one of the properties isn't set and we couldn't listen on usb,
1406 // listen on the default port.
1407 property_get("service.adb.tcp.port", value, "");
1408 if (!value[0]) {
1409 property_get("persist.adb.tcp.port", value, "");
1410 }
1411 if (sscanf(value, "%d", &port) == 1 && port > 0) {
1412 printf("using port=%d\n", port);
1413 // listen on TCP port specified by service.adb.tcp.port property
1414 local_init(port);
1415 } else if (!usb) {
Mike Lockwooda8b38752009-08-26 12:50:22 -07001416 // listen on default port
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001417 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001418 }
Mike J. Chen9acc2ee2012-07-20 18:16:21 -07001419
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001420 D("adb_main(): pre init_jdwp()\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001421 init_jdwp();
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001422 D("adb_main(): post init_jdwp()\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001423#endif
1424
1425 if (is_daemon)
1426 {
1427 // inform our parent that we are up and running.
1428#ifdef HAVE_WIN32_PROC
1429 DWORD count;
1430 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
1431#elif defined(HAVE_FORKEXEC)
1432 fprintf(stderr, "OK\n");
1433#endif
1434 start_logging();
1435 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001436 D("Event loop starting\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001437
1438 fdevent_loop();
1439
1440 usb_cleanup();
1441
1442 return 0;
1443}
1444
David 'Digit' Turner963a4492013-03-21 21:07:42 +01001445// Try to handle a network forwarding request.
1446// This returns 1 on success, 0 on failure, and -1 to indicate this is not
1447// a forwarding-related request.
1448int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd)
1449{
1450 if (!strcmp(service, "list-forward")) {
1451 // Create the list of forward redirections.
1452 int buffer_size = format_listeners(NULL, 0);
1453 // Add one byte for the trailing zero.
1454 char* buffer = malloc(buffer_size + 1);
1455 if (buffer == NULL) {
1456 sendfailmsg(reply_fd, "not enough memory");
1457 return 1;
1458 }
1459 (void) format_listeners(buffer, buffer_size + 1);
1460#if ADB_HOST
1461 send_msg_with_okay(reply_fd, buffer, buffer_size);
1462#else
1463 send_msg_with_header(reply_fd, buffer, buffer_size);
1464#endif
1465 free(buffer);
1466 return 1;
1467 }
1468
1469 if (!strcmp(service, "killforward-all")) {
1470 remove_all_listeners();
1471#if ADB_HOST
1472 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
1473 adb_write(reply_fd, "OKAY", 4);
1474#endif
1475 adb_write(reply_fd, "OKAY", 4);
1476 return 1;
1477 }
1478
1479 if (!strncmp(service, "forward:",8) ||
1480 !strncmp(service, "killforward:",12)) {
1481 char *local, *remote, *err;
1482 int r;
1483 atransport *transport;
1484
1485 int createForward = strncmp(service, "kill", 4);
1486 int no_rebind = 0;
1487
1488 local = strchr(service, ':') + 1;
1489
1490 // Handle forward:norebind:<local>... here
1491 if (createForward && !strncmp(local, "norebind:", 9)) {
1492 no_rebind = 1;
1493 local = strchr(local, ':') + 1;
1494 }
1495
1496 remote = strchr(local,';');
1497
1498 if (createForward) {
1499 // Check forward: parameter format: '<local>;<remote>'
1500 if(remote == 0) {
1501 sendfailmsg(reply_fd, "malformed forward spec");
1502 return 1;
1503 }
1504
1505 *remote++ = 0;
1506 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')) {
1507 sendfailmsg(reply_fd, "malformed forward spec");
1508 return 1;
1509 }
1510 } else {
1511 // Check killforward: parameter format: '<local>'
1512 if (local[0] == 0) {
1513 sendfailmsg(reply_fd, "malformed forward spec");
1514 return 1;
1515 }
1516 }
1517
1518 transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
1519 if (!transport) {
1520 sendfailmsg(reply_fd, err);
1521 return 1;
1522 }
1523
1524 if (createForward) {
1525 r = install_listener(local, remote, transport, no_rebind);
1526 } else {
1527 r = remove_listener(local, transport);
1528 }
1529 if(r == 0) {
1530#if ADB_HOST
1531 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
1532 writex(reply_fd, "OKAY", 4);
1533#endif
1534 writex(reply_fd, "OKAY", 4);
1535 return 1;
1536 }
1537
1538 if (createForward) {
1539 const char* message;
1540 switch (r) {
1541 case INSTALL_STATUS_CANNOT_BIND:
1542 message = "cannot bind to socket";
1543 break;
1544 case INSTALL_STATUS_CANNOT_REBIND:
1545 message = "cannot rebind existing socket";
1546 break;
1547 default:
1548 message = "internal error";
1549 }
1550 sendfailmsg(reply_fd, message);
1551 } else {
1552 sendfailmsg(reply_fd, "cannot remove listener");
1553 }
1554 return 1;
1555 }
1556 return 0;
1557}
1558
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001559int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
1560{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001561 if(!strcmp(service, "kill")) {
1562 fprintf(stderr,"adb server killed by remote request\n");
1563 fflush(stdout);
1564 adb_write(reply_fd, "OKAY", 4);
1565 usb_cleanup();
1566 exit(0);
1567 }
1568
1569#if ADB_HOST
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -07001570 atransport *transport = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001571 // "transport:" is used for switching transport with a specified serial number
1572 // "transport-usb:" is used for switching transport to the only USB transport
1573 // "transport-local:" is used for switching transport to the only local transport
1574 // "transport-any:" is used for switching transport to the only transport
1575 if (!strncmp(service, "transport", strlen("transport"))) {
1576 char* error_string = "unknown failure";
1577 transport_type type = kTransportAny;
1578
1579 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
1580 type = kTransportUsb;
1581 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
1582 type = kTransportLocal;
1583 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
1584 type = kTransportAny;
1585 } else if (!strncmp(service, "transport:", strlen("transport:"))) {
1586 service += strlen("transport:");
Tom Marlin851ee3a2011-07-27 12:56:14 -05001587 serial = service;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001588 }
1589
1590 transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
1591
1592 if (transport) {
1593 s->transport = transport;
1594 adb_write(reply_fd, "OKAY", 4);
1595 } else {
1596 sendfailmsg(reply_fd, error_string);
1597 }
1598 return 1;
1599 }
1600
1601 // return a list of all connected devices
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001602 if (!strncmp(service, "devices", 7)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001603 char buffer[4096];
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001604 int use_long = !strcmp(service+7, "-l");
1605 if (use_long || service[7] == 0) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001606 memset(buffer, 0, sizeof(buffer));
1607 D("Getting device list \n");
1608 list_transports(buffer, sizeof(buffer), use_long);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001609 D("Wrote device list \n");
Snild Dolkow4516a872014-01-30 10:08:38 +01001610 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001611 return 0;
1612 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001613 }
1614
Mike Lockwood5f2c4a12009-10-11 23:04:18 -04001615 // remove TCP transport
1616 if (!strncmp(service, "disconnect:", 11)) {
1617 char buffer[4096];
1618 memset(buffer, 0, sizeof(buffer));
1619 char* serial = service + 11;
Mike Lockwood01c2c302010-05-24 10:44:35 -04001620 if (serial[0] == 0) {
1621 // disconnect from all TCP devices
1622 unregister_all_tcp_transports();
Mike Lockwood5f2c4a12009-10-11 23:04:18 -04001623 } else {
Mike Lockwood01c2c302010-05-24 10:44:35 -04001624 char hostbuf[100];
1625 // assume port 5555 if no port is specified
1626 if (!strchr(serial, ':')) {
1627 snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
1628 serial = hostbuf;
1629 }
1630 atransport *t = find_transport(serial);
1631
1632 if (t) {
1633 unregister_transport(t);
1634 } else {
1635 snprintf(buffer, sizeof(buffer), "No such device %s", serial);
1636 }
Mike Lockwood5f2c4a12009-10-11 23:04:18 -04001637 }
1638
Snild Dolkow4516a872014-01-30 10:08:38 +01001639 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Mike Lockwoodb51ae572009-08-24 15:58:40 -07001640 return 0;
1641 }
1642
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001643 // returns our value for ADB_SERVER_VERSION
1644 if (!strcmp(service, "version")) {
1645 char version[12];
1646 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
Snild Dolkow4516a872014-01-30 10:08:38 +01001647 send_msg_with_okay(reply_fd, version, strlen(version));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001648 return 0;
1649 }
1650
1651 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
1652 char *out = "unknown";
1653 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1654 if (transport && transport->serial) {
1655 out = transport->serial;
1656 }
Snild Dolkow4516a872014-01-30 10:08:38 +01001657 send_msg_with_okay(reply_fd, out, strlen(out));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001658 return 0;
1659 }
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001660 if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
1661 char *out = "unknown";
1662 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1663 if (transport && transport->devpath) {
1664 out = transport->devpath;
1665 }
Snild Dolkow4516a872014-01-30 10:08:38 +01001666 send_msg_with_okay(reply_fd, out, strlen(out));
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001667 return 0;
1668 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001669 // indicates a new emulator instance has started
1670 if (!strncmp(service,"emulator:",9)) {
1671 int port = atoi(service+9);
1672 local_connect(port);
1673 /* we don't even need to send a reply */
1674 return 0;
1675 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001676
1677 if(!strncmp(service,"get-state",strlen("get-state"))) {
1678 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1679 char *state = connection_state_name(transport);
Snild Dolkow4516a872014-01-30 10:08:38 +01001680 send_msg_with_okay(reply_fd, state, strlen(state));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001681 return 0;
1682 }
Simon Yee51c11e2014-07-14 17:23:06 -07001683#endif // ADB_HOST
1684
1685 int ret = handle_forward_request(service, ttype, serial, reply_fd);
1686 if (ret >= 0)
1687 return ret - 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001688 return -1;
1689}
1690
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001691int main(int argc, char **argv)
1692{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001693#if ADB_HOST
1694 adb_sysdeps_init();
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001695 adb_trace_init();
1696 D("Handling commandline()\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001697 return adb_commandline(argc - 1, argv + 1);
1698#else
Vladimir Chtchetkine32a15712012-02-27 10:41:53 -08001699 /* If adbd runs inside the emulator this will enable adb tracing via
1700 * adb-debug qemud service in the emulator. */
1701 adb_qemu_trace_init();
Nick Kralevich0d9c0862014-01-18 09:25:04 -08001702 while(1) {
1703 int c;
1704 int option_index = 0;
1705 static struct option opts[] = {
1706 {"root_seclabel", required_argument, 0, 's' },
1707 {"device_banner", required_argument, 0, 'b' }
1708 };
1709 c = getopt_long(argc, argv, "", opts, &option_index);
1710 if (c == -1)
1711 break;
1712 switch (c) {
1713 case 's':
1714 root_seclabel = optarg;
1715 break;
1716 case 'b':
1717 adb_device_banner = optarg;
1718 break;
1719 default:
1720 break;
1721 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001722 }
Mike Lockwood4ac1e282009-05-25 18:17:55 -04001723
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001724 start_device_log();
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001725 D("Handling main()\n");
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +01001726 return adb_main(0, DEFAULT_ADB_PORT);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001727#endif
1728}