static bool deploy_agent(bool checkTimeStamps) { std::vector<const char*> srcs; std::string jar_path = get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar"); std::string script_path = get_agent_component_host_path("/system/bin/deployagent", "/deployagent"); srcs.push_back(jar_path.c_str()); srcs.push_back(script_path.c_str()); if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) { // on windows the shell script might have lost execute permission // so need to set this explicitly const char* kChmodCommandPattern = "chmod 777 %sdeployagent"; std::string chmodCommand = android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath); int ret = send_shell_command(chmodCommand); if (ret != 0) { error_exit("Error executing %s returncode: %d", chmodCommand.c_str(), ret); } } else { error_exit("Error pushing agent files to device"); } return true; }
// Watcher void *watcher(void *path) { char * moved_from = 0; int events = IN_CREATE | IN_MOVED_TO | IN_MOVED_FROM | IN_DELETE | IN_MODIFY; int plen = strlen((char *) path); // initialize and watch the entire directory tree from the current working // directory downwards for all events if (!inotifytools_initialize() || !inotifytools_watch_recursively(path, events)) { fprintf(stderr, "%s\n", strerror(inotifytools_error())); return -1; } else { fprintf(stderr, "Watching %s\n", (char *) path); } // set time format to 24 hour time, HH:MM:SS inotifytools_set_printf_timefmt("%T"); // Output all events as "<timestamp> <path> <events>" struct inotify_event * event = inotifytools_next_event(-1); while (event) { inotifytools_printf(event, "%T %w%f %e\n"); // For recursivity if ((event->mask & IN_CREATE) || (!moved_from && (event->mask & IN_MOVED_TO))) { // New file - if it is a directory, watch it static char * new_file; nasprintf(&new_file, "%s%s", inotifytools_filename_from_wd( event->wd), event->name); //ACTION if(isdir(new_file)) { int fd; char buf[4096]; snprintf(buf, sizeof buf, "shell:mkdir \"%s\%s\"", fdir( inotifytools_filename_from_wd(event->wd), plen), event->name); fd = adb_connect(buf); adb_read(fd, buf, 4096); if (fd < 0) { fprintf(stderr,"error: %s\n", adb_error()); return -1; } adb_close(fd); } else { do_sync_push(new_file, fdir(inotifytools_filename_from_wd( event->wd), plen), 0); } if (isdir(new_file) && !inotifytools_watch_recursively(new_file, events)) { fprintf(stderr, "Couldn't watch new directory %s: %s\n", new_file, strerror(inotifytools_error())); } free(new_file); } // IN_CREATE else if (event->mask & IN_MOVED_FROM) {
int install_app(transport_type transport, char* serial, int argc, char** argv) { struct stat st; int err; const char *const DATA_DEST = "/data/local/tmp/%s"; const char *const SD_DEST = "/sdcard/tmp/%s"; const char* where = DATA_DEST; char to[PATH_MAX]; char* filename = argv[argc - 1]; const char* p; int i; for (i = 0; i < argc; i++) { if (!strcmp(argv[i], "-s")) where = SD_DEST; } p = adb_dirstop(filename); if (p) { p++; snprintf(to, sizeof to, where, p); } else { snprintf(to, sizeof to, where, filename); } if (p[0] == '\0') { } err = stat(filename, &st); if (err != 0) { fprintf(stderr, "can't find '%s' to install\n", filename); return 1; } if (!S_ISREG(st.st_mode)) { fprintf(stderr, "can't install '%s' because it's not a file\n", filename); return 1; } if (!(err = do_sync_push(filename, to, 1 /* verify APK */))) { /* file in place; tell the Package Manager to install it */ argv[argc - 1] = to; /* destination name, not source location */ pm_command(transport, serial, argc, argv); delete_file(transport, serial, to); } return err; }
int adb_commandline(int argc, char **argv) { char buf[4096]; int no_daemon = 0; int is_daemon = 0; int persist = 0; int r; int quote; transport_type ttype = kTransportAny; char* serial = NULL; /* If defined, this should be an absolute path to * the directory containing all of the various system images * for a particular product. If not defined, and the adb * command requires this information, then the user must * specify the path using "-p". */ gProductOutPath = getenv("ANDROID_PRODUCT_OUT"); if (gProductOutPath == NULL || gProductOutPath[0] == '\0') { gProductOutPath = NULL; } // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint serial = getenv("ANDROID_SERIAL"); /* modifiers and flags */ while(argc > 0) { if(!strcmp(argv[0],"nodaemon")) { no_daemon = 1; } else if (!strcmp(argv[0], "fork-server")) { /* this is a special flag used only when the ADB client launches the ADB Server */ is_daemon = 1; } else if(!strcmp(argv[0],"persist")) { persist = 1; } else if(!strncmp(argv[0], "-p", 2)) { const char *product = NULL; if (argv[0][2] == '\0') { if (argc < 2) return usage(); product = argv[1]; argc--; argv++; } else { product = argv[1] + 2; } gProductOutPath = find_product_out_path(product); if (gProductOutPath == NULL) { fprintf(stderr, "adb: could not resolve \"-p %s\"\n", product); return usage(); } } else if (argv[0][0]=='-' && argv[0][1]=='s') { if (isdigit(argv[0][2])) { serial = argv[0] + 2; } else { if(argc < 2) return usage(); serial = argv[1]; argc--; argv++; } } else if (!strcmp(argv[0],"-d")) { ttype = kTransportUsb; } else if (!strcmp(argv[0],"-e")) { ttype = kTransportLocal; } else { /* out of recognized modifiers and flags */ break; } argc--; argv++; } adb_set_transport(ttype, serial); if ((argc > 0) && (!strcmp(argv[0],"server"))) { if (no_daemon || is_daemon) { r = adb_main(is_daemon); } else { r = launch_server(); } if(r) { fprintf(stderr,"* could not start server *\n"); } return r; } top: if(argc == 0) { return usage(); } /* adb_connect() commands */ if(!strcmp(argv[0], "devices")) { char *tmp; snprintf(buf, sizeof buf, "host:%s", argv[0]); tmp = adb_query(buf); if(tmp) { printf("List of devices attached \n"); printf("%s\n", tmp); return 0; } else { return 1; } } if(!strcmp(argv[0], "connect") || !strcmp(argv[0], "disconnect")) { char *tmp; if (argc != 2) { fprintf(stderr, "Usage: adb %s <host>:<port>\n", argv[0]); return 1; } snprintf(buf, sizeof buf, "host:%s:%s", argv[0], argv[1]); tmp = adb_query(buf); if(tmp) { printf("%s\n", tmp); return 0; } else { return 1; } } if (!strcmp(argv[0], "emu")) { return adb_send_emulator_command(argc, argv); } if(!strcmp(argv[0], "shell")) { int r; int fd; if(argc < 2) { return interactive_shell(); } snprintf(buf, sizeof buf, "shell:%s", argv[1]); argc -= 2; argv += 2; while(argc-- > 0) { strcat(buf, " "); /* quote empty strings and strings with spaces */ quote = (**argv == 0 || strchr(*argv, ' ')); if (quote) strcat(buf, "\""); strcat(buf, *argv++); if (quote) strcat(buf, "\""); } for(;;) { fd = adb_connect(buf); if(fd >= 0) { read_and_dump(fd); adb_close(fd); r = 0; } else { fprintf(stderr,"error: %s\n", adb_error()); r = -1; } if(persist) { fprintf(stderr,"\n- waiting for device -\n"); adb_sleep_ms(1000); do_cmd(ttype, serial, "wait-for-device", 0); } else { return r; } } } if(!strcmp(argv[0], "kill-server")) { int fd; fd = _adb_connect("host:kill"); if(fd == -1) { fprintf(stderr,"* server not running *\n"); return 1; } return 0; } if(!strcmp(argv[0], "remount") || !strcmp(argv[0], "reboot") || !strcmp(argv[0], "reboot-bootloader") || !strcmp(argv[0], "tcpip") || !strcmp(argv[0], "usb") || !strcmp(argv[0], "root")) { char command[100]; if (!strcmp(argv[0], "reboot-bootloader")) snprintf(command, sizeof(command), "reboot:bootloader"); else if (argc > 1) snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]); else snprintf(command, sizeof(command), "%s:", argv[0]); int fd = adb_connect(command); if(fd >= 0) { read_and_dump(fd); adb_close(fd); return 0; } fprintf(stderr,"error: %s\n", adb_error()); return 1; } if(!strcmp(argv[0], "bugreport")) { if (argc != 1) return usage(); do_cmd(ttype, serial, "shell", "bugreport", 0); return 0; } /* adb_command() wrapper commands */ if(!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) { char* service = argv[0]; if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) { if (ttype == kTransportUsb) { service = "wait-for-usb"; } else if (ttype == kTransportLocal) { service = "wait-for-local"; } else { service = "wait-for-any"; } } format_host_command(buf, sizeof buf, service, ttype, serial); if (adb_command(buf)) { D("failure: %s *\n",adb_error()); fprintf(stderr,"error: %s\n", adb_error()); return 1; } /* Allow a command to be run after wait-for-device, * e.g. 'adb wait-for-device shell'. */ if(argc > 1) { argc--; argv++; goto top; } return 0; } if(!strcmp(argv[0], "forward")) { if(argc != 3) return usage(); if (serial) { snprintf(buf, sizeof buf, "host-serial:%s:forward:%s;%s",serial, argv[1], argv[2]); } else if (ttype == kTransportUsb) { snprintf(buf, sizeof buf, "host-usb:forward:%s;%s", argv[1], argv[2]); } else if (ttype == kTransportLocal) { snprintf(buf, sizeof buf, "host-local:forward:%s;%s", argv[1], argv[2]); } else { snprintf(buf, sizeof buf, "host:forward:%s;%s", argv[1], argv[2]); } if(adb_command(buf)) { fprintf(stderr,"error: %s\n", adb_error()); return 1; } return 0; } /* do_sync_*() commands */ if(!strcmp(argv[0], "ls")) { if(argc != 2) return usage(); return do_sync_ls(argv[1]); } if(!strcmp(argv[0], "push")) { if(argc != 3) return usage(); return do_sync_push(argv[1], argv[2], 0 /* no verify APK */); } if(!strcmp(argv[0], "pull")) { if (argc == 2) { return do_sync_pull(argv[1], "."); } else if (argc == 3) { return do_sync_pull(argv[1], argv[2]); } else { return usage(); } } if(!strcmp(argv[0], "install")) { if (argc < 2) return usage(); return install_app(ttype, serial, argc, argv); } if(!strcmp(argv[0], "uninstall")) { if (argc < 2) return usage(); return uninstall_app(ttype, serial, argc, argv); } if(!strcmp(argv[0], "sync")) { char *srcarg, *android_srcpath, *data_srcpath; int listonly = 0; int ret; if(argc < 2) { /* No local path was specified. */ srcarg = NULL; } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) { listonly = 1; if (argc == 3) { srcarg = argv[2]; } else { srcarg = NULL; } } else if(argc == 2) { /* A local path or "android"/"data" arg was specified. */ srcarg = argv[1]; } else { return usage(); } ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath); if(ret != 0) return usage(); if(android_srcpath != NULL) ret = do_sync_sync(android_srcpath, "/system", listonly); if(ret == 0 && data_srcpath != NULL) ret = do_sync_sync(data_srcpath, "/data", listonly); free(android_srcpath); free(data_srcpath); return ret; } /* passthrough commands */ if(!strcmp(argv[0],"get-state") || !strcmp(argv[0],"get-serialno")) { char *tmp; format_host_command(buf, sizeof buf, argv[0], ttype, serial); tmp = adb_query(buf); if(tmp) { printf("%s\n", tmp); return 0; } else { return 1; } } /* other commands */ if(!strcmp(argv[0],"status-window")) { status_window(ttype, serial); return 0; } if(!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat")) { return logcat(ttype, serial, argc, argv); } if(!strcmp(argv[0],"ppp")) { return ppp(argc, argv); } if (!strcmp(argv[0], "start-server")) { return adb_connect("host:start-server"); } if (!strcmp(argv[0], "jdwp")) { int fd = adb_connect("jdwp"); if (fd >= 0) { read_and_dump(fd); adb_close(fd); return 0; } else { fprintf(stderr, "error: %s\n", adb_error()); return -1; } } /* "adb /?" is a common idiom under Windows */ if(!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) { help(); return 0; } if(!strcmp(argv[0], "version")) { version(stdout); return 0; } usage(); return 1; }
int install_app(transport_type transport, char* serial, int argc, char** argv) { static const char *const DATA_DEST = "/data/local/tmp/%s"; static const char *const SD_DEST = "/sdcard/tmp/%s"; const char* where = DATA_DEST; char apk_dest[PATH_MAX]; char verification_dest[PATH_MAX]; char* apk_file; char* verification_file = NULL; int file_arg = -1; int err; int i; int verify_apk = 1; for (i = 1; i < argc; i++) { if (*argv[i] != '-') { file_arg = i; break; } else if (!strcmp(argv[i], "-i")) { // Skip the installer package name. i++; } else if (!strcmp(argv[i], "-s")) { where = SD_DEST; } else if (!strcmp(argv[i], "--algo")) { verify_apk = 0; i++; } else if (!strcmp(argv[i], "--iv")) { verify_apk = 0; i++; } else if (!strcmp(argv[i], "--key")) { verify_apk = 0; i++; } } if (file_arg < 0) { fprintf(stderr, "can't find filename in arguments\n"); return 1; } else if (file_arg + 2 < argc) { fprintf(stderr, "too many files specified; only takes APK file and verifier file\n"); return 1; } apk_file = argv[file_arg]; if (file_arg != argc - 1) { verification_file = argv[file_arg + 1]; } if (check_file(apk_file) || check_file(verification_file)) { return 1; } snprintf(apk_dest, sizeof apk_dest, where, get_basename(apk_file)); if (verification_file != NULL) { snprintf(verification_dest, sizeof(verification_dest), where, get_basename(verification_file)); if (!strcmp(apk_dest, verification_dest)) { fprintf(stderr, "APK and verification file can't have the same name\n"); return 1; } } err = do_sync_push(apk_file, apk_dest, verify_apk); if (err) { goto cleanup_apk; } else { argv[file_arg] = apk_dest; /* destination name, not source location */ } if (verification_file != NULL) { err = do_sync_push(verification_file, verification_dest, 0 /* no verify APK */); if (err) { goto cleanup_apk; } else { argv[file_arg + 1] = verification_dest; /* destination name, not source location */ } } pm_command(transport, serial, argc, argv); cleanup_apk: if (verification_file != NULL) { delete_file(transport, serial, verification_dest); } delete_file(transport, serial, apk_dest); return err; }