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