diff --git a/Sources/Packages/Sources/SecretAgentKit/Agent.swift b/Sources/Packages/Sources/SecretAgentKit/Agent.swift index 83ce175..5f3e6b3 100644 --- a/Sources/Packages/Sources/SecretAgentKit/Agent.swift +++ b/Sources/Packages/Sources/SecretAgentKit/Agent.swift @@ -120,7 +120,7 @@ extension Agent { /// Gives any store with no loaded secrets a chance to reload. func reloadSecretsIfNeccessary() async { for store in await storeList.stores { - if await store.secrets.isEmpty { + if await store.isAvailable, await store.secrets.isEmpty { let name = await store.name logger.debug("Store \(name, privacy: .public) has no loaded secrets. Reloading.") await store.reloadSecrets() diff --git a/Sources/Packages/Sources/SecretAgentKit/SigningRequestTracer.swift b/Sources/Packages/Sources/SecretAgentKit/SigningRequestTracer.swift index 8801d6f..1a2226f 100644 --- a/Sources/Packages/Sources/SecretAgentKit/SigningRequestTracer.swift +++ b/Sources/Packages/Sources/SecretAgentKit/SigningRequestTracer.swift @@ -36,7 +36,7 @@ extension SigningRequestTracer { /// - Parameter pid: The process ID to look up. /// - Returns: A ``SecretKit.SigningRequestProvenance.Process`` describing the process. func process(from pid: Int32) -> SigningRequestProvenance.Process { - var pidAndNameInfo = self.pidAndNameInfo(from: pid) + var pidAndNameInfo = unsafe self.pidAndNameInfo(from: pid) let ppid = unsafe pidAndNameInfo.kp_eproc.e_ppid != 0 ? pidAndNameInfo.kp_eproc.e_ppid : nil let procName = unsafe withUnsafeMutablePointer(to: &pidAndNameInfo.kp_proc.p_comm.0) { pointer in unsafe String(cString: pointer) diff --git a/Sources/Packages/Sources/SecretAgentKit/SocketController.swift b/Sources/Packages/Sources/SecretAgentKit/SocketController.swift index 9de2564..891a6c0 100644 --- a/Sources/Packages/Sources/SecretAgentKit/SocketController.swift +++ b/Sources/Packages/Sources/SecretAgentKit/SocketController.swift @@ -37,12 +37,19 @@ public struct SocketController { port = SocketPort(path: path) fileHandle = FileHandle(fileDescriptor: port.socket, closeOnDealloc: true) Task { [fileHandle, sessionsContinuation, logger] in + logger.debug("Starting socket controller task") for await notification in NotificationCenter.default.notifications(named: .NSFileHandleConnectionAccepted) { logger.debug("Socket controller accepted connection") - guard let new = notification.userInfo?[NSFileHandleNotificationFileHandleItem] as? FileHandle else { continue } + guard let new = notification.userInfo?[NSFileHandleNotificationFileHandleItem] as? FileHandle else { + logger.error("FileHandle not present in user info.") + continue + } let session = Session(fileHandle: new) + logger.debug("Yielding session") sessionsContinuation.yield(session) + logger.debug("Yielded session") await fileHandle.acceptConnectionInBackgroundAndNotifyOnMainActor() + logger.debug("Session waiting for connection.") } } fileHandle.acceptConnectionInBackgroundAndNotify(forModes: [RunLoop.Mode.common]) @@ -74,11 +81,15 @@ extension SocketController { /// Initializes a new Session. /// - Parameter fileHandle: The FileHandle used to communicate with the socket. init(fileHandle: FileHandle) { + logger.debug("Initializing session.") self.fileHandle = fileHandle provenance = SigningRequestTracer().provenance(from: fileHandle) + logger.debug("Traced provenance.") (messages, messagesContinuation) = AsyncStream.makeStream() Task { [messagesContinuation, logger] in + logger.debug("Starting Session task.") for await _ in NotificationCenter.default.notifications(named: .NSFileHandleDataAvailable, object: fileHandle) { + logger.debug("Session received data avilable message.") let data = fileHandle.availableData guard !data.isEmpty else { logger.debug("Socket controller received empty data, ending continuation.") @@ -90,7 +101,8 @@ extension SocketController { logger.debug("Socket controller yielded data.") } } - Task { + Task { [logger] in + logger.debug("Session waiting for data.") await fileHandle.waitForDataInBackgroundAndNotifyOnMainActor() } }