From b5e857ae87ba805ffcfb72cb3cc9a15e32a1c6a9 Mon Sep 17 00:00:00 2001 From: "A. Cody Schuffelen" Date: Thu, 24 Sep 2026 16:07:23 -0700 Subject: [PATCH 1/3] Migrate SharedFD::VsockServer callers to Fd::VsockServer Bug: b/545278508 --- .../common/libs/utils/vsock_connection.cpp | 4 ++-- .../host/commands/run_cvd/launch/BUILD.bazel | 4 +++- .../host/commands/run_cvd/launch/modem.cpp | 22 +++++++++---------- .../commands/run_cvd/launch/netsim_server.cpp | 19 ++++++++-------- .../run_cvd/launch/tombstone_receiver.cpp | 18 +++++++-------- .../commands/socket_vsock_proxy/server.cpp | 5 ++++- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/base/cvd/cuttlefish/common/libs/utils/vsock_connection.cpp b/base/cvd/cuttlefish/common/libs/utils/vsock_connection.cpp index 08347ee497c..40051e31bbf 100644 --- a/base/cvd/cuttlefish/common/libs/utils/vsock_connection.cpp +++ b/base/cvd/cuttlefish/common/libs/utils/vsock_connection.cpp @@ -240,8 +240,8 @@ void VsockServerConnection::ServerShutdown() { bool VsockServerConnection::Connect(unsigned int port, unsigned int cid, std::optional vhost_user_vsock_cid) { if (!server_fd_->IsOpen()) { - server_fd_ = cuttlefish::SharedFD::VsockServer(port, SOCK_STREAM, - vhost_user_vsock_cid, cid); + server_fd_ = Fd::VsockServer(port, SOCK_STREAM, vhost_user_vsock_cid, cid) + .value_or(Fd()); } if (server_fd_->IsOpen()) { fd_ = Fd::Accept(*server_fd_).value_or(Fd()); diff --git a/base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel b/base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel index 45bf4bb6760..43c463ae591 100644 --- a/base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel @@ -269,6 +269,7 @@ cf_cc_library( hdrs = ["modem.h"], deps = [ "//cuttlefish/common/libs/fs", + "//cuttlefish/common/libs/fs:fd", "//cuttlefish/host/libs/config:cuttlefish_config", "//cuttlefish/host/libs/config:known_paths", "//cuttlefish/host/libs/feature", @@ -285,6 +286,7 @@ cf_cc_library( hdrs = ["netsim_server.h"], deps = [ "//cuttlefish/common/libs/fs", + "//cuttlefish/common/libs/fs:fd", "//cuttlefish/common/libs/utils:files", "//cuttlefish/files:file_exists", "//cuttlefish/host/commands/run_cvd/launch:grpc_socket_creator", @@ -508,7 +510,7 @@ cf_cc_library( srcs = ["tombstone_receiver.cpp"], hdrs = ["tombstone_receiver.h"], deps = [ - "//cuttlefish/common/libs/fs", + "//cuttlefish/common/libs/fs:fd", "//cuttlefish/files:directory_exists", "//cuttlefish/host/libs/config:cuttlefish_config", "//cuttlefish/host/libs/config:known_paths", diff --git a/base/cvd/cuttlefish/host/commands/run_cvd/launch/modem.cpp b/base/cvd/cuttlefish/host/commands/run_cvd/launch/modem.cpp index 2b7e17d5e17..9d1d8bdc6d9 100644 --- a/base/cvd/cuttlefish/host/commands/run_cvd/launch/modem.cpp +++ b/base/cvd/cuttlefish/host/commands/run_cvd/launch/modem.cpp @@ -24,6 +24,7 @@ #include "absl/log/log.h" +#include "cuttlefish/common/libs/fs/fd.h" #include "cuttlefish/common/libs/fs/shared_fd.h" #include "cuttlefish/host/libs/config/cuttlefish_config.h" #include "cuttlefish/host/libs/config/known_paths.h" @@ -73,22 +74,19 @@ Result> ModemSimulator( CF_EXPECT(instance_number >= 0 && instance_number < 4, "Modem simulator instance number should range between 0 and 3"); auto ports = instance.modem_simulator_ports(); - std::vector sockets; + std::vector sockets; for (int i = 0; i < instance_number; ++i) { auto pos = ports.find(','); auto temp = (pos != std::string::npos) ? ports.substr(0, pos) : ports; auto port = std::stoi(temp); ports = ports.substr(pos + 1); - auto modem_sim_socket = SharedFD::VsockServer( - port, SOCK_STREAM, - instance.vhost_user_vsock() - ? std::make_optional(instance.vsock_guest_cid()) - : std::nullopt); - CF_EXPECT( - modem_sim_socket->IsOpen(), - modem_sim_socket->StrError() - << " (try `cvd reset`, or `pkill run_cvd` and `pkill crosvm`)"); + Fd modem_sim_socket = CF_EXPECT( + Fd::VsockServer(port, SOCK_STREAM, + instance.vhost_user_vsock() + ? std::make_optional(instance.vsock_guest_cid()) + : std::nullopt), + " (try `cvd reset`, or `pkill run_cvd` and `pkill crosvm`)"); sockets.emplace_back(std::move(modem_sim_socket)); } @@ -100,11 +98,11 @@ Result> ModemSimulator( cmd.AddParameter(std::string{"-sim_type="} + std::to_string(sim_type)); cmd.AddParameter("-server_fds="); bool first_socket = true; - for (const auto& socket : sockets) { + for (auto& socket : sockets) { if (!first_socket) { cmd.AppendToLastParameter(","); } - cmd.AppendToLastParameter(socket); + cmd.AppendToLastParameter(SharedFD(std::move(socket))); first_socket = false; } return cmd; diff --git a/base/cvd/cuttlefish/host/commands/run_cvd/launch/netsim_server.cpp b/base/cvd/cuttlefish/host/commands/run_cvd/launch/netsim_server.cpp index 3d47b01051b..8075de18a9e 100644 --- a/base/cvd/cuttlefish/host/commands/run_cvd/launch/netsim_server.cpp +++ b/base/cvd/cuttlefish/host/commands/run_cvd/launch/netsim_server.cpp @@ -28,6 +28,7 @@ #include "fruit/fruit_forward_decls.h" #include "fruit/macro.h" +#include "cuttlefish/common/libs/fs/fd.h" #include "cuttlefish/common/libs/fs/shared_fd.h" #include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/files/file_exists.h" @@ -261,17 +262,17 @@ class NetsimServer : public CommandSource { absl::SimpleAtoi(port_strings[i], &port), "Failed to parse modem simulator port: " << port_strings[i]); - auto vsock = SharedFD::VsockServer( - port, SOCK_STREAM, - instance.vhost_user_vsock() - ? std::make_optional(instance.vsock_guest_cid()) - : std::nullopt); - CF_EXPECT(vsock->IsOpen(), vsock->StrError() - << " (try `cvd reset`, or `pkill " - "run_cvd` and `pkill crosvm`)"); + Fd vsock = + CF_EXPECT(Fd::VsockServer( + port, SOCK_STREAM, + instance.vhost_user_vsock() + ? std::make_optional(instance.vsock_guest_cid()) + : std::nullopt), + " (try `cvd reset`, or `pkill " + "run_cvd` and `pkill crosvm`)"); Chip chip("CELLULAR"); - chip.vsock_fd = vsock; + chip.vsock_fd = std::move(vsock); chip.sim_type = instance.modem_simulator_sim_type(); device.chips.emplace_back(chip); } diff --git a/base/cvd/cuttlefish/host/commands/run_cvd/launch/tombstone_receiver.cpp b/base/cvd/cuttlefish/host/commands/run_cvd/launch/tombstone_receiver.cpp index 204d033e8bd..5ac187ef72c 100644 --- a/base/cvd/cuttlefish/host/commands/run_cvd/launch/tombstone_receiver.cpp +++ b/base/cvd/cuttlefish/host/commands/run_cvd/launch/tombstone_receiver.cpp @@ -18,12 +18,12 @@ #include #include -#include #include +#include #include "absl/log/log.h" -#include "cuttlefish/common/libs/fs/shared_fd.h" +#include "cuttlefish/common/libs/fs/fd.h" #include "cuttlefish/files/directory_exists.h" #include "cuttlefish/host/libs/config/cuttlefish_config.h" #include "cuttlefish/host/libs/config/known_paths.h" @@ -47,16 +47,14 @@ Result TombstoneReceiver( } auto port = instance.tombstone_receiver_port(); - auto socket = - SharedFD::VsockServer(port, SOCK_STREAM, - instance.vhost_user_vsock() - ? std::make_optional(instance.vsock_guest_cid()) - : std::nullopt); - CF_EXPECTF(socket->IsOpen(), "Can't tombstone server socket: '{}'", - socket->StrError()); + Fd socket = CF_EXPECT( + Fd::VsockServer(port, SOCK_STREAM, + instance.vhost_user_vsock() + ? std::make_optional(instance.vsock_guest_cid()) + : std::nullopt)); return Command(TombstoneReceiverBinary()) - .AddParameter("-server_fd=", socket) + .AddParameter("-server_fd=", std::move(socket)) .AddParameter("-tombstone_dir=", tombstone_dir); } diff --git a/base/cvd/cuttlefish/host/commands/socket_vsock_proxy/server.cpp b/base/cvd/cuttlefish/host/commands/socket_vsock_proxy/server.cpp index 3e1abde8258..30e07b23c83 100644 --- a/base/cvd/cuttlefish/host/commands/socket_vsock_proxy/server.cpp +++ b/base/cvd/cuttlefish/host/commands/socket_vsock_proxy/server.cpp @@ -89,7 +89,10 @@ VsockServer::VsockServer(int port, std::optional vhost_user_vsock_cid) Result VsockServer::Start() { SharedFD server; do { - server = SharedFD::VsockServer(port_, SOCK_STREAM, vhost_user_vsock_cid_); + // TODO(schuffelen): Expose errno from Fd results so + // socketErrorIsRecoverable works correctly + server = Fd::VsockServer(port_, SOCK_STREAM, vhost_user_vsock_cid_) + .value_or(Fd()); if (!server->IsOpen() && !socketErrorIsRecoverable(server->GetErrno())) { LOG(ERROR) << "Could not open vsock socket: " << server->StrError(); // socket_vsock_proxy will now wait forever in the guest on encountering From 601c0b681517dff967fb276eae7972863b3fbd4f Mon Sep 17 00:00:00 2001 From: "A. Cody Schuffelen" Date: Thu, 24 Sep 2026 16:32:58 -0700 Subject: [PATCH 2/3] Remove SharedFD::VsockServer Bug: b/545278508 --- base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp | 12 ------------ base/cvd/cuttlefish/common/libs/fs/shared_fd.h | 5 ----- 2 files changed, 17 deletions(-) diff --git a/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp b/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp index d8f7e455faa..0454007aa08 100644 --- a/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp +++ b/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp @@ -264,18 +264,6 @@ SharedFD SharedFD::SocketLocalServer(const std::string& name, bool abstract, } #ifdef __linux__ -SharedFD SharedFD::VsockServer( - unsigned int port, int type, - std::optional vhost_user_vsock_listening_cid, unsigned int cid) { - return Fd::VsockServer(port, type, vhost_user_vsock_listening_cid, cid) - .value_or(Fd()); -} - -SharedFD SharedFD::VsockServer( - int type, std::optional vhost_user_vsock_listening_cid) { - return VsockServer(VMADDR_PORT_ANY, type, vhost_user_vsock_listening_cid); -} - std::string SharedFD::GetVhostUserVsockServerAddr( unsigned int port, int vhost_user_vsock_listening_cid) { return Fd::GetVhostUserVsockServerAddr(port, vhost_user_vsock_listening_cid); diff --git a/base/cvd/cuttlefish/common/libs/fs/shared_fd.h b/base/cvd/cuttlefish/common/libs/fs/shared_fd.h index da58ec17c8e..0d68dcd1d96 100644 --- a/base/cvd/cuttlefish/common/libs/fs/shared_fd.h +++ b/base/cvd/cuttlefish/common/libs/fs/shared_fd.h @@ -186,11 +186,6 @@ class SharedFD { // necessary. // TODO: combining them when vhost-user-vsock impl supports a kind of // VMADDR_CID_HOST - static SharedFD VsockServer(unsigned int port, int type, - std::optional vhost_user_vsock_listening_cid, - unsigned int cid = VMADDR_CID_ANY); - static SharedFD VsockServer( - int type, std::optional vhost_user_vsock_listening_cid); static SharedFD VsockClient(unsigned int cid, unsigned int port, int type, bool vhost_user); static std::string GetVhostUserVsockServerAddr( From 4472eec8da62039b9273228dec9fe4de8e41a90f Mon Sep 17 00:00:00 2001 From: "A. Cody Schuffelen" Date: Thu, 24 Sep 2026 16:37:36 -0700 Subject: [PATCH 3/3] Remove SharedFD::GetVhostUserVsock[Client,Server]Addr This is also in Fd Bug: b/545278508 --- base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp | 13 ++----------- base/cvd/cuttlefish/common/libs/fs/shared_fd.h | 3 --- .../host/commands/run_cvd/launch/BUILD.bazel | 2 +- .../commands/run_cvd/launch/vhal_proxy_server.cpp | 8 ++++---- 4 files changed, 7 insertions(+), 19 deletions(-) diff --git a/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp b/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp index 0454007aa08..0e177c08854 100644 --- a/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp +++ b/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp @@ -264,21 +264,12 @@ SharedFD SharedFD::SocketLocalServer(const std::string& name, bool abstract, } #ifdef __linux__ -std::string SharedFD::GetVhostUserVsockServerAddr( - unsigned int port, int vhost_user_vsock_listening_cid) { - return Fd::GetVhostUserVsockServerAddr(port, vhost_user_vsock_listening_cid); -} - -std::string SharedFD::GetVhostUserVsockClientAddr(int cid) { - return Fd::GetVhostUserVsockClientAddr(cid); -} - SharedFD SharedFD::VsockClient(unsigned int cid, unsigned int port, int type, bool vhost_user) { if (vhost_user) { // TODO(b/277909042): better path than /tmp/vsock_{}/vm.vsock - auto client = SharedFD::SocketLocalClient(GetVhostUserVsockClientAddr(cid), - false /* abstract */, type); + auto client = SharedFD::SocketLocalClient( + Fd::GetVhostUserVsockClientAddr(cid), false /* abstract */, type); const std::string msg = fmt::format("connect {}\n", port); SendAll(client, msg); diff --git a/base/cvd/cuttlefish/common/libs/fs/shared_fd.h b/base/cvd/cuttlefish/common/libs/fs/shared_fd.h index 0d68dcd1d96..f067b4d29b5 100644 --- a/base/cvd/cuttlefish/common/libs/fs/shared_fd.h +++ b/base/cvd/cuttlefish/common/libs/fs/shared_fd.h @@ -188,9 +188,6 @@ class SharedFD { // VMADDR_CID_HOST static SharedFD VsockClient(unsigned int cid, unsigned int port, int type, bool vhost_user); - static std::string GetVhostUserVsockServerAddr( - unsigned int port, int vhost_user_vsock_listening_cid); - static std::string GetVhostUserVsockClientAddr(int cid); #endif auto operator<=>(const SharedFD&) const = default; diff --git a/base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel b/base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel index 43c463ae591..c8575e22b52 100644 --- a/base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel @@ -542,7 +542,7 @@ cf_cc_library( srcs = ["vhal_proxy_server.cpp"], hdrs = ["vhal_proxy_server.h"], deps = [ - "//cuttlefish/common/libs/fs", + "//cuttlefish/common/libs/fs:fd", "//cuttlefish/host/libs/config:cuttlefish_config", "//cuttlefish/host/libs/config:known_paths", "//cuttlefish/host/libs/feature", diff --git a/base/cvd/cuttlefish/host/commands/run_cvd/launch/vhal_proxy_server.cpp b/base/cvd/cuttlefish/host/commands/run_cvd/launch/vhal_proxy_server.cpp index 77d44fbd597..a3ebfc5b3ad 100644 --- a/base/cvd/cuttlefish/host/commands/run_cvd/launch/vhal_proxy_server.cpp +++ b/base/cvd/cuttlefish/host/commands/run_cvd/launch/vhal_proxy_server.cpp @@ -22,7 +22,7 @@ #include "fmt/core.h" #include "fmt/format.h" -#include "cuttlefish/common/libs/fs/shared_fd.h" +#include "cuttlefish/common/libs/fs/fd.h" #include "cuttlefish/host/libs/config/cuttlefish_config.h" #include "cuttlefish/host/libs/config/known_paths.h" #include "cuttlefish/host/libs/feature/command_source.h" @@ -42,9 +42,9 @@ std::optional VhalProxyServer( .AddParameter(fmt::format( "{}:{}", vhal_proxy_server::kEthAddr, port)); if (instance.vhost_user_vsock()) { - command.AddParameter( - fmt::format("unix://{}", SharedFD::GetVhostUserVsockServerAddr( - port, instance.vsock_guest_cid()))); + command.AddParameter(fmt::format( + "unix://{}", + Fd::GetVhostUserVsockServerAddr(port, instance.vsock_guest_cid()))); } else { command.AddParameter(fmt::format("vsock:{}:{}", VMADDR_CID_HOST, port)); }