void* adb_server_register_guest(void* opaque, AdbGuestRoutines* callbacks) { if (_adb_server_initialized) { AdbHost* adb_host; /* Create and initialize ADB guest descriptor. */ AdbGuest* const adb_guest = _adb_guest_new(&_adb_server); adb_guest->opaque = opaque; adb_guest->callbacks = callbacks; /* Lets see if there is a pending ADB host for the new guest. */ adb_host = (AdbHost*)alist_remove_head(&_adb_server.pending_hosts); if (adb_host != NULL) { /* Tie up ADB host with the ADB guest. */ alist_insert_tail(&_adb_server.adb_guests, &adb_guest->list_entry); alist_insert_tail(&_adb_server.adb_hosts, &adb_host->list_entry); _adb_connect(adb_host, adb_guest); } else { /* Host is not available. Pend this guest. */ D("Pend ADB guest %p(o=%p)", adb_guest, adb_guest->opaque); alist_insert_tail(&_adb_server.pending_guests, &adb_guest->list_entry); } return adb_guest; } else { D("%s is called on an uninitialized ADB server.", __FUNCTION__); return NULL; } }
static int _do_sync_push(const char *lpath, const char *rpath, int show_progress) { struct stat st; unsigned mode; int fd; if((fd = _adb_connect("sync:")) < 0) { return 1; } if(stat(lpath, &st)) { sync_quit(fd); return 2; } if(S_ISDIR(st.st_mode)) { if(copy_local_dir_remote(fd, lpath, rpath, 0, 0)) { return 3; } else { sync_quit(fd); } } else { if(sync_readmode(fd, rpath, &mode)) { return 4; } if((mode != 0) && S_ISDIR(mode)) { /* if we're copying a local file to a remote directory, ** we *really* want to copy to remotedir + "/" + localfilename */ const char *name = strrchr(lpath, '/'); if(name == 0) { name = lpath; } else { name++; } int tmplen = strlen(name) + strlen(rpath) + 2; char *tmp = malloc(strlen(name) + strlen(rpath) + 2); if(tmp == 0) return 1; snprintf(tmp, tmplen, "%s/%s", rpath, name); rpath = tmp; } if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, show_progress)) { return 5; } else { sync_quit(fd); return 0; } } return 0; }
/* I/O callback on ADB server socket. */ static void _on_server_socket_io(void* opaque, int fd, unsigned events) { AdbHost* adb_host; AdbGuest* adb_guest; AdbServer* adb_srv = (AdbServer*)opaque; assert(adb_srv->so == fd); /* Since this is a server socket, we only expect a connection I/O here. */ if ((events & LOOP_IO_READ) == 0) { D("Unexpected write I/O on ADB server socket"); return; } /* Create AdbHost instance for the new host connection. */ adb_host = _adb_host_new(adb_srv); /* Accept the connection. */ adb_host->host_so = socket_accept(fd, &adb_srv->socket_address); if (adb_host->host_so < 0) { D("Unable to accept ADB connection: %s", strerror(errno)); _adb_host_free(adb_host); return; } /* Prepare for I/O on the host connection socket. */ loopIo_init(adb_host->io, adb_srv->looper, adb_host->host_so, _on_adb_host_io, adb_host); /* Lets see if there is an ADB guest waiting for a host connection. */ adb_guest = (AdbGuest*)alist_remove_head(&adb_srv->pending_guests); if (adb_guest != NULL) { /* Tie up ADB host with the ADB guest. */ alist_insert_tail(&adb_srv->adb_guests, &adb_guest->list_entry); alist_insert_tail(&adb_srv->adb_hosts, &adb_host->list_entry); _adb_connect(adb_host, adb_guest); } else { /* Pend this connection. */ D("Pend ADB host %p(so=%d)", adb_host, adb_host->host_so); alist_insert_tail(&adb_srv->pending_hosts, &adb_host->list_entry); } /* Enable I/O on the host socket. */ loopIo_wantRead(adb_host->io); }
static char *_send_shellcommand(transport_type transport, char* serial, char* cmd) { int fd; if((fd=_adb_connect(cmd))<0) { return NULL; } char buf[4096<<2]; char *ptr=buf; int left=sizeof(buf); int len = 0; while(left>0) { int n = adb_read(fd, ptr, left); if(n>0) { ptr += n; left -= n; len += n; } else if(n<=0) { break; } } adb_close(fd); return strndup(buf, len); }
int adb_connect(const char *service) { // first query the adb server's version int fd = _adb_connect("host:version"); D("adb_connect: service %s\n", service); if(fd == -2 && __adb_server_name) { fprintf(stderr,"** Cannot start server on remote host\n"); return fd; } else if(fd == -2) { fprintf(stdout,"* daemon not running. starting it now on port %d *\n", __adb_server_port); start_server: if(launch_server(__adb_server_port)) { fprintf(stderr,"* failed to start daemon *\n"); return -1; } else { fprintf(stdout,"* daemon started successfully *\n"); } /* give the server some time to start properly and detect devices */ adb_sleep_ms(3000); // fall through to _adb_connect } else { // if server was running, check its version to make sure it is not out of date char buf[100]; size_t n; int version = ADB_SERVER_VERSION - 1; // if we have a file descriptor, then parse version result if(fd >= 0) { if(!ReadFdExactly(fd, buf, 4)) goto error; buf[4] = 0; n = strtoul(buf, 0, 16); if(n > sizeof(buf)) goto error; if(!ReadFdExactly(fd, buf, n)) goto error; adb_close(fd); if (sscanf(buf, "%04x", &version) != 1) goto error; } else { // if fd is -1, then check for "unknown host service", // which would indicate a version of adb that does not support the version command if (strcmp(__adb_error, "unknown host service") != 0) return fd; } if(version != ADB_SERVER_VERSION) { printf("adb server is out of date. killing...\n"); fd = _adb_connect("host:kill"); adb_close(fd); /* XXX can we better detect its death? */ adb_sleep_ms(2000); goto start_server; } } // if the command is start-server, we are done. if (!strcmp(service, "host:start-server")) return 0; fd = _adb_connect(service); if(fd == -1) { D("_adb_connect error: %s", __adb_error); } else if(fd == -2) { fprintf(stderr,"** daemon still not running\n"); } D("adb_connect: return fd %d\n", fd); return fd; error: adb_close(fd); return -1; }
int adb_connect(const std::string& service, std::string* error) { // first query the adb server's version int fd = _adb_connect("host:version", error); D("adb_connect: service %s", service.c_str()); if (fd == -2 && __adb_server_name) { fprintf(stderr,"** Cannot start server on remote host\n"); // error is the original network connection error return fd; } else if (fd == -2) { fprintf(stdout,"* daemon not running. starting it now on port %d *\n", __adb_server_port); start_server: if (launch_server(__adb_server_port)) { fprintf(stderr,"* failed to start daemon *\n"); // launch_server() has already printed detailed error info, so just // return a generic error string about the overall adb_connect() // that the caller requested. *error = "cannot connect to daemon"; return -1; } else { fprintf(stdout,"* daemon started successfully *\n"); } /* give the server some time to start properly and detect devices */ adb_sleep_ms(3000); // fall through to _adb_connect } else { // if server was running, check its version to make sure it is not out of date int version = ADB_SERVER_VERSION - 1; // if we have a file descriptor, then parse version result if (fd >= 0) { std::string version_string; if (!ReadProtocolString(fd, &version_string, error)) { goto error; } adb_close(fd); if (sscanf(&version_string[0], "%04x", &version) != 1) { *error = android::base::StringPrintf( "cannot parse version string: %s", version_string.c_str()); return -1; } } else { // if fd is -1, then check for "unknown host service", // which would indicate a version of adb that does not support the // version command, in which case we should fall-through to kill it. if (*error != "unknown host service") { return fd; } } if (version != ADB_SERVER_VERSION) { printf("adb server is out of date. killing...\n"); fd = _adb_connect("host:kill", error); if (fd >= 0) { adb_close(fd); } else { // If we couldn't connect to the server or had some other error, // report it, but still try to start the server. fprintf(stderr, "error: %s\n", error->c_str()); } /* XXX can we better detect its death? */ adb_sleep_ms(2000); goto start_server; } } // if the command is start-server, we are done. if (service == "host:start-server") { return 0; } fd = _adb_connect(service, error); if (fd == -1) { D("_adb_connect error: %s", error->c_str()); } else if(fd == -2) { fprintf(stderr,"** daemon still not running\n"); } D("adb_connect: return fd %d", fd); return fd; error: adb_close(fd); return -1; }
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 adb_connect(const std::string& service, std::string* error) { // first query the adb server's version int fd = _adb_connect("host:version", error); D("adb_connect: service %s", service.c_str()); if (fd == -2 && !is_local_socket_spec(__adb_server_socket_spec)) { fprintf(stderr,"** Cannot start server on remote host\n"); // error is the original network connection error return fd; } else if (fd == -2) { fprintf(stdout, "* daemon not running. starting it now at %s *\n", __adb_server_socket_spec); start_server: if (launch_server(__adb_server_socket_spec)) { fprintf(stderr,"* failed to start daemon *\n"); // launch_server() has already printed detailed error info, so just // return a generic error string about the overall adb_connect() // that the caller requested. *error = "cannot connect to daemon"; return -1; } else { fprintf(stdout,"* daemon started successfully *\n"); } // Give the server some time to start properly and detect devices. std::this_thread::sleep_for(3s); // fall through to _adb_connect } else { // If a server is already running, check its version matches. int version = ADB_SERVER_VERSION - 1; // If we have a file descriptor, then parse version result. if (fd >= 0) { std::string version_string; if (!ReadProtocolString(fd, &version_string, error)) { adb_close(fd); return -1; } ReadOrderlyShutdown(fd); adb_close(fd); if (sscanf(&version_string[0], "%04x", &version) != 1) { *error = android::base::StringPrintf("cannot parse version string: %s", version_string.c_str()); return -1; } } else { // If fd is -1 check for "unknown host service" which would // indicate a version of adb that does not support the // version command, in which case we should fall-through to kill it. if (*error != "unknown host service") { return fd; } } if (version != ADB_SERVER_VERSION) { printf("adb server version (%d) doesn't match this client (%d); killing...\n", version, ADB_SERVER_VERSION); fd = _adb_connect("host:kill", error); if (fd >= 0) { ReadOrderlyShutdown(fd); adb_close(fd); } else { // If we couldn't connect to the server or had some other error, // report it, but still try to start the server. fprintf(stderr, "error: %s\n", error->c_str()); } /* XXX can we better detect its death? */ std::this_thread::sleep_for(2s); goto start_server; } } // if the command is start-server, we are done. if (service == "host:start-server") { return 0; } fd = _adb_connect(service, error); if (fd == -1) { D("_adb_connect error: %s", error->c_str()); } else if(fd == -2) { fprintf(stderr,"** daemon still not running\n"); } D("adb_connect: return fd %d", fd); return fd; }
// returns a file descriptor to use with read_fd // if < 0, then FAIL DLL_EXPORT int connect_service(const char * service) { return _adb_connect(service); }
int adb_connect(const std::string& service, std::string* error) { // first query the adb server's version int fd = _adb_connect("host:version", error); D("adb_connect: service %s\n", service.c_str()); if (fd == -2 && __adb_server_name) { fprintf(stderr,"** Cannot start server on remote host\n"); return fd; } else if (fd == -2) { fprintf(stdout,"* daemon not running. starting it now on port %d *\n", __adb_server_port); start_server: if (launch_server(__adb_server_port)) { fprintf(stderr,"* failed to start daemon *\n"); return -1; } else { fprintf(stdout,"* daemon started successfully *\n"); } /* give the server some time to start properly and detect devices */ adb_sleep_ms(3000); // fall through to _adb_connect } else { // if server was running, check its version to make sure it is not out of date int version = ADB_SERVER_VERSION - 1; // if we have a file descriptor, then parse version result if (fd >= 0) { std::string version_string; if (!ReadProtocolString(fd, &version_string, error)) { goto error; } adb_close(fd); if (sscanf(&version_string[0], "%04x", &version) != 1) { goto error; } } else { // if fd is -1, then check for "unknown host service", // which would indicate a version of adb that does not support the version command if (*error == "unknown host service") { return fd; } } if (version != ADB_SERVER_VERSION) { printf("adb server is out of date. killing...\n"); fd = _adb_connect("host:kill", error); adb_close(fd); /* XXX can we better detect its death? */ adb_sleep_ms(2000); goto start_server; } } // if the command is start-server, we are done. if (service == "host:start-server") { return 0; } fd = _adb_connect(service, error); if (fd == -1) { D("_adb_connect error: %s\n", error->c_str()); } else if(fd == -2) { fprintf(stderr,"** daemon still not running\n"); } D("adb_connect: return fd %d\n", fd); return fd; error: adb_close(fd); return -1; }