]> git.saurik.com Git - apple/configd.git/blame - configd.tproj/session.c
configd-137.3.tar.gz
[apple/configd.git] / configd.tproj / session.c
CommitLineData
5958d7c0 1/*
009ee3c6 2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
5958d7c0
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
009ee3c6 5 *
009ee3c6
A
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
5958d7c0
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
009ee3c6
A
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 *
5958d7c0
A
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
0fae82ee
A
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
5958d7c0
A
34#include "configd.h"
35#include "configd_server.h"
36#include "session.h"
37
38/* information maintained for each active session */
39static serverSessionRef *sessions = NULL;
40static int nSessions = 0;
41
42
009ee3c6 43__private_extern__
5958d7c0
A
44serverSessionRef
45getSession(mach_port_t server)
46{
47 int i;
48
49 if (server == MACH_PORT_NULL) {
dbf6a266 50 SCLog(TRUE, LOG_NOTICE, CFSTR("Excuse me, why is getSession() being called with an invalid port?"));
5958d7c0
A
51 return NULL;
52 }
53
009ee3c6
A
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;
5958d7c0
A
65 }
66 }
67
68 /* no sessions available */
69 return NULL;
70}
71
72
009ee3c6 73__private_extern__
5958d7c0
A
74serverSessionRef
75addSession(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 {
009ee3c6 86 for (i = 0; i < nSessions; i++) {
5958d7c0
A
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++;
dbf6a266 96 sessions = reallocf(sessions, ((nSessions) * sizeof(serverSessionRef)));
5958d7c0
A
97 }
98 }
99
dbf6a266 100 // allocate a new session for this server
5958d7c0
A
101 sessions[n] = malloc(sizeof(serverSession));
102 sessions[n]->key = CFMachPortGetPort(server);
103 sessions[n]->serverPort = server;
104 sessions[n]->serverRunLoopSource = NULL;
0fae82ee 105 sessions[n]->store = NULL;
5958d7c0
A
106 sessions[n]->callerEUID = 1; /* not "root" */
107 sessions[n]->callerEGID = 1; /* not "wheel" */
108
109 return sessions[n];
110}
111
112
009ee3c6 113__private_extern__
5958d7c0
A
114void
115removeSession(mach_port_t server)
116{
117 int i;
118 serverSessionRef thisSession;
119 CFStringRef sessionKey;
120
009ee3c6 121 for (i = 0; i < nSessions; i++) {
5958d7c0
A
122 thisSession = sessions[i];
123
124 if (thisSession == NULL) {
125 /* found an empty slot, skip it */
126 continue;
127 } else if (thisSession->key == server) {
128 /*
129 * We don't need any remaining information in the
130 * sessionData dictionary, remove it.
131 */
132 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), server);
133 CFDictionaryRemoveValue(sessionData, sessionKey);
134 CFRelease(sessionKey);
135
136 /*
137 * Lastly, get rid of the per-session structure.
138 */
139 free(thisSession);
140 sessions[i] = NULL;
141
142 return;
143 }
144 }
145
146 return;
147}
148
149
009ee3c6 150__private_extern__
5958d7c0
A
151void
152cleanupSession(mach_port_t server)
153{
154 int i;
155
009ee3c6 156 for (i = 0; i < nSessions; i++) {
5958d7c0
A
157 serverSessionRef thisSession = sessions[i];
158
159 if ((thisSession != NULL) && (thisSession->key == server)) {
160 /*
161 * session entry still exists.
162 */
163
009ee3c6
A
164 if (_configd_trace) {
165 SCTrace(TRUE, _configd_trace, CFSTR("cleanup : %5d\n"), server);
166 }
167
5958d7c0
A
168 /*
169 * Ensure that any changes made while we held the "lock"
170 * are discarded.
171 */
0fae82ee
A
172 if ((storeLocked > 0) &&
173 ((SCDynamicStorePrivateRef)thisSession->store)->locked) {
5958d7c0 174 /*
0fae82ee 175 * swap store and associated data which, after
5958d7c0
A
176 * being closed, will result in the restoration
177 * of the original pre-"locked" data.
178 */
0fae82ee 179 _swapLockedStoreData();
5958d7c0
A
180 }
181
182 /*
183 * Close any open connections including cancelling any outstanding
184 * notification requests and releasing any locks.
185 */
009ee3c6 186 (void) __SCDynamicStoreClose(&thisSession->store, TRUE);
5958d7c0
A
187
188 /*
189 * Lastly, remove the session entry.
190 */
191 removeSession(server);
192
193 return;
194 }
195 }
196 return;
197}
198
199
009ee3c6 200__private_extern__
5958d7c0
A
201void
202listSessions()
203{
204 int i;
205
206 fprintf(stderr, "Current sessions:");
009ee3c6 207 for (i = 0; i < nSessions; i++) {
5958d7c0
A
208 serverSessionRef thisSession = sessions[i];
209
210 if (thisSession == NULL) {
211 continue;
212 }
213
214 fprintf(stderr, " %d", thisSession->key);
215
0fae82ee
A
216 if (thisSession->store) {
217 task_t task = ((SCDynamicStorePrivateRef)thisSession->store)->notifySignalTask;
5958d7c0
A
218
219 if (task != TASK_NULL) {
220 fprintf(stderr, "/%d", task);
221 }
222 }
223 }
224 fprintf(stderr, "\n");
225}
226