Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Retry the service profile only for launchd's refusals, both ways

Lakr233523d9697f26e
diff --git a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneTemplateSetupBoot.swift b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneTemplateSetupBoot.swift
index f62b135c..fb2c7817 100644
--- a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneTemplateSetupBoot.swift
+++ b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneTemplateSetupBoot.swift
@@ -869,25 +869,17 @@ public final class VPhoneTemplateSetupBoot {
// Undo a profile only when one is recorded; a fresh guest has none.
let state = try call("services.profile", [:], deadline: end)
if state["profile"] as? String ?? "none" != "none" {
- try requireApplied(call("services.profile.apply", ["profile": "none", "force": true], deadline: end))
+ try requireApplied(applyProfile(["profile": "none", "force": true], deadline: end))
}
outcome.serviceProfile = "none"
outcome.serviceGroups = []
log(" service profile none")
return
}
- let params: [String: Any] = ["profile": plan.serviceProfile, "groups": plan.serviceGroups, "force": true]
- var applied = try call("services.profile.apply", params, deadline: end)
- // launchd can refuse a disable this early in the boot and take it a
- // moment later: "disable requires root (launchd status 1)" for
- // com.apple.seserviced on iOS 26.5 and com.apple.fitnesscoachingd on
- // iPadOS 27.0.1 (2026-10-23). The apply records only what it changed,
- // so applying again redoes just the rest.
- for wait in [5.0, 15.0] where !Self.failures(applied).isEmpty && clock.now().addingTimeInterval(wait) < end {
- log(" retrying the service profile in \(Int(wait)) s: \(Self.failures(applied).joined(separator: "; "))")
- clock.sleep(wait)
- applied = try call("services.profile.apply", params, deadline: end)
- }
+ let applied = try applyProfile(
+ ["profile": plan.serviceProfile, "groups": plan.serviceGroups, "force": true],
+ deadline: end,
+ )
try requireApplied(applied)
guard applied["profile"] as? String == plan.serviceProfile else {
throw StepError("services.profile.apply answered profile \(applied["profile"] ?? "nil"), not \(plan.serviceProfile)")
@@ -922,6 +914,32 @@ public final class VPhoneTemplateSetupBoot {
}
}
+ /// `services.profile.apply`, applied again 5 and 15 s later while launchd
+ /// refuses some of its changes this early in the boot and takes them a
+ /// moment later: "disable requires root (launchd status 1)" for
+ /// com.apple.seserviced on iOS 26.5 and com.apple.fitnesscoachingd on
+ /// iPadOS 27.0.1 (2026-10-23). vphoned records only what an apply changed,
+ /// so another apply redoes just the rest; what each one disabled is added up.
+ private func applyProfile(_ params: [String: Any], deadline end: Date) throws -> [String: Any] {
+ var applied = try call("services.profile.apply", params, deadline: end)
+ var disabled = strings(applied["disabled"])
+ for wait in [5.0, 15.0] where Self.isEarlyBootRefusal(applied) && clock.now().addingTimeInterval(wait) < end {
+ log(" retrying the service profile in \(Int(wait)) s: \(Self.failures(applied).joined(separator: "; "))")
+ clock.sleep(wait)
+ applied = try call("services.profile.apply", params, deadline: end)
+ disabled += strings(applied["disabled"])
+ }
+ applied["disabled"] = disabled
+ return applied
+ }
+
+ /// Whether every change an apply could not make was launchd refusing it,
+ /// as icli reports its EPERM and EACCES. Anything else fails at once.
+ static func isEarlyBootRefusal(_ result: [String: Any]) -> Bool {
+ let failed = result["failed"] as? [[String: Any]] ?? []
+ return !failed.isEmpty && failed.allSatisfy { ($0["error"] as? String ?? "").contains("requires root (launchd status") }
+ }
+
private func requireApplied(_ result: [String: Any]) throws {
let failed = Self.failures(result)
guard failed.isEmpty else {
diff --git a/VPhoneKit/VPhoneCoreKitTests/Bundle/TemplateSetupBootTests.swift b/VPhoneKit/VPhoneCoreKitTests/Bundle/TemplateSetupBootTests.swift
index ebd51765..5c305d2f 100644
--- a/VPhoneKit/VPhoneCoreKitTests/Bundle/TemplateSetupBootTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/Bundle/TemplateSetupBootTests.swift
@@ -48,6 +48,7 @@ private final class FakeGuest: VPhoneTemplateSetupMachine {
/// Applies whose disable of com.apple.weatherd launchd refuses, as it
/// refused com.apple.seserviced early in an iOS 26.5 setup boot.
var profileRefusals = 0
+ var profileRefusalError = "disable requires root (launchd status 1)"
var rebootNeverReturns = false
var stopsCleanly = true
/// vphoned still answers the first ping after `system.reboot`, then takes
@@ -224,7 +225,7 @@ private final class FakeGuest: VPhoneTemplateSetupMachine {
if wanted == "trimmed", profileRefusals > 0 {
profileRefusals -= 1
return ["profile": wanted, "disabled": [String](), "enabled": [String](), "owned": 0, "failed": [
- ["label": "com.apple.weatherd", "action": "disable", "error": "disable requires root (launchd status 1)"],
+ ["label": "com.apple.weatherd", "action": "disable", "error": profileRefusalError],
]]
}
profile = wanted
@@ -448,6 +449,18 @@ struct TemplateSetupBootTests {
#expect(outcome.serviceProfile == "trimmed")
}
+ @Test func `a profile failure that is not launchd refusing fails at once`() throws {
+ let guest = FakeGuest(clock: FakeClock())
+ guest.profileRefusals = 1
+ guest.profileRefusalError = "unknown service"
+ guard case let .failure(failure) = run(guest) else {
+ Issue.record("the setup boot succeeded")
+ return
+ }
+ #expect(failure.step == .serviceProfile)
+ #expect(guest.calls.count(where: { $0 == "services.profile.apply" }) == 1)
+ }
+
@Test func `a service launchd keeps refusing fails the profile step after two retries`() throws {
let guest = FakeGuest(clock: FakeClock())
guest.profileRefusals = 5