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 24, 2000 Allan Nathanson <ajn@apple.com>
35 #include <mach/mach.h>
36 #include <mach/mach_error.h>
37 #include <servers/bootstrap.h>
39 #include <SystemConfiguration/SystemConfiguration.h>
40 #include <SystemConfiguration/SCPrivate.h>
41 #include "SCDynamicStoreInternal.h"
42 #include "config.h" /* MiG generated file */
45 __SCDynamicStoreCopyDescription(CFTypeRef cf
) {
46 CFAllocatorRef allocator
= CFGetAllocator(cf
);
47 CFMutableStringRef result
;
49 result
= CFStringCreateMutable(allocator
, 0);
50 CFStringAppendFormat(result
, NULL
, CFSTR("<SCDynamicStore %p [%p]> {\n"), cf
, allocator
);
51 CFStringAppendFormat(result
, NULL
, CFSTR("}"));
58 __SCDynamicStoreDeallocate(CFTypeRef cf
)
63 SCDynamicStoreRef store
= (SCDynamicStoreRef
)cf
;
64 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
66 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreDeallocate:"));
68 (void) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED
, &oldThreadState
);
70 /* Remove/cancel any outstanding notification requests. */
71 (void) SCDynamicStoreNotifyCancel(store
);
73 if (storePrivate
->server
&& storePrivate
->locked
) {
74 (void) SCDynamicStoreUnlock(store
); /* release the lock */
77 if (storePrivate
->server
!= MACH_PORT_NULL
) {
78 status
= configclose(storePrivate
->server
, (int *)&sc_status
);
79 if (status
!= KERN_SUCCESS
) {
80 if (status
!= MACH_SEND_INVALID_DEST
)
81 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("configclose(): %s"), mach_error_string(status
));
84 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
85 storePrivate
->server
= MACH_PORT_NULL
;
88 (void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, &oldThreadState
);
91 /* release any callback context info */
92 if (storePrivate
->rlsContext
.release
) {
93 storePrivate
->rlsContext
.release(storePrivate
->rlsContext
.info
);
96 /* release any keys being watched */
97 CFRelease(storePrivate
->keys
);
98 CFRelease(storePrivate
->patterns
);
104 static CFTypeID __kSCDynamicStoreTypeID
= _kCFRuntimeNotATypeID
;
107 static const CFRuntimeClass __SCDynamicStoreClass
= {
109 "SCDynamicStore", // className
112 __SCDynamicStoreDeallocate
, // dealloc
115 NULL
, // copyFormattingDesc
116 __SCDynamicStoreCopyDescription
// copyDebugDesc
120 static pthread_once_t initialized
= PTHREAD_ONCE_INIT
;
123 __SCDynamicStoreInitialize(void) {
124 __kSCDynamicStoreTypeID
= _CFRuntimeRegisterClass(&__SCDynamicStoreClass
);
129 SCDynamicStorePrivateRef
130 __SCDynamicStoreCreatePrivate(CFAllocatorRef allocator
,
131 const CFStringRef name
,
132 SCDynamicStoreCallBack callout
,
133 SCDynamicStoreContext
*context
)
136 SCDynamicStorePrivateRef storePrivate
;
138 /* initialize runtime */
139 pthread_once(&initialized
, __SCDynamicStoreInitialize
);
141 /* allocate session */
142 size
= sizeof(SCDynamicStorePrivate
) - sizeof(CFRuntimeBase
);
143 storePrivate
= (SCDynamicStorePrivateRef
)_CFRuntimeCreateInstance(allocator
,
144 __kSCDynamicStoreTypeID
,
151 /* server side of the "configd" session */
152 storePrivate
->server
= MACH_PORT_NULL
;
155 storePrivate
->locked
= FALSE
;
157 /* Notification status */
158 storePrivate
->notifyStatus
= NotifierNotRegistered
;
160 /* "client" information associated with SCDynamicStoreCreateRunLoopSource() */
161 storePrivate
->rlsRefs
= 0;
162 storePrivate
->rls
= NULL
;
163 storePrivate
->rlsFunction
= callout
;
164 storePrivate
->rlsContext
.info
= NULL
;
165 storePrivate
->rlsContext
.retain
= NULL
;
166 storePrivate
->rlsContext
.release
= NULL
;
167 storePrivate
->rlsContext
.copyDescription
= NULL
;
169 bcopy(context
, &storePrivate
->rlsContext
, sizeof(SCDynamicStoreContext
));
170 if (context
->retain
) {
171 storePrivate
->rlsContext
.info
= (void *)context
->retain(context
->info
);
175 /* "client" information associated with SCDynamicStoreNotifyCallback() */
176 storePrivate
->callbackFunction
= NULL
;
177 storePrivate
->callbackArgument
= NULL
;
178 storePrivate
->callbackPort
= NULL
;
179 storePrivate
->callbackRunLoop
= NULL
;
180 storePrivate
->callbackRunLoopSource
= NULL
;
182 /* "server" information associated with SCDynamicStoreSetNotificationKeys() */
183 storePrivate
->keys
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
184 storePrivate
->patterns
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
186 /* "server" information associated with SCDynamicStoreNotifyMachPort(); */
187 storePrivate
->notifyPort
= MACH_PORT_NULL
;
188 storePrivate
->notifyPortIdentifier
= 0;
190 /* "server" information associated with SCDynamicStoreNotifyFileDescriptor(); */
191 storePrivate
->notifyFile
= -1;
192 storePrivate
->notifyFileIdentifier
= 0;
194 /* "server" information associated with SCDynamicStoreNotifySignal(); */
195 storePrivate
->notifySignal
= 0;
196 storePrivate
->notifySignalTask
= TASK_NULL
;
203 SCDynamicStoreCreate(CFAllocatorRef allocator
,
205 SCDynamicStoreCallBack callout
,
206 SCDynamicStoreContext
*context
)
208 SCDynamicStorePrivateRef storePrivate
;
209 kern_return_t status
;
210 mach_port_t bootstrap_port
;
212 CFStringRef bundleID
= NULL
;
215 CFDataRef utfName
; /* serialized name */
221 SCLog(TRUE
, LOG_DEBUG
, CFSTR("SCDynamicStoreCreate:"));
222 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" name = %@"), name
);
226 * allocate and initialize a new session
228 storePrivate
= __SCDynamicStoreCreatePrivate(allocator
, name
, callout
, context
);
230 status
= task_get_bootstrap_port(mach_task_self(), &bootstrap_port
);
231 if (status
!= KERN_SUCCESS
) {
232 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("task_get_bootstrap_port(): %s"), mach_error_string(status
));
233 CFRelease(storePrivate
);
238 server_name
= getenv("SCD_SERVER");
240 server_name
= SCD_SERVER
;
243 status
= bootstrap_look_up(bootstrap_port
, server_name
, &server
);
245 case BOOTSTRAP_SUCCESS
:
246 /* service currently registered, "a good thing" (tm) */
248 case BOOTSTRAP_UNKNOWN_SERVICE
:
249 /* service not currently registered, try again later */
250 CFRelease(storePrivate
);
256 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("bootstrap_look_up() failed: status=%d"), status
);
258 CFRelease(storePrivate
);
263 /* serialize the name */
264 bundle
= CFBundleGetMainBundle();
266 bundleID
= CFBundleGetIdentifier(bundle
);
272 url
= CFBundleCopyExecutableURL(bundle
);
274 bundleID
= CFURLCopyPath(url
);
281 CFStringRef fullName
;
283 if (CFEqual(bundleID
, CFSTR("/"))) {
285 bundleID
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("(%d)"), getpid());
288 fullName
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@:%@"), bundleID
, name
);
295 if (!_SCSerializeString(name
, &utfName
, (void **)&myNameRef
, &myNameLen
)) {
297 _SCErrorSet(kSCStatusFailed
);
302 /* open a new session with the server */
303 status
= configopen(server
, myNameRef
, myNameLen
, &storePrivate
->server
, (int *)&sc_status
);
308 if (status
!= KERN_SUCCESS
) {
309 if (status
!= MACH_SEND_INVALID_DEST
)
310 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("configopen(): %s"), mach_error_string(status
));
311 CFRelease(storePrivate
);
316 if (sc_status
!= kSCStatusOK
) {
317 CFRelease(storePrivate
);
318 _SCErrorSet(sc_status
);
322 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" server port = %d"), storePrivate
->server
);
323 return (SCDynamicStoreRef
)storePrivate
;
328 SCDynamicStoreGetTypeID(void) {
329 pthread_once(&initialized
, __SCDynamicStoreInitialize
); /* initialize runtime */
330 return __kSCDynamicStoreTypeID
;