From a76e5f035eeffeb53c6bf0e667fe13625d5ffbac Mon Sep 17 00:00:00 2001 From: David Pursell Date: Mon, 22 Feb 2016 14:27:23 -0800 Subject: [PATCH] adb: use TCP keepalive. Currently adb only realizes a TCP transport has gone away when it tries to send a packet, which caused problems in particular for `adb reboot` since no packets are sent, leading to the client hanging until Ctrl+C. This CL turns on TCP keepalive packets to send 1 packet every second, allowing up to 10 failures before disconnecting. Using built-in TCP functionality turns out to be much cleaner in this case than trying to implement our own keepalive packets at the application layer, and should be more lightweight since it's all done in the TCP stack. Bug: http://b/23093474 Change-Id: Ifb41cbb85b9752a9f394e1eed3c6ac4da47a4e4d (cherry picked from commit bfd9503d1dfc652d0497fa1a5994b3d73e296898) --- adb/Android.mk | 2 ++ adb/services.cpp | 5 ++++ adb/sysdeps.h | 5 ++++ adb/sysdeps_unix.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ adb/sysdeps_win32.cpp | 27 ++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 adb/sysdeps_unix.cpp diff --git a/adb/Android.mk b/adb/Android.mk index 4777883db..2dba41d86 100644 --- a/adb/Android.mk +++ b/adb/Android.mk @@ -79,10 +79,12 @@ LIBADB_windows_CFLAGS := \ LIBADB_darwin_SRC_FILES := \ get_my_path_darwin.cpp \ + sysdeps_unix.cpp \ usb_osx.cpp \ LIBADB_linux_SRC_FILES := \ get_my_path_linux.cpp \ + sysdeps_unix.cpp \ usb_linux.cpp \ LIBADB_windows_SRC_FILES := \ diff --git a/adb/services.cpp b/adb/services.cpp index 2eef1c265..d5e963b72 100644 --- a/adb/services.cpp +++ b/adb/services.cpp @@ -421,6 +421,11 @@ static void connect_device(const std::string& address, std::string* response) { close_on_exec(fd); disable_tcp_nagle(fd); + // Send a TCP keepalive ping to the device every second so we can detect disconnects. + if (!set_tcp_keepalive(fd, 1)) { + D("warning: failed to configure TCP keepalives (%s)", strerror(errno)); + } + int ret = register_socket_transport(fd, serial.c_str(), port, 0); if (ret < 0) { adb_close(fd); diff --git a/adb/sysdeps.h b/adb/sysdeps.h index 7af2979ec..ce0f28932 100644 --- a/adb/sysdeps.h +++ b/adb/sysdeps.h @@ -841,4 +841,9 @@ static inline void disable_tcp_nagle(int fd) { adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off)); } +// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set +// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be +// configured to drop after 10 missed keepalives. Returns true on success. +bool set_tcp_keepalive(int fd, int interval_sec); + #endif /* _ADB_SYSDEPS_H */ diff --git a/adb/sysdeps_unix.cpp b/adb/sysdeps_unix.cpp new file mode 100644 index 000000000..4445a4420 --- /dev/null +++ b/adb/sysdeps_unix.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sysdeps.h" + +bool set_tcp_keepalive(int fd, int interval_sec) { + int enable = (interval_sec > 0); + if (adb_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable))) { + return false; + } + + if (!enable) { + return true; + } + + // Idle time before sending the first keepalive is TCP_KEEPIDLE on Linux, TCP_KEEPALIVE on Mac. +#if defined(TCP_KEEPIDLE) + if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &interval_sec, sizeof(interval_sec))) { + return false; + } +#elif defined(TCP_KEEPALIVE) + if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &interval_sec, sizeof(interval_sec))) { + return false; + } +#endif + + // TCP_KEEPINTVL and TCP_KEEPCNT are available on Linux 2.4+ and OS X 10.8+ (Mountain Lion). +#if defined(TCP_KEEPINTVL) + if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval_sec, sizeof(interval_sec))) { + return false; + } +#endif + +#if defined(TCP_KEEPCNT) + // On Windows this value is hardcoded to 10. This is a reasonable value, so we do the same here + // to match behavior. See SO_KEEPALIVE documentation at + // https://msdn.microsoft.com/en-us/library/windows/desktop/ee470551(v=vs.85).aspx. + const int keepcnt = 10; + if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &keepcnt, sizeof(keepcnt))) { + return false; + } +#endif + + return true; +} diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp index 7eae186c4..a2f34fbb4 100644 --- a/adb/sysdeps_win32.cpp +++ b/adb/sysdeps_win32.cpp @@ -1228,6 +1228,33 @@ bool set_file_block_mode(int fd, bool block) { } } +bool set_tcp_keepalive(int fd, int interval_sec) { + FH fh = _fh_from_int(fd, __func__); + + if (!fh || fh->clazz != &_fh_socket_class) { + D("set_tcp_keepalive(%d) failed: invalid fd", fd); + errno = EBADF; + return false; + } + + tcp_keepalive keepalive; + keepalive.onoff = (interval_sec > 0); + keepalive.keepalivetime = interval_sec * 1000; + keepalive.keepaliveinterval = interval_sec * 1000; + + DWORD bytes_returned = 0; + if (WSAIoctl(fh->fh_socket, SIO_KEEPALIVE_VALS, &keepalive, sizeof(keepalive), nullptr, 0, + &bytes_returned, nullptr, nullptr) != 0) { + const DWORD err = WSAGetLastError(); + D("set_tcp_keepalive(%d) failed: %s", fd, + android::base::SystemErrorCodeToString(err).c_str()); + _socket_set_errno(err); + return false; + } + + return true; +} + static adb_mutex_t g_console_output_buffer_lock; void