2 * Copyright (c) 2000-2003 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 */
54 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" notifier port closed, disabling notifier"));
55 } else if (cbFunc
== NULL
) {
56 /* there is no (longer) a callback function, disable additional callbacks */
57 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" no callback function, disabling notifier"));
59 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" executing notifiction function"));
60 if ((*cbFunc
)(store
, cbArg
)) {
62 * callback function returned success.
66 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" callback returned error, disabling notifier"));
71 if (port
!= storePrivate
->callbackPort
) {
72 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("informCallback, why is port != callbackPort?"));
76 /* remove the run loop source */
77 CFRunLoopRemoveSource(storePrivate
->callbackRunLoop
,
78 storePrivate
->callbackRunLoopSource
,
79 kCFRunLoopDefaultMode
);
80 CFRelease(storePrivate
->callbackRunLoopSource
);
81 storePrivate
->callbackRunLoop
= NULL
;
82 storePrivate
->callbackRunLoopSource
= NULL
;
85 CFMachPortInvalidate(storePrivate
->callbackPort
);
86 CFRelease(storePrivate
->callbackPort
);
87 storePrivate
->callbackPort
= NULL
;
89 /* disable notifier */
90 storePrivate
->notifyStatus
= NotifierNotRegistered
;
91 storePrivate
->callbackArgument
= NULL
;
92 storePrivate
->callbackFunction
= NULL
;
99 SCDynamicStoreNotifyCallback(SCDynamicStoreRef store
,
100 CFRunLoopRef runLoop
,
101 SCDynamicStoreCallBack_v1 func
,
104 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
105 kern_return_t status
;
107 mach_port_t oldNotify
;
109 CFMachPortContext context
= { 0
116 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreNotifyCallback:"));
119 /* sorry, you must provide a session */
120 _SCErrorSet(kSCStatusNoStoreSession
);
124 if (storePrivate
->server
== MACH_PORT_NULL
) {
125 /* sorry, you must have an open session to play */
126 _SCErrorSet(kSCStatusNoStoreServer
);
130 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
131 /* sorry, you can only have one notification registered at once */
132 _SCErrorSet(kSCStatusNotifierActive
);
136 /* Allocating port (for server response) */
137 storePrivate
->callbackPort
= CFMachPortCreate(NULL
,
142 /* Request a notification when/if the server dies */
143 port
= CFMachPortGetPort(storePrivate
->callbackPort
);
144 status
= mach_port_request_notification(mach_task_self(),
146 MACH_NOTIFY_NO_SENDERS
,
149 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
151 if (status
!= KERN_SUCCESS
) {
152 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
153 CFMachPortInvalidate(storePrivate
->callbackPort
);
154 CFRelease(storePrivate
->callbackPort
);
159 if (oldNotify
!= MACH_PORT_NULL
) {
160 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCDynamicStoreNotifyCallback(): why is oldNotify != MACH_PORT_NULL?"));
163 /* Requesting notification via mach port */
164 status
= notifyviaport(storePrivate
->server
,
169 if (status
!= KERN_SUCCESS
) {
170 if (status
!= MACH_SEND_INVALID_DEST
)
171 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("notifyviaport(): %s"), mach_error_string(status
));
172 CFMachPortInvalidate(storePrivate
->callbackPort
);
173 CFRelease(storePrivate
->callbackPort
);
174 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
175 storePrivate
->server
= MACH_PORT_NULL
;
180 if (sc_status
!= kSCStatusOK
) {
181 _SCErrorSet(sc_status
);
185 /* set notifier active */
186 storePrivate
->notifyStatus
= Using_NotifierInformViaCallback
;
188 /* Creating/adding a run loop source for the port */
189 storePrivate
->callbackArgument
= arg
;
190 storePrivate
->callbackFunction
= func
;
191 storePrivate
->callbackRunLoop
= runLoop
;
192 storePrivate
->callbackRunLoopSource
=
193 CFMachPortCreateRunLoopSource(NULL
, storePrivate
->callbackPort
, 0);
195 CFRunLoopAddSource(storePrivate
->callbackRunLoop
,
196 storePrivate
->callbackRunLoopSource
,
197 kCFRunLoopDefaultMode
);
204 rlsCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
206 mach_msg_empty_rcv_t
*buf
= msg
;
207 mach_msg_id_t msgid
= buf
->header
.msgh_id
;
208 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
209 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
211 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
212 /* the server died, disable additional callbacks */
213 SCLog(_sc_verbose
, LOG_INFO
, CFSTR(" rlsCallback(), notifier port closed"));
216 if (port
!= storePrivate
->callbackPort
) {
217 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("rlsCallback(), why is port != callbackPort?"));
221 /* remove the run loop source(s) */
222 CFRunLoopSourceInvalidate(storePrivate
->callbackRunLoopSource
);
223 CFRelease(storePrivate
->callbackRunLoopSource
);
224 storePrivate
->callbackRunLoopSource
= NULL
;
226 /* invalidate port */
227 CFMachPortInvalidate(storePrivate
->callbackPort
);
228 CFRelease(storePrivate
->callbackPort
);
229 storePrivate
->callbackPort
= NULL
;
234 /* signal the real runloop source */
235 CFRunLoopSourceSignal(storePrivate
->rls
);
241 rlsPortInvalidate(CFMachPortRef mp
, void *info
) {
242 mach_port_t port
= CFMachPortGetPort(mp
);
244 // A simple deallocate won't get rid of all the references we've accumulated
245 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" invalidate = %d"), port
);
246 (void)mach_port_mod_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, -1);
251 rlsSchedule(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
253 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
254 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
256 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("schedule notifications for mode %@"), mode
);
258 if (storePrivate
->rlsRefs
++ == 0) {
259 CFMachPortContext context
= { 0
265 mach_port_t oldNotify
;
268 kern_return_t status
;
270 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" activate callback runloop source"));
272 /* Allocating port (for server response) */
273 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &port
);
274 if (status
!= KERN_SUCCESS
) {
275 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_allocate(): %s"), mach_error_string(status
));
278 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" port = %d"), port
);
280 status
= mach_port_insert_right(mach_task_self(),
283 MACH_MSG_TYPE_MAKE_SEND
);
284 if (status
!= KERN_SUCCESS
) {
285 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_insert_right(): %s"), mach_error_string(status
));
286 (void) mach_port_destroy(mach_task_self(), port
);
290 /* Request a notification when/if the server dies */
291 status
= mach_port_request_notification(mach_task_self(),
293 MACH_NOTIFY_NO_SENDERS
,
296 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
298 if (status
!= KERN_SUCCESS
) {
299 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
300 (void) mach_port_destroy(mach_task_self(), port
);
304 if (oldNotify
!= MACH_PORT_NULL
) {
305 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("rlsSchedule(): why is oldNotify != MACH_PORT_NULL?"));
308 status
= notifyviaport(storePrivate
->server
, port
, 0, (int *)&sc_status
);
309 if (status
!= KERN_SUCCESS
) {
310 if (status
!= MACH_SEND_INVALID_DEST
)
311 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("notifyviaport(): %s"), mach_error_string(status
));
312 (void) mach_port_destroy(mach_task_self(), port
);
313 port
= MACH_PORT_NULL
;
314 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
315 storePrivate
->server
= MACH_PORT_NULL
;
319 storePrivate
->callbackPort
= CFMachPortCreateWithPort(NULL
, port
, rlsCallback
, &context
, NULL
);
320 CFMachPortSetInvalidationCallBack(storePrivate
->callbackPort
, rlsPortInvalidate
);
321 storePrivate
->callbackRunLoopSource
= CFMachPortCreateRunLoopSource(NULL
, storePrivate
->callbackPort
, 0);
324 if (storePrivate
->callbackRunLoopSource
) {
325 CFRunLoopAddSource(rl
, storePrivate
->callbackRunLoopSource
, mode
);
333 rlsCancel(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
335 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
336 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
338 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("cancel notifications for mode %@"), mode
);
340 if (storePrivate
->callbackRunLoopSource
) {
341 CFRunLoopRemoveSource(rl
, storePrivate
->callbackRunLoopSource
, mode
);
344 if (--storePrivate
->rlsRefs
== 0) {
346 kern_return_t status
;
348 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" cancel callback runloop source"));
350 if (storePrivate
->callbackRunLoopSource
) {
351 /* remove the run loop source */
352 CFRelease(storePrivate
->callbackRunLoopSource
);
353 storePrivate
->callbackRunLoopSource
= NULL
;
356 if (storePrivate
->callbackPort
) {
357 /* invalidate port */
358 CFMachPortInvalidate(storePrivate
->callbackPort
);
359 CFRelease(storePrivate
->callbackPort
);
360 storePrivate
->callbackPort
= NULL
;
363 if (storePrivate
->server
) {
364 status
= notifycancel(storePrivate
->server
, (int *)&sc_status
);
365 if (status
!= KERN_SUCCESS
) {
366 if (status
!= MACH_SEND_INVALID_DEST
)
367 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("notifycancel(): %s"), mach_error_string(status
));
368 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
369 storePrivate
->server
= MACH_PORT_NULL
;
379 rlsPerform(void *info
)
381 CFArrayRef changedKeys
;
383 void (*context_release
)(const void *);
384 SCDynamicStoreCallBack rlsFunction
;
385 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
386 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
388 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" executing notifiction function"));
390 changedKeys
= SCDynamicStoreCopyNotifiedKeys(store
);
392 /* something happened to the server */
396 rlsFunction
= storePrivate
->rlsFunction
;
398 if (NULL
!= storePrivate
->rlsContext
.retain
) {
399 context_info
= (void *)storePrivate
->rlsContext
.retain(storePrivate
->rlsContext
.info
);
400 context_release
= storePrivate
->rlsContext
.release
;
402 context_info
= storePrivate
->rlsContext
.info
;
403 context_release
= NULL
;
405 (*rlsFunction
)(store
, changedKeys
, context_info
);
406 if (context_release
) {
407 context_release(context_info
);
410 CFRelease(changedKeys
);
416 rlsRetain(CFTypeRef cf
)
418 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
419 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
421 if (storePrivate
->notifyStatus
!= Using_NotifierInformViaRunLoop
) {
422 /* mark RLS active */
423 storePrivate
->notifyStatus
= Using_NotifierInformViaRunLoop
;
424 /* keep a reference to the store */
433 rlsRelease(CFTypeRef cf
)
435 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
436 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
438 /* mark RLS inactive */
439 storePrivate
->notifyStatus
= NotifierNotRegistered
;
440 storePrivate
->rls
= NULL
;
442 /* release our reference to the store */
450 SCDynamicStoreCreateRunLoopSource(CFAllocatorRef allocator
,
451 SCDynamicStoreRef store
,
454 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
456 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreCreateRunLoopSource:"));
459 /* sorry, you must provide a session */
460 _SCErrorSet(kSCStatusNoStoreSession
);
464 if (storePrivate
->server
== MACH_PORT_NULL
) {
465 /* sorry, you must have an open session to play */
466 _SCErrorSet(kSCStatusNoStoreServer
);
470 switch (storePrivate
->notifyStatus
) {
471 case NotifierNotRegistered
:
472 case Using_NotifierInformViaRunLoop
:
473 /* OK to enable runloop notification */
476 /* sorry, you can only have one notification registered at once */
477 _SCErrorSet(kSCStatusNotifierActive
);
481 if (storePrivate
->rls
) {
482 CFRetain(storePrivate
->rls
);
484 CFRunLoopSourceContext context
= { 0 // version
485 , (void *)store
// info
486 , rlsRetain
// retain
487 , rlsRelease
// release
488 , CFCopyDescription
// copyDescription
491 , rlsSchedule
// schedule
492 , rlsCancel
// cancel
493 , rlsPerform
// perform
496 storePrivate
->rls
= CFRunLoopSourceCreate(allocator
, order
, &context
);
499 if (!storePrivate
->rls
) {
500 _SCErrorSet(kSCStatusFailed
);
504 return storePrivate
->rls
;