blob: 4c90dcce799fb6a97a763188d9b9b8e92b5d22d2 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_ADB
18
19#include "sysdeps.h"
20#include "adb.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021
Dan Albertecbd8742015-03-08 21:12:08 -070022#include <ctype.h>
23#include <errno.h>
24#include <stdarg.h>
25#include <stddef.h>
26#include <stdint.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027#include <stdio.h>
28#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080029#include <string.h>
Mike Lockwood4ac1e282009-05-25 18:17:55 -040030#include <sys/time.h>
Dan Albertecbd8742015-03-08 21:12:08 -070031#include <time.h>
32
33#include <string>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080034
Elliott Hughesa585cbd2015-04-20 08:09:20 -070035#include <base/stringprintf.h>
Elliott Hughesfb2ba512015-04-24 23:02:00 -070036#include <base/strings.h>
Elliott Hughesa585cbd2015-04-20 08:09:20 -070037
Benoit Goby2cc19e42012-04-12 12:23:49 -070038#include "adb_auth.h"
Dan Albert66a91b02015-02-24 21:26:58 -080039#include "adb_io.h"
Dan Albert020292b2015-02-18 18:03:26 -080040#include "adb_listeners.h"
Dan Albertb302d122015-02-24 15:51:19 -080041#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042
Scott Andersonfa020922012-05-25 14:10:02 -070043#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
44
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080045#if !ADB_HOST
Nick Kralevich7472d292013-05-23 09:54:13 -070046#include <cutils/properties.h>
Nick Kralevich9de01bd2013-02-28 14:12:58 -080047#include <sys/capability.h>
Jeff Sharkey11987252012-08-14 21:00:22 -070048#include <sys/mount.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080049#endif
50
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070051#if ADB_TRACE
52ADB_MUTEX_DEFINE( D_lock );
53#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080054
55int HOST = 0;
56
Scott Andersonfa020922012-05-25 14:10:02 -070057#if !ADB_HOST
Dan Albert432ffe22015-02-18 18:22:45 -080058const char *adb_device_banner = "device";
Scott Andersonfa020922012-05-25 14:10:02 -070059#endif
The Android Open Source Project9ca14dc2009-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
Dan Albertecbd8742015-03-08 21:12:08 -070083#if !ADB_HOST
84void start_device_log(void) {
Dan Albertecbd8742015-03-08 21:12:08 -070085 struct tm now;
86 time_t t;
87 tzset();
88 time(&t);
89 localtime_r(&t, &now);
90
Dan Albertdec2b242015-03-19 22:53:30 -070091 char timestamp[PATH_MAX];
92 strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", &now);
Dan Albertecbd8742015-03-08 21:12:08 -070093
Dan Albertdec2b242015-03-19 22:53:30 -070094 char path[PATH_MAX];
95 snprintf(path, sizeof(path), "/data/adb/adb-%s-%d", timestamp, getpid());
96
97 int fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
Dan Albertecbd8742015-03-08 21:12:08 -070098 if (fd == -1) {
99 return;
100 }
101
102 // redirect stdout and stderr to the log file
103 dup2(fd, STDOUT_FILENO);
104 dup2(fd, STDERR_FILENO);
105 fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
106 adb_close(fd);
Dan Albertecbd8742015-03-08 21:12:08 -0700107}
108#endif
109
110int adb_trace_mask;
111
112std::string get_trace_setting_from_env() {
113 const char* setting = getenv("ADB_TRACE");
114 if (setting == nullptr) {
115 setting = "";
116 }
117
118 return std::string(setting);
119}
120
121#if !ADB_HOST
122std::string get_trace_setting_from_prop() {
123 char buf[PROPERTY_VALUE_MAX];
124 property_get("persist.adb.trace_mask", buf, "");
125 return std::string(buf);
126}
127#endif
128
129std::string get_trace_setting() {
130#if ADB_HOST
131 return get_trace_setting_from_env();
132#else
133 return get_trace_setting_from_prop();
134#endif
135}
136
137// Split the comma/space/colum/semi-column separated list of tags from the trace
138// setting and build the trace mask from it. note that '1' and 'all' are special
139// cases to enable all tracing.
140//
141// adb's trace setting comes from the ADB_TRACE environment variable, whereas
142// adbd's comes from the system property persist.adb.trace_mask.
143void adb_trace_init() {
144 const std::string trace_setting = get_trace_setting();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800145
146 static const struct {
147 const char* tag;
148 int flag;
149 } tags[] = {
150 { "1", 0 },
151 { "all", 0 },
152 { "adb", TRACE_ADB },
153 { "sockets", TRACE_SOCKETS },
154 { "packets", TRACE_PACKETS },
155 { "rwx", TRACE_RWX },
156 { "usb", TRACE_USB },
157 { "sync", TRACE_SYNC },
158 { "sysdeps", TRACE_SYSDEPS },
159 { "transport", TRACE_TRANSPORT },
160 { "jdwp", TRACE_JDWP },
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700161 { "services", TRACE_SERVICES },
Benoit Goby2cc19e42012-04-12 12:23:49 -0700162 { "auth", TRACE_AUTH },
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800163 { NULL, 0 }
164 };
165
Dan Albertecbd8742015-03-08 21:12:08 -0700166 if (trace_setting.empty()) {
167 return;
168 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800169
Dan Albertecbd8742015-03-08 21:12:08 -0700170 // Use a comma/colon/semi-colon/space separated list
171 const char* p = trace_setting.c_str();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800172 while (*p) {
173 int len, tagn;
174
Dan Albertecbd8742015-03-08 21:12:08 -0700175 const char* q = strpbrk(p, " ,:;");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800176 if (q == NULL) {
177 q = p + strlen(p);
178 }
179 len = q - p;
180
Dan Albertecbd8742015-03-08 21:12:08 -0700181 for (tagn = 0; tags[tagn].tag != NULL; tagn++) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800182 int taglen = strlen(tags[tagn].tag);
183
Dan Albertecbd8742015-03-08 21:12:08 -0700184 if (len == taglen && !memcmp(tags[tagn].tag, p, len)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800185 int flag = tags[tagn].flag;
186 if (flag == 0) {
187 adb_trace_mask = ~0;
188 return;
189 }
190 adb_trace_mask |= (1 << flag);
191 break;
192 }
193 }
194 p = q;
195 if (*p)
196 p++;
197 }
Dan Albertecbd8742015-03-08 21:12:08 -0700198
199#if !ADB_HOST
200 start_device_log();
201#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800202}
203
Dan Albertf30d73c2015-02-25 17:51:28 -0800204apacket* get_apacket(void)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800205{
Dan Albertf30d73c2015-02-25 17:51:28 -0800206 apacket* p = reinterpret_cast<apacket*>(malloc(sizeof(apacket)));
207 if (p == nullptr) {
208 fatal("failed to allocate an apacket");
209 }
210
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800211 memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
212 return p;
213}
214
215void put_apacket(apacket *p)
216{
217 free(p);
218}
219
Benoit Goby2cc19e42012-04-12 12:23:49 -0700220void handle_online(atransport *t)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800221{
222 D("adb: online\n");
Benoit Goby2cc19e42012-04-12 12:23:49 -0700223 t->online = 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800224}
225
226void handle_offline(atransport *t)
227{
228 D("adb: offline\n");
229 //Close the associated usb
Benoit Goby2cc19e42012-04-12 12:23:49 -0700230 t->online = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800231 run_transport_disconnects(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800232}
233
Benoit Goby2cc19e42012-04-12 12:23:49 -0700234#if DEBUG_PACKETS
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800235#define DUMPMAX 32
236void print_packet(const char *label, apacket *p)
237{
238 char *tag;
239 char *x;
240 unsigned count;
241
242 switch(p->msg.command){
243 case A_SYNC: tag = "SYNC"; break;
244 case A_CNXN: tag = "CNXN" ; break;
245 case A_OPEN: tag = "OPEN"; break;
246 case A_OKAY: tag = "OKAY"; break;
247 case A_CLSE: tag = "CLSE"; break;
248 case A_WRTE: tag = "WRTE"; break;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700249 case A_AUTH: tag = "AUTH"; break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800250 default: tag = "????"; break;
251 }
252
253 fprintf(stderr, "%s: %s %08x %08x %04x \"",
254 label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
255 count = p->msg.data_length;
256 x = (char*) p->data;
257 if(count > DUMPMAX) {
258 count = DUMPMAX;
259 tag = "\n";
260 } else {
261 tag = "\"\n";
262 }
263 while(count-- > 0){
264 if((*x >= ' ') && (*x < 127)) {
265 fputc(*x, stderr);
266 } else {
267 fputc('.', stderr);
268 }
269 x++;
270 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700271 fputs(tag, stderr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800272}
273#endif
274
275static void send_ready(unsigned local, unsigned remote, atransport *t)
276{
277 D("Calling send_ready \n");
278 apacket *p = get_apacket();
279 p->msg.command = A_OKAY;
280 p->msg.arg0 = local;
281 p->msg.arg1 = remote;
282 send_packet(p, t);
283}
284
285static void send_close(unsigned local, unsigned remote, atransport *t)
286{
287 D("Calling send_close \n");
288 apacket *p = get_apacket();
289 p->msg.command = A_CLSE;
290 p->msg.arg0 = local;
291 p->msg.arg1 = remote;
292 send_packet(p, t);
293}
294
Scott Andersonfa020922012-05-25 14:10:02 -0700295static size_t fill_connect_data(char *buf, size_t bufsize)
296{
297#if ADB_HOST
298 return snprintf(buf, bufsize, "host::") + 1;
299#else
300 static const char *cnxn_props[] = {
301 "ro.product.name",
302 "ro.product.model",
303 "ro.product.device",
304 };
305 static const int num_cnxn_props = ARRAY_SIZE(cnxn_props);
306 int i;
307 size_t remaining = bufsize;
308 size_t len;
309
310 len = snprintf(buf, remaining, "%s::", adb_device_banner);
311 remaining -= len;
312 buf += len;
313 for (i = 0; i < num_cnxn_props; i++) {
314 char value[PROPERTY_VALUE_MAX];
315 property_get(cnxn_props[i], value, "");
316 len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value);
317 remaining -= len;
318 buf += len;
319 }
320
321 return bufsize - remaining + 1;
322#endif
323}
324
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100325#if !ADB_HOST
326static void send_msg_with_header(int fd, const char* msg, size_t msglen) {
327 char header[5];
328 if (msglen > 0xffff)
329 msglen = 0xffff;
330 snprintf(header, sizeof(header), "%04x", (unsigned)msglen);
Dan Albert66a91b02015-02-24 21:26:58 -0800331 WriteFdExactly(fd, header, 4);
332 WriteFdExactly(fd, msg, msglen);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100333}
334#endif
335
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -0700336#if ADB_HOST
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100337static void send_msg_with_okay(int fd, const char* msg, size_t msglen) {
Snild Dolkow4516a872014-01-30 10:08:38 +0100338 char header[9];
339 if (msglen > 0xffff)
340 msglen = 0xffff;
341 snprintf(header, sizeof(header), "OKAY%04x", (unsigned)msglen);
Dan Albert66a91b02015-02-24 21:26:58 -0800342 WriteFdExactly(fd, header, 8);
343 WriteFdExactly(fd, msg, msglen);
Snild Dolkow4516a872014-01-30 10:08:38 +0100344}
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -0700345#endif // ADB_HOST
Snild Dolkow4516a872014-01-30 10:08:38 +0100346
Dan Albert056ad0e2015-02-18 17:47:33 -0800347void send_connect(atransport *t)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800348{
349 D("Calling send_connect \n");
350 apacket *cp = get_apacket();
351 cp->msg.command = A_CNXN;
352 cp->msg.arg0 = A_VERSION;
353 cp->msg.arg1 = MAX_PAYLOAD;
Scott Andersonfa020922012-05-25 14:10:02 -0700354 cp->msg.data_length = fill_connect_data((char *)cp->data,
355 sizeof(cp->data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800356 send_packet(cp, t);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700357}
358
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -0700359#if ADB_HOST
Dan Albertf30d73c2015-02-25 17:51:28 -0800360static const char* connection_state_name(atransport *t)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800361{
362 if (t == NULL) {
363 return "unknown";
364 }
365
366 switch(t->connection_state) {
367 case CS_BOOTLOADER:
368 return "bootloader";
369 case CS_DEVICE:
370 return "device";
trevdd11cfc52013-04-17 14:34:23 +0100371 case CS_RECOVERY:
372 return "recovery";
373 case CS_SIDELOAD:
374 return "sideload";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800375 case CS_OFFLINE:
376 return "offline";
Nick Kralevich23b39f02013-02-15 10:22:08 -0800377 case CS_UNAUTHORIZED:
378 return "unauthorized";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379 default:
380 return "unknown";
381 }
382}
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -0700383#endif // ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800384
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700385// qual_overwrite is used to overwrite a qualifier string. dst is a
386// pointer to a char pointer. It is assumed that if *dst is non-NULL, it
387// was malloc'ed and needs to freed. *dst will be set to a dup of src.
388// TODO: switch to std::string for these atransport fields instead.
389static void qual_overwrite(char** dst, const std::string& src) {
Scott Andersonfa020922012-05-25 14:10:02 -0700390 free(*dst);
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700391 *dst = strdup(src.c_str());
Scott Andersonfa020922012-05-25 14:10:02 -0700392}
393
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700394void parse_banner(const char* banner, atransport* t) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800395 D("parse_banner: %s\n", banner);
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700396
397 // The format is something like:
398 // "device::ro.product.name=x;ro.product.model=y;ro.product.device=z;".
399 std::vector<std::string> pieces = android::base::Split(banner, ":");
400
401 if (pieces.size() > 2) {
402 const std::string& props = pieces[2];
403 for (auto& prop : android::base::Split(props, ";")) {
404 // The list of properties was traditionally ;-terminated rather than ;-separated.
405 if (prop.empty()) continue;
406
407 std::vector<std::string> key_value = android::base::Split(prop, "=");
408 if (key_value.size() != 2) continue;
409
410 const std::string& key = key_value[0];
411 const std::string& value = key_value[1];
412 if (key == "ro.product.name") {
413 qual_overwrite(&t->product, value);
414 } else if (key == "ro.product.model") {
415 qual_overwrite(&t->model, value);
416 } else if (key == "ro.product.device") {
417 qual_overwrite(&t->device, value);
Scott Andersonfa020922012-05-25 14:10:02 -0700418 }
419 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800420 }
421
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700422 const std::string& type = pieces[0];
423 if (type == "bootloader") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800424 D("setting connection_state to CS_BOOTLOADER\n");
425 t->connection_state = CS_BOOTLOADER;
426 update_transports();
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700427 } else if (type == "device") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800428 D("setting connection_state to CS_DEVICE\n");
429 t->connection_state = CS_DEVICE;
430 update_transports();
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700431 } else if (type == "recovery") {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800432 D("setting connection_state to CS_RECOVERY\n");
433 t->connection_state = CS_RECOVERY;
434 update_transports();
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700435 } else if (type == "sideload") {
Doug Zongker6b217ed2012-01-09 14:54:53 -0800436 D("setting connection_state to CS_SIDELOAD\n");
437 t->connection_state = CS_SIDELOAD;
438 update_transports();
Doug Zongker6b217ed2012-01-09 14:54:53 -0800439 }
440
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441 t->connection_state = CS_HOST;
442}
443
444void handle_packet(apacket *p, atransport *t)
445{
446 asocket *s;
447
Viral Mehta841f9a72010-06-16 18:41:28 +0530448 D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
449 ((char*) (&(p->msg.command)))[1],
450 ((char*) (&(p->msg.command)))[2],
451 ((char*) (&(p->msg.command)))[3]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800452 print_packet("recv", p);
453
454 switch(p->msg.command){
455 case A_SYNC:
456 if(p->msg.arg0){
457 send_packet(p, t);
458 if(HOST) send_connect(t);
459 } else {
460 t->connection_state = CS_OFFLINE;
461 handle_offline(t);
462 send_packet(p, t);
463 }
464 return;
465
466 case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
467 /* XXX verify version, etc */
468 if(t->connection_state != CS_OFFLINE) {
469 t->connection_state = CS_OFFLINE;
470 handle_offline(t);
471 }
Benoit Goby2cc19e42012-04-12 12:23:49 -0700472
Elliott Hughesfb2ba512015-04-24 23:02:00 -0700473 parse_banner(reinterpret_cast<const char*>(p->data), t);
Benoit Goby2cc19e42012-04-12 12:23:49 -0700474
475 if (HOST || !auth_enabled) {
476 handle_online(t);
477 if(!HOST) send_connect(t);
478 } else {
479 send_auth_request(t);
480 }
481 break;
482
483 case A_AUTH:
484 if (p->msg.arg0 == ADB_AUTH_TOKEN) {
Nick Kralevich23b39f02013-02-15 10:22:08 -0800485 t->connection_state = CS_UNAUTHORIZED;
Benoit Goby2cc19e42012-04-12 12:23:49 -0700486 t->key = adb_auth_nextkey(t->key);
487 if (t->key) {
488 send_auth_response(p->data, p->msg.data_length, t);
489 } else {
490 /* No more private keys to try, send the public key */
491 send_auth_publickey(t);
492 }
493 } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
494 if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
495 adb_auth_verified(t);
496 t->failed_auth_attempts = 0;
497 } else {
498 if (t->failed_auth_attempts++ > 10)
499 adb_sleep_ms(1000);
500 send_auth_request(t);
501 }
502 } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
503 adb_auth_confirm_key(p->data, p->msg.data_length, t);
504 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800505 break;
506
507 case A_OPEN: /* OPEN(local-id, 0, "destination") */
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100508 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800509 char *name = (char*) p->data;
510 name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
511 s = create_local_service_socket(name);
512 if(s == 0) {
513 send_close(0, p->msg.arg0, t);
514 } else {
515 s->peer = create_remote_socket(p->msg.arg0, t);
516 s->peer->peer = s;
517 send_ready(s->id, s->peer->id, t);
518 s->ready(s);
519 }
520 }
521 break;
522
523 case A_OKAY: /* READY(local-id, remote-id, "") */
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100524 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
525 if((s = find_local_socket(p->msg.arg1, 0))) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800526 if(s->peer == 0) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100527 /* On first READY message, create the connection. */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800528 s->peer = create_remote_socket(p->msg.arg0, t);
529 s->peer->peer = s;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100530 s->ready(s);
531 } else if (s->peer->id == p->msg.arg0) {
532 /* Other READY messages must use the same local-id */
533 s->ready(s);
534 } else {
535 D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s\n",
536 p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800537 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800538 }
539 }
540 break;
541
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100542 case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */
543 if (t->online && p->msg.arg1 != 0) {
544 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
545 /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
546 * a failed OPEN only. However, due to a bug in previous ADB
547 * versions, CLOSE(0, remote-id, "") was also used for normal
548 * CLOSE() operations.
549 *
550 * This is bad because it means a compromised adbd could
551 * send packets to close connections between the host and
552 * other devices. To avoid this, only allow this if the local
553 * socket has a peer on the same transport.
554 */
555 if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
556 D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s\n",
557 p->msg.arg1, t->serial, s->peer->transport->serial);
558 } else {
559 s->close(s);
560 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800561 }
562 }
563 break;
564
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100565 case A_WRTE: /* WRITE(local-id, remote-id, <data>) */
566 if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
567 if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800568 unsigned rid = p->msg.arg0;
569 p->len = p->msg.data_length;
570
571 if(s->enqueue(s, p) == 0) {
572 D("Enqueue the socket\n");
573 send_ready(s->id, rid, t);
574 }
575 return;
576 }
577 }
578 break;
579
580 default:
581 printf("handle_packet: what is %08x?!\n", p->msg.command);
582 }
583
584 put_apacket(p);
585}
586
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800587#if ADB_HOST
Nick Kralevich23b39f02013-02-15 10:22:08 -0800588
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100589int launch_server(int server_port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800590{
Yabin Cui2fa43212014-11-11 09:24:11 -0800591#if defined(_WIN32)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800592 /* we need to start the server in the background */
593 /* we create a PIPE that will be used to wait for the server's "OK" */
594 /* message since the pipe handles must be inheritable, we use a */
595 /* security attribute */
596 HANDLE pipe_read, pipe_write;
Ray Donnellyc0324702012-11-29 01:18:50 +0000597 HANDLE stdout_handle, stderr_handle;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800598 SECURITY_ATTRIBUTES sa;
599 STARTUPINFO startup;
600 PROCESS_INFORMATION pinfo;
601 char program_path[ MAX_PATH ];
602 int ret;
603
604 sa.nLength = sizeof(sa);
605 sa.lpSecurityDescriptor = NULL;
606 sa.bInheritHandle = TRUE;
607
608 /* create pipe, and ensure its read handle isn't inheritable */
609 ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
610 if (!ret) {
611 fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
612 return -1;
613 }
614
615 SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
616
Ray Donnellyc0324702012-11-29 01:18:50 +0000617 /* Some programs want to launch an adb command and collect its output by
618 * calling CreateProcess with inheritable stdout/stderr handles, then
619 * using read() to get its output. When this happens, the stdout/stderr
620 * handles passed to the adb client process will also be inheritable.
621 * When starting the adb server here, care must be taken to reset them
622 * to non-inheritable.
623 * Otherwise, something bad happens: even if the adb command completes,
624 * the calling process is stuck while read()-ing from the stdout/stderr
625 * descriptors, because they're connected to corresponding handles in the
626 * adb server process (even if the latter never uses/writes to them).
627 */
628 stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
629 stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
630 if (stdout_handle != INVALID_HANDLE_VALUE) {
631 SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
632 }
633 if (stderr_handle != INVALID_HANDLE_VALUE) {
634 SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
635 }
636
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800637 ZeroMemory( &startup, sizeof(startup) );
638 startup.cb = sizeof(startup);
639 startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
640 startup.hStdOutput = pipe_write;
641 startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
642 startup.dwFlags = STARTF_USESTDHANDLES;
643
644 ZeroMemory( &pinfo, sizeof(pinfo) );
645
646 /* get path of current program */
647 GetModuleFileName( NULL, program_path, sizeof(program_path) );
Wenhao Lie0fa2c52013-11-13 16:23:37 +0800648 char args[64];
649 snprintf(args, sizeof(args), "adb -P %d fork-server server", server_port);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800650 ret = CreateProcess(
651 program_path, /* program path */
Wenhao Lie0fa2c52013-11-13 16:23:37 +0800652 args,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800653 /* the fork-server argument will set the
654 debug = 2 in the child */
655 NULL, /* process handle is not inheritable */
656 NULL, /* thread handle is not inheritable */
657 TRUE, /* yes, inherit some handles */
658 DETACHED_PROCESS, /* the new process doesn't have a console */
659 NULL, /* use parent's environment block */
660 NULL, /* use parent's starting directory */
661 &startup, /* startup info, i.e. std handles */
662 &pinfo );
663
664 CloseHandle( pipe_write );
665
666 if (!ret) {
667 fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
668 CloseHandle( pipe_read );
669 return -1;
670 }
671
672 CloseHandle( pinfo.hProcess );
673 CloseHandle( pinfo.hThread );
674
675 /* wait for the "OK\n" message */
676 {
677 char temp[3];
678 DWORD count;
679
680 ret = ReadFile( pipe_read, temp, 3, &count, NULL );
681 CloseHandle( pipe_read );
682 if ( !ret ) {
683 fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
684 return -1;
685 }
686 if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
687 fprintf(stderr, "ADB server didn't ACK\n" );
688 return -1;
689 }
690 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800691#else /* !defined(_WIN32) */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800692 char path[PATH_MAX];
693 int fd[2];
694
695 // set up a pipe so the child can tell us when it is ready.
696 // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
697 if (pipe(fd)) {
698 fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
699 return -1;
700 }
Alexey Tarasov857f17a2009-10-22 02:55:00 +1100701 get_my_path(path, PATH_MAX);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800702 pid_t pid = fork();
703 if(pid < 0) return -1;
704
705 if (pid == 0) {
706 // child side of the fork
707
708 // redirect stderr to the pipe
709 // we use stderr instead of stdout due to stdout's buffering behavior.
710 adb_close(fd[0]);
711 dup2(fd[1], STDERR_FILENO);
712 adb_close(fd[1]);
713
Matt Gumbel411775c2012-11-14 10:16:17 -0800714 char str_port[30];
715 snprintf(str_port, sizeof(str_port), "%d", server_port);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800716 // child process
Matt Gumbel411775c2012-11-14 10:16:17 -0800717 int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800718 // this should not return
719 fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
720 } else {
721 // parent side of the fork
722
723 char temp[3];
724
725 temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
726 // wait for the "OK\n" message
727 adb_close(fd[1]);
728 int ret = adb_read(fd[0], temp, 3);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700729 int saved_errno = errno;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800730 adb_close(fd[0]);
731 if (ret < 0) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700732 fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800733 return -1;
734 }
735 if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
736 fprintf(stderr, "ADB server didn't ACK\n" );
737 return -1;
738 }
739
740 setsid();
741 }
Yabin Cui2fa43212014-11-11 09:24:11 -0800742#endif /* !defined(_WIN32) */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800743 return 0;
744}
Yabin Cui2fa43212014-11-11 09:24:11 -0800745#endif /* ADB_HOST */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800746
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100747// Try to handle a network forwarding request.
748// This returns 1 on success, 0 on failure, and -1 to indicate this is not
749// a forwarding-related request.
750int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd)
751{
752 if (!strcmp(service, "list-forward")) {
753 // Create the list of forward redirections.
754 int buffer_size = format_listeners(NULL, 0);
755 // Add one byte for the trailing zero.
Dan Albertf30d73c2015-02-25 17:51:28 -0800756 char* buffer = reinterpret_cast<char*>(malloc(buffer_size + 1));
757 if (buffer == nullptr) {
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100758 sendfailmsg(reply_fd, "not enough memory");
759 return 1;
760 }
761 (void) format_listeners(buffer, buffer_size + 1);
762#if ADB_HOST
763 send_msg_with_okay(reply_fd, buffer, buffer_size);
764#else
765 send_msg_with_header(reply_fd, buffer, buffer_size);
766#endif
767 free(buffer);
768 return 1;
769 }
770
771 if (!strcmp(service, "killforward-all")) {
772 remove_all_listeners();
773#if ADB_HOST
774 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
775 adb_write(reply_fd, "OKAY", 4);
776#endif
777 adb_write(reply_fd, "OKAY", 4);
778 return 1;
779 }
780
781 if (!strncmp(service, "forward:",8) ||
782 !strncmp(service, "killforward:",12)) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800783 char *local, *remote;
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100784 atransport *transport;
785
786 int createForward = strncmp(service, "kill", 4);
787 int no_rebind = 0;
788
789 local = strchr(service, ':') + 1;
790
791 // Handle forward:norebind:<local>... here
792 if (createForward && !strncmp(local, "norebind:", 9)) {
793 no_rebind = 1;
794 local = strchr(local, ':') + 1;
795 }
796
797 remote = strchr(local,';');
798
799 if (createForward) {
800 // Check forward: parameter format: '<local>;<remote>'
801 if(remote == 0) {
802 sendfailmsg(reply_fd, "malformed forward spec");
803 return 1;
804 }
805
806 *remote++ = 0;
807 if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')) {
808 sendfailmsg(reply_fd, "malformed forward spec");
809 return 1;
810 }
811 } else {
812 // Check killforward: parameter format: '<local>'
813 if (local[0] == 0) {
814 sendfailmsg(reply_fd, "malformed forward spec");
815 return 1;
816 }
817 }
818
Elliott Hughesab882422015-04-16 22:54:44 -0700819 std::string error_msg;
820 transport = acquire_one_transport(CS_ANY, ttype, serial, &error_msg);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100821 if (!transport) {
Elliott Hughesab882422015-04-16 22:54:44 -0700822 sendfailmsg(reply_fd, error_msg.c_str());
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100823 return 1;
824 }
825
Elliott Hughesa585cbd2015-04-20 08:09:20 -0700826 install_status_t r;
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100827 if (createForward) {
828 r = install_listener(local, remote, transport, no_rebind);
829 } else {
830 r = remove_listener(local, transport);
831 }
Elliott Hughesa585cbd2015-04-20 08:09:20 -0700832 if (r == INSTALL_STATUS_OK) {
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100833#if ADB_HOST
834 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
Dan Albert66a91b02015-02-24 21:26:58 -0800835 WriteFdExactly(reply_fd, "OKAY", 4);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100836#endif
Dan Albert66a91b02015-02-24 21:26:58 -0800837 WriteFdExactly(reply_fd, "OKAY", 4);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100838 return 1;
839 }
840
Elliott Hughesa585cbd2015-04-20 08:09:20 -0700841 std::string message;
842 switch (r) {
843 case INSTALL_STATUS_OK: message = " "; break;
844 case INSTALL_STATUS_INTERNAL_ERROR: message = "internal error"; break;
845 case INSTALL_STATUS_CANNOT_BIND:
846 message = android::base::StringPrintf("cannot bind to socket: %s", strerror(errno));
847 break;
848 case INSTALL_STATUS_CANNOT_REBIND:
849 message = android::base::StringPrintf("cannot rebind existing socket: %s", strerror(errno));
850 break;
851 case INSTALL_STATUS_LISTENER_NOT_FOUND: message = "listener not found"; break;
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100852 }
Elliott Hughesa585cbd2015-04-20 08:09:20 -0700853 sendfailmsg(reply_fd, message.c_str());
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100854 return 1;
855 }
856 return 0;
857}
858
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800859int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
860{
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800861 if(!strcmp(service, "kill")) {
862 fprintf(stderr,"adb server killed by remote request\n");
863 fflush(stdout);
864 adb_write(reply_fd, "OKAY", 4);
865 usb_cleanup();
866 exit(0);
867 }
868
869#if ADB_HOST
Chih-Hung Hsiehf5de7662014-09-05 15:38:15 -0700870 atransport *transport = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800871 // "transport:" is used for switching transport with a specified serial number
872 // "transport-usb:" is used for switching transport to the only USB transport
873 // "transport-local:" is used for switching transport to the only local transport
874 // "transport-any:" is used for switching transport to the only transport
875 if (!strncmp(service, "transport", strlen("transport"))) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800876 transport_type type = kTransportAny;
877
878 if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
879 type = kTransportUsb;
880 } else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
881 type = kTransportLocal;
882 } else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
883 type = kTransportAny;
884 } else if (!strncmp(service, "transport:", strlen("transport:"))) {
885 service += strlen("transport:");
Tom Marlin851ee3a2011-07-27 12:56:14 -0500886 serial = service;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800887 }
888
Elliott Hughesab882422015-04-16 22:54:44 -0700889 std::string error_msg = "unknown failure";
890 transport = acquire_one_transport(CS_ANY, type, serial, &error_msg);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800891
892 if (transport) {
893 s->transport = transport;
894 adb_write(reply_fd, "OKAY", 4);
895 } else {
Elliott Hughesab882422015-04-16 22:54:44 -0700896 sendfailmsg(reply_fd, error_msg.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800897 }
898 return 1;
899 }
900
901 // return a list of all connected devices
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700902 if (!strncmp(service, "devices", 7)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800903 char buffer[4096];
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700904 int use_long = !strcmp(service+7, "-l");
905 if (use_long || service[7] == 0) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700906 memset(buffer, 0, sizeof(buffer));
907 D("Getting device list \n");
908 list_transports(buffer, sizeof(buffer), use_long);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700909 D("Wrote device list \n");
Snild Dolkow4516a872014-01-30 10:08:38 +0100910 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700911 return 0;
912 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800913 }
914
Mike Lockwood5f2c4a12009-10-11 23:04:18 -0400915 // remove TCP transport
916 if (!strncmp(service, "disconnect:", 11)) {
917 char buffer[4096];
918 memset(buffer, 0, sizeof(buffer));
919 char* serial = service + 11;
Mike Lockwood01c2c302010-05-24 10:44:35 -0400920 if (serial[0] == 0) {
921 // disconnect from all TCP devices
922 unregister_all_tcp_transports();
Mike Lockwood5f2c4a12009-10-11 23:04:18 -0400923 } else {
Mike Lockwood01c2c302010-05-24 10:44:35 -0400924 char hostbuf[100];
925 // assume port 5555 if no port is specified
926 if (!strchr(serial, ':')) {
927 snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
928 serial = hostbuf;
929 }
930 atransport *t = find_transport(serial);
931
932 if (t) {
933 unregister_transport(t);
934 } else {
935 snprintf(buffer, sizeof(buffer), "No such device %s", serial);
936 }
Mike Lockwood5f2c4a12009-10-11 23:04:18 -0400937 }
938
Snild Dolkow4516a872014-01-30 10:08:38 +0100939 send_msg_with_okay(reply_fd, buffer, strlen(buffer));
Mike Lockwoodb51ae572009-08-24 15:58:40 -0700940 return 0;
941 }
942
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800943 // returns our value for ADB_SERVER_VERSION
944 if (!strcmp(service, "version")) {
945 char version[12];
946 snprintf(version, sizeof version, "%04x", ADB_SERVER_VERSION);
Snild Dolkow4516a872014-01-30 10:08:38 +0100947 send_msg_with_okay(reply_fd, version, strlen(version));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800948 return 0;
949 }
950
951 if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800952 const char *out = "unknown";
953 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
Elliott Hughesab882422015-04-16 22:54:44 -0700954 if (transport && transport->serial) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800955 out = transport->serial;
956 }
Snild Dolkow4516a872014-01-30 10:08:38 +0100957 send_msg_with_okay(reply_fd, out, strlen(out));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800958 return 0;
959 }
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700960 if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800961 const char *out = "unknown";
962 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
Elliott Hughesab882422015-04-16 22:54:44 -0700963 if (transport && transport->devpath) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700964 out = transport->devpath;
965 }
Snild Dolkow4516a872014-01-30 10:08:38 +0100966 send_msg_with_okay(reply_fd, out, strlen(out));
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700967 return 0;
968 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800969 // indicates a new emulator instance has started
970 if (!strncmp(service,"emulator:",9)) {
971 int port = atoi(service+9);
972 local_connect(port);
973 /* we don't even need to send a reply */
974 return 0;
975 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800976
977 if(!strncmp(service,"get-state",strlen("get-state"))) {
978 transport = acquire_one_transport(CS_ANY, ttype, serial, NULL);
Dan Albertf30d73c2015-02-25 17:51:28 -0800979 const char *state = connection_state_name(transport);
Snild Dolkow4516a872014-01-30 10:08:38 +0100980 send_msg_with_okay(reply_fd, state, strlen(state));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800981 return 0;
982 }
Simon Yee51c11e2014-07-14 17:23:06 -0700983#endif // ADB_HOST
984
985 int ret = handle_forward_request(service, ttype, serial, reply_fd);
986 if (ret >= 0)
987 return ret - 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800988 return -1;
989}