Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Republish the gyroscope status only while samples flow

Jacky7cdf6a53e330
diff --git a/Research/Guest/virtual_gyroscope.md b/Research/Guest/virtual_gyroscope.md
index 76ceefd4..dd780fbd 100644
--- a/Research/Guest/virtual_gyroscope.md
+++ b/Research/Guest/virtual_gyroscope.md
@@ -38,10 +38,14 @@ Clear leaves the service and subscriptions alive and emits stationary samples.
After a HID reset, the old service is cancelled and released only from its
cancel handler, then a new service is registered after five seconds.
-`VPhoneGyroscopeStatus` is updated at configuration and service transitions and
-every two seconds. The RPC result includes `provider` and `provider_running`;
-the latter requires an enumerated service and a heartbeat younger than ten
-seconds. The status includes service ID, PID, report interval, dispatched event
+`VPhoneGyroscopeStatus` is updated at configuration and service transitions,
+and every two seconds while samples are dispatched (a client has set a report
+interval). The RPC result includes `provider` and `provider_running`; the
+latter requires an enumerated service and a running process with the status's
+PID, and while samples are dispatched also a status younger than ten seconds.
+Until 2026-10-09 the provider republished every two seconds regardless, so an
+idle guest's backboardd rewrote the preferences domain through cfprefsd 450
+times in 15 minutes. The status includes service ID, PID, report interval, dispatched event
count, dispatch failures, last error, and the configuration consumed by the
provider. **Enumeration and dispatch do not prove CoreMotion acceptance.**
diff --git a/VPhoneDaemon/Native/vphoned_gyro.m b/VPhoneDaemon/Native/vphoned_gyro.m
index e49b5608..bbfdf4e0 100644
--- a/VPhoneDaemon/Native/vphoned_gyro.m
+++ b/VPhoneDaemon/Native/vphoned_gyro.m
@@ -1,6 +1,8 @@
#import "Include/VphonedNative.h"
#import "../../VPhoneGuestComponents/Gyroscope/VPhoneGyroscopeState.h"
+#include <errno.h>
#include <notify.h>
+#include <signal.h>
static id VPhoneGyroscopeRead(CFStringRef key) {
return CFBridgingRelease(CFPreferencesCopyValue(key, CFSTR(VP_GYRO_DOMAIN), CFSTR("mobile"),
@@ -19,7 +21,18 @@ NSDictionary *vp_gyro_get(void) {
[status[@"enumerated"] isKindOfClass:NSNumber.class]) {
result[@"provider"] = status;
double age = [NSDate date].timeIntervalSince1970 - [status[@"updated_at"] doubleValue];
- result[@"provider_running"] = @([status[@"enumerated"] boolValue] && age >= 0 && age < 10);
+ // The provider republishes every two seconds only while it dispatches
+ // samples; idle, its status is as old as the last transition, and the
+ // backboardd that wrote it must still be running instead.
+ BOOL dispatching = [status[@"report_interval_us"] isKindOfClass:NSNumber.class] &&
+ [status[@"report_interval_us"] unsignedLongLongValue] > 0;
+ BOOL alive = NO;
+ if ([status[@"pid"] isKindOfClass:NSNumber.class]) {
+ pid_t pid = [status[@"pid"] intValue];
+ alive = pid > 1 && (kill(pid, 0) == 0 || errno == EPERM);
+ }
+ BOOL fresh = age >= 0 && age < 10;
+ result[@"provider_running"] = @([status[@"enumerated"] boolValue] && alive && (fresh || !dispatching));
} else {
result[@"provider"] = NSNull.null;
result[@"provider_running"] = @NO;
diff --git a/VPhoneGuestComponents/Gyroscope/libvphonegyro.m b/VPhoneGuestComponents/Gyroscope/libvphonegyro.m
index c96df1a9..79aa669e 100644
--- a/VPhoneGuestComponents/Gyroscope/libvphonegyro.m
+++ b/VPhoneGuestComponents/Gyroscope/libvphonegyro.m
@@ -10,6 +10,7 @@
@interface VPhoneGyroscopeProvider : NSObject
@property (nonatomic, strong) dispatch_queue_t queue;
@property (nonatomic, strong) dispatch_source_t timer;
+@property (nonatomic, strong) dispatch_source_t heartbeat;
@property (nonatomic, strong) id<VPhoneGyroscopeHIDService> service;
@property (nonatomic) Class<VPhoneGyroscopeHIDEvent> eventClass;
@property (nonatomic, strong) NSMutableDictionary *properties;
@@ -54,6 +55,18 @@
uint64_t period = self.enumerated && self.interval ? self.interval * NSEC_PER_USEC : DISPATCH_TIME_FOREVER;
dispatch_source_set_timer(self.timer, period == DISPATCH_TIME_FOREVER ? DISPATCH_TIME_FOREVER : DISPATCH_TIME_NOW,
period, NSEC_PER_MSEC);
+ // The status is republished every two seconds only while samples flow,
+ // to keep its dispatch counts current. Otherwise it changes only at the
+ // transitions, which publish it themselves, and vphoned judges the
+ // provider by its pid. An unconditional heartbeat had backboardd rewrite
+ // the preferences domain through cfprefsd every two seconds for the
+ // guest's whole life.
+ if (self.heartbeat) {
+ BOOL flowing = period != DISPATCH_TIME_FOREVER;
+ dispatch_source_set_timer(self.heartbeat,
+ flowing ? dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC) : DISPATCH_TIME_FOREVER,
+ 2 * NSEC_PER_SEC, 100 * NSEC_PER_MSEC);
+ }
}
- (id)sample {
@@ -141,15 +154,12 @@
}
}
});
+ self.heartbeat = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.queue);
+ dispatch_source_set_event_handler(self.heartbeat, ^{ [self publishStatus]; });
[self updateTimer];
dispatch_resume(self.timer);
+ dispatch_resume(self.heartbeat);
[self createService];
- [self heartbeat];
-}
-
-- (void)heartbeat {
- [self publishStatus];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), self.queue, ^{ [self heartbeat]; });
}
// MARK: - HIDVirtualEventServiceDelegate selectors