]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configopen.c
705b2f6eb339cab2bc6f0298d911a6732659255d
[apple/configd.git] / configd.tproj / _configopen.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include "configd.h"
37 #include "configd_server.h"
38 #include "session.h"
39
40 __private_extern__
41 int
42 __SCDynamicStoreOpen(SCDynamicStoreRef *store, CFStringRef name)
43 {
44 if (_configd_verbose) {
45 SCLog(TRUE, LOG_DEBUG, CFSTR("__SCDynamicStoreOpen:"));
46 SCLog(TRUE, LOG_DEBUG, CFSTR(" name = %@"), name);
47 }
48
49 /*
50 * allocate and initialize a new session
51 */
52 *store = (SCDynamicStoreRef)__SCDynamicStoreCreatePrivate(NULL, name, NULL, NULL);
53
54 /*
55 * If necessary, initialize the store and session data dictionaries
56 */
57 if (storeData == NULL) {
58 sessionData = CFDictionaryCreateMutable(NULL,
59 0,
60 &kCFTypeDictionaryKeyCallBacks,
61 &kCFTypeDictionaryValueCallBacks);
62 storeData = CFDictionaryCreateMutable(NULL,
63 0,
64 &kCFTypeDictionaryKeyCallBacks,
65 &kCFTypeDictionaryValueCallBacks);
66 patternData = CFDictionaryCreateMutable(NULL,
67 0,
68 &kCFTypeDictionaryKeyCallBacks,
69 &kCFTypeDictionaryValueCallBacks);
70 changedKeys = CFSetCreateMutable(NULL,
71 0,
72 &kCFTypeSetCallBacks);
73 deferredRemovals = CFSetCreateMutable(NULL,
74 0,
75 &kCFTypeSetCallBacks);
76 removedSessionKeys = CFSetCreateMutable(NULL,
77 0,
78 &kCFTypeSetCallBacks);
79 }
80
81 return kSCStatusOK;
82 }
83
84
85 __private_extern__
86 kern_return_t
87 _configopen(mach_port_t server,
88 xmlData_t nameRef, /* raw XML bytes */
89 mach_msg_type_number_t nameLen,
90 mach_port_t *newServer,
91 int *sc_status)
92 {
93 kern_return_t status;
94 serverSessionRef mySession, newSession;
95 CFStringRef name; /* name (un-serialized) */
96 mach_port_t oldNotify;
97 CFStringRef sessionKey;
98 CFDictionaryRef info;
99 CFMutableDictionaryRef newInfo;
100 CFMachPortRef mp;
101
102 if (_configd_verbose) {
103 SCLog(TRUE, LOG_DEBUG, CFSTR("Open new session."));
104 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
105 }
106
107 /* un-serialize the name */
108 if (!_SCUnserializeString(&name, NULL, (void *)nameRef, nameLen)) {
109 *sc_status = kSCStatusFailed;
110 return KERN_SUCCESS;
111 }
112
113 if (!isA_CFString(name)) {
114 CFRelease(name);
115 *sc_status = kSCStatusInvalidArgument;
116 return KERN_SUCCESS;
117 }
118
119 mySession = getSession(server);
120 if (mySession->store) {
121 CFRelease(name);
122 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" Sorry, this session is already open."));
123 *sc_status = kSCStatusFailed; /* you can't re-open an "open" session */
124 return KERN_SUCCESS;
125 }
126
127 /* Create the server port for this session */
128 mp = CFMachPortCreate(NULL, configdCallback, NULL, NULL);
129
130 /* return the newly allocated port to be used for this session */
131 *newServer = CFMachPortGetPort(mp);
132
133 /*
134 * establish the new session
135 */
136 newSession = addSession(mp);
137
138 /* Create and add a run loop source for the port */
139 newSession->serverRunLoopSource = CFMachPortCreateRunLoopSource(NULL, mp, 0);
140 CFRunLoopAddSource(CFRunLoopGetCurrent(),
141 newSession->serverRunLoopSource,
142 kCFRunLoopDefaultMode);
143
144 /*
145 * save the credentials associated with the caller.
146 */
147 newSession->callerEUID = mySession->callerEUID;
148 newSession->callerEGID = mySession->callerEGID;
149
150 if (_configd_trace) {
151 SCTrace(TRUE, _configd_trace, CFSTR("open : %5d : %@\n"), *newServer, name);
152 }
153
154 *sc_status = __SCDynamicStoreOpen(&newSession->store, name);
155
156 /*
157 * Make the server port accessible to the framework routines.
158 */
159 ((SCDynamicStorePrivateRef)newSession->store)->server = *newServer;
160
161 /* Request a notification when/if the client dies */
162 status = mach_port_request_notification(mach_task_self(),
163 *newServer,
164 MACH_NOTIFY_NO_SENDERS,
165 1,
166 *newServer,
167 MACH_MSG_TYPE_MAKE_SEND_ONCE,
168 &oldNotify);
169 if (status != KERN_SUCCESS) {
170 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status));
171 CFRelease(name);
172 cleanupSession(*newServer);
173 *newServer = MACH_PORT_NULL;
174 *sc_status = kSCStatusFailed;
175 return KERN_SUCCESS;
176 }
177
178 if (oldNotify != MACH_PORT_NULL) {
179 SCLog(_configd_verbose, LOG_ERR, CFSTR("_configopen(): why is oldNotify != MACH_PORT_NULL?"));
180 }
181
182 /*
183 * Save the name of the calling application / plug-in with the session data.
184 */
185 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), *newServer);
186 info = CFDictionaryGetValue(sessionData, sessionKey);
187 if (info) {
188 newInfo = CFDictionaryCreateMutableCopy(NULL, 0, info);
189 } else {
190 newInfo = CFDictionaryCreateMutable(NULL,
191 0,
192 &kCFTypeDictionaryKeyCallBacks,
193 &kCFTypeDictionaryValueCallBacks);
194 }
195 CFDictionarySetValue(newInfo, kSCDName, name);
196 CFRelease(name);
197 CFDictionarySetValue(sessionData, sessionKey, newInfo);
198 CFRelease(newInfo);
199 CFRelease(sessionKey);
200
201 return KERN_SUCCESS;
202 }