import Darwin public import Foundation public final class DriverSocket { public let handle: FileHandle public let path: String? private var closed = false private init(descriptor: Int32, path: String? = nil) { handle = FileHandle(fileDescriptor: descriptor, closeOnDealloc: true) self.path = path } public static func listen(path: String) throws -> DriverSocket { var address = try Address(path) let fd = try descriptor() var bound = false do { guard address.withPointer({ Darwin.bind(fd, $0, $1) }) == 0 else { throw DriverError("Driver socket bind failed: \(errno)") } bound = true guard chmod(path, 0o600) == 0, Darwin.listen(fd, 1) == 0, fcntl(fd, F_SETFL, O_NONBLOCK) == 0 else { throw DriverError("Driver socket listen failed: \(errno)") } return DriverSocket(descriptor: fd, path: path) } catch { Darwin.close(fd) if bound, unlink(path) != 0 { throw DriverError("Driver socket setup failed: \(error); cleanup failed: \(errno)") } throw error } } public static func connect(path: String) throws -> DriverSocket { var address = try Address(path) let fd = try descriptor() do { guard address.withPointer({ Darwin.connect(fd, $0, $1) }) == 0 else { throw DriverError("Driver socket connect failed: \(errno)") } try requireCurrentUser(fd) return DriverSocket(descriptor: fd) } catch { Darwin.close(fd) throw error } } public func accept() throws -> DriverSocket? { guard !closed else { throw DriverError("Driver listener is closed") } let fd = Darwin.accept(handle.fileDescriptor, nil, nil) if fd < 0 { if errno == EAGAIN || errno == EINTR { return nil } throw DriverError("Driver socket accept failed: \(errno)") } let socket = DriverSocket(descriptor: fd) do { try Self.configure(fd) try Self.requireCurrentUser(fd) return socket } catch { try socket.close() throw error } } public func peerPID() throws -> Int32 { var pid: Int32 = 0 var length = socklen_t(MemoryLayout.size) guard getsockopt(handle.fileDescriptor, SOL_LOCAL, LOCAL_PEERPID, &pid, &length) == 0, pid > 0 else { throw DriverError("Cannot identify driver socket peer: \(errno)") } return pid } public func close() throws { guard !closed else { return } closed = true try handle.close() if let path, unlink(path) != 0 { throw DriverError("Cannot remove driver socket: \(errno)") } } private static func requireCurrentUser(_ fd: Int32) throws { var uid: uid_t = 0 var gid: gid_t = 0 guard getpeereid(fd, &uid, &gid) == 0, uid == getuid() else { throw DriverError("Driver socket peer is not the current user") } } private static func descriptor() throws -> Int32 { let fd = Darwin.socket(AF_UNIX, SOCK_STREAM, 0) guard fd >= 0 else { throw DriverError("Cannot create driver socket: \(errno)") } do { try configure(fd) } catch { Darwin.close(fd) throw error } return fd } private static func configure(_ fd: Int32) throws { var enabled: Int32 = 1 guard fcntl(fd, F_SETFD, FD_CLOEXEC) == 0, setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &enabled, socklen_t(MemoryLayout.size)) == 0 else { throw DriverError("Cannot configure driver socket: \(errno)") } } private struct Address { var value = sockaddr_un() init(_ path: String) throws { let bytes = Array(path.utf8) guard path.hasPrefix("/"), !bytes.contains(0), bytes.count < MemoryLayout.size(ofValue: value.sun_path) else { throw DriverError("Driver socket path must be absolute, without NUL, and shorter than 104 bytes") } value.sun_family = sa_family_t(AF_UNIX) value.sun_len = UInt8(MemoryLayout.size) withUnsafeMutableBytes(of: &value.sun_path) { $0.copyBytes(from: bytes) } } mutating func withPointer(_ body: (UnsafePointer, socklen_t) -> Result) -> Result { withUnsafePointer(to: &value) { pointer in pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { body($0, socklen_t(MemoryLayout.size)) } } } } }