2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * March 31, 2000 Allan Nathanson <ajn@apple.com>
36 #include <mach/mach.h>
37 #include <mach/mach_error.h>
39 #include <SystemConfiguration/SystemConfiguration.h>
40 #include <SystemConfiguration/SCPrivate.h>
41 #include "SCDynamicStoreInternal.h"
42 #include "config.h" /* MiG generated file */
45 informCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
47 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
48 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
49 mach_msg_empty_rcv_t
*buf
= msg
;
50 mach_msg_id_t msgid
= buf
->header
.msgh_id
;
51 SCDynamicStoreCallBack_v1 cbFunc
= storePrivate
->callbackFunction
;
52 void *cbArg
= storePrivate
->callbackArgument
;
54 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
55 /* the server died, disable additional callbacks */
56 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 */
59 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" no callback function, disabling notifier"));
61 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" executing notifiction function"));
62 if ((*cbFunc
)(store
, cbArg
)) {
64 * callback function returned success.
68 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" callback returned error, disabling notifier"));
73 if (port
!= storePrivate
->callbackPort
) {
74 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("informCallback, why is port != callbackPort?"));
78 /* remove the run loop source */
79 CFRunLoopRemoveSource(storePrivate
->callbackRunLoop
,
80 storePrivate
->callbackRunLoopSource
,
81 kCFRunLoopDefaultMode
);
82 CFRelease(storePrivate
->callbackRunLoopSource
);
83 storePrivate
->callbackRunLoop
= NULL
;
84 storePrivate
->callbackRunLoopSource
= NULL
;
87 CFMachPortInvalidate(storePrivate
->callbackPort
);
88 CFRelease(storePrivate
->callbackPort
);
89 storePrivate
->callbackPort
= NULL
;
91 /* disable notifier */
92 storePrivate
->notifyStatus
= NotifierNotRegistered
;
93 storePrivate
->callbackArgument
= NULL
;
94 storePrivate
->callbackFunction
= NULL
;
101 SCDynamicStoreNotifyCallback(SCDynamicStoreRef store
,
102 CFRunLoopRef runLoop
,
103 SCDynamicStoreCallBack_v1 func
,
106 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
107 kern_return_t status
;
109 mach_port_t oldNotify
;
111 CFMachPortContext context
= { 0
118 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreNotifyCallback:"));
121 /* sorry, you must provide a session */
122 _SCErrorSet(kSCStatusNoStoreSession
);
126 if (storePrivate
->server
== MACH_PORT_NULL
) {
127 /* sorry, you must have an open session to play */
128 _SCErrorSet(kSCStatusNoStoreServer
);
132 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
133 /* sorry, you can only have one notification registered at once */
134 _SCErrorSet(kSCStatusNotifierActive
);
138 /* Allocating port (for server response) */
139 storePrivate
->callbackPort
= CFMachPortCreate(NULL
,
144 /* Request a notification when/if the server dies */
145 port
= CFMachPortGetPort(storePrivate
->callbackPort
);
146 status
= mach_port_request_notification(mach_task_self(),
148 MACH_NOTIFY_NO_SENDERS
,
151 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
153 if (status
!= KERN_SUCCESS
) {
154 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
155 CFMachPortInvalidate(storePrivate
->callbackPort
);
156 CFRelease(storePrivate
->callbackPort
);
161 if (oldNotify
!= MACH_PORT_NULL
) {
162 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCDynamicStoreNotifyCallback(): why is oldNotify != MACH_PORT_NULL?"));
165 /* Requesting notification via mach port */
166 status
= notifyviaport(storePrivate
->server
,
171 if (status
!= KERN_SUCCESS
) {
172 if (status
!= MACH_SEND_INVALID_DEST
)
173 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("notifyviaport(): %s"), mach_error_string(status
));
174 CFMachPortInvalidate(storePrivate
->callbackPort
);
175 CFRelease(storePrivate
->callbackPort
);
176 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
177 storePrivate
->server
= MACH_PORT_NULL
;
182 if (sc_status
!= kSCStatusOK
) {
183 _SCErrorSet(sc_status
);
187 /* set notifier active */
188 storePrivate
->notifyStatus
= Using_NotifierInformViaCallback
;
190 /* Creating/adding a run loop source for the port */
191 storePrivate
->callbackArgument
= arg
;
192 storePrivate
->callbackFunction
= func
;
193 storePrivate
->callbackRunLoop
= runLoop
;
194 storePrivate
->callbackRunLoopSource
=
195 CFMachPortCreateRunLoopSource(NULL
, storePrivate
->callbackPort
, 0);
197 CFRunLoopAddSource(storePrivate
->callbackRunLoop
,
198 storePrivate
->callbackRunLoopSource
,
199 kCFRunLoopDefaultMode
);
206 rlsCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
208 mach_msg_empty_rcv_t
*buf
= msg
;
209 mach_msg_id_t msgid
= buf
->header
.msgh_id
;
210 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
211 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
213 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
214 /* the server died, disable additional callbacks */
215 SCLog(_sc_verbose
, LOG_INFO
, CFSTR(" rlsCallback(), notifier port closed"));
218 if (port
!= storePrivate
->callbackPort
) {
219 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("rlsCallback(), why is port != callbackPort?"));
223 /* remove the run loop source(s) */
224 CFRunLoopSourceInvalidate(storePrivate
->callbackRunLoopSource
);
225 CFRelease(storePrivate
->callbackRunLoopSource
);
226 storePrivate
->callbackRunLoopSource
= NULL
;
228 /* invalidate port */
229 CFMachPortInvalidate(storePrivate
->callbackPort
);
230 CFRelease(storePrivate
->callbackPort
);
231 storePrivate
->callbackPort
= NULL
;
236 /* signal the real runloop source */
237 CFRunLoopSourceSignal(storePrivate
->rls
);
243 rlsPortInvalidate(CFMachPortRef mp
, void *info
) {
244 mach_port_t port
= CFMachPortGetPort(mp
);
246 // A simple deallocate won't get rid of all the references we've accumulated
247 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" invalidate = %d"), port
);
248 (void)mach_port_mod_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, -1);
253 rlsSchedule(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
255 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
256 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
258 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("schedule notifications for mode %@"), mode
);
260 if (storePrivate
->rlsRefs
++ == 0) {
261 CFMachPortContext context
= { 0
267 mach_port_t oldNotify
;
270 kern_return_t status
;
272 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" activate callback runloop source"));
274 /* Allocating port (for server response) */
275 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &port
);
276 if (status
!= KERN_SUCCESS
) {
277 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_allocate(): %s"), mach_error_string(status
));
280 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" port = %d"), port
);
282 status
= mach_port_insert_right(mach_task_self(),
285 MACH_MSG_TYPE_MAKE_SEND
);
286 if (status
!= KERN_SUCCESS
) {
287 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_insert_right(): %s"), mach_error_string(status
));
288 (void) mach_port_destroy(mach_task_self(), port
);
292 /* Request a notification when/if the server dies */
293 status
= mach_port_request_notification(mach_task_self(),
295 MACH_NOTIFY_NO_SENDERS
,
298 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
300 if (status
!= KERN_SUCCESS
) {
301 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
302 (void) mach_port_destroy(mach_task_self(), port
);
306 if (oldNotify
!= MACH_PORT_NULL
) {
307 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("rlsSchedule(): why is oldNotify != MACH_PORT_NULL?"));
310 status
= notifyviaport(storePrivate
->server
, port
, 0, (int *)&sc_status
);
311 if (status
!= KERN_SUCCESS
) {
312 if (status
!= MACH_SEND_INVALID_DEST
)
313 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("notifyviaport(): %s"), mach_error_string(status
));
314 (void) mach_port_destroy(mach_task_self(), port
);
315 port
= MACH_PORT_NULL
;
316 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
317 storePrivate
->server
= MACH_PORT_NULL
;
321 storePrivate
->callbackPort
= CFMachPortCreateWithPort(NULL
, port
, rlsCallback
, &context
, NULL
);
322 CFMachPortSetInvalidationCallBack(storePrivate
->callbackPort
, rlsPortInvalidate
);
323 storePrivate
->callbackRunLoopSource
= CFMachPortCreateRunLoopSource(NULL
, storePrivate
->callbackPort
, 0);
326 if (storePrivate
->callbackRunLoopSource
) {
327 CFRunLoopAddSource(rl
, storePrivate
->callbackRunLoopSource
, mode
);
335 rlsCancel(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
337 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
338 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
340 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("cancel notifications for mode %@"), mode
);
342 if (storePrivate
->callbackRunLoopSource
) {
343 CFRunLoopRemoveSource(rl
, storePrivate
->callbackRunLoopSource
, mode
);
346 if (--storePrivate
->rlsRefs
== 0) {
348 kern_return_t status
;
350 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" cancel callback runloop source"));
352 if (storePrivate
->callbackRunLoopSource
) {
353 /* remove the run loop source */
354 CFRelease(storePrivate
->callbackRunLoopSource
);
355 storePrivate
->callbackRunLoopSource
= NULL
;
358 if (storePrivate
->callbackPort
) {
359 /* invalidate port */
360 CFMachPortInvalidate(storePrivate
->callbackPort
);
361 CFRelease(storePrivate
->callbackPort
);
362 storePrivate
->callbackPort
= NULL
;
365 if (storePrivate
->server
) {
366 status
= notifycancel(storePrivate
->server
, (int *)&sc_status
);
367 if (status
!= KERN_SUCCESS
) {
368 if (status
!= MACH_SEND_INVALID_DEST
)
369 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("notifycancel(): %s"), mach_error_string(status
));
370 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
371 storePrivate
->server
= MACH_PORT_NULL
;
381 rlsPerform(void *info
)
383 CFArrayRef changedKeys
;
385 void (*context_release
)(const void *);
386 SCDynamicStoreCallBack rlsFunction
;
387 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
388 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
390 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" executing notifiction function"));
392 changedKeys
= SCDynamicStoreCopyNotifiedKeys(store
);
394 /* something happened to the server */
398 rlsFunction
= storePrivate
->rlsFunction
;
400 if (NULL
!= storePrivate
->rlsContext
.retain
) {
401 context_info
= (void *)storePrivate
->rlsContext
.retain(storePrivate
->rlsContext
.info
);
402 context_release
= storePrivate
->rlsContext
.release
;
404 context_info
= storePrivate
->rlsContext
.info
;
405 context_release
= NULL
;
407 (*rlsFunction
)(store
, changedKeys
, context_info
);
408 if (context_release
) {
409 context_release(context_info
);
412 CFRelease(changedKeys
);
418 rlsRetain(CFTypeRef cf
)
420 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
421 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
423 if (storePrivate
->notifyStatus
!= Using_NotifierInformViaRunLoop
) {
424 /* mark RLS active */
425 storePrivate
->notifyStatus
= Using_NotifierInformViaRunLoop
;
426 /* keep a reference to the store */
435 rlsRelease(CFTypeRef cf
)
437 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
438 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
440 /* mark RLS inactive */
441 storePrivate
->notifyStatus
= NotifierNotRegistered
;
442 storePrivate
->rls
= NULL
;
444 /* release our reference to the store */
452 SCDynamicStoreCreateRunLoopSource(CFAllocatorRef allocator
,
453 SCDynamicStoreRef store
,
456 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
458 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreCreateRunLoopSource:"));
461 /* sorry, you must provide a session */
462 _SCErrorSet(kSCStatusNoStoreSession
);
466 if (storePrivate
->server
== MACH_PORT_NULL
) {
467 /* sorry, you must have an open session to play */
468 _SCErrorSet(kSCStatusNoStoreServer
);
472 switch (storePrivate
->notifyStatus
) {
473 case NotifierNotRegistered
:
474 case Using_NotifierInformViaRunLoop
:
475 /* OK to enable runloop notification */
478 /* sorry, you can only have one notification registered at once */
479 _SCErrorSet(kSCStatusNotifierActive
);
483 if (storePrivate
->rls
) {
484 CFRetain(storePrivate
->rls
);
486 CFRunLoopSourceContext context
= { 0 // version
487 , (void *)store
// info
488 , rlsRetain
// retain
489 , rlsRelease
// release
490 , CFCopyDescription
// copyDescription
493 , rlsSchedule
// schedule
494 , rlsCancel
// cancel
495 , rlsPerform
// perform
498 storePrivate
->rls
= CFRunLoopSourceCreate(allocator
, order
, &context
);
501 if (!storePrivate
->rls
) {
502 _SCErrorSet(kSCStatusFailed
);
506 return storePrivate
->rls
;