2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 #include "configd_server.h"
27 /* information maintained for each active session */
28 static serverSessionRef
*sessions
= NULL
;
29 static int nSessions
= 0;
33 getSession(mach_port_t server
)
37 if (server
== MACH_PORT_NULL
) {
38 SCDLog(LOG_NOTICE
, CFSTR("Excuse me, why is getSession() being called with an invalid port?"));
43 for (i
=0; i
<nSessions
; i
++) {
44 serverSessionRef thisSession
= sessions
[i
];
46 if (thisSession
== NULL
) {
47 /* found an empty slot, skip it */
49 } else if (thisSession
->key
== server
) {
50 return thisSession
; /* we've seen this server before */
51 } else if ((thisSession
->session
!= NULL
) &&
52 (((SCDSessionPrivateRef
)thisSession
->session
)->notifySignalTask
== server
)) {
58 /* no sessions available */
64 addSession(CFMachPortRef server
)
70 /* new session (actually, the first) found */
71 sessions
= malloc(sizeof(serverSessionRef
));
75 for (i
=0; i
<nSessions
; i
++) {
76 if (sessions
[i
] == NULL
) {
77 /* found an empty slot, use it */
81 /* new session identified */
83 /* no empty slots, add one to the list */
85 sessions
= realloc(sessions
, ((nSessions
) * sizeof(serverSessionRef
)));
89 SCDLog(LOG_DEBUG
, CFSTR("Allocating new session for port %d"), CFMachPortGetPort(server
));
90 sessions
[n
] = malloc(sizeof(serverSession
));
91 sessions
[n
]->key
= CFMachPortGetPort(server
);
92 sessions
[n
]->serverPort
= server
;
93 sessions
[n
]->serverRunLoopSource
= NULL
;
94 sessions
[n
]->session
= NULL
;
95 sessions
[n
]->callerEUID
= 1; /* not "root" */
96 sessions
[n
]->callerEGID
= 1; /* not "wheel" */
103 removeSession(mach_port_t server
)
106 serverSessionRef thisSession
;
107 CFStringRef sessionKey
;
109 for (i
=0; i
<nSessions
; i
++) {
110 thisSession
= sessions
[i
];
112 if (thisSession
== NULL
) {
113 /* found an empty slot, skip it */
115 } else if (thisSession
->key
== server
) {
117 * We don't need any remaining information in the
118 * sessionData dictionary, remove it.
120 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), server
);
121 CFDictionaryRemoveValue(sessionData
, sessionKey
);
122 CFRelease(sessionKey
);
125 * Lastly, get rid of the per-session structure.
139 cleanupSession(mach_port_t server
)
143 for (i
=0; i
<nSessions
; i
++) {
144 serverSessionRef thisSession
= sessions
[i
];
146 if ((thisSession
!= NULL
) && (thisSession
->key
== server
)) {
148 * session entry still exists.
152 * Ensure that any changes made while we held the "lock"
155 if (SCDOptionGet(NULL
, kSCDOptionIsLocked
) &&
156 SCDOptionGet(thisSession
->session
, kSCDOptionIsLocked
)) {
158 * swap cache and associated data which, after
159 * being closed, will result in the restoration
160 * of the original pre-"locked" data.
162 _swapLockedCacheData();
166 * Close any open connections including cancelling any outstanding
167 * notification requests and releasing any locks.
169 (void) _SCDClose(&thisSession
->session
);
172 * Lastly, remove the session entry.
174 removeSession(server
);
188 fprintf(stderr
, "Current sessions:");
189 for (i
=0; i
<nSessions
; i
++) {
190 serverSessionRef thisSession
= sessions
[i
];
192 if (thisSession
== NULL
) {
196 fprintf(stderr
, " %d", thisSession
->key
);
198 if (thisSession
->session
!= NULL
) {
199 task_t task
= ((SCDSessionPrivateRef
)thisSession
->session
)->notifySignalTask
;
201 if (task
!= TASK_NULL
) {
202 fprintf(stderr
, "/%d", task
);
206 fprintf(stderr
, "\n");