Skip to content

Commit 16813a7

Browse files
torarnvcurdbecker
authored andcommitted
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 9649eda commit 16813a7

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
@@ -1332,13 +1332,12 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
13321332
request: Com_Apple_Containerization_Sandbox_V3_ConfigureDnsRequest,
13331333
context: GRPCCore.ServerContext
13341334
) async throws -> Com_Apple_Containerization_Sandbox_V3_ConfigureDnsResponse {
1335-
let domain = request.hasDomain ? request.domain : nil
13361335
log.debug(
13371336
"configureDns",
13381337
metadata: [
13391338
"location": "\(request.location)",
13401339
"nameservers": "\(request.nameservers)",
1341-
"domain": "\(domain ?? "")",
1340+
"domain": "\(request.hasDomain ? request.domain : "")",
13421341
"searchDomains": "\(request.searchDomains)",
13431342
"options": "\(request.options)",
13441343
])
@@ -1347,8 +1346,27 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContext.SimpleServ
13471346
let etc = URL(fileURLWithPath: request.location).appendingPathComponent("etc")
13481347
try FileManager.default.createDirectory(atPath: etc.path, withIntermediateDirectories: true)
13491348
let resolvConf = etc.appendingPathComponent("resolv.conf")
1349+
var nameservers = request.nameservers
1350+
var domain = request.hasDomain ? request.domain : nil
1351+
if nameservers.isEmpty || domain == nil,
1352+
let pnp = try? String(contentsOfFile: "/proc/net/pnp", encoding: .utf8)
1353+
{
1354+
let lines = pnp.split(separator: "\n")
1355+
if nameservers.isEmpty {
1356+
nameservers =
1357+
lines
1358+
.filter { $0.hasPrefix("nameserver") }
1359+
.compactMap { $0.split(separator: " ").dropFirst().first.map(String.init) }
1360+
}
1361+
if domain == nil {
1362+
domain =
1363+
lines
1364+
.first { $0.hasPrefix("domain") }
1365+
.flatMap { $0.split(separator: " ").dropFirst().first.map(String.init) }
1366+
}
1367+
}
13501368
let config = DNS(
1351-
nameservers: request.nameservers,
1369+
nameservers: nameservers,
13521370
domain: domain,
13531371
searchDomains: request.searchDomains,
13541372
options: request.options

0 commit comments

Comments
 (0)