]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000, 2001, 2003-2005 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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 | |
11 | * file. | |
12 | * | |
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. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | /* | |
25 | * Modification History | |
26 | * | |
27 | * June 1, 2001 Allan Nathanson <ajn@apple.com> | |
28 | * - public API conversion | |
29 | * | |
30 | * March 24, 2000 Allan Nathanson <ajn@apple.com> | |
31 | * - initial revision | |
32 | */ | |
33 | ||
34 | #include "configd.h" | |
35 | #include "configd_server.h" | |
36 | #include "session.h" | |
37 | ||
38 | /* information maintained for each active session */ | |
39 | static serverSessionRef *sessions = NULL; | |
40 | static int nSessions = 0; | |
41 | ||
42 | ||
43 | __private_extern__ | |
44 | serverSessionRef | |
45 | getSession(mach_port_t server) | |
46 | { | |
47 | int i; | |
48 | ||
49 | if (server == MACH_PORT_NULL) { | |
50 | SCLog(TRUE, LOG_NOTICE, CFSTR("Excuse me, why is getSession() being called with an invalid port?")); | |
51 | return NULL; | |
52 | } | |
53 | ||
54 | for (i = 0; i < nSessions; i++) { | |
55 | serverSessionRef thisSession = sessions[i]; | |
56 | ||
57 | if (thisSession == NULL) { | |
58 | /* found an empty slot, skip it */ | |
59 | continue; | |
60 | } else if (thisSession->key == server) { | |
61 | return thisSession; /* we've seen this server before */ | |
62 | } else if (thisSession->store && | |
63 | (((SCDynamicStorePrivateRef)thisSession->store)->notifySignalTask == server)) { | |
64 | return thisSession; | |
65 | } | |
66 | } | |
67 | ||
68 | /* no sessions available */ | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | ||
73 | __private_extern__ | |
74 | serverSessionRef | |
75 | addSession(CFMachPortRef server) | |
76 | { | |
77 | int i; | |
78 | int n = -1; | |
79 | ||
80 | if (nSessions <= 0) { | |
81 | /* new session (actually, the first) found */ | |
82 | sessions = malloc(sizeof(serverSessionRef)); | |
83 | n = 0; | |
84 | nSessions = 1; | |
85 | } else { | |
86 | for (i = 0; i < nSessions; i++) { | |
87 | if (sessions[i] == NULL) { | |
88 | /* found an empty slot, use it */ | |
89 | n = i; | |
90 | } | |
91 | } | |
92 | /* new session identified */ | |
93 | if (n < 0) { | |
94 | /* no empty slots, add one to the list */ | |
95 | n = nSessions++; | |
96 | sessions = reallocf(sessions, ((nSessions) * sizeof(serverSessionRef))); | |
97 | } | |
98 | } | |
99 | ||
100 | // allocate a new session for this server | |
101 | sessions[n] = malloc(sizeof(serverSession)); | |
102 | sessions[n]->key = CFMachPortGetPort(server); | |
103 | sessions[n]->serverPort = server; | |
104 | sessions[n]->serverRunLoopSource = NULL; | |
105 | sessions[n]->store = NULL; | |
106 | sessions[n]->callerEUID = 1; /* not "root" */ | |
107 | ||
108 | return sessions[n]; | |
109 | } | |
110 | ||
111 | ||
112 | __private_extern__ | |
113 | void | |
114 | removeSession(mach_port_t server) | |
115 | { | |
116 | int i; | |
117 | serverSessionRef thisSession; | |
118 | CFStringRef sessionKey; | |
119 | ||
120 | for (i = 0; i < nSessions; i++) { | |
121 | thisSession = sessions[i]; | |
122 | ||
123 | if (thisSession == NULL) { | |
124 | /* found an empty slot, skip it */ | |
125 | continue; | |
126 | } else if (thisSession->key == server) { | |
127 | /* | |
128 | * We don't need any remaining information in the | |
129 | * sessionData dictionary, remove it. | |
130 | */ | |
131 | sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), server); | |
132 | CFDictionaryRemoveValue(sessionData, sessionKey); | |
133 | CFRelease(sessionKey); | |
134 | ||
135 | /* | |
136 | * Lastly, get rid of the per-session structure. | |
137 | */ | |
138 | free(thisSession); | |
139 | sessions[i] = NULL; | |
140 | ||
141 | return; | |
142 | } | |
143 | } | |
144 | ||
145 | return; | |
146 | } | |
147 | ||
148 | ||
149 | __private_extern__ | |
150 | void | |
151 | cleanupSession(mach_port_t server) | |
152 | { | |
153 | int i; | |
154 | ||
155 | for (i = 0; i < nSessions; i++) { | |
156 | serverSessionRef thisSession = sessions[i]; | |
157 | ||
158 | if ((thisSession != NULL) && (thisSession->key == server)) { | |
159 | /* | |
160 | * session entry still exists. | |
161 | */ | |
162 | ||
163 | if (_configd_trace) { | |
164 | SCTrace(TRUE, _configd_trace, CFSTR("cleanup : %5d\n"), server); | |
165 | } | |
166 | ||
167 | /* | |
168 | * Ensure that any changes made while we held the "lock" | |
169 | * are discarded. | |
170 | */ | |
171 | if ((storeLocked > 0) && | |
172 | ((SCDynamicStorePrivateRef)thisSession->store)->locked) { | |
173 | /* | |
174 | * swap store and associated data which, after | |
175 | * being closed, will result in the restoration | |
176 | * of the original pre-"locked" data. | |
177 | */ | |
178 | _swapLockedStoreData(); | |
179 | } | |
180 | ||
181 | /* | |
182 | * Close any open connections including cancelling any outstanding | |
183 | * notification requests and releasing any locks. | |
184 | */ | |
185 | (void) __SCDynamicStoreClose(&thisSession->store, TRUE); | |
186 | ||
187 | /* | |
188 | * Lastly, remove the session entry. | |
189 | */ | |
190 | removeSession(server); | |
191 | ||
192 | return; | |
193 | } | |
194 | } | |
195 | return; | |
196 | } | |
197 | ||
198 | ||
199 | __private_extern__ | |
200 | void | |
201 | listSessions() | |
202 | { | |
203 | int i; | |
204 | ||
205 | fprintf(stderr, "Current sessions:"); | |
206 | for (i = 0; i < nSessions; i++) { | |
207 | serverSessionRef thisSession = sessions[i]; | |
208 | ||
209 | if (thisSession == NULL) { | |
210 | continue; | |
211 | } | |
212 | ||
213 | fprintf(stderr, " %d", thisSession->key); | |
214 | ||
215 | if (thisSession->store) { | |
216 | task_t task = ((SCDynamicStorePrivateRef)thisSession->store)->notifySignalTask; | |
217 | ||
218 | if (task != TASK_NULL) { | |
219 | fprintf(stderr, "/%d", task); | |
220 | } | |
221 | } | |
222 | } | |
223 | fprintf(stderr, "\n"); | |
224 | } | |
225 |