Revert "Revert "Support socket activation of adb server""
Test: actually build both device and client adb this time
This reverts commit 2a34e58d3d64ab183228a51596d6e196b4d373b8.
Change-Id: I8c2b8f24884ff36ff2460dab2500bad777100dfd
diff --git a/socket_spec.cpp b/socket_spec.cpp
index 27e8c46..9ce443e 100644
--- a/socket_spec.cpp
+++ b/socket_spec.cpp
@@ -16,6 +16,7 @@
#include "socket_spec.h"
+#include <limits>
#include <string>
#include <string_view>
#include <unordered_map>
@@ -28,10 +29,12 @@
#include <cutils/sockets.h>
#include "adb.h"
+#include "adb_utils.h"
#include "sysdeps.h"
using namespace std::string_literals;
+using android::base::ConsumePrefix;
using android::base::StringPrintf;
#if defined(__linux__)
@@ -131,7 +134,7 @@
return true;
}
}
- return spec.starts_with("tcp:");
+ return spec.starts_with("tcp:") || spec.starts_with("acceptfd:");
}
bool is_local_socket_spec(std::string_view spec) {
@@ -235,6 +238,9 @@
*error = "vsock is only supported on linux";
return false;
#endif // ADB_LINUX
+ } else if (address.starts_with("acceptfd:")) {
+ *error = "cannot connect to acceptfd";
+ return false;
}
for (const auto& it : kLocalSocketTypes) {
@@ -334,6 +340,46 @@
*error = "vsock is only supported on linux";
return -1;
#endif // ADB_LINUX
+ } else if (ConsumePrefix(&spec, "acceptfd:")) {
+#if ADB_WINDOWS
+ *error = "socket activation not supported under Windows";
+ return -1;
+#else
+ // We inherited the socket from some kind of launcher. It's already bound and
+ // listening. Return a copy of the FD instead of the FD itself so we implement the
+ // normal "listen" contract and can succeed more than once.
+ unsigned int fd_u;
+ if (!ParseUint(&fd_u, spec) || fd_u > std::numeric_limits<int>::max()) {
+ *error = "invalid fd";
+ return -1;
+ }
+ int fd = static_cast<int>(fd_u);
+ int flags = get_fd_flags(fd);
+ if (flags < 0) {
+ *error = android::base::StringPrintf("could not get flags of inherited fd %d: '%s'", fd,
+ strerror(errno));
+ return -1;
+ }
+ if (flags & FD_CLOEXEC) {
+ *error = android::base::StringPrintf("fd %d was not inherited from parent", fd);
+ return -1;
+ }
+
+ int dummy_sock_type;
+ socklen_t dummy_sock_type_size = sizeof(dummy_sock_type);
+ if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &dummy_sock_type, &dummy_sock_type_size)) {
+ *error = android::base::StringPrintf("fd %d does not refer to a socket", fd);
+ return -1;
+ }
+
+ int new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
+ if (new_fd < 0) {
+ *error = android::base::StringPrintf("could not dup inherited fd %d: '%s'", fd,
+ strerror(errno));
+ return -1;
+ }
+ return new_fd;
+#endif
}
for (const auto& it : kLocalSocketTypes) {