blob: a336478d64fe49371fe20dca4f7e0ccd3e19f53c [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 Albert9e719342015-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
Dan Alberte2740252015-02-18 17:47:33 -0800289void send_connect(atransport *t)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290{
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
Chih-Hung Hsiehf787b382014-09-05 15:38:15 -0700301#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302static char *connection_state_name(atransport *t)
303{
304 if (t == NULL) {
305 return "unknown";
306 }
307
308 switch(t->connection_state) {
309 case CS_BOOTLOADER:
310 return "bootloader";
311 case CS_DEVICE:
312 return "device";
trevda5ad5392013-04-17 14:34:23 +0100313 case CS_RECOVERY:
314 return "recovery";
315 case CS_SIDELOAD:
316 return "sideload";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 case CS_OFFLINE:
318 return "offline";
Benoit Goby77e8e582013-01-15 12:36:47 -0800319 case CS_UNAUTHORIZED:
320 return "unauthorized";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 default:
322 return "unknown";
323 }
324}
Chih-Hung Hsiehf787b382014-09-05 15:38:15 -0700325#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326
Scott Andersone82c2db2012-05-25 14:10:02 -0700327/* qual_overwrite is used to overwrite a qualifier string. dst is a
328 * pointer to a char pointer. It is assumed that if *dst is non-NULL, it
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700329 * was malloc'ed and needs to freed. *dst will be set to a dup of src.
Scott Andersone82c2db2012-05-25 14:10:02 -0700330 */
331static void qual_overwrite(char **dst, const char *src)
332{
333 if (!dst)
334 return;
335
336 free(*dst);
337 *dst = NULL;
338
339 if (!src || !*src)
340 return;
341
342 *dst = strdup(src);
343}
344
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345void parse_banner(char *banner, atransport *t)
346{
Scott Andersone82c2db2012-05-25 14:10:02 -0700347 static const char *prop_seps = ";";
348 static const char key_val_sep = '=';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700349 char *cp;
350 char *type;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351
352 D("parse_banner: %s\n", banner);
353 type = banner;
Scott Andersone82c2db2012-05-25 14:10:02 -0700354 cp = strchr(type, ':');
355 if (cp) {
356 *cp++ = 0;
357 /* Nothing is done with second field. */
358 cp = strchr(cp, ':');
359 if (cp) {
360 char *save;
361 char *key;
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700362 key = adb_strtok_r(cp + 1, prop_seps, &save);
Scott Andersone82c2db2012-05-25 14:10:02 -0700363 while (key) {
364 cp = strchr(key, key_val_sep);
365 if (cp) {
366 *cp++ = '\0';
367 if (!strcmp(key, "ro.product.name"))
368 qual_overwrite(&t->product, cp);
369 else if (!strcmp(key, "ro.product.model"))
370 qual_overwrite(&t->model, cp);
371 else if (!strcmp(key, "ro.product.device"))
372 qual_overwrite(&t->device, cp);
373 }
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700374 key = adb_strtok_r(NULL, prop_seps, &save);
Scott Andersone82c2db2012-05-25 14:10:02 -0700375 }
376 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 }
378
379 if(!strcmp(type, "bootloader")){
380 D("setting connection_state to CS_BOOTLOADER\n");
381 t->connection_state = CS_BOOTLOADER;
382 update_transports();
383 return;
384 }
385
386 if(!strcmp(type, "device")) {
387 D("setting connection_state to CS_DEVICE\n");
388 t->connection_state = CS_DEVICE;
389 update_transports();
390 return;
391 }
392
393 if(!strcmp(type, "recovery")) {
394 D("setting connection_state to CS_RECOVERY\n");
395 t->connection_state = CS_RECOVERY;
396 update_transports();
397 return;
398 }
399
Doug Zongker447f0612012-01-09 14:54:53 -0800400 if(!strcmp(type, "sideload")) {
401 D("setting connection_state to CS_SIDELOAD\n");
402 t->connection_state = CS_SIDELOAD;
403 update_transports();
404 return;
405 }
406
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 t->connection_state = CS_HOST;
408}
409
410void handle_packet(apacket *p, atransport *t)
411{
412 asocket *s;
413
Viral Mehta899913f2010-06-16 18:41:28 +0530414 D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
415 ((char*) (&(p->msg.command)))[1],
416 ((char*) (&(p->msg.command)))[2],
417 ((char*) (&(p->msg.command)))[3]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 print_packet("recv", p);
419
420 switch(p->msg.command){
421 case A_SYNC:
422 if(p->msg.arg0){
423 send_packet(p, t);
424 if(HOST) send_connect(t);
425 } else {
426 t->connection_state = CS_OFFLINE;
427 handle_offline(t);
428 send_packet(p, t);
429 }
430 return;
431
432 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
433 /* XXX verify version, etc */
434 if(t->connection_state != CS_OFFLINE) {
435 t->connection_state = CS_OFFLINE;
436 handle_offline(t);
437 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700438
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 parse_banner((char*) p->data, t);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700440
441 if (HOST || !auth_enabled) {
442 handle_online(t);
443 if(!HOST) send_connect(t);
444 } else {
445 send_auth_request(t);
446 }
447 break;
448
449 case A_AUTH:
450 if (p->msg.arg0 == ADB_AUTH_TOKEN) {
Benoit Goby77e8e582013-01-15 12:36:47 -0800451 t->connection_state = CS_UNAUTHORIZED;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700452 t->key = adb_auth_nextkey(t->key);
453 if (t->key) {
454 send_auth_response(p->data, p->msg.data_length, t);
455 } else {
456 /* No more private keys to try, send the public key */
457 send_auth_publickey(t);
458 }
459 } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
460 if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
461 adb_auth_verified(t);
462 t->failed_auth_attempts = 0;
463 } else {
464 if (t->failed_auth_attempts++ > 10)
465 adb_sleep_ms(1000);
466 send_auth_request(t);
467 }
468 } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
469 adb_auth_confirm_key(p->data, p->msg.data_length, t);
470 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 break;
472
473 case A_OPEN: /* OPEN(local-id, 0, "destination") */
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100474 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 char *name = (char*) p->data;
476 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
477 s = create_local_service_socket(name);
478 if(s == 0) {
479 send_close(0, p->msg.arg0, t);
480 } else {
481 s->peer = create_remote_socket(p->msg.arg0, t);
482 s->peer->peer = s;
483 send_ready(s->id, s->peer->id, t);
484 s->ready(s);
485 }
486 }
487 break;
488
489 case A_OKAY: /* READY(local-id, remote-id, "") */
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100490 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
491 if((s = find_local_socket(p->msg.arg1, 0))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 if(s->peer == 0) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100493 /* On first READY message, create the connection. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 s->peer = create_remote_socket(p->msg.arg0, t);
495 s->peer->peer = s;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100496 s->ready(s);
497 } else if (s->peer->id == p->msg.arg0) {
498 /* Other READY messages must use the same local-id */
499 s->ready(s);
500 } else {
501 D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s\n",
502 p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 }
505 }
506 break;
507
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100508 case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */
509 if (t->online && p->msg.arg1 != 0) {
510 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
511 /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
512 * a failed OPEN only. However, due to a bug in previous ADB
513 * versions, CLOSE(0, remote-id, "") was also used for normal
514 * CLOSE() operations.
515 *
516 * This is bad because it means a compromised adbd could
517 * send packets to close connections between the host and
518 * other devices. To avoid this, only allow this if the local
519 * socket has a peer on the same transport.
520 */
521 if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
522 D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s\n",
523 p->msg.arg1, t->serial, s->peer->transport->serial);
524 } else {
525 s->close(s);
526 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 }
528 }
529 break;
530
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100531 case A_WRTE: /* WRITE(local-id, remote-id, <data>) */
532 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
533 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534 unsigned rid = p->msg.arg0;
535 p->len = p->msg.data_length;
536
537 if(s->enqueue(s, p) == 0) {
538 D("Enqueue the socket\n");
539 send_ready(s->id, rid, t);
540 }
541 return;
542 }
543 }
544 break;
545
546 default:
547 printf("handle_packet: what is %08x?!\n", p->msg.command);
548 }
549
550 put_apacket(p);
551}
552
553alistener listener_list = {
554 .next = &listener_list,
555 .prev = &listener_list,
556};
557
558static void ss_listener_event_func(int _fd, unsigned ev, void *_l)
559{
560 asocket *s;
561
562 if(ev & FDE_READ) {
563 struct sockaddr addr;
564 socklen_t alen;
565 int fd;
566
567 alen = sizeof(addr);
568 fd = adb_socket_accept(_fd, &addr, &alen);
569 if(fd < 0) return;
570
571 adb_socket_setbufsize(fd, CHUNK_SIZE);
572
573 s = create_local_socket(fd);
574 if(s) {
575 connect_to_smartsocket(s);
576 return;
577 }
578
579 adb_close(fd);
580 }
581}
582
583static void listener_event_func(int _fd, unsigned ev, void *_l)
584{
585 alistener *l = _l;
586 asocket *s;
587
588 if(ev & FDE_READ) {
589 struct sockaddr addr;
590 socklen_t alen;
591 int fd;
592
593 alen = sizeof(addr);
594 fd = adb_socket_accept(_fd, &addr, &alen);
595 if(fd < 0) return;
596
597 s = create_local_socket(fd);
598 if(s) {
599 s->transport = l->transport;
600 connect_to_remote(s, l->connect_to);
601 return;
602 }
603
604 adb_close(fd);
605 }
606}
607
608static void free_listener(alistener* l)
609{
610 if (l->next) {
611 l->next->prev = l->prev;
612 l->prev->next = l->next;
613 l->next = l->prev = l;
614 }
615
616 // closes the corresponding fd
617 fdevent_remove(&l->fde);
618
619 if (l->local_name)
620 free((char*)l->local_name);
621
622 if (l->connect_to)
623 free((char*)l->connect_to);
624
625 if (l->transport) {
626 remove_transport_disconnect(l->transport, &l->disconnect);
627 }
628 free(l);
629}
630
631static void listener_disconnect(void* _l, atransport* t)
632{
633 alistener* l = _l;
634
635 free_listener(l);
636}
637
638int local_name_to_fd(const char *name)
639{
640 int port;
641
642 if(!strncmp("tcp:", name, 4)){
643 int ret;
644 port = atoi(name + 4);
Matt Gumbeld7b33082012-11-14 10:16:17 -0800645
646 if (gListenAll > 0) {
647 ret = socket_inaddr_any_server(port, SOCK_STREAM);
648 } else {
649 ret = socket_loopback_server(port, SOCK_STREAM);
650 }
651
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652 return ret;
653 }
654#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
655 // It's non-sensical to support the "reserved" space on the adb host side
656 if(!strncmp(name, "local:", 6)) {
657 return socket_local_server(name + 6,
658 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
659 } else if(!strncmp(name, "localabstract:", 14)) {
660 return socket_local_server(name + 14,
661 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
662 } else if(!strncmp(name, "localfilesystem:", 16)) {
663 return socket_local_server(name + 16,
664 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
665 }
666
667#endif
668 printf("unknown local portname '%s'\n", name);
669 return -1;
670}
671
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100672// Write a single line describing a listener to a user-provided buffer.
673// Appends a trailing zero, even in case of truncation, but the function
674// returns the full line length.
675// If |buffer| is NULL, does not write but returns required size.
676static int format_listener(alistener* l, char* buffer, size_t buffer_len) {
677 // Format is simply:
678 //
679 // <device-serial> " " <local-name> " " <remote-name> "\n"
680 //
681 int local_len = strlen(l->local_name);
682 int connect_len = strlen(l->connect_to);
683 int serial_len = strlen(l->transport->serial);
684
685 if (buffer != NULL) {
686 snprintf(buffer, buffer_len, "%s %s %s\n",
687 l->transport->serial, l->local_name, l->connect_to);
688 }
689 // NOTE: snprintf() on Windows returns -1 in case of truncation, so
690 // return the computed line length instead.
691 return local_len + connect_len + serial_len + 3;
692}
693
694// Write the list of current listeners (network redirections) into a
695// user-provided buffer. Appends a trailing zero, even in case of
696// trunctaion, but return the full size in bytes.
697// If |buffer| is NULL, does not write but returns required size.
698static int format_listeners(char* buf, size_t buflen)
699{
700 alistener* l;
701 int result = 0;
702 for (l = listener_list.next; l != &listener_list; l = l->next) {
703 // Ignore special listeners like those for *smartsocket*
704 if (l->connect_to[0] == '*')
705 continue;
706 int len = format_listener(l, buf, buflen);
707 // Ensure there is space for the trailing zero.
708 result += len;
709 if (buf != NULL) {
710 buf += len;
711 buflen -= len;
712 if (buflen <= 0)
713 break;
714 }
715 }
716 return result;
717}
718
719static int remove_listener(const char *local_name, atransport* transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720{
721 alistener *l;
722
723 for (l = listener_list.next; l != &listener_list; l = l->next) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100724 if (!strcmp(local_name, l->local_name)) {
725 listener_disconnect(l, l->transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726 return 0;
727 }
728 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729 return -1;
730}
731
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100732static void remove_all_listeners(void)
733{
734 alistener *l, *l_next;
735 for (l = listener_list.next; l != &listener_list; l = l_next) {
736 l_next = l->next;
737 // Never remove smart sockets.
738 if (l->connect_to[0] == '*')
739 continue;
740 listener_disconnect(l, l->transport);
741 }
742}
743
744// error/status codes for install_listener.
745typedef enum {
746 INSTALL_STATUS_OK = 0,
747 INSTALL_STATUS_INTERNAL_ERROR = -1,
748 INSTALL_STATUS_CANNOT_BIND = -2,
749 INSTALL_STATUS_CANNOT_REBIND = -3,
750} install_status_t;
751
752static install_status_t install_listener(const char *local_name,
753 const char *connect_to,
754 atransport* transport,
755 int no_rebind)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756{
757 alistener *l;
758
759 //printf("install_listener('%s','%s')\n", local_name, connect_to);
760
761 for(l = listener_list.next; l != &listener_list; l = l->next){
762 if(strcmp(local_name, l->local_name) == 0) {
763 char *cto;
764
765 /* can't repurpose a smartsocket */
766 if(l->connect_to[0] == '*') {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100767 return INSTALL_STATUS_INTERNAL_ERROR;
768 }
769
770 /* can't repurpose a listener if 'no_rebind' is true */
771 if (no_rebind) {
772 return INSTALL_STATUS_CANNOT_REBIND;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800773 }
774
775 cto = strdup(connect_to);
776 if(cto == 0) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100777 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 }
779
780 //printf("rebinding '%s' to '%s'\n", local_name, connect_to);
781 free((void*) l->connect_to);
782 l->connect_to = cto;
783 if (l->transport != transport) {
784 remove_transport_disconnect(l->transport, &l->disconnect);
785 l->transport = transport;
786 add_transport_disconnect(l->transport, &l->disconnect);
787 }
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100788 return INSTALL_STATUS_OK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800789 }
790 }
791
792 if((l = calloc(1, sizeof(alistener))) == 0) goto nomem;
793 if((l->local_name = strdup(local_name)) == 0) goto nomem;
794 if((l->connect_to = strdup(connect_to)) == 0) goto nomem;
795
796
797 l->fd = local_name_to_fd(local_name);
798 if(l->fd < 0) {
799 free((void*) l->local_name);
800 free((void*) l->connect_to);
801 free(l);
802 printf("cannot bind '%s'\n", local_name);
803 return -2;
804 }
805
806 close_on_exec(l->fd);
807 if(!strcmp(l->connect_to, "*smartsocket*")) {
808 fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);
809 } else {
810 fdevent_install(&l->fde, l->fd, listener_event_func, l);
811 }
812 fdevent_set(&l->fde, FDE_READ);
813
814 l->next = &listener_list;
815 l->prev = listener_list.prev;
816 l->next->prev = l;
817 l->prev->next = l;
818 l->transport = transport;
819
820 if (transport) {
821 l->disconnect.opaque = l;
822 l->disconnect.func = listener_disconnect;
823 add_transport_disconnect(transport, &l->disconnect);
824 }
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100825 return INSTALL_STATUS_OK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800826
827nomem:
828 fatal("cannot allocate listener");
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100829 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830}
831
Yabin Cuie77b6a02014-11-11 09:24:11 -0800832#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800833static BOOL WINAPI ctrlc_handler(DWORD type)
834{
835 exit(STATUS_CONTROL_C_EXIT);
836 return TRUE;
837}
838#endif
839
840static void adb_cleanup(void)
841{
842 usb_cleanup();
843}
844
845void start_logging(void)
846{
Yabin Cuie77b6a02014-11-11 09:24:11 -0800847#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800848 char temp[ MAX_PATH ];
849 FILE* fnul;
850 FILE* flog;
851
852 GetTempPath( sizeof(temp) - 8, temp );
853 strcat( temp, "adb.log" );
854
855 /* Win32 specific redirections */
856 fnul = fopen( "NUL", "rt" );
857 if (fnul != NULL)
858 stdin[0] = fnul[0];
859
860 flog = fopen( temp, "at" );
861 if (flog == NULL)
862 flog = fnul;
863
864 setvbuf( flog, NULL, _IONBF, 0 );
865
866 stdout[0] = flog[0];
867 stderr[0] = flog[0];
868 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
869#else
870 int fd;
871
872 fd = unix_open("/dev/null", O_RDONLY);
873 dup2(fd, 0);
JP Abgrall408fa572011-03-16 15:57:42 -0700874 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800875
876 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
877 if(fd < 0) {
878 fd = unix_open("/dev/null", O_WRONLY);
879 }
880 dup2(fd, 1);
881 dup2(fd, 2);
JP Abgrall408fa572011-03-16 15:57:42 -0700882 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800883 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
884#endif
885}
886
887#if !ADB_HOST
888void start_device_log(void)
889{
890 int fd;
Mike Lockwood1f546e62009-05-25 18:17:55 -0400891 char path[PATH_MAX];
892 struct tm now;
893 time_t t;
894 char value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895
Mike Lockwood1f546e62009-05-25 18:17:55 -0400896 // read the trace mask from persistent property persist.adb.trace_mask
897 // give up if the property is not set or cannot be parsed
898 property_get("persist.adb.trace_mask", value, "");
899 if (sscanf(value, "%x", &adb_trace_mask) != 1)
900 return;
901
902 adb_mkdir("/data/adb", 0775);
903 tzset();
904 time(&t);
905 localtime_r(&t, &now);
906 strftime(path, sizeof(path),
907 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
908 &now);
909 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800910 if (fd < 0)
911 return;
912
913 // redirect stdout and stderr to the log file
914 dup2(fd, 1);
915 dup2(fd, 2);
916 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
Benoit Goby95ef8282011-02-01 18:57:41 -0800917 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800918
919 fd = unix_open("/dev/null", O_RDONLY);
920 dup2(fd, 0);
Benoit Goby95ef8282011-02-01 18:57:41 -0800921 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800922}
923#endif
924
925#if ADB_HOST
JP Abgrall571c1362012-12-06 18:18:12 -0800926
927#ifdef WORKAROUND_BUG6558362
928#include <sched.h>
929#define AFFINITY_ENVVAR "ADB_CPU_AFFINITY_BUG6558362"
930void adb_set_affinity(void)
931{
932 cpu_set_t cpu_set;
933 const char* cpunum_str = getenv(AFFINITY_ENVVAR);
934 char* strtol_res;
935 int cpu_num;
936
937 if (!cpunum_str || !*cpunum_str)
938 return;
939 cpu_num = strtol(cpunum_str, &strtol_res, 0);
940 if (*strtol_res != '\0')
941 fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, AFFINITY_ENVVAR);
942
943 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
944 D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
945 CPU_ZERO(&cpu_set);
946 CPU_SET(cpu_num, &cpu_set);
947 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
948 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
949 D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
950}
951#endif
952
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100953int launch_server(int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800954{
Yabin Cuie77b6a02014-11-11 09:24:11 -0800955#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800956 /* we need to start the server in the background */
957 /* we create a PIPE that will be used to wait for the server's "OK" */
958 /* message since the pipe handles must be inheritable, we use a */
959 /* security attribute */
960 HANDLE pipe_read, pipe_write;
Ray Donnelly267aa8b2012-11-29 01:18:50 +0000961 HANDLE stdout_handle, stderr_handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962 SECURITY_ATTRIBUTES sa;
963 STARTUPINFO startup;
964 PROCESS_INFORMATION pinfo;
965 char program_path[ MAX_PATH ];
966 int ret;
967
968 sa.nLength = sizeof(sa);
969 sa.lpSecurityDescriptor = NULL;
970 sa.bInheritHandle = TRUE;
971
972 /* create pipe, and ensure its read handle isn't inheritable */
973 ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
974 if (!ret) {
975 fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
976 return -1;
977 }
978
979 SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
980
Ray Donnelly267aa8b2012-11-29 01:18:50 +0000981 /* Some programs want to launch an adb command and collect its output by
982 * calling CreateProcess with inheritable stdout/stderr handles, then
983 * using read() to get its output. When this happens, the stdout/stderr
984 * handles passed to the adb client process will also be inheritable.
985 * When starting the adb server here, care must be taken to reset them
986 * to non-inheritable.
987 * Otherwise, something bad happens: even if the adb command completes,
988 * the calling process is stuck while read()-ing from the stdout/stderr
989 * descriptors, because they're connected to corresponding handles in the
990 * adb server process (even if the latter never uses/writes to them).
991 */
992 stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
993 stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
994 if (stdout_handle != INVALID_HANDLE_VALUE) {
995 SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
996 }
997 if (stderr_handle != INVALID_HANDLE_VALUE) {
998 SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
999 }
1000
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001001 ZeroMemory( &startup, sizeof(startup) );
1002 startup.cb = sizeof(startup);
1003 startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
1004 startup.hStdOutput = pipe_write;
1005 startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
1006 startup.dwFlags = STARTF_USESTDHANDLES;
1007
1008 ZeroMemory( &pinfo, sizeof(pinfo) );
1009
1010 /* get path of current program */
1011 GetModuleFileName( NULL, program_path, sizeof(program_path) );
Wenhao Lia09558c2013-11-13 16:23:37 +08001012 char args[64];
1013 snprintf(args, sizeof(args), "adb -P %d fork-server server", server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001014 ret = CreateProcess(
1015 program_path, /* program path */
Wenhao Lia09558c2013-11-13 16:23:37 +08001016 args,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001017 /* the fork-server argument will set the
1018 debug = 2 in the child */
1019 NULL, /* process handle is not inheritable */
1020 NULL, /* thread handle is not inheritable */
1021 TRUE, /* yes, inherit some handles */
1022 DETACHED_PROCESS, /* the new process doesn't have a console */
1023 NULL, /* use parent's environment block */
1024 NULL, /* use parent's starting directory */
1025 &startup, /* startup info, i.e. std handles */
1026 &pinfo );
1027
1028 CloseHandle( pipe_write );
1029
1030 if (!ret) {
1031 fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
1032 CloseHandle( pipe_read );
1033 return -1;
1034 }
1035
1036 CloseHandle( pinfo.hProcess );
1037 CloseHandle( pinfo.hThread );
1038
1039 /* wait for the "OK\n" message */
1040 {
1041 char temp[3];
1042 DWORD count;
1043
1044 ret = ReadFile( pipe_read, temp, 3, &count, NULL );
1045 CloseHandle( pipe_read );
1046 if ( !ret ) {
1047 fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
1048 return -1;
1049 }
1050 if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1051 fprintf(stderr, "ADB server didn't ACK\n" );
1052 return -1;
1053 }
1054 }
Yabin Cuie77b6a02014-11-11 09:24:11 -08001055#else /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001056 char path[PATH_MAX];
1057 int fd[2];
1058
1059 // set up a pipe so the child can tell us when it is ready.
1060 // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
1061 if (pipe(fd)) {
1062 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
1063 return -1;
1064 }
Alexey Tarasov31664102009-10-22 02:55:00 +11001065 get_my_path(path, PATH_MAX);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001066 pid_t pid = fork();
1067 if(pid < 0) return -1;
1068
1069 if (pid == 0) {
1070 // child side of the fork
1071
1072 // redirect stderr to the pipe
1073 // we use stderr instead of stdout due to stdout's buffering behavior.
1074 adb_close(fd[0]);
1075 dup2(fd[1], STDERR_FILENO);
1076 adb_close(fd[1]);
1077
Matt Gumbeld7b33082012-11-14 10:16:17 -08001078 char str_port[30];
1079 snprintf(str_port, sizeof(str_port), "%d", server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080 // child process
Matt Gumbeld7b33082012-11-14 10:16:17 -08001081 int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001082 // this should not return
1083 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
1084 } else {
1085 // parent side of the fork
1086
1087 char temp[3];
1088
1089 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
1090 // wait for the "OK\n" message
1091 adb_close(fd[1]);
1092 int ret = adb_read(fd[0], temp, 3);
JP Abgrall408fa572011-03-16 15:57:42 -07001093 int saved_errno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001094 adb_close(fd[0]);
1095 if (ret < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -07001096 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001097 return -1;
1098 }
1099 if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1100 fprintf(stderr, "ADB server didn't ACK\n" );
1101 return -1;
1102 }
1103
1104 setsid();
1105 }
Yabin Cuie77b6a02014-11-11 09:24:11 -08001106#endif /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001107 return 0;
1108}
Yabin Cuie77b6a02014-11-11 09:24:11 -08001109#endif /* ADB_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001110
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001111/* Constructs a local name of form tcp:port.
1112 * target_str points to the target string, it's content will be overwritten.
1113 * target_size is the capacity of the target string.
1114 * server_port is the port number to use for the local name.
1115 */
1116void build_local_name(char* target_str, size_t target_size, int server_port)
1117{
1118 snprintf(target_str, target_size, "tcp:%d", server_port);
1119}
1120
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001121#if !ADB_HOST
Nick Kralevich080427e2013-02-15 14:39:15 -08001122
1123static void drop_capabilities_bounding_set_if_needed() {
1124#ifdef ALLOW_ADBD_ROOT
1125 char value[PROPERTY_VALUE_MAX];
1126 property_get("ro.debuggable", value, "");
1127 if (strcmp(value, "1") == 0) {
1128 return;
1129 }
1130#endif
1131 int i;
1132 for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
Nick Kralevichca8e66a2013-04-18 12:20:02 -07001133 if (i == CAP_SETUID || i == CAP_SETGID) {
Nick Kralevich080427e2013-02-15 14:39:15 -08001134 // CAP_SETUID CAP_SETGID needed by /system/bin/run-as
1135 continue;
1136 }
1137 int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
1138
1139 // Some kernels don't have file capabilities compiled in, and
1140 // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically
1141 // die when we see such misconfigured kernels.
1142 if ((err < 0) && (errno != EINVAL)) {
1143 exit(1);
1144 }
1145 }
1146}
1147
Dan Pasanenb7d9ef32014-10-06 12:57:20 -05001148static bool should_drop_privileges() {
1149#if defined(ALLOW_ADBD_ROOT)
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001150 char value[PROPERTY_VALUE_MAX];
1151
Dan Pasanenb7d9ef32014-10-06 12:57:20 -05001152 // The emulator is never secure, so don't drop privileges there.
1153 // TODO: this seems like a bug --- shouldn't the emulator behave like a device?
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001154 property_get("ro.kernel.qemu", value, "");
Dan Pasanenb7d9ef32014-10-06 12:57:20 -05001155 if (strcmp(value, "1") == 0) {
1156 return false;
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001157 }
Dan Pasanenb7d9ef32014-10-06 12:57:20 -05001158
Dan Pasanenb7d9ef32014-10-06 12:57:20 -05001159 property_get("ro.secure", value, "1");
1160 bool ro_secure = (strcmp(value, "1") == 0);
1161
Elliott Hughes7cbda852015-02-18 16:57:30 -08001162 // Drop privileges if ro.secure is set...
1163 bool drop = ro_secure;
1164
Dan Pasanenb7d9ef32014-10-06 12:57:20 -05001165 property_get("ro.debuggable", value, "");
1166 bool ro_debuggable = (strcmp(value, "1") == 0);
Dan Pasanenb7d9ef32014-10-06 12:57:20 -05001167 property_get("service.adb.root", value, "");
1168 bool adb_root = (strcmp(value, "1") == 0);
1169 bool adb_unroot = (strcmp(value, "0") == 0);
Elliott Hughes7cbda852015-02-18 16:57:30 -08001170
1171 // ...except "adb root" lets you keep privileges in a debuggable build.
1172 if (ro_debuggable && adb_root) {
1173 drop = false;
Dan Pasanenb7d9ef32014-10-06 12:57:20 -05001174 }
1175
Elliott Hughes7cbda852015-02-18 16:57:30 -08001176 // ...and "adb unroot" lets you explicitly drop privileges.
1177 if (adb_unroot) {
1178 drop = true;
1179 }
1180
1181 return drop;
Dan Pasanenb7d9ef32014-10-06 12:57:20 -05001182#else
1183 return true; // "adb root" not allowed, always drop privileges.
Nick Kralevich5890fe32012-01-19 13:11:35 -08001184#endif /* ALLOW_ADBD_ROOT */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001185}
1186#endif /* !ADB_HOST */
1187
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001188int adb_main(int is_daemon, int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189{
1190#if !ADB_HOST
Mike Lockwood2f38b692009-08-24 15:58:40 -07001191 int port;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001192 char value[PROPERTY_VALUE_MAX];
Nick Kralevicheb68fa82012-04-02 13:00:35 -07001193
1194 umask(000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001195#endif
1196
1197 atexit(adb_cleanup);
Yabin Cuie77b6a02014-11-11 09:24:11 -08001198#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001199 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
Yabin Cuie77b6a02014-11-11 09:24:11 -08001200#else
JP Abgrall408fa572011-03-16 15:57:42 -07001201 // No SIGCHLD. Let the service subproc handle its children.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001202 signal(SIGPIPE, SIG_IGN);
1203#endif
1204
1205 init_transport_registration();
1206
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001207#if ADB_HOST
1208 HOST = 1;
JP Abgrall571c1362012-12-06 18:18:12 -08001209
1210#ifdef WORKAROUND_BUG6558362
1211 if(is_daemon) adb_set_affinity();
1212#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001213 usb_init();
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001214 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001215 adb_auth_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001216
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001217 char local_name[30];
1218 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001219 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001220 exit(1);
1221 }
1222#else
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001223 property_get("ro.adb.secure", value, "0");
1224 auth_enabled = !strcmp(value, "1");
1225 if (auth_enabled)
1226 adb_auth_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001227
Jeff Sharkeyd6d42862012-09-06 13:05:40 -07001228 // Our external storage path may be different than apps, since
1229 // we aren't able to bind mount after dropping root.
1230 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
1231 if (NULL != adb_external_storage) {
1232 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
1233 } else {
1234 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
1235 " unchanged.\n");
1236 }
1237
Nick Kraleviche5cbf4e2014-06-18 11:24:27 -07001238 /* add extra groups:
1239 ** AID_ADB to access the USB driver
1240 ** AID_LOG to read system logs (adb logcat)
1241 ** AID_INPUT to diagnose input issues (getevent)
Elliott Hughes187eade2015-02-03 11:59:22 -08001242 ** AID_INET to diagnose network issues (ping)
Nick Kraleviche5cbf4e2014-06-18 11:24:27 -07001243 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
1244 ** AID_SDCARD_R to allow reading from the SD card
1245 ** AID_SDCARD_RW to allow writing to the SD card
1246 ** AID_NET_BW_STATS to read out qtaguid statistics
1247 */
Ajay Dudani8432ddc2014-11-21 11:57:29 -08001248 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_NET_BT,
1249 AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
Nick Kraleviche5cbf4e2014-06-18 11:24:27 -07001250 AID_NET_BW_STATS };
1251 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
1252 exit(1);
1253 }
1254
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001255 /* don't listen on a port (default 5037) if running in secure mode */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001256 /* don't run as root if we are running in secure mode */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001257 if (should_drop_privileges()) {
Nick Kralevich080427e2013-02-15 14:39:15 -08001258 drop_capabilities_bounding_set_if_needed();
1259
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001260 /* then switch user and group to "shell" */
Nick Kralevich44db9902010-08-27 14:35:07 -07001261 if (setgid(AID_SHELL) != 0) {
1262 exit(1);
1263 }
1264 if (setuid(AID_SHELL) != 0) {
1265 exit(1);
1266 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001267
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001268 D("Local port disabled\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001269 } else {
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001270 char local_name[30];
Nick Kralevichd49aa252014-01-18 09:25:04 -08001271 if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
1272 // b/12587913: fix setcon to allow const pointers
1273 if (setcon((char *)root_seclabel) < 0) {
1274 exit(1);
1275 }
1276 }
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001277 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001278 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001279 exit(1);
1280 }
1281 }
1282
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001283 int usb = 0;
1284 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -07001285 // listen on USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001286 usb_init();
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001287 usb = 1;
1288 }
1289
1290 // If one of these properties is set, also listen on that port
1291 // If one of the properties isn't set and we couldn't listen on usb,
1292 // listen on the default port.
1293 property_get("service.adb.tcp.port", value, "");
1294 if (!value[0]) {
1295 property_get("persist.adb.tcp.port", value, "");
1296 }
1297 if (sscanf(value, "%d", &port) == 1 && port > 0) {
1298 printf("using port=%d\n", port);
1299 // listen on TCP port specified by service.adb.tcp.port property
1300 local_init(port);
1301 } else if (!usb) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -07001302 // listen on default port
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001303 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001304 }
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001305
JP Abgrall408fa572011-03-16 15:57:42 -07001306 D("adb_main(): pre init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001307 init_jdwp();
JP Abgrall408fa572011-03-16 15:57:42 -07001308 D("adb_main(): post init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001309#endif
1310
1311 if (is_daemon)
1312 {
1313 // inform our parent that we are up and running.
Yabin Cuie77b6a02014-11-11 09:24:11 -08001314#if defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001315 DWORD count;
1316 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
Yabin Cuie77b6a02014-11-11 09:24:11 -08001317#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001318 fprintf(stderr, "OK\n");
1319#endif
1320 start_logging();
1321 }
JP Abgrall408fa572011-03-16 15:57:42 -07001322 D("Event loop starting\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001323
1324 fdevent_loop();
1325
1326 usb_cleanup();
1327
1328 return 0;
1329}
1330
David 'Digit' Turner25258692013-03-21 21:07:42 +01001331// Try to handle a network forwarding request.
1332// This returns 1 on success, 0 on failure, and -1 to indicate this is not
1333// a forwarding-related request.
1334int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd)
1335{
1336 if (!strcmp(service, "list-forward")) {
1337 // Create the list of forward redirections.
1338 int buffer_size = format_listeners(NULL, 0);
1339 // Add one byte for the trailing zero.
1340 char* buffer = malloc(buffer_size + 1);
1341 if (buffer == NULL) {
1342 sendfailmsg(reply_fd, "not enough memory");
1343 return 1;
1344 }
1345 (void) format_listeners(buffer, buffer_size + 1);
1346#if ADB_HOST
1347 send_msg_with_okay(reply_fd, buffer, buffer_size);
1348#else
1349 send_msg_with_header(reply_fd, buffer, buffer_size);
1350#endif
1351 free(buffer);
1352 return 1;
1353 }
1354
1355 if (!strcmp(service, "killforward-all")) {
1356 remove_all_listeners();
1357#if ADB_HOST
1358 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
1359 adb_write(reply_fd, "OKAY", 4);
1360#endif
1361 adb_write(reply_fd, "OKAY", 4);
1362 return 1;
1363 }
1364
1365 if (!strncmp(service, "forward:",8) ||
1366 !strncmp(service, "killforward:",12)) {
1367 char *local, *remote, *err;
1368 int r;
1369 atransport *transport;
1370
1371 int createForward = strncmp(service, "kill", 4);
1372 int no_rebind = 0;
1373
1374 local = strchr(service, ':') + 1;
1375
1376 // Handle forward:norebind:<local>... here
1377 if (createForward && !strncmp(local, "norebind:", 9)) {
1378 no_rebind = 1;
1379 local = strchr(local, ':') + 1;
1380 }
1381
1382 remote = strchr(local,';');
1383
1384 if (createForward) {
1385 // Check forward: parameter format: '<local>;<remote>'
1386 if(remote == 0) {
1387 sendfailmsg(reply_fd, "malformed forward spec");
1388 return 1;
1389 }
1390
1391 *remote++ = 0;
1392 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')) {
1393 sendfailmsg(reply_fd, "malformed forward spec");
1394 return 1;
1395 }
1396 } else {
1397 // Check killforward: parameter format: '<local>'
1398 if (local[0] == 0) {
1399 sendfailmsg(reply_fd, "malformed forward spec");
1400 return 1;
1401 }
1402 }
1403
1404 transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
1405 if (!transport) {
1406 sendfailmsg(reply_fd, err);
1407 return 1;
1408 }
1409
1410 if (createForward) {
1411 r = install_listener(local, remote, transport, no_rebind);
1412 } else {
1413 r = remove_listener(local, transport);
1414 }
1415 if(r == 0) {
1416#if ADB_HOST
1417 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
1418 writex(reply_fd, "OKAY", 4);
1419#endif
1420 writex(reply_fd, "OKAY", 4);
1421 return 1;
1422 }
1423
1424 if (createForward) {
1425 const char* message;
1426 switch (r) {
1427 case INSTALL_STATUS_CANNOT_BIND:
1428 message = "cannot bind to socket";
1429 break;
1430 case INSTALL_STATUS_CANNOT_REBIND:
1431 message = "cannot rebind existing socket";
1432 break;
1433 default:
1434 message = "internal error";
1435 }
1436 sendfailmsg(reply_fd, message);
1437 } else {
1438 sendfailmsg(reply_fd, "cannot remove listener");
1439 }
1440 return 1;
1441 }
1442 return 0;
1443}
1444
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001445int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
1446{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001447 if(!strcmp(service, "kill")) {
1448 fprintf(stderr,"adb server killed by remote request\n");
1449 fflush(stdout);
1450 adb_write(reply_fd, "OKAY", 4);
1451 usb_cleanup();
1452 exit(0);
1453 }
1454
1455#if ADB_HOST
Chih-Hung Hsiehf787b382014-09-05 15:38:15 -07001456 atransport *transport = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001457 // "transport:" is used for switching transport with a specified serial number
1458 // "transport-usb:" is used for switching transport to the only USB transport
1459 // "transport-local:" is used for switching transport to the only local transport
1460 // "transport-any:" is used for switching transport to the only transport
1461 if (!strncmp(service, "transport", strlen("transport"))) {
1462 char* error_string = "unknown failure";
1463 transport_type type = kTransportAny;
1464
1465 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
1466 type = kTransportUsb;
1467 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
1468 type = kTransportLocal;
1469 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
1470 type = kTransportAny;
1471 } else if (!strncmp(service, "transport:", strlen("transport:"))) {
1472 service += strlen("transport:");
Tom Marlin3175c8e2011-07-27 12:56:14 -05001473 serial = service;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001474 }
1475
1476 transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
1477
1478 if (transport) {
1479 s->transport = transport;
1480 adb_write(reply_fd, "OKAY", 4);
1481 } else {
1482 sendfailmsg(reply_fd, error_string);
1483 }
1484 return 1;
1485 }
1486
1487 // return a list of all connected devices
Scott Andersone109d262012-04-20 11:21:14 -07001488 if (!strncmp(service, "devices", 7)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001489 char buffer[4096];
Scott Andersone109d262012-04-20 11:21:14 -07001490 int use_long = !strcmp(service+7, "-l");
1491 if (use_long || service[7] == 0) {
Scott Andersone109d262012-04-20 11:21:14 -07001492 memset(buffer, 0, sizeof(buffer));
1493 D("Getting device list \n");
1494 list_transports(buffer, sizeof(buffer), use_long);
Scott Andersone109d262012-04-20 11:21:14 -07001495 D("Wrote device list \n");
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001496 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Scott Andersone109d262012-04-20 11:21:14 -07001497 return 0;
1498 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001499 }
1500
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001501 // remove TCP transport
1502 if (!strncmp(service, "disconnect:", 11)) {
1503 char buffer[4096];
1504 memset(buffer, 0, sizeof(buffer));
1505 char* serial = service + 11;
Mike Lockwoodcbbe79ad2010-05-24 10:44:35 -04001506 if (serial[0] == 0) {
1507 // disconnect from all TCP devices
1508 unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001509 } else {
Mike Lockwoodcbbe79ad2010-05-24 10:44:35 -04001510 char hostbuf[100];
1511 // assume port 5555 if no port is specified
1512 if (!strchr(serial, ':')) {
1513 snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
1514 serial = hostbuf;
1515 }
1516 atransport *t = find_transport(serial);
1517
1518 if (t) {
1519 unregister_transport(t);
1520 } else {
1521 snprintf(buffer, sizeof(buffer), "No such device %s", serial);
1522 }
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001523 }
1524
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001525 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Mike Lockwood2f38b692009-08-24 15:58:40 -07001526 return 0;
1527 }
1528
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001529 // returns our value for ADB_SERVER_VERSION
1530 if (!strcmp(service, "version")) {
1531 char version[12];
1532 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001533 send_msg_with_okay(reply_fd, version, strlen(version));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001534 return 0;
1535 }
1536
1537 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
1538 char *out = "unknown";
1539 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1540 if (transport && transport->serial) {
1541 out = transport->serial;
1542 }
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001543 send_msg_with_okay(reply_fd, out, strlen(out));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001544 return 0;
1545 }
Scott Andersone109d262012-04-20 11:21:14 -07001546 if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
1547 char *out = "unknown";
1548 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1549 if (transport && transport->devpath) {
1550 out = transport->devpath;
1551 }
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001552 send_msg_with_okay(reply_fd, out, strlen(out));
Scott Andersone109d262012-04-20 11:21:14 -07001553 return 0;
1554 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001555 // indicates a new emulator instance has started
1556 if (!strncmp(service,"emulator:",9)) {
1557 int port = atoi(service+9);
1558 local_connect(port);
1559 /* we don't even need to send a reply */
1560 return 0;
1561 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001562
1563 if(!strncmp(service,"get-state",strlen("get-state"))) {
1564 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1565 char *state = connection_state_name(transport);
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001566 send_msg_with_okay(reply_fd, state, strlen(state));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001567 return 0;
1568 }
Simon Yedc22c3c2014-07-14 17:23:06 -07001569#endif // ADB_HOST
1570
1571 int ret = handle_forward_request(service, ttype, serial, reply_fd);
1572 if (ret >= 0)
1573 return ret - 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001574 return -1;
1575}
1576
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001577int main(int argc, char **argv)
1578{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001579#if ADB_HOST
1580 adb_sysdeps_init();
JP Abgrall408fa572011-03-16 15:57:42 -07001581 adb_trace_init();
1582 D("Handling commandline()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001583 return adb_commandline(argc - 1, argv + 1);
1584#else
Vladimir Chtchetkine28781b02012-02-27 10:41:53 -08001585 /* If adbd runs inside the emulator this will enable adb tracing via
1586 * adb-debug qemud service in the emulator. */
1587 adb_qemu_trace_init();
Nick Kralevichd49aa252014-01-18 09:25:04 -08001588 while(1) {
1589 int c;
1590 int option_index = 0;
1591 static struct option opts[] = {
1592 {"root_seclabel", required_argument, 0, 's' },
1593 {"device_banner", required_argument, 0, 'b' }
1594 };
1595 c = getopt_long(argc, argv, "", opts, &option_index);
1596 if (c == -1)
1597 break;
1598 switch (c) {
1599 case 's':
1600 root_seclabel = optarg;
1601 break;
1602 case 'b':
1603 adb_device_banner = optarg;
1604 break;
1605 default:
1606 break;
1607 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001608 }
Mike Lockwood1f546e62009-05-25 18:17:55 -04001609
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001610 start_device_log();
JP Abgrall408fa572011-03-16 15:57:42 -07001611 D("Handling main()\n");
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001612 return adb_main(0, DEFAULT_ADB_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001613#endif
1614}