xref: /illumos-gate/usr/src/cmd/dcs/sparc/sun4u/dcs_ses.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cb620785Sraf  * Common Development and Distribution License (the "License").
6*cb620785Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*cb620785Sraf 
227c478bd9Sstevel@tonic-gate /*
23*cb620785Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*cb620785Sraf  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This file is a module that provides an interface to managing
297c478bd9Sstevel@tonic-gate  * concurrent sessions executed in either a separate thread or a
307c478bd9Sstevel@tonic-gate  * separate process. Threads are used only if the compile time flag
317c478bd9Sstevel@tonic-gate  * DCS_MULTI_THREAD is set. Otherwise, a new process is forked for
327c478bd9Sstevel@tonic-gate  * each session.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Multiple processes are used to enable full Internationalization
357c478bd9Sstevel@tonic-gate  * support. This support requires that each session is able to set
367c478bd9Sstevel@tonic-gate  * its own locale for use in reporting errors to the user. Currently,
377c478bd9Sstevel@tonic-gate  * this is not possible using multiple threads because the locale
387c478bd9Sstevel@tonic-gate  * can not be set for an individual thread. For this reason, multiple
397c478bd9Sstevel@tonic-gate  * processes are supported until proper locale support is provided
407c478bd9Sstevel@tonic-gate  * for multiple threads.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * When Solaris supports a different locale in each thread, all
437c478bd9Sstevel@tonic-gate  * code used to enable using multiple processes should be removed.
447c478bd9Sstevel@tonic-gate  * To simplify this process, all references to DCS_MULTI_THREAD can
457c478bd9Sstevel@tonic-gate  * be found in this file.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <stdlib.h>
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
527c478bd9Sstevel@tonic-gate #include <errno.h>
537c478bd9Sstevel@tonic-gate #include <signal.h>
547c478bd9Sstevel@tonic-gate #include <syslog.h>
557c478bd9Sstevel@tonic-gate #include <locale.h>
567c478bd9Sstevel@tonic-gate #include <sys/socket.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
597c478bd9Sstevel@tonic-gate #include <thread.h>
60*cb620785Sraf #include <pthread.h>
617c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
627c478bd9Sstevel@tonic-gate #include <sys/types.h>
637c478bd9Sstevel@tonic-gate #include <sys/wait.h>
647c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include "dcs.h"
677c478bd9Sstevel@tonic-gate #include "rdr_messages.h"
687c478bd9Sstevel@tonic-gate #include "rdr_param_types.h"
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	DCS_DEFAULT_LOCALE		"C"
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* session allocation/deallocation functions */
757c478bd9Sstevel@tonic-gate static int ses_alloc(void);
767c478bd9Sstevel@tonic-gate static int ses_free(void);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* handler functions */
797c478bd9Sstevel@tonic-gate static void *ses_handler(void *arg);
807c478bd9Sstevel@tonic-gate #ifndef DCS_MULTI_THREAD
817c478bd9Sstevel@tonic-gate static void exit_handler(int sig, siginfo_t *info, void *context);
827c478bd9Sstevel@tonic-gate #endif /* !DCS_MULTI_THREAD */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* session accounting functions */
857c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
867c478bd9Sstevel@tonic-gate static void ses_thr_exit(void);
877c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Global structure that holds all relevant information
927c478bd9Sstevel@tonic-gate  * about the current session. If multiple threads are
937c478bd9Sstevel@tonic-gate  * used, the thread specific data mechanism is used. This
947c478bd9Sstevel@tonic-gate  * requires a data key to access the thread's private
957c478bd9Sstevel@tonic-gate  * session information.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
98*cb620785Sraf thread_key_t	ses_key = THR_ONCE_KEY;
997c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
1007c478bd9Sstevel@tonic-gate session_t	*ses;
1017c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Information about the current number of active sessions.
1067c478bd9Sstevel@tonic-gate  * If multiple threads are used, synchronization objects
1077c478bd9Sstevel@tonic-gate  * are required.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static ulong_t sessions = 0;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
1127c478bd9Sstevel@tonic-gate static mutex_t	sessions_lock = DEFAULTMUTEX;
1137c478bd9Sstevel@tonic-gate static cond_t	sessions_cv   = DEFAULTCV;
1147c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * ses_start:
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * Start the session handler. If multiple threads are used, create a new
1217c478bd9Sstevel@tonic-gate  * thread that runs the ses_handler() function. If multiple processes
1227c478bd9Sstevel@tonic-gate  * are used, fork a new process and call ses_handler().
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate int
ses_start(int fd)1257c478bd9Sstevel@tonic-gate ses_start(int fd)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	int	thr_err;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	mutex_lock(&sessions_lock);
1337c478bd9Sstevel@tonic-gate 	sessions++;
1347c478bd9Sstevel@tonic-gate 	mutex_unlock(&sessions_lock);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	thr_err = thr_create(NULL, 0, ses_handler, (void *)fd,
1377c478bd9Sstevel@tonic-gate 	    THR_DETACHED | THR_NEW_LWP, NULL);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return ((thr_err) ? -1 : 0);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	int 	pid;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	pid = fork();
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (pid == -1) {
1497c478bd9Sstevel@tonic-gate 		(void) rdr_close(fd);
1507c478bd9Sstevel@tonic-gate 		return (-1);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * Parent:
1557c478bd9Sstevel@tonic-gate 	 */
1567c478bd9Sstevel@tonic-gate 	if (pid) {
1577c478bd9Sstevel@tonic-gate 		/* close the child's fd */
1587c478bd9Sstevel@tonic-gate 		(void) close(fd);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		sessions++;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 		return (0);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * Child:
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	ses_handler((void *)fd);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Prevent return to parent's loop
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 	exit(0);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * ses_close:
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  * Initiate the closure of a session by sending an RDR_SES_END message
1857c478bd9Sstevel@tonic-gate  * to the client. It does not attempt to close the network connection.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate int
ses_close(int err_code)1887c478bd9Sstevel@tonic-gate ses_close(int err_code)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	session_t	*sp;
1917c478bd9Sstevel@tonic-gate 	cfga_params_t	req_data;
1927c478bd9Sstevel@tonic-gate 	rdr_msg_hdr_t	req_hdr;
1937c478bd9Sstevel@tonic-gate 	int		snd_status;
1947c478bd9Sstevel@tonic-gate 	static char	*op_name = "session close";
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* get the current session information */
1987c478bd9Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
1997c478bd9Sstevel@tonic-gate 		ses_close(DCS_ERROR);
2007c478bd9Sstevel@tonic-gate 		return (-1);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* check if already sent session end */
2047c478bd9Sstevel@tonic-gate 	if (sp->state == DCS_SES_END) {
2057c478bd9Sstevel@tonic-gate 		return (0);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/* prepare header information */
2097c478bd9Sstevel@tonic-gate 	init_msg(&req_hdr);
2107c478bd9Sstevel@tonic-gate 	req_hdr.message_opcode = RDR_SES_END;
2117c478bd9Sstevel@tonic-gate 	req_hdr.data_type = RDR_REQUEST;
2127c478bd9Sstevel@tonic-gate 	req_hdr.status = err_code;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* no operation specific data */
2157c478bd9Sstevel@tonic-gate 	(void) memset(&req_data, 0, sizeof (req_data));
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	PRINT_MSG_DBG(DCS_SEND, &req_hdr);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* send the message */
2207c478bd9Sstevel@tonic-gate 	snd_status = rdr_snd_msg(sp->fd, &req_hdr, &req_data, DCS_SND_TIMEOUT);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (snd_status == RDR_ABORTED) {
2237c478bd9Sstevel@tonic-gate 		abort_handler();
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (snd_status != RDR_OK) {
2277c478bd9Sstevel@tonic-gate 		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/*
2317c478bd9Sstevel@tonic-gate 	 * Setting the session state to DCS_SES_END will
2327c478bd9Sstevel@tonic-gate 	 * cause the session handler to terminate the
2337c478bd9Sstevel@tonic-gate 	 * network connection. This should happen whether
2347c478bd9Sstevel@tonic-gate 	 * or not the session end message that was just
2357c478bd9Sstevel@tonic-gate 	 * sent was received successfully.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	sp->state = DCS_SES_END;
2387c478bd9Sstevel@tonic-gate 	return (0);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * ses_abort:
2447c478bd9Sstevel@tonic-gate  *
2457c478bd9Sstevel@tonic-gate  * Attempt to abort an active session. If multiple threads are used,
2467c478bd9Sstevel@tonic-gate  * the parameter represents a thread_t identifier. If multiple
2477c478bd9Sstevel@tonic-gate  * processes are used, the parameter represents a pid. In either
2487c478bd9Sstevel@tonic-gate  * case, use this identifier to send a SIGINT signal to the approprate
2497c478bd9Sstevel@tonic-gate  * session.
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate int
ses_abort(long ses_id)2527c478bd9Sstevel@tonic-gate ses_abort(long ses_id)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "killing session %d", ses_id);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (thr_kill(ses_id, SIGINT) != 0) {
2597c478bd9Sstevel@tonic-gate 		/*
2607c478bd9Sstevel@tonic-gate 		 * If the thread cannot be found, we will assume
2617c478bd9Sstevel@tonic-gate 		 * that the session was able to exit normally. In
2627c478bd9Sstevel@tonic-gate 		 * this case, there is no error since the desired
2637c478bd9Sstevel@tonic-gate 		 * result has already been achieved.
2647c478bd9Sstevel@tonic-gate 		 */
2657c478bd9Sstevel@tonic-gate 		if (errno == ESRCH) {
2667c478bd9Sstevel@tonic-gate 			return (0);
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 		return (-1);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (kill(ses_id, SIGINT) == -1) {
2747c478bd9Sstevel@tonic-gate 		/*
2757c478bd9Sstevel@tonic-gate 		 * If the process cannot be found, we will assume
2767c478bd9Sstevel@tonic-gate 		 * that the session was able to exit normally. In
2777c478bd9Sstevel@tonic-gate 		 * this case, there is no error since the desired
2787c478bd9Sstevel@tonic-gate 		 * result has already been achieved.
2797c478bd9Sstevel@tonic-gate 		 */
2807c478bd9Sstevel@tonic-gate 		if (errno == ESRCH) {
2817c478bd9Sstevel@tonic-gate 			return (0);
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 		return (-1);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	return (0);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * ses_abort_enable:
2947c478bd9Sstevel@tonic-gate  *
2957c478bd9Sstevel@tonic-gate  * Enter a mode where the current session can be aborted. This mode
2967c478bd9Sstevel@tonic-gate  * will persist until ses_abort_disable() is called.
2977c478bd9Sstevel@tonic-gate  *
2987c478bd9Sstevel@tonic-gate  * A signal handler for SIGINT must be installed prior to calling this
2997c478bd9Sstevel@tonic-gate  * function. If this is not the case, and multiple threads are used,
3007c478bd9Sstevel@tonic-gate  * the default handler for SIGINT will cause the entire process to
3017c478bd9Sstevel@tonic-gate  * exit, rather than just the current session. If multiple processes
3027c478bd9Sstevel@tonic-gate  * are used, the default handler for SIGINT will not affect the main
3037c478bd9Sstevel@tonic-gate  * process, but it will prevent both sides from gracefully closing
3047c478bd9Sstevel@tonic-gate  * the session.
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate void
ses_abort_enable(void)3077c478bd9Sstevel@tonic-gate ses_abort_enable(void)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	sigset_t	unblock_set;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/* unblock SIGINT */
3137c478bd9Sstevel@tonic-gate 	sigemptyset(&unblock_set);
3147c478bd9Sstevel@tonic-gate 	sigaddset(&unblock_set, SIGINT);
3157c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &unblock_set, NULL);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * ses_abort_disable:
3217c478bd9Sstevel@tonic-gate  *
3227c478bd9Sstevel@tonic-gate  * Exit the mode where the current session can be aborted. This
3237c478bd9Sstevel@tonic-gate  * will leave the mode entered by ses_abort_enable().
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate void
ses_abort_disable(void)3267c478bd9Sstevel@tonic-gate ses_abort_disable(void)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	sigset_t	block_set;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/* block SIGINT */
3327c478bd9Sstevel@tonic-gate 	sigemptyset(&block_set);
3337c478bd9Sstevel@tonic-gate 	sigaddset(&block_set, SIGINT);
3347c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_set, NULL);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate /*
3397c478bd9Sstevel@tonic-gate  * ses_setlocale:
3407c478bd9Sstevel@tonic-gate  *
3417c478bd9Sstevel@tonic-gate  * Set the locale for the current session. Currently, if multiple threads
3427c478bd9Sstevel@tonic-gate  * are used, the 'C' locale is specified for all cases. Once there is support
3437c478bd9Sstevel@tonic-gate  * for setting a thread specific locale, the requested locale will be used.
3447c478bd9Sstevel@tonic-gate  * If multiple processes are used, an attempt is made to set the locale of
3457c478bd9Sstevel@tonic-gate  * the process to the locale passed in as a parameter.
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate int
ses_setlocale(char * locale)3487c478bd9Sstevel@tonic-gate ses_setlocale(char *locale)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	char	*new_locale;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/* sanity check */
3537c478bd9Sstevel@tonic-gate 	if (locale == NULL) {
3547c478bd9Sstevel@tonic-gate 		locale = DCS_DEFAULT_LOCALE;
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*
3607c478bd9Sstevel@tonic-gate 	 * Reserved for setting the locale on a per thread
3617c478bd9Sstevel@tonic-gate 	 * basis. Currently there is no Solaris support for
3627c478bd9Sstevel@tonic-gate 	 * this, so use the default locale.
3637c478bd9Sstevel@tonic-gate 	 */
3647c478bd9Sstevel@tonic-gate 	new_locale = setlocale(LC_ALL, DCS_DEFAULT_LOCALE);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	new_locale = setlocale(LC_ALL, locale);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	if ((new_locale == NULL) || (strcmp(new_locale, locale) != 0)) {
3737c478bd9Sstevel@tonic-gate 		/* silently fall back to C locale */
3747c478bd9Sstevel@tonic-gate 		new_locale = setlocale(LC_ALL, DCS_DEFAULT_LOCALE);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "using '%s' locale", new_locale);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	return (0);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * ses_init_signals:
3857c478bd9Sstevel@tonic-gate  *
3867c478bd9Sstevel@tonic-gate  * Initialize the set of signals to be blocked. It is assumed that the
3877c478bd9Sstevel@tonic-gate  * mask parameter initially contains all signals. If multiple threads
3887c478bd9Sstevel@tonic-gate  * are used, this is the correct behavior and the mask is not altered.
3897c478bd9Sstevel@tonic-gate  * If multiple processes are used, session accounting is performed in
3907c478bd9Sstevel@tonic-gate  * a SIGCHLD handler and so SIGCHLD must not be blocked. The action of
3917c478bd9Sstevel@tonic-gate  * initializing this handler is also performed in this function.
3927c478bd9Sstevel@tonic-gate  */
3937c478bd9Sstevel@tonic-gate /* ARGSUSED */
3947c478bd9Sstevel@tonic-gate void
ses_init_signals(sigset_t * mask)3957c478bd9Sstevel@tonic-gate ses_init_signals(sigset_t *mask)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate #ifndef DCS_MULTI_THREAD
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	struct sigaction	act;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/* unblock SIGCHLD */
4037c478bd9Sstevel@tonic-gate 	(void) sigdelset(mask, SIGCHLD);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * Establish a handler for SIGCHLD
4077c478bd9Sstevel@tonic-gate 	 */
4087c478bd9Sstevel@tonic-gate 	(void) memset(&act, 0, sizeof (act));
4097c478bd9Sstevel@tonic-gate 	act.sa_sigaction = exit_handler;
4107c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, NULL);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate #endif /* !DCS_MULTI_THREAD */
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * ses_sleep:
4207c478bd9Sstevel@tonic-gate  *
4217c478bd9Sstevel@tonic-gate  * Sleep for a specified amount of time, but don't prevent the
4227c478bd9Sstevel@tonic-gate  * session from being aborted.
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate void
ses_sleep(int sec)4257c478bd9Sstevel@tonic-gate ses_sleep(int sec)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	ses_abort_enable();
4287c478bd9Sstevel@tonic-gate 	sleep(sec);
4297c478bd9Sstevel@tonic-gate 	ses_abort_disable();
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * ses_wait:
4357c478bd9Sstevel@tonic-gate  *
4367c478bd9Sstevel@tonic-gate  * Wait for the number of active sessions to drop below the maximum
4377c478bd9Sstevel@tonic-gate  * allowed number of active sessions. If multiple threads are used,
4387c478bd9Sstevel@tonic-gate  * the thread waits on a condition variable until a child thread
4397c478bd9Sstevel@tonic-gate  * signals that it is going to exit. If multiple processes are used,
4407c478bd9Sstevel@tonic-gate  * the process waits until at least one child process exits.
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate static void
ses_wait(void)4437c478bd9Sstevel@tonic-gate ses_wait(void)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	mutex_lock(&sessions_lock);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	while (sessions >= max_sessions) {
4507c478bd9Sstevel@tonic-gate 		cond_wait(&sessions_cv, &sessions_lock);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	mutex_unlock(&sessions_lock);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (sessions >= max_sessions) {
4587c478bd9Sstevel@tonic-gate 		(void) wait(NULL);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate  * ses_poll:
4677c478bd9Sstevel@tonic-gate  *
4687c478bd9Sstevel@tonic-gate  * Poll on the file descriptors passed in as a parameter. Before polling,
4697c478bd9Sstevel@tonic-gate  * a check is performed to see if the number of active sessions is less
4707c478bd9Sstevel@tonic-gate  * than the maximum number of active sessions allowed. If the limit for
4717c478bd9Sstevel@tonic-gate  * active sessions is reached, the poll will be delayed until at least
4727c478bd9Sstevel@tonic-gate  * one session exits.
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate int
ses_poll(struct pollfd fds[],nfds_t nfds,int timeout)4757c478bd9Sstevel@tonic-gate ses_poll(struct pollfd fds[], nfds_t nfds, int timeout)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	int	err;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	ses_wait();
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	err = poll(fds, nfds, timeout);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	return (err);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  * curr_ses:
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  * Return a pointer to the global session information. If multiple threads
4927c478bd9Sstevel@tonic-gate  * are being used, this will point to a thread specific instance of a
4937c478bd9Sstevel@tonic-gate  * session structure.
4947c478bd9Sstevel@tonic-gate  */
4957c478bd9Sstevel@tonic-gate session_t *
curr_ses(void)4967c478bd9Sstevel@tonic-gate curr_ses(void)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
4997c478bd9Sstevel@tonic-gate 
500*cb620785Sraf 	return (pthread_getspecific(ses_key));
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	return (ses);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate /*
5117c478bd9Sstevel@tonic-gate  * curr_ses_id:
5127c478bd9Sstevel@tonic-gate  *
5137c478bd9Sstevel@tonic-gate  * Return the session identifier. This is either the thread_t identifier
5147c478bd9Sstevel@tonic-gate  * of the thread, or the pid of the process.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate long
curr_ses_id(void)5177c478bd9Sstevel@tonic-gate curr_ses_id(void)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	return (thr_self());
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	return (getpid());
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * ses_handler:
5337c478bd9Sstevel@tonic-gate  *
5347c478bd9Sstevel@tonic-gate  * Handle initialization and processing of a session. Initializes a session
5357c478bd9Sstevel@tonic-gate  * and enters a loop which waits for requests. When a request comes in, it
5367c478bd9Sstevel@tonic-gate  * is dispatched. When the session is terminated, the loop exits and the
5377c478bd9Sstevel@tonic-gate  * session is cleaned up.
5387c478bd9Sstevel@tonic-gate  */
5397c478bd9Sstevel@tonic-gate static void *
ses_handler(void * arg)5407c478bd9Sstevel@tonic-gate ses_handler(void *arg)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	session_t		*sp;
5437c478bd9Sstevel@tonic-gate 	rdr_msg_hdr_t		op_hdr;
5447c478bd9Sstevel@tonic-gate 	cfga_params_t		op_data;
5457c478bd9Sstevel@tonic-gate 	int			rcv_status;
5467c478bd9Sstevel@tonic-gate 	sigset_t		block_set;
5477c478bd9Sstevel@tonic-gate 	struct sigaction	act;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	static char *dcs_state_str[] = {
5507c478bd9Sstevel@tonic-gate 		"unknown state",
5517c478bd9Sstevel@tonic-gate 		"DCS_CONNECTED",
5527c478bd9Sstevel@tonic-gate 		"DCS_SES_REQ",
5537c478bd9Sstevel@tonic-gate 		"DCS_SES_ESTBL",
5547c478bd9Sstevel@tonic-gate 		"DCS_CONF_PENDING",
5557c478bd9Sstevel@tonic-gate 		"DCS_CONF_DONE",
5567c478bd9Sstevel@tonic-gate 		"DCS_SES_END"
5577c478bd9Sstevel@tonic-gate 	};
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (ses_alloc() == -1) {
5617c478bd9Sstevel@tonic-gate 		(void) rdr_close((int)arg);
5627c478bd9Sstevel@tonic-gate 		return ((void *)-1);
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
5667c478bd9Sstevel@tonic-gate 		ses_close(DCS_ERROR);
5677c478bd9Sstevel@tonic-gate 		return (NULL);
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/* initialize session information */
5717c478bd9Sstevel@tonic-gate 	memset(sp, 0, sizeof (session_t));
5727c478bd9Sstevel@tonic-gate 	sp->state = DCS_CONNECTED;
5737c478bd9Sstevel@tonic-gate 	sp->random_resp = lrand48();
5747c478bd9Sstevel@tonic-gate 	sp->fd = (int)arg;
5757c478bd9Sstevel@tonic-gate 	sp->id = curr_ses_id();
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/* initially, block all signals and cancels */
5787c478bd9Sstevel@tonic-gate 	(void) sigfillset(&block_set);
5797c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_set, NULL);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	/* set the abort handler for this session */
5827c478bd9Sstevel@tonic-gate 	(void) memset(&act, 0, sizeof (act));
5837c478bd9Sstevel@tonic-gate 	act.sa_handler = abort_handler;
5847c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "session handler starting...");
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/*
5897c478bd9Sstevel@tonic-gate 	 * Process all requests in the session until the
5907c478bd9Sstevel@tonic-gate 	 * session is terminated
5917c478bd9Sstevel@tonic-gate 	 */
5927c478bd9Sstevel@tonic-gate 	for (;;) {
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		DCS_DBG(DBG_STATE, "session state: %s",
5957c478bd9Sstevel@tonic-gate 		    dcs_state_str[sp->state]);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 		if (sp->state == DCS_SES_END) {
5987c478bd9Sstevel@tonic-gate 			break;
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		(void) memset(&op_hdr, 0, sizeof (op_hdr));
6027c478bd9Sstevel@tonic-gate 		(void) memset(&op_data, 0, sizeof (op_data));
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		rcv_status = rdr_rcv_msg(sp->fd, &op_hdr, &op_data,
6057c478bd9Sstevel@tonic-gate 		    DCS_RCV_TIMEOUT);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		if (rcv_status != RDR_OK) {
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 			switch (rcv_status) {
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 			case RDR_TIMEOUT:
6127c478bd9Sstevel@tonic-gate 				DCS_DBG(DBG_SES, "receive timed out");
6137c478bd9Sstevel@tonic-gate 				break;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 			case RDR_DISCONNECT:
6167c478bd9Sstevel@tonic-gate 				dcs_log_msg(LOG_NOTICE, DCS_DISCONNECT);
6177c478bd9Sstevel@tonic-gate 				break;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 			case RDR_ABORTED:
6207c478bd9Sstevel@tonic-gate 				dcs_log_msg(LOG_INFO, DCS_SES_ABORTED);
6217c478bd9Sstevel@tonic-gate 				break;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 			case RDR_MSG_INVAL:
6247c478bd9Sstevel@tonic-gate 				/*
6257c478bd9Sstevel@tonic-gate 				 * Only log invalid messages if a session has
6267c478bd9Sstevel@tonic-gate 				 * already been established. Logging invalid
6277c478bd9Sstevel@tonic-gate 				 * session request messages could flood syslog.
6287c478bd9Sstevel@tonic-gate 				 */
6297c478bd9Sstevel@tonic-gate 				if (sp->state != DCS_CONNECTED) {
6307c478bd9Sstevel@tonic-gate 					dcs_log_msg(LOG_WARNING, DCS_MSG_INVAL);
6317c478bd9Sstevel@tonic-gate 				} else {
6327c478bd9Sstevel@tonic-gate 					DCS_DBG(DBG_SES, "received an invalid "
6337c478bd9Sstevel@tonic-gate 					    "message");
6347c478bd9Sstevel@tonic-gate 				}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 				break;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 			default:
6397c478bd9Sstevel@tonic-gate 				dcs_log_msg(LOG_ERR, DCS_RECEIVE_ERR);
6407c478bd9Sstevel@tonic-gate 				break;
6417c478bd9Sstevel@tonic-gate 			}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 			/*
6447c478bd9Sstevel@tonic-gate 			 * We encountered an unrecoverable error,
6457c478bd9Sstevel@tonic-gate 			 * so exit this session handler.
6467c478bd9Sstevel@tonic-gate 			 */
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		} else {
6507c478bd9Sstevel@tonic-gate 			/* handle the message */
6517c478bd9Sstevel@tonic-gate 			dcs_dispatch_message(&op_hdr, &op_data);
6527c478bd9Sstevel@tonic-gate 			rdr_cleanup_params(op_hdr.message_opcode, &op_data);
6537c478bd9Sstevel@tonic-gate 		}
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "connection closed");
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	/* clean up */
6597c478bd9Sstevel@tonic-gate 	(void) rdr_close(sp->fd);
6607c478bd9Sstevel@tonic-gate 	(void) ses_free();
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
6637c478bd9Sstevel@tonic-gate 	ses_thr_exit();
6647c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	return (0);
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate /*
6717c478bd9Sstevel@tonic-gate  * abort_handler:
6727c478bd9Sstevel@tonic-gate  *
6737c478bd9Sstevel@tonic-gate  * Handle a request to abort a session. This function should be installed
6747c478bd9Sstevel@tonic-gate  * as the signal handler for SIGINT. It sends a message to the client
6757c478bd9Sstevel@tonic-gate  * indicating that the session was aborted, and that the operation failed
6767c478bd9Sstevel@tonic-gate  * as a result. The session then terminates, and the thread or process
6777c478bd9Sstevel@tonic-gate  * handling the session exits.
6787c478bd9Sstevel@tonic-gate  */
6797c478bd9Sstevel@tonic-gate void
abort_handler(void)6807c478bd9Sstevel@tonic-gate abort_handler(void)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	session_t	*sp;
6837c478bd9Sstevel@tonic-gate 	rdr_msg_hdr_t	op_hdr;
6847c478bd9Sstevel@tonic-gate 	cfga_params_t	op_data;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/* get the current session information */
6887c478bd9Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
6897c478bd9Sstevel@tonic-gate 		ses_close(DCS_ERROR);
6907c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
6917c478bd9Sstevel@tonic-gate 		ses_thr_exit();
6927c478bd9Sstevel@tonic-gate 		thr_exit(0);
6937c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
6947c478bd9Sstevel@tonic-gate 		exit(0);
6957c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	DCS_DBG(DBG_MSG, "abort_handler()");
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/* prepare header information */
7017c478bd9Sstevel@tonic-gate 	init_msg(&op_hdr);
7027c478bd9Sstevel@tonic-gate 	op_hdr.message_opcode = sp->curr_msg.hdr->message_opcode;
7037c478bd9Sstevel@tonic-gate 	op_hdr.data_type = RDR_REPLY;
7047c478bd9Sstevel@tonic-gate 	op_hdr.status = DCS_SES_ABORTED;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/* no operation specific data */
7077c478bd9Sstevel@tonic-gate 	(void) memset(&op_data, 0, sizeof (op_data));
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	PRINT_MSG_DBG(DCS_SEND, &op_hdr);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	(void) rdr_snd_msg(sp->fd, &op_hdr, &op_data, DCS_SND_TIMEOUT);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	DCS_DBG(DBG_INFO, "abort_handler: connection closed");
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/* clean up */
7167c478bd9Sstevel@tonic-gate 	rdr_cleanup_params(op_hdr.message_opcode, sp->curr_msg.params);
7177c478bd9Sstevel@tonic-gate 	(void) rdr_close(sp->fd);
7187c478bd9Sstevel@tonic-gate 	(void) ses_free();
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	dcs_log_msg(LOG_INFO, DCS_SES_ABORTED);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
7237c478bd9Sstevel@tonic-gate 	ses_thr_exit();
7247c478bd9Sstevel@tonic-gate 	thr_exit(0);
7257c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
7267c478bd9Sstevel@tonic-gate 	exit(0);
7277c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate #ifndef DCS_MULTI_THREAD
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate  * exit_handler:
7357c478bd9Sstevel@tonic-gate  *
7367c478bd9Sstevel@tonic-gate  * If multiple processes are used, this function is used to record
7377c478bd9Sstevel@tonic-gate  * the fact that a child process has exited. In order to make sure
7387c478bd9Sstevel@tonic-gate  * that all zombie processes are released, a waitpid() is performed
7397c478bd9Sstevel@tonic-gate  * for the child that has exited.
7407c478bd9Sstevel@tonic-gate  */
7417c478bd9Sstevel@tonic-gate /* ARGSUSED */
7427c478bd9Sstevel@tonic-gate static void
exit_handler(int sig,siginfo_t * info,void * context)7437c478bd9Sstevel@tonic-gate exit_handler(int sig, siginfo_t *info, void *context)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	sessions--;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (info != NULL) {
7487c478bd9Sstevel@tonic-gate 		(void) waitpid(info->si_pid, NULL, 0);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate #endif /* !DCS_MULTI_THREAD */
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate /*
7567c478bd9Sstevel@tonic-gate  * ses_alloc:
7577c478bd9Sstevel@tonic-gate  *
7587c478bd9Sstevel@tonic-gate  * Allocate the memory required for the global session structure.
7597c478bd9Sstevel@tonic-gate  * If multiple threads are used, create a thread specific data
7607c478bd9Sstevel@tonic-gate  * key. This will only occur the first time that this function
7617c478bd9Sstevel@tonic-gate  * gets called.
7627c478bd9Sstevel@tonic-gate  */
7637c478bd9Sstevel@tonic-gate static int
ses_alloc(void)7647c478bd9Sstevel@tonic-gate ses_alloc(void)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	session_t	*sp;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	int		thr_err;
7717c478bd9Sstevel@tonic-gate 
772*cb620785Sraf 	thr_err = thr_keycreate_once(&ses_key, NULL);
773*cb620785Sraf 	if (thr_err)
774*cb620785Sraf 		return (-1);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "allocating session memory");
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	sp = (session_t *)malloc(sizeof (session_t));
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (!sp) {
7837c478bd9Sstevel@tonic-gate 		dcs_log_msg(LOG_ERR, DCS_INT_ERR, "malloc", strerror(errno));
7847c478bd9Sstevel@tonic-gate 		return (-1);
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	thr_err = thr_setspecific(ses_key, sp);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	return ((thr_err) ? -1 : 0);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate #else /* DCS_MULTI_THREAD */
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	/* make the data global */
7967c478bd9Sstevel@tonic-gate 	ses = sp;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	return (0);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate /*
8057c478bd9Sstevel@tonic-gate  * ses_free:
8067c478bd9Sstevel@tonic-gate  *
8077c478bd9Sstevel@tonic-gate  * Deallocate the memory associated with the global session structure.
8087c478bd9Sstevel@tonic-gate  */
8097c478bd9Sstevel@tonic-gate static int
ses_free(void)8107c478bd9Sstevel@tonic-gate ses_free(void)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	session_t	*sp;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	DCS_DBG(DBG_SES, "freeing session memory");
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if ((sp = curr_ses()) == NULL) {
8187c478bd9Sstevel@tonic-gate 		ses_close(DCS_ERROR);
8197c478bd9Sstevel@tonic-gate 		return (-1);
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	if (sp) {
8237c478bd9Sstevel@tonic-gate 		(void) free((void *)sp);
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	return (0);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate #ifdef DCS_MULTI_THREAD
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate /*
8337c478bd9Sstevel@tonic-gate  * ses_thr_exit:
8347c478bd9Sstevel@tonic-gate  *
8357c478bd9Sstevel@tonic-gate  * If multiple threads are used, this function is used to record the
8367c478bd9Sstevel@tonic-gate  * fact that a child thread has exited. In addition, the condition
8377c478bd9Sstevel@tonic-gate  * variable is signaled so that the main thread can wakeup and begin
8387c478bd9Sstevel@tonic-gate  * accepting connections again.
8397c478bd9Sstevel@tonic-gate  */
8407c478bd9Sstevel@tonic-gate static void
ses_thr_exit()8417c478bd9Sstevel@tonic-gate ses_thr_exit()
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate 	mutex_lock(&sessions_lock);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	sessions--;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	cond_signal(&sessions_cv);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	mutex_unlock(&sessions_lock);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate #endif /* DCS_MULTI_THREAD */
853