Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130import Darwinpublic 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<Int32>.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<Int32>.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<sockaddr_un>.size) withUnsafeMutableBytes(of: &value.sun_path) { $0.copyBytes(from: bytes) } }
mutating func withPointer<Result>(_ body: (UnsafePointer<sockaddr>, socklen_t) -> Result) -> Result { withUnsafePointer(to: &value) { pointer in pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { body($0, socklen_t(MemoryLayout<sockaddr_un>.size)) } } } }}