Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Apply the service profile again when launchd refuses a change early on

Lakr2338b1e66ccf9ae
diff --git a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneTemplateSetupBoot.swift b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneTemplateSetupBoot.swift
index 841586b5..f62b135c 100644
--- a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneTemplateSetupBoot.swift
+++ b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneTemplateSetupBoot.swift
@@ -876,11 +876,18 @@ public final class VPhoneTemplateSetupBoot {
log(" service profile none")
return
}
- let applied = try call(
- "services.profile.apply",
- ["profile": plan.serviceProfile, "groups": plan.serviceGroups, "force": true],
- deadline: end,
- )
+ 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)
+ }
try requireApplied(applied)
guard applied["profile"] as? String == plan.serviceProfile else {
throw StepError("services.profile.apply answered profile \(applied["profile"] ?? "nil"), not \(plan.serviceProfile)")
@@ -916,14 +923,19 @@ public final class VPhoneTemplateSetupBoot {
}
private func requireApplied(_ result: [String: Any]) throws {
- let failed = (result["failed"] as? [[String: Any]] ?? []).map { row in
- "\(row["label"] as? String ?? "?") (\(row["action"] as? String ?? "?"): \(row["error"] as? String ?? "?"))"
- }
+ let failed = Self.failures(result)
guard failed.isEmpty else {
throw StepError("services.profile.apply could not change \(failed.joined(separator: "; "))")
}
}
+ /// The services an apply could not change, as `label (action: error)`.
+ static func failures(_ result: [String: Any]) -> [String] {
+ (result["failed"] as? [[String: Any]] ?? []).map { row in
+ "\(row["label"] as? String ?? "?") (\(row["action"] as? String ?? "?"): \(row["error"] as? String ?? "?"))"
+ }
+ }
+
/// f. Reboots the guest and waits for vphoned of the next boot: launchd's
/// start time (pid 1) tells the boots apart.
private func reboot() throws {
diff --git a/VPhoneKit/VPhoneCoreKitTests/Bundle/TemplateSetupBootTests.swift b/VPhoneKit/VPhoneCoreKitTests/Bundle/TemplateSetupBootTests.swift
index 7e601ec7..ebd51765 100644
--- a/VPhoneKit/VPhoneCoreKitTests/Bundle/TemplateSetupBootTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/Bundle/TemplateSetupBootTests.swift
@@ -45,6 +45,9 @@ private final class FakeGuest: VPhoneTemplateSetupMachine {
/// Apps vphoned had to unregister more than once, as it reports them.
var unregisterAttempts: [String: Int] = [:]
var servicesKeepRunning = false
+ /// 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 rebootNeverReturns = false
var stopsCleanly = true
/// vphoned still answers the first ping after `system.reboot`, then takes
@@ -218,6 +221,12 @@ private final class FakeGuest: VPhoneTemplateSetupMachine {
return ["results": results, "removed": ids.count, "failed": 0]
case "services.profile.apply":
let wanted = params["profile"] as? String ?? ""
+ 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)"],
+ ]]
+ }
profile = wanted
if wanted == "trimmed" {
groups = ["app_store", "base", "signin_followup"] + (params["groups"] as? [String] ?? [])
@@ -431,6 +440,26 @@ struct TemplateSetupBootTests {
#expect(outcome.isComplete)
}
+ @Test func `a service launchd refuses early in the boot is applied again`() throws {
+ let guest = FakeGuest(clock: FakeClock())
+ guest.profileRefusals = 1
+ let outcome = try run(guest).get()
+ #expect(guest.calls.count(where: { $0 == "services.profile.apply" }) == 2)
+ #expect(outcome.serviceProfile == "trimmed")
+ }
+
+ @Test func `a service launchd keeps refusing fails the profile step after two retries`() throws {
+ let guest = FakeGuest(clock: FakeClock())
+ guest.profileRefusals = 5
+ guard case let .failure(failure) = run(guest) else {
+ Issue.record("the setup boot succeeded")
+ return
+ }
+ #expect(failure.step == .serviceProfile)
+ #expect(failure.reason.contains("com.apple.weatherd"))
+ #expect(guest.calls.count(where: { $0 == "services.profile.apply" }) == 3)
+ }
+
@Test func `setup skip that fails early in the boot is retried`() throws {
let guest = FakeGuest(clock: FakeClock())
guest.skipFailures = 3