blob: e297c961991b8731e086507f79db837d9bff007a [file] [log] [blame]
The Android Open Source Projectdd7bc332009-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 Andersonc7993af2012-05-25 13:55:46 -070024#include <stddef.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <string.h>
26#include <time.h>
Mike Lockwood1f546e62009-05-25 18:17:55 -040027#include <sys/time.h>
Ray Donnellycbb98912012-11-29 01:36:08 +000028#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30#include "sysdeps.h"
31#include "adb.h"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070032#include "adb_auth.h"
Dan Albert21c3eaf2015-02-18 17:20:44 -080033#include "qemu_tracing.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
Scott Andersone82c2db2012-05-25 14:10:02 -070035#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
36
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#if !ADB_HOST
Nick Kralevich893a4a42013-05-23 09:54:13 -070038#include <cutils/properties.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039#include <private/android_filesystem_config.h>
Nick Kraleviche2864bf2013-02-28 14:12:58 -080040#include <sys/capability.h>
Jeff Sharkey885342a2012-08-14 21:00:22 -070041#include <sys/mount.h>
Elliott Hughesb4dd6ef2014-07-18 16:44:58 -070042#include <sys/prctl.h>
Nick Kralevichd49aa252014-01-18 09:25:04 -080043#include <getopt.h>
44#include <selinux/selinux.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045#endif
46
JP Abgrall408fa572011-03-16 15:57:42 -070047#if ADB_TRACE
48ADB_MUTEX_DEFINE( D_lock );
49#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
51int HOST = 0;
Matt Gumbeld7b33082012-11-14 10:16:17 -080052int gListenAll = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070054static int auth_enabled = 0;
55
Scott Andersone82c2db2012-05-25 14:10:02 -070056#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057static const char *adb_device_banner = "device";
Nick Kralevichd49aa252014-01-18 09:25:04 -080058static const char *root_seclabel = NULL;
Scott Andersone82c2db2012-05-25 14:10:02 -070059#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
61void fatal(const char *fmt, ...)
62{
63 va_list ap;
64 va_start(ap, fmt);
65 fprintf(stderr, "error: ");
66 vfprintf(stderr, fmt, ap);
67 fprintf(stderr, "\n");
68 va_end(ap);
69 exit(-1);
70}
71
72void fatal_errno(const char *fmt, ...)
73{
74 va_list ap;
75 va_start(ap, fmt);
76 fprintf(stderr, "error: %s: ", strerror(errno));
77 vfprintf(stderr, fmt, ap);
78 fprintf(stderr, "\n");
79 va_end(ap);
80 exit(-1);
81}
82
83int adb_trace_mask;
84
85/* read a comma/space/colum/semi-column separated list of tags
86 * from the ADB_TRACE environment variable and build the trace
87 * mask from it. note that '1' and 'all' are special cases to
88 * enable all tracing
89 */
90void adb_trace_init(void)
91{
92 const char* p = getenv("ADB_TRACE");
93 const char* q;
94
95 static const struct {
96 const char* tag;
97 int flag;
98 } tags[] = {
99 { "1", 0 },
100 { "all", 0 },
101 { "adb", TRACE_ADB },
102 { "sockets", TRACE_SOCKETS },
103 { "packets", TRACE_PACKETS },
104 { "rwx", TRACE_RWX },
105 { "usb", TRACE_USB },
106 { "sync", TRACE_SYNC },
107 { "sysdeps", TRACE_SYSDEPS },
108 { "transport", TRACE_TRANSPORT },
109 { "jdwp", TRACE_JDWP },
JP Abgrall408fa572011-03-16 15:57:42 -0700110 { "services", TRACE_SERVICES },
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700111 { "auth", TRACE_AUTH },
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 { NULL, 0 }
113 };
114
115 if (p == NULL)
116 return;
117
118 /* use a comma/column/semi-colum/space separated list */
119 while (*p) {
120 int len, tagn;
121
122 q = strpbrk(p, " ,:;");
123 if (q == NULL) {
124 q = p + strlen(p);
125 }
126 len = q - p;
127
128 for (tagn = 0; tags[tagn].tag != NULL; tagn++)
129 {
130 int taglen = strlen(tags[tagn].tag);
131
132 if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
133 {
134 int flag = tags[tagn].flag;
135 if (flag == 0) {
136 adb_trace_mask = ~0;
137 return;
138 }
139 adb_trace_mask |= (1 << flag);
140 break;
141 }
142 }
143 p = q;
144 if (*p)
145 p++;
146 }
147}
148
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149apacket *get_apacket(void)
150{
151 apacket *p = malloc(sizeof(apacket));
152 if(p == 0) fatal("failed to allocate an apacket");
153 memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
154 return p;
155}
156
157void put_apacket(apacket *p)
158{
159 free(p);
160}
161
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700162void handle_online(atransport *t)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163{
164 D("adb: online\n");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700165 t->online = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166}
167
168void handle_offline(atransport *t)
169{
170 D("adb: offline\n");
171 //Close the associated usb
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700172 t->online = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 run_transport_disconnects(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174}
175
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700176#if DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177#define DUMPMAX 32
178void print_packet(const char *label, apacket *p)
179{
180 char *tag;
181 char *x;
182 unsigned count;
183
184 switch(p->msg.command){
185 case A_SYNC: tag = "SYNC"; break;
186 case A_CNXN: tag = "CNXN" ; break;
187 case A_OPEN: tag = "OPEN"; break;
188 case A_OKAY: tag = "OKAY"; break;
189 case A_CLSE: tag = "CLSE"; break;
190 case A_WRTE: tag = "WRTE"; break;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700191 case A_AUTH: tag = "AUTH"; break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 default: tag = "????"; break;
193 }
194
195 fprintf(stderr, "%s: %s %08x %08x %04x \"",
196 label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
197 count = p->msg.data_length;
198 x = (char*) p->data;
199 if(count > DUMPMAX) {
200 count = DUMPMAX;
201 tag = "\n";
202 } else {
203 tag = "\"\n";
204 }
205 while(count-- > 0){
206 if((*x >= ' ') && (*x < 127)) {
207 fputc(*x, stderr);
208 } else {
209 fputc('.', stderr);
210 }
211 x++;
212 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700213 fputs(tag, stderr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214}
215#endif
216
217static void send_ready(unsigned local, unsigned remote, atransport *t)
218{
219 D("Calling send_ready \n");
220 apacket *p = get_apacket();
221 p->msg.command = A_OKAY;
222 p->msg.arg0 = local;
223 p->msg.arg1 = remote;
224 send_packet(p, t);
225}
226
227static void send_close(unsigned local, unsigned remote, atransport *t)
228{
229 D("Calling send_close \n");
230 apacket *p = get_apacket();
231 p->msg.command = A_CLSE;
232 p->msg.arg0 = local;
233 p->msg.arg1 = remote;
234 send_packet(p, t);
235}
236
Scott Andersone82c2db2012-05-25 14:10:02 -0700237static size_t fill_connect_data(char *buf, size_t bufsize)
238{
239#if ADB_HOST
240 return snprintf(buf, bufsize, "host::") + 1;
241#else
242 static const char *cnxn_props[] = {
243 "ro.product.name",
244 "ro.product.model",
245 "ro.product.device",
246 };
247 static const int num_cnxn_props = ARRAY_SIZE(cnxn_props);
248 int i;
249 size_t remaining = bufsize;
250 size_t len;
251
252 len = snprintf(buf, remaining, "%s::", adb_device_banner);
253 remaining -= len;
254 buf += len;
255 for (i = 0; i < num_cnxn_props; i++) {
256 char value[PROPERTY_VALUE_MAX];
257 property_get(cnxn_props[i], value, "");
258 len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value);
259 remaining -= len;
260 buf += len;
261 }
262
263 return bufsize - remaining + 1;
264#endif
265}
266
David 'Digit' Turner25258692013-03-21 21:07:42 +0100267#if !ADB_HOST
268static void send_msg_with_header(int fd, const char* msg, size_t msglen) {
269 char header[5];
270 if (msglen > 0xffff)
271 msglen = 0xffff;
272 snprintf(header, sizeof(header), "%04x", (unsigned)msglen);
273 writex(fd, header, 4);
274 writex(fd, msg, msglen);
275}
276#endif
277
Chih-Hung Hsiehf787b382014-09-05 15:38:15 -0700278#if ADB_HOST
David 'Digit' Turner25258692013-03-21 21:07:42 +0100279static void send_msg_with_okay(int fd, const char* msg, size_t msglen) {
Snild Dolkow2264e7c2014-01-30 10:08:38 +0100280 char header[9];
281 if (msglen > 0xffff)
282 msglen = 0xffff;
283 snprintf(header, sizeof(header), "OKAY%04x", (unsigned)msglen);
284 writex(fd, header, 8);
285 writex(fd, msg, msglen);
286}
Chih-Hung Hsiehf787b382014-09-05 15:38:15 -0700287#endif // ADB_HOST
Snild Dolkow2264e7c2014-01-30 10:08:38 +0100288
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289static void send_connect(atransport *t)
290{
291 D("Calling send_connect \n");
292 apacket *cp = get_apacket();
293 cp->msg.command = A_CNXN;
294 cp->msg.arg0 = A_VERSION;
295 cp->msg.arg1 = MAX_PAYLOAD;
Scott Andersone82c2db2012-05-25 14:10:02 -0700296 cp->msg.data_length = fill_connect_data((char *)cp->data,
297 sizeof(cp->data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 send_packet(cp, t);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700299}
300
Benoit Goby045a4a92013-01-15 19:59:14 -0800301void send_auth_request(atransport *t)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700302{
303 D("Calling send_auth_request\n");
304 apacket *p;
305 int ret;
306
307 ret = adb_auth_generate_token(t->token, sizeof(t->token));
308 if (ret != sizeof(t->token)) {
309 D("Error generating token ret=%d\n", ret);
310 return;
311 }
312
313 p = get_apacket();
314 memcpy(p->data, t->token, ret);
315 p->msg.command = A_AUTH;
316 p->msg.arg0 = ADB_AUTH_TOKEN;
317 p->msg.data_length = ret;
318 send_packet(p, t);
319}
320
321static void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
322{
323 D("Calling send_auth_response\n");
324 apacket *p = get_apacket();
325 int ret;
326
327 ret = adb_auth_sign(t->key, token, token_size, p->data);
328 if (!ret) {
329 D("Error signing the token\n");
330 put_apacket(p);
331 return;
332 }
333
334 p->msg.command = A_AUTH;
335 p->msg.arg0 = ADB_AUTH_SIGNATURE;
336 p->msg.data_length = ret;
337 send_packet(p, t);
338}
339
340static void send_auth_publickey(atransport *t)
341{
342 D("Calling send_auth_publickey\n");
343 apacket *p = get_apacket();
344 int ret;
345
346 ret = adb_auth_get_userkey(p->data, sizeof(p->data));
347 if (!ret) {
348 D("Failed to get user public key\n");
349 put_apacket(p);
350 return;
351 }
352
353 p->msg.command = A_AUTH;
354 p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
355 p->msg.data_length = ret;
356 send_packet(p, t);
357}
358
359void adb_auth_verified(atransport *t)
360{
361 handle_online(t);
362 send_connect(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363}
364
Chih-Hung Hsiehf787b382014-09-05 15:38:15 -0700365#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366static char *connection_state_name(atransport *t)
367{
368 if (t == NULL) {
369 return "unknown";
370 }
371
372 switch(t->connection_state) {
373 case CS_BOOTLOADER:
374 return "bootloader";
375 case CS_DEVICE:
376 return "device";
trevda5ad5392013-04-17 14:34:23 +0100377 case CS_RECOVERY:
378 return "recovery";
379 case CS_SIDELOAD:
380 return "sideload";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 case CS_OFFLINE:
382 return "offline";
Benoit Goby77e8e582013-01-15 12:36:47 -0800383 case CS_UNAUTHORIZED:
384 return "unauthorized";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 default:
386 return "unknown";
387 }
388}
Chih-Hung Hsiehf787b382014-09-05 15:38:15 -0700389#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390
Scott Andersone82c2db2012-05-25 14:10:02 -0700391/* qual_overwrite is used to overwrite a qualifier string. dst is a
392 * pointer to a char pointer. It is assumed that if *dst is non-NULL, it
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700393 * was malloc'ed and needs to freed. *dst will be set to a dup of src.
Scott Andersone82c2db2012-05-25 14:10:02 -0700394 */
395static void qual_overwrite(char **dst, const char *src)
396{
397 if (!dst)
398 return;
399
400 free(*dst);
401 *dst = NULL;
402
403 if (!src || !*src)
404 return;
405
406 *dst = strdup(src);
407}
408
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409void parse_banner(char *banner, atransport *t)
410{
Scott Andersone82c2db2012-05-25 14:10:02 -0700411 static const char *prop_seps = ";";
412 static const char key_val_sep = '=';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700413 char *cp;
414 char *type;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415
416 D("parse_banner: %s\n", banner);
417 type = banner;
Scott Andersone82c2db2012-05-25 14:10:02 -0700418 cp = strchr(type, ':');
419 if (cp) {
420 *cp++ = 0;
421 /* Nothing is done with second field. */
422 cp = strchr(cp, ':');
423 if (cp) {
424 char *save;
425 char *key;
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700426 key = adb_strtok_r(cp + 1, prop_seps, &save);
Scott Andersone82c2db2012-05-25 14:10:02 -0700427 while (key) {
428 cp = strchr(key, key_val_sep);
429 if (cp) {
430 *cp++ = '\0';
431 if (!strcmp(key, "ro.product.name"))
432 qual_overwrite(&t->product, cp);
433 else if (!strcmp(key, "ro.product.model"))
434 qual_overwrite(&t->model, cp);
435 else if (!strcmp(key, "ro.product.device"))
436 qual_overwrite(&t->device, cp);
437 }
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700438 key = adb_strtok_r(NULL, prop_seps, &save);
Scott Andersone82c2db2012-05-25 14:10:02 -0700439 }
440 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 }
442
443 if(!strcmp(type, "bootloader")){
444 D("setting connection_state to CS_BOOTLOADER\n");
445 t->connection_state = CS_BOOTLOADER;
446 update_transports();
447 return;
448 }
449
450 if(!strcmp(type, "device")) {
451 D("setting connection_state to CS_DEVICE\n");
452 t->connection_state = CS_DEVICE;
453 update_transports();
454 return;
455 }
456
457 if(!strcmp(type, "recovery")) {
458 D("setting connection_state to CS_RECOVERY\n");
459 t->connection_state = CS_RECOVERY;
460 update_transports();
461 return;
462 }
463
Doug Zongker447f0612012-01-09 14:54:53 -0800464 if(!strcmp(type, "sideload")) {
465 D("setting connection_state to CS_SIDELOAD\n");
466 t->connection_state = CS_SIDELOAD;
467 update_transports();
468 return;
469 }
470
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 t->connection_state = CS_HOST;
472}
473
474void handle_packet(apacket *p, atransport *t)
475{
476 asocket *s;
477
Viral Mehta899913f2010-06-16 18:41:28 +0530478 D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
479 ((char*) (&(p->msg.command)))[1],
480 ((char*) (&(p->msg.command)))[2],
481 ((char*) (&(p->msg.command)))[3]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 print_packet("recv", p);
483
484 switch(p->msg.command){
485 case A_SYNC:
486 if(p->msg.arg0){
487 send_packet(p, t);
488 if(HOST) send_connect(t);
489 } else {
490 t->connection_state = CS_OFFLINE;
491 handle_offline(t);
492 send_packet(p, t);
493 }
494 return;
495
496 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
497 /* XXX verify version, etc */
498 if(t->connection_state != CS_OFFLINE) {
499 t->connection_state = CS_OFFLINE;
500 handle_offline(t);
501 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700502
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 parse_banner((char*) p->data, t);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700504
505 if (HOST || !auth_enabled) {
506 handle_online(t);
507 if(!HOST) send_connect(t);
508 } else {
509 send_auth_request(t);
510 }
511 break;
512
513 case A_AUTH:
514 if (p->msg.arg0 == ADB_AUTH_TOKEN) {
Benoit Goby77e8e582013-01-15 12:36:47 -0800515 t->connection_state = CS_UNAUTHORIZED;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700516 t->key = adb_auth_nextkey(t->key);
517 if (t->key) {
518 send_auth_response(p->data, p->msg.data_length, t);
519 } else {
520 /* No more private keys to try, send the public key */
521 send_auth_publickey(t);
522 }
523 } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
524 if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
525 adb_auth_verified(t);
526 t->failed_auth_attempts = 0;
527 } else {
528 if (t->failed_auth_attempts++ > 10)
529 adb_sleep_ms(1000);
530 send_auth_request(t);
531 }
532 } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
533 adb_auth_confirm_key(p->data, p->msg.data_length, t);
534 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 break;
536
537 case A_OPEN: /* OPEN(local-id, 0, "destination") */
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100538 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 char *name = (char*) p->data;
540 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
541 s = create_local_service_socket(name);
542 if(s == 0) {
543 send_close(0, p->msg.arg0, t);
544 } else {
545 s->peer = create_remote_socket(p->msg.arg0, t);
546 s->peer->peer = s;
547 send_ready(s->id, s->peer->id, t);
548 s->ready(s);
549 }
550 }
551 break;
552
553 case A_OKAY: /* READY(local-id, remote-id, "") */
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100554 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
555 if((s = find_local_socket(p->msg.arg1, 0))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 if(s->peer == 0) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100557 /* On first READY message, create the connection. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 s->peer = create_remote_socket(p->msg.arg0, t);
559 s->peer->peer = s;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100560 s->ready(s);
561 } else if (s->peer->id == p->msg.arg0) {
562 /* Other READY messages must use the same local-id */
563 s->ready(s);
564 } else {
565 D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s\n",
566 p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800567 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568 }
569 }
570 break;
571
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100572 case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */
573 if (t->online && p->msg.arg1 != 0) {
574 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
575 /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
576 * a failed OPEN only. However, due to a bug in previous ADB
577 * versions, CLOSE(0, remote-id, "") was also used for normal
578 * CLOSE() operations.
579 *
580 * This is bad because it means a compromised adbd could
581 * send packets to close connections between the host and
582 * other devices. To avoid this, only allow this if the local
583 * socket has a peer on the same transport.
584 */
585 if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
586 D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s\n",
587 p->msg.arg1, t->serial, s->peer->transport->serial);
588 } else {
589 s->close(s);
590 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800591 }
592 }
593 break;
594
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100595 case A_WRTE: /* WRITE(local-id, remote-id, <data>) */
596 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
597 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598 unsigned rid = p->msg.arg0;
599 p->len = p->msg.data_length;
600
601 if(s->enqueue(s, p) == 0) {
602 D("Enqueue the socket\n");
603 send_ready(s->id, rid, t);
604 }
605 return;
606 }
607 }
608 break;
609
610 default:
611 printf("handle_packet: what is %08x?!\n", p->msg.command);
612 }
613
614 put_apacket(p);
615}
616
617alistener listener_list = {
618 .next = &listener_list,
619 .prev = &listener_list,
620};
621
622static void ss_listener_event_func(int _fd, unsigned ev, void *_l)
623{
624 asocket *s;
625
626 if(ev & FDE_READ) {
627 struct sockaddr addr;
628 socklen_t alen;
629 int fd;
630
631 alen = sizeof(addr);
632 fd = adb_socket_accept(_fd, &addr, &alen);
633 if(fd < 0) return;
634
635 adb_socket_setbufsize(fd, CHUNK_SIZE);
636
637 s = create_local_socket(fd);
638 if(s) {
639 connect_to_smartsocket(s);
640 return;
641 }
642
643 adb_close(fd);
644 }
645}
646
647static void listener_event_func(int _fd, unsigned ev, void *_l)
648{
649 alistener *l = _l;
650 asocket *s;
651
652 if(ev & FDE_READ) {
653 struct sockaddr addr;
654 socklen_t alen;
655 int fd;
656
657 alen = sizeof(addr);
658 fd = adb_socket_accept(_fd, &addr, &alen);
659 if(fd < 0) return;
660
661 s = create_local_socket(fd);
662 if(s) {
663 s->transport = l->transport;
664 connect_to_remote(s, l->connect_to);
665 return;
666 }
667
668 adb_close(fd);
669 }
670}
671
672static void free_listener(alistener* l)
673{
674 if (l->next) {
675 l->next->prev = l->prev;
676 l->prev->next = l->next;
677 l->next = l->prev = l;
678 }
679
680 // closes the corresponding fd
681 fdevent_remove(&l->fde);
682
683 if (l->local_name)
684 free((char*)l->local_name);
685
686 if (l->connect_to)
687 free((char*)l->connect_to);
688
689 if (l->transport) {
690 remove_transport_disconnect(l->transport, &l->disconnect);
691 }
692 free(l);
693}
694
695static void listener_disconnect(void* _l, atransport* t)
696{
697 alistener* l = _l;
698
699 free_listener(l);
700}
701
702int local_name_to_fd(const char *name)
703{
704 int port;
705
706 if(!strncmp("tcp:", name, 4)){
707 int ret;
708 port = atoi(name + 4);
Matt Gumbeld7b33082012-11-14 10:16:17 -0800709
710 if (gListenAll > 0) {
711 ret = socket_inaddr_any_server(port, SOCK_STREAM);
712 } else {
713 ret = socket_loopback_server(port, SOCK_STREAM);
714 }
715
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716 return ret;
717 }
718#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
719 // It's non-sensical to support the "reserved" space on the adb host side
720 if(!strncmp(name, "local:", 6)) {
721 return socket_local_server(name + 6,
722 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
723 } else if(!strncmp(name, "localabstract:", 14)) {
724 return socket_local_server(name + 14,
725 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
726 } else if(!strncmp(name, "localfilesystem:", 16)) {
727 return socket_local_server(name + 16,
728 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
729 }
730
731#endif
732 printf("unknown local portname '%s'\n", name);
733 return -1;
734}
735
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100736// Write a single line describing a listener to a user-provided buffer.
737// Appends a trailing zero, even in case of truncation, but the function
738// returns the full line length.
739// If |buffer| is NULL, does not write but returns required size.
740static int format_listener(alistener* l, char* buffer, size_t buffer_len) {
741 // Format is simply:
742 //
743 // <device-serial> " " <local-name> " " <remote-name> "\n"
744 //
745 int local_len = strlen(l->local_name);
746 int connect_len = strlen(l->connect_to);
747 int serial_len = strlen(l->transport->serial);
748
749 if (buffer != NULL) {
750 snprintf(buffer, buffer_len, "%s %s %s\n",
751 l->transport->serial, l->local_name, l->connect_to);
752 }
753 // NOTE: snprintf() on Windows returns -1 in case of truncation, so
754 // return the computed line length instead.
755 return local_len + connect_len + serial_len + 3;
756}
757
758// Write the list of current listeners (network redirections) into a
759// user-provided buffer. Appends a trailing zero, even in case of
760// trunctaion, but return the full size in bytes.
761// If |buffer| is NULL, does not write but returns required size.
762static int format_listeners(char* buf, size_t buflen)
763{
764 alistener* l;
765 int result = 0;
766 for (l = listener_list.next; l != &listener_list; l = l->next) {
767 // Ignore special listeners like those for *smartsocket*
768 if (l->connect_to[0] == '*')
769 continue;
770 int len = format_listener(l, buf, buflen);
771 // Ensure there is space for the trailing zero.
772 result += len;
773 if (buf != NULL) {
774 buf += len;
775 buflen -= len;
776 if (buflen <= 0)
777 break;
778 }
779 }
780 return result;
781}
782
783static int remove_listener(const char *local_name, atransport* transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800784{
785 alistener *l;
786
787 for (l = listener_list.next; l != &listener_list; l = l->next) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100788 if (!strcmp(local_name, l->local_name)) {
789 listener_disconnect(l, l->transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790 return 0;
791 }
792 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800793 return -1;
794}
795
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100796static void remove_all_listeners(void)
797{
798 alistener *l, *l_next;
799 for (l = listener_list.next; l != &listener_list; l = l_next) {
800 l_next = l->next;
801 // Never remove smart sockets.
802 if (l->connect_to[0] == '*')
803 continue;
804 listener_disconnect(l, l->transport);
805 }
806}
807
808// error/status codes for install_listener.
809typedef enum {
810 INSTALL_STATUS_OK = 0,
811 INSTALL_STATUS_INTERNAL_ERROR = -1,
812 INSTALL_STATUS_CANNOT_BIND = -2,
813 INSTALL_STATUS_CANNOT_REBIND = -3,
814} install_status_t;
815
816static install_status_t install_listener(const char *local_name,
817 const char *connect_to,
818 atransport* transport,
819 int no_rebind)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800820{
821 alistener *l;
822
823 //printf("install_listener('%s','%s')\n", local_name, connect_to);
824
825 for(l = listener_list.next; l != &listener_list; l = l->next){
826 if(strcmp(local_name, l->local_name) == 0) {
827 char *cto;
828
829 /* can't repurpose a smartsocket */
830 if(l->connect_to[0] == '*') {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100831 return INSTALL_STATUS_INTERNAL_ERROR;
832 }
833
834 /* can't repurpose a listener if 'no_rebind' is true */
835 if (no_rebind) {
836 return INSTALL_STATUS_CANNOT_REBIND;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800837 }
838
839 cto = strdup(connect_to);
840 if(cto == 0) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100841 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800842 }
843
844 //printf("rebinding '%s' to '%s'\n", local_name, connect_to);
845 free((void*) l->connect_to);
846 l->connect_to = cto;
847 if (l->transport != transport) {
848 remove_transport_disconnect(l->transport, &l->disconnect);
849 l->transport = transport;
850 add_transport_disconnect(l->transport, &l->disconnect);
851 }
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100852 return INSTALL_STATUS_OK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853 }
854 }
855
856 if((l = calloc(1, sizeof(alistener))) == 0) goto nomem;
857 if((l->local_name = strdup(local_name)) == 0) goto nomem;
858 if((l->connect_to = strdup(connect_to)) == 0) goto nomem;
859
860
861 l->fd = local_name_to_fd(local_name);
862 if(l->fd < 0) {
863 free((void*) l->local_name);
864 free((void*) l->connect_to);
865 free(l);
866 printf("cannot bind '%s'\n", local_name);
867 return -2;
868 }
869
870 close_on_exec(l->fd);
871 if(!strcmp(l->connect_to, "*smartsocket*")) {
872 fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);
873 } else {
874 fdevent_install(&l->fde, l->fd, listener_event_func, l);
875 }
876 fdevent_set(&l->fde, FDE_READ);
877
878 l->next = &listener_list;
879 l->prev = listener_list.prev;
880 l->next->prev = l;
881 l->prev->next = l;
882 l->transport = transport;
883
884 if (transport) {
885 l->disconnect.opaque = l;
886 l->disconnect.func = listener_disconnect;
887 add_transport_disconnect(transport, &l->disconnect);
888 }
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100889 return INSTALL_STATUS_OK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800890
891nomem:
892 fatal("cannot allocate listener");
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100893 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800894}
895
Yabin Cuie77b6a02014-11-11 09:24:11 -0800896#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800897static BOOL WINAPI ctrlc_handler(DWORD type)
898{
899 exit(STATUS_CONTROL_C_EXIT);
900 return TRUE;
901}
902#endif
903
904static void adb_cleanup(void)
905{
906 usb_cleanup();
907}
908
909void start_logging(void)
910{
Yabin Cuie77b6a02014-11-11 09:24:11 -0800911#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800912 char temp[ MAX_PATH ];
913 FILE* fnul;
914 FILE* flog;
915
916 GetTempPath( sizeof(temp) - 8, temp );
917 strcat( temp, "adb.log" );
918
919 /* Win32 specific redirections */
920 fnul = fopen( "NUL", "rt" );
921 if (fnul != NULL)
922 stdin[0] = fnul[0];
923
924 flog = fopen( temp, "at" );
925 if (flog == NULL)
926 flog = fnul;
927
928 setvbuf( flog, NULL, _IONBF, 0 );
929
930 stdout[0] = flog[0];
931 stderr[0] = flog[0];
932 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
933#else
934 int fd;
935
936 fd = unix_open("/dev/null", O_RDONLY);
937 dup2(fd, 0);
JP Abgrall408fa572011-03-16 15:57:42 -0700938 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800939
940 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
941 if(fd < 0) {
942 fd = unix_open("/dev/null", O_WRONLY);
943 }
944 dup2(fd, 1);
945 dup2(fd, 2);
JP Abgrall408fa572011-03-16 15:57:42 -0700946 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800947 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
948#endif
949}
950
951#if !ADB_HOST
952void start_device_log(void)
953{
954 int fd;
Mike Lockwood1f546e62009-05-25 18:17:55 -0400955 char path[PATH_MAX];
956 struct tm now;
957 time_t t;
958 char value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800959
Mike Lockwood1f546e62009-05-25 18:17:55 -0400960 // read the trace mask from persistent property persist.adb.trace_mask
961 // give up if the property is not set or cannot be parsed
962 property_get("persist.adb.trace_mask", value, "");
963 if (sscanf(value, "%x", &adb_trace_mask) != 1)
964 return;
965
966 adb_mkdir("/data/adb", 0775);
967 tzset();
968 time(&t);
969 localtime_r(&t, &now);
970 strftime(path, sizeof(path),
971 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
972 &now);
973 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800974 if (fd < 0)
975 return;
976
977 // redirect stdout and stderr to the log file
978 dup2(fd, 1);
979 dup2(fd, 2);
980 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
Benoit Goby95ef8282011-02-01 18:57:41 -0800981 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800982
983 fd = unix_open("/dev/null", O_RDONLY);
984 dup2(fd, 0);
Benoit Goby95ef8282011-02-01 18:57:41 -0800985 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800986}
987#endif
988
989#if ADB_HOST
JP Abgrall571c1362012-12-06 18:18:12 -0800990
991#ifdef WORKAROUND_BUG6558362
992#include <sched.h>
993#define AFFINITY_ENVVAR "ADB_CPU_AFFINITY_BUG6558362"
994void adb_set_affinity(void)
995{
996 cpu_set_t cpu_set;
997 const char* cpunum_str = getenv(AFFINITY_ENVVAR);
998 char* strtol_res;
999 int cpu_num;
1000
1001 if (!cpunum_str || !*cpunum_str)
1002 return;
1003 cpu_num = strtol(cpunum_str, &strtol_res, 0);
1004 if (*strtol_res != '\0')
1005 fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, AFFINITY_ENVVAR);
1006
1007 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
1008 D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
1009 CPU_ZERO(&cpu_set);
1010 CPU_SET(cpu_num, &cpu_set);
1011 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
1012 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
1013 D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
1014}
1015#endif
1016
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001017int launch_server(int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001018{
Yabin Cuie77b6a02014-11-11 09:24:11 -08001019#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001020 /* we need to start the server in the background */
1021 /* we create a PIPE that will be used to wait for the server's "OK" */
1022 /* message since the pipe handles must be inheritable, we use a */
1023 /* security attribute */
1024 HANDLE pipe_read, pipe_write;
Ray Donnelly267aa8b2012-11-29 01:18:50 +00001025 HANDLE stdout_handle, stderr_handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001026 SECURITY_ATTRIBUTES sa;
1027 STARTUPINFO startup;
1028 PROCESS_INFORMATION pinfo;
1029 char program_path[ MAX_PATH ];
1030 int ret;
1031
1032 sa.nLength = sizeof(sa);
1033 sa.lpSecurityDescriptor = NULL;
1034 sa.bInheritHandle = TRUE;
1035
1036 /* create pipe, and ensure its read handle isn't inheritable */
1037 ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
1038 if (!ret) {
1039 fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
1040 return -1;
1041 }
1042
1043 SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
1044
Ray Donnelly267aa8b2012-11-29 01:18:50 +00001045 /* Some programs want to launch an adb command and collect its output by
1046 * calling CreateProcess with inheritable stdout/stderr handles, then
1047 * using read() to get its output. When this happens, the stdout/stderr
1048 * handles passed to the adb client process will also be inheritable.
1049 * When starting the adb server here, care must be taken to reset them
1050 * to non-inheritable.
1051 * Otherwise, something bad happens: even if the adb command completes,
1052 * the calling process is stuck while read()-ing from the stdout/stderr
1053 * descriptors, because they're connected to corresponding handles in the
1054 * adb server process (even if the latter never uses/writes to them).
1055 */
1056 stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
1057 stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
1058 if (stdout_handle != INVALID_HANDLE_VALUE) {
1059 SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
1060 }
1061 if (stderr_handle != INVALID_HANDLE_VALUE) {
1062 SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
1063 }
1064
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001065 ZeroMemory( &startup, sizeof(startup) );
1066 startup.cb = sizeof(startup);
1067 startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
1068 startup.hStdOutput = pipe_write;
1069 startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
1070 startup.dwFlags = STARTF_USESTDHANDLES;
1071
1072 ZeroMemory( &pinfo, sizeof(pinfo) );
1073
1074 /* get path of current program */
1075 GetModuleFileName( NULL, program_path, sizeof(program_path) );
Wenhao Lia09558c2013-11-13 16:23:37 +08001076 char args[64];
1077 snprintf(args, sizeof(args), "adb -P %d fork-server server", server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001078 ret = CreateProcess(
1079 program_path, /* program path */
Wenhao Lia09558c2013-11-13 16:23:37 +08001080 args,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001081 /* the fork-server argument will set the
1082 debug = 2 in the child */
1083 NULL, /* process handle is not inheritable */
1084 NULL, /* thread handle is not inheritable */
1085 TRUE, /* yes, inherit some handles */
1086 DETACHED_PROCESS, /* the new process doesn't have a console */
1087 NULL, /* use parent's environment block */
1088 NULL, /* use parent's starting directory */
1089 &startup, /* startup info, i.e. std handles */
1090 &pinfo );
1091
1092 CloseHandle( pipe_write );
1093
1094 if (!ret) {
1095 fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
1096 CloseHandle( pipe_read );
1097 return -1;
1098 }
1099
1100 CloseHandle( pinfo.hProcess );
1101 CloseHandle( pinfo.hThread );
1102
1103 /* wait for the "OK\n" message */
1104 {
1105 char temp[3];
1106 DWORD count;
1107
1108 ret = ReadFile( pipe_read, temp, 3, &count, NULL );
1109 CloseHandle( pipe_read );
1110 if ( !ret ) {
1111 fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
1112 return -1;
1113 }
1114 if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1115 fprintf(stderr, "ADB server didn't ACK\n" );
1116 return -1;
1117 }
1118 }
Yabin Cuie77b6a02014-11-11 09:24:11 -08001119#else /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001120 char path[PATH_MAX];
1121 int fd[2];
1122
1123 // set up a pipe so the child can tell us when it is ready.
1124 // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
1125 if (pipe(fd)) {
1126 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
1127 return -1;
1128 }
Alexey Tarasov31664102009-10-22 02:55:00 +11001129 get_my_path(path, PATH_MAX);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001130 pid_t pid = fork();
1131 if(pid < 0) return -1;
1132
1133 if (pid == 0) {
1134 // child side of the fork
1135
1136 // redirect stderr to the pipe
1137 // we use stderr instead of stdout due to stdout's buffering behavior.
1138 adb_close(fd[0]);
1139 dup2(fd[1], STDERR_FILENO);
1140 adb_close(fd[1]);
1141
Matt Gumbeld7b33082012-11-14 10:16:17 -08001142 char str_port[30];
1143 snprintf(str_port, sizeof(str_port), "%d", server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001144 // child process
Matt Gumbeld7b33082012-11-14 10:16:17 -08001145 int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001146 // this should not return
1147 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
1148 } else {
1149 // parent side of the fork
1150
1151 char temp[3];
1152
1153 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
1154 // wait for the "OK\n" message
1155 adb_close(fd[1]);
1156 int ret = adb_read(fd[0], temp, 3);
JP Abgrall408fa572011-03-16 15:57:42 -07001157 int saved_errno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001158 adb_close(fd[0]);
1159 if (ret < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -07001160 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001161 return -1;
1162 }
1163 if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1164 fprintf(stderr, "ADB server didn't ACK\n" );
1165 return -1;
1166 }
1167
1168 setsid();
1169 }
Yabin Cuie77b6a02014-11-11 09:24:11 -08001170#endif /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001171 return 0;
1172}
Yabin Cuie77b6a02014-11-11 09:24:11 -08001173#endif /* ADB_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001174
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001175/* Constructs a local name of form tcp:port.
1176 * target_str points to the target string, it's content will be overwritten.
1177 * target_size is the capacity of the target string.
1178 * server_port is the port number to use for the local name.
1179 */
1180void build_local_name(char* target_str, size_t target_size, int server_port)
1181{
1182 snprintf(target_str, target_size, "tcp:%d", server_port);
1183}
1184
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001185#if !ADB_HOST
Nick Kralevich080427e2013-02-15 14:39:15 -08001186
1187static void drop_capabilities_bounding_set_if_needed() {
1188#ifdef ALLOW_ADBD_ROOT
1189 char value[PROPERTY_VALUE_MAX];
1190 property_get("ro.debuggable", value, "");
1191 if (strcmp(value, "1") == 0) {
1192 return;
1193 }
1194#endif
1195 int i;
1196 for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
Nick Kralevichca8e66a2013-04-18 12:20:02 -07001197 if (i == CAP_SETUID || i == CAP_SETGID) {
Nick Kralevich080427e2013-02-15 14:39:15 -08001198 // CAP_SETUID CAP_SETGID needed by /system/bin/run-as
1199 continue;
1200 }
1201 int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
1202
1203 // Some kernels don't have file capabilities compiled in, and
1204 // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically
1205 // die when we see such misconfigured kernels.
1206 if ((err < 0) && (errno != EINVAL)) {
1207 exit(1);
1208 }
1209 }
1210}
1211
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001212static int should_drop_privileges() {
Nick Kralevich5890fe32012-01-19 13:11:35 -08001213#ifndef ALLOW_ADBD_ROOT
1214 return 1;
1215#else /* ALLOW_ADBD_ROOT */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001216 int secure = 0;
1217 char value[PROPERTY_VALUE_MAX];
1218
1219 /* run adbd in secure mode if ro.secure is set and
1220 ** we are not in the emulator
1221 */
1222 property_get("ro.kernel.qemu", value, "");
1223 if (strcmp(value, "1") != 0) {
1224 property_get("ro.secure", value, "1");
1225 if (strcmp(value, "1") == 0) {
1226 // don't run as root if ro.secure is set...
1227 secure = 1;
1228
1229 // ... except we allow running as root in userdebug builds if the
1230 // service.adb.root property has been set by the "adb root" command
1231 property_get("ro.debuggable", value, "");
1232 if (strcmp(value, "1") == 0) {
1233 property_get("service.adb.root", value, "");
1234 if (strcmp(value, "1") == 0) {
1235 secure = 0;
1236 }
1237 }
1238 }
1239 }
1240 return secure;
Nick Kralevich5890fe32012-01-19 13:11:35 -08001241#endif /* ALLOW_ADBD_ROOT */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001242}
1243#endif /* !ADB_HOST */
1244
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001245int adb_main(int is_daemon, int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001246{
1247#if !ADB_HOST
Mike Lockwood2f38b692009-08-24 15:58:40 -07001248 int port;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001249 char value[PROPERTY_VALUE_MAX];
Nick Kralevicheb68fa82012-04-02 13:00:35 -07001250
1251 umask(000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252#endif
1253
1254 atexit(adb_cleanup);
Yabin Cuie77b6a02014-11-11 09:24:11 -08001255#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001256 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
Yabin Cuie77b6a02014-11-11 09:24:11 -08001257#else
JP Abgrall408fa572011-03-16 15:57:42 -07001258 // No SIGCHLD. Let the service subproc handle its children.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001259 signal(SIGPIPE, SIG_IGN);
1260#endif
1261
1262 init_transport_registration();
1263
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001264#if ADB_HOST
1265 HOST = 1;
JP Abgrall571c1362012-12-06 18:18:12 -08001266
1267#ifdef WORKAROUND_BUG6558362
1268 if(is_daemon) adb_set_affinity();
1269#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001270 usb_init();
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001271 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001272 adb_auth_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001273
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001274 char local_name[30];
1275 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001276 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001277 exit(1);
1278 }
1279#else
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001280 property_get("ro.adb.secure", value, "0");
1281 auth_enabled = !strcmp(value, "1");
1282 if (auth_enabled)
1283 adb_auth_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001284
Jeff Sharkeyd6d42862012-09-06 13:05:40 -07001285 // Our external storage path may be different than apps, since
1286 // we aren't able to bind mount after dropping root.
1287 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
1288 if (NULL != adb_external_storage) {
1289 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
1290 } else {
1291 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
1292 " unchanged.\n");
1293 }
1294
Nick Kraleviche5cbf4e2014-06-18 11:24:27 -07001295 /* add extra groups:
1296 ** AID_ADB to access the USB driver
1297 ** AID_LOG to read system logs (adb logcat)
1298 ** AID_INPUT to diagnose input issues (getevent)
Elliott Hughes187eade2015-02-03 11:59:22 -08001299 ** AID_INET to diagnose network issues (ping)
Nick Kraleviche5cbf4e2014-06-18 11:24:27 -07001300 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
1301 ** AID_SDCARD_R to allow reading from the SD card
1302 ** AID_SDCARD_RW to allow writing to the SD card
1303 ** AID_NET_BW_STATS to read out qtaguid statistics
1304 */
Ajay Dudani8432ddc2014-11-21 11:57:29 -08001305 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_NET_BT,
1306 AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
Nick Kraleviche5cbf4e2014-06-18 11:24:27 -07001307 AID_NET_BW_STATS };
1308 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
1309 exit(1);
1310 }
1311
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001312 /* don't listen on a port (default 5037) if running in secure mode */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001313 /* don't run as root if we are running in secure mode */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001314 if (should_drop_privileges()) {
Nick Kralevich080427e2013-02-15 14:39:15 -08001315 drop_capabilities_bounding_set_if_needed();
1316
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001317 /* then switch user and group to "shell" */
Nick Kralevich44db9902010-08-27 14:35:07 -07001318 if (setgid(AID_SHELL) != 0) {
1319 exit(1);
1320 }
1321 if (setuid(AID_SHELL) != 0) {
1322 exit(1);
1323 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001324
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001325 D("Local port disabled\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001326 } else {
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001327 char local_name[30];
Nick Kralevichd49aa252014-01-18 09:25:04 -08001328 if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
1329 // b/12587913: fix setcon to allow const pointers
1330 if (setcon((char *)root_seclabel) < 0) {
1331 exit(1);
1332 }
1333 }
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001334 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001335 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001336 exit(1);
1337 }
1338 }
1339
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001340 int usb = 0;
1341 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -07001342 // listen on USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001343 usb_init();
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001344 usb = 1;
1345 }
1346
1347 // If one of these properties is set, also listen on that port
1348 // If one of the properties isn't set and we couldn't listen on usb,
1349 // listen on the default port.
1350 property_get("service.adb.tcp.port", value, "");
1351 if (!value[0]) {
1352 property_get("persist.adb.tcp.port", value, "");
1353 }
1354 if (sscanf(value, "%d", &port) == 1 && port > 0) {
1355 printf("using port=%d\n", port);
1356 // listen on TCP port specified by service.adb.tcp.port property
1357 local_init(port);
1358 } else if (!usb) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -07001359 // listen on default port
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001360 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001361 }
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001362
JP Abgrall408fa572011-03-16 15:57:42 -07001363 D("adb_main(): pre init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001364 init_jdwp();
JP Abgrall408fa572011-03-16 15:57:42 -07001365 D("adb_main(): post init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001366#endif
1367
1368 if (is_daemon)
1369 {
1370 // inform our parent that we are up and running.
Yabin Cuie77b6a02014-11-11 09:24:11 -08001371#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001372 DWORD count;
1373 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
Yabin Cuie77b6a02014-11-11 09:24:11 -08001374#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001375 fprintf(stderr, "OK\n");
1376#endif
1377 start_logging();
1378 }
JP Abgrall408fa572011-03-16 15:57:42 -07001379 D("Event loop starting\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001380
1381 fdevent_loop();
1382
1383 usb_cleanup();
1384
1385 return 0;
1386}
1387
David 'Digit' Turner25258692013-03-21 21:07:42 +01001388// Try to handle a network forwarding request.
1389// This returns 1 on success, 0 on failure, and -1 to indicate this is not
1390// a forwarding-related request.
1391int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd)
1392{
1393 if (!strcmp(service, "list-forward")) {
1394 // Create the list of forward redirections.
1395 int buffer_size = format_listeners(NULL, 0);
1396 // Add one byte for the trailing zero.
1397 char* buffer = malloc(buffer_size + 1);
1398 if (buffer == NULL) {
1399 sendfailmsg(reply_fd, "not enough memory");
1400 return 1;
1401 }
1402 (void) format_listeners(buffer, buffer_size + 1);
1403#if ADB_HOST
1404 send_msg_with_okay(reply_fd, buffer, buffer_size);
1405#else
1406 send_msg_with_header(reply_fd, buffer, buffer_size);
1407#endif
1408 free(buffer);
1409 return 1;
1410 }
1411
1412 if (!strcmp(service, "killforward-all")) {
1413 remove_all_listeners();
1414#if ADB_HOST
1415 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
1416 adb_write(reply_fd, "OKAY", 4);
1417#endif
1418 adb_write(reply_fd, "OKAY", 4);
1419 return 1;
1420 }
1421
1422 if (!strncmp(service, "forward:",8) ||
1423 !strncmp(service, "killforward:",12)) {
1424 char *local, *remote, *err;
1425 int r;
1426 atransport *transport;
1427
1428 int createForward = strncmp(service, "kill", 4);
1429 int no_rebind = 0;
1430
1431 local = strchr(service, ':') + 1;
1432
1433 // Handle forward:norebind:<local>... here
1434 if (createForward && !strncmp(local, "norebind:", 9)) {
1435 no_rebind = 1;
1436 local = strchr(local, ':') + 1;
1437 }
1438
1439 remote = strchr(local,';');
1440
1441 if (createForward) {
1442 // Check forward: parameter format: '<local>;<remote>'
1443 if(remote == 0) {
1444 sendfailmsg(reply_fd, "malformed forward spec");
1445 return 1;
1446 }
1447
1448 *remote++ = 0;
1449 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')) {
1450 sendfailmsg(reply_fd, "malformed forward spec");
1451 return 1;
1452 }
1453 } else {
1454 // Check killforward: parameter format: '<local>'
1455 if (local[0] == 0) {
1456 sendfailmsg(reply_fd, "malformed forward spec");
1457 return 1;
1458 }
1459 }
1460
1461 transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
1462 if (!transport) {
1463 sendfailmsg(reply_fd, err);
1464 return 1;
1465 }
1466
1467 if (createForward) {
1468 r = install_listener(local, remote, transport, no_rebind);
1469 } else {
1470 r = remove_listener(local, transport);
1471 }
1472 if(r == 0) {
1473#if ADB_HOST
1474 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
1475 writex(reply_fd, "OKAY", 4);
1476#endif
1477 writex(reply_fd, "OKAY", 4);
1478 return 1;
1479 }
1480
1481 if (createForward) {
1482 const char* message;
1483 switch (r) {
1484 case INSTALL_STATUS_CANNOT_BIND:
1485 message = "cannot bind to socket";
1486 break;
1487 case INSTALL_STATUS_CANNOT_REBIND:
1488 message = "cannot rebind existing socket";
1489 break;
1490 default:
1491 message = "internal error";
1492 }
1493 sendfailmsg(reply_fd, message);
1494 } else {
1495 sendfailmsg(reply_fd, "cannot remove listener");
1496 }
1497 return 1;
1498 }
1499 return 0;
1500}
1501
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001502int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
1503{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001504 if(!strcmp(service, "kill")) {
1505 fprintf(stderr,"adb server killed by remote request\n");
1506 fflush(stdout);
1507 adb_write(reply_fd, "OKAY", 4);
1508 usb_cleanup();
1509 exit(0);
1510 }
1511
1512#if ADB_HOST
Chih-Hung Hsiehf787b382014-09-05 15:38:15 -07001513 atransport *transport = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001514 // "transport:" is used for switching transport with a specified serial number
1515 // "transport-usb:" is used for switching transport to the only USB transport
1516 // "transport-local:" is used for switching transport to the only local transport
1517 // "transport-any:" is used for switching transport to the only transport
1518 if (!strncmp(service, "transport", strlen("transport"))) {
1519 char* error_string = "unknown failure";
1520 transport_type type = kTransportAny;
1521
1522 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
1523 type = kTransportUsb;
1524 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
1525 type = kTransportLocal;
1526 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
1527 type = kTransportAny;
1528 } else if (!strncmp(service, "transport:", strlen("transport:"))) {
1529 service += strlen("transport:");
Tom Marlin3175c8e2011-07-27 12:56:14 -05001530 serial = service;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001531 }
1532
1533 transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
1534
1535 if (transport) {
1536 s->transport = transport;
1537 adb_write(reply_fd, "OKAY", 4);
1538 } else {
1539 sendfailmsg(reply_fd, error_string);
1540 }
1541 return 1;
1542 }
1543
1544 // return a list of all connected devices
Scott Andersone109d262012-04-20 11:21:14 -07001545 if (!strncmp(service, "devices", 7)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001546 char buffer[4096];
Scott Andersone109d262012-04-20 11:21:14 -07001547 int use_long = !strcmp(service+7, "-l");
1548 if (use_long || service[7] == 0) {
Scott Andersone109d262012-04-20 11:21:14 -07001549 memset(buffer, 0, sizeof(buffer));
1550 D("Getting device list \n");
1551 list_transports(buffer, sizeof(buffer), use_long);
Scott Andersone109d262012-04-20 11:21:14 -07001552 D("Wrote device list \n");
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001553 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Scott Andersone109d262012-04-20 11:21:14 -07001554 return 0;
1555 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001556 }
1557
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001558 // remove TCP transport
1559 if (!strncmp(service, "disconnect:", 11)) {
1560 char buffer[4096];
1561 memset(buffer, 0, sizeof(buffer));
1562 char* serial = service + 11;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001563 if (serial[0] == 0) {
1564 // disconnect from all TCP devices
1565 unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001566 } else {
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001567 char hostbuf[100];
1568 // assume port 5555 if no port is specified
1569 if (!strchr(serial, ':')) {
1570 snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
1571 serial = hostbuf;
1572 }
1573 atransport *t = find_transport(serial);
1574
1575 if (t) {
1576 unregister_transport(t);
1577 } else {
1578 snprintf(buffer, sizeof(buffer), "No such device %s", serial);
1579 }
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001580 }
1581
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001582 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Mike Lockwood2f38b692009-08-24 15:58:40 -07001583 return 0;
1584 }
1585
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001586 // returns our value for ADB_SERVER_VERSION
1587 if (!strcmp(service, "version")) {
1588 char version[12];
1589 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001590 send_msg_with_okay(reply_fd, version, strlen(version));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001591 return 0;
1592 }
1593
1594 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
1595 char *out = "unknown";
1596 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1597 if (transport && transport->serial) {
1598 out = transport->serial;
1599 }
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001600 send_msg_with_okay(reply_fd, out, strlen(out));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001601 return 0;
1602 }
Scott Andersone109d262012-04-20 11:21:14 -07001603 if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
1604 char *out = "unknown";
1605 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1606 if (transport && transport->devpath) {
1607 out = transport->devpath;
1608 }
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001609 send_msg_with_okay(reply_fd, out, strlen(out));
Scott Andersone109d262012-04-20 11:21:14 -07001610 return 0;
1611 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001612 // indicates a new emulator instance has started
1613 if (!strncmp(service,"emulator:",9)) {
1614 int port = atoi(service+9);
1615 local_connect(port);
1616 /* we don't even need to send a reply */
1617 return 0;
1618 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001619
1620 if(!strncmp(service,"get-state",strlen("get-state"))) {
1621 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1622 char *state = connection_state_name(transport);
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001623 send_msg_with_okay(reply_fd, state, strlen(state));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001624 return 0;
1625 }
Simon Yedc22c3c2014-07-14 17:23:06 -07001626#endif // ADB_HOST
1627
1628 int ret = handle_forward_request(service, ttype, serial, reply_fd);
1629 if (ret >= 0)
1630 return ret - 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001631 return -1;
1632}
1633
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001634int main(int argc, char **argv)
1635{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001636#if ADB_HOST
1637 adb_sysdeps_init();
JP Abgrall408fa572011-03-16 15:57:42 -07001638 adb_trace_init();
1639 D("Handling commandline()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001640 return adb_commandline(argc - 1, argv + 1);
1641#else
Vladimir Chtchetkine28781b02012-02-27 10:41:53 -08001642 /* If adbd runs inside the emulator this will enable adb tracing via
1643 * adb-debug qemud service in the emulator. */
1644 adb_qemu_trace_init();
Nick Kralevichd49aa252014-01-18 09:25:04 -08001645 while(1) {
1646 int c;
1647 int option_index = 0;
1648 static struct option opts[] = {
1649 {"root_seclabel", required_argument, 0, 's' },
1650 {"device_banner", required_argument, 0, 'b' }
1651 };
1652 c = getopt_long(argc, argv, "", opts, &option_index);
1653 if (c == -1)
1654 break;
1655 switch (c) {
1656 case 's':
1657 root_seclabel = optarg;
1658 break;
1659 case 'b':
1660 adb_device_banner = optarg;
1661 break;
1662 default:
1663 break;
1664 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001665 }
Mike Lockwood1f546e62009-05-25 18:17:55 -04001666
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001667 start_device_log();
JP Abgrall408fa572011-03-16 15:57:42 -07001668 D("Handling main()\n");
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001669 return adb_main(0, DEFAULT_ADB_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001670#endif
1671}