• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <ctype.h>
27 #include <assert.h>
28 
29 #include "sysdeps.h"
30 
31 #ifdef HAVE_TERMIO_H
32 #include <termios.h>
33 #endif
34 
35 #define  TRACE_TAG  TRACE_ADB
36 #include "adb.h"
37 #include "adb_client.h"
38 #include "file_sync_service.h"
39 
40 enum {
41     IGNORE_DATA,
42     WIPE_DATA,
43     FLASH_DATA
44 };
45 
46 static int do_cmd(transport_type ttype, char* serial, char *cmd, ...);
47 
48 void get_my_path(char *s, size_t maxLen);
49 int find_sync_dirs(const char *srcarg,
50         char **android_srcdir_out, char **data_srcdir_out);
51 int install_app(transport_type transport, char* serial, int argc, char** argv);
52 int uninstall_app(transport_type transport, char* serial, int argc, char** argv);
53 
54 static const char *gProductOutPath = NULL;
55 
product_file(const char * extra)56 static char *product_file(const char *extra)
57 {
58     int n;
59     char *x;
60 
61     if (gProductOutPath == NULL) {
62         fprintf(stderr, "adb: Product directory not specified; "
63                 "use -p or define ANDROID_PRODUCT_OUT\n");
64         exit(1);
65     }
66 
67     n = strlen(gProductOutPath) + strlen(extra) + 2;
68     x = malloc(n);
69     if (x == 0) {
70         fprintf(stderr, "adb: Out of memory (product_file())\n");
71         exit(1);
72     }
73 
74     snprintf(x, (size_t)n, "%s" OS_PATH_SEPARATOR_STR "%s", gProductOutPath, extra);
75     return x;
76 }
77 
version(FILE * out)78 void version(FILE * out) {
79     fprintf(out, "Android Debug Bridge version %d.%d.%d\n",
80          ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION);
81 }
82 
help()83 void help()
84 {
85     version(stderr);
86 
87     fprintf(stderr,
88         "\n"
89         " -d                            - directs command to the only connected USB device\n"
90         "                                 returns an error if more than one USB device is present.\n"
91         " -e                            - directs command to the only running emulator.\n"
92         "                                 returns an error if more than one emulator is running.\n"
93         " -s <serial number>            - directs command to the USB device or emulator with\n"
94         "                                 the given serial number. Overrides ANDROID_SERIAL\n"
95         "                                 environment variable.\n"
96         " -p <product name or path>     - simple product name like 'sooner', or\n"
97         "                                 a relative/absolute path to a product\n"
98         "                                 out directory like 'out/target/product/sooner'.\n"
99         "                                 If -p is not specified, the ANDROID_PRODUCT_OUT\n"
100         "                                 environment variable is used, which must\n"
101         "                                 be an absolute path.\n"
102         " devices                       - list all connected devices\n"
103         " connect <host>[:<port>]       - connect to a device via TCP/IP\n"
104         "                                 Port 5555 is used by default if no port number is specified.\n"
105         " disconnect [<host>[:<port>]]  - disconnect from a TCP/IP device.\n"
106         "                                 Port 5555 is used by default if no port number is specified.\n"
107         "                                 Using this ocmmand with no additional arguments\n"
108         "                                 will disconnect from all connected TCP/IP devices.\n"
109         "\n"
110         "device commands:\n"
111         "  adb push <local> <remote>    - copy file/dir to device\n"
112         "  adb pull <remote> [<local>]  - copy file/dir from device\n"
113         "  adb sync [ <directory> ]     - copy host->device only if changed\n"
114         "                                 (-l means list but don't copy)\n"
115         "                                 (see 'adb help all')\n"
116         "  adb shell                    - run remote shell interactively\n"
117         "  adb shell <command>          - run remote shell command\n"
118         "  adb emu <command>            - run emulator console command\n"
119         "  adb logcat [ <filter-spec> ] - View device log\n"
120         "  adb forward <local> <remote> - forward socket connections\n"
121         "                                 forward specs are one of: \n"
122         "                                   tcp:<port>\n"
123         "                                   localabstract:<unix domain socket name>\n"
124         "                                   localreserved:<unix domain socket name>\n"
125         "                                   localfilesystem:<unix domain socket name>\n"
126         "                                   dev:<character device name>\n"
127         "                                   jdwp:<process pid> (remote only)\n"
128         "  adb jdwp                     - list PIDs of processes hosting a JDWP transport\n"
129         "  adb install [-l] [-r] [-s] <file> - push this package file to the device and install it\n"
130         "                                 ('-l' means forward-lock the app)\n"
131         "                                 ('-r' means reinstall the app, keeping its data)\n"
132         "                                 ('-s' means install on SD card instead of internal storage)\n"
133         "  adb uninstall [-k] <package> - remove this app package from the device\n"
134         "                                 ('-k' means keep the data and cache directories)\n"
135         "  adb bugreport                - return all information from the device\n"
136         "                                 that should be included in a bug report.\n"
137         "\n"
138         "  adb help                     - show this help message\n"
139         "  adb version                  - show version num\n"
140         "\n"
141         "DATAOPTS:\n"
142         " (no option)                   - don't touch the data partition\n"
143         "  -w                           - wipe the data partition\n"
144         "  -d                           - flash the data partition\n"
145         "\n"
146         "scripting:\n"
147         "  adb wait-for-device          - block until device is online\n"
148         "  adb start-server             - ensure that there is a server running\n"
149         "  adb kill-server              - kill the server if it is running\n"
150         "  adb get-state                - prints: offline | bootloader | device\n"
151         "  adb get-serialno             - prints: <serial-number>\n"
152         "  adb status-window            - continuously print device status for a specified device\n"
153         "  adb remount                  - remounts the /system partition on the device read-write\n"
154         "  adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program\n"
155         "  adb reboot-bootloader        - reboots the device into the bootloader\n"
156         "  adb root                     - restarts the adbd daemon with root permissions\n"
157         "  adb usb                      - restarts the adbd daemon listening on USB\n"
158         "  adb tcpip <port>             - restarts the adbd daemon listening on TCP on the specified port"
159         "\n"
160         "networking:\n"
161         "  adb ppp <tty> [parameters]   - Run PPP over USB.\n"
162         " Note: you should not automatically start a PPP connection.\n"
163         " <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1\n"
164         " [parameters] - Eg. defaultroute debug dump local notty usepeerdns\n"
165         "\n"
166         "adb sync notes: adb sync [ <directory> ]\n"
167         "  <localdir> can be interpreted in several ways:\n"
168         "\n"
169         "  - If <directory> is not specified, both /system and /data partitions will be updated.\n"
170         "\n"
171         "  - If it is \"system\" or \"data\", only the corresponding partition\n"
172         "    is updated.\n"
173         "\n"
174         "environmental variables:\n"
175         "  ADB_TRACE                    - Print debug information. A comma separated list of the following values\n"
176         "                                 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp\n"
177         "  ANDROID_SERIAL               - The serial number to connect to. -s takes priority over this if given.\n"
178         "  ANDROID_LOG_TAGS             - When used with the logcat option, only these debug tags are printed.\n"
179         );
180 }
181 
usage()182 int usage()
183 {
184     help();
185     return 1;
186 }
187 
188 #ifdef HAVE_TERMIO_H
189 static struct termios tio_save;
190 
stdin_raw_init(int fd)191 static void stdin_raw_init(int fd)
192 {
193     struct termios tio;
194 
195     if(tcgetattr(fd, &tio)) return;
196     if(tcgetattr(fd, &tio_save)) return;
197 
198     tio.c_lflag = 0; /* disable CANON, ECHO*, etc */
199 
200         /* no timeout but request at least one character per read */
201     tio.c_cc[VTIME] = 0;
202     tio.c_cc[VMIN] = 1;
203 
204     tcsetattr(fd, TCSANOW, &tio);
205     tcflush(fd, TCIFLUSH);
206 }
207 
stdin_raw_restore(int fd)208 static void stdin_raw_restore(int fd)
209 {
210     tcsetattr(fd, TCSANOW, &tio_save);
211     tcflush(fd, TCIFLUSH);
212 }
213 #endif
214 
read_and_dump(int fd)215 static void read_and_dump(int fd)
216 {
217     char buf[4096];
218     int len;
219 
220     while(fd >= 0) {
221         len = adb_read(fd, buf, 4096);
222         if(len == 0) {
223             break;
224         }
225 
226         if(len < 0) {
227             if(errno == EINTR) continue;
228             break;
229         }
230         fwrite(buf, 1, len, stdout);
231         fflush(stdout);
232     }
233 }
234 
stdin_read_thread(void * x)235 static void *stdin_read_thread(void *x)
236 {
237     int fd, fdi;
238     unsigned char buf[1024];
239     int r, n;
240     int state = 0;
241 
242     int *fds = (int*) x;
243     fd = fds[0];
244     fdi = fds[1];
245     free(fds);
246 
247     for(;;) {
248         /* fdi is really the client's stdin, so use read, not adb_read here */
249         r = unix_read(fdi, buf, 1024);
250         if(r == 0) break;
251         if(r < 0) {
252             if(errno == EINTR) continue;
253             break;
254         }
255         for(n = 0; n < r; n++){
256             switch(buf[n]) {
257             case '\n':
258                 state = 1;
259                 break;
260             case '\r':
261                 state = 1;
262                 break;
263             case '~':
264                 if(state == 1) state++;
265                 break;
266             case '.':
267                 if(state == 2) {
268                     fprintf(stderr,"\n* disconnect *\n");
269 #ifdef HAVE_TERMIO_H
270                     stdin_raw_restore(fdi);
271 #endif
272                     exit(0);
273                 }
274             default:
275                 state = 0;
276             }
277         }
278         r = adb_write(fd, buf, r);
279         if(r <= 0) {
280             break;
281         }
282     }
283     return 0;
284 }
285 
interactive_shell(void)286 int interactive_shell(void)
287 {
288     adb_thread_t thr;
289     int fdi, fd;
290     int *fds;
291 
292     fd = adb_connect("shell:");
293     if(fd < 0) {
294         fprintf(stderr,"error: %s\n", adb_error());
295         return 1;
296     }
297     fdi = 0; //dup(0);
298 
299     fds = malloc(sizeof(int) * 2);
300     fds[0] = fd;
301     fds[1] = fdi;
302 
303 #ifdef HAVE_TERMIO_H
304     stdin_raw_init(fdi);
305 #endif
306     adb_thread_create(&thr, stdin_read_thread, fds);
307     read_and_dump(fd);
308 #ifdef HAVE_TERMIO_H
309     stdin_raw_restore(fdi);
310 #endif
311     return 0;
312 }
313 
314 
format_host_command(char * buffer,size_t buflen,const char * command,transport_type ttype,const char * serial)315 static void format_host_command(char* buffer, size_t  buflen, const char* command, transport_type ttype, const char* serial)
316 {
317     if (serial) {
318         snprintf(buffer, buflen, "host-serial:%s:%s", serial, command);
319     } else {
320         const char* prefix = "host";
321         if (ttype == kTransportUsb)
322             prefix = "host-usb";
323         else if (ttype == kTransportLocal)
324             prefix = "host-local";
325 
326         snprintf(buffer, buflen, "%s:%s", prefix, command);
327     }
328 }
329 
status_window(transport_type ttype,const char * serial)330 static void status_window(transport_type ttype, const char* serial)
331 {
332     char command[4096];
333     char *state = 0;
334     char *laststate = 0;
335 
336         /* silence stderr */
337 #ifdef _WIN32
338     /* XXX: TODO */
339 #else
340     int  fd;
341     fd = unix_open("/dev/null", O_WRONLY);
342     dup2(fd, 2);
343     adb_close(fd);
344 #endif
345 
346     format_host_command(command, sizeof command, "get-state", ttype, serial);
347 
348     for(;;) {
349         adb_sleep_ms(250);
350 
351         if(state) {
352             free(state);
353             state = 0;
354         }
355 
356         state = adb_query(command);
357 
358         if(state) {
359             if(laststate && !strcmp(state,laststate)){
360                 continue;
361             } else {
362                 if(laststate) free(laststate);
363                 laststate = strdup(state);
364             }
365         }
366 
367         printf("%c[2J%c[2H", 27, 27);
368         printf("Android Debug Bridge\n");
369         printf("State: %s\n", state ? state : "offline");
370         fflush(stdout);
371     }
372 }
373 
374 /** duplicate string and quote all \ " ( ) chars + space character. */
375 static char *
dupAndQuote(const char * s)376 dupAndQuote(const char *s)
377 {
378     const char *ts;
379     size_t alloc_len;
380     char *ret;
381     char *dest;
382 
383     ts = s;
384 
385     alloc_len = 0;
386 
387     for( ;*ts != '\0'; ts++) {
388         alloc_len++;
389         if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
390             alloc_len++;
391         }
392     }
393 
394     ret = (char *)malloc(alloc_len + 1);
395 
396     ts = s;
397     dest = ret;
398 
399     for ( ;*ts != '\0'; ts++) {
400         if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
401             *dest++ = '\\';
402         }
403 
404         *dest++ = *ts;
405     }
406 
407     *dest++ = '\0';
408 
409     return ret;
410 }
411 
412 /**
413  * Run ppp in "notty" mode against a resource listed as the first parameter
414  * eg:
415  *
416  * ppp dev:/dev/omap_csmi_tty0 <ppp options>
417  *
418  */
ppp(int argc,char ** argv)419 int ppp(int argc, char **argv)
420 {
421 #ifdef HAVE_WIN32_PROC
422     fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
423     return -1;
424 #else
425     char *adb_service_name;
426     pid_t pid;
427     int fd;
428 
429     if (argc < 2) {
430         fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n",
431                 argv[0]);
432 
433         return 1;
434     }
435 
436     adb_service_name = argv[1];
437 
438     fd = adb_connect(adb_service_name);
439 
440     if(fd < 0) {
441         fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n",
442                 adb_service_name, adb_error());
443         return 1;
444     }
445 
446     pid = fork();
447 
448     if (pid < 0) {
449         perror("from fork()");
450         return 1;
451     } else if (pid == 0) {
452         int err;
453         int i;
454         const char **ppp_args;
455 
456         // copy args
457         ppp_args = (const char **) alloca(sizeof(char *) * argc + 1);
458         ppp_args[0] = "pppd";
459         for (i = 2 ; i < argc ; i++) {
460             //argv[2] and beyond become ppp_args[1] and beyond
461             ppp_args[i - 1] = argv[i];
462         }
463         ppp_args[i-1] = NULL;
464 
465         // child side
466 
467         dup2(fd, STDIN_FILENO);
468         dup2(fd, STDOUT_FILENO);
469         adb_close(STDERR_FILENO);
470         adb_close(fd);
471 
472         err = execvp("pppd", (char * const *)ppp_args);
473 
474         if (err < 0) {
475             perror("execing pppd");
476         }
477         exit(-1);
478     } else {
479         // parent side
480 
481         adb_close(fd);
482         return 0;
483     }
484 #endif /* !HAVE_WIN32_PROC */
485 }
486 
send_shellcommand(transport_type transport,char * serial,char * buf)487 static int send_shellcommand(transport_type transport, char* serial, char* buf)
488 {
489     int fd, ret;
490 
491     for(;;) {
492         fd = adb_connect(buf);
493         if(fd >= 0)
494             break;
495         fprintf(stderr,"- waiting for device -\n");
496         adb_sleep_ms(1000);
497         do_cmd(transport, serial, "wait-for-device", 0);
498     }
499 
500     read_and_dump(fd);
501     ret = adb_close(fd);
502     if (ret)
503         perror("close");
504 
505     return ret;
506 }
507 
logcat(transport_type transport,char * serial,int argc,char ** argv)508 static int logcat(transport_type transport, char* serial, int argc, char **argv)
509 {
510     char buf[4096];
511 
512     char *log_tags;
513     char *quoted_log_tags;
514 
515     log_tags = getenv("ANDROID_LOG_TAGS");
516     quoted_log_tags = dupAndQuote(log_tags == NULL ? "" : log_tags);
517 
518     snprintf(buf, sizeof(buf),
519         "shell:export ANDROID_LOG_TAGS=\"\%s\" ; exec logcat",
520         quoted_log_tags);
521 
522     free(quoted_log_tags);
523 
524     argc -= 1;
525     argv += 1;
526     while(argc-- > 0) {
527         char *quoted;
528 
529         quoted = dupAndQuote (*argv++);
530 
531         strncat(buf, " ", sizeof(buf)-1);
532         strncat(buf, quoted, sizeof(buf)-1);
533         free(quoted);
534     }
535 
536     send_shellcommand(transport, serial, buf);
537     return 0;
538 }
539 
540 #define SENTINEL_FILE "config" OS_PATH_SEPARATOR_STR "envsetup.make"
top_works(const char * top)541 static int top_works(const char *top)
542 {
543     if (top != NULL && adb_is_absolute_host_path(top)) {
544         char path_buf[PATH_MAX];
545         snprintf(path_buf, sizeof(path_buf),
546                 "%s" OS_PATH_SEPARATOR_STR SENTINEL_FILE, top);
547         return access(path_buf, F_OK) == 0;
548     }
549     return 0;
550 }
551 
find_top_from(const char * indir,char path_buf[PATH_MAX])552 static char *find_top_from(const char *indir, char path_buf[PATH_MAX])
553 {
554     strcpy(path_buf, indir);
555     while (1) {
556         if (top_works(path_buf)) {
557             return path_buf;
558         }
559         char *s = adb_dirstop(path_buf);
560         if (s != NULL) {
561             *s = '\0';
562         } else {
563             path_buf[0] = '\0';
564             return NULL;
565         }
566     }
567 }
568 
find_top(char path_buf[PATH_MAX])569 static char *find_top(char path_buf[PATH_MAX])
570 {
571     char *top = getenv("ANDROID_BUILD_TOP");
572     if (top != NULL && top[0] != '\0') {
573         if (!top_works(top)) {
574             fprintf(stderr, "adb: bad ANDROID_BUILD_TOP value \"%s\"\n", top);
575             return NULL;
576         }
577     } else {
578         top = getenv("TOP");
579         if (top != NULL && top[0] != '\0') {
580             if (!top_works(top)) {
581                 fprintf(stderr, "adb: bad TOP value \"%s\"\n", top);
582                 return NULL;
583             }
584         } else {
585             top = NULL;
586         }
587     }
588 
589     if (top != NULL) {
590         /* The environment pointed to a top directory that works.
591          */
592         strcpy(path_buf, top);
593         return path_buf;
594     }
595 
596     /* The environment didn't help.  Walk up the tree from the CWD
597      * to see if we can find the top.
598      */
599     char dir[PATH_MAX];
600     top = find_top_from(getcwd(dir, sizeof(dir)), path_buf);
601     if (top == NULL) {
602         /* If the CWD isn't under a good-looking top, see if the
603          * executable is.
604          */
605         get_my_path(dir, PATH_MAX);
606         top = find_top_from(dir, path_buf);
607     }
608     return top;
609 }
610 
611 /* <hint> may be:
612  * - A simple product name
613  *   e.g., "sooner"
614 TODO: debug?  sooner-debug, sooner:debug?
615  * - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir
616  *   e.g., "out/target/product/sooner"
617  * - An absolute path to the PRODUCT_OUT dir
618  *   e.g., "/src/device/out/target/product/sooner"
619  *
620  * Given <hint>, try to construct an absolute path to the
621  * ANDROID_PRODUCT_OUT dir.
622  */
find_product_out_path(const char * hint)623 static const char *find_product_out_path(const char *hint)
624 {
625     static char path_buf[PATH_MAX];
626 
627     if (hint == NULL || hint[0] == '\0') {
628         return NULL;
629     }
630 
631     /* If it's already absolute, don't bother doing any work.
632      */
633     if (adb_is_absolute_host_path(hint)) {
634         strcpy(path_buf, hint);
635         return path_buf;
636     }
637 
638     /* If there are any slashes in it, assume it's a relative path;
639      * make it absolute.
640      */
641     if (adb_dirstart(hint) != NULL) {
642         if (getcwd(path_buf, sizeof(path_buf)) == NULL) {
643             fprintf(stderr, "adb: Couldn't get CWD: %s\n", strerror(errno));
644             return NULL;
645         }
646         if (strlen(path_buf) + 1 + strlen(hint) >= sizeof(path_buf)) {
647             fprintf(stderr, "adb: Couldn't assemble path\n");
648             return NULL;
649         }
650         strcat(path_buf, OS_PATH_SEPARATOR_STR);
651         strcat(path_buf, hint);
652         return path_buf;
653     }
654 
655     /* It's a string without any slashes.  Try to do something with it.
656      *
657      * Try to find the root of the build tree, and build a PRODUCT_OUT
658      * path from there.
659      */
660     char top_buf[PATH_MAX];
661     const char *top = find_top(top_buf);
662     if (top == NULL) {
663         fprintf(stderr, "adb: Couldn't find top of build tree\n");
664         return NULL;
665     }
666 //TODO: if we have a way to indicate debug, look in out/debug/target/...
667     snprintf(path_buf, sizeof(path_buf),
668             "%s" OS_PATH_SEPARATOR_STR
669             "out" OS_PATH_SEPARATOR_STR
670             "target" OS_PATH_SEPARATOR_STR
671             "product" OS_PATH_SEPARATOR_STR
672             "%s", top_buf, hint);
673     if (access(path_buf, F_OK) < 0) {
674         fprintf(stderr, "adb: Couldn't find a product dir "
675                 "based on \"-p %s\"; \"%s\" doesn't exist\n", hint, path_buf);
676         return NULL;
677     }
678     return path_buf;
679 }
680 
adb_commandline(int argc,char ** argv)681 int adb_commandline(int argc, char **argv)
682 {
683     char buf[4096];
684     int no_daemon = 0;
685     int is_daemon = 0;
686     int persist = 0;
687     int r;
688     int quote;
689     transport_type ttype = kTransportAny;
690     char* serial = NULL;
691     char* server_port_str = NULL;
692 
693         /* If defined, this should be an absolute path to
694          * the directory containing all of the various system images
695          * for a particular product.  If not defined, and the adb
696          * command requires this information, then the user must
697          * specify the path using "-p".
698          */
699     gProductOutPath = getenv("ANDROID_PRODUCT_OUT");
700     if (gProductOutPath == NULL || gProductOutPath[0] == '\0') {
701         gProductOutPath = NULL;
702     }
703     // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
704 
705     serial = getenv("ANDROID_SERIAL");
706 
707     /* Validate and assign the server port */
708     server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
709     int server_port = DEFAULT_ADB_PORT;
710     if (server_port_str && strlen(server_port_str) > 0) {
711         server_port = (int) strtol(server_port_str, NULL, 0);
712         if (server_port <= 0) {
713             fprintf(stderr,
714                     "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number. Got \"%s\"\n",
715                     server_port_str);
716             return usage();
717         }
718     }
719 
720     /* modifiers and flags */
721     while(argc > 0) {
722         if(!strcmp(argv[0],"nodaemon")) {
723             no_daemon = 1;
724         } else if (!strcmp(argv[0], "fork-server")) {
725             /* this is a special flag used only when the ADB client launches the ADB Server */
726             is_daemon = 1;
727         } else if(!strcmp(argv[0],"persist")) {
728             persist = 1;
729         } else if(!strncmp(argv[0], "-p", 2)) {
730             const char *product = NULL;
731             if (argv[0][2] == '\0') {
732                 if (argc < 2) return usage();
733                 product = argv[1];
734                 argc--;
735                 argv++;
736             } else {
737                 product = argv[1] + 2;
738             }
739             gProductOutPath = find_product_out_path(product);
740             if (gProductOutPath == NULL) {
741                 fprintf(stderr, "adb: could not resolve \"-p %s\"\n",
742                         product);
743                 return usage();
744             }
745         } else if (argv[0][0]=='-' && argv[0][1]=='s') {
746             if (isdigit(argv[0][2])) {
747                 serial = argv[0] + 2;
748             } else {
749                 if(argc < 2 || argv[0][2] != '\0') return usage();
750                 serial = argv[1];
751                 argc--;
752                 argv++;
753             }
754         } else if (!strcmp(argv[0],"-d")) {
755             ttype = kTransportUsb;
756         } else if (!strcmp(argv[0],"-e")) {
757             ttype = kTransportLocal;
758         } else {
759                 /* out of recognized modifiers and flags */
760             break;
761         }
762         argc--;
763         argv++;
764     }
765 
766     adb_set_transport(ttype, serial);
767     adb_set_tcp_specifics(server_port);
768 
769     if ((argc > 0) && (!strcmp(argv[0],"server"))) {
770         if (no_daemon || is_daemon) {
771             r = adb_main(is_daemon, server_port);
772         } else {
773             r = launch_server(server_port);
774         }
775         if(r) {
776             fprintf(stderr,"* could not start server *\n");
777         }
778         return r;
779     }
780 
781 top:
782     if(argc == 0) {
783         return usage();
784     }
785 
786     /* adb_connect() commands */
787 
788     if(!strcmp(argv[0], "devices")) {
789         char *tmp;
790         snprintf(buf, sizeof buf, "host:%s", argv[0]);
791         tmp = adb_query(buf);
792         if(tmp) {
793             printf("List of devices attached \n");
794             printf("%s\n", tmp);
795             return 0;
796         } else {
797             return 1;
798         }
799     }
800 
801     if(!strcmp(argv[0], "connect")) {
802         char *tmp;
803         if (argc != 2) {
804             fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
805             return 1;
806         }
807         snprintf(buf, sizeof buf, "host:connect:%s", argv[1]);
808         tmp = adb_query(buf);
809         if(tmp) {
810             printf("%s\n", tmp);
811             return 0;
812         } else {
813             return 1;
814         }
815     }
816 
817     if(!strcmp(argv[0], "disconnect")) {
818         char *tmp;
819         if (argc > 2) {
820             fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
821             return 1;
822         }
823         if (argc == 2) {
824             snprintf(buf, sizeof buf, "host:disconnect:%s", argv[1]);
825         } else {
826             snprintf(buf, sizeof buf, "host:disconnect:");
827         }
828         tmp = adb_query(buf);
829         if(tmp) {
830             printf("%s\n", tmp);
831             return 0;
832         } else {
833             return 1;
834         }
835     }
836 
837     if (!strcmp(argv[0], "emu")) {
838         return adb_send_emulator_command(argc, argv);
839     }
840 
841     if(!strcmp(argv[0], "shell")) {
842         int r;
843         int fd;
844 
845         if(argc < 2) {
846             return interactive_shell();
847         }
848 
849         snprintf(buf, sizeof buf, "shell:%s", argv[1]);
850         argc -= 2;
851         argv += 2;
852         while(argc-- > 0) {
853             strcat(buf, " ");
854 
855             /* quote empty strings and strings with spaces */
856             quote = (**argv == 0 || strchr(*argv, ' '));
857             if (quote)
858                 strcat(buf, "\"");
859             strcat(buf, *argv++);
860             if (quote)
861                 strcat(buf, "\"");
862         }
863 
864         for(;;) {
865             fd = adb_connect(buf);
866             if(fd >= 0) {
867                 read_and_dump(fd);
868                 adb_close(fd);
869                 r = 0;
870             } else {
871                 fprintf(stderr,"error: %s\n", adb_error());
872                 r = -1;
873             }
874 
875             if(persist) {
876                 fprintf(stderr,"\n- waiting for device -\n");
877                 adb_sleep_ms(1000);
878                 do_cmd(ttype, serial, "wait-for-device", 0);
879             } else {
880                 return r;
881             }
882         }
883     }
884 
885     if(!strcmp(argv[0], "kill-server")) {
886         int fd;
887         fd = _adb_connect("host:kill");
888         if(fd == -1) {
889             fprintf(stderr,"* server not running *\n");
890             return 1;
891         }
892         return 0;
893     }
894 
895     if(!strcmp(argv[0], "remount") || !strcmp(argv[0], "reboot")
896             || !strcmp(argv[0], "reboot-bootloader")
897             || !strcmp(argv[0], "tcpip") || !strcmp(argv[0], "usb")
898             || !strcmp(argv[0], "root")) {
899         char command[100];
900         if (!strcmp(argv[0], "reboot-bootloader"))
901             snprintf(command, sizeof(command), "reboot:bootloader");
902         else if (argc > 1)
903             snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]);
904         else
905             snprintf(command, sizeof(command), "%s:", argv[0]);
906         int fd = adb_connect(command);
907         if(fd >= 0) {
908             read_and_dump(fd);
909             adb_close(fd);
910             return 0;
911         }
912         fprintf(stderr,"error: %s\n", adb_error());
913         return 1;
914     }
915 
916     if(!strcmp(argv[0], "bugreport")) {
917         if (argc != 1) return usage();
918         do_cmd(ttype, serial, "shell", "bugreport", 0);
919         return 0;
920     }
921 
922     /* adb_command() wrapper commands */
923 
924     if(!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
925         char* service = argv[0];
926         if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) {
927             if (ttype == kTransportUsb) {
928                 service = "wait-for-usb";
929             } else if (ttype == kTransportLocal) {
930                 service = "wait-for-local";
931             } else {
932                 service = "wait-for-any";
933             }
934         }
935 
936         format_host_command(buf, sizeof buf, service, ttype, serial);
937 
938         if (adb_command(buf)) {
939             D("failure: %s *\n",adb_error());
940             fprintf(stderr,"error: %s\n", adb_error());
941             return 1;
942         }
943 
944         /* Allow a command to be run after wait-for-device,
945             * e.g. 'adb wait-for-device shell'.
946             */
947         if(argc > 1) {
948             argc--;
949             argv++;
950             goto top;
951         }
952         return 0;
953     }
954 
955     if(!strcmp(argv[0], "forward")) {
956         if(argc != 3) return usage();
957         if (serial) {
958             snprintf(buf, sizeof buf, "host-serial:%s:forward:%s;%s",serial, argv[1], argv[2]);
959         } else if (ttype == kTransportUsb) {
960             snprintf(buf, sizeof buf, "host-usb:forward:%s;%s", argv[1], argv[2]);
961         } else if (ttype == kTransportLocal) {
962             snprintf(buf, sizeof buf, "host-local:forward:%s;%s", argv[1], argv[2]);
963         } else {
964             snprintf(buf, sizeof buf, "host:forward:%s;%s", argv[1], argv[2]);
965         }
966         if(adb_command(buf)) {
967             fprintf(stderr,"error: %s\n", adb_error());
968             return 1;
969         }
970         return 0;
971     }
972 
973     /* do_sync_*() commands */
974 
975     if(!strcmp(argv[0], "ls")) {
976         if(argc != 2) return usage();
977         return do_sync_ls(argv[1]);
978     }
979 
980     if(!strcmp(argv[0], "push")) {
981         if(argc != 3) return usage();
982         return do_sync_push(argv[1], argv[2], 0 /* no verify APK */);
983     }
984 
985     if(!strcmp(argv[0], "pull")) {
986         if (argc == 2) {
987             return do_sync_pull(argv[1], ".");
988         } else if (argc == 3) {
989             return do_sync_pull(argv[1], argv[2]);
990         } else {
991             return usage();
992         }
993     }
994 
995     if(!strcmp(argv[0], "install")) {
996         if (argc < 2) return usage();
997         return install_app(ttype, serial, argc, argv);
998     }
999 
1000     if(!strcmp(argv[0], "uninstall")) {
1001         if (argc < 2) return usage();
1002         return uninstall_app(ttype, serial, argc, argv);
1003     }
1004 
1005     if(!strcmp(argv[0], "sync")) {
1006         char *srcarg, *android_srcpath, *data_srcpath;
1007         int listonly = 0;
1008 
1009         int ret;
1010         if(argc < 2) {
1011             /* No local path was specified. */
1012             srcarg = NULL;
1013         } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
1014             listonly = 1;
1015             if (argc == 3) {
1016                 srcarg = argv[2];
1017             } else {
1018                 srcarg = NULL;
1019             }
1020         } else if(argc == 2) {
1021             /* A local path or "android"/"data" arg was specified. */
1022             srcarg = argv[1];
1023         } else {
1024             return usage();
1025         }
1026         ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath);
1027         if(ret != 0) return usage();
1028 
1029         if(android_srcpath != NULL)
1030             ret = do_sync_sync(android_srcpath, "/system", listonly);
1031         if(ret == 0 && data_srcpath != NULL)
1032             ret = do_sync_sync(data_srcpath, "/data", listonly);
1033 
1034         free(android_srcpath);
1035         free(data_srcpath);
1036         return ret;
1037     }
1038 
1039     /* passthrough commands */
1040 
1041     if(!strcmp(argv[0],"get-state") ||
1042         !strcmp(argv[0],"get-serialno"))
1043     {
1044         char *tmp;
1045 
1046         format_host_command(buf, sizeof buf, argv[0], ttype, serial);
1047         tmp = adb_query(buf);
1048         if(tmp) {
1049             printf("%s\n", tmp);
1050             return 0;
1051         } else {
1052             return 1;
1053         }
1054     }
1055 
1056     /* other commands */
1057 
1058     if(!strcmp(argv[0],"status-window")) {
1059         status_window(ttype, serial);
1060         return 0;
1061     }
1062 
1063     if(!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat")) {
1064         return logcat(ttype, serial, argc, argv);
1065     }
1066 
1067     if(!strcmp(argv[0],"ppp")) {
1068         return ppp(argc, argv);
1069     }
1070 
1071     if (!strcmp(argv[0], "start-server")) {
1072         return adb_connect("host:start-server");
1073     }
1074 
1075     if (!strcmp(argv[0], "jdwp")) {
1076         int  fd = adb_connect("jdwp");
1077         if (fd >= 0) {
1078             read_and_dump(fd);
1079             adb_close(fd);
1080             return 0;
1081         } else {
1082             fprintf(stderr, "error: %s\n", adb_error());
1083             return -1;
1084         }
1085     }
1086 
1087     /* "adb /?" is a common idiom under Windows */
1088     if(!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
1089         help();
1090         return 0;
1091     }
1092 
1093     if(!strcmp(argv[0], "version")) {
1094         version(stdout);
1095         return 0;
1096     }
1097 
1098     usage();
1099     return 1;
1100 }
1101 
do_cmd(transport_type ttype,char * serial,char * cmd,...)1102 static int do_cmd(transport_type ttype, char* serial, char *cmd, ...)
1103 {
1104     char *argv[16];
1105     int argc;
1106     va_list ap;
1107 
1108     va_start(ap, cmd);
1109     argc = 0;
1110 
1111     if (serial) {
1112         argv[argc++] = "-s";
1113         argv[argc++] = serial;
1114     } else if (ttype == kTransportUsb) {
1115         argv[argc++] = "-d";
1116     } else if (ttype == kTransportLocal) {
1117         argv[argc++] = "-e";
1118     }
1119 
1120     argv[argc++] = cmd;
1121     while((argv[argc] = va_arg(ap, char*)) != 0) argc++;
1122     va_end(ap);
1123 
1124 #if 0
1125     int n;
1126     fprintf(stderr,"argc = %d\n",argc);
1127     for(n = 0; n < argc; n++) {
1128         fprintf(stderr,"argv[%d] = \"%s\"\n", n, argv[n]);
1129     }
1130 #endif
1131 
1132     return adb_commandline(argc, argv);
1133 }
1134 
find_sync_dirs(const char * srcarg,char ** android_srcdir_out,char ** data_srcdir_out)1135 int find_sync_dirs(const char *srcarg,
1136         char **android_srcdir_out, char **data_srcdir_out)
1137 {
1138     char *android_srcdir, *data_srcdir;
1139 
1140     if(srcarg == NULL) {
1141         android_srcdir = product_file("system");
1142         data_srcdir = product_file("data");
1143     } else {
1144         /* srcarg may be "data", "system" or NULL.
1145          * if srcarg is NULL, then both data and system are synced
1146          */
1147         if(strcmp(srcarg, "system") == 0) {
1148             android_srcdir = product_file("system");
1149             data_srcdir = NULL;
1150         } else if(strcmp(srcarg, "data") == 0) {
1151             android_srcdir = NULL;
1152             data_srcdir = product_file("data");
1153         } else {
1154             /* It's not "system" or "data".
1155              */
1156             return 1;
1157         }
1158     }
1159 
1160     if(android_srcdir_out != NULL)
1161         *android_srcdir_out = android_srcdir;
1162     else
1163         free(android_srcdir);
1164 
1165     if(data_srcdir_out != NULL)
1166         *data_srcdir_out = data_srcdir;
1167     else
1168         free(data_srcdir);
1169 
1170     return 0;
1171 }
1172 
pm_command(transport_type transport,char * serial,int argc,char ** argv)1173 static int pm_command(transport_type transport, char* serial,
1174                       int argc, char** argv)
1175 {
1176     char buf[4096];
1177 
1178     snprintf(buf, sizeof(buf), "shell:pm");
1179 
1180     while(argc-- > 0) {
1181         char *quoted;
1182 
1183         quoted = dupAndQuote(*argv++);
1184 
1185         strncat(buf, " ", sizeof(buf)-1);
1186         strncat(buf, quoted, sizeof(buf)-1);
1187         free(quoted);
1188     }
1189 
1190     send_shellcommand(transport, serial, buf);
1191     return 0;
1192 }
1193 
uninstall_app(transport_type transport,char * serial,int argc,char ** argv)1194 int uninstall_app(transport_type transport, char* serial, int argc, char** argv)
1195 {
1196     /* if the user choose the -k option, we refuse to do it until devices are
1197        out with the option to uninstall the remaining data somehow (adb/ui) */
1198     if (argc == 3 && strcmp(argv[1], "-k") == 0)
1199     {
1200         printf(
1201             "The -k option uninstalls the application while retaining the data/cache.\n"
1202             "At the moment, there is no way to remove the remaining data.\n"
1203             "You will have to reinstall the application with the same signature, and fully uninstall it.\n"
1204             "If you truly wish to continue, execute 'adb shell pm uninstall -k %s'\n", argv[2]);
1205         return -1;
1206     }
1207 
1208     /* 'adb uninstall' takes the same arguments as 'pm uninstall' on device */
1209     return pm_command(transport, serial, argc, argv);
1210 }
1211 
delete_file(transport_type transport,char * serial,char * filename)1212 static int delete_file(transport_type transport, char* serial, char* filename)
1213 {
1214     char buf[4096];
1215     char* quoted;
1216 
1217     snprintf(buf, sizeof(buf), "shell:rm ");
1218     quoted = dupAndQuote(filename);
1219     strncat(buf, quoted, sizeof(buf)-1);
1220     free(quoted);
1221 
1222     send_shellcommand(transport, serial, buf);
1223     return 0;
1224 }
1225 
install_app(transport_type transport,char * serial,int argc,char ** argv)1226 int install_app(transport_type transport, char* serial, int argc, char** argv)
1227 {
1228     struct stat st;
1229     int err;
1230     const char *const DATA_DEST = "/data/local/tmp/%s";
1231     const char *const SD_DEST = "/sdcard/tmp/%s";
1232     const char* where = DATA_DEST;
1233     char to[PATH_MAX];
1234     char* filename = argv[argc - 1];
1235     const char* p;
1236     int i;
1237 
1238     for (i = 0; i < argc; i++) {
1239         if (!strcmp(argv[i], "-s"))
1240             where = SD_DEST;
1241     }
1242 
1243     p = adb_dirstop(filename);
1244     if (p) {
1245         p++;
1246         snprintf(to, sizeof to, where, p);
1247     } else {
1248         snprintf(to, sizeof to, where, filename);
1249     }
1250     if (p[0] == '\0') {
1251     }
1252 
1253     err = stat(filename, &st);
1254     if (err != 0) {
1255         fprintf(stderr, "can't find '%s' to install\n", filename);
1256         return 1;
1257     }
1258     if (!S_ISREG(st.st_mode)) {
1259         fprintf(stderr, "can't install '%s' because it's not a file\n",
1260                 filename);
1261         return 1;
1262     }
1263 
1264     if (!(err = do_sync_push(filename, to, 1 /* verify APK */))) {
1265         /* file in place; tell the Package Manager to install it */
1266         argv[argc - 1] = to;       /* destination name, not source location */
1267         pm_command(transport, serial, argc, argv);
1268         delete_file(transport, serial, to);
1269     }
1270 
1271     return err;
1272 }
1273