2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * Modification History
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
29 * March 31, 2000 Allan Nathanson <ajn@apple.com>
33 #include <mach/mach.h>
34 #include <mach/mach_error.h>
36 #include <SystemConfiguration/SystemConfiguration.h>
37 #include <SystemConfiguration/SCPrivate.h>
38 #include "SCDynamicStoreInternal.h"
39 #include "config.h" /* MiG generated file */
41 #include "v1Compatibility.h"
44 informCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
46 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
47 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
48 mach_msg_empty_rcv_t
*buf
= msg
;
49 mach_msg_id_t msgid
= buf
->header
.msgh_id
;
50 SCDynamicStoreCallBack_v1 cbFunc
= storePrivate
->callbackFunction
;
51 void *cbArg
= storePrivate
->callbackArgument
;
53 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
54 /* the server died, disable additional callbacks */
55 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" notifier port closed, disabling notifier"));
56 } else if (cbFunc
== NULL
) {
57 /* there is no (longer) a callback function, disable additional callbacks */
58 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" no callback function, disabling notifier"));
60 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" executing notifiction function"));
61 if ((*cbFunc
)(store
, cbArg
)) {
63 * callback function returned success.
67 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" callback returned error, disabling notifier"));
72 if (port
!= storePrivate
->callbackPort
) {
73 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("informCallback, why is port != callbackPort?"));
77 /* remove the run loop source */
78 CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
79 storePrivate
->callbackRunLoopSource
,
80 kCFRunLoopDefaultMode
);
81 CFRelease(storePrivate
->callbackRunLoopSource
);
84 CFMachPortInvalidate(storePrivate
->callbackPort
);
85 CFRelease(storePrivate
->callbackPort
);
87 /* disable notifier */
88 storePrivate
->notifyStatus
= NotifierNotRegistered
;
89 storePrivate
->callbackArgument
= NULL
;
90 storePrivate
->callbackFunction
= NULL
;
91 storePrivate
->callbackPort
= NULL
;
92 storePrivate
->callbackRunLoop
= NULL
;
93 storePrivate
->callbackRunLoopSource
= NULL
;
100 SCDynamicStoreNotifyCallback(SCDynamicStoreRef store
,
101 CFRunLoopRef runLoop
,
102 SCDynamicStoreCallBack_v1 func
,
105 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
106 kern_return_t status
;
108 mach_port_t oldNotify
;
110 CFMachPortContext context
= { 0
117 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreNotifyCallback:"));
120 /* sorry, you must provide a session */
121 _SCErrorSet(kSCStatusNoStoreSession
);
125 if (storePrivate
->server
== MACH_PORT_NULL
) {
126 /* sorry, you must have an open session to play */
127 _SCErrorSet(kSCStatusNoStoreServer
);
131 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
132 /* sorry, you can only have one notification registered at once */
133 _SCErrorSet(kSCStatusNotifierActive
);
137 /* Allocating port (for server response) */
138 storePrivate
->callbackPort
= CFMachPortCreate(NULL
,
143 /* Request a notification when/if the server dies */
144 port
= CFMachPortGetPort(storePrivate
->callbackPort
);
145 status
= mach_port_request_notification(mach_task_self(),
147 MACH_NOTIFY_NO_SENDERS
,
150 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
152 if (status
!= KERN_SUCCESS
) {
153 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
154 CFMachPortInvalidate(storePrivate
->callbackPort
);
155 CFRelease(storePrivate
->callbackPort
);
160 if (oldNotify
!= MACH_PORT_NULL
) {
161 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCDynamicStoreNotifyCallback(): why is oldNotify != MACH_PORT_NULL?"));
164 /* Requesting notification via mach port */
165 status
= notifyviaport(storePrivate
->server
,
170 if (status
!= KERN_SUCCESS
) {
171 if (status
!= MACH_SEND_INVALID_DEST
)
172 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("notifyviaport(): %s"), mach_error_string(status
));
173 CFMachPortInvalidate(storePrivate
->callbackPort
);
174 CFRelease(storePrivate
->callbackPort
);
175 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
176 storePrivate
->server
= MACH_PORT_NULL
;
181 if (sc_status
!= kSCStatusOK
) {
182 _SCErrorSet(sc_status
);
186 /* set notifier active */
187 storePrivate
->notifyStatus
= Using_NotifierInformViaCallback
;
189 /* Creating/adding a run loop source for the port */
190 storePrivate
->callbackArgument
= arg
;
191 storePrivate
->callbackFunction
= func
;
192 storePrivate
->callbackRunLoop
= runLoop
;
193 storePrivate
->callbackRunLoopSource
=
194 CFMachPortCreateRunLoopSource(NULL
, storePrivate
->callbackPort
, 0);
196 CFRunLoopAddSource(storePrivate
->callbackRunLoop
,
197 storePrivate
->callbackRunLoopSource
,
198 kCFRunLoopDefaultMode
);
205 rlsCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
207 mach_msg_empty_rcv_t
*buf
= msg
;
208 mach_msg_id_t msgid
= buf
->header
.msgh_id
;
209 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
210 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
212 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
213 /* the server died, disable additional callbacks */
214 SCLog(_sc_verbose
, LOG_INFO
, CFSTR(" rlsCallback(), notifier port closed"));
217 if (port
!= storePrivate
->callbackPort
) {
218 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("rlsCallback(), why is port != callbackPort?"));
222 /* remove the run loop source(s) */
223 CFRunLoopSourceInvalidate(storePrivate
->callbackRunLoopSource
);
224 CFRelease(storePrivate
->callbackRunLoopSource
);
225 storePrivate
->callbackRunLoopSource
= NULL
;
227 /* invalidate port */
228 CFMachPortInvalidate(storePrivate
->callbackPort
);
229 CFRelease(storePrivate
->callbackPort
);
230 storePrivate
->callbackPort
= NULL
;
235 /* signal the real runloop source */
236 CFRunLoopSourceSignal(storePrivate
->rls
);
242 rlsPortInvalidate(CFMachPortRef mp
, void *info
) {
243 mach_port_t port
= CFMachPortGetPort(mp
);
245 // A simple deallocate won't get rid of all the references we've accumulated
246 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" invalidate = %d"), port
);
247 mach_port_destroy(mach_task_self(), port
);
252 rlsSchedule(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
254 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
255 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
257 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("schedule notifications for mode %@"), mode
);
259 if (storePrivate
->rlsRefs
++ == 0) {
260 CFMachPortContext context
= { 0
266 mach_port_t oldNotify
;
269 kern_return_t status
;
271 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" activate callback runloop source"));
273 /* Allocating port (for server response) */
274 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &port
);
275 if (status
!= KERN_SUCCESS
) {
276 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_allocate(): %s"), mach_error_string(status
));
279 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" port = %d"), port
);
281 status
= mach_port_insert_right(mach_task_self(),
284 MACH_MSG_TYPE_MAKE_SEND
);
285 if (status
!= KERN_SUCCESS
) {
286 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_insert_right(): %s"), mach_error_string(status
));
287 (void) mach_port_destroy(mach_task_self(), port
);
291 /* Request a notification when/if the server dies */
292 status
= mach_port_request_notification(mach_task_self(),
294 MACH_NOTIFY_NO_SENDERS
,
297 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
299 if (status
!= KERN_SUCCESS
) {
300 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
301 (void) mach_port_destroy(mach_task_self(), port
);
305 if (oldNotify
!= MACH_PORT_NULL
) {
306 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("rlsSchedule(): why is oldNotify != MACH_PORT_NULL?"));
309 status
= notifyviaport(storePrivate
->server
, port
, 0, (int *)&sc_status
);
310 if (status
!= KERN_SUCCESS
) {
311 if (status
!= MACH_SEND_INVALID_DEST
)
312 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("notifyviaport(): %s"), mach_error_string(status
));
313 (void) mach_port_destroy(mach_task_self(), port
);
314 port
= MACH_PORT_NULL
;
315 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
316 storePrivate
->server
= MACH_PORT_NULL
;
320 storePrivate
->callbackPort
= CFMachPortCreateWithPort(NULL
, port
, rlsCallback
, &context
, NULL
);
321 CFMachPortSetInvalidationCallBack(storePrivate
->callbackPort
, rlsPortInvalidate
);
322 storePrivate
->callbackRunLoopSource
= CFMachPortCreateRunLoopSource(NULL
, storePrivate
->callbackPort
, 0);
325 CFRunLoopAddSource(rl
, storePrivate
->callbackRunLoopSource
, mode
);
331 rlsCancel(void *info
, CFRunLoopRef rl
, CFStringRef mode
)
333 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
334 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
336 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("cancel notifications for mode %@"), mode
);
338 CFRunLoopRemoveSource(rl
, storePrivate
->callbackRunLoopSource
, mode
);
340 if (--storePrivate
->rlsRefs
== 0) {
342 kern_return_t status
;
344 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" cancel callback runloop source"));
346 /* remove the run loop source */
347 CFRelease(storePrivate
->callbackRunLoopSource
);
348 storePrivate
->callbackRunLoopSource
= NULL
;
350 /* invalidate port */
351 CFMachPortInvalidate(storePrivate
->callbackPort
);
352 CFRelease(storePrivate
->callbackPort
);
353 storePrivate
->callbackPort
= NULL
;
355 status
= notifycancel(storePrivate
->server
, (int *)&sc_status
);
356 if (status
!= KERN_SUCCESS
) {
357 if (status
!= MACH_SEND_INVALID_DEST
)
358 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("notifycancel(): %s"), mach_error_string(status
));
359 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
360 storePrivate
->server
= MACH_PORT_NULL
;
368 rlsPerform(void *info
)
370 CFArrayRef changedKeys
;
372 void (*context_release
)(const void *);
373 SCDynamicStoreCallBack rlsFunction
;
374 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
375 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
377 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" executing notifiction function"));
379 changedKeys
= SCDynamicStoreCopyNotifiedKeys(store
);
381 /* something happened to the server */
385 rlsFunction
= storePrivate
->rlsFunction
;
387 if (NULL
!= storePrivate
->rlsContext
.retain
) {
388 context_info
= (void *)storePrivate
->rlsContext
.retain(storePrivate
->rlsContext
.info
);
389 context_release
= storePrivate
->rlsContext
.release
;
391 context_info
= storePrivate
->rlsContext
.info
;
392 context_release
= NULL
;
394 (*rlsFunction
)(store
, changedKeys
, context_info
);
395 if (context_release
) {
396 context_release(context_info
);
399 CFRelease(changedKeys
);
405 rlsRetain(CFTypeRef cf
)
407 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
408 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
410 if (storePrivate
->notifyStatus
!= Using_NotifierInformViaRunLoop
) {
411 /* mark RLS active */
412 storePrivate
->notifyStatus
= Using_NotifierInformViaRunLoop
;
413 /* keep a reference to the store */
421 rlsRelease(CFTypeRef cf
)
423 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
424 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
426 /* mark RLS inactive */
427 storePrivate
->notifyStatus
= NotifierNotRegistered
;
428 storePrivate
->rls
= NULL
;
430 /* release our reference to the store */
438 SCDynamicStoreCreateRunLoopSource(CFAllocatorRef allocator
,
439 SCDynamicStoreRef store
,
442 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
444 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreCreateRunLoopSource:"));
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
) {
470 CFRetain(storePrivate
->rls
);
472 CFRunLoopSourceContext context
= { 0 // version
473 , (void *)store
// info
474 , rlsRetain
// retain
475 , rlsRelease
// release
476 , CFCopyDescription
// copyDescription
479 , rlsSchedule
// schedule
480 , rlsCancel
// cancel
481 , rlsPerform
// perform
484 storePrivate
->rls
= CFRunLoopSourceCreate(allocator
, order
, &context
);
487 if (!storePrivate
->rls
) {
488 _SCErrorSet(kSCStatusFailed
);
492 return storePrivate
->rls
;