Skip to content

Commit 863e3c2

Browse files
authored
Bind and connect to Unix domain sockets using relative paths (#1192)
1 parent 372affb commit 863e3c2

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

Sources/tart/Commands/Exec.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,16 @@ struct Exec: AsyncParsableCommand {
4747
try! group.syncShutdownGracefully()
4848
}
4949

50+
// Change the current working directory to a VM's base directory
51+
// to work around Unix domain socket 104 byte limitation [1]
52+
//
53+
// [1]: https://blog.8-p.info/en/2020/06/11/unix-domain-socket-length/
54+
if let baseURL = vmDir.controlSocketURL.baseURL {
55+
FileManager.default.changeCurrentDirectoryPath(baseURL.path())
56+
}
57+
5058
let channel = try GRPCChannelPool.with(
51-
target: .unixDomainSocket(vmDir.controlSocketURL.path()),
59+
target: .unixDomainSocket(vmDir.controlSocketURL.relativePath),
5260
transportSecurity: .plaintext,
5361
eventLoopGroup: group,
5462
)

Sources/tart/Commands/IP.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ struct IP: AsyncParsableCommand {
6868
throw RuntimeError.Generic("Cannot perform IP resolution via Tart Guest Agent when control socket URL is not set")
6969
}
7070

71-
if let ip = try await AgentResolver.ResolveIP(controlSocketURL) {
71+
// Change the current working directory to a VM's base directory
72+
// to work around Unix domain socket 104 byte limitation [1]
73+
//
74+
// [1]: https://blog.8-p.info/en/2020/06/11/unix-domain-socket-length/
75+
if let baseURL = controlSocketURL.baseURL {
76+
FileManager.default.changeCurrentDirectoryPath(baseURL.path())
77+
}
78+
79+
if let ip = try await AgentResolver.ResolveIP(controlSocketURL.relativePath) {
7280
return ip
7381
}
7482
}

Sources/tart/ControlSocket.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ class ControlSocket {
2121
// if any, otherwise we may get the "address already in use" error
2222
try? FileManager.default.removeItem(atPath: controlSocketURL.path())
2323

24+
// Change the current working directory to a VM's base directory
25+
// to work around Unix domain socket 104 byte limitation [1]
26+
//
27+
// [1]: https://blog.8-p.info/en/2020/06/11/unix-domain-socket-length/
28+
if let baseURL = controlSocketURL.baseURL {
29+
FileManager.default.changeCurrentDirectoryPath(baseURL.path())
30+
}
31+
2432
let serverChannel = try await ServerBootstrap(group: eventLoopGroup)
25-
.bind(unixDomainSocketPath: controlSocketURL.path()) { childChannel in
33+
.bind(unixDomainSocketPath: controlSocketURL.relativePath) { childChannel in
2634
childChannel.eventLoop.makeCompletedFuture {
2735
return try NIOAsyncChannel<ByteBuffer, ByteBuffer>(
2836
wrappingChannelSynchronously: childChannel

Sources/tart/MACAddressResolver/AgentResolver.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ import Cirruslabs_TartGuestAgent_Apple_Swift
66
import Cirruslabs_TartGuestAgent_Grpc_Swift
77

88
class AgentResolver {
9-
static func ResolveIP(_ controlSocketURL: URL) async throws -> IPv4Address? {
9+
static func ResolveIP(_ controlSocketPath: String) async throws -> IPv4Address? {
1010
do {
11-
return try await resolveIP(controlSocketURL)
11+
return try await resolveIP(controlSocketPath)
1212
} catch let error as GRPCConnectionPoolError {
1313
return nil
1414
}
1515
}
1616

17-
private static func resolveIP(_ controlSocketURL: URL) async throws -> IPv4Address? {
17+
private static func resolveIP(_ controlSocketPath: String) async throws -> IPv4Address? {
1818
// Create a gRPC channel connected to the VM's control socket
1919
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
2020
defer {
2121
try! group.syncShutdownGracefully()
2222
}
2323

2424
let channel = try GRPCChannelPool.with(
25-
target: .unixDomainSocket(controlSocketURL.path()),
25+
target: .unixDomainSocket(controlSocketPath),
2626
transportSecurity: .plaintext,
2727
eventLoopGroup: group,
2828
)

Sources/tart/VMDirectory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct VMDirectory: Prunable {
2727
baseURL.appendingPathComponent("manifest.json")
2828
}
2929
var controlSocketURL: URL {
30-
baseURL.appendingPathComponent("control.sock")
30+
URL(fileURLWithPath: "control.sock", relativeTo: baseURL)
3131
}
3232

3333
var explicitlyPulledMark: URL {

0 commit comments

Comments
 (0)