2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
37 #include "configd_server.h"
40 /* information maintained for each active session */
41 static serverSessionRef
*sessions
= NULL
;
42 static int nSessions
= 0;
47 getSession(mach_port_t server
)
51 if (server
== MACH_PORT_NULL
) {
52 SCLog(_configd_verbose
, LOG_NOTICE
, CFSTR("Excuse me, why is getSession() being called with an invalid port?"));
56 for (i
= 0; i
< nSessions
; i
++) {
57 serverSessionRef thisSession
= sessions
[i
];
59 if (thisSession
== NULL
) {
60 /* found an empty slot, skip it */
62 } else if (thisSession
->key
== server
) {
63 return thisSession
; /* we've seen this server before */
64 } else if (thisSession
->store
&&
65 (((SCDynamicStorePrivateRef
)thisSession
->store
)->notifySignalTask
== server
)) {
70 /* no sessions available */
77 addSession(CFMachPortRef server
)
83 /* new session (actually, the first) found */
84 sessions
= malloc(sizeof(serverSessionRef
));
88 for (i
= 0; i
< nSessions
; i
++) {
89 if (sessions
[i
] == NULL
) {
90 /* found an empty slot, use it */
94 /* new session identified */
96 /* no empty slots, add one to the list */
98 sessions
= realloc(sessions
, ((nSessions
) * sizeof(serverSessionRef
)));
102 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Allocating new session for port %d"), CFMachPortGetPort(server
));
103 sessions
[n
] = malloc(sizeof(serverSession
));
104 sessions
[n
]->key
= CFMachPortGetPort(server
);
105 sessions
[n
]->serverPort
= server
;
106 sessions
[n
]->serverRunLoopSource
= NULL
;
107 sessions
[n
]->store
= NULL
;
108 sessions
[n
]->callerEUID
= 1; /* not "root" */
109 sessions
[n
]->callerEGID
= 1; /* not "wheel" */
117 removeSession(mach_port_t server
)
120 serverSessionRef thisSession
;
121 CFStringRef sessionKey
;
123 for (i
= 0; i
< nSessions
; i
++) {
124 thisSession
= sessions
[i
];
126 if (thisSession
== NULL
) {
127 /* found an empty slot, skip it */
129 } else if (thisSession
->key
== server
) {
131 * We don't need any remaining information in the
132 * sessionData dictionary, remove it.
134 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), server
);
135 CFDictionaryRemoveValue(sessionData
, sessionKey
);
136 CFRelease(sessionKey
);
139 * Lastly, get rid of the per-session structure.
154 cleanupSession(mach_port_t server
)
158 for (i
= 0; i
< nSessions
; i
++) {
159 serverSessionRef thisSession
= sessions
[i
];
161 if ((thisSession
!= NULL
) && (thisSession
->key
== server
)) {
163 * session entry still exists.
166 if (_configd_trace
) {
167 SCTrace(TRUE
, _configd_trace
, CFSTR("cleanup : %5d\n"), server
);
171 * Ensure that any changes made while we held the "lock"
174 if ((storeLocked
> 0) &&
175 ((SCDynamicStorePrivateRef
)thisSession
->store
)->locked
) {
177 * swap store and associated data which, after
178 * being closed, will result in the restoration
179 * of the original pre-"locked" data.
181 _swapLockedStoreData();
185 * Close any open connections including cancelling any outstanding
186 * notification requests and releasing any locks.
188 (void) __SCDynamicStoreClose(&thisSession
->store
, TRUE
);
191 * Lastly, remove the session entry.
193 removeSession(server
);
208 fprintf(stderr
, "Current sessions:");
209 for (i
= 0; i
< nSessions
; i
++) {
210 serverSessionRef thisSession
= sessions
[i
];
212 if (thisSession
== NULL
) {
216 fprintf(stderr
, " %d", thisSession
->key
);
218 if (thisSession
->store
) {
219 task_t task
= ((SCDynamicStorePrivateRef
)thisSession
->store
)->notifySignalTask
;
221 if (task
!= TASK_NULL
) {
222 fprintf(stderr
, "/%d", task
);
226 fprintf(stderr
, "\n");