Skip to content

Commit 16cf565

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 23c8a66 commit 16cf565

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

vminitd/Sources/vminitd/Server+GRPC.swift

Lines changed: 19 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,25 @@ 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 = lines
1292+
.filter { $0.hasPrefix("nameserver") }
1293+
.compactMap { $0.split(separator: " ").dropFirst().first.map(String.init) }
1294+
}
1295+
if domain == nil {
1296+
domain = lines
1297+
.first { $0.hasPrefix("domain") }
1298+
.flatMap { $0.split(separator: " ").dropFirst().first.map(String.init) }
1299+
}
1300+
}
12851301
let config = DNS(
1286-
nameservers: request.nameservers,
1302+
nameservers: nameservers,
12871303
domain: domain,
12881304
searchDomains: request.searchDomains,
12891305
options: request.options

0 commit comments

Comments
 (0)