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 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 CFStringRef name
; /* name (un-serialized) */
85 mach_port_t oldNotify
;
86 CFStringRef sessionKey
;
88 CFMutableDictionaryRef newInfo
;
91 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Open new session."));
92 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" server = %d"), server
);
94 /* un-serialize the name */
95 if (!_SCUnserialize((CFPropertyListRef
*)&name
, (void *)nameRef
, nameLen
)) {
96 *sc_status
= kSCStatusFailed
;
100 if (!isA_CFString(name
)) {
102 *sc_status
= kSCStatusInvalidArgument
;
106 mySession
= getSession(server
);
107 if (mySession
->store
) {
109 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" Sorry, this session is already open."));
110 *sc_status
= kSCStatusFailed
; /* you can't re-open an "open" session */
114 /* Create the server port for this session */
115 mp
= CFMachPortCreate(NULL
, configdCallback
, NULL
, NULL
);
117 /* return the newly allocated port to be used for this session */
118 *newServer
= CFMachPortGetPort(mp
);
121 * establish the new session
123 newSession
= addSession(mp
);
125 /* Create and add a run loop source for the port */
126 newSession
->serverRunLoopSource
= CFMachPortCreateRunLoopSource(NULL
, mp
, 0);
127 CFRunLoopAddSource(CFRunLoopGetCurrent(),
128 newSession
->serverRunLoopSource
,
129 kCFRunLoopDefaultMode
);
132 * save the credentials associated with the caller.
134 newSession
->callerEUID
= mySession
->callerEUID
;
135 newSession
->callerEGID
= mySession
->callerEGID
;
137 *sc_status
= __SCDynamicStoreOpen(&newSession
->store
, name
);
140 * Make the server port accessible to the framework routines.
142 ((SCDynamicStorePrivateRef
)newSession
->store
)->server
= *newServer
;
144 /* Request a notification when/if the client dies */
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(_configd_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
155 cleanupSession(*newServer
);
156 *newServer
= MACH_PORT_NULL
;
157 *sc_status
= kSCStatusFailed
;
161 if (oldNotify
!= MACH_PORT_NULL
) {
162 SCLog(_configd_verbose
, LOG_ERR
, CFSTR("_configopen(): why is oldNotify != MACH_PORT_NULL?"));
166 * Save the name of the calling application / plug-in with the session data.
168 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), *newServer
);
169 info
= CFDictionaryGetValue(sessionData
, sessionKey
);
171 newInfo
= CFDictionaryCreateMutableCopy(NULL
, 0, info
);
173 newInfo
= CFDictionaryCreateMutable(NULL
,
175 &kCFTypeDictionaryKeyCallBacks
,
176 &kCFTypeDictionaryValueCallBacks
);
178 CFDictionarySetValue(newInfo
, kSCDName
, name
);
180 CFDictionarySetValue(sessionData
, sessionKey
, newInfo
);
182 CFRelease(sessionKey
);