Skip to content

Commit 4e02fed

Browse files
committed
vminitd: Fall back to /proc/net/pnp for unset nameservers or domain
When configureDns is called with an empty nameservers list or no domain, read /proc/net/pnp (written by the kernel IP_PNP DHCP client) and use any nameserver and domain lines found there. The two are filled in independently, so an explicit nameserver does not prevent the domain from being read from pnp. This provides automatic DNS configuration for bridge-mode containers without a new RPC or proto change.
1 parent 583cdc5 commit 4e02fed

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

vminitd/Sources/VminitdCore/Server+GRPC.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,13 +1267,12 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
12671267
request: Com_Apple_Containerization_Sandbox_V3_ConfigureDnsRequest,
12681268
context: GRPCCore.ServerContext
12691269
) async throws -> Com_Apple_Containerization_Sandbox_V3_ConfigureDnsResponse {
1270-
let domain = request.hasDomain ? request.domain : nil
12711270
log.debug(
12721271
"configureDns",
12731272
metadata: [
12741273
"location": "\(request.location)",
12751274
"nameservers": "\(request.nameservers)",
1276-
"domain": "\(domain ?? "")",
1275+
"domain": "\(request.hasDomain ? request.domain : "")",
12771276
"searchDomains": "\(request.searchDomains)",
12781277
"options": "\(request.options)",
12791278
])
@@ -1282,8 +1281,27 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
12821281
let etc = URL(fileURLWithPath: request.location).appendingPathComponent("etc")
12831282
try FileManager.default.createDirectory(atPath: etc.path, withIntermediateDirectories: true)
12841283
let resolvConf = etc.appendingPathComponent("resolv.conf")
1284+
var nameservers = request.nameservers
1285+
var domain = request.hasDomain ? request.domain : nil
1286+
if nameservers.isEmpty || domain == nil,
1287+
let pnp = try? String(contentsOfFile: "/proc/net/pnp", encoding: .utf8)
1288+
{
1289+
let lines = pnp.split(separator: "\n")
1290+
if nameservers.isEmpty {
1291+
nameservers =
1292+
lines
1293+
.filter { $0.hasPrefix("nameserver") }
1294+
.compactMap { $0.split(separator: " ").dropFirst().first.map(String.init) }
1295+
}
1296+
if domain == nil {
1297+
domain =
1298+
lines
1299+
.first { $0.hasPrefix("domain") }
1300+
.flatMap { $0.split(separator: " ").dropFirst().first.map(String.init) }
1301+
}
1302+
}
12851303
let config = DNS(
1286-
nameservers: request.nameservers,
1304+
nameservers: nameservers,
12871305
domain: domain,
12881306
searchDomains: request.searchDomains,
12891307
options: request.options

0 commit comments

Comments
 (0)