2 * Copyright (c) 2000-2005 Apple Computer, 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 <mach/mach.h>
35 #include <mach/mach_error.h>
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCPrivate.h>
39 #include "SCDynamicStoreInternal.h"
40 #include "config.h" /* MiG generated file */
43 informCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
45 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
46 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
47 mach_msg_empty_rcv_t
*buf
= msg
;
48 mach_msg_id_t msgid
= buf
->header
.msgh_id
;
49 SCDynamicStoreCallBack_v1 cbFunc
= storePrivate
->callbackFunction
;
50 void *cbArg
= storePrivate
->callbackArgument
;
52 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
53 /* the server died, disable additional callbacks */
55 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" notifier port closed, disabling notifier"));
57 } else if (cbFunc
== NULL
) {
58 /* there is no (longer) a callback function, disable additional callbacks */
60 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" no callback function, disabling notifier"));
64 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" executing notification function"));
66 if ((*cbFunc
)(store
, cbArg
)) {
68 * callback function returned success.
73 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" callback returned error, disabling notifier"));
79 if (port
!= storePrivate
->callbackPort
) {
80 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("informCallback, why is port != callbackPort?"));
84 /* invalidate the run loop source */
85 CFRunLoopSourceInvalidate(storePrivate
->callbackRLS
);
86 CFRelease(storePrivate
->callbackRLS
);
87 storePrivate
->callbackRLS
= NULL
;
90 CFMachPortInvalidate(storePrivate
->callbackPort
);
91 CFRelease(storePrivate
->callbackPort
);
92 storePrivate
->callbackPort
= NULL
;
94 /* disable notifier */
95 storePrivate
->notifyStatus
= NotifierNotRegistered
;
96 storePrivate
->callbackArgument
= NULL
;
97 storePrivate
->callbackFunction
= NULL
;
104 notifyMPCopyDescription(const void *info
)
106 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
108 return CFStringCreateWithFormat(NULL
,
110 CFSTR("<SCDynamicStore notification MP> {store = %p}"),
116 SCDynamicStoreNotifyCallback(SCDynamicStoreRef store
,
117 CFRunLoopRef runLoop
,
118 SCDynamicStoreCallBack_v1 func
,
121 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
122 kern_return_t status
;
124 mach_port_t oldNotify
;
126 CFMachPortContext context
= { 0
130 , notifyMPCopyDescription
134 /* sorry, you must provide a session */
135 _SCErrorSet(kSCStatusNoStoreSession
);
139 if (storePrivate
->server
== MACH_PORT_NULL
) {
140 /* sorry, you must have an open session to play */
141 _SCErrorSet(kSCStatusNoStoreServer
);
145 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
146 /* sorry, you can only have one notification registered at once */
147 _SCErrorSet(kSCStatusNotifierActive
);
151 /* Allocating port (for server response) */
152 storePrivate
->callbackPort
= CFMachPortCreate(NULL
,
157 /* Request a notification when/if the server dies */
158 port
= CFMachPortGetPort(storePrivate
->callbackPort
);
159 status
= mach_port_request_notification(mach_task_self(),
161 MACH_NOTIFY_NO_SENDERS
,
164 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
166 if (status
!= KERN_SUCCESS
) {
167 SCLog(TRUE
, LOG_DEBUG
, CFSTR("SCDynamicStoreNotifyCallback mach_port_request_notification(): %s"), mach_error_string(status
));
168 CFMachPortInvalidate(storePrivate
->callbackPort
);
169 CFRelease(storePrivate
->callbackPort
);
175 if (oldNotify
!= MACH_PORT_NULL
) {
176 SCLog(TRUE
, LOG_ERR
, CFSTR("SCDynamicStoreNotifyCallback(): why is oldNotify != MACH_PORT_NULL?"));
180 /* Requesting notification via mach port */
181 status
= notifyviaport(storePrivate
->server
,
186 if (status
!= KERN_SUCCESS
) {
188 if (status
!= MACH_SEND_INVALID_DEST
)
189 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreNotifyCallback notifyviaport(): %s"), mach_error_string(status
));
191 CFMachPortInvalidate(storePrivate
->callbackPort
);
192 CFRelease(storePrivate
->callbackPort
);
193 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
194 storePrivate
->server
= MACH_PORT_NULL
;
199 if (sc_status
!= kSCStatusOK
) {
200 _SCErrorSet(sc_status
);
204 /* set notifier active */
205 storePrivate
->notifyStatus
= Using_NotifierInformViaCallback
;
207 /* Creating/adding a run loop source for the port */
208 storePrivate
->callbackArgument
= arg
;
209 storePrivate
->callbackFunction
= func
;
210 storePrivate
->callbackRLS
= CFMachPortCreateRunLoopSource(NULL
, storePrivate
->callbackPort
, 0);
211 CFRunLoopAddSource(runLoop
, storePrivate
->callbackRLS
, kCFRunLoopDefaultMode
);
218 rlsCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
220 mach_msg_empty_rcv_t
*buf
= msg
;
221 mach_msg_id_t msgid
= buf
->header
.msgh_id
;
222 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
223 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
225 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
226 /* the server died, disable additional callbacks */
228 SCLog(_sc_verbose
, LOG_INFO
, CFSTR(" rlsCallback(), notifier port closed"));
232 if (port
!= storePrivate
->callbackPort
) {
233 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("rlsCallback(), why is port != callbackPort?"));
237 /* invalidate the run loop source(s) */
238 CFRunLoopSourceInvalidate(storePrivate
->callbackRLS
);
239 CFRelease(storePrivate
->callbackRLS
);
240 storePrivate
->callbackRLS
= NULL
;
242 /* invalidate port */
243 CFMachPortInvalidate(storePrivate
->callbackPort
);
244 CFRelease(storePrivate
->callbackPort
);
245 storePrivate
->callbackPort
= NULL
;
250 /* signal the real runloop source */
251 CFRunLoopSourceSignal(storePrivate
->rls
);
257 rlsPortInvalidate(CFMachPortRef mp
, void *info
) {
258 mach_port_t port
= CFMachPortGetPort(mp
);
260 // A simple deallocate won't get rid of all the references we've accumulated
262 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" invalidate = %d"), port
);
264 (void)mach_port_mod_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, -1);
269 rlsSchedule(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
271 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
272 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
275 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("schedule notifications for mode %@"), mode
);
278 if (storePrivate
->rlsRefs
++ == 0) {
279 CFMachPortContext context
= { 0
283 , notifyMPCopyDescription
285 mach_port_t oldNotify
;
288 kern_return_t status
;
291 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" activate callback runloop source"));
294 /* Allocating port (for server response) */
295 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &port
);
296 if (status
!= KERN_SUCCESS
) {
297 SCLog(TRUE
, LOG_DEBUG
, CFSTR("mach_port_allocate(): %s"), mach_error_string(status
));
301 status
= mach_port_insert_right(mach_task_self(),
304 MACH_MSG_TYPE_MAKE_SEND
);
305 if (status
!= KERN_SUCCESS
) {
306 SCLog(TRUE
, LOG_DEBUG
, CFSTR("mach_port_insert_right(): %s"), mach_error_string(status
));
307 (void) mach_port_destroy(mach_task_self(), port
);
311 /* Request a notification when/if the server dies */
312 status
= mach_port_request_notification(mach_task_self(),
314 MACH_NOTIFY_NO_SENDERS
,
317 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
319 if (status
!= KERN_SUCCESS
) {
320 SCLog(TRUE
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
321 (void) mach_port_destroy(mach_task_self(), port
);
326 if (oldNotify
!= MACH_PORT_NULL
) {
327 SCLog(TRUE
, LOG_ERR
, CFSTR("rlsSchedule(): why is oldNotify != MACH_PORT_NULL?"));
331 status
= notifyviaport(storePrivate
->server
, port
, 0, (int *)&sc_status
);
332 if (status
!= KERN_SUCCESS
) {
334 if (status
!= MACH_SEND_INVALID_DEST
)
335 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("notifyviaport(): %s"), mach_error_string(status
));
337 (void) mach_port_destroy(mach_task_self(), port
);
338 port
= MACH_PORT_NULL
;
339 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
340 storePrivate
->server
= MACH_PORT_NULL
;
344 storePrivate
->callbackPort
= CFMachPortCreateWithPort(NULL
, port
, rlsCallback
, &context
, NULL
);
345 CFMachPortSetInvalidationCallBack(storePrivate
->callbackPort
, rlsPortInvalidate
);
346 storePrivate
->callbackRLS
= CFMachPortCreateRunLoopSource(NULL
, storePrivate
->callbackPort
, 0);
349 if (storePrivate
->callbackRLS
!= NULL
) {
350 CFRunLoopAddSource(rl
, storePrivate
->callbackRLS
, mode
);
358 rlsCancel(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
360 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
361 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
364 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("cancel notifications for mode %@"), mode
);
367 if (storePrivate
->callbackRLS
!= NULL
) {
368 CFRunLoopRemoveSource(rl
, storePrivate
->callbackRLS
, mode
);
371 if (--storePrivate
->rlsRefs
== 0) {
373 kern_return_t status
;
376 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" cancel callback runloop source"));
379 if (storePrivate
->callbackRLS
!= NULL
) {
380 /* invalidate & remove the run loop source */
381 CFRunLoopSourceInvalidate(storePrivate
->callbackRLS
);
382 CFRelease(storePrivate
->callbackRLS
);
383 storePrivate
->callbackRLS
= NULL
;
386 if (storePrivate
->callbackPort
!= NULL
) {
387 /* invalidate port */
388 CFMachPortInvalidate(storePrivate
->callbackPort
);
389 CFRelease(storePrivate
->callbackPort
);
390 storePrivate
->callbackPort
= NULL
;
393 if (storePrivate
->server
!= MACH_PORT_NULL
) {
394 status
= notifycancel(storePrivate
->server
, (int *)&sc_status
);
395 if (status
!= KERN_SUCCESS
) {
397 if (status
!= MACH_SEND_INVALID_DEST
)
398 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("notifycancel(): %s"), mach_error_string(status
));
400 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
401 storePrivate
->server
= MACH_PORT_NULL
;
411 rlsPerform(void *info
)
413 CFArrayRef changedKeys
;
415 void (*context_release
)(const void *);
416 SCDynamicStoreCallBack rlsFunction
;
417 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
418 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
421 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" executing notification function"));
424 changedKeys
= SCDynamicStoreCopyNotifiedKeys(store
);
425 if (changedKeys
== NULL
) {
426 /* if no changes or something happened to the server */
430 if (CFArrayGetCount(changedKeys
) == 0) {
434 rlsFunction
= storePrivate
->rlsFunction
;
436 if (NULL
!= storePrivate
->rlsContext
.retain
) {
437 context_info
= (void *)storePrivate
->rlsContext
.retain(storePrivate
->rlsContext
.info
);
438 context_release
= storePrivate
->rlsContext
.release
;
440 context_info
= storePrivate
->rlsContext
.info
;
441 context_release
= NULL
;
443 (*rlsFunction
)(store
, changedKeys
, context_info
);
444 if (context_release
) {
445 context_release(context_info
);
450 CFRelease(changedKeys
);
456 rlsRetain(CFTypeRef cf
)
458 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
459 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
461 if (storePrivate
->notifyStatus
!= Using_NotifierInformViaRunLoop
) {
462 /* mark RLS active */
463 storePrivate
->notifyStatus
= Using_NotifierInformViaRunLoop
;
464 /* keep a reference to the store */
473 rlsRelease(CFTypeRef cf
)
475 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
476 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
478 /* mark RLS inactive */
479 storePrivate
->notifyStatus
= NotifierNotRegistered
;
480 storePrivate
->rls
= NULL
;
482 /* release our reference to the store */
490 rlsCopyDescription(const void *info
)
492 CFMutableStringRef result
;
493 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
494 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
496 result
= CFStringCreateMutable(NULL
, 0);
497 CFStringAppendFormat(result
, NULL
, CFSTR("<SCDynamicStore RLS> {"));
498 CFStringAppendFormat(result
, NULL
, CFSTR("store = %p"), store
);
499 if (storePrivate
->notifyStatus
== Using_NotifierInformViaRunLoop
) {
500 CFStringRef description
= NULL
;
502 CFStringAppendFormat(result
, NULL
, CFSTR(", callout = %p"), storePrivate
->rlsFunction
);
504 if ((storePrivate
->rlsContext
.info
!= NULL
) && (storePrivate
->rlsContext
.copyDescription
!= NULL
)) {
505 description
= (*storePrivate
->rlsContext
.copyDescription
)(storePrivate
->rlsContext
.info
);
507 if (description
== NULL
) {
508 description
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<SCDynamicStore context %p>"), storePrivate
->rlsContext
.info
);
510 if (description
== NULL
) {
511 description
= CFRetain(CFSTR("<no description>"));
513 CFStringAppendFormat(result
, NULL
, CFSTR(", context = %@"), description
);
514 CFRelease(description
);
516 CFStringAppendFormat(result
, NULL
, CFSTR(", callout = %p"), storePrivate
->callbackFunction
);
517 CFStringAppendFormat(result
, NULL
, CFSTR(", info = %p"), storePrivate
->callbackArgument
);
519 CFStringAppendFormat(result
, NULL
, CFSTR("}"));
526 SCDynamicStoreCreateRunLoopSource(CFAllocatorRef allocator
,
527 SCDynamicStoreRef store
,
530 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
533 /* sorry, you must provide a session */
534 _SCErrorSet(kSCStatusNoStoreSession
);
538 if (storePrivate
->server
== MACH_PORT_NULL
) {
539 /* sorry, you must have an open session to play */
540 _SCErrorSet(kSCStatusNoStoreServer
);
544 switch (storePrivate
->notifyStatus
) {
545 case NotifierNotRegistered
:
546 case Using_NotifierInformViaRunLoop
:
547 /* OK to enable runloop notification */
550 /* sorry, you can only have one notification registered at once */
551 _SCErrorSet(kSCStatusNotifierActive
);
555 if (storePrivate
->rls
!= NULL
) {
556 CFRetain(storePrivate
->rls
);
558 CFRunLoopSourceContext context
= { 0 // version
559 , (void *)store
// info
560 , rlsRetain
// retain
561 , rlsRelease
// release
562 , rlsCopyDescription
// copyDescription
565 , rlsSchedule
// schedule
566 , rlsCancel
// cancel
567 , rlsPerform
// perform
570 storePrivate
->rls
= CFRunLoopSourceCreate(allocator
, order
, &context
);
573 if (storePrivate
->rls
== NULL
) {
574 _SCErrorSet(kSCStatusFailed
);
578 return storePrivate
->rls
;