Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Keep the end of a root CFW run in the machine's console log

Jacky5ecee05d30b6
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHelperClient.swift b/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHelperClient.swift
index 00d1c43b..0a9a3956 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHelperClient.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHelperClient.swift
@@ -254,8 +254,10 @@ final class VPhoneLaunchpadHelperClient {
onLine: @escaping @Sendable (String) -> Void,
) async throws -> Int32 {
let authorization = try await authorizationSession.externalForm()
- receiver.setHandler(onLine)
- defer { receiver.setHandler(nil) }
+ guard receiver.claim(onLine) else {
+ throw Self.firmwareBusy
+ }
+ defer { receiver.release() }
return try await withTaskCancellationHandler {
try await request { proxy, done in
proxy.installCustomFirmware(
@@ -286,8 +288,10 @@ final class VPhoneLaunchpadHelperClient {
onLine: @escaping @Sendable (String) -> Void,
) async throws -> Int32 {
let authorization = try await authorizationSession.externalForm()
- receiver.setHandler(onLine)
- defer { receiver.setHandler(nil) }
+ guard receiver.claim(onLine) else {
+ throw Self.firmwareBusy
+ }
+ defer { receiver.release() }
return try await withTaskCancellationHandler {
try await request { proxy, done in
proxy.updateGuestEnvironment(
@@ -340,6 +344,11 @@ final class VPhoneLaunchpadHelperClient {
proxy?.cancelCustomFirmware {}
}
+ /// A CFW install or environment update from this app is still streaming.
+ private static var firmwareBusy: VPhoneLaunchpadError {
+ VPhoneLaunchpadError(String(localized: "Another CFW install or environment update is in progress. Wait for it to finish, then try again."))
+ }
+
// MARK: - XPC plumbing
private func currentConnection() -> NSXPCConnection {
@@ -504,18 +513,40 @@ final nonisolated class VPhoneLaunchpadHelperAuthorizationSession: @unchecked Se
}
/// Receives output lines the helper streams back during a CFW install.
+///
+/// One operation owns it at a time, as the helper runs one at a time. A second
+/// request made while one is streaming is turned away here: if it set its own
+/// handler, the running install's lines would go to the wrong log, and when it
+/// finished it would clear the handler the running install still needs.
final nonisolated class VPhoneLaunchpadHelperReceiver: NSObject, VPhoneLaunchpadHelperClientProtocol, @unchecked Sendable {
private let lock = NSLock()
private var handler: (@Sendable (String) -> Void)?
- func setHandler(_ handler: (@Sendable (String) -> Void)?) {
- lock.withLock { self.handler = handler }
+ /// Takes the receiver for one operation; false while another holds it.
+ func claim(_ handler: @escaping @Sendable (String) -> Void) -> Bool {
+ lock.withLock {
+ guard self.handler == nil else {
+ return false
+ }
+ self.handler = handler
+ return true
+ }
+ }
+
+ func release() {
+ lock.withLock { handler = nil }
}
func helperDidEmit(line: String) {
let handler = lock.withLock { self.handler }
handler?(line)
}
+
+ /// XPC hands this over after every line sent before it, so replying is
+ /// how the helper learns they have all been written.
+ func helperDidFinishOutput(reply: @escaping @Sendable () -> Void) {
+ reply()
+ }
}
#if DEBUG
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings b/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings
index 87528d71..bda10158 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings
@@ -5772,6 +5772,15 @@
"zh-Hans" : { "stringUnit" : { "state" : "translated", "value" : "正在更新客户机环境…" } }
}
},
+ "Another CFW install or environment update is in progress. Wait for it to finish, then try again." : {
+ "localizations" : {
+ "en" : { "stringUnit" : { "state" : "translated", "value" : "Another CFW install or environment update is in progress. Wait for it to finish, then try again." } },
+ "ja" : { "stringUnit" : { "state" : "translated", "value" : "別の CFW インストールまたはゲスト環境の更新が実行中です。完了するまで待ってから、もう一度お試しください。" } },
+ "ko" : { "stringUnit" : { "state" : "translated", "value" : "다른 CFW 설치 또는 게스트 환경 업데이트가 진행 중입니다. 완료될 때까지 기다린 후 다시 시도하십시오." } },
+ "vi" : { "stringUnit" : { "state" : "translated", "value" : "Một lần cài CFW hoặc cập nhật môi trường máy khách khác đang chạy. Hãy đợi nó xong rồi thử lại." } },
+ "zh-Hans" : { "stringUnit" : { "state" : "translated", "value" : "另一个 CFW 安装或客户机环境更新正在进行。请等它完成后再试。" } }
+ }
+ },
"Unable to update the guest environment." : {
"localizations" : {
"en" : { "stringUnit" : { "state" : "translated", "value" : "Unable to update the guest environment." } },
diff --git a/VPhoneLaunchpad/VPhoneLaunchpadHelper/VPhoneLaunchpadHelperService.swift b/VPhoneLaunchpad/VPhoneLaunchpadHelper/VPhoneLaunchpadHelperService.swift
index aeb019ba..3d6e9e5d 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpadHelper/VPhoneLaunchpadHelperService.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpadHelper/VPhoneLaunchpadHelperService.swift
@@ -173,6 +173,7 @@ final class VPhoneLaunchpadHelperService: NSObject, VPhoneLaunchpadHelperProtoco
emit("$ vphone-cli \(request.arguments.joined(separator: " ")) (as root)")
VPhoneLaunchpadLineReader.readLines(from: pipe.fileHandleForReading) { emit($0) }
process.waitUntilExit()
+ finishOutput()
reply(process.terminationStatus, nil)
}
}
@@ -296,4 +297,19 @@ final class VPhoneLaunchpadHelperService: NSObject, VPhoneLaunchpadHelperProtoco
let client = connection?.remoteObjectProxy as? VPhoneLaunchpadHelperClientProtocol
client?.helperDidEmit(line: line)
}
+
+ /// Waits until the app has handled every line emitted so far, the
+ /// connection fails, or 30 seconds pass. Without it the request's reply
+ /// could overtake the last lines, which then reach an app that has stopped
+ /// listening: the output a command flushes when it exits never made the
+ /// console log. Bounded, so a stuck app cannot hold the install lock.
+ private func finishOutput() {
+ let handled = DispatchSemaphore(value: 0)
+ let proxy = connection?.remoteObjectProxyWithErrorHandler { _ in handled.signal() }
+ guard let client = proxy as? VPhoneLaunchpadHelperClientProtocol else {
+ return
+ }
+ client.helperDidFinishOutput { handled.signal() }
+ _ = handled.wait(timeout: .now() + 30)
+ }
}
diff --git a/VPhoneLaunchpad/VPhoneLaunchpadShared/VPhoneLaunchpadHelperProtocol.swift b/VPhoneLaunchpad/VPhoneLaunchpadShared/VPhoneLaunchpadHelperProtocol.swift
index c64f4c5b..3e658122 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpadShared/VPhoneLaunchpadHelperProtocol.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpadShared/VPhoneLaunchpadHelperProtocol.swift
@@ -96,4 +96,11 @@ nonisolated protocol VPhoneLaunchpadHelperProtocol {
@objc(VPhoneLaunchpadHelperClientProtocol)
nonisolated protocol VPhoneLaunchpadHelperClientProtocol {
func helperDidEmit(line: String)
+
+ /// Replies once every line sent before it has been handled. The helper
+ /// waits for this before it replies to the request that produced them:
+ /// XPC orders the helper's own messages, but a request's reply can reach
+ /// the app ahead of lines still queued, and the app stops listening for
+ /// lines when the reply arrives.
+ func helperDidFinishOutput(reply: @escaping @Sendable () -> Void)
}