From 7b506090e1ac51f4990f17621c6e33847b0632a2 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 20 Apr 2015 08:09:20 -0700 Subject: [PATCH] Always explain why bind(2) failed. This has confused several people lately. Bug: http://b/20219978 Change-Id: I2537ceb83bff0b3166c230c728d4389a983db858 --- adb/Android.mk | 4 ++++ adb/adb.cpp | 33 ++++++++++++++++----------------- adb/adb_listeners.cpp | 8 ++++---- adb/adb_listeners.h | 3 ++- adb/transport_local.cpp | 2 +- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/adb/Android.mk b/adb/Android.mk index 2ea89e911..85d173088 100644 --- a/adb/Android.mk +++ b/adb/Android.mk @@ -60,6 +60,8 @@ LOCAL_SRC_FILES := \ qemu_tracing.cpp \ usb_linux_client.cpp \ +LOCAL_SHARED_LIBRARIES := libbase + include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) @@ -71,6 +73,8 @@ LOCAL_SRC_FILES := \ $(LIBADB_$(HOST_OS)_SRC_FILES) \ adb_auth_host.cpp \ +LOCAL_SHARED_LIBRARIES := libbase + # Even though we're building a static library (and thus there's no link step for # this to take effect), this adds the SSL includes to our path. LOCAL_STATIC_LIBRARIES := libcrypto_static diff --git a/adb/adb.cpp b/adb/adb.cpp index b09e85314..de82cd48e 100644 --- a/adb/adb.cpp +++ b/adb/adb.cpp @@ -32,6 +32,8 @@ #include +#include + #include "adb_auth.h" #include "adb_io.h" #include "adb_listeners.h" @@ -802,7 +804,6 @@ int handle_forward_request(const char* service, transport_type ttype, char* seri if (!strncmp(service, "forward:",8) || !strncmp(service, "killforward:",12)) { char *local, *remote; - int r; atransport *transport; int createForward = strncmp(service, "kill", 4); @@ -845,12 +846,13 @@ int handle_forward_request(const char* service, transport_type ttype, char* seri return 1; } + install_status_t r; if (createForward) { r = install_listener(local, remote, transport, no_rebind); } else { r = remove_listener(local, transport); } - if(r == 0) { + if (r == INSTALL_STATUS_OK) { #if ADB_HOST /* On the host: 1st OKAY is connect, 2nd OKAY is status */ WriteFdExactly(reply_fd, "OKAY", 4); @@ -859,22 +861,19 @@ int handle_forward_request(const char* service, transport_type ttype, char* seri return 1; } - if (createForward) { - const char* message; - switch (r) { - case INSTALL_STATUS_CANNOT_BIND: - message = "cannot bind to socket"; - break; - case INSTALL_STATUS_CANNOT_REBIND: - message = "cannot rebind existing socket"; - break; - default: - message = "internal error"; - } - sendfailmsg(reply_fd, message); - } else { - sendfailmsg(reply_fd, "cannot remove listener"); + std::string message; + switch (r) { + case INSTALL_STATUS_OK: message = " "; break; + case INSTALL_STATUS_INTERNAL_ERROR: message = "internal error"; break; + case INSTALL_STATUS_CANNOT_BIND: + message = android::base::StringPrintf("cannot bind to socket: %s", strerror(errno)); + break; + case INSTALL_STATUS_CANNOT_REBIND: + message = android::base::StringPrintf("cannot rebind existing socket: %s", strerror(errno)); + break; + case INSTALL_STATUS_LISTENER_NOT_FOUND: message = "listener not found"; break; } + sendfailmsg(reply_fd, message.c_str()); return 1; } return 0; diff --git a/adb/adb_listeners.cpp b/adb/adb_listeners.cpp index 84b9c6490..a1a5ddb52 100644 --- a/adb/adb_listeners.cpp +++ b/adb/adb_listeners.cpp @@ -190,17 +190,17 @@ int format_listeners(char* buf, size_t buflen) return result; } -int remove_listener(const char *local_name, atransport* transport) +install_status_t remove_listener(const char *local_name, atransport* transport) { alistener *l; for (l = listener_list.next; l != &listener_list; l = l->next) { if (!strcmp(local_name, l->local_name)) { listener_disconnect(l, l->transport); - return 0; + return INSTALL_STATUS_OK; } } - return -1; + return INSTALL_STATUS_LISTENER_NOT_FOUND; } void remove_all_listeners(void) @@ -268,10 +268,10 @@ install_status_t install_listener(const char *local_name, listener->fd = local_name_to_fd(local_name); if (listener->fd < 0) { + printf("cannot bind '%s': %s\n", local_name, strerror(errno)); free(listener->local_name); free(listener->connect_to); free(listener); - printf("cannot bind '%s'\n", local_name); return INSTALL_STATUS_CANNOT_BIND; } diff --git a/adb/adb_listeners.h b/adb/adb_listeners.h index 6421df105..f55fdee62 100644 --- a/adb/adb_listeners.h +++ b/adb/adb_listeners.h @@ -25,6 +25,7 @@ enum install_status_t { INSTALL_STATUS_INTERNAL_ERROR = -1, INSTALL_STATUS_CANNOT_BIND = -2, INSTALL_STATUS_CANNOT_REBIND = -3, + INSTALL_STATUS_LISTENER_NOT_FOUND = -4, }; extern alistener listener_list; @@ -40,7 +41,7 @@ install_status_t install_listener(const char *local_name, int format_listeners(char* buf, size_t buflen); -int remove_listener(const char *local_name, atransport* transport); +install_status_t remove_listener(const char* local_name, atransport* transport); void remove_all_listeners(void); #endif /* __ADB_LISTENERS_H */ diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp index fe3c87f3f..30e6bf5c6 100644 --- a/adb/transport_local.cpp +++ b/adb/transport_local.cpp @@ -144,7 +144,7 @@ static void *server_socket_thread(void * arg) if(serverfd == -1) { serverfd = socket_inaddr_any_server(port, SOCK_STREAM); if(serverfd < 0) { - D("server: cannot bind socket yet\n"); + D("server: cannot bind socket yet: %s\n", strerror(errno)); adb_sleep_ms(1000); continue; }