2 * Copyright (c) 2000-2005, 2008-2013 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * March 31, 2000 Allan Nathanson <ajn@apple.com>
34 #include <Availability.h>
35 #include <TargetConditionals.h>
36 #include <sys/cdefs.h>
37 #include <dispatch/dispatch.h>
38 #include <mach/mach.h>
39 #include <mach/mach_error.h>
41 #include <SystemConfiguration/SystemConfiguration.h>
42 #include <SystemConfiguration/SCPrivate.h>
43 #include "SCDynamicStoreInternal.h"
44 #include "config.h" /* MiG generated file */
48 notifyMPCopyDescription(const void *info
)
50 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
52 return CFStringCreateWithFormat(NULL
,
54 CFSTR("<SCDynamicStore notification MP> {store = %p}"),
60 rlsCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
62 mach_no_senders_notification_t
*buf
= msg
;
63 mach_msg_id_t msgid
= buf
->not_header
.msgh_id
;
64 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
65 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
67 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
68 /* the server died, disable additional callbacks */
70 SCLog(_sc_verbose
, LOG_INFO
, CFSTR(" rlsCallback(), notifier port closed"));
74 if (port
!= storePrivate
->rlsNotifyPort
) {
75 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("rlsCallback(), why is port != rlsNotifyPort?"));
79 /* re-establish notification and inform the client */
80 (void)__SCDynamicStoreReconnectNotifications(store
);
83 /* signal the real runloop source */
84 if (storePrivate
->rls
!= NULL
) {
85 CFRunLoopSourceSignal(storePrivate
->rls
);
92 rlsSchedule(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
94 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
95 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
98 SCLog(_sc_verbose
, LOG_DEBUG
,
99 CFSTR("schedule notifications for mode %@"),
100 (rl
!= NULL
) ? mode
: CFSTR("libdispatch"));
103 if (storePrivate
->rlList
== NULL
) {
104 CFMachPortContext context
= { 0
108 , notifyMPCopyDescription
110 mach_port_t oldNotify
;
113 kern_return_t status
;
116 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" activate callback runloop source"));
119 /* Allocating port (for server response) */
120 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &port
);
121 if (status
!= KERN_SUCCESS
) {
122 SCLog(TRUE
, LOG_ERR
, CFSTR("rlsSchedule mach_port_allocate(): %s"), mach_error_string(status
));
126 status
= mach_port_insert_right(mach_task_self(),
129 MACH_MSG_TYPE_MAKE_SEND
);
130 if (status
!= KERN_SUCCESS
) {
132 * We can't insert a send right into our own port! This should
133 * only happen if someone stomped on OUR port (so let's leave
136 SCLog(TRUE
, LOG_ERR
, CFSTR("rlsSchedule mach_port_insert_right(): %s"), mach_error_string(status
));
140 /* Request a notification when/if the server dies */
141 status
= mach_port_request_notification(mach_task_self(),
143 MACH_NOTIFY_NO_SENDERS
,
146 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
148 if (status
!= KERN_SUCCESS
) {
150 * We can't request a notification for our own port! This should
151 * only happen if someone stomped on OUR port (so let's leave
154 SCLog(TRUE
, LOG_ERR
, CFSTR("rlsSchedule mach_port_request_notification(): %s"), mach_error_string(status
));
158 if (oldNotify
!= MACH_PORT_NULL
) {
159 SCLog(TRUE
, LOG_ERR
, CFSTR("rlsSchedule(): oldNotify != MACH_PORT_NULL"));
164 __MACH_PORT_DEBUG(TRUE
, "*** rlsSchedule", port
);
165 status
= notifyviaport(storePrivate
->server
, port
, 0, (int *)&sc_status
);
167 if (__SCDynamicStoreCheckRetryAndHandleError(store
,
170 "rlsSchedule notifyviaport()")) {
174 if (status
!= KERN_SUCCESS
) {
175 if ((status
== MACH_SEND_INVALID_DEST
) || (status
== MIG_SERVER_DIED
)) {
176 /* remove the send right that we tried (but failed) to pass to the server */
177 (void) mach_port_deallocate(mach_task_self(), port
);
180 /* remove our receive right */
181 (void) mach_port_mod_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, -1);
185 if (sc_status
!= kSCStatusOK
) {
186 /* something [else] didn't work, remove our receive right */
187 (void) mach_port_mod_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, -1);
191 __MACH_PORT_DEBUG(TRUE
, "*** rlsSchedule (after notifyviaport)", port
);
192 storePrivate
->rlsNotifyPort
= _SC_CFMachPortCreateWithPort("SCDynamicStore",
196 storePrivate
->rlsNotifyRLS
= CFMachPortCreateRunLoopSource(NULL
, storePrivate
->rlsNotifyPort
, 0);
198 storePrivate
->rlList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
201 if ((rl
!= NULL
) && (storePrivate
->rlsNotifyRLS
!= NULL
)) {
202 if (!_SC_isScheduled(store
, rl
, mode
, storePrivate
->rlList
)) {
204 * if we are not already scheduled with this runLoop / runLoopMode
206 CFRunLoopAddSource(rl
, storePrivate
->rlsNotifyRLS
, mode
);
207 __MACH_PORT_DEBUG(TRUE
, "*** rlsSchedule (after CFRunLoopAddSource)", CFMachPortGetPort(storePrivate
->rlsNotifyPort
));
210 _SC_schedule(store
, rl
, mode
, storePrivate
->rlList
);
218 rlsCancel(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
221 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
222 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
225 SCLog(_sc_verbose
, LOG_DEBUG
,
226 CFSTR("cancel notifications for mode %@"),
227 (rl
!= NULL
) ? mode
: CFSTR("libdispatch"));
230 if ((rl
!= NULL
) && (storePrivate
->rlsNotifyRLS
!= NULL
)) {
231 if (_SC_unschedule(store
, rl
, mode
, storePrivate
->rlList
, FALSE
)) {
233 * if currently scheduled on this runLoop / runLoopMode
235 n
= CFArrayGetCount(storePrivate
->rlList
);
236 if (n
== 0 || !_SC_isScheduled(store
, rl
, mode
, storePrivate
->rlList
)) {
238 * if we are no longer scheduled to receive notifications for
239 * this runLoop / runLoopMode
241 CFRunLoopRemoveSource(rl
, storePrivate
->rlsNotifyRLS
, mode
);
248 kern_return_t status
;
251 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" cancel callback runloop source"));
253 __MACH_PORT_DEBUG((storePrivate
->rlsNotifyPort
!= NULL
),
255 CFMachPortGetPort(storePrivate
->rlsNotifyPort
));
257 if (storePrivate
->rlList
!= NULL
) {
258 CFRelease(storePrivate
->rlList
);
259 storePrivate
->rlList
= NULL
;
262 if (storePrivate
->rlsNotifyRLS
!= NULL
) {
263 /* invalidate & remove the run loop source */
264 CFRunLoopSourceInvalidate(storePrivate
->rlsNotifyRLS
);
265 CFRelease(storePrivate
->rlsNotifyRLS
);
266 storePrivate
->rlsNotifyRLS
= NULL
;
269 if (storePrivate
->rlsNotifyPort
!= NULL
) {
272 mp
= CFMachPortGetPort(storePrivate
->rlsNotifyPort
);
273 __MACH_PORT_DEBUG((storePrivate
->rlsNotifyPort
!= NULL
),
274 "*** rlsCancel (before invalidating/releasing CFMachPort)",
277 /* invalidate and release port */
278 CFMachPortInvalidate(storePrivate
->rlsNotifyPort
);
279 CFRelease(storePrivate
->rlsNotifyPort
);
280 storePrivate
->rlsNotifyPort
= NULL
;
282 /* and, finally, remove our receive right */
283 (void)mach_port_mod_refs(mach_task_self(), mp
, MACH_PORT_RIGHT_RECEIVE
, -1);
286 if (storePrivate
->server
!= MACH_PORT_NULL
) {
287 status
= notifycancel(storePrivate
->server
, (int *)&sc_status
);
289 (void) __SCDynamicStoreCheckRetryAndHandleError(store
,
292 "rlsCancel notifycancel()");
294 if (status
!= KERN_SUCCESS
) {
305 rlsPerform(void *info
)
307 CFArrayRef changedKeys
;
309 void (*context_release
)(const void *);
310 SCDynamicStoreCallBack rlsFunction
;
311 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
312 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
315 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" executing notification function"));
318 changedKeys
= SCDynamicStoreCopyNotifiedKeys(store
);
319 if (storePrivate
->disconnectForceCallBack
) {
320 storePrivate
->disconnectForceCallBack
= FALSE
;
321 if (changedKeys
== NULL
) {
322 changedKeys
= CFArrayCreate(NULL
, NULL
, 0, &kCFTypeArrayCallBacks
);
325 if (changedKeys
== NULL
) {
326 /* if no changes or something happened to the server */
328 } else if (CFArrayGetCount(changedKeys
) == 0) {
333 rlsFunction
= storePrivate
->rlsFunction
;
335 if (storePrivate
->rlsContext
.retain
!= NULL
) {
336 context_info
= (void *)storePrivate
->rlsContext
.retain(storePrivate
->rlsContext
.info
);
337 context_release
= storePrivate
->rlsContext
.release
;
339 context_info
= storePrivate
->rlsContext
.info
;
340 context_release
= NULL
;
342 if (rlsFunction
!= NULL
) {
343 (*rlsFunction
)(store
, changedKeys
, context_info
);
345 if (context_release
!= NULL
) {
346 context_release(context_info
);
351 CFRelease(changedKeys
);
357 rlsRetain(CFTypeRef cf
)
359 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
360 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
362 switch (storePrivate
->notifyStatus
) {
363 case NotifierNotRegistered
:
364 /* mark RLS active */
365 storePrivate
->notifyStatus
= Using_NotifierInformViaRunLoop
;
366 /* keep a reference to the store */
369 case Using_NotifierInformViaRunLoop
:
372 SCLog(TRUE
, LOG_ERR
, CFSTR("rlsRetain() error: notifyStatus=%d"), storePrivate
->notifyStatus
);
381 rlsRelease(CFTypeRef cf
)
383 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
384 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
386 switch (storePrivate
->notifyStatus
) {
387 case NotifierNotRegistered
:
389 case Using_NotifierInformViaRunLoop
:
390 /* mark RLS inactive */
391 storePrivate
->notifyStatus
= NotifierNotRegistered
;
392 storePrivate
->rls
= NULL
;
394 /* release our reference to the store */
398 SCLog(TRUE
, LOG_ERR
, CFSTR("rlsRelease() error: notifyStatus=%d"), storePrivate
->notifyStatus
);
407 rlsCopyDescription(const void *info
)
409 CFMutableStringRef result
;
410 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
411 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
413 result
= CFStringCreateMutable(NULL
, 0);
414 CFStringAppendFormat(result
, NULL
, CFSTR("<SCDynamicStore RLS> {"));
415 CFStringAppendFormat(result
, NULL
, CFSTR("store = %p"), store
);
416 if (storePrivate
->notifyStatus
== Using_NotifierInformViaRunLoop
) {
417 CFStringRef description
= NULL
;
419 CFStringAppendFormat(result
, NULL
, CFSTR(", callout = %p"), storePrivate
->rlsFunction
);
421 if ((storePrivate
->rlsContext
.info
!= NULL
) && (storePrivate
->rlsContext
.copyDescription
!= NULL
)) {
422 description
= (*storePrivate
->rlsContext
.copyDescription
)(storePrivate
->rlsContext
.info
);
424 if (description
== NULL
) {
425 description
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<SCDynamicStore context %p>"), storePrivate
->rlsContext
.info
);
427 if (description
== NULL
) {
428 description
= CFRetain(CFSTR("<no description>"));
430 CFStringAppendFormat(result
, NULL
, CFSTR(", context = %@"), description
);
431 CFRelease(description
);
433 CFStringAppendFormat(result
, NULL
, CFSTR("}"));
440 SCDynamicStoreCreateRunLoopSource(CFAllocatorRef allocator
,
441 SCDynamicStoreRef store
,
444 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
447 /* sorry, you must provide a session */
448 _SCErrorSet(kSCStatusNoStoreSession
);
452 if (storePrivate
->server
== MACH_PORT_NULL
) {
453 /* sorry, you must have an open session to play */
454 _SCErrorSet(kSCStatusNoStoreServer
);
458 switch (storePrivate
->notifyStatus
) {
459 case NotifierNotRegistered
:
460 case Using_NotifierInformViaRunLoop
:
461 /* OK to enable runloop notification */
464 /* sorry, you can only have one notification registered at once */
465 _SCErrorSet(kSCStatusNotifierActive
);
469 if (storePrivate
->rls
!= NULL
) {
470 CFRetain(storePrivate
->rls
);
472 CFRunLoopSourceContext context
= { 0 // version
473 , (void *)store
// info
474 , rlsRetain
// retain
475 , rlsRelease
// release
476 , rlsCopyDescription
// copyDescription
479 , rlsSchedule
// schedule
480 , rlsCancel
// cancel
481 , rlsPerform
// perform
484 storePrivate
->rls
= CFRunLoopSourceCreate(allocator
, order
, &context
);
485 if (storePrivate
->rls
== NULL
) {
486 _SCErrorSet(kSCStatusFailed
);
490 return storePrivate
->rls
;
495 SCDynamicStoreSetDispatchQueue(SCDynamicStoreRef store
, dispatch_queue_t queue
)
497 dispatch_group_t drainGroup
= NULL
;
498 dispatch_queue_t drainQueue
= NULL
;
499 dispatch_group_t group
= NULL
;
502 dispatch_source_t source
;
503 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
506 // sorry, you must provide a session
507 _SCErrorSet(kSCStatusNoStoreSession
);
512 if (storePrivate
->dispatchQueue
== NULL
) {
513 _SCErrorSet(kSCStatusInvalidArgument
);
521 if (storePrivate
->server
== MACH_PORT_NULL
) {
522 // sorry, you must have an open session to play
523 _SCErrorSet(kSCStatusNoStoreServer
);
527 if ((storePrivate
->dispatchQueue
!= NULL
) || (storePrivate
->rls
!= NULL
)) {
528 _SCErrorSet(kSCStatusInvalidArgument
);
532 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
533 // sorry, you can only have one notification registered at once...
534 _SCErrorSet(kSCStatusNotifierActive
);
539 * mark our using of the SCDynamicStore notifications, create and schedule
540 * the notification port (storePrivate->rlsNotifyPort), and a bunch of other
543 storePrivate
->notifyStatus
= Using_NotifierInformViaDispatch
;
544 rlsSchedule((void*)store
, NULL
, NULL
);
545 if (storePrivate
->rlsNotifyPort
== NULL
) {
546 /* if we could not schedule the notification */
547 _SCErrorSet(kSCStatusFailed
);
551 // retain the dispatch queue
552 storePrivate
->dispatchQueue
= queue
;
553 dispatch_retain(storePrivate
->dispatchQueue
);
556 // We've taken a reference to the callers dispatch_queue and we
557 // want to hold on to that reference until we've processed any/all
558 // notifications. To facilitate this we create a group, dispatch
559 // any notification blocks to via that group, and when the caller
560 // has told us to stop the notifications (unschedule) we wait for
561 // the group to empty and use the group's finalizer to release
562 // our reference to the SCDynamicStore.
564 group
= dispatch_group_create();
565 storePrivate
->dispatchGroup
= group
;
567 dispatch_set_context(storePrivate
->dispatchGroup
, (void *)store
);
568 dispatch_set_finalizer_f(storePrivate
->dispatchGroup
, (dispatch_function_t
)CFRelease
);
570 // create a dispatch source for the mach notifications
571 mp
= CFMachPortGetPort(storePrivate
->rlsNotifyPort
);
572 source
= dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV
, mp
, 0, queue
);
573 if (source
== NULL
) {
574 SCLog(TRUE
, LOG_ERR
, CFSTR("SCDynamicStore dispatch_source_create() failed"));
575 _SCErrorSet(kSCStatusFailed
);
579 dispatch_source_set_event_handler(source
, ^{
583 u_int8_t buf
[sizeof(mach_msg_empty_t
) + MAX_TRAILER_SIZE
];
584 mach_msg_empty_rcv_t msg
;
585 mach_no_senders_notification_t no_senders
;
588 kr
= mach_msg(¬ify_msg
.msg
.header
, // msg
589 MACH_RCV_MSG
, // options
591 sizeof(notify_msg
), // rcv_size
593 MACH_MSG_TIMEOUT_NONE
, // timeout
594 MACH_PORT_NULL
); // notify
595 if (kr
!= KERN_SUCCESS
) {
597 CFSTR("SCDynamicStore notification handler, kr=0x%x"),
602 msgid
= notify_msg
.msg
.header
.msgh_id
;
605 dispatch_group_async(group
, queue
, ^{
606 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
607 // re-establish notification and inform the client
608 (void)__SCDynamicStoreReconnectNotifications(store
);
610 rlsPerform(storePrivate
);
615 dispatch_source_set_cancel_handler(source
, ^{
616 dispatch_release(source
);
619 storePrivate
->dispatchSource
= source
;
620 dispatch_resume(source
);
628 if (storePrivate
->dispatchSource
!= NULL
) {
629 dispatch_source_cancel(storePrivate
->dispatchSource
);
630 storePrivate
->dispatchSource
= NULL
;
632 drainGroup
= storePrivate
->dispatchGroup
;
633 storePrivate
->dispatchGroup
= NULL
;
634 drainQueue
= storePrivate
->dispatchQueue
;
635 storePrivate
->dispatchQueue
= NULL
;
637 rlsCancel((void*)store
, NULL
, NULL
);
639 if (drainGroup
!= NULL
) {
640 dispatch_group_notify(drainGroup
, drainQueue
, ^{
641 // release group/queue references
642 dispatch_release(drainQueue
);
643 dispatch_release(drainGroup
); // releases our store reference
647 storePrivate
->notifyStatus
= NotifierNotRegistered
;