summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes2015-08-13 12:49:21 -0500
committerGerrit Code Review2015-08-13 12:49:21 -0500
commit57532b2a067082fa5968094c2c615f1832fa1971 (patch)
tree01bc08055d904450316e3a0ba0a74ddb029f2901
parent8836031b27f7c62a801501f1da7d09dc3db9686d (diff)
parentbf7c605d87f87c03066c384cecb0f0c91aa31403 (diff)
downloadplatform-system-core-57532b2a067082fa5968094c2c615f1832fa1971.tar.gz
platform-system-core-57532b2a067082fa5968094c2c615f1832fa1971.tar.xz
platform-system-core-57532b2a067082fa5968094c2c615f1832fa1971.zip
Merge "adb: improve network error info"
-rw-r--r--adb/adb.cpp2
-rw-r--r--adb/adb_listeners.cpp4
-rw-r--r--adb/client/main.cpp2
-rw-r--r--adb/sysdeps_win32.cpp64
4 files changed, 50 insertions, 22 deletions
diff --git a/adb/adb.cpp b/adb/adb.cpp
index b83d16486..a8737f5a4 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -858,7 +858,7 @@ int handle_forward_request(const char* service, TransportType type, const char*
858 case INSTALL_STATUS_OK: message = "success (!)"; break; 858 case INSTALL_STATUS_OK: message = "success (!)"; break;
859 case INSTALL_STATUS_INTERNAL_ERROR: message = "internal error"; break; 859 case INSTALL_STATUS_INTERNAL_ERROR: message = "internal error"; break;
860 case INSTALL_STATUS_CANNOT_BIND: 860 case INSTALL_STATUS_CANNOT_BIND:
861 message = android::base::StringPrintf("cannot bind to socket: %s", 861 message = android::base::StringPrintf("cannot bind listener: %s",
862 error.c_str()); 862 error.c_str());
863 break; 863 break;
864 case INSTALL_STATUS_CANNOT_REBIND: 864 case INSTALL_STATUS_CANNOT_REBIND:
diff --git a/adb/adb_listeners.cpp b/adb/adb_listeners.cpp
index 1e7ce5d28..8fb2d19da 100644
--- a/adb/adb_listeners.cpp
+++ b/adb/adb_listeners.cpp
@@ -190,16 +190,19 @@ InstallStatus install_listener(const std::string& local_name,
190 190
191 /* can't repurpose a smartsocket */ 191 /* can't repurpose a smartsocket */
192 if(l->connect_to[0] == '*') { 192 if(l->connect_to[0] == '*') {
193 *error = "cannot repurpose smartsocket";
193 return INSTALL_STATUS_INTERNAL_ERROR; 194 return INSTALL_STATUS_INTERNAL_ERROR;
194 } 195 }
195 196
196 /* can't repurpose a listener if 'no_rebind' is true */ 197 /* can't repurpose a listener if 'no_rebind' is true */
197 if (no_rebind) { 198 if (no_rebind) {
199 *error = "cannot rebind";
198 return INSTALL_STATUS_CANNOT_REBIND; 200 return INSTALL_STATUS_CANNOT_REBIND;
199 } 201 }
200 202
201 cto = strdup(connect_to); 203 cto = strdup(connect_to);
202 if(cto == 0) { 204 if(cto == 0) {
205 *error = "cannot duplicate string";
203 return INSTALL_STATUS_INTERNAL_ERROR; 206 return INSTALL_STATUS_INTERNAL_ERROR;
204 } 207 }
205 208
@@ -232,7 +235,6 @@ InstallStatus install_listener(const std::string& local_name,
232 235
233 listener->fd = local_name_to_fd(listener->local_name, error); 236 listener->fd = local_name_to_fd(listener->local_name, error);
234 if (listener->fd < 0) { 237 if (listener->fd < 0) {
235 printf("cannot bind '%s': %s\n", listener->local_name, error->c_str());
236 free(listener->local_name); 238 free(listener->local_name);
237 free(listener->connect_to); 239 free(listener->connect_to);
238 free(listener); 240 free(listener);
diff --git a/adb/client/main.cpp b/adb/client/main.cpp
index cd06f4b34..206442a38 100644
--- a/adb/client/main.cpp
+++ b/adb/client/main.cpp
@@ -156,7 +156,7 @@ int adb_main(int is_daemon, int server_port, int ack_reply_fd) {
156 std::string error; 156 std::string error;
157 std::string local_name = android::base::StringPrintf("tcp:%d", server_port); 157 std::string local_name = android::base::StringPrintf("tcp:%d", server_port);
158 if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) { 158 if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) {
159 LOG(FATAL) << "Could not install *smartsocket* listener: " << error; 159 fatal("could not install *smartsocket* listener: %s", error.c_str());
160 } 160 }
161 161
162 // Inform our parent that we are up and running. 162 // Inform our parent that we are up and running.
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index f52571310..f534d6116 100644
--- a/adb/sysdeps_win32.cpp
+++ b/adb/sysdeps_win32.cpp
@@ -554,6 +554,9 @@ static void _socket_set_errno( const DWORD err ) {
554 // POSIX and socket error codes, so this can only meaningfully map so much. 554 // POSIX and socket error codes, so this can only meaningfully map so much.
555 switch ( err ) { 555 switch ( err ) {
556 case 0: errno = 0; break; 556 case 0: errno = 0; break;
557 // Mapping WSAEWOULDBLOCK to EAGAIN is absolutely critical because
558 // non-blocking sockets can cause an error code of WSAEWOULDBLOCK and
559 // callers check specifically for EAGAIN.
557 case WSAEWOULDBLOCK: errno = EAGAIN; break; 560 case WSAEWOULDBLOCK: errno = EAGAIN; break;
558 case WSAEINTR: errno = EINTR; break; 561 case WSAEINTR: errno = EINTR; break;
559 case WSAEFAULT: errno = EFAULT; break; 562 case WSAEFAULT: errno = EFAULT; break;
@@ -619,8 +622,12 @@ static int _fh_socket_read(FH f, void* buf, int len) {
619 int result = recv(f->fh_socket, reinterpret_cast<char*>(buf), len, 0); 622 int result = recv(f->fh_socket, reinterpret_cast<char*>(buf), len, 0);
620 if (result == SOCKET_ERROR) { 623 if (result == SOCKET_ERROR) {
621 const DWORD err = WSAGetLastError(); 624 const DWORD err = WSAGetLastError();
622 D("recv fd %d failed: %s\n", _fh_to_int(f), 625 // WSAEWOULDBLOCK is normal with a non-blocking socket, so don't trace
623 SystemErrorCodeToString(err).c_str()); 626 // that to reduce spam and confusion.
627 if (err != WSAEWOULDBLOCK) {
628 D("recv fd %d failed: %s\n", _fh_to_int(f),
629 SystemErrorCodeToString(err).c_str());
630 }
624 _socket_set_errno(err); 631 _socket_set_errno(err);
625 result = -1; 632 result = -1;
626 } 633 }
@@ -701,14 +708,19 @@ int network_loopback_client(int port, int type, std::string* error) {
701 708
702 s = socket(AF_INET, type, 0); 709 s = socket(AF_INET, type, 0);
703 if(s == INVALID_SOCKET) { 710 if(s == INVALID_SOCKET) {
704 *error = SystemErrorCodeToString(WSAGetLastError()); 711 *error = android::base::StringPrintf("cannot create socket: %s",
705 D("could not create socket: %s\n", error->c_str()); 712 SystemErrorCodeToString(WSAGetLastError()).c_str());
713 D("%s\n", error->c_str());
706 return -1; 714 return -1;
707 } 715 }
708 f->fh_socket = s; 716 f->fh_socket = s;
709 717
710 if(connect(s, (struct sockaddr *) &addr, sizeof(addr)) == SOCKET_ERROR) { 718 if(connect(s, (struct sockaddr *) &addr, sizeof(addr)) == SOCKET_ERROR) {
711 *error = SystemErrorCodeToString(WSAGetLastError()); 719 // Save err just in case inet_ntoa() or ntohs() changes the last error.
720 const DWORD err = WSAGetLastError();
721 *error = android::base::StringPrintf("cannot connect to %s:%u: %s",
722 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
723 SystemErrorCodeToString(err).c_str());
712 D("could not connect to %s:%d: %s\n", 724 D("could not connect to %s:%d: %s\n",
713 type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str()); 725 type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
714 return -1; 726 return -1;
@@ -750,31 +762,40 @@ static int _network_server(int port, int type, u_long interface_address,
750 // IPv4 and IPv6. 762 // IPv4 and IPv6.
751 s = socket(AF_INET, type, 0); 763 s = socket(AF_INET, type, 0);
752 if (s == INVALID_SOCKET) { 764 if (s == INVALID_SOCKET) {
753 *error = SystemErrorCodeToString(WSAGetLastError()); 765 *error = android::base::StringPrintf("cannot create socket: %s",
754 D("could not create socket: %s\n", error->c_str()); 766 SystemErrorCodeToString(WSAGetLastError()).c_str());
767 D("%s\n", error->c_str());
755 return -1; 768 return -1;
756 } 769 }
757 770
758 f->fh_socket = s; 771 f->fh_socket = s;
759 772
773 // Note: SO_REUSEADDR on Windows allows multiple processes to bind to the
774 // same port, so instead use SO_EXCLUSIVEADDRUSE.
760 n = 1; 775 n = 1;
761 if (setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (const char*)&n, 776 if (setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (const char*)&n,
762 sizeof(n)) == SOCKET_ERROR) { 777 sizeof(n)) == SOCKET_ERROR) {
763 *error = SystemErrorCodeToString(WSAGetLastError()); 778 *error = android::base::StringPrintf(
764 D("setsockopt level %d optname %d failed: %s\n", 779 "cannot set socket option SO_EXCLUSIVEADDRUSE: %s",
765 SOL_SOCKET, SO_EXCLUSIVEADDRUSE, error->c_str()); 780 SystemErrorCodeToString(WSAGetLastError()).c_str());
781 D("%s\n", error->c_str());
766 return -1; 782 return -1;
767 } 783 }
768 784
769 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) == SOCKET_ERROR) { 785 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) == SOCKET_ERROR) {
770 *error = SystemErrorCodeToString(WSAGetLastError()); 786 // Save err just in case inet_ntoa() or ntohs() changes the last error.
787 const DWORD err = WSAGetLastError();
788 *error = android::base::StringPrintf("cannot bind to %s:%u: %s",
789 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
790 SystemErrorCodeToString(err).c_str());
771 D("could not bind to %s:%d: %s\n", 791 D("could not bind to %s:%d: %s\n",
772 type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str()); 792 type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
773 return -1; 793 return -1;
774 } 794 }
775 if (type == SOCK_STREAM) { 795 if (type == SOCK_STREAM) {
776 if (listen(s, LISTEN_BACKLOG) == SOCKET_ERROR) { 796 if (listen(s, LISTEN_BACKLOG) == SOCKET_ERROR) {
777 *error = SystemErrorCodeToString(WSAGetLastError()); 797 *error = android::base::StringPrintf("cannot listen on socket: %s",
798 SystemErrorCodeToString(WSAGetLastError()).c_str());
778 D("could not listen on %s:%d: %s\n", 799 D("could not listen on %s:%d: %s\n",
779 type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str()); 800 type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
780 return -1; 801 return -1;
@@ -825,9 +846,10 @@ int network_connect(const std::string& host, int port, int type, int timeout, st
825 // with GetProcAddress("GetAddrInfoW"). 846 // with GetProcAddress("GetAddrInfoW").
826#endif 847#endif
827 if (getaddrinfo(host.c_str(), port_str, &hints, &addrinfo_ptr) != 0) { 848 if (getaddrinfo(host.c_str(), port_str, &hints, &addrinfo_ptr) != 0) {
828 *error = SystemErrorCodeToString(WSAGetLastError()); 849 *error = android::base::StringPrintf(
829 D("could not resolve host '%s' and port %s: %s\n", host.c_str(), 850 "cannot resolve host '%s' and port %s: %s", host.c_str(),
830 port_str, error->c_str()); 851 port_str, SystemErrorCodeToString(WSAGetLastError()).c_str());
852 D("%s\n", error->c_str());
831 return -1; 853 return -1;
832 } 854 }
833 std::unique_ptr<struct addrinfo, decltype(freeaddrinfo)*> 855 std::unique_ptr<struct addrinfo, decltype(freeaddrinfo)*>
@@ -840,8 +862,9 @@ int network_connect(const std::string& host, int port, int type, int timeout, st
840 SOCKET s = socket(addrinfo->ai_family, addrinfo->ai_socktype, 862 SOCKET s = socket(addrinfo->ai_family, addrinfo->ai_socktype,
841 addrinfo->ai_protocol); 863 addrinfo->ai_protocol);
842 if(s == INVALID_SOCKET) { 864 if(s == INVALID_SOCKET) {
843 *error = SystemErrorCodeToString(WSAGetLastError()); 865 *error = android::base::StringPrintf("cannot create socket: %s",
844 D("could not create socket: %s\n", error->c_str()); 866 SystemErrorCodeToString(WSAGetLastError()).c_str());
867 D("%s\n", error->c_str());
845 return -1; 868 return -1;
846 } 869 }
847 f->fh_socket = s; 870 f->fh_socket = s;
@@ -849,7 +872,10 @@ int network_connect(const std::string& host, int port, int type, int timeout, st
849 // TODO: Implement timeouts for Windows. Seems like the default in theory 872 // TODO: Implement timeouts for Windows. Seems like the default in theory
850 // (according to http://serverfault.com/a/671453) and in practice is 21 sec. 873 // (according to http://serverfault.com/a/671453) and in practice is 21 sec.
851 if(connect(s, addrinfo->ai_addr, addrinfo->ai_addrlen) == SOCKET_ERROR) { 874 if(connect(s, addrinfo->ai_addr, addrinfo->ai_addrlen) == SOCKET_ERROR) {
852 *error = SystemErrorCodeToString(WSAGetLastError()); 875 // TODO: Use WSAAddressToString or inet_ntop on address.
876 *error = android::base::StringPrintf("cannot connect to %s:%s: %s",
877 host.c_str(), port_str,
878 SystemErrorCodeToString(WSAGetLastError()).c_str());
853 D("could not connect to %s:%s:%s: %s\n", 879 D("could not connect to %s:%s:%s: %s\n",
854 type != SOCK_STREAM ? "udp" : "tcp", host.c_str(), port_str, 880 type != SOCK_STREAM ? "udp" : "tcp", host.c_str(), port_str,
855 error->c_str()); 881 error->c_str());