From ff6c1e191ac9e3955d38e9537501eb92f7e5f357 Mon Sep 17 00:00:00 2001 From: "Ying-Chun Liu (PaulLiu)" Date: Wed, 26 Aug 2026 00:13:27 +0100 Subject: [PATCH] base: cvd: allocd: Search /usr/sbin for dnsmasq and fix PID file path In Debian, dnsmasq is located in /usr/sbin/dnsmasq which is not typically in non-root users' $PATH. Add SearchForDnsmasq and DnsmasqPath fallback helpers similar to IptablesPath. Also fix StopDnsmasq to use CvdDir() instead of hardcoded /var/run/ and fix inverted file.is_open() logic. Signed-off-by: Ying-Chun Liu (PaulLiu) --- base/cvd/allocd/alloc_utils.cpp | 33 +++++++++++++++++++++++++++++---- base/cvd/allocd/alloc_utils.h | 1 + 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/base/cvd/allocd/alloc_utils.cpp b/base/cvd/allocd/alloc_utils.cpp index c7483747d3b..44c56df590c 100644 --- a/base/cvd/allocd/alloc_utils.cpp +++ b/base/cvd/allocd/alloc_utils.cpp @@ -57,6 +57,15 @@ Result SearchForIptables() { return CF_EXPECT(Search({"/usr/sbin", "/sbin"}, "iptables")); } +Result SearchForDnsmasq() { + Result p = Search(cuttlefish::Path(), "dnsmasq"); + if (p.has_value()) { + return p; + } + + return CF_EXPECT(Search({"/usr/sbin", "/sbin"}, "dnsmasq")); +} + } // namespace bool CreateEthernetIface(std::string_view name, std::string_view bridge_name) { @@ -284,11 +293,19 @@ void CleanupBridgeGateway(std::string_view name, std::string_view ipaddr, bool StartDnsmasq(std::string_view bridge_name, std::string_view gateway, std::string_view dhcp_range) { + Result dnsmasq_path = DnsmasqPath(); + if (!dnsmasq_path.has_value()) { + LOG(ERROR) << "Could not find dnsmasq binary: " + << dnsmasq_path.error().Message(); + return false; + } + auto dns_servers = "8.8.8.8,8.8.4.4"; auto dns6_servers = "2001:4860:4860::8888,2001:4860:4860::8844"; return Execute( - {"dnsmasq", "--port=0", "--strict-order", "--except-interface=lo", + {*dnsmasq_path, "--port=0", "--strict-order", + "--except-interface=lo", absl::StrCat("--interface=", bridge_name), absl::StrCat("--listen-address=", gateway), "--bind-interfaces", absl::StrCat("--dhcp-range=", dhcp_range), @@ -305,12 +322,12 @@ bool StartDnsmasq(std::string_view bridge_name, std::string_view gateway, bool StopDnsmasq(std::string_view name) { std::ifstream file; std::string filename = - absl::StrFormat("/var/run/cuttlefish-dnsmasq-%s.pid", name); + absl::StrCat(CvdDir(), "/cuttlefish-dnsmasq-", name, ".pid"); LOG(INFO) << "stopping dnsmasq for interface: " << name; file.open(filename); - if (file.is_open()) { + if (!file.is_open()) { LOG(INFO) << "dnsmasq file:" << filename - << " could not be opened, assume dnsmaq has already stopped"; + << " could not be opened, assume dnsmasq has already stopped"; return true; } @@ -366,4 +383,12 @@ Result IptablesPath() { return *iptables_path; } +Result DnsmasqPath() { + static const absl::NoDestructor dnsmasq_path( + SearchForDnsmasq().value_or("")); + + CF_EXPECT(!dnsmasq_path->empty(), "could not find dnsmasq"); + return *dnsmasq_path; +} + } // namespace cuttlefish diff --git a/base/cvd/allocd/alloc_utils.h b/base/cvd/allocd/alloc_utils.h index 471833c9c38..2b7d39de742 100644 --- a/base/cvd/allocd/alloc_utils.h +++ b/base/cvd/allocd/alloc_utils.h @@ -53,6 +53,7 @@ struct GatewayConfig { int RunExternalCommand(const std::string& command); Result IptablesPath(); +Result DnsmasqPath(); std::optional GetUserName(uid_t uid); bool CreateTap(std::string_view name);