CapabilityProtocol.swift
sha256:bd1eb24434d281fe65142038c1bf217493421f5a86880c22dff7e6037d2d4700
feat(companion): add production macOS distribution path
Human
minor
⚠ breaking
24 days ago
| 1 | import Foundation |
| 2 | import XPC |
| 3 | |
| 4 | public enum CustodyCapability { |
| 5 | public static let scopeKey = "scope" |
| 6 | public static let descriptorKey = "capability_fd" |
| 7 | public static let errorKey = "error" |
| 8 | |
| 9 | /// Requests a capability over a launchd Mach service. The service validates |
| 10 | /// the sender from the XPC message audit token before attaching a descriptor. |
| 11 | public static func request(mode: KnowtationRuntimeMode) throws -> FileHandle { |
| 12 | let connection = xpc_connection_create_mach_service( |
| 13 | KnowtationReleaseContract.custodyMachService, |
| 14 | nil, |
| 15 | 0 |
| 16 | ) |
| 17 | xpc_connection_set_event_handler(connection) { _ in } |
| 18 | xpc_connection_resume(connection) |
| 19 | defer { xpc_connection_cancel(connection) } |
| 20 | |
| 21 | let message = xpc_dictionary_create(nil, nil, 0) |
| 22 | xpc_dictionary_set_string(message, scopeKey, mode.rawValue) |
| 23 | let reply = xpc_connection_send_message_with_reply_sync(connection, message) |
| 24 | guard xpc_get_type(reply) == XPC_TYPE_DICTIONARY else { |
| 25 | throw RuntimeLaunchError.capabilityUnavailable("invalid service reply") |
| 26 | } |
| 27 | if let errorPointer = xpc_dictionary_get_string(reply, errorKey) { |
| 28 | throw RuntimeLaunchError.capabilityUnavailable(String(cString: errorPointer)) |
| 29 | } |
| 30 | let descriptor = xpc_dictionary_dup_fd(reply, descriptorKey) |
| 31 | guard descriptor >= 0 else { |
| 32 | throw RuntimeLaunchError.capabilityUnavailable("descriptor unavailable") |
| 33 | } |
| 34 | return FileHandle(fileDescriptor: descriptor, closeOnDealloc: true) |
| 35 | } |
| 36 | } |
File History
1 commit
sha256:bd1eb24434d281fe65142038c1bf217493421f5a86880c22dff7e6037d2d4700
feat(companion): add production macOS distribution path
Human
minor
⚠
24 days ago