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>
34 #include "configd_server.h"
38 __SCDynamicStoreOpen(SCDynamicStoreRef
*store
, CFStringRef name
)
40 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreOpen:"));
41 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" name = %@"), name
);
44 * allocate and initialize a new session
46 *store
= __SCDynamicStoreCreatePrivate(NULL
, name
, NULL
, NULL
);
49 * If necessary, initialize the store and session data dictionaries
51 if (storeData
== NULL
) {
52 storeData
= CFDictionaryCreateMutable(NULL
,
54 &kCFTypeDictionaryKeyCallBacks
,
55 &kCFTypeDictionaryValueCallBacks
);
56 sessionData
= CFDictionaryCreateMutable(NULL
,
58 &kCFTypeDictionaryKeyCallBacks
,
59 &kCFTypeDictionaryValueCallBacks
);
60 changedKeys
= CFSetCreateMutable(NULL
,
62 &kCFTypeSetCallBacks
);
63 deferredRemovals
= CFSetCreateMutable(NULL
,
65 &kCFTypeSetCallBacks
);
66 removedSessionKeys
= CFSetCreateMutable(NULL
,
68 &kCFTypeSetCallBacks
);
76 _configopen(mach_port_t server
,
77 xmlData_t nameRef
, /* raw XML bytes */
78 mach_msg_type_number_t nameLen
,
79 mach_port_t
*newServer
,
83 serverSessionRef mySession
, newSession
;
84 CFDataRef xmlName
; /* name (XML serialized) */
85 CFStringRef name
; /* name (un-serialized) */
87 mach_port_t oldNotify
;
88 CFStringRef sessionKey
;
90 CFMutableDictionaryRef newInfo
;
93 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Open new session."));
94 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" server = %d"), server
);
96 /* un-serialize the name */
97 xmlName
= CFDataCreate(NULL
, nameRef
, nameLen
);
98 status
= vm_deallocate(mach_task_self(), (vm_address_t
)nameRef
, nameLen
);
99 if (status
!= KERN_SUCCESS
) {
101 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("vm_deallocate(): %s"), mach_error_string(status
));
102 /* non-fatal???, proceed */
104 name
= CFPropertyListCreateFromXMLData(NULL
,
106 kCFPropertyListImmutable
,
111 SCLog(_configd_verbose
, LOG_DEBUG
,
112 CFSTR("CFPropertyListCreateFromXMLData() name: %@"),
116 *sc_status
= kSCStatusFailed
;
118 } else if (!isA_CFString(name
)) {
120 *sc_status
= kSCStatusInvalidArgument
;
124 mySession
= getSession(server
);
125 if (mySession
->store
) {
127 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" Sorry, this session is already open."));
128 *sc_status
= kSCStatusFailed
; /* you can't re-open an "open" session */
132 /* Create the server port for this session */
133 mp
= CFMachPortCreate(NULL
, configdCallback
, NULL
, NULL
);
135 /* return the newly allocated port to be used for this session */
136 *newServer
= CFMachPortGetPort(mp
);
139 * establish the new session
141 newSession
= addSession(mp
);
143 /* Create and add a run loop source for the port */
144 newSession
->serverRunLoopSource
= CFMachPortCreateRunLoopSource(NULL
, mp
, 0);
145 CFRunLoopAddSource(CFRunLoopGetCurrent(),
146 newSession
->serverRunLoopSource
,
147 kCFRunLoopDefaultMode
);
150 * save the credentials associated with the caller.
152 newSession
->callerEUID
= mySession
->callerEUID
;
153 newSession
->callerEGID
= mySession
->callerEGID
;
155 *sc_status
= __SCDynamicStoreOpen(&newSession
->store
, name
);
158 * Make the server port accessible to the framework routines.
160 ((SCDynamicStorePrivateRef
)newSession
->store
)->server
= *newServer
;
162 /* Request a notification when/if the client dies */
163 status
= mach_port_request_notification(mach_task_self(),
165 MACH_NOTIFY_NO_SENDERS
,
168 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
170 if (status
!= KERN_SUCCESS
) {
171 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
173 cleanupSession(*newServer
);
174 *newServer
= MACH_PORT_NULL
;
175 *sc_status
= kSCStatusFailed
;
179 if (oldNotify
!= MACH_PORT_NULL
) {
180 SCLog(_configd_verbose
, LOG_ERR
, CFSTR("_configopen(): why is oldNotify != MACH_PORT_NULL?"));
184 * Save the name of the calling application / plug-in with the session data.
186 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), *newServer
);
187 info
= CFDictionaryGetValue(sessionData
, sessionKey
);
189 newInfo
= CFDictionaryCreateMutableCopy(NULL
, 0, info
);
191 newInfo
= CFDictionaryCreateMutable(NULL
,
193 &kCFTypeDictionaryKeyCallBacks
,
194 &kCFTypeDictionaryValueCallBacks
);
196 CFDictionarySetValue(newInfo
, kSCDName
, name
);
198 CFDictionarySetValue(sessionData
, sessionKey
, newInfo
);
200 CFRelease(sessionKey
);