xref: /onnv-gate/usr/src/cmd/dcs/sparc/sun4u/dcs_ses.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This file is a module that provides an interface to managing
31*0Sstevel@tonic-gate  * concurrent sessions executed in either a separate thread or a
32*0Sstevel@tonic-gate  * separate process. Threads are used only if the compile time flag
33*0Sstevel@tonic-gate  * DCS_MULTI_THREAD is set. Otherwise, a new process is forked for
34*0Sstevel@tonic-gate  * each session.
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * Multiple processes are used to enable full Internationalization
37*0Sstevel@tonic-gate  * support. This support requires that each session is able to set
38*0Sstevel@tonic-gate  * its own locale for use in reporting errors to the user. Currently,
39*0Sstevel@tonic-gate  * this is not possible using multiple threads because the locale
40*0Sstevel@tonic-gate  * can not be set for an individual thread. For this reason, multiple
41*0Sstevel@tonic-gate  * processes are supported until proper locale support is provided
42*0Sstevel@tonic-gate  * for multiple threads.
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * When Solaris supports a different locale in each thread, all
45*0Sstevel@tonic-gate  * code used to enable using multiple processes should be removed.
46*0Sstevel@tonic-gate  * To simplify this process, all references to DCS_MULTI_THREAD can
47*0Sstevel@tonic-gate  * be found in this file.
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #include <stdlib.h>
51*0Sstevel@tonic-gate #include <stdio.h>
52*0Sstevel@tonic-gate #include <unistd.h>
53*0Sstevel@tonic-gate #include <string.h>
54*0Sstevel@tonic-gate #include <errno.h>
55*0Sstevel@tonic-gate #include <signal.h>
56*0Sstevel@tonic-gate #include <syslog.h>
57*0Sstevel@tonic-gate #include <locale.h>
58*0Sstevel@tonic-gate #include <sys/socket.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
61*0Sstevel@tonic-gate #include <thread.h>
62*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
63*0Sstevel@tonic-gate #include <sys/types.h>
64*0Sstevel@tonic-gate #include <sys/wait.h>
65*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #include "dcs.h"
68*0Sstevel@tonic-gate #include "rdr_messages.h"
69*0Sstevel@tonic-gate #include "rdr_param_types.h"
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate #define	DCS_DEFAULT_LOCALE		"C"
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /* session allocation/deallocation functions */
76*0Sstevel@tonic-gate static int ses_alloc(void);
77*0Sstevel@tonic-gate static int ses_free(void);
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /* handler functions */
80*0Sstevel@tonic-gate static void *ses_handler(void *arg);
81*0Sstevel@tonic-gate #ifndef DCS_MULTI_THREAD
82*0Sstevel@tonic-gate static void exit_handler(int sig, siginfo_t *info, void *context);
83*0Sstevel@tonic-gate #endif /* !DCS_MULTI_THREAD */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate /* session accounting functions */
86*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
87*0Sstevel@tonic-gate static void ses_thr_exit(void);
88*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * Global structure that holds all relevant information
93*0Sstevel@tonic-gate  * about the current session. If multiple threads are
94*0Sstevel@tonic-gate  * used, the thread specific data mechanism is used. This
95*0Sstevel@tonic-gate  * requires a data key to access the thread's private
96*0Sstevel@tonic-gate  * session information.
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
99*0Sstevel@tonic-gate thread_key_t	ses_key;
100*0Sstevel@tonic-gate static int	thr_key_created = 0;
101*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
102*0Sstevel@tonic-gate session_t	*ses;
103*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate /*
107*0Sstevel@tonic-gate  * Information about the current number of active sessions.
108*0Sstevel@tonic-gate  * If multiple threads are used, synchronization objects
109*0Sstevel@tonic-gate  * are required.
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate static ulong_t sessions = 0;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
114*0Sstevel@tonic-gate static mutex_t	sessions_lock = DEFAULTMUTEX;
115*0Sstevel@tonic-gate static cond_t	sessions_cv   = DEFAULTCV;
116*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate /*
120*0Sstevel@tonic-gate  * ses_start:
121*0Sstevel@tonic-gate  *
122*0Sstevel@tonic-gate  * Start the session handler. If multiple threads are used, create a new
123*0Sstevel@tonic-gate  * thread that runs the ses_handler() function. If multiple processes
124*0Sstevel@tonic-gate  * are used, fork a new process and call ses_handler().
125*0Sstevel@tonic-gate  */
126*0Sstevel@tonic-gate int
127*0Sstevel@tonic-gate ses_start(int fd)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	int	thr_err;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	mutex_lock(&sessions_lock);
135*0Sstevel@tonic-gate 	sessions++;
136*0Sstevel@tonic-gate 	mutex_unlock(&sessions_lock);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	thr_err = thr_create(NULL, 0, ses_handler, (void *)fd,
139*0Sstevel@tonic-gate 	    THR_DETACHED | THR_NEW_LWP, NULL);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	return ((thr_err) ? -1 : 0);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	int 	pid;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	pid = fork();
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if (pid == -1) {
151*0Sstevel@tonic-gate 		(void) rdr_close(fd);
152*0Sstevel@tonic-gate 		return (-1);
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	/*
156*0Sstevel@tonic-gate 	 * Parent:
157*0Sstevel@tonic-gate 	 */
158*0Sstevel@tonic-gate 	if (pid) {
159*0Sstevel@tonic-gate 		/* close the child's fd */
160*0Sstevel@tonic-gate 		(void) close(fd);
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		sessions++;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		return (0);
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/*
168*0Sstevel@tonic-gate 	 * Child:
169*0Sstevel@tonic-gate 	 */
170*0Sstevel@tonic-gate 	ses_handler((void *)fd);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	/*
173*0Sstevel@tonic-gate 	 * Prevent return to parent's loop
174*0Sstevel@tonic-gate 	 */
175*0Sstevel@tonic-gate 	exit(0);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	/* NOTREACHED */
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate /*
184*0Sstevel@tonic-gate  * ses_close:
185*0Sstevel@tonic-gate  *
186*0Sstevel@tonic-gate  * Initiate the closure of a session by sending an RDR_SES_END message
187*0Sstevel@tonic-gate  * to the client. It does not attempt to close the network connection.
188*0Sstevel@tonic-gate  */
189*0Sstevel@tonic-gate int
190*0Sstevel@tonic-gate ses_close(int err_code)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	session_t	*sp;
193*0Sstevel@tonic-gate 	cfga_params_t	req_data;
194*0Sstevel@tonic-gate 	rdr_msg_hdr_t	req_hdr;
195*0Sstevel@tonic-gate 	int		snd_status;
196*0Sstevel@tonic-gate 	static char	*op_name = "session close";
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	/* get the current session information */
200*0Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
201*0Sstevel@tonic-gate 		ses_close(DCS_ERROR);
202*0Sstevel@tonic-gate 		return (-1);
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	/* check if already sent session end */
206*0Sstevel@tonic-gate 	if (sp->state == DCS_SES_END) {
207*0Sstevel@tonic-gate 		return (0);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	/* prepare header information */
211*0Sstevel@tonic-gate 	init_msg(&req_hdr);
212*0Sstevel@tonic-gate 	req_hdr.message_opcode = RDR_SES_END;
213*0Sstevel@tonic-gate 	req_hdr.data_type = RDR_REQUEST;
214*0Sstevel@tonic-gate 	req_hdr.status = err_code;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	/* no operation specific data */
217*0Sstevel@tonic-gate 	(void) memset(&req_data, 0, sizeof (req_data));
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	PRINT_MSG_DBG(DCS_SEND, &req_hdr);
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	/* send the message */
222*0Sstevel@tonic-gate 	snd_status = rdr_snd_msg(sp->fd, &req_hdr, &req_data, DCS_SND_TIMEOUT);
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	if (snd_status == RDR_ABORTED) {
225*0Sstevel@tonic-gate 		abort_handler();
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	if (snd_status != RDR_OK) {
229*0Sstevel@tonic-gate 		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	/*
233*0Sstevel@tonic-gate 	 * Setting the session state to DCS_SES_END will
234*0Sstevel@tonic-gate 	 * cause the session handler to terminate the
235*0Sstevel@tonic-gate 	 * network connection. This should happen whether
236*0Sstevel@tonic-gate 	 * or not the session end message that was just
237*0Sstevel@tonic-gate 	 * sent was received successfully.
238*0Sstevel@tonic-gate 	 */
239*0Sstevel@tonic-gate 	sp->state = DCS_SES_END;
240*0Sstevel@tonic-gate 	return (0);
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate  * ses_abort:
246*0Sstevel@tonic-gate  *
247*0Sstevel@tonic-gate  * Attempt to abort an active session. If multiple threads are used,
248*0Sstevel@tonic-gate  * the parameter represents a thread_t identifier. If multiple
249*0Sstevel@tonic-gate  * processes are used, the parameter represents a pid. In either
250*0Sstevel@tonic-gate  * case, use this identifier to send a SIGINT signal to the approprate
251*0Sstevel@tonic-gate  * session.
252*0Sstevel@tonic-gate  */
253*0Sstevel@tonic-gate int
254*0Sstevel@tonic-gate ses_abort(long ses_id)
255*0Sstevel@tonic-gate {
256*0Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "killing session %d", ses_id);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	if (thr_kill(ses_id, SIGINT) != 0) {
261*0Sstevel@tonic-gate 		/*
262*0Sstevel@tonic-gate 		 * If the thread cannot be found, we will assume
263*0Sstevel@tonic-gate 		 * that the session was able to exit normally. In
264*0Sstevel@tonic-gate 		 * this case, there is no error since the desired
265*0Sstevel@tonic-gate 		 * result has already been achieved.
266*0Sstevel@tonic-gate 		 */
267*0Sstevel@tonic-gate 		if (errno == ESRCH) {
268*0Sstevel@tonic-gate 			return (0);
269*0Sstevel@tonic-gate 		}
270*0Sstevel@tonic-gate 		return (-1);
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if (kill(ses_id, SIGINT) == -1) {
276*0Sstevel@tonic-gate 		/*
277*0Sstevel@tonic-gate 		 * If the process cannot be found, we will assume
278*0Sstevel@tonic-gate 		 * that the session was able to exit normally. In
279*0Sstevel@tonic-gate 		 * this case, there is no error since the desired
280*0Sstevel@tonic-gate 		 * result has already been achieved.
281*0Sstevel@tonic-gate 		 */
282*0Sstevel@tonic-gate 		if (errno == ESRCH) {
283*0Sstevel@tonic-gate 			return (0);
284*0Sstevel@tonic-gate 		}
285*0Sstevel@tonic-gate 		return (-1);
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	return (0);
291*0Sstevel@tonic-gate }
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate /*
295*0Sstevel@tonic-gate  * ses_abort_enable:
296*0Sstevel@tonic-gate  *
297*0Sstevel@tonic-gate  * Enter a mode where the current session can be aborted. This mode
298*0Sstevel@tonic-gate  * will persist until ses_abort_disable() is called.
299*0Sstevel@tonic-gate  *
300*0Sstevel@tonic-gate  * A signal handler for SIGINT must be installed prior to calling this
301*0Sstevel@tonic-gate  * function. If this is not the case, and multiple threads are used,
302*0Sstevel@tonic-gate  * the default handler for SIGINT will cause the entire process to
303*0Sstevel@tonic-gate  * exit, rather than just the current session. If multiple processes
304*0Sstevel@tonic-gate  * are used, the default handler for SIGINT will not affect the main
305*0Sstevel@tonic-gate  * process, but it will prevent both sides from gracefully closing
306*0Sstevel@tonic-gate  * the session.
307*0Sstevel@tonic-gate  */
308*0Sstevel@tonic-gate void
309*0Sstevel@tonic-gate ses_abort_enable(void)
310*0Sstevel@tonic-gate {
311*0Sstevel@tonic-gate 	sigset_t	unblock_set;
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	/* unblock SIGINT */
315*0Sstevel@tonic-gate 	sigemptyset(&unblock_set);
316*0Sstevel@tonic-gate 	sigaddset(&unblock_set, SIGINT);
317*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &unblock_set, NULL);
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate /*
322*0Sstevel@tonic-gate  * ses_abort_disable:
323*0Sstevel@tonic-gate  *
324*0Sstevel@tonic-gate  * Exit the mode where the current session can be aborted. This
325*0Sstevel@tonic-gate  * will leave the mode entered by ses_abort_enable().
326*0Sstevel@tonic-gate  */
327*0Sstevel@tonic-gate void
328*0Sstevel@tonic-gate ses_abort_disable(void)
329*0Sstevel@tonic-gate {
330*0Sstevel@tonic-gate 	sigset_t	block_set;
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	/* block SIGINT */
334*0Sstevel@tonic-gate 	sigemptyset(&block_set);
335*0Sstevel@tonic-gate 	sigaddset(&block_set, SIGINT);
336*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_set, NULL);
337*0Sstevel@tonic-gate }
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate /*
341*0Sstevel@tonic-gate  * ses_setlocale:
342*0Sstevel@tonic-gate  *
343*0Sstevel@tonic-gate  * Set the locale for the current session. Currently, if multiple threads
344*0Sstevel@tonic-gate  * are used, the 'C' locale is specified for all cases. Once there is support
345*0Sstevel@tonic-gate  * for setting a thread specific locale, the requested locale will be used.
346*0Sstevel@tonic-gate  * If multiple processes are used, an attempt is made to set the locale of
347*0Sstevel@tonic-gate  * the process to the locale passed in as a parameter.
348*0Sstevel@tonic-gate  */
349*0Sstevel@tonic-gate int
350*0Sstevel@tonic-gate ses_setlocale(char *locale)
351*0Sstevel@tonic-gate {
352*0Sstevel@tonic-gate 	char	*new_locale;
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	/* sanity check */
355*0Sstevel@tonic-gate 	if (locale == NULL) {
356*0Sstevel@tonic-gate 		locale = DCS_DEFAULT_LOCALE;
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	/*
362*0Sstevel@tonic-gate 	 * Reserved for setting the locale on a per thread
363*0Sstevel@tonic-gate 	 * basis. Currently there is no Solaris support for
364*0Sstevel@tonic-gate 	 * this, so use the default locale.
365*0Sstevel@tonic-gate 	 */
366*0Sstevel@tonic-gate 	new_locale = setlocale(LC_ALL, DCS_DEFAULT_LOCALE);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	new_locale = setlocale(LC_ALL, locale);
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	if ((new_locale == NULL) || (strcmp(new_locale, locale) != 0)) {
375*0Sstevel@tonic-gate 		/* silently fall back to C locale */
376*0Sstevel@tonic-gate 		new_locale = setlocale(LC_ALL, DCS_DEFAULT_LOCALE);
377*0Sstevel@tonic-gate 	}
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "using '%s' locale", new_locale);
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	return (0);
382*0Sstevel@tonic-gate }
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate /*
386*0Sstevel@tonic-gate  * ses_init_signals:
387*0Sstevel@tonic-gate  *
388*0Sstevel@tonic-gate  * Initialize the set of signals to be blocked. It is assumed that the
389*0Sstevel@tonic-gate  * mask parameter initially contains all signals. If multiple threads
390*0Sstevel@tonic-gate  * are used, this is the correct behavior and the mask is not altered.
391*0Sstevel@tonic-gate  * If multiple processes are used, session accounting is performed in
392*0Sstevel@tonic-gate  * a SIGCHLD handler and so SIGCHLD must not be blocked. The action of
393*0Sstevel@tonic-gate  * initializing this handler is also performed in this function.
394*0Sstevel@tonic-gate  */
395*0Sstevel@tonic-gate /* ARGSUSED */
396*0Sstevel@tonic-gate void
397*0Sstevel@tonic-gate ses_init_signals(sigset_t *mask)
398*0Sstevel@tonic-gate {
399*0Sstevel@tonic-gate #ifndef DCS_MULTI_THREAD
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	struct sigaction	act;
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	/* unblock SIGCHLD */
405*0Sstevel@tonic-gate 	(void) sigdelset(mask, SIGCHLD);
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	/*
408*0Sstevel@tonic-gate 	 * Establish a handler for SIGCHLD
409*0Sstevel@tonic-gate 	 */
410*0Sstevel@tonic-gate 	(void) memset(&act, 0, sizeof (act));
411*0Sstevel@tonic-gate 	act.sa_sigaction = exit_handler;
412*0Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, NULL);
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate #endif /* !DCS_MULTI_THREAD */
417*0Sstevel@tonic-gate }
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate /*
421*0Sstevel@tonic-gate  * ses_sleep:
422*0Sstevel@tonic-gate  *
423*0Sstevel@tonic-gate  * Sleep for a specified amount of time, but don't prevent the
424*0Sstevel@tonic-gate  * session from being aborted.
425*0Sstevel@tonic-gate  */
426*0Sstevel@tonic-gate void
427*0Sstevel@tonic-gate ses_sleep(int sec)
428*0Sstevel@tonic-gate {
429*0Sstevel@tonic-gate 	ses_abort_enable();
430*0Sstevel@tonic-gate 	sleep(sec);
431*0Sstevel@tonic-gate 	ses_abort_disable();
432*0Sstevel@tonic-gate }
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate /*
436*0Sstevel@tonic-gate  * ses_wait:
437*0Sstevel@tonic-gate  *
438*0Sstevel@tonic-gate  * Wait for the number of active sessions to drop below the maximum
439*0Sstevel@tonic-gate  * allowed number of active sessions. If multiple threads are used,
440*0Sstevel@tonic-gate  * the thread waits on a condition variable until a child thread
441*0Sstevel@tonic-gate  * signals that it is going to exit. If multiple processes are used,
442*0Sstevel@tonic-gate  * the process waits until at least one child process exits.
443*0Sstevel@tonic-gate  */
444*0Sstevel@tonic-gate static void
445*0Sstevel@tonic-gate ses_wait(void)
446*0Sstevel@tonic-gate {
447*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	mutex_lock(&sessions_lock);
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 	while (sessions >= max_sessions) {
452*0Sstevel@tonic-gate 		cond_wait(&sessions_cv, &sessions_lock);
453*0Sstevel@tonic-gate 	}
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	mutex_unlock(&sessions_lock);
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	if (sessions >= max_sessions) {
460*0Sstevel@tonic-gate 		(void) wait(NULL);
461*0Sstevel@tonic-gate 	}
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
464*0Sstevel@tonic-gate }
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate /*
468*0Sstevel@tonic-gate  * ses_poll:
469*0Sstevel@tonic-gate  *
470*0Sstevel@tonic-gate  * Poll on the file descriptors passed in as a parameter. Before polling,
471*0Sstevel@tonic-gate  * a check is performed to see if the number of active sessions is less
472*0Sstevel@tonic-gate  * than the maximum number of active sessions allowed. If the limit for
473*0Sstevel@tonic-gate  * active sessions is reached, the poll will be delayed until at least
474*0Sstevel@tonic-gate  * one session exits.
475*0Sstevel@tonic-gate  */
476*0Sstevel@tonic-gate int
477*0Sstevel@tonic-gate ses_poll(struct pollfd fds[], nfds_t nfds, int timeout)
478*0Sstevel@tonic-gate {
479*0Sstevel@tonic-gate 	int	err;
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	ses_wait();
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 	err = poll(fds, nfds, timeout);
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	return (err);
487*0Sstevel@tonic-gate }
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate /*
491*0Sstevel@tonic-gate  * curr_ses:
492*0Sstevel@tonic-gate  *
493*0Sstevel@tonic-gate  * Return a pointer to the global session information. If multiple threads
494*0Sstevel@tonic-gate  * are being used, this will point to a thread specific instance of a
495*0Sstevel@tonic-gate  * session structure.
496*0Sstevel@tonic-gate  */
497*0Sstevel@tonic-gate session_t *
498*0Sstevel@tonic-gate curr_ses(void)
499*0Sstevel@tonic-gate {
500*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	session_t	*sp;
503*0Sstevel@tonic-gate 	int		thr_err;
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate 	thr_err = thr_getspecific(ses_key, (void **)&sp);
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 	return ((thr_err) ? NULL : sp);
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	return (ses);
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
515*0Sstevel@tonic-gate }
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate /*
519*0Sstevel@tonic-gate  * curr_ses_id:
520*0Sstevel@tonic-gate  *
521*0Sstevel@tonic-gate  * Return the session identifier. This is either the thread_t identifier
522*0Sstevel@tonic-gate  * of the thread, or the pid of the process.
523*0Sstevel@tonic-gate  */
524*0Sstevel@tonic-gate long
525*0Sstevel@tonic-gate curr_ses_id(void)
526*0Sstevel@tonic-gate {
527*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	return (thr_self());
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	return (getpid());
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
536*0Sstevel@tonic-gate }
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate /*
540*0Sstevel@tonic-gate  * ses_handler:
541*0Sstevel@tonic-gate  *
542*0Sstevel@tonic-gate  * Handle initialization and processing of a session. Initializes a session
543*0Sstevel@tonic-gate  * and enters a loop which waits for requests. When a request comes in, it
544*0Sstevel@tonic-gate  * is dispatched. When the session is terminated, the loop exits and the
545*0Sstevel@tonic-gate  * session is cleaned up.
546*0Sstevel@tonic-gate  */
547*0Sstevel@tonic-gate static void *
548*0Sstevel@tonic-gate ses_handler(void *arg)
549*0Sstevel@tonic-gate {
550*0Sstevel@tonic-gate 	session_t		*sp;
551*0Sstevel@tonic-gate 	rdr_msg_hdr_t		op_hdr;
552*0Sstevel@tonic-gate 	cfga_params_t		op_data;
553*0Sstevel@tonic-gate 	int			rcv_status;
554*0Sstevel@tonic-gate 	sigset_t		block_set;
555*0Sstevel@tonic-gate 	struct sigaction	act;
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	static char *dcs_state_str[] = {
558*0Sstevel@tonic-gate 		"unknown state",
559*0Sstevel@tonic-gate 		"DCS_CONNECTED",
560*0Sstevel@tonic-gate 		"DCS_SES_REQ",
561*0Sstevel@tonic-gate 		"DCS_SES_ESTBL",
562*0Sstevel@tonic-gate 		"DCS_CONF_PENDING",
563*0Sstevel@tonic-gate 		"DCS_CONF_DONE",
564*0Sstevel@tonic-gate 		"DCS_SES_END"
565*0Sstevel@tonic-gate 	};
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	if (ses_alloc() == -1) {
569*0Sstevel@tonic-gate 		(void) rdr_close((int)arg);
570*0Sstevel@tonic-gate 		return ((void *)-1);
571*0Sstevel@tonic-gate 	}
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
574*0Sstevel@tonic-gate 		ses_close(DCS_ERROR);
575*0Sstevel@tonic-gate 		return (NULL);
576*0Sstevel@tonic-gate 	}
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	/* initialize session information */
579*0Sstevel@tonic-gate 	memset(sp, 0, sizeof (session_t));
580*0Sstevel@tonic-gate 	sp->state = DCS_CONNECTED;
581*0Sstevel@tonic-gate 	sp->random_resp = lrand48();
582*0Sstevel@tonic-gate 	sp->fd = (int)arg;
583*0Sstevel@tonic-gate 	sp->id = curr_ses_id();
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	/* initially, block all signals and cancels */
586*0Sstevel@tonic-gate 	(void) sigfillset(&block_set);
587*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_set, NULL);
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	/* set the abort handler for this session */
590*0Sstevel@tonic-gate 	(void) memset(&act, 0, sizeof (act));
591*0Sstevel@tonic-gate 	act.sa_handler = abort_handler;
592*0Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "session handler starting...");
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	/*
597*0Sstevel@tonic-gate 	 * Process all requests in the session until the
598*0Sstevel@tonic-gate 	 * session is terminated
599*0Sstevel@tonic-gate 	 */
600*0Sstevel@tonic-gate 	for (;;) {
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 		DCS_DBG(DBG_STATE, "session state: %s",
603*0Sstevel@tonic-gate 		    dcs_state_str[sp->state]);
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 		if (sp->state == DCS_SES_END) {
606*0Sstevel@tonic-gate 			break;
607*0Sstevel@tonic-gate 		}
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 		(void) memset(&op_hdr, 0, sizeof (op_hdr));
610*0Sstevel@tonic-gate 		(void) memset(&op_data, 0, sizeof (op_data));
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 		rcv_status = rdr_rcv_msg(sp->fd, &op_hdr, &op_data,
613*0Sstevel@tonic-gate 		    DCS_RCV_TIMEOUT);
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 		if (rcv_status != RDR_OK) {
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 			switch (rcv_status) {
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 			case RDR_TIMEOUT:
620*0Sstevel@tonic-gate 				DCS_DBG(DBG_SES, "receive timed out");
621*0Sstevel@tonic-gate 				break;
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 			case RDR_DISCONNECT:
624*0Sstevel@tonic-gate 				dcs_log_msg(LOG_NOTICE, DCS_DISCONNECT);
625*0Sstevel@tonic-gate 				break;
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 			case RDR_ABORTED:
628*0Sstevel@tonic-gate 				dcs_log_msg(LOG_INFO, DCS_SES_ABORTED);
629*0Sstevel@tonic-gate 				break;
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 			case RDR_MSG_INVAL:
632*0Sstevel@tonic-gate 				/*
633*0Sstevel@tonic-gate 				 * Only log invalid messages if a session has
634*0Sstevel@tonic-gate 				 * already been established. Logging invalid
635*0Sstevel@tonic-gate 				 * session request messages could flood syslog.
636*0Sstevel@tonic-gate 				 */
637*0Sstevel@tonic-gate 				if (sp->state != DCS_CONNECTED) {
638*0Sstevel@tonic-gate 					dcs_log_msg(LOG_WARNING, DCS_MSG_INVAL);
639*0Sstevel@tonic-gate 				} else {
640*0Sstevel@tonic-gate 					DCS_DBG(DBG_SES, "received an invalid "
641*0Sstevel@tonic-gate 					    "message");
642*0Sstevel@tonic-gate 				}
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 				break;
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 			default:
647*0Sstevel@tonic-gate 				dcs_log_msg(LOG_ERR, DCS_RECEIVE_ERR);
648*0Sstevel@tonic-gate 				break;
649*0Sstevel@tonic-gate 			}
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 			/*
652*0Sstevel@tonic-gate 			 * We encountered an unrecoverable error,
653*0Sstevel@tonic-gate 			 * so exit this session handler.
654*0Sstevel@tonic-gate 			 */
655*0Sstevel@tonic-gate 			break;
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 		} else {
658*0Sstevel@tonic-gate 			/* handle the message */
659*0Sstevel@tonic-gate 			dcs_dispatch_message(&op_hdr, &op_data);
660*0Sstevel@tonic-gate 			rdr_cleanup_params(op_hdr.message_opcode, &op_data);
661*0Sstevel@tonic-gate 		}
662*0Sstevel@tonic-gate 	}
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "connection closed");
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 	/* clean up */
667*0Sstevel@tonic-gate 	(void) rdr_close(sp->fd);
668*0Sstevel@tonic-gate 	(void) ses_free();
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
671*0Sstevel@tonic-gate 	ses_thr_exit();
672*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate 	return (0);
675*0Sstevel@tonic-gate }
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate /*
679*0Sstevel@tonic-gate  * abort_handler:
680*0Sstevel@tonic-gate  *
681*0Sstevel@tonic-gate  * Handle a request to abort a session. This function should be installed
682*0Sstevel@tonic-gate  * as the signal handler for SIGINT. It sends a message to the client
683*0Sstevel@tonic-gate  * indicating that the session was aborted, and that the operation failed
684*0Sstevel@tonic-gate  * as a result. The session then terminates, and the thread or process
685*0Sstevel@tonic-gate  * handling the session exits.
686*0Sstevel@tonic-gate  */
687*0Sstevel@tonic-gate void
688*0Sstevel@tonic-gate abort_handler(void)
689*0Sstevel@tonic-gate {
690*0Sstevel@tonic-gate 	session_t	*sp;
691*0Sstevel@tonic-gate 	rdr_msg_hdr_t	op_hdr;
692*0Sstevel@tonic-gate 	cfga_params_t	op_data;
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 	/* get the current session information */
696*0Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
697*0Sstevel@tonic-gate 		ses_close(DCS_ERROR);
698*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
699*0Sstevel@tonic-gate 		ses_thr_exit();
700*0Sstevel@tonic-gate 		thr_exit(0);
701*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
702*0Sstevel@tonic-gate 		exit(0);
703*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
704*0Sstevel@tonic-gate 	}
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	DCS_DBG(DBG_MSG, "abort_handler()");
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	/* prepare header information */
709*0Sstevel@tonic-gate 	init_msg(&op_hdr);
710*0Sstevel@tonic-gate 	op_hdr.message_opcode = sp->curr_msg.hdr->message_opcode;
711*0Sstevel@tonic-gate 	op_hdr.data_type = RDR_REPLY;
712*0Sstevel@tonic-gate 	op_hdr.status = DCS_SES_ABORTED;
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 	/* no operation specific data */
715*0Sstevel@tonic-gate 	(void) memset(&op_data, 0, sizeof (op_data));
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	PRINT_MSG_DBG(DCS_SEND, &op_hdr);
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 	(void) rdr_snd_msg(sp->fd, &op_hdr, &op_data, DCS_SND_TIMEOUT);
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 	DCS_DBG(DBG_INFO, "abort_handler: connection closed");
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	/* clean up */
724*0Sstevel@tonic-gate 	rdr_cleanup_params(op_hdr.message_opcode, sp->curr_msg.params);
725*0Sstevel@tonic-gate 	(void) rdr_close(sp->fd);
726*0Sstevel@tonic-gate 	(void) ses_free();
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 	dcs_log_msg(LOG_INFO, DCS_SES_ABORTED);
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
731*0Sstevel@tonic-gate 	ses_thr_exit();
732*0Sstevel@tonic-gate 	thr_exit(0);
733*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
734*0Sstevel@tonic-gate 	exit(0);
735*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
736*0Sstevel@tonic-gate }
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate #ifndef DCS_MULTI_THREAD
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate /*
742*0Sstevel@tonic-gate  * exit_handler:
743*0Sstevel@tonic-gate  *
744*0Sstevel@tonic-gate  * If multiple processes are used, this function is used to record
745*0Sstevel@tonic-gate  * the fact that a child process has exited. In order to make sure
746*0Sstevel@tonic-gate  * that all zombie processes are released, a waitpid() is performed
747*0Sstevel@tonic-gate  * for the child that has exited.
748*0Sstevel@tonic-gate  */
749*0Sstevel@tonic-gate /* ARGSUSED */
750*0Sstevel@tonic-gate static void
751*0Sstevel@tonic-gate exit_handler(int sig, siginfo_t *info, void *context)
752*0Sstevel@tonic-gate {
753*0Sstevel@tonic-gate 	sessions--;
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate 	if (info != NULL) {
756*0Sstevel@tonic-gate 		(void) waitpid(info->si_pid, NULL, 0);
757*0Sstevel@tonic-gate 	}
758*0Sstevel@tonic-gate }
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate #endif /* !DCS_MULTI_THREAD */
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate /*
764*0Sstevel@tonic-gate  * ses_alloc:
765*0Sstevel@tonic-gate  *
766*0Sstevel@tonic-gate  * Allocate the memory required for the global session structure.
767*0Sstevel@tonic-gate  * If multiple threads are used, create a thread specific data
768*0Sstevel@tonic-gate  * key. This will only occur the first time that this function
769*0Sstevel@tonic-gate  * gets called.
770*0Sstevel@tonic-gate  */
771*0Sstevel@tonic-gate static int
772*0Sstevel@tonic-gate ses_alloc(void)
773*0Sstevel@tonic-gate {
774*0Sstevel@tonic-gate 	session_t	*sp;
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 	int		thr_err;
779*0Sstevel@tonic-gate 
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	if (!thr_key_created) {
782*0Sstevel@tonic-gate 		thr_err = thr_keycreate(&ses_key, NULL);
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate 		if (thr_err) {
785*0Sstevel@tonic-gate 			return (-1);
786*0Sstevel@tonic-gate 		}
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 		thr_key_created = 1;
789*0Sstevel@tonic-gate 	}
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "allocating session memory");
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate 	sp = (session_t *)malloc(sizeof (session_t));
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	if (!sp) {
798*0Sstevel@tonic-gate 		dcs_log_msg(LOG_ERR, DCS_INT_ERR, "malloc", strerror(errno));
799*0Sstevel@tonic-gate 		return (-1);
800*0Sstevel@tonic-gate 	}
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	thr_err = thr_setspecific(ses_key, sp);
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	return ((thr_err) ? -1 : 0);
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 	/* make the data global */
811*0Sstevel@tonic-gate 	ses = sp;
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	return (0);
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
816*0Sstevel@tonic-gate }
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate 
819*0Sstevel@tonic-gate /*
820*0Sstevel@tonic-gate  * ses_free:
821*0Sstevel@tonic-gate  *
822*0Sstevel@tonic-gate  * Deallocate the memory associated with the global session structure.
823*0Sstevel@tonic-gate  */
824*0Sstevel@tonic-gate static int
825*0Sstevel@tonic-gate ses_free(void)
826*0Sstevel@tonic-gate {
827*0Sstevel@tonic-gate 	session_t	*sp;
828*0Sstevel@tonic-gate 
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "freeing session memory");
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
833*0Sstevel@tonic-gate 		ses_close(DCS_ERROR);
834*0Sstevel@tonic-gate 		return (-1);
835*0Sstevel@tonic-gate 	}
836*0Sstevel@tonic-gate 
837*0Sstevel@tonic-gate 	if (sp) {
838*0Sstevel@tonic-gate 		(void) free((void *)sp);
839*0Sstevel@tonic-gate 	}
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate 	return (0);
842*0Sstevel@tonic-gate }
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate /*
848*0Sstevel@tonic-gate  * ses_thr_exit:
849*0Sstevel@tonic-gate  *
850*0Sstevel@tonic-gate  * If multiple threads are used, this function is used to record the
851*0Sstevel@tonic-gate  * fact that a child thread has exited. In addition, the condition
852*0Sstevel@tonic-gate  * variable is signaled so that the main thread can wakeup and begin
853*0Sstevel@tonic-gate  * accepting connections again.
854*0Sstevel@tonic-gate  */
855*0Sstevel@tonic-gate static void
856*0Sstevel@tonic-gate ses_thr_exit()
857*0Sstevel@tonic-gate {
858*0Sstevel@tonic-gate 	mutex_lock(&sessions_lock);
859*0Sstevel@tonic-gate 
860*0Sstevel@tonic-gate 	sessions--;
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 	cond_signal(&sessions_cv);
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 	mutex_unlock(&sessions_lock);
865*0Sstevel@tonic-gate }
866*0Sstevel@tonic-gate 
867*0Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
868