blob: 0e41b34c632778f63af38be7c8718a0e4929a1de [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"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Scott Andersone82c2db2012-05-25 14:10:02 -070034#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
35
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#if !ADB_HOST
Nick Kralevich893a4a42013-05-23 09:54:13 -070037#include <cutils/properties.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#include <private/android_filesystem_config.h>
Nick Kraleviche2864bf2013-02-28 14:12:58 -080039#include <sys/capability.h>
Mike Lockwood5f4b0512009-08-04 20:37:51 -040040#include <linux/prctl.h>
Jeff Sharkey885342a2012-08-14 21:00:22 -070041#include <sys/mount.h>
Nick Kralevichd49aa252014-01-18 09:25:04 -080042#include <getopt.h>
43#include <selinux/selinux.h>
Xavier Ducroheta09fbd12009-05-20 17:33:53 -070044#else
45#include "usb_vendors.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#endif
47
JP Abgrall408fa572011-03-16 15:57:42 -070048#if ADB_TRACE
49ADB_MUTEX_DEFINE( D_lock );
50#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
52int HOST = 0;
Matt Gumbeld7b33082012-11-14 10:16:17 -080053int gListenAll = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070055static int auth_enabled = 0;
56
Scott Andersone82c2db2012-05-25 14:10:02 -070057#if !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058static const char *adb_device_banner = "device";
Nick Kralevichd49aa252014-01-18 09:25:04 -080059static const char *root_seclabel = NULL;
Scott Andersone82c2db2012-05-25 14:10:02 -070060#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
62void fatal(const char *fmt, ...)
63{
64 va_list ap;
65 va_start(ap, fmt);
66 fprintf(stderr, "error: ");
67 vfprintf(stderr, fmt, ap);
68 fprintf(stderr, "\n");
69 va_end(ap);
70 exit(-1);
71}
72
73void fatal_errno(const char *fmt, ...)
74{
75 va_list ap;
76 va_start(ap, fmt);
77 fprintf(stderr, "error: %s: ", strerror(errno));
78 vfprintf(stderr, fmt, ap);
79 fprintf(stderr, "\n");
80 va_end(ap);
81 exit(-1);
82}
83
84int adb_trace_mask;
85
86/* read a comma/space/colum/semi-column separated list of tags
87 * from the ADB_TRACE environment variable and build the trace
88 * mask from it. note that '1' and 'all' are special cases to
89 * enable all tracing
90 */
91void adb_trace_init(void)
92{
93 const char* p = getenv("ADB_TRACE");
94 const char* q;
95
96 static const struct {
97 const char* tag;
98 int flag;
99 } tags[] = {
100 { "1", 0 },
101 { "all", 0 },
102 { "adb", TRACE_ADB },
103 { "sockets", TRACE_SOCKETS },
104 { "packets", TRACE_PACKETS },
105 { "rwx", TRACE_RWX },
106 { "usb", TRACE_USB },
107 { "sync", TRACE_SYNC },
108 { "sysdeps", TRACE_SYSDEPS },
109 { "transport", TRACE_TRANSPORT },
110 { "jdwp", TRACE_JDWP },
JP Abgrall408fa572011-03-16 15:57:42 -0700111 { "services", TRACE_SERVICES },
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700112 { "auth", TRACE_AUTH },
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 { NULL, 0 }
114 };
115
116 if (p == NULL)
117 return;
118
119 /* use a comma/column/semi-colum/space separated list */
120 while (*p) {
121 int len, tagn;
122
123 q = strpbrk(p, " ,:;");
124 if (q == NULL) {
125 q = p + strlen(p);
126 }
127 len = q - p;
128
129 for (tagn = 0; tags[tagn].tag != NULL; tagn++)
130 {
131 int taglen = strlen(tags[tagn].tag);
132
133 if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
134 {
135 int flag = tags[tagn].flag;
136 if (flag == 0) {
137 adb_trace_mask = ~0;
138 return;
139 }
140 adb_trace_mask |= (1 << flag);
141 break;
142 }
143 }
144 p = q;
145 if (*p)
146 p++;
147 }
148}
149
Vladimir Chtchetkine28781b02012-02-27 10:41:53 -0800150#if !ADB_HOST
151/*
152 * Implements ADB tracing inside the emulator.
153 */
154
155#include <stdarg.h>
156
157/*
158 * Redefine open and write for qemu_pipe.h that contains inlined references
159 * to those routines. We will redifine them back after qemu_pipe.h inclusion.
160 */
161
162#undef open
163#undef write
164#define open adb_open
165#define write adb_write
166#include <hardware/qemu_pipe.h>
167#undef open
168#undef write
169#define open ___xxx_open
170#define write ___xxx_write
171
172/* A handle to adb-debug qemud service in the emulator. */
173int adb_debug_qemu = -1;
174
175/* Initializes connection with the adb-debug qemud service in the emulator. */
176static int adb_qemu_trace_init(void)
177{
178 char con_name[32];
179
180 if (adb_debug_qemu >= 0) {
181 return 0;
182 }
183
184 /* adb debugging QEMUD service connection request. */
185 snprintf(con_name, sizeof(con_name), "qemud:adb-debug");
186 adb_debug_qemu = qemu_pipe_open(con_name);
187 return (adb_debug_qemu >= 0) ? 0 : -1;
188}
189
190void adb_qemu_trace(const char* fmt, ...)
191{
192 va_list args;
193 va_start(args, fmt);
194 char msg[1024];
195
196 if (adb_debug_qemu >= 0) {
197 vsnprintf(msg, sizeof(msg), fmt, args);
198 adb_write(adb_debug_qemu, msg, strlen(msg));
199 }
200}
201#endif /* !ADB_HOST */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202
203apacket *get_apacket(void)
204{
205 apacket *p = malloc(sizeof(apacket));
206 if(p == 0) fatal("failed to allocate an apacket");
207 memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
208 return p;
209}
210
211void put_apacket(apacket *p)
212{
213 free(p);
214}
215
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700216void handle_online(atransport *t)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217{
218 D("adb: online\n");
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700219 t->online = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220}
221
222void handle_offline(atransport *t)
223{
224 D("adb: offline\n");
225 //Close the associated usb
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700226 t->online = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 run_transport_disconnects(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228}
229
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700230#if DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231#define DUMPMAX 32
232void print_packet(const char *label, apacket *p)
233{
234 char *tag;
235 char *x;
236 unsigned count;
237
238 switch(p->msg.command){
239 case A_SYNC: tag = "SYNC"; break;
240 case A_CNXN: tag = "CNXN" ; break;
241 case A_OPEN: tag = "OPEN"; break;
242 case A_OKAY: tag = "OKAY"; break;
243 case A_CLSE: tag = "CLSE"; break;
244 case A_WRTE: tag = "WRTE"; break;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700245 case A_AUTH: tag = "AUTH"; break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 default: tag = "????"; break;
247 }
248
249 fprintf(stderr, "%s: %s %08x %08x %04x \"",
250 label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
251 count = p->msg.data_length;
252 x = (char*) p->data;
253 if(count > DUMPMAX) {
254 count = DUMPMAX;
255 tag = "\n";
256 } else {
257 tag = "\"\n";
258 }
259 while(count-- > 0){
260 if((*x >= ' ') && (*x < 127)) {
261 fputc(*x, stderr);
262 } else {
263 fputc('.', stderr);
264 }
265 x++;
266 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700267 fputs(tag, stderr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268}
269#endif
270
271static void send_ready(unsigned local, unsigned remote, atransport *t)
272{
273 D("Calling send_ready \n");
274 apacket *p = get_apacket();
275 p->msg.command = A_OKAY;
276 p->msg.arg0 = local;
277 p->msg.arg1 = remote;
278 send_packet(p, t);
279}
280
281static void send_close(unsigned local, unsigned remote, atransport *t)
282{
283 D("Calling send_close \n");
284 apacket *p = get_apacket();
285 p->msg.command = A_CLSE;
286 p->msg.arg0 = local;
287 p->msg.arg1 = remote;
288 send_packet(p, t);
289}
290
Scott Andersone82c2db2012-05-25 14:10:02 -0700291static size_t fill_connect_data(char *buf, size_t bufsize)
292{
293#if ADB_HOST
294 return snprintf(buf, bufsize, "host::") + 1;
295#else
296 static const char *cnxn_props[] = {
297 "ro.product.name",
298 "ro.product.model",
299 "ro.product.device",
300 };
301 static const int num_cnxn_props = ARRAY_SIZE(cnxn_props);
302 int i;
303 size_t remaining = bufsize;
304 size_t len;
305
306 len = snprintf(buf, remaining, "%s::", adb_device_banner);
307 remaining -= len;
308 buf += len;
309 for (i = 0; i < num_cnxn_props; i++) {
310 char value[PROPERTY_VALUE_MAX];
311 property_get(cnxn_props[i], value, "");
312 len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value);
313 remaining -= len;
314 buf += len;
315 }
316
317 return bufsize - remaining + 1;
318#endif
319}
320
Snild Dolkow2264e7c2014-01-30 10:08:38 +0100321static void send_msg_with_okay(int fd, char* msg, size_t msglen) {
322 char header[9];
323 if (msglen > 0xffff)
324 msglen = 0xffff;
325 snprintf(header, sizeof(header), "OKAY%04x", (unsigned)msglen);
326 writex(fd, header, 8);
327 writex(fd, msg, msglen);
328}
329
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330static void send_connect(atransport *t)
331{
332 D("Calling send_connect \n");
333 apacket *cp = get_apacket();
334 cp->msg.command = A_CNXN;
335 cp->msg.arg0 = A_VERSION;
336 cp->msg.arg1 = MAX_PAYLOAD;
Scott Andersone82c2db2012-05-25 14:10:02 -0700337 cp->msg.data_length = fill_connect_data((char *)cp->data,
338 sizeof(cp->data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 send_packet(cp, t);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700340}
341
Benoit Goby045a4a92013-01-15 19:59:14 -0800342void send_auth_request(atransport *t)
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700343{
344 D("Calling send_auth_request\n");
345 apacket *p;
346 int ret;
347
348 ret = adb_auth_generate_token(t->token, sizeof(t->token));
349 if (ret != sizeof(t->token)) {
350 D("Error generating token ret=%d\n", ret);
351 return;
352 }
353
354 p = get_apacket();
355 memcpy(p->data, t->token, ret);
356 p->msg.command = A_AUTH;
357 p->msg.arg0 = ADB_AUTH_TOKEN;
358 p->msg.data_length = ret;
359 send_packet(p, t);
360}
361
362static void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
363{
364 D("Calling send_auth_response\n");
365 apacket *p = get_apacket();
366 int ret;
367
368 ret = adb_auth_sign(t->key, token, token_size, p->data);
369 if (!ret) {
370 D("Error signing the token\n");
371 put_apacket(p);
372 return;
373 }
374
375 p->msg.command = A_AUTH;
376 p->msg.arg0 = ADB_AUTH_SIGNATURE;
377 p->msg.data_length = ret;
378 send_packet(p, t);
379}
380
381static void send_auth_publickey(atransport *t)
382{
383 D("Calling send_auth_publickey\n");
384 apacket *p = get_apacket();
385 int ret;
386
387 ret = adb_auth_get_userkey(p->data, sizeof(p->data));
388 if (!ret) {
389 D("Failed to get user public key\n");
390 put_apacket(p);
391 return;
392 }
393
394 p->msg.command = A_AUTH;
395 p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
396 p->msg.data_length = ret;
397 send_packet(p, t);
398}
399
400void adb_auth_verified(atransport *t)
401{
402 handle_online(t);
403 send_connect(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404}
405
406static char *connection_state_name(atransport *t)
407{
408 if (t == NULL) {
409 return "unknown";
410 }
411
412 switch(t->connection_state) {
413 case CS_BOOTLOADER:
414 return "bootloader";
415 case CS_DEVICE:
416 return "device";
trevda5ad5392013-04-17 14:34:23 +0100417 case CS_RECOVERY:
418 return "recovery";
419 case CS_SIDELOAD:
420 return "sideload";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 case CS_OFFLINE:
422 return "offline";
Benoit Goby77e8e582013-01-15 12:36:47 -0800423 case CS_UNAUTHORIZED:
424 return "unauthorized";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 default:
426 return "unknown";
427 }
428}
429
Scott Andersone82c2db2012-05-25 14:10:02 -0700430/* qual_overwrite is used to overwrite a qualifier string. dst is a
431 * pointer to a char pointer. It is assumed that if *dst is non-NULL, it
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700432 * was malloc'ed and needs to freed. *dst will be set to a dup of src.
Scott Andersone82c2db2012-05-25 14:10:02 -0700433 */
434static void qual_overwrite(char **dst, const char *src)
435{
436 if (!dst)
437 return;
438
439 free(*dst);
440 *dst = NULL;
441
442 if (!src || !*src)
443 return;
444
445 *dst = strdup(src);
446}
447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448void parse_banner(char *banner, atransport *t)
449{
Scott Andersone82c2db2012-05-25 14:10:02 -0700450 static const char *prop_seps = ";";
451 static const char key_val_sep = '=';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700452 char *cp;
453 char *type;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454
455 D("parse_banner: %s\n", banner);
456 type = banner;
Scott Andersone82c2db2012-05-25 14:10:02 -0700457 cp = strchr(type, ':');
458 if (cp) {
459 *cp++ = 0;
460 /* Nothing is done with second field. */
461 cp = strchr(cp, ':');
462 if (cp) {
463 char *save;
464 char *key;
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700465 key = adb_strtok_r(cp + 1, prop_seps, &save);
Scott Andersone82c2db2012-05-25 14:10:02 -0700466 while (key) {
467 cp = strchr(key, key_val_sep);
468 if (cp) {
469 *cp++ = '\0';
470 if (!strcmp(key, "ro.product.name"))
471 qual_overwrite(&t->product, cp);
472 else if (!strcmp(key, "ro.product.model"))
473 qual_overwrite(&t->model, cp);
474 else if (!strcmp(key, "ro.product.device"))
475 qual_overwrite(&t->device, cp);
476 }
Scott Anderson1b7a7e82012-06-05 17:54:27 -0700477 key = adb_strtok_r(NULL, prop_seps, &save);
Scott Andersone82c2db2012-05-25 14:10:02 -0700478 }
479 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 }
481
482 if(!strcmp(type, "bootloader")){
483 D("setting connection_state to CS_BOOTLOADER\n");
484 t->connection_state = CS_BOOTLOADER;
485 update_transports();
486 return;
487 }
488
489 if(!strcmp(type, "device")) {
490 D("setting connection_state to CS_DEVICE\n");
491 t->connection_state = CS_DEVICE;
492 update_transports();
493 return;
494 }
495
496 if(!strcmp(type, "recovery")) {
497 D("setting connection_state to CS_RECOVERY\n");
498 t->connection_state = CS_RECOVERY;
499 update_transports();
500 return;
501 }
502
Doug Zongker447f0612012-01-09 14:54:53 -0800503 if(!strcmp(type, "sideload")) {
504 D("setting connection_state to CS_SIDELOAD\n");
505 t->connection_state = CS_SIDELOAD;
506 update_transports();
507 return;
508 }
509
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 t->connection_state = CS_HOST;
511}
512
513void handle_packet(apacket *p, atransport *t)
514{
515 asocket *s;
516
Viral Mehta899913f2010-06-16 18:41:28 +0530517 D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
518 ((char*) (&(p->msg.command)))[1],
519 ((char*) (&(p->msg.command)))[2],
520 ((char*) (&(p->msg.command)))[3]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 print_packet("recv", p);
522
523 switch(p->msg.command){
524 case A_SYNC:
525 if(p->msg.arg0){
526 send_packet(p, t);
527 if(HOST) send_connect(t);
528 } else {
529 t->connection_state = CS_OFFLINE;
530 handle_offline(t);
531 send_packet(p, t);
532 }
533 return;
534
535 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
536 /* XXX verify version, etc */
537 if(t->connection_state != CS_OFFLINE) {
538 t->connection_state = CS_OFFLINE;
539 handle_offline(t);
540 }
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 parse_banner((char*) p->data, t);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700543
544 if (HOST || !auth_enabled) {
545 handle_online(t);
546 if(!HOST) send_connect(t);
547 } else {
548 send_auth_request(t);
549 }
550 break;
551
552 case A_AUTH:
553 if (p->msg.arg0 == ADB_AUTH_TOKEN) {
Benoit Goby77e8e582013-01-15 12:36:47 -0800554 t->connection_state = CS_UNAUTHORIZED;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700555 t->key = adb_auth_nextkey(t->key);
556 if (t->key) {
557 send_auth_response(p->data, p->msg.data_length, t);
558 } else {
559 /* No more private keys to try, send the public key */
560 send_auth_publickey(t);
561 }
562 } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
563 if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
564 adb_auth_verified(t);
565 t->failed_auth_attempts = 0;
566 } else {
567 if (t->failed_auth_attempts++ > 10)
568 adb_sleep_ms(1000);
569 send_auth_request(t);
570 }
571 } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
572 adb_auth_confirm_key(p->data, p->msg.data_length, t);
573 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574 break;
575
576 case A_OPEN: /* OPEN(local-id, 0, "destination") */
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100577 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800578 char *name = (char*) p->data;
579 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
580 s = create_local_service_socket(name);
581 if(s == 0) {
582 send_close(0, p->msg.arg0, t);
583 } else {
584 s->peer = create_remote_socket(p->msg.arg0, t);
585 s->peer->peer = s;
586 send_ready(s->id, s->peer->id, t);
587 s->ready(s);
588 }
589 }
590 break;
591
592 case A_OKAY: /* READY(local-id, remote-id, "") */
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100593 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
594 if((s = find_local_socket(p->msg.arg1, 0))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 if(s->peer == 0) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100596 /* On first READY message, create the connection. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 s->peer = create_remote_socket(p->msg.arg0, t);
598 s->peer->peer = s;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100599 s->ready(s);
600 } else if (s->peer->id == p->msg.arg0) {
601 /* Other READY messages must use the same local-id */
602 s->ready(s);
603 } else {
604 D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s\n",
605 p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607 }
608 }
609 break;
610
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100611 case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */
612 if (t->online && p->msg.arg1 != 0) {
613 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
614 /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
615 * a failed OPEN only. However, due to a bug in previous ADB
616 * versions, CLOSE(0, remote-id, "") was also used for normal
617 * CLOSE() operations.
618 *
619 * This is bad because it means a compromised adbd could
620 * send packets to close connections between the host and
621 * other devices. To avoid this, only allow this if the local
622 * socket has a peer on the same transport.
623 */
624 if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
625 D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s\n",
626 p->msg.arg1, t->serial, s->peer->transport->serial);
627 } else {
628 s->close(s);
629 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 }
631 }
632 break;
633
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100634 case A_WRTE: /* WRITE(local-id, remote-id, <data>) */
635 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
636 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800637 unsigned rid = p->msg.arg0;
638 p->len = p->msg.data_length;
639
640 if(s->enqueue(s, p) == 0) {
641 D("Enqueue the socket\n");
642 send_ready(s->id, rid, t);
643 }
644 return;
645 }
646 }
647 break;
648
649 default:
650 printf("handle_packet: what is %08x?!\n", p->msg.command);
651 }
652
653 put_apacket(p);
654}
655
656alistener listener_list = {
657 .next = &listener_list,
658 .prev = &listener_list,
659};
660
661static void ss_listener_event_func(int _fd, unsigned ev, void *_l)
662{
663 asocket *s;
664
665 if(ev & FDE_READ) {
666 struct sockaddr addr;
667 socklen_t alen;
668 int fd;
669
670 alen = sizeof(addr);
671 fd = adb_socket_accept(_fd, &addr, &alen);
672 if(fd < 0) return;
673
674 adb_socket_setbufsize(fd, CHUNK_SIZE);
675
676 s = create_local_socket(fd);
677 if(s) {
678 connect_to_smartsocket(s);
679 return;
680 }
681
682 adb_close(fd);
683 }
684}
685
686static void listener_event_func(int _fd, unsigned ev, void *_l)
687{
688 alistener *l = _l;
689 asocket *s;
690
691 if(ev & FDE_READ) {
692 struct sockaddr addr;
693 socklen_t alen;
694 int fd;
695
696 alen = sizeof(addr);
697 fd = adb_socket_accept(_fd, &addr, &alen);
698 if(fd < 0) return;
699
700 s = create_local_socket(fd);
701 if(s) {
702 s->transport = l->transport;
703 connect_to_remote(s, l->connect_to);
704 return;
705 }
706
707 adb_close(fd);
708 }
709}
710
711static void free_listener(alistener* l)
712{
713 if (l->next) {
714 l->next->prev = l->prev;
715 l->prev->next = l->next;
716 l->next = l->prev = l;
717 }
718
719 // closes the corresponding fd
720 fdevent_remove(&l->fde);
721
722 if (l->local_name)
723 free((char*)l->local_name);
724
725 if (l->connect_to)
726 free((char*)l->connect_to);
727
728 if (l->transport) {
729 remove_transport_disconnect(l->transport, &l->disconnect);
730 }
731 free(l);
732}
733
734static void listener_disconnect(void* _l, atransport* t)
735{
736 alistener* l = _l;
737
738 free_listener(l);
739}
740
741int local_name_to_fd(const char *name)
742{
743 int port;
744
745 if(!strncmp("tcp:", name, 4)){
746 int ret;
747 port = atoi(name + 4);
Matt Gumbeld7b33082012-11-14 10:16:17 -0800748
749 if (gListenAll > 0) {
750 ret = socket_inaddr_any_server(port, SOCK_STREAM);
751 } else {
752 ret = socket_loopback_server(port, SOCK_STREAM);
753 }
754
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755 return ret;
756 }
757#ifndef HAVE_WIN32_IPC /* no Unix-domain sockets on Win32 */
758 // It's non-sensical to support the "reserved" space on the adb host side
759 if(!strncmp(name, "local:", 6)) {
760 return socket_local_server(name + 6,
761 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
762 } else if(!strncmp(name, "localabstract:", 14)) {
763 return socket_local_server(name + 14,
764 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
765 } else if(!strncmp(name, "localfilesystem:", 16)) {
766 return socket_local_server(name + 16,
767 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
768 }
769
770#endif
771 printf("unknown local portname '%s'\n", name);
772 return -1;
773}
774
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100775// Write a single line describing a listener to a user-provided buffer.
776// Appends a trailing zero, even in case of truncation, but the function
777// returns the full line length.
778// If |buffer| is NULL, does not write but returns required size.
779static int format_listener(alistener* l, char* buffer, size_t buffer_len) {
780 // Format is simply:
781 //
782 // <device-serial> " " <local-name> " " <remote-name> "\n"
783 //
784 int local_len = strlen(l->local_name);
785 int connect_len = strlen(l->connect_to);
786 int serial_len = strlen(l->transport->serial);
787
788 if (buffer != NULL) {
789 snprintf(buffer, buffer_len, "%s %s %s\n",
790 l->transport->serial, l->local_name, l->connect_to);
791 }
792 // NOTE: snprintf() on Windows returns -1 in case of truncation, so
793 // return the computed line length instead.
794 return local_len + connect_len + serial_len + 3;
795}
796
797// Write the list of current listeners (network redirections) into a
798// user-provided buffer. Appends a trailing zero, even in case of
799// trunctaion, but return the full size in bytes.
800// If |buffer| is NULL, does not write but returns required size.
801static int format_listeners(char* buf, size_t buflen)
802{
803 alistener* l;
804 int result = 0;
805 for (l = listener_list.next; l != &listener_list; l = l->next) {
806 // Ignore special listeners like those for *smartsocket*
807 if (l->connect_to[0] == '*')
808 continue;
809 int len = format_listener(l, buf, buflen);
810 // Ensure there is space for the trailing zero.
811 result += len;
812 if (buf != NULL) {
813 buf += len;
814 buflen -= len;
815 if (buflen <= 0)
816 break;
817 }
818 }
819 return result;
820}
821
822static int remove_listener(const char *local_name, atransport* transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800823{
824 alistener *l;
825
826 for (l = listener_list.next; l != &listener_list; l = l->next) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100827 if (!strcmp(local_name, l->local_name)) {
828 listener_disconnect(l, l->transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829 return 0;
830 }
831 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800832 return -1;
833}
834
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100835static void remove_all_listeners(void)
836{
837 alistener *l, *l_next;
838 for (l = listener_list.next; l != &listener_list; l = l_next) {
839 l_next = l->next;
840 // Never remove smart sockets.
841 if (l->connect_to[0] == '*')
842 continue;
843 listener_disconnect(l, l->transport);
844 }
845}
846
847// error/status codes for install_listener.
848typedef enum {
849 INSTALL_STATUS_OK = 0,
850 INSTALL_STATUS_INTERNAL_ERROR = -1,
851 INSTALL_STATUS_CANNOT_BIND = -2,
852 INSTALL_STATUS_CANNOT_REBIND = -3,
853} install_status_t;
854
855static install_status_t install_listener(const char *local_name,
856 const char *connect_to,
857 atransport* transport,
858 int no_rebind)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800859{
860 alistener *l;
861
862 //printf("install_listener('%s','%s')\n", local_name, connect_to);
863
864 for(l = listener_list.next; l != &listener_list; l = l->next){
865 if(strcmp(local_name, l->local_name) == 0) {
866 char *cto;
867
868 /* can't repurpose a smartsocket */
869 if(l->connect_to[0] == '*') {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100870 return INSTALL_STATUS_INTERNAL_ERROR;
871 }
872
873 /* can't repurpose a listener if 'no_rebind' is true */
874 if (no_rebind) {
875 return INSTALL_STATUS_CANNOT_REBIND;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800876 }
877
878 cto = strdup(connect_to);
879 if(cto == 0) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100880 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800881 }
882
883 //printf("rebinding '%s' to '%s'\n", local_name, connect_to);
884 free((void*) l->connect_to);
885 l->connect_to = cto;
886 if (l->transport != transport) {
887 remove_transport_disconnect(l->transport, &l->disconnect);
888 l->transport = transport;
889 add_transport_disconnect(l->transport, &l->disconnect);
890 }
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100891 return INSTALL_STATUS_OK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800892 }
893 }
894
895 if((l = calloc(1, sizeof(alistener))) == 0) goto nomem;
896 if((l->local_name = strdup(local_name)) == 0) goto nomem;
897 if((l->connect_to = strdup(connect_to)) == 0) goto nomem;
898
899
900 l->fd = local_name_to_fd(local_name);
901 if(l->fd < 0) {
902 free((void*) l->local_name);
903 free((void*) l->connect_to);
904 free(l);
905 printf("cannot bind '%s'\n", local_name);
906 return -2;
907 }
908
909 close_on_exec(l->fd);
910 if(!strcmp(l->connect_to, "*smartsocket*")) {
911 fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);
912 } else {
913 fdevent_install(&l->fde, l->fd, listener_event_func, l);
914 }
915 fdevent_set(&l->fde, FDE_READ);
916
917 l->next = &listener_list;
918 l->prev = listener_list.prev;
919 l->next->prev = l;
920 l->prev->next = l;
921 l->transport = transport;
922
923 if (transport) {
924 l->disconnect.opaque = l;
925 l->disconnect.func = listener_disconnect;
926 add_transport_disconnect(transport, &l->disconnect);
927 }
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100928 return INSTALL_STATUS_OK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800929
930nomem:
931 fatal("cannot allocate listener");
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +0100932 return INSTALL_STATUS_INTERNAL_ERROR;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800933}
934
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800935#ifdef HAVE_WIN32_PROC
936static BOOL WINAPI ctrlc_handler(DWORD type)
937{
938 exit(STATUS_CONTROL_C_EXIT);
939 return TRUE;
940}
941#endif
942
943static void adb_cleanup(void)
944{
945 usb_cleanup();
946}
947
948void start_logging(void)
949{
950#ifdef HAVE_WIN32_PROC
951 char temp[ MAX_PATH ];
952 FILE* fnul;
953 FILE* flog;
954
955 GetTempPath( sizeof(temp) - 8, temp );
956 strcat( temp, "adb.log" );
957
958 /* Win32 specific redirections */
959 fnul = fopen( "NUL", "rt" );
960 if (fnul != NULL)
961 stdin[0] = fnul[0];
962
963 flog = fopen( temp, "at" );
964 if (flog == NULL)
965 flog = fnul;
966
967 setvbuf( flog, NULL, _IONBF, 0 );
968
969 stdout[0] = flog[0];
970 stderr[0] = flog[0];
971 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
972#else
973 int fd;
974
975 fd = unix_open("/dev/null", O_RDONLY);
976 dup2(fd, 0);
JP Abgrall408fa572011-03-16 15:57:42 -0700977 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800978
979 fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
980 if(fd < 0) {
981 fd = unix_open("/dev/null", O_WRONLY);
982 }
983 dup2(fd, 1);
984 dup2(fd, 2);
JP Abgrall408fa572011-03-16 15:57:42 -0700985 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800986 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
987#endif
988}
989
990#if !ADB_HOST
991void start_device_log(void)
992{
993 int fd;
Mike Lockwood1f546e62009-05-25 18:17:55 -0400994 char path[PATH_MAX];
995 struct tm now;
996 time_t t;
997 char value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800998
Mike Lockwood1f546e62009-05-25 18:17:55 -0400999 // read the trace mask from persistent property persist.adb.trace_mask
1000 // give up if the property is not set or cannot be parsed
1001 property_get("persist.adb.trace_mask", value, "");
1002 if (sscanf(value, "%x", &adb_trace_mask) != 1)
1003 return;
1004
1005 adb_mkdir("/data/adb", 0775);
1006 tzset();
1007 time(&t);
1008 localtime_r(&t, &now);
1009 strftime(path, sizeof(path),
1010 "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
1011 &now);
1012 fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001013 if (fd < 0)
1014 return;
1015
1016 // redirect stdout and stderr to the log file
1017 dup2(fd, 1);
1018 dup2(fd, 2);
1019 fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
Benoit Goby95ef8282011-02-01 18:57:41 -08001020 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001021
1022 fd = unix_open("/dev/null", O_RDONLY);
1023 dup2(fd, 0);
Benoit Goby95ef8282011-02-01 18:57:41 -08001024 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001025}
1026#endif
1027
1028#if ADB_HOST
JP Abgrall571c1362012-12-06 18:18:12 -08001029
1030#ifdef WORKAROUND_BUG6558362
1031#include <sched.h>
1032#define AFFINITY_ENVVAR "ADB_CPU_AFFINITY_BUG6558362"
1033void adb_set_affinity(void)
1034{
1035 cpu_set_t cpu_set;
1036 const char* cpunum_str = getenv(AFFINITY_ENVVAR);
1037 char* strtol_res;
1038 int cpu_num;
1039
1040 if (!cpunum_str || !*cpunum_str)
1041 return;
1042 cpu_num = strtol(cpunum_str, &strtol_res, 0);
1043 if (*strtol_res != '\0')
1044 fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, AFFINITY_ENVVAR);
1045
1046 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
1047 D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
1048 CPU_ZERO(&cpu_set);
1049 CPU_SET(cpu_num, &cpu_set);
1050 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
1051 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
1052 D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
1053}
1054#endif
1055
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001056int launch_server(int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001057{
1058#ifdef HAVE_WIN32_PROC
1059 /* we need to start the server in the background */
1060 /* we create a PIPE that will be used to wait for the server's "OK" */
1061 /* message since the pipe handles must be inheritable, we use a */
1062 /* security attribute */
1063 HANDLE pipe_read, pipe_write;
Ray Donnelly267aa8b2012-11-29 01:18:50 +00001064 HANDLE stdout_handle, stderr_handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001065 SECURITY_ATTRIBUTES sa;
1066 STARTUPINFO startup;
1067 PROCESS_INFORMATION pinfo;
1068 char program_path[ MAX_PATH ];
1069 int ret;
1070
1071 sa.nLength = sizeof(sa);
1072 sa.lpSecurityDescriptor = NULL;
1073 sa.bInheritHandle = TRUE;
1074
1075 /* create pipe, and ensure its read handle isn't inheritable */
1076 ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
1077 if (!ret) {
1078 fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
1079 return -1;
1080 }
1081
1082 SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
1083
Ray Donnelly267aa8b2012-11-29 01:18:50 +00001084 /* Some programs want to launch an adb command and collect its output by
1085 * calling CreateProcess with inheritable stdout/stderr handles, then
1086 * using read() to get its output. When this happens, the stdout/stderr
1087 * handles passed to the adb client process will also be inheritable.
1088 * When starting the adb server here, care must be taken to reset them
1089 * to non-inheritable.
1090 * Otherwise, something bad happens: even if the adb command completes,
1091 * the calling process is stuck while read()-ing from the stdout/stderr
1092 * descriptors, because they're connected to corresponding handles in the
1093 * adb server process (even if the latter never uses/writes to them).
1094 */
1095 stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
1096 stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
1097 if (stdout_handle != INVALID_HANDLE_VALUE) {
1098 SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
1099 }
1100 if (stderr_handle != INVALID_HANDLE_VALUE) {
1101 SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
1102 }
1103
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001104 ZeroMemory( &startup, sizeof(startup) );
1105 startup.cb = sizeof(startup);
1106 startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
1107 startup.hStdOutput = pipe_write;
1108 startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
1109 startup.dwFlags = STARTF_USESTDHANDLES;
1110
1111 ZeroMemory( &pinfo, sizeof(pinfo) );
1112
1113 /* get path of current program */
1114 GetModuleFileName( NULL, program_path, sizeof(program_path) );
1115
1116 ret = CreateProcess(
1117 program_path, /* program path */
1118 "adb fork-server server",
1119 /* the fork-server argument will set the
1120 debug = 2 in the child */
1121 NULL, /* process handle is not inheritable */
1122 NULL, /* thread handle is not inheritable */
1123 TRUE, /* yes, inherit some handles */
1124 DETACHED_PROCESS, /* the new process doesn't have a console */
1125 NULL, /* use parent's environment block */
1126 NULL, /* use parent's starting directory */
1127 &startup, /* startup info, i.e. std handles */
1128 &pinfo );
1129
1130 CloseHandle( pipe_write );
1131
1132 if (!ret) {
1133 fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
1134 CloseHandle( pipe_read );
1135 return -1;
1136 }
1137
1138 CloseHandle( pinfo.hProcess );
1139 CloseHandle( pinfo.hThread );
1140
1141 /* wait for the "OK\n" message */
1142 {
1143 char temp[3];
1144 DWORD count;
1145
1146 ret = ReadFile( pipe_read, temp, 3, &count, NULL );
1147 CloseHandle( pipe_read );
1148 if ( !ret ) {
1149 fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
1150 return -1;
1151 }
1152 if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1153 fprintf(stderr, "ADB server didn't ACK\n" );
1154 return -1;
1155 }
1156 }
1157#elif defined(HAVE_FORKEXEC)
1158 char path[PATH_MAX];
1159 int fd[2];
1160
1161 // set up a pipe so the child can tell us when it is ready.
1162 // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
1163 if (pipe(fd)) {
1164 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
1165 return -1;
1166 }
Alexey Tarasov31664102009-10-22 02:55:00 +11001167 get_my_path(path, PATH_MAX);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001168 pid_t pid = fork();
1169 if(pid < 0) return -1;
1170
1171 if (pid == 0) {
1172 // child side of the fork
1173
1174 // redirect stderr to the pipe
1175 // we use stderr instead of stdout due to stdout's buffering behavior.
1176 adb_close(fd[0]);
1177 dup2(fd[1], STDERR_FILENO);
1178 adb_close(fd[1]);
1179
Matt Gumbeld7b33082012-11-14 10:16:17 -08001180 char str_port[30];
1181 snprintf(str_port, sizeof(str_port), "%d", server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001182 // child process
Matt Gumbeld7b33082012-11-14 10:16:17 -08001183 int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184 // this should not return
1185 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
1186 } else {
1187 // parent side of the fork
1188
1189 char temp[3];
1190
1191 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
1192 // wait for the "OK\n" message
1193 adb_close(fd[1]);
1194 int ret = adb_read(fd[0], temp, 3);
JP Abgrall408fa572011-03-16 15:57:42 -07001195 int saved_errno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001196 adb_close(fd[0]);
1197 if (ret < 0) {
JP Abgrall408fa572011-03-16 15:57:42 -07001198 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001199 return -1;
1200 }
1201 if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
1202 fprintf(stderr, "ADB server didn't ACK\n" );
1203 return -1;
1204 }
1205
1206 setsid();
1207 }
1208#else
1209#error "cannot implement background server start on this platform"
1210#endif
1211 return 0;
1212}
1213#endif
1214
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001215/* Constructs a local name of form tcp:port.
1216 * target_str points to the target string, it's content will be overwritten.
1217 * target_size is the capacity of the target string.
1218 * server_port is the port number to use for the local name.
1219 */
1220void build_local_name(char* target_str, size_t target_size, int server_port)
1221{
1222 snprintf(target_str, target_size, "tcp:%d", server_port);
1223}
1224
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001225#if !ADB_HOST
Nick Kralevich080427e2013-02-15 14:39:15 -08001226
1227static void drop_capabilities_bounding_set_if_needed() {
1228#ifdef ALLOW_ADBD_ROOT
1229 char value[PROPERTY_VALUE_MAX];
1230 property_get("ro.debuggable", value, "");
1231 if (strcmp(value, "1") == 0) {
1232 return;
1233 }
1234#endif
1235 int i;
1236 for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
Nick Kralevichca8e66a2013-04-18 12:20:02 -07001237 if (i == CAP_SETUID || i == CAP_SETGID) {
Nick Kralevich080427e2013-02-15 14:39:15 -08001238 // CAP_SETUID CAP_SETGID needed by /system/bin/run-as
1239 continue;
1240 }
1241 int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
1242
1243 // Some kernels don't have file capabilities compiled in, and
1244 // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically
1245 // die when we see such misconfigured kernels.
1246 if ((err < 0) && (errno != EINVAL)) {
1247 exit(1);
1248 }
1249 }
1250}
1251
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001252static int should_drop_privileges() {
Nick Kralevich5890fe32012-01-19 13:11:35 -08001253#ifndef ALLOW_ADBD_ROOT
1254 return 1;
1255#else /* ALLOW_ADBD_ROOT */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001256 int secure = 0;
1257 char value[PROPERTY_VALUE_MAX];
1258
1259 /* run adbd in secure mode if ro.secure is set and
1260 ** we are not in the emulator
1261 */
1262 property_get("ro.kernel.qemu", value, "");
1263 if (strcmp(value, "1") != 0) {
1264 property_get("ro.secure", value, "1");
1265 if (strcmp(value, "1") == 0) {
1266 // don't run as root if ro.secure is set...
1267 secure = 1;
1268
1269 // ... except we allow running as root in userdebug builds if the
1270 // service.adb.root property has been set by the "adb root" command
1271 property_get("ro.debuggable", value, "");
1272 if (strcmp(value, "1") == 0) {
1273 property_get("service.adb.root", value, "");
1274 if (strcmp(value, "1") == 0) {
1275 secure = 0;
1276 }
1277 }
1278 }
1279 }
1280 return secure;
Nick Kralevich5890fe32012-01-19 13:11:35 -08001281#endif /* ALLOW_ADBD_ROOT */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001282}
1283#endif /* !ADB_HOST */
1284
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001285int adb_main(int is_daemon, int server_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001286{
1287#if !ADB_HOST
Mike Lockwood2f38b692009-08-24 15:58:40 -07001288 int port;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001289 char value[PROPERTY_VALUE_MAX];
Nick Kralevicheb68fa82012-04-02 13:00:35 -07001290
1291 umask(000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001292#endif
1293
1294 atexit(adb_cleanup);
1295#ifdef HAVE_WIN32_PROC
1296 SetConsoleCtrlHandler( ctrlc_handler, TRUE );
1297#elif defined(HAVE_FORKEXEC)
JP Abgrall408fa572011-03-16 15:57:42 -07001298 // No SIGCHLD. Let the service subproc handle its children.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001299 signal(SIGPIPE, SIG_IGN);
1300#endif
1301
1302 init_transport_registration();
1303
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001304#if ADB_HOST
1305 HOST = 1;
JP Abgrall571c1362012-12-06 18:18:12 -08001306
1307#ifdef WORKAROUND_BUG6558362
1308 if(is_daemon) adb_set_affinity();
1309#endif
Xavier Ducroheta09fbd12009-05-20 17:33:53 -07001310 usb_vendors_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001311 usb_init();
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001312 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001313 adb_auth_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001314
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001315 char local_name[30];
1316 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001317 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001318 exit(1);
1319 }
1320#else
Benoit Gobyd5fcafa2012-04-12 12:23:49 -07001321 property_get("ro.adb.secure", value, "0");
1322 auth_enabled = !strcmp(value, "1");
1323 if (auth_enabled)
1324 adb_auth_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001325
Jeff Sharkeyd6d42862012-09-06 13:05:40 -07001326 // Our external storage path may be different than apps, since
1327 // we aren't able to bind mount after dropping root.
1328 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
1329 if (NULL != adb_external_storage) {
1330 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
1331 } else {
1332 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
1333 " unchanged.\n");
1334 }
1335
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001336 /* don't listen on a port (default 5037) if running in secure mode */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001337 /* don't run as root if we are running in secure mode */
Nick Kralevichbd9206b2012-01-19 10:18:59 -08001338 if (should_drop_privileges()) {
Nick Kralevich080427e2013-02-15 14:39:15 -08001339 drop_capabilities_bounding_set_if_needed();
1340
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001341 /* add extra groups:
1342 ** AID_ADB to access the USB driver
1343 ** AID_LOG to read system logs (adb logcat)
1344 ** AID_INPUT to diagnose input issues (getevent)
1345 ** AID_INET to diagnose network issues (netcfg, ping)
1346 ** AID_GRAPHICS to access the frame buffer
The Android Open Source Project20155492009-03-11 12:12:01 -07001347 ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
Dianne Hackborn50458cf2012-03-07 12:57:14 -08001348 ** AID_SDCARD_R to allow reading from the SD card
Mike Lockwood6a3075c2009-05-25 13:52:00 -04001349 ** AID_SDCARD_RW to allow writing to the SD card
Mike Lockwoodd969faa2010-02-24 16:07:23 -05001350 ** AID_MOUNT to allow unmounting the SD card before rebooting
JP Abgrall61b90bd2011-11-09 10:30:08 -08001351 ** AID_NET_BW_STATS to read out qtaguid statistics
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001352 */
The Android Open Source Project20155492009-03-11 12:12:01 -07001353 gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
Dianne Hackborn50458cf2012-03-07 12:57:14 -08001354 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
1355 AID_MOUNT, AID_NET_BW_STATS };
Nick Kralevich44db9902010-08-27 14:35:07 -07001356 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
1357 exit(1);
1358 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001359
1360 /* then switch user and group to "shell" */
Nick Kralevich44db9902010-08-27 14:35:07 -07001361 if (setgid(AID_SHELL) != 0) {
1362 exit(1);
1363 }
1364 if (setuid(AID_SHELL) != 0) {
1365 exit(1);
1366 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001367
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001368 D("Local port disabled\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001369 } else {
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001370 char local_name[30];
Nick Kralevichd49aa252014-01-18 09:25:04 -08001371 if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
1372 // b/12587913: fix setcon to allow const pointers
1373 if (setcon((char *)root_seclabel) < 0) {
1374 exit(1);
1375 }
1376 }
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001377 build_local_name(local_name, sizeof(local_name), server_port);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001378 if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001379 exit(1);
1380 }
1381 }
1382
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001383 int usb = 0;
1384 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -07001385 // listen on USB
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001386 usb_init();
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001387 usb = 1;
1388 }
1389
1390 // If one of these properties is set, also listen on that port
1391 // If one of the properties isn't set and we couldn't listen on usb,
1392 // listen on the default port.
1393 property_get("service.adb.tcp.port", value, "");
1394 if (!value[0]) {
1395 property_get("persist.adb.tcp.port", value, "");
1396 }
1397 if (sscanf(value, "%d", &port) == 1 && port > 0) {
1398 printf("using port=%d\n", port);
1399 // listen on TCP port specified by service.adb.tcp.port property
1400 local_init(port);
1401 } else if (!usb) {
Mike Lockwoodcef31a02009-08-26 12:50:22 -07001402 // listen on default port
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001403 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001404 }
Mike J. Chen1dd55c52012-07-20 18:16:21 -07001405
JP Abgrall408fa572011-03-16 15:57:42 -07001406 D("adb_main(): pre init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001407 init_jdwp();
JP Abgrall408fa572011-03-16 15:57:42 -07001408 D("adb_main(): post init_jdwp()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001409#endif
1410
1411 if (is_daemon)
1412 {
1413 // inform our parent that we are up and running.
1414#ifdef HAVE_WIN32_PROC
1415 DWORD count;
1416 WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
1417#elif defined(HAVE_FORKEXEC)
1418 fprintf(stderr, "OK\n");
1419#endif
1420 start_logging();
1421 }
JP Abgrall408fa572011-03-16 15:57:42 -07001422 D("Event loop starting\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001423
1424 fdevent_loop();
1425
1426 usb_cleanup();
1427
1428 return 0;
1429}
1430
1431int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
1432{
1433 atransport *transport = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001434
1435 if(!strcmp(service, "kill")) {
1436 fprintf(stderr,"adb server killed by remote request\n");
1437 fflush(stdout);
1438 adb_write(reply_fd, "OKAY", 4);
1439 usb_cleanup();
1440 exit(0);
1441 }
1442
1443#if ADB_HOST
1444 // "transport:" is used for switching transport with a specified serial number
1445 // "transport-usb:" is used for switching transport to the only USB transport
1446 // "transport-local:" is used for switching transport to the only local transport
1447 // "transport-any:" is used for switching transport to the only transport
1448 if (!strncmp(service, "transport", strlen("transport"))) {
1449 char* error_string = "unknown failure";
1450 transport_type type = kTransportAny;
1451
1452 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
1453 type = kTransportUsb;
1454 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
1455 type = kTransportLocal;
1456 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
1457 type = kTransportAny;
1458 } else if (!strncmp(service, "transport:", strlen("transport:"))) {
1459 service += strlen("transport:");
Tom Marlin3175c8e2011-07-27 12:56:14 -05001460 serial = service;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001461 }
1462
1463 transport = acquire_one_transport(CS_ANY, type, serial, &error_string);
1464
1465 if (transport) {
1466 s->transport = transport;
1467 adb_write(reply_fd, "OKAY", 4);
1468 } else {
1469 sendfailmsg(reply_fd, error_string);
1470 }
1471 return 1;
1472 }
1473
1474 // return a list of all connected devices
Scott Andersone109d262012-04-20 11:21:14 -07001475 if (!strncmp(service, "devices", 7)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001476 char buffer[4096];
Scott Andersone109d262012-04-20 11:21:14 -07001477 int use_long = !strcmp(service+7, "-l");
1478 if (use_long || service[7] == 0) {
Scott Andersone109d262012-04-20 11:21:14 -07001479 memset(buffer, 0, sizeof(buffer));
1480 D("Getting device list \n");
1481 list_transports(buffer, sizeof(buffer), use_long);
Scott Andersone109d262012-04-20 11:21:14 -07001482 D("Wrote device list \n");
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001483 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Scott Andersone109d262012-04-20 11:21:14 -07001484 return 0;
1485 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001486 }
1487
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001488 // remove TCP transport
1489 if (!strncmp(service, "disconnect:", 11)) {
1490 char buffer[4096];
1491 memset(buffer, 0, sizeof(buffer));
1492 char* serial = service + 11;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001493 if (serial[0] == 0) {
1494 // disconnect from all TCP devices
1495 unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001496 } else {
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001497 char hostbuf[100];
1498 // assume port 5555 if no port is specified
1499 if (!strchr(serial, ':')) {
1500 snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
1501 serial = hostbuf;
1502 }
1503 atransport *t = find_transport(serial);
1504
1505 if (t) {
1506 unregister_transport(t);
1507 } else {
1508 snprintf(buffer, sizeof(buffer), "No such device %s", serial);
1509 }
Mike Lockwood74d7ff82009-10-11 23:04:18 -04001510 }
1511
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001512 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Mike Lockwood2f38b692009-08-24 15:58:40 -07001513 return 0;
1514 }
1515
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001516 // returns our value for ADB_SERVER_VERSION
1517 if (!strcmp(service, "version")) {
1518 char version[12];
1519 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001520 send_msg_with_okay(reply_fd, version, strlen(version));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001521 return 0;
1522 }
1523
1524 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
1525 char *out = "unknown";
1526 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1527 if (transport && transport->serial) {
1528 out = transport->serial;
1529 }
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001530 send_msg_with_okay(reply_fd, out, strlen(out));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001531 return 0;
1532 }
Scott Andersone109d262012-04-20 11:21:14 -07001533 if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
1534 char *out = "unknown";
1535 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1536 if (transport && transport->devpath) {
1537 out = transport->devpath;
1538 }
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001539 send_msg_with_okay(reply_fd, out, strlen(out));
Scott Andersone109d262012-04-20 11:21:14 -07001540 return 0;
1541 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001542 // indicates a new emulator instance has started
1543 if (!strncmp(service,"emulator:",9)) {
1544 int port = atoi(service+9);
1545 local_connect(port);
1546 /* we don't even need to send a reply */
1547 return 0;
1548 }
1549#endif // ADB_HOST
1550
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001551 if(!strcmp(service,"list-forward")) {
1552 // Create the list of forward redirections.
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001553 int buffer_size = format_listeners(NULL, 0);
1554 // Add one byte for the trailing zero.
1555 char* buffer = malloc(buffer_size+1);
1556 (void) format_listeners(buffer, buffer_size+1);
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001557 send_msg_with_okay(reply_fd, buffer, buffer_size);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001558 free(buffer);
1559 return 0;
1560 }
1561
1562 if (!strcmp(service,"killforward-all")) {
1563 remove_all_listeners();
1564 adb_write(reply_fd, "OKAYOKAY", 8);
1565 return 0;
1566 }
1567
1568 if(!strncmp(service,"forward:",8) ||
1569 !strncmp(service,"killforward:",12)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001570 char *local, *remote, *err;
1571 int r;
1572 atransport *transport;
1573
1574 int createForward = strncmp(service,"kill",4);
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001575 int no_rebind = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001576
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001577 local = strchr(service, ':') + 1;
1578
1579 // Handle forward:norebind:<local>... here
1580 if (createForward && !strncmp(local, "norebind:", 9)) {
1581 no_rebind = 1;
1582 local = strchr(local, ':') + 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001583 }
1584
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001585 remote = strchr(local,';');
1586
1587 if (createForward) {
1588 // Check forward: parameter format: '<local>;<remote>'
1589 if(remote == 0) {
1590 sendfailmsg(reply_fd, "malformed forward spec");
1591 return 0;
1592 }
1593
1594 *remote++ = 0;
1595 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')){
1596 sendfailmsg(reply_fd, "malformed forward spec");
1597 return 0;
1598 }
1599 } else {
1600 // Check killforward: parameter format: '<local>'
1601 if (local[0] == 0) {
1602 sendfailmsg(reply_fd, "malformed forward spec");
1603 return 0;
1604 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001605 }
1606
1607 transport = acquire_one_transport(CS_ANY, ttype, serial, &err);
1608 if (!transport) {
1609 sendfailmsg(reply_fd, err);
1610 return 0;
1611 }
1612
1613 if (createForward) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001614 r = install_listener(local, remote, transport, no_rebind);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001615 } else {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001616 r = remove_listener(local, transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001617 }
1618 if(r == 0) {
1619 /* 1st OKAY is connect, 2nd OKAY is status */
1620 writex(reply_fd, "OKAYOKAY", 8);
1621 return 0;
1622 }
1623
1624 if (createForward) {
David 'Digit' Turner0d82fbf2012-11-14 15:01:55 +01001625 const char* message;
1626 switch (r) {
1627 case INSTALL_STATUS_CANNOT_BIND:
1628 message = "cannot bind to socket";
1629 break;
1630 case INSTALL_STATUS_CANNOT_REBIND:
1631 message = "cannot rebind existing socket";
1632 break;
1633 default:
1634 message = "internal error";
1635 }
1636 sendfailmsg(reply_fd, message);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001637 } else {
1638 sendfailmsg(reply_fd, "cannot remove listener");
1639 }
1640 return 0;
1641 }
1642
1643 if(!strncmp(service,"get-state",strlen("get-state"))) {
1644 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
1645 char *state = connection_state_name(transport);
Snild Dolkow2264e7c2014-01-30 10:08:38 +01001646 send_msg_with_okay(reply_fd, state, strlen(state));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001647 return 0;
1648 }
1649 return -1;
1650}
1651
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001652int main(int argc, char **argv)
1653{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001654#if ADB_HOST
1655 adb_sysdeps_init();
JP Abgrall408fa572011-03-16 15:57:42 -07001656 adb_trace_init();
1657 D("Handling commandline()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001658 return adb_commandline(argc - 1, argv + 1);
1659#else
Vladimir Chtchetkine28781b02012-02-27 10:41:53 -08001660 /* If adbd runs inside the emulator this will enable adb tracing via
1661 * adb-debug qemud service in the emulator. */
1662 adb_qemu_trace_init();
Nick Kralevichd49aa252014-01-18 09:25:04 -08001663 while(1) {
1664 int c;
1665 int option_index = 0;
1666 static struct option opts[] = {
1667 {"root_seclabel", required_argument, 0, 's' },
1668 {"device_banner", required_argument, 0, 'b' }
1669 };
1670 c = getopt_long(argc, argv, "", opts, &option_index);
1671 if (c == -1)
1672 break;
1673 switch (c) {
1674 case 's':
1675 root_seclabel = optarg;
1676 break;
1677 case 'b':
1678 adb_device_banner = optarg;
1679 break;
1680 default:
1681 break;
1682 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001683 }
Mike Lockwood1f546e62009-05-25 18:17:55 -04001684
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001685 start_device_log();
JP Abgrall408fa572011-03-16 15:57:42 -07001686 D("Handling main()\n");
Stefan Hilzingera84a42e2010-04-19 12:21:12 +01001687 return adb_main(0, DEFAULT_ADB_PORT);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001688#endif
1689}