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 #include "configd_server.h"
28 _SCDOpen(SCDSessionRef
*session
, CFStringRef name
)
30 SCDSessionPrivateRef sessionPrivate
;
32 SCDLog(LOG_DEBUG
, CFSTR("_SCDOpen:"));
33 SCDLog(LOG_DEBUG
, CFSTR(" name = %@"), name
);
36 * allocate and initialize a new session
38 sessionPrivate
= (SCDSessionPrivateRef
)_SCDSessionCreatePrivate();
39 *session
= (SCDSessionRef
)sessionPrivate
;
42 * If necessary, initialize the cache and session data dictionaries
44 if (cacheData
== NULL
) {
45 cacheData
= CFDictionaryCreateMutable(NULL
,
47 &kCFTypeDictionaryKeyCallBacks
,
48 &kCFTypeDictionaryValueCallBacks
);
49 sessionData
= CFDictionaryCreateMutable(NULL
,
51 &kCFTypeDictionaryKeyCallBacks
,
52 &kCFTypeDictionaryValueCallBacks
);
53 changedKeys
= CFSetCreateMutable(NULL
,
55 &kCFTypeSetCallBacks
);
56 deferredRemovals
= CFSetCreateMutable(NULL
,
58 &kCFTypeSetCallBacks
);
59 removedSessionKeys
= CFSetCreateMutable(NULL
,
61 &kCFTypeSetCallBacks
);
69 _configopen(mach_port_t server
,
70 xmlData_t nameRef
, /* raw XML bytes */
71 mach_msg_type_number_t nameLen
,
72 mach_port_t
*newServer
,
76 serverSessionRef mySession
, newSession
;
77 CFDataRef xmlName
; /* name (XML serialized) */
78 CFStringRef name
; /* name (un-serialized) */
80 mach_port_t oldNotify
;
81 CFStringRef sessionKey
;
83 CFMutableDictionaryRef newInfo
;
86 SCDLog(LOG_DEBUG
, CFSTR("Open new session."));
87 SCDLog(LOG_DEBUG
, CFSTR(" server = %d"), server
);
89 /* un-serialize the name */
90 xmlName
= CFDataCreate(NULL
, nameRef
, nameLen
);
91 status
= vm_deallocate(mach_task_self(), (vm_address_t
)nameRef
, nameLen
);
92 if (status
!= KERN_SUCCESS
) {
94 SCDLog(LOG_DEBUG
, CFSTR("vm_deallocate(): %s"), mach_error_string(status
));
95 /* non-fatal???, proceed */
97 name
= CFPropertyListCreateFromXMLData(NULL
,
99 kCFPropertyListImmutable
,
103 SCDLog(LOG_DEBUG
, CFSTR("CFPropertyListCreateFromXMLData() name: %s"), xmlError
);
104 *scd_status
= SCD_FAILED
;
108 mySession
= getSession(server
);
109 if (mySession
->session
) {
111 SCDLog(LOG_DEBUG
, CFSTR(" Sorry, this session is already open."));
112 *scd_status
= SCD_FAILED
; /* you can't re-open an "open" session */
116 /* Create the server port for this session */
117 mp
= CFMachPortCreate(NULL
, configdCallback
, NULL
, NULL
);
119 /* return the newly allocated port to be used for this session */
120 *newServer
= CFMachPortGetPort(mp
);
123 * establish the new session
125 newSession
= addSession(mp
);
127 /* Create and add a run loop source for the port */
128 newSession
->serverRunLoopSource
= CFMachPortCreateRunLoopSource(NULL
, mp
, 0);
129 CFRunLoopAddSource(CFRunLoopGetCurrent(),
130 newSession
->serverRunLoopSource
,
131 kCFRunLoopDefaultMode
);
134 * save the credentials associated with the caller.
136 newSession
->callerEUID
= mySession
->callerEUID
;
137 newSession
->callerEGID
= mySession
->callerEGID
;
139 *scd_status
= _SCDOpen(&newSession
->session
, name
);
142 * Make the server port accessible to the framework routines.
144 ((SCDSessionPrivateRef
)newSession
->session
)->server
= *newServer
;
146 /* Request a notification when/if the client dies */
147 status
= mach_port_request_notification(mach_task_self(),
149 MACH_NOTIFY_NO_SENDERS
,
152 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
154 if (status
!= KERN_SUCCESS
) {
155 SCDLog(LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
157 cleanupSession(*newServer
);
158 *newServer
= MACH_PORT_NULL
;
159 *scd_status
= SCD_FAILED
;
164 if (oldNotify
!= MACH_PORT_NULL
) {
165 SCDLog(LOG_DEBUG
, CFSTR("_configopen(): why is oldNotify != MACH_PORT_NULL?"));
170 * Save the name of the calling application / plug-in with the session data.
172 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), *newServer
);
173 info
= CFDictionaryGetValue(sessionData
, sessionKey
);
175 newInfo
= CFDictionaryCreateMutableCopy(NULL
, 0, info
);
177 newInfo
= CFDictionaryCreateMutable(NULL
,
179 &kCFTypeDictionaryKeyCallBacks
,
180 &kCFTypeDictionaryValueCallBacks
);
182 CFDictionarySetValue(newInfo
, kSCDName
, name
);
184 CFDictionarySetValue(sessionData
, sessionKey
, newInfo
);
186 CFRelease(sessionKey
);