2 * Copyright (c) 2000 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 24, 2000 Allan Nathanson <ajn@apple.com>
33 #include <mach/mach.h>
34 #include <mach/mach_error.h>
35 #include <servers/bootstrap.h>
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCPrivate.h>
39 #include "SCDynamicStoreInternal.h"
40 #include "config.h" /* MiG generated file */
43 __SCDynamicStoreCopyDescription(CFTypeRef cf
) {
44 CFAllocatorRef allocator
= CFGetAllocator(cf
);
45 CFMutableStringRef result
;
47 result
= CFStringCreateMutable(allocator
, 0);
48 CFStringAppendFormat(result
, NULL
, CFSTR("<SCDynamicStore %p [%p]> {\n"), cf
, allocator
);
49 CFStringAppendFormat(result
, NULL
, CFSTR("}"));
56 __SCDynamicStoreDeallocate(CFTypeRef cf
)
62 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
63 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
65 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreDeallocate:"));
67 (void) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED
, &oldThreadState
);
69 /* Remove notification keys */
70 if ((keyCnt
= CFSetGetCount(storePrivate
->keys
)) > 0) {
72 CFArrayRef keysToRemove
;
75 watchedKeys
= CFAllocatorAllocate(NULL
, keyCnt
* sizeof(CFStringRef
), 0);
76 CFSetGetValues(storePrivate
->keys
, watchedKeys
);
77 keysToRemove
= CFArrayCreate(NULL
, watchedKeys
, keyCnt
, &kCFTypeArrayCallBacks
);
78 CFAllocatorDeallocate(NULL
, watchedKeys
);
79 for (i
=0; i
<keyCnt
; i
++) {
80 (void) SCDynamicStoreRemoveWatchedKey(store
,
81 CFArrayGetValueAtIndex(keysToRemove
, i
),
84 CFRelease(keysToRemove
);
87 /* Remove regex notification keys */
88 if ((keyCnt
= CFSetGetCount(storePrivate
->reKeys
)) > 0) {
90 CFArrayRef keysToRemove
;
93 watchedKeys
= CFAllocatorAllocate(NULL
, keyCnt
* sizeof(CFStringRef
), 0);
94 CFSetGetValues(storePrivate
->reKeys
, watchedKeys
);
95 keysToRemove
= CFArrayCreate(NULL
, watchedKeys
, keyCnt
, &kCFTypeArrayCallBacks
);
96 CFAllocatorDeallocate(NULL
, watchedKeys
);
97 for (i
=0; i
<keyCnt
; i
++) {
98 (void) SCDynamicStoreRemoveWatchedKey(store
,
99 CFArrayGetValueAtIndex(keysToRemove
, i
),
102 CFRelease(keysToRemove
);
105 /* Remove/cancel any outstanding notification requests. */
106 (void) SCDynamicStoreNotifyCancel(store
);
108 if (storePrivate
->server
&& storePrivate
->locked
) {
109 (void) SCDynamicStoreUnlock(store
); /* release the lock */
112 if (storePrivate
->server
!= MACH_PORT_NULL
) {
113 status
= configclose(storePrivate
->server
, (int *)&sc_status
);
114 if (status
!= KERN_SUCCESS
) {
115 if (status
!= MACH_SEND_INVALID_DEST
)
116 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("configclose(): %s"), mach_error_string(status
));
119 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
120 storePrivate
->server
= MACH_PORT_NULL
;
123 (void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, &oldThreadState
);
124 pthread_testcancel();
126 /* release any callback context info */
127 if (storePrivate
->rlsContext
.release
) {
128 storePrivate
->rlsContext
.release(storePrivate
->rlsContext
.info
);
131 /* release any keys being watched */
132 CFRelease(storePrivate
->keys
);
133 CFRelease(storePrivate
->reKeys
);
139 static CFTypeID __kSCDynamicStoreTypeID
= _kCFRuntimeNotATypeID
;
142 static const CFRuntimeClass __SCDynamicStoreClass
= {
144 "SCDynamicStore", // className
147 __SCDynamicStoreDeallocate
, // dealloc
150 NULL
, // copyFormattingDesc
151 __SCDynamicStoreCopyDescription
// copyDebugDesc
155 static pthread_once_t initialized
= PTHREAD_ONCE_INIT
;
159 __SCDynamicStoreInitialize(void) {
160 __kSCDynamicStoreTypeID
= _CFRuntimeRegisterClass(&__SCDynamicStoreClass
);
166 __SCDynamicStoreCreatePrivate(CFAllocatorRef allocator
,
167 const CFStringRef name
,
168 SCDynamicStoreCallBack callout
,
169 SCDynamicStoreContext
*context
)
171 SCDynamicStorePrivateRef store
;
174 /* initialize runtime */
175 pthread_once(&initialized
, __SCDynamicStoreInitialize
);
177 /* allocate session */
178 size
= sizeof(SCDynamicStorePrivate
) - sizeof(CFRuntimeBase
);
179 store
= (SCDynamicStorePrivateRef
)_CFRuntimeCreateInstance(allocator
,
180 __kSCDynamicStoreTypeID
,
187 /* server side of the "configd" session */
188 store
->server
= MACH_PORT_NULL
;
191 store
->locked
= FALSE
;
193 /* SCDKeys being watched */
194 store
->keys
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
195 store
->reKeys
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
197 /* Notification status */
198 store
->notifyStatus
= NotifierNotRegistered
;
200 /* "client" information associated with SCDynamicStoreCreateRunLoopSource() */
203 store
->rlsFunction
= callout
;
204 store
->rlsContext
.info
= NULL
;
205 store
->rlsContext
.retain
= NULL
;
206 store
->rlsContext
.release
= NULL
;
207 store
->rlsContext
.copyDescription
= NULL
;
209 bcopy(context
, &store
->rlsContext
, sizeof(SCDynamicStoreContext
));
210 if (context
->retain
) {
211 store
->rlsContext
.info
= (void *)context
->retain(context
->info
);
215 /* "client" information associated with SCDynamicStoreNotifyCallback() */
216 store
->callbackFunction
= NULL
;
217 store
->callbackArgument
= NULL
;
218 store
->callbackPort
= NULL
;
219 store
->callbackRunLoop
= NULL
;
220 store
->callbackRunLoopSource
= NULL
;
222 /* "server" information associated with SCDynamicStoreNotifyMachPort(); */
223 store
->notifyPort
= MACH_PORT_NULL
;
224 store
->notifyPortIdentifier
= 0;
226 /* "server" information associated with SCDynamicStoreNotifyFileDescriptor(); */
227 store
->notifyFile
= -1;
228 store
->notifyFileIdentifier
= 0;
230 /* "server" information associated with SCDynamicStoreNotifySignal(); */
231 store
->notifySignal
= 0;
232 store
->notifySignalTask
= TASK_NULL
;
234 return (SCDynamicStoreRef
)store
;
239 SCDynamicStoreCreate(CFAllocatorRef allocator
,
241 SCDynamicStoreCallBack callout
,
242 SCDynamicStoreContext
*context
)
244 SCDynamicStoreRef store
;
245 SCDynamicStorePrivateRef storePrivate
;
246 kern_return_t status
;
247 mach_port_t bootstrap_port
;
249 CFDataRef xmlName
; /* serialized name */
254 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreCreate:"));
255 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" name = %@"), name
);
258 * allocate and initialize a new session
260 store
= __SCDynamicStoreCreatePrivate(allocator
, name
, callout
, context
);
261 storePrivate
= (SCDynamicStorePrivateRef
)store
;
263 status
= task_get_bootstrap_port(mach_task_self(), &bootstrap_port
);
264 if (status
!= KERN_SUCCESS
) {
265 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("task_get_bootstrap_port(): %s"), mach_error_string(status
));
271 status
= bootstrap_look_up(bootstrap_port
, SCD_SERVER
, &server
);
273 case BOOTSTRAP_SUCCESS
:
274 /* service currently registered, "a good thing" (tm) */
276 case BOOTSTRAP_UNKNOWN_SERVICE
:
277 /* service not currently registered, try again later */
284 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("bootstrap_status: %s"), mach_error_string(status
));
291 /* serialize the name */
292 xmlName
= CFPropertyListCreateXMLData(NULL
, name
);
293 myNameRef
= (xmlData_t
)CFDataGetBytePtr(xmlName
);
294 myNameLen
= CFDataGetLength(xmlName
);
296 /* open a new session with the server */
297 status
= configopen(server
, myNameRef
, myNameLen
, &storePrivate
->server
, (int *)&sc_status
);
302 if (status
!= KERN_SUCCESS
) {
303 if (status
!= MACH_SEND_INVALID_DEST
)
304 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("configopen(): %s"), mach_error_string(status
));
310 if (sc_status
!= kSCStatusOK
) {
312 _SCErrorSet(sc_status
);
316 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" server port = %d"), storePrivate
->server
);
322 SCDynamicStoreGetTypeID(void) {
323 return __kSCDynamicStoreTypeID
;