2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * March 24, 2000 Allan Nathanson <ajn@apple.com>
35 #include "configd_server.h"
38 /* information maintained for each active session */
39 static serverSessionRef
*sessions
= NULL
;
40 static int nSessions
= 0;
45 getSession(mach_port_t server
)
49 if (server
== MACH_PORT_NULL
) {
50 SCLog(TRUE
, LOG_NOTICE
, CFSTR("Excuse me, why is getSession() being called with an invalid port?"));
54 for (i
= 0; i
< nSessions
; i
++) {
55 serverSessionRef thisSession
= sessions
[i
];
57 if (thisSession
== NULL
) {
58 /* found an empty slot, skip it */
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
)) {
68 /* no sessions available */
75 addSession(CFMachPortRef server
)
81 /* new session (actually, the first) found */
82 sessions
= malloc(sizeof(serverSessionRef
));
86 for (i
= 0; i
< nSessions
; i
++) {
87 if (sessions
[i
] == NULL
) {
88 /* found an empty slot, use it */
92 /* new session identified */
94 /* no empty slots, add one to the list */
96 sessions
= reallocf(sessions
, ((nSessions
) * sizeof(serverSessionRef
)));
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 sessions
[n
]->callerEGID
= 1; /* not "wheel" */
115 removeSession(mach_port_t server
)
118 serverSessionRef thisSession
;
119 CFStringRef sessionKey
;
121 for (i
= 0; i
< nSessions
; i
++) {
122 thisSession
= sessions
[i
];
124 if (thisSession
== NULL
) {
125 /* found an empty slot, skip it */
127 } else if (thisSession
->key
== server
) {
129 * We don't need any remaining information in the
130 * sessionData dictionary, remove it.
132 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), server
);
133 CFDictionaryRemoveValue(sessionData
, sessionKey
);
134 CFRelease(sessionKey
);
137 * Lastly, get rid of the per-session structure.
152 cleanupSession(mach_port_t server
)
156 for (i
= 0; i
< nSessions
; i
++) {
157 serverSessionRef thisSession
= sessions
[i
];
159 if ((thisSession
!= NULL
) && (thisSession
->key
== server
)) {
161 * session entry still exists.
164 if (_configd_trace
) {
165 SCTrace(TRUE
, _configd_trace
, CFSTR("cleanup : %5d\n"), server
);
169 * Ensure that any changes made while we held the "lock"
172 if ((storeLocked
> 0) &&
173 ((SCDynamicStorePrivateRef
)thisSession
->store
)->locked
) {
175 * swap store and associated data which, after
176 * being closed, will result in the restoration
177 * of the original pre-"locked" data.
179 _swapLockedStoreData();
183 * Close any open connections including cancelling any outstanding
184 * notification requests and releasing any locks.
186 (void) __SCDynamicStoreClose(&thisSession
->store
, TRUE
);
189 * Lastly, remove the session entry.
191 removeSession(server
);
206 fprintf(stderr
, "Current sessions:");
207 for (i
= 0; i
< nSessions
; i
++) {
208 serverSessionRef thisSession
= sessions
[i
];
210 if (thisSession
== NULL
) {
214 fprintf(stderr
, " %d", thisSession
->key
);
216 if (thisSession
->store
) {
217 task_t task
= ((SCDynamicStorePrivateRef
)thisSession
->store
)->notifySignalTask
;
219 if (task
!= TASK_NULL
) {
220 fprintf(stderr
, "/%d", task
);
224 fprintf(stderr
, "\n");