xref: /onnv-gate/usr/src/cmd/dcs/sparc/sun4u/dcs_ses.c (revision 3864:2ae506652d11)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3864Sraf  * Common Development and Distribution License (the "License").
6*3864Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*3864Sraf 
220Sstevel@tonic-gate /*
23*3864Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*3864Sraf  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * This file is a module that provides an interface to managing
310Sstevel@tonic-gate  * concurrent sessions executed in either a separate thread or a
320Sstevel@tonic-gate  * separate process. Threads are used only if the compile time flag
330Sstevel@tonic-gate  * DCS_MULTI_THREAD is set. Otherwise, a new process is forked for
340Sstevel@tonic-gate  * each session.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * Multiple processes are used to enable full Internationalization
370Sstevel@tonic-gate  * support. This support requires that each session is able to set
380Sstevel@tonic-gate  * its own locale for use in reporting errors to the user. Currently,
390Sstevel@tonic-gate  * this is not possible using multiple threads because the locale
400Sstevel@tonic-gate  * can not be set for an individual thread. For this reason, multiple
410Sstevel@tonic-gate  * processes are supported until proper locale support is provided
420Sstevel@tonic-gate  * for multiple threads.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * When Solaris supports a different locale in each thread, all
450Sstevel@tonic-gate  * code used to enable using multiple processes should be removed.
460Sstevel@tonic-gate  * To simplify this process, all references to DCS_MULTI_THREAD can
470Sstevel@tonic-gate  * be found in this file.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <stdlib.h>
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <unistd.h>
530Sstevel@tonic-gate #include <string.h>
540Sstevel@tonic-gate #include <errno.h>
550Sstevel@tonic-gate #include <signal.h>
560Sstevel@tonic-gate #include <syslog.h>
570Sstevel@tonic-gate #include <locale.h>
580Sstevel@tonic-gate #include <sys/socket.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
610Sstevel@tonic-gate #include <thread.h>
62*3864Sraf #include <pthread.h>
630Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
640Sstevel@tonic-gate #include <sys/types.h>
650Sstevel@tonic-gate #include <sys/wait.h>
660Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include "dcs.h"
690Sstevel@tonic-gate #include "rdr_messages.h"
700Sstevel@tonic-gate #include "rdr_param_types.h"
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #define	DCS_DEFAULT_LOCALE		"C"
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /* session allocation/deallocation functions */
770Sstevel@tonic-gate static int ses_alloc(void);
780Sstevel@tonic-gate static int ses_free(void);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /* handler functions */
810Sstevel@tonic-gate static void *ses_handler(void *arg);
820Sstevel@tonic-gate #ifndef DCS_MULTI_THREAD
830Sstevel@tonic-gate static void exit_handler(int sig, siginfo_t *info, void *context);
840Sstevel@tonic-gate #endif /* !DCS_MULTI_THREAD */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /* session accounting functions */
870Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
880Sstevel@tonic-gate static void ses_thr_exit(void);
890Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * Global structure that holds all relevant information
940Sstevel@tonic-gate  * about the current session. If multiple threads are
950Sstevel@tonic-gate  * used, the thread specific data mechanism is used. This
960Sstevel@tonic-gate  * requires a data key to access the thread's private
970Sstevel@tonic-gate  * session information.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
100*3864Sraf thread_key_t	ses_key = THR_ONCE_KEY;
1010Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
1020Sstevel@tonic-gate session_t	*ses;
1030Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * Information about the current number of active sessions.
1080Sstevel@tonic-gate  * If multiple threads are used, synchronization objects
1090Sstevel@tonic-gate  * are required.
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate static ulong_t sessions = 0;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
1140Sstevel@tonic-gate static mutex_t	sessions_lock = DEFAULTMUTEX;
1150Sstevel@tonic-gate static cond_t	sessions_cv   = DEFAULTCV;
1160Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * ses_start:
1210Sstevel@tonic-gate  *
1220Sstevel@tonic-gate  * Start the session handler. If multiple threads are used, create a new
1230Sstevel@tonic-gate  * thread that runs the ses_handler() function. If multiple processes
1240Sstevel@tonic-gate  * are used, fork a new process and call ses_handler().
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate int
ses_start(int fd)1270Sstevel@tonic-gate ses_start(int fd)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	int	thr_err;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	mutex_lock(&sessions_lock);
1350Sstevel@tonic-gate 	sessions++;
1360Sstevel@tonic-gate 	mutex_unlock(&sessions_lock);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	thr_err = thr_create(NULL, 0, ses_handler, (void *)fd,
1390Sstevel@tonic-gate 	    THR_DETACHED | THR_NEW_LWP, NULL);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	return ((thr_err) ? -1 : 0);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	int 	pid;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	pid = fork();
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (pid == -1) {
1510Sstevel@tonic-gate 		(void) rdr_close(fd);
1520Sstevel@tonic-gate 		return (-1);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/*
1560Sstevel@tonic-gate 	 * Parent:
1570Sstevel@tonic-gate 	 */
1580Sstevel@tonic-gate 	if (pid) {
1590Sstevel@tonic-gate 		/* close the child's fd */
1600Sstevel@tonic-gate 		(void) close(fd);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 		sessions++;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 		return (0);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * Child:
1690Sstevel@tonic-gate 	 */
1700Sstevel@tonic-gate 	ses_handler((void *)fd);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/*
1730Sstevel@tonic-gate 	 * Prevent return to parent's loop
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	exit(0);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/* NOTREACHED */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * ses_close:
1850Sstevel@tonic-gate  *
1860Sstevel@tonic-gate  * Initiate the closure of a session by sending an RDR_SES_END message
1870Sstevel@tonic-gate  * to the client. It does not attempt to close the network connection.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate int
ses_close(int err_code)1900Sstevel@tonic-gate ses_close(int err_code)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	session_t	*sp;
1930Sstevel@tonic-gate 	cfga_params_t	req_data;
1940Sstevel@tonic-gate 	rdr_msg_hdr_t	req_hdr;
1950Sstevel@tonic-gate 	int		snd_status;
1960Sstevel@tonic-gate 	static char	*op_name = "session close";
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/* get the current session information */
2000Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
2010Sstevel@tonic-gate 		ses_close(DCS_ERROR);
2020Sstevel@tonic-gate 		return (-1);
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/* check if already sent session end */
2060Sstevel@tonic-gate 	if (sp->state == DCS_SES_END) {
2070Sstevel@tonic-gate 		return (0);
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* prepare header information */
2110Sstevel@tonic-gate 	init_msg(&req_hdr);
2120Sstevel@tonic-gate 	req_hdr.message_opcode = RDR_SES_END;
2130Sstevel@tonic-gate 	req_hdr.data_type = RDR_REQUEST;
2140Sstevel@tonic-gate 	req_hdr.status = err_code;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/* no operation specific data */
2170Sstevel@tonic-gate 	(void) memset(&req_data, 0, sizeof (req_data));
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	PRINT_MSG_DBG(DCS_SEND, &req_hdr);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/* send the message */
2220Sstevel@tonic-gate 	snd_status = rdr_snd_msg(sp->fd, &req_hdr, &req_data, DCS_SND_TIMEOUT);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if (snd_status == RDR_ABORTED) {
2250Sstevel@tonic-gate 		abort_handler();
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (snd_status != RDR_OK) {
2290Sstevel@tonic-gate 		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/*
2330Sstevel@tonic-gate 	 * Setting the session state to DCS_SES_END will
2340Sstevel@tonic-gate 	 * cause the session handler to terminate the
2350Sstevel@tonic-gate 	 * network connection. This should happen whether
2360Sstevel@tonic-gate 	 * or not the session end message that was just
2370Sstevel@tonic-gate 	 * sent was received successfully.
2380Sstevel@tonic-gate 	 */
2390Sstevel@tonic-gate 	sp->state = DCS_SES_END;
2400Sstevel@tonic-gate 	return (0);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate  * ses_abort:
2460Sstevel@tonic-gate  *
2470Sstevel@tonic-gate  * Attempt to abort an active session. If multiple threads are used,
2480Sstevel@tonic-gate  * the parameter represents a thread_t identifier. If multiple
2490Sstevel@tonic-gate  * processes are used, the parameter represents a pid. In either
2500Sstevel@tonic-gate  * case, use this identifier to send a SIGINT signal to the approprate
2510Sstevel@tonic-gate  * session.
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate int
ses_abort(long ses_id)2540Sstevel@tonic-gate ses_abort(long ses_id)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "killing session %d", ses_id);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (thr_kill(ses_id, SIGINT) != 0) {
2610Sstevel@tonic-gate 		/*
2620Sstevel@tonic-gate 		 * If the thread cannot be found, we will assume
2630Sstevel@tonic-gate 		 * that the session was able to exit normally. In
2640Sstevel@tonic-gate 		 * this case, there is no error since the desired
2650Sstevel@tonic-gate 		 * result has already been achieved.
2660Sstevel@tonic-gate 		 */
2670Sstevel@tonic-gate 		if (errno == ESRCH) {
2680Sstevel@tonic-gate 			return (0);
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 		return (-1);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (kill(ses_id, SIGINT) == -1) {
2760Sstevel@tonic-gate 		/*
2770Sstevel@tonic-gate 		 * If the process cannot be found, we will assume
2780Sstevel@tonic-gate 		 * that the session was able to exit normally. In
2790Sstevel@tonic-gate 		 * this case, there is no error since the desired
2800Sstevel@tonic-gate 		 * result has already been achieved.
2810Sstevel@tonic-gate 		 */
2820Sstevel@tonic-gate 		if (errno == ESRCH) {
2830Sstevel@tonic-gate 			return (0);
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 		return (-1);
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	return (0);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * ses_abort_enable:
2960Sstevel@tonic-gate  *
2970Sstevel@tonic-gate  * Enter a mode where the current session can be aborted. This mode
2980Sstevel@tonic-gate  * will persist until ses_abort_disable() is called.
2990Sstevel@tonic-gate  *
3000Sstevel@tonic-gate  * A signal handler for SIGINT must be installed prior to calling this
3010Sstevel@tonic-gate  * function. If this is not the case, and multiple threads are used,
3020Sstevel@tonic-gate  * the default handler for SIGINT will cause the entire process to
3030Sstevel@tonic-gate  * exit, rather than just the current session. If multiple processes
3040Sstevel@tonic-gate  * are used, the default handler for SIGINT will not affect the main
3050Sstevel@tonic-gate  * process, but it will prevent both sides from gracefully closing
3060Sstevel@tonic-gate  * the session.
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate void
ses_abort_enable(void)3090Sstevel@tonic-gate ses_abort_enable(void)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	sigset_t	unblock_set;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/* unblock SIGINT */
3150Sstevel@tonic-gate 	sigemptyset(&unblock_set);
3160Sstevel@tonic-gate 	sigaddset(&unblock_set, SIGINT);
3170Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &unblock_set, NULL);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate  * ses_abort_disable:
3230Sstevel@tonic-gate  *
3240Sstevel@tonic-gate  * Exit the mode where the current session can be aborted. This
3250Sstevel@tonic-gate  * will leave the mode entered by ses_abort_enable().
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate void
ses_abort_disable(void)3280Sstevel@tonic-gate ses_abort_disable(void)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	sigset_t	block_set;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/* block SIGINT */
3340Sstevel@tonic-gate 	sigemptyset(&block_set);
3350Sstevel@tonic-gate 	sigaddset(&block_set, SIGINT);
3360Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_set, NULL);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate  * ses_setlocale:
3420Sstevel@tonic-gate  *
3430Sstevel@tonic-gate  * Set the locale for the current session. Currently, if multiple threads
3440Sstevel@tonic-gate  * are used, the 'C' locale is specified for all cases. Once there is support
3450Sstevel@tonic-gate  * for setting a thread specific locale, the requested locale will be used.
3460Sstevel@tonic-gate  * If multiple processes are used, an attempt is made to set the locale of
3470Sstevel@tonic-gate  * the process to the locale passed in as a parameter.
3480Sstevel@tonic-gate  */
3490Sstevel@tonic-gate int
ses_setlocale(char * locale)3500Sstevel@tonic-gate ses_setlocale(char *locale)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	char	*new_locale;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/* sanity check */
3550Sstevel@tonic-gate 	if (locale == NULL) {
3560Sstevel@tonic-gate 		locale = DCS_DEFAULT_LOCALE;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * Reserved for setting the locale on a per thread
3630Sstevel@tonic-gate 	 * basis. Currently there is no Solaris support for
3640Sstevel@tonic-gate 	 * this, so use the default locale.
3650Sstevel@tonic-gate 	 */
3660Sstevel@tonic-gate 	new_locale = setlocale(LC_ALL, DCS_DEFAULT_LOCALE);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	new_locale = setlocale(LC_ALL, locale);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if ((new_locale == NULL) || (strcmp(new_locale, locale) != 0)) {
3750Sstevel@tonic-gate 		/* silently fall back to C locale */
3760Sstevel@tonic-gate 		new_locale = setlocale(LC_ALL, DCS_DEFAULT_LOCALE);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "using '%s' locale", new_locale);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	return (0);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate  * ses_init_signals:
3870Sstevel@tonic-gate  *
3880Sstevel@tonic-gate  * Initialize the set of signals to be blocked. It is assumed that the
3890Sstevel@tonic-gate  * mask parameter initially contains all signals. If multiple threads
3900Sstevel@tonic-gate  * are used, this is the correct behavior and the mask is not altered.
3910Sstevel@tonic-gate  * If multiple processes are used, session accounting is performed in
3920Sstevel@tonic-gate  * a SIGCHLD handler and so SIGCHLD must not be blocked. The action of
3930Sstevel@tonic-gate  * initializing this handler is also performed in this function.
3940Sstevel@tonic-gate  */
3950Sstevel@tonic-gate /* ARGSUSED */
3960Sstevel@tonic-gate void
ses_init_signals(sigset_t * mask)3970Sstevel@tonic-gate ses_init_signals(sigset_t *mask)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate #ifndef DCS_MULTI_THREAD
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	struct sigaction	act;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	/* unblock SIGCHLD */
4050Sstevel@tonic-gate 	(void) sigdelset(mask, SIGCHLD);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/*
4080Sstevel@tonic-gate 	 * Establish a handler for SIGCHLD
4090Sstevel@tonic-gate 	 */
4100Sstevel@tonic-gate 	(void) memset(&act, 0, sizeof (act));
4110Sstevel@tonic-gate 	act.sa_sigaction = exit_handler;
4120Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, NULL);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate #endif /* !DCS_MULTI_THREAD */
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate  * ses_sleep:
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * Sleep for a specified amount of time, but don't prevent the
4240Sstevel@tonic-gate  * session from being aborted.
4250Sstevel@tonic-gate  */
4260Sstevel@tonic-gate void
ses_sleep(int sec)4270Sstevel@tonic-gate ses_sleep(int sec)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	ses_abort_enable();
4300Sstevel@tonic-gate 	sleep(sec);
4310Sstevel@tonic-gate 	ses_abort_disable();
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate  * ses_wait:
4370Sstevel@tonic-gate  *
4380Sstevel@tonic-gate  * Wait for the number of active sessions to drop below the maximum
4390Sstevel@tonic-gate  * allowed number of active sessions. If multiple threads are used,
4400Sstevel@tonic-gate  * the thread waits on a condition variable until a child thread
4410Sstevel@tonic-gate  * signals that it is going to exit. If multiple processes are used,
4420Sstevel@tonic-gate  * the process waits until at least one child process exits.
4430Sstevel@tonic-gate  */
4440Sstevel@tonic-gate static void
ses_wait(void)4450Sstevel@tonic-gate ses_wait(void)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	mutex_lock(&sessions_lock);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	while (sessions >= max_sessions) {
4520Sstevel@tonic-gate 		cond_wait(&sessions_cv, &sessions_lock);
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	mutex_unlock(&sessions_lock);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (sessions >= max_sessions) {
4600Sstevel@tonic-gate 		(void) wait(NULL);
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate  * ses_poll:
4690Sstevel@tonic-gate  *
4700Sstevel@tonic-gate  * Poll on the file descriptors passed in as a parameter. Before polling,
4710Sstevel@tonic-gate  * a check is performed to see if the number of active sessions is less
4720Sstevel@tonic-gate  * than the maximum number of active sessions allowed. If the limit for
4730Sstevel@tonic-gate  * active sessions is reached, the poll will be delayed until at least
4740Sstevel@tonic-gate  * one session exits.
4750Sstevel@tonic-gate  */
4760Sstevel@tonic-gate int
ses_poll(struct pollfd fds[],nfds_t nfds,int timeout)4770Sstevel@tonic-gate ses_poll(struct pollfd fds[], nfds_t nfds, int timeout)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate 	int	err;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	ses_wait();
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	err = poll(fds, nfds, timeout);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	return (err);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate /*
4910Sstevel@tonic-gate  * curr_ses:
4920Sstevel@tonic-gate  *
4930Sstevel@tonic-gate  * Return a pointer to the global session information. If multiple threads
4940Sstevel@tonic-gate  * are being used, this will point to a thread specific instance of a
4950Sstevel@tonic-gate  * session structure.
4960Sstevel@tonic-gate  */
4970Sstevel@tonic-gate session_t *
curr_ses(void)4980Sstevel@tonic-gate curr_ses(void)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
5010Sstevel@tonic-gate 
502*3864Sraf 	return (pthread_getspecific(ses_key));
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	return (ses);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate  * curr_ses_id:
5140Sstevel@tonic-gate  *
5150Sstevel@tonic-gate  * Return the session identifier. This is either the thread_t identifier
5160Sstevel@tonic-gate  * of the thread, or the pid of the process.
5170Sstevel@tonic-gate  */
5180Sstevel@tonic-gate long
curr_ses_id(void)5190Sstevel@tonic-gate curr_ses_id(void)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	return (thr_self());
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	return (getpid());
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  * ses_handler:
5350Sstevel@tonic-gate  *
5360Sstevel@tonic-gate  * Handle initialization and processing of a session. Initializes a session
5370Sstevel@tonic-gate  * and enters a loop which waits for requests. When a request comes in, it
5380Sstevel@tonic-gate  * is dispatched. When the session is terminated, the loop exits and the
5390Sstevel@tonic-gate  * session is cleaned up.
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate static void *
ses_handler(void * arg)5420Sstevel@tonic-gate ses_handler(void *arg)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	session_t		*sp;
5450Sstevel@tonic-gate 	rdr_msg_hdr_t		op_hdr;
5460Sstevel@tonic-gate 	cfga_params_t		op_data;
5470Sstevel@tonic-gate 	int			rcv_status;
5480Sstevel@tonic-gate 	sigset_t		block_set;
5490Sstevel@tonic-gate 	struct sigaction	act;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	static char *dcs_state_str[] = {
5520Sstevel@tonic-gate 		"unknown state",
5530Sstevel@tonic-gate 		"DCS_CONNECTED",
5540Sstevel@tonic-gate 		"DCS_SES_REQ",
5550Sstevel@tonic-gate 		"DCS_SES_ESTBL",
5560Sstevel@tonic-gate 		"DCS_CONF_PENDING",
5570Sstevel@tonic-gate 		"DCS_CONF_DONE",
5580Sstevel@tonic-gate 		"DCS_SES_END"
5590Sstevel@tonic-gate 	};
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	if (ses_alloc() == -1) {
5630Sstevel@tonic-gate 		(void) rdr_close((int)arg);
5640Sstevel@tonic-gate 		return ((void *)-1);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
5680Sstevel@tonic-gate 		ses_close(DCS_ERROR);
5690Sstevel@tonic-gate 		return (NULL);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/* initialize session information */
5730Sstevel@tonic-gate 	memset(sp, 0, sizeof (session_t));
5740Sstevel@tonic-gate 	sp->state = DCS_CONNECTED;
5750Sstevel@tonic-gate 	sp->random_resp = lrand48();
5760Sstevel@tonic-gate 	sp->fd = (int)arg;
5770Sstevel@tonic-gate 	sp->id = curr_ses_id();
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	/* initially, block all signals and cancels */
5800Sstevel@tonic-gate 	(void) sigfillset(&block_set);
5810Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_set, NULL);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/* set the abort handler for this session */
5840Sstevel@tonic-gate 	(void) memset(&act, 0, sizeof (act));
5850Sstevel@tonic-gate 	act.sa_handler = abort_handler;
5860Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "session handler starting...");
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/*
5910Sstevel@tonic-gate 	 * Process all requests in the session until the
5920Sstevel@tonic-gate 	 * session is terminated
5930Sstevel@tonic-gate 	 */
5940Sstevel@tonic-gate 	for (;;) {
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		DCS_DBG(DBG_STATE, "session state: %s",
5970Sstevel@tonic-gate 		    dcs_state_str[sp->state]);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		if (sp->state == DCS_SES_END) {
6000Sstevel@tonic-gate 			break;
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		(void) memset(&op_hdr, 0, sizeof (op_hdr));
6040Sstevel@tonic-gate 		(void) memset(&op_data, 0, sizeof (op_data));
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		rcv_status = rdr_rcv_msg(sp->fd, &op_hdr, &op_data,
6070Sstevel@tonic-gate 		    DCS_RCV_TIMEOUT);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 		if (rcv_status != RDR_OK) {
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 			switch (rcv_status) {
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 			case RDR_TIMEOUT:
6140Sstevel@tonic-gate 				DCS_DBG(DBG_SES, "receive timed out");
6150Sstevel@tonic-gate 				break;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 			case RDR_DISCONNECT:
6180Sstevel@tonic-gate 				dcs_log_msg(LOG_NOTICE, DCS_DISCONNECT);
6190Sstevel@tonic-gate 				break;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 			case RDR_ABORTED:
6220Sstevel@tonic-gate 				dcs_log_msg(LOG_INFO, DCS_SES_ABORTED);
6230Sstevel@tonic-gate 				break;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 			case RDR_MSG_INVAL:
6260Sstevel@tonic-gate 				/*
6270Sstevel@tonic-gate 				 * Only log invalid messages if a session has
6280Sstevel@tonic-gate 				 * already been established. Logging invalid
6290Sstevel@tonic-gate 				 * session request messages could flood syslog.
6300Sstevel@tonic-gate 				 */
6310Sstevel@tonic-gate 				if (sp->state != DCS_CONNECTED) {
6320Sstevel@tonic-gate 					dcs_log_msg(LOG_WARNING, DCS_MSG_INVAL);
6330Sstevel@tonic-gate 				} else {
6340Sstevel@tonic-gate 					DCS_DBG(DBG_SES, "received an invalid "
6350Sstevel@tonic-gate 					    "message");
6360Sstevel@tonic-gate 				}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 				break;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 			default:
6410Sstevel@tonic-gate 				dcs_log_msg(LOG_ERR, DCS_RECEIVE_ERR);
6420Sstevel@tonic-gate 				break;
6430Sstevel@tonic-gate 			}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 			/*
6460Sstevel@tonic-gate 			 * We encountered an unrecoverable error,
6470Sstevel@tonic-gate 			 * so exit this session handler.
6480Sstevel@tonic-gate 			 */
6490Sstevel@tonic-gate 			break;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		} else {
6520Sstevel@tonic-gate 			/* handle the message */
6530Sstevel@tonic-gate 			dcs_dispatch_message(&op_hdr, &op_data);
6540Sstevel@tonic-gate 			rdr_cleanup_params(op_hdr.message_opcode, &op_data);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "connection closed");
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/* clean up */
6610Sstevel@tonic-gate 	(void) rdr_close(sp->fd);
6620Sstevel@tonic-gate 	(void) ses_free();
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
6650Sstevel@tonic-gate 	ses_thr_exit();
6660Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	return (0);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate  * abort_handler:
6740Sstevel@tonic-gate  *
6750Sstevel@tonic-gate  * Handle a request to abort a session. This function should be installed
6760Sstevel@tonic-gate  * as the signal handler for SIGINT. It sends a message to the client
6770Sstevel@tonic-gate  * indicating that the session was aborted, and that the operation failed
6780Sstevel@tonic-gate  * as a result. The session then terminates, and the thread or process
6790Sstevel@tonic-gate  * handling the session exits.
6800Sstevel@tonic-gate  */
6810Sstevel@tonic-gate void
abort_handler(void)6820Sstevel@tonic-gate abort_handler(void)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	session_t	*sp;
6850Sstevel@tonic-gate 	rdr_msg_hdr_t	op_hdr;
6860Sstevel@tonic-gate 	cfga_params_t	op_data;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/* get the current session information */
6900Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
6910Sstevel@tonic-gate 		ses_close(DCS_ERROR);
6920Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
6930Sstevel@tonic-gate 		ses_thr_exit();
6940Sstevel@tonic-gate 		thr_exit(0);
6950Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
6960Sstevel@tonic-gate 		exit(0);
6970Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	DCS_DBG(DBG_MSG, "abort_handler()");
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/* prepare header information */
7030Sstevel@tonic-gate 	init_msg(&op_hdr);
7040Sstevel@tonic-gate 	op_hdr.message_opcode = sp->curr_msg.hdr->message_opcode;
7050Sstevel@tonic-gate 	op_hdr.data_type = RDR_REPLY;
7060Sstevel@tonic-gate 	op_hdr.status = DCS_SES_ABORTED;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/* no operation specific data */
7090Sstevel@tonic-gate 	(void) memset(&op_data, 0, sizeof (op_data));
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	PRINT_MSG_DBG(DCS_SEND, &op_hdr);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	(void) rdr_snd_msg(sp->fd, &op_hdr, &op_data, DCS_SND_TIMEOUT);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	DCS_DBG(DBG_INFO, "abort_handler: connection closed");
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/* clean up */
7180Sstevel@tonic-gate 	rdr_cleanup_params(op_hdr.message_opcode, sp->curr_msg.params);
7190Sstevel@tonic-gate 	(void) rdr_close(sp->fd);
7200Sstevel@tonic-gate 	(void) ses_free();
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	dcs_log_msg(LOG_INFO, DCS_SES_ABORTED);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
7250Sstevel@tonic-gate 	ses_thr_exit();
7260Sstevel@tonic-gate 	thr_exit(0);
7270Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
7280Sstevel@tonic-gate 	exit(0);
7290Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate #ifndef DCS_MULTI_THREAD
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate /*
7360Sstevel@tonic-gate  * exit_handler:
7370Sstevel@tonic-gate  *
7380Sstevel@tonic-gate  * If multiple processes are used, this function is used to record
7390Sstevel@tonic-gate  * the fact that a child process has exited. In order to make sure
7400Sstevel@tonic-gate  * that all zombie processes are released, a waitpid() is performed
7410Sstevel@tonic-gate  * for the child that has exited.
7420Sstevel@tonic-gate  */
7430Sstevel@tonic-gate /* ARGSUSED */
7440Sstevel@tonic-gate static void
exit_handler(int sig,siginfo_t * info,void * context)7450Sstevel@tonic-gate exit_handler(int sig, siginfo_t *info, void *context)
7460Sstevel@tonic-gate {
7470Sstevel@tonic-gate 	sessions--;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if (info != NULL) {
7500Sstevel@tonic-gate 		(void) waitpid(info->si_pid, NULL, 0);
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate #endif /* !DCS_MULTI_THREAD */
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate /*
7580Sstevel@tonic-gate  * ses_alloc:
7590Sstevel@tonic-gate  *
7600Sstevel@tonic-gate  * Allocate the memory required for the global session structure.
7610Sstevel@tonic-gate  * If multiple threads are used, create a thread specific data
7620Sstevel@tonic-gate  * key. This will only occur the first time that this function
7630Sstevel@tonic-gate  * gets called.
7640Sstevel@tonic-gate  */
7650Sstevel@tonic-gate static int
ses_alloc(void)7660Sstevel@tonic-gate ses_alloc(void)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	session_t	*sp;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	int		thr_err;
7730Sstevel@tonic-gate 
774*3864Sraf 	thr_err = thr_keycreate_once(&ses_key, NULL);
775*3864Sraf 	if (thr_err)
776*3864Sraf 		return (-1);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "allocating session memory");
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	sp = (session_t *)malloc(sizeof (session_t));
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if (!sp) {
7850Sstevel@tonic-gate 		dcs_log_msg(LOG_ERR, DCS_INT_ERR, "malloc", strerror(errno));
7860Sstevel@tonic-gate 		return (-1);
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	thr_err = thr_setspecific(ses_key, sp);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	return ((thr_err) ? -1 : 0);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	/* make the data global */
7980Sstevel@tonic-gate 	ses = sp;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	return (0);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate /*
8070Sstevel@tonic-gate  * ses_free:
8080Sstevel@tonic-gate  *
8090Sstevel@tonic-gate  * Deallocate the memory associated with the global session structure.
8100Sstevel@tonic-gate  */
8110Sstevel@tonic-gate static int
ses_free(void)8120Sstevel@tonic-gate ses_free(void)
8130Sstevel@tonic-gate {
8140Sstevel@tonic-gate 	session_t	*sp;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "freeing session memory");
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
8200Sstevel@tonic-gate 		ses_close(DCS_ERROR);
8210Sstevel@tonic-gate 		return (-1);
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if (sp) {
8250Sstevel@tonic-gate 		(void) free((void *)sp);
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	return (0);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate /*
8350Sstevel@tonic-gate  * ses_thr_exit:
8360Sstevel@tonic-gate  *
8370Sstevel@tonic-gate  * If multiple threads are used, this function is used to record the
8380Sstevel@tonic-gate  * fact that a child thread has exited. In addition, the condition
8390Sstevel@tonic-gate  * variable is signaled so that the main thread can wakeup and begin
8400Sstevel@tonic-gate  * accepting connections again.
8410Sstevel@tonic-gate  */
8420Sstevel@tonic-gate static void
ses_thr_exit()8430Sstevel@tonic-gate ses_thr_exit()
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate 	mutex_lock(&sessions_lock);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	sessions--;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	cond_signal(&sessions_cv);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	mutex_unlock(&sessions_lock);
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
855