5 // Created by Mitch Adler on 11/2/16.
9 #import <Foundation/Foundation.h>
11 #import "CKDAKSLockMonitor.h"
13 #include <utilities/SecCFRelease.h>
14 #include <utilities/SecAKSWrappers.h>
15 #include <utilities/debugging.h>
19 @interface CKDAKSLockMonitor ()
21 @property XPCNotificationDispatcher* dispatcher;
22 @property XPCNotificationBlock notificationBlock;
23 @property dispatch_queue_t queue;
27 @implementation CKDAKSLockMonitor
29 + (instancetype) monitor {
30 return [[CKDAKSLockMonitor alloc] init];
33 - (instancetype)init {
34 if ((self = [super init])) {
35 XPCNotificationDispatcher* dispatcher = [XPCNotificationDispatcher dispatcher];
37 _queue = dispatch_queue_create("CKDAKSLockMonitor", NULL);
39 _unlockedSinceBoot = false;
41 /* also use dispatch to make sure */
43 __weak typeof(self) weakSelf = self;
44 notify_register_dispatch(kUserKeybagStateChangeNotification, &token, _queue, ^(int t) {
45 [weakSelf _onqueueRecheck];
50 [dispatcher addListener: self];
56 - (void) handleNotification:(const char *)name {
57 if (strcmp(name, kUserKeybagStateChangeNotification) == 0 || strcmp(name, "com.apple.mobile.keybagd.lock_status") == 0) {
62 - (void) notifyListener {
63 // Take a strong reference:
64 __strong __typeof(self.listener) listener = self.listener;
75 - (void)connectTo: (NSObject<CKDLockListener>*) listener {
77 [self notifyListener];
81 dispatch_async(_queue, ^{
82 [self _onqueueRecheck];
86 - (void) _onqueueRecheck {
87 dispatch_assert_queue(_queue);
88 CFErrorRef aksError = NULL;
89 bool locked = true; // Assume locked if we get an error
91 if (!SecAKSGetIsLocked(&locked, &aksError)) {
92 secerror("%@ Got error querying lock state: %@", self, aksError);
93 CFReleaseSafe(aksError);
96 BOOL previousLocked = self.locked;
100 _unlockedSinceBoot = true;
103 if (previousLocked != self.locked) {
104 // recheck might get called from ckdkvsproxy_queue (see 30510390)
105 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
106 [self notifyListener];