xref: /titanic_52/usr/src/lib/libnsl/rpc/svc_run.c (revision 361f55a51b4e8a8d5a73478602f4afdc6e199da5)
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
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * 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  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26*361f55a5SMarcel Telka /*
27*361f55a5SMarcel Telka  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
28*361f55a5SMarcel Telka  */
29e8031f0aSraf 
307c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
317c478bd9Sstevel@tonic-gate /* All Rights Reserved */
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
347c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
357c478bd9Sstevel@tonic-gate  * California.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * This is the rpc server side idle loop
407c478bd9Sstevel@tonic-gate  * Wait for input, call server program.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate #include "mt.h"
437c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <signal.h>
477c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
487c478bd9Sstevel@tonic-gate #include <errno.h>
497c478bd9Sstevel@tonic-gate #include <sys/poll.h>
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <syslog.h>
527c478bd9Sstevel@tonic-gate #include <thread.h>
537c478bd9Sstevel@tonic-gate #include <assert.h>
547c478bd9Sstevel@tonic-gate #include <libintl.h>
557c478bd9Sstevel@tonic-gate #include <values.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate extern const char __nsl_dom[];
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate extern int __rpc_compress_pollfd(int, pollfd_t *, pollfd_t *);
607c478bd9Sstevel@tonic-gate extern bool_t __is_a_userfd(int P_fd);
617c478bd9Sstevel@tonic-gate extern void   __destroy_userfd();
627c478bd9Sstevel@tonic-gate extern void clear_pollfd(int);
637c478bd9Sstevel@tonic-gate extern void set_pollfd(int /* fd */, short /* events */);
647c478bd9Sstevel@tonic-gate extern void svc_getreq_poll();
657c478bd9Sstevel@tonic-gate extern void (*__proc_cleanup_cb)();
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static void start_threads();
687c478bd9Sstevel@tonic-gate static void create_pipe();
697c478bd9Sstevel@tonic-gate static void clear_pipe();
707c478bd9Sstevel@tonic-gate static int select_next_pollfd();
717c478bd9Sstevel@tonic-gate static SVCXPRT *make_xprt_copy();
727c478bd9Sstevel@tonic-gate static void _svc_run_mt();
737c478bd9Sstevel@tonic-gate static void _svc_run();
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate int _svc_prog_dispatch();
767c478bd9Sstevel@tonic-gate static void _svc_done_private();
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate extern rwlock_t svc_fd_lock;
797c478bd9Sstevel@tonic-gate extern mutex_t	svc_door_mutex;
807c478bd9Sstevel@tonic-gate extern cond_t	svc_door_waitcv;
817c478bd9Sstevel@tonic-gate extern int	svc_ndoorfds;
827c478bd9Sstevel@tonic-gate extern void	__svc_cleanup_door_xprts();
837c478bd9Sstevel@tonic-gate extern void	__svc_free_xprtlist();
847c478bd9Sstevel@tonic-gate extern void	__svc_getreq_user(struct pollfd *);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Maximum fragment size allowed for connection oriented connections.
887c478bd9Sstevel@tonic-gate  * Zero means that no maximum size limit has been requested.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate int __rpc_connmaxrec = 0;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* Inter-Record Timeout in secs for non-blocked connection RPC */
937c478bd9Sstevel@tonic-gate int __rpc_irtimeout = 35;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Request exclusive access to tcp and udp non-priv ports bound with a
977c478bd9Sstevel@tonic-gate  * wildcard addr.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate bool_t __rpc_tp_exclbind = FALSE;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
102*361f55a5SMarcel Telka  * Maximum number of outstanding connection indications (listen backlog).
103*361f55a5SMarcel Telka  */
104*361f55a5SMarcel Telka static int __svc_lstnbklog = 64;	/* Chosen Arbitrarily */
105*361f55a5SMarcel Telka 
106*361f55a5SMarcel Telka /*
1077c478bd9Sstevel@tonic-gate  * XXX - eventually, all mutexes and their initializations static
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * Variables used for MT
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate int svc_mt_mode;		/* multi-threading mode */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate int svc_pipe[2];	/* pipe for breaking out of poll: read(0), write(1) */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* BEGIN PROTECTED BY svc_mutex */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static int svc_thr_max = 16;	/* default maximum number of threads allowed */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static int svc_thr_total;	/* current number of threads */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static int svc_thr_active;	/* current number of threads active */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /* circular array of file descriptors with pending data */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	CIRCULAR_BUFSIZE	1024
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static int svc_pending_fds[CIRCULAR_BUFSIZE+1];	/* fds with pending data */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static int svc_next_pending;			/* next one to be processed */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static int svc_last_pending;			/* last one in list */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static int svc_total_pending;			/* total in list */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static int svc_thr_total_creates;	/* total created - stats */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static int svc_thr_total_create_errors;	/* total create errors - stats */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static int svc_waiters;		/* number of waiting threads */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /* END PROTECTED BY svc_mutex */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /* BEGIN PROTECTED BY svc_fd_lock: */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate int svc_nfds;		/* total number of active file descriptors */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate int svc_nfds_set;	/* total number of fd bits set in svc_fdset */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate int svc_max_fd = 0;	/* largest active file descriptor */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate int svc_npollfds;	/* total number of active pollfds */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate int svc_npollfds_set;	/* total number of pollfd set in svc_pollfd */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate int svc_max_pollfd;	/* largest active pollfd so far */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int svc_pollfd_allocd;  /* number of pollfd structures allocated */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /* END PROTECTED BY svc_fd_lock: */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /* BEGIN PROTECTED BY svc_thr_mutex */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #define	POLLSET_EXTEND	256
1667c478bd9Sstevel@tonic-gate static int svc_pollset_allocd;
1677c478bd9Sstevel@tonic-gate static struct pollfd *svc_pollset;
1687c478bd9Sstevel@tonic-gate 				/*
1697c478bd9Sstevel@tonic-gate 				 * array of file descriptors currently active
1707c478bd9Sstevel@tonic-gate 				 */
1717c478bd9Sstevel@tonic-gate static int svc_polled;		/* no of fds polled in last poll() - input */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static int svc_pollfds;		/* no of active fds in last poll() - output */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static int svc_next_pollfd;	/* next fd  to processin svc_pollset */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate bool_t svc_polling;		/* true if a thread is polling */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /* END PROTECTED BY svc_thr_mutex */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /* BEGIN PROTECTED BY svc_exit_mutex */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static bool_t svc_exit_done = TRUE;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /* END PROTECTED BY svc_exit_mutex */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Warlock section
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_mutex:
1927c478bd9Sstevel@tonic-gate 	svc_thr_total, svc_thr_active, svc_pending_fds, svc_next_pending,
1937c478bd9Sstevel@tonic-gate 	svc_last_pending, svc_total_pending, svc_thr_total_creates,
1947c478bd9Sstevel@tonic-gate 	svc_thr_total_create_errors,
1957c478bd9Sstevel@tonic-gate 	svcxprt_list_t::next, svcxprt_ext_t::my_xlist,
1967c478bd9Sstevel@tonic-gate 	svc_thr_max, svc_waiters
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_fd_lock:
2007c478bd9Sstevel@tonic-gate 	svc_xports, svc_fdset, svc_nfds, svc_nfds_set, svc_max_fd,
2017c478bd9Sstevel@tonic-gate 	svc_pollfd, svc_npollfds, svc_npollfds_set, svc_max_pollfd
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_thr_mutex:
2057c478bd9Sstevel@tonic-gate 	svc_pollset, svc_pollfds, svc_next_pollfd, svc_polling
2067c478bd9Sstevel@tonic-gate 	svc_pollset_allocd, svc_polled
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_exit_mutex:
2107c478bd9Sstevel@tonic-gate 	svc_exit_done
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /* VARIABLES READABLE WITHOUT LOCK:
2147c478bd9Sstevel@tonic-gate 	svc_thr_total, svc_thr_active, svc_thr_total_creates,
2157c478bd9Sstevel@tonic-gate 	svc_thr_total_create_errors,
2167c478bd9Sstevel@tonic-gate 	svc_xports, svc_nfds, svc_nfds_set, svc_max_fd,
2177c478bd9Sstevel@tonic-gate 	svc_npollfds, svc_npollfds_set, svc_max_pollfd,
2187c478bd9Sstevel@tonic-gate 	svc_pollfds, svc_next_pollfd, svc_exit_done, svc_polling,
2197c478bd9Sstevel@tonic-gate 	svc_thr_max, svc_waiters
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY "program_logic":
2237c478bd9Sstevel@tonic-gate 	rpc_msg::, svc_req::, svcxprt_ext_t::flags, svc_mt_mode,
2247c478bd9Sstevel@tonic-gate 	svcxprt_ext_t::parent
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /* LOCK ORDER:
2287c478bd9Sstevel@tonic-gate 	svc_exit_mutex, svc_thr_mutex, svc_mutex, svc_fd_lock
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate void
23361961e0fSrobinson svc_run(void)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	/* NO OTHER THREADS ARE RUNNING */
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	svc_exit_done = FALSE;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	while ((svc_npollfds > 0 || svc_ndoorfds > 0) && !svc_exit_done) {
2407c478bd9Sstevel@tonic-gate 		if (svc_npollfds > 0) {
2417c478bd9Sstevel@tonic-gate 			switch (svc_mt_mode) {
2427c478bd9Sstevel@tonic-gate 			case RPC_SVC_MT_NONE:
2437c478bd9Sstevel@tonic-gate 				_svc_run();
2447c478bd9Sstevel@tonic-gate 				break;
2457c478bd9Sstevel@tonic-gate 			default:
2467c478bd9Sstevel@tonic-gate 				_svc_run_mt();
2477c478bd9Sstevel@tonic-gate 				break;
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 			continue;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 
25261961e0fSrobinson 		(void) mutex_lock(&svc_door_mutex);
2537c478bd9Sstevel@tonic-gate 		if (svc_ndoorfds > 0)
25461961e0fSrobinson 			(void) cond_wait(&svc_door_waitcv, &svc_door_mutex);
25561961e0fSrobinson 		(void) mutex_unlock(&svc_door_mutex);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  *	This function causes svc_run() to exit by destroying all
2627c478bd9Sstevel@tonic-gate  *	service handles.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate void
26561961e0fSrobinson svc_exit(void)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	SVCXPRT	*xprt;
2687c478bd9Sstevel@tonic-gate 	int fd;
2697c478bd9Sstevel@tonic-gate 	char dummy;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/* NO LOCKS HELD */
2727c478bd9Sstevel@tonic-gate 
27361961e0fSrobinson 	(void) mutex_lock(&svc_exit_mutex);
2747c478bd9Sstevel@tonic-gate 	if (svc_exit_done) {
27561961e0fSrobinson 		(void) mutex_unlock(&svc_exit_mutex);
2767c478bd9Sstevel@tonic-gate 		return;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	svc_exit_done = TRUE;
2797c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < svc_max_pollfd; fd++) {
2807c478bd9Sstevel@tonic-gate 		xprt = svc_xports[fd];
2817c478bd9Sstevel@tonic-gate 		if (xprt) {
2827c478bd9Sstevel@tonic-gate 			SVC_DESTROY(xprt);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	__svc_free_xprtlist();
2867c478bd9Sstevel@tonic-gate 	__svc_cleanup_door_xprts();
28761961e0fSrobinson 	(void) mutex_unlock(&svc_exit_mutex);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (svc_mt_mode != RPC_SVC_MT_NONE) {
29061961e0fSrobinson 		(void) mutex_lock(&svc_mutex);
29161961e0fSrobinson 		(void) cond_broadcast(&svc_thr_fdwait);
29261961e0fSrobinson 		(void) mutex_unlock(&svc_mutex);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		(void) write(svc_pipe[1], &dummy, sizeof (dummy));
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
29761961e0fSrobinson 	(void) mutex_lock(&svc_door_mutex);
29861961e0fSrobinson 	(void) cond_signal(&svc_door_waitcv);	/* wake up door dispatching */
29961961e0fSrobinson 	(void) mutex_unlock(&svc_door_mutex);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/* destroy reactor information if any */
3027c478bd9Sstevel@tonic-gate 	__destroy_userfd();
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate  * this funtion is called with svc_fd_lock and svc_thr_mutex
3087c478bd9Sstevel@tonic-gate  */
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate static int
3117c478bd9Sstevel@tonic-gate alloc_pollset(int npollfds)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	if (npollfds > svc_pollset_allocd) {
3147c478bd9Sstevel@tonic-gate 		pollfd_t *tmp;
3157c478bd9Sstevel@tonic-gate 		do {
3167c478bd9Sstevel@tonic-gate 			svc_pollset_allocd += POLLSET_EXTEND;
3177c478bd9Sstevel@tonic-gate 		} while (npollfds > svc_pollset_allocd);
3187c478bd9Sstevel@tonic-gate 		tmp = realloc(svc_pollset,
3197c478bd9Sstevel@tonic-gate 		    sizeof (pollfd_t) * svc_pollset_allocd);
3207c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
3217c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "alloc_pollset: out of memory");
3227c478bd9Sstevel@tonic-gate 			return (-1);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		svc_pollset = tmp;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	return (0);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate static void
33061961e0fSrobinson _svc_run(void)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	sigset_t set, oldset;
3337c478bd9Sstevel@tonic-gate 	int npollfds;
3347c478bd9Sstevel@tonic-gate 	int i;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * Block SIGALRM while doing work.  Unblock it while doing poll().
3387c478bd9Sstevel@tonic-gate 	 * This is so that services like rpc.rstatd can cause the poll()
3397c478bd9Sstevel@tonic-gate 	 * to be interrupted due to alarm() but that we don't end up in
3407c478bd9Sstevel@tonic-gate 	 * an MT-unsafe signal handler at an inopportune time.
3417c478bd9Sstevel@tonic-gate 	 */
342e8031f0aSraf 	(void) sigemptyset(&set);
343e8031f0aSraf 	(void) sigaddset(&set, SIGALRM);
344e8031f0aSraf 	(void) sigprocmask(SIG_BLOCK, &set, &oldset);
3457c478bd9Sstevel@tonic-gate 	while (!svc_exit_done) {
3467c478bd9Sstevel@tonic-gate 		/*
3477c478bd9Sstevel@tonic-gate 		 * Check whether there is any server fd on which we may want
3487c478bd9Sstevel@tonic-gate 		 * to wait.
3497c478bd9Sstevel@tonic-gate 		 */
35061961e0fSrobinson 		(void) rw_rdlock(&svc_fd_lock);
3517c478bd9Sstevel@tonic-gate 		if (alloc_pollset(svc_npollfds) == -1)
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 		npollfds = __rpc_compress_pollfd(svc_max_pollfd,
3547c478bd9Sstevel@tonic-gate 		    svc_pollfd, svc_pollset);
35561961e0fSrobinson 		(void) rw_unlock(&svc_fd_lock);
3567c478bd9Sstevel@tonic-gate 		if (npollfds == 0)
3577c478bd9Sstevel@tonic-gate 			break;	/* None waiting, hence return */
3587c478bd9Sstevel@tonic-gate 
359e8031f0aSraf 		(void) sigprocmask(SIG_SETMASK, &oldset, NULL);
3607c478bd9Sstevel@tonic-gate 		i = poll(svc_pollset, npollfds, -1);
361e8031f0aSraf 		(void) sigprocmask(SIG_BLOCK, &set, &oldset);
3627c478bd9Sstevel@tonic-gate 		switch (i) {
3637c478bd9Sstevel@tonic-gate 		case -1:
3647c478bd9Sstevel@tonic-gate 			/*
3657c478bd9Sstevel@tonic-gate 			 * We ignore all errors, continuing with the assumption
3667c478bd9Sstevel@tonic-gate 			 * that it was set by the signal handlers (or any
3677c478bd9Sstevel@tonic-gate 			 * other outside event) and not caused by poll().
3687c478bd9Sstevel@tonic-gate 			 */
3697c478bd9Sstevel@tonic-gate 		case 0:
3707c478bd9Sstevel@tonic-gate 			continue;
3717c478bd9Sstevel@tonic-gate 		default:
3727c478bd9Sstevel@tonic-gate 			svc_getreq_poll(svc_pollset, i);
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 	}
375e8031f0aSraf 	(void) sigprocmask(SIG_SETMASK, &oldset, NULL);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate  * In _svc_run_mt, myfd is linked with mypollfd
3807c478bd9Sstevel@tonic-gate  * svc_pollset[mypollfd].fd == myfd
3817c478bd9Sstevel@tonic-gate  * However, in some cases, the link can not be made, thus we define the
3827c478bd9Sstevel@tonic-gate  * following values for these special cases
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate enum {
3857c478bd9Sstevel@tonic-gate 	INVALID_POLLFD	= -200,
3867c478bd9Sstevel@tonic-gate 	FD_FROM_PENDING
3877c478bd9Sstevel@tonic-gate };
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate static void
39061961e0fSrobinson _svc_run_mt(void)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	int npollfds;
3937c478bd9Sstevel@tonic-gate 	int n_polled, dispatch;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	static bool_t first_time = TRUE;
3967c478bd9Sstevel@tonic-gate 	bool_t main_thread = FALSE;
3977c478bd9Sstevel@tonic-gate 	int n_new;
3987c478bd9Sstevel@tonic-gate 	int myfd, mypollfd;
3997c478bd9Sstevel@tonic-gate 	SVCXPRT *parent_xprt, *xprt;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/*
4027c478bd9Sstevel@tonic-gate 	 * Server is multi-threaded.  Do "first time" initializations.
4037c478bd9Sstevel@tonic-gate 	 * Since only one thread exists in the beginning, there's no
4047c478bd9Sstevel@tonic-gate 	 * need for mutex protection for first time initializations.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	if (first_time) {
4077c478bd9Sstevel@tonic-gate 		first_time = FALSE;
4087c478bd9Sstevel@tonic-gate 		main_thread = TRUE;
4097c478bd9Sstevel@tonic-gate 		svc_thr_total = 1;	/* this thread */
4107c478bd9Sstevel@tonic-gate 		svc_next_pending = svc_last_pending = 0;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		/*
4137c478bd9Sstevel@tonic-gate 		 * Create a pipe for waking up the poll, if new
4147c478bd9Sstevel@tonic-gate 		 * descriptors have been added to svc_fdset.
4157c478bd9Sstevel@tonic-gate 		 */
4167c478bd9Sstevel@tonic-gate 		create_pipe();
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/* OTHER THREADS ARE RUNNING */
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if (svc_exit_done)
4227c478bd9Sstevel@tonic-gate 		return;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	for (;;) {
4257c478bd9Sstevel@tonic-gate 		/*
4267c478bd9Sstevel@tonic-gate 		 * svc_thr_mutex prevents more than one thread from
4277c478bd9Sstevel@tonic-gate 		 * trying to select a descriptor to process further.
4287c478bd9Sstevel@tonic-gate 		 * svc_thr_mutex is unlocked after a thread selects
4297c478bd9Sstevel@tonic-gate 		 * a descriptor on which to receive data.  If there are
4307c478bd9Sstevel@tonic-gate 		 * no such descriptors, the thread will poll with
4317c478bd9Sstevel@tonic-gate 		 * svc_thr_mutex locked, after unlocking all other
4327c478bd9Sstevel@tonic-gate 		 * locks.  This prevents more than one thread from
4337c478bd9Sstevel@tonic-gate 		 * trying to poll at the same time.
4347c478bd9Sstevel@tonic-gate 		 */
43561961e0fSrobinson 		(void) mutex_lock(&svc_thr_mutex);
43661961e0fSrobinson 		(void) mutex_lock(&svc_mutex);
4377c478bd9Sstevel@tonic-gate continue_with_locks:
4387c478bd9Sstevel@tonic-gate 		myfd = -1;
4397c478bd9Sstevel@tonic-gate 		mypollfd = INVALID_POLLFD;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		/*
4427c478bd9Sstevel@tonic-gate 		 * Check if there are any descriptors with data pending.
4437c478bd9Sstevel@tonic-gate 		 */
4447c478bd9Sstevel@tonic-gate 		if (svc_total_pending > 0) {
4457c478bd9Sstevel@tonic-gate 			myfd = svc_pending_fds[svc_next_pending++];
4467c478bd9Sstevel@tonic-gate 			mypollfd = FD_FROM_PENDING;
4477c478bd9Sstevel@tonic-gate 			if (svc_next_pending > CIRCULAR_BUFSIZE)
4487c478bd9Sstevel@tonic-gate 				svc_next_pending = 0;
4497c478bd9Sstevel@tonic-gate 			svc_total_pending--;
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		/*
4537c478bd9Sstevel@tonic-gate 		 * Get the next active file descriptor to process.
4547c478bd9Sstevel@tonic-gate 		 */
4557c478bd9Sstevel@tonic-gate 		if (myfd == -1 && svc_pollfds == 0) {
4567c478bd9Sstevel@tonic-gate 			/*
4577c478bd9Sstevel@tonic-gate 			 * svc_pollset is empty; do polling
4587c478bd9Sstevel@tonic-gate 			 */
4597c478bd9Sstevel@tonic-gate 			svc_polling = TRUE;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 			/*
4627c478bd9Sstevel@tonic-gate 			 * if there are no file descriptors, return
4637c478bd9Sstevel@tonic-gate 			 */
46461961e0fSrobinson 			(void) rw_rdlock(&svc_fd_lock);
4657c478bd9Sstevel@tonic-gate 			if (svc_npollfds == 0 ||
4667c478bd9Sstevel@tonic-gate 			    alloc_pollset(svc_npollfds + 1) == -1) {
46761961e0fSrobinson 				(void) rw_unlock(&svc_fd_lock);
4687c478bd9Sstevel@tonic-gate 				svc_polling = FALSE;
4697c478bd9Sstevel@tonic-gate 				svc_thr_total--;
47061961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
47161961e0fSrobinson 				(void) mutex_unlock(&svc_thr_mutex);
4727c478bd9Sstevel@tonic-gate 				if (!main_thread) {
4737c478bd9Sstevel@tonic-gate 					thr_exit(NULL);
4747c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
4757c478bd9Sstevel@tonic-gate 				}
4767c478bd9Sstevel@tonic-gate 				break;
4777c478bd9Sstevel@tonic-gate 			}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 			npollfds = __rpc_compress_pollfd(svc_max_pollfd,
4807c478bd9Sstevel@tonic-gate 			    svc_pollfd, svc_pollset);
48161961e0fSrobinson 			(void) rw_unlock(&svc_fd_lock);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 			if (npollfds == 0) {
4847c478bd9Sstevel@tonic-gate 				/*
4857c478bd9Sstevel@tonic-gate 				 * There are file descriptors, but none of them
4867c478bd9Sstevel@tonic-gate 				 * are available for polling.  If this is the
4877c478bd9Sstevel@tonic-gate 				 * main thread, or if no thread is waiting,
4887c478bd9Sstevel@tonic-gate 				 * wait on condition variable, otherwise exit.
4897c478bd9Sstevel@tonic-gate 				 */
4907c478bd9Sstevel@tonic-gate 				svc_polling = FALSE;
49161961e0fSrobinson 				(void) mutex_unlock(&svc_thr_mutex);
4927c478bd9Sstevel@tonic-gate 				if ((!main_thread) && svc_waiters > 0) {
4937c478bd9Sstevel@tonic-gate 					svc_thr_total--;
49461961e0fSrobinson 					(void) mutex_unlock(&svc_mutex);
4957c478bd9Sstevel@tonic-gate 					thr_exit(NULL);
4967c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
4977c478bd9Sstevel@tonic-gate 				}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 				while (svc_npollfds_set == 0 &&
5007c478bd9Sstevel@tonic-gate 				    svc_pollfds == 0 &&
5017c478bd9Sstevel@tonic-gate 				    svc_total_pending == 0 &&
5027c478bd9Sstevel@tonic-gate 				    !svc_exit_done) {
5037c478bd9Sstevel@tonic-gate 					svc_waiters++;
50461961e0fSrobinson 					(void) cond_wait(&svc_thr_fdwait,
50561961e0fSrobinson 					    &svc_mutex);
5067c478bd9Sstevel@tonic-gate 					svc_waiters--;
5077c478bd9Sstevel@tonic-gate 				}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 				/*
5107c478bd9Sstevel@tonic-gate 				 * Check exit flag.  If this is not the main
5117c478bd9Sstevel@tonic-gate 				 * thread, exit.
5127c478bd9Sstevel@tonic-gate 				 */
5137c478bd9Sstevel@tonic-gate 				if (svc_exit_done) {
5147c478bd9Sstevel@tonic-gate 					svc_thr_total--;
51561961e0fSrobinson 					(void) mutex_unlock(&svc_mutex);
5167c478bd9Sstevel@tonic-gate 					if (!main_thread)
5177c478bd9Sstevel@tonic-gate 						thr_exit(NULL);
5187c478bd9Sstevel@tonic-gate 					break;
5197c478bd9Sstevel@tonic-gate 				}
5207c478bd9Sstevel@tonic-gate 
52161961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
5227c478bd9Sstevel@tonic-gate 				continue;
5237c478bd9Sstevel@tonic-gate 			}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 			/*
5267c478bd9Sstevel@tonic-gate 			 * We're ready to poll.  Always set svc_pipe[0]
5277c478bd9Sstevel@tonic-gate 			 * as the last one, since the poll will occasionally
5287c478bd9Sstevel@tonic-gate 			 * need to be interrupted.  Release svc_mutex for
5297c478bd9Sstevel@tonic-gate 			 * the duration of the poll, but hold on to
5307c478bd9Sstevel@tonic-gate 			 * svc_thr_mutex, as we don't want any other thread
5317c478bd9Sstevel@tonic-gate 			 * to do the same.
5327c478bd9Sstevel@tonic-gate 			 */
5337c478bd9Sstevel@tonic-gate 			svc_pollset[npollfds].fd = svc_pipe[0];
5347c478bd9Sstevel@tonic-gate 			svc_pollset[npollfds].events = MASKVAL;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 			do {
5377c478bd9Sstevel@tonic-gate 				int i, j;
5387c478bd9Sstevel@tonic-gate 
53961961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
5407c478bd9Sstevel@tonic-gate 				n_polled = poll(svc_pollset, npollfds + 1, -1);
54161961e0fSrobinson 				(void) mutex_lock(&svc_mutex);
5427c478bd9Sstevel@tonic-gate 				if (n_polled <= 0)
5437c478bd9Sstevel@tonic-gate 					continue;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 				/*
5467c478bd9Sstevel@tonic-gate 				 * Check if information returned indicates one
5477c478bd9Sstevel@tonic-gate 				 * or more closed fd's; find and remove any such
5487c478bd9Sstevel@tonic-gate 				 * information
5497c478bd9Sstevel@tonic-gate 				 */
5507c478bd9Sstevel@tonic-gate 				for (i = 0; i <= npollfds; i++) {
5517c478bd9Sstevel@tonic-gate 					if (svc_pollset[i].revents & POLLNVAL) {
5527c478bd9Sstevel@tonic-gate 						/* Overwrite svc_pollset[i] */
5537c478bd9Sstevel@tonic-gate 						for (j = i; j < npollfds; j++)
5547c478bd9Sstevel@tonic-gate 							svc_pollset[j] =
5557c478bd9Sstevel@tonic-gate 							    svc_pollset[j + 1];
5567c478bd9Sstevel@tonic-gate 						(void) memset(&svc_pollset[j],
5577c478bd9Sstevel@tonic-gate 						    0, sizeof (struct pollfd));
5587c478bd9Sstevel@tonic-gate 						npollfds--;
5597c478bd9Sstevel@tonic-gate 						n_polled--;
5607c478bd9Sstevel@tonic-gate 						i--;
5617c478bd9Sstevel@tonic-gate 					}
5627c478bd9Sstevel@tonic-gate 				}
5637c478bd9Sstevel@tonic-gate 			} while (n_polled <= 0);
5647c478bd9Sstevel@tonic-gate 			svc_polling = FALSE;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			/*
5677c478bd9Sstevel@tonic-gate 			 * If there's data in the pipe, clear it.
5687c478bd9Sstevel@tonic-gate 			 */
5697c478bd9Sstevel@tonic-gate 			if (svc_pollset[npollfds].revents) {
5707c478bd9Sstevel@tonic-gate 				clear_pipe();
5717c478bd9Sstevel@tonic-gate 				n_polled--;
5727c478bd9Sstevel@tonic-gate 				svc_pollset[npollfds].revents = 0;
5737c478bd9Sstevel@tonic-gate 			}
5747c478bd9Sstevel@tonic-gate 			svc_polled = npollfds;
5757c478bd9Sstevel@tonic-gate 			svc_pollfds = n_polled;
5767c478bd9Sstevel@tonic-gate 			svc_next_pollfd = 0;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 			/*
5797c478bd9Sstevel@tonic-gate 			 * Check exit flag.
5807c478bd9Sstevel@tonic-gate 			 */
5817c478bd9Sstevel@tonic-gate 			if (svc_exit_done) {
5827c478bd9Sstevel@tonic-gate 				svc_thr_total--;
58361961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
58461961e0fSrobinson 				(void) mutex_unlock(&svc_thr_mutex);
5857c478bd9Sstevel@tonic-gate 				if (!main_thread) {
5867c478bd9Sstevel@tonic-gate 					thr_exit(NULL);
5877c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
5887c478bd9Sstevel@tonic-gate 				}
5897c478bd9Sstevel@tonic-gate 				break;
5907c478bd9Sstevel@tonic-gate 			}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 			/*
5937c478bd9Sstevel@tonic-gate 			 * If no descriptor is active, continue.
5947c478bd9Sstevel@tonic-gate 			 */
5957c478bd9Sstevel@tonic-gate 			if (svc_pollfds == 0)
5967c478bd9Sstevel@tonic-gate 				goto continue_with_locks;
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		/*
6007c478bd9Sstevel@tonic-gate 		 * If a file descriptor has already not been selected,
6017c478bd9Sstevel@tonic-gate 		 * choose a file descriptor.
6027c478bd9Sstevel@tonic-gate 		 * svc_pollfds and svc_next_pollfd are updated.
6037c478bd9Sstevel@tonic-gate 		 */
6047c478bd9Sstevel@tonic-gate 		if (myfd == -1) {
6057c478bd9Sstevel@tonic-gate 			if (select_next_pollfd(&myfd, &mypollfd) == -1)
6067c478bd9Sstevel@tonic-gate 				goto continue_with_locks;
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		/*
6107c478bd9Sstevel@tonic-gate 		 * Check to see if new threads need to be started.
6117c478bd9Sstevel@tonic-gate 		 * Count of threads that could be gainfully employed is
6127c478bd9Sstevel@tonic-gate 		 * obtained as follows:
6137c478bd9Sstevel@tonic-gate 		 *	- count 1 for poller
6147c478bd9Sstevel@tonic-gate 		 *	- count 1 for this request
6157c478bd9Sstevel@tonic-gate 		 *	- count active file descriptors (svc_pollfds)
6167c478bd9Sstevel@tonic-gate 		 *	- count pending file descriptors
6177c478bd9Sstevel@tonic-gate 		 *
6187c478bd9Sstevel@tonic-gate 		 * (svc_thr_total - svc_thr_active) are already available.
6197c478bd9Sstevel@tonic-gate 		 * This thread is one of the available threads.
6207c478bd9Sstevel@tonic-gate 		 *
6217c478bd9Sstevel@tonic-gate 		 * Number of new threads should not exceed
6227c478bd9Sstevel@tonic-gate 		 *	(svc_thr_max - svc_thr_total).
6237c478bd9Sstevel@tonic-gate 		 */
6247c478bd9Sstevel@tonic-gate 		if (svc_thr_total < svc_thr_max &&
6257c478bd9Sstevel@tonic-gate 		    svc_mt_mode == RPC_SVC_MT_AUTO && !svc_exit_done) {
6267c478bd9Sstevel@tonic-gate 			n_new = 1 + 1 + svc_pollfds + svc_total_pending -
6277c478bd9Sstevel@tonic-gate 			    (svc_thr_total - svc_thr_active);
6287c478bd9Sstevel@tonic-gate 			if (n_new > (svc_thr_max - svc_thr_total))
6297c478bd9Sstevel@tonic-gate 				n_new = svc_thr_max - svc_thr_total;
6307c478bd9Sstevel@tonic-gate 			if (n_new > 0)
6317c478bd9Sstevel@tonic-gate 				start_threads(n_new);
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		/*
6357c478bd9Sstevel@tonic-gate 		 * Get parent xprt.  It is possible for the parent service
6367c478bd9Sstevel@tonic-gate 		 * handle to be destroyed by now, due to a race condition.
6377c478bd9Sstevel@tonic-gate 		 * Check for this, and if so, log a warning and go on.
6387c478bd9Sstevel@tonic-gate 		 */
6397c478bd9Sstevel@tonic-gate 		parent_xprt = svc_xports[myfd];
6407c478bd9Sstevel@tonic-gate 		if (parent_xprt == NULL) {
6417c478bd9Sstevel@tonic-gate 			/* Check if it is not a user FD */
6427c478bd9Sstevel@tonic-gate 			if (__is_a_userfd(myfd) == TRUE)
6437c478bd9Sstevel@tonic-gate 				__svc_getreq_user(&(svc_pollset[mypollfd]));
6447c478bd9Sstevel@tonic-gate 			goto continue_with_locks;
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6477c478bd9Sstevel@tonic-gate 		if (svc_defunct(parent_xprt) || svc_failed(parent_xprt))
6487c478bd9Sstevel@tonic-gate 			goto continue_with_locks;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		/*
6517c478bd9Sstevel@tonic-gate 		 * Make a copy of parent xprt, update svc_fdset.
6527c478bd9Sstevel@tonic-gate 		 */
6537c478bd9Sstevel@tonic-gate 		if ((xprt = make_xprt_copy(parent_xprt)) == NULL)
6547c478bd9Sstevel@tonic-gate 			goto continue_with_locks;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		/*
6577c478bd9Sstevel@tonic-gate 		 * Keep track of active threads in automatic mode.
6587c478bd9Sstevel@tonic-gate 		 */
6597c478bd9Sstevel@tonic-gate 		if (svc_mt_mode == RPC_SVC_MT_AUTO)
6607c478bd9Sstevel@tonic-gate 			svc_thr_active++;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * Release mutexes so other threads can get going.
6647c478bd9Sstevel@tonic-gate 		 */
66561961e0fSrobinson 		(void) mutex_unlock(&svc_mutex);
66661961e0fSrobinson 		(void) mutex_unlock(&svc_thr_mutex);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		/*
6697c478bd9Sstevel@tonic-gate 		 * Process request.
6707c478bd9Sstevel@tonic-gate 		 */
6717c478bd9Sstevel@tonic-gate 		{
6727c478bd9Sstevel@tonic-gate 			struct rpc_msg *msg;
6737c478bd9Sstevel@tonic-gate 			struct svc_req *r;
6747c478bd9Sstevel@tonic-gate 			char *cred_area;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6777c478bd9Sstevel@tonic-gate 			msg = SVCEXT(xprt)->msg;
6787c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6797c478bd9Sstevel@tonic-gate 			r = SVCEXT(xprt)->req;
6807c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6817c478bd9Sstevel@tonic-gate 			cred_area = SVCEXT(xprt)->cred_area;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 			msg->rm_call.cb_cred.oa_base = cred_area;
6857c478bd9Sstevel@tonic-gate 			msg->rm_call.cb_verf.oa_base =
6867c478bd9Sstevel@tonic-gate 			    &(cred_area[MAX_AUTH_BYTES]);
6877c478bd9Sstevel@tonic-gate 			r->rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 			/*
6907c478bd9Sstevel@tonic-gate 			 * receive RPC message
6917c478bd9Sstevel@tonic-gate 			 */
6927c478bd9Sstevel@tonic-gate 			if ((dispatch = SVC_RECV(xprt, msg))) {
6937c478bd9Sstevel@tonic-gate 				if (svc_mt_mode != RPC_SVC_MT_NONE)
6947c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6957c478bd9Sstevel@tonic-gate 					svc_flags(xprt) |= SVC_ARGS_CHECK;
6967c478bd9Sstevel@tonic-gate 				dispatch = _svc_prog_dispatch(xprt, msg, r);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 				/*
6997c478bd9Sstevel@tonic-gate 				 * Call cleanup procedure if set.
7007c478bd9Sstevel@tonic-gate 				 */
7017c478bd9Sstevel@tonic-gate 				if (__proc_cleanup_cb != NULL)
7027c478bd9Sstevel@tonic-gate 					(*__proc_cleanup_cb)(xprt);
7037c478bd9Sstevel@tonic-gate 			} else
7047c478bd9Sstevel@tonic-gate 				svc_args_done(xprt);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 			/*
7077c478bd9Sstevel@tonic-gate 			 * Finish up, if automatic mode, or not dispatched.
7087c478bd9Sstevel@tonic-gate 			 */
7097c478bd9Sstevel@tonic-gate 			if (svc_mt_mode == RPC_SVC_MT_AUTO || !dispatch) {
7107c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
7117c478bd9Sstevel@tonic-gate 				if (svc_flags(xprt) & SVC_ARGS_CHECK)
7127c478bd9Sstevel@tonic-gate 					svc_args_done(xprt);
71361961e0fSrobinson 				(void) mutex_lock(&svc_mutex);
7147c478bd9Sstevel@tonic-gate 				_svc_done_private(xprt);
7157c478bd9Sstevel@tonic-gate 				if (svc_mt_mode == RPC_SVC_MT_AUTO) {
7167c478bd9Sstevel@tonic-gate 					/*
7177c478bd9Sstevel@tonic-gate 					 * not active any more
7187c478bd9Sstevel@tonic-gate 					 */
7197c478bd9Sstevel@tonic-gate 					svc_thr_active--;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 					/*
7227c478bd9Sstevel@tonic-gate 					 * If not main thread, exit unless
7237c478bd9Sstevel@tonic-gate 					 * there's some immediate work.
7247c478bd9Sstevel@tonic-gate 					 */
7257c478bd9Sstevel@tonic-gate 					if (!main_thread &&
7267c478bd9Sstevel@tonic-gate 					    svc_pollfds <= 0 &&
7277c478bd9Sstevel@tonic-gate 					    svc_total_pending <= 0 &&
7287c478bd9Sstevel@tonic-gate 					    (svc_polling ||
7297c478bd9Sstevel@tonic-gate 					    svc_waiters > 0)) {
7307c478bd9Sstevel@tonic-gate 						svc_thr_total--;
7317c478bd9Sstevel@tonic-gate 						if (svc_thr_total ==
7327c478bd9Sstevel@tonic-gate 						    svc_waiters) {
73361961e0fSrobinson 							(void) cond_broadcast(
7347c478bd9Sstevel@tonic-gate 							    &svc_thr_fdwait);
7357c478bd9Sstevel@tonic-gate 						}
73661961e0fSrobinson 						(void) mutex_unlock(&svc_mutex);
7377c478bd9Sstevel@tonic-gate 						thr_exit(NULL);
7387c478bd9Sstevel@tonic-gate 						/* NOTREACHED */
7397c478bd9Sstevel@tonic-gate 					}
7407c478bd9Sstevel@tonic-gate 				}
74161961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
7427c478bd9Sstevel@tonic-gate 			}
7437c478bd9Sstevel@tonic-gate 		}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate /*
7507c478bd9Sstevel@tonic-gate  * start_threads() - Start specified number of threads.
7517c478bd9Sstevel@tonic-gate  */
7527c478bd9Sstevel@tonic-gate static void
75361961e0fSrobinson start_threads(int num_threads)
7547c478bd9Sstevel@tonic-gate {
7557c478bd9Sstevel@tonic-gate 	int		i;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_mutex));
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_threads; i++) {
7607c478bd9Sstevel@tonic-gate 		if (thr_create(NULL, 0, (void *(*)(void *))_svc_run_mt, NULL,
7617c478bd9Sstevel@tonic-gate 		    THR_DETACHED, NULL) == 0) {
7627c478bd9Sstevel@tonic-gate 			svc_thr_total++;
7637c478bd9Sstevel@tonic-gate 			svc_thr_total_creates++;
7647c478bd9Sstevel@tonic-gate 		} else {
7657c478bd9Sstevel@tonic-gate 			svc_thr_total_create_errors++;
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate  * create_pipe() - create pipe for breaking out of poll.
7737c478bd9Sstevel@tonic-gate  */
7747c478bd9Sstevel@tonic-gate static void
77561961e0fSrobinson create_pipe(void)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	if (pipe(svc_pipe) == -1) {
7787c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(__nsl_dom,
7797c478bd9Sstevel@tonic-gate 		    "RPC: svc could not create pipe - exiting"));
7807c478bd9Sstevel@tonic-gate 		exit(1);
7817c478bd9Sstevel@tonic-gate 	}
782e8031f0aSraf 	if (fcntl(svc_pipe[0], F_SETFL, O_NONBLOCK) == -1) {
7837c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(__nsl_dom,
7847c478bd9Sstevel@tonic-gate 		    "RPC: svc pipe error - exiting"));
7857c478bd9Sstevel@tonic-gate 		exit(1);
7867c478bd9Sstevel@tonic-gate 	}
787e8031f0aSraf 	if (fcntl(svc_pipe[1], F_SETFL, O_NONBLOCK) == -1) {
7887c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(__nsl_dom,
7897c478bd9Sstevel@tonic-gate 		    "RPC: svc pipe error - exiting"));
7907c478bd9Sstevel@tonic-gate 		exit(1);
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate /*
7967c478bd9Sstevel@tonic-gate  * clear_pipe() - Empty data in pipe.
7977c478bd9Sstevel@tonic-gate  */
7987c478bd9Sstevel@tonic-gate static void
79961961e0fSrobinson clear_pipe(void)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	char	buf[16];
8027c478bd9Sstevel@tonic-gate 	int	i;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	do {
8057c478bd9Sstevel@tonic-gate 		i = read(svc_pipe[0], buf, sizeof (buf));
8067c478bd9Sstevel@tonic-gate 	} while (i == sizeof (buf));
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate  * select_next_pollfd() - Select the next active fd in svc_pollset.
8127c478bd9Sstevel@tonic-gate  */
8137c478bd9Sstevel@tonic-gate static int
8147c478bd9Sstevel@tonic-gate select_next_pollfd(int *fd, int *pollfdIndex)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	int i;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_thr_mutex));
8197c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_mutex));
8207c478bd9Sstevel@tonic-gate 
821*361f55a5SMarcel Telka 	for (i = svc_next_pollfd; svc_pollfds > 0 && i < svc_polled; i++) {
8227c478bd9Sstevel@tonic-gate 		if (svc_pollset[i].revents) {
8237c478bd9Sstevel@tonic-gate 			svc_pollfds--;
8247c478bd9Sstevel@tonic-gate 			/*
8257c478bd9Sstevel@tonic-gate 			 * No more special case for POLLNVAL, because it may
8267c478bd9Sstevel@tonic-gate 			 * be linked with a user file descriptot callback
8277c478bd9Sstevel@tonic-gate 			 */
8287c478bd9Sstevel@tonic-gate 			svc_next_pollfd = i + 1;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 			*fd = svc_pollset[i].fd;
8317c478bd9Sstevel@tonic-gate 			*pollfdIndex = i;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 			return (0);
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 	svc_next_pollfd = svc_pollfds = 0;
8377c478bd9Sstevel@tonic-gate 	*fd = -1;
8387c478bd9Sstevel@tonic-gate 	*pollfdIndex = INVALID_POLLFD;
8397c478bd9Sstevel@tonic-gate 	return (-1);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate  * make_xprt_copy() - make a copy of the parent xprt.
8457c478bd9Sstevel@tonic-gate  * Clear fd bit in svc_fdset.
8467c478bd9Sstevel@tonic-gate  */
8477c478bd9Sstevel@tonic-gate static SVCXPRT *
84861961e0fSrobinson make_xprt_copy(SVCXPRT *parent)
8497c478bd9Sstevel@tonic-gate {
8507c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8517c478bd9Sstevel@tonic-gate 	SVCXPRT_LIST	*xlist = SVCEXT(parent)->my_xlist;
8527c478bd9Sstevel@tonic-gate 	SVCXPRT_LIST	*xret;
8537c478bd9Sstevel@tonic-gate 	SVCXPRT		*xprt;
8547c478bd9Sstevel@tonic-gate 	int		fd = parent->xp_fd;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_mutex));
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	xret = xlist->next;
8597c478bd9Sstevel@tonic-gate 	if (xret) {
8607c478bd9Sstevel@tonic-gate 		xlist->next = xret->next;
8617c478bd9Sstevel@tonic-gate 		xret->next = NULL;
8627c478bd9Sstevel@tonic-gate 		xprt = xret->xprt;
8637c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8647c478bd9Sstevel@tonic-gate 		svc_flags(xprt) = svc_flags(parent);
8657c478bd9Sstevel@tonic-gate 	} else
8667c478bd9Sstevel@tonic-gate 		xprt = svc_copy(parent);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	if (xprt) {
8697c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8707c478bd9Sstevel@tonic-gate 		SVCEXT(parent)->refcnt++;
87161961e0fSrobinson 		(void) rw_wrlock(&svc_fd_lock);
8727c478bd9Sstevel@tonic-gate 		clear_pollfd(fd);
87361961e0fSrobinson 		(void) rw_unlock(&svc_fd_lock);
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 	return (xprt);
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate  * _svc_done_private() - return copies to library.
8807c478bd9Sstevel@tonic-gate  */
8817c478bd9Sstevel@tonic-gate static void
88261961e0fSrobinson _svc_done_private(SVCXPRT *xprt)
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate 	SVCXPRT		*parent;
8857c478bd9Sstevel@tonic-gate 	SVCXPRT_LIST	*xhead, *xlist;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_mutex));
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8907c478bd9Sstevel@tonic-gate 	if ((parent = SVCEXT(xprt)->parent) == NULL)
8917c478bd9Sstevel@tonic-gate 		return;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8947c478bd9Sstevel@tonic-gate 	xhead = SVCEXT(parent)->my_xlist;
8957c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8967c478bd9Sstevel@tonic-gate 	xlist = SVCEXT(xprt)->my_xlist;
8977c478bd9Sstevel@tonic-gate 	xlist->next = xhead->next;
8987c478bd9Sstevel@tonic-gate 	xhead->next = xlist;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9017c478bd9Sstevel@tonic-gate 	SVCEXT(parent)->refcnt--;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	/*
9047c478bd9Sstevel@tonic-gate 	 * Propagate any error flags.  This is done in both directions to
9057c478bd9Sstevel@tonic-gate 	 * ensure that if one child gets an error, everyone will see it
9067c478bd9Sstevel@tonic-gate 	 * (even if there are multiple outstanding children) and the
9077c478bd9Sstevel@tonic-gate 	 * transport will get closed.
9087c478bd9Sstevel@tonic-gate 	 */
9097c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9107c478bd9Sstevel@tonic-gate 	svc_flags(xprt) |= svc_flags(parent);
9117c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9127c478bd9Sstevel@tonic-gate 	if (svc_failed(xprt) || svc_defunct(xprt)) {
9137c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9147c478bd9Sstevel@tonic-gate 		svc_flags(parent) |= (svc_flags(xprt) &
9157c478bd9Sstevel@tonic-gate 		    (SVC_FAILED | SVC_DEFUNCT));
9167c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9177c478bd9Sstevel@tonic-gate 		if (SVCEXT(parent)->refcnt == 0)
9187c478bd9Sstevel@tonic-gate 			_svc_destroy_private(xprt);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate void
9237c478bd9Sstevel@tonic-gate svc_done(SVCXPRT *xprt)
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate 	if (svc_mt_mode != RPC_SVC_MT_USER)
9267c478bd9Sstevel@tonic-gate 		return;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * Make sure file descriptor is released in user mode.
9307c478bd9Sstevel@tonic-gate 	 * If the xprt is a door, do nothing: this work is performed by
9317c478bd9Sstevel@tonic-gate 	 * svc_door.c's return_xprt_copy() routine, which is basically a
9327c478bd9Sstevel@tonic-gate 	 * door-specific copy of _svc_done_private().
9337c478bd9Sstevel@tonic-gate 	 */
9347c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9357c478bd9Sstevel@tonic-gate 	if (svc_type(xprt) == SVC_DOOR)
9367c478bd9Sstevel@tonic-gate 		return;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9397c478bd9Sstevel@tonic-gate 	if (svc_flags(xprt) & SVC_ARGS_CHECK)
9407c478bd9Sstevel@tonic-gate 		svc_args_done(xprt);
9417c478bd9Sstevel@tonic-gate 
94261961e0fSrobinson 	(void) mutex_lock(&svc_mutex);
9437c478bd9Sstevel@tonic-gate 	_svc_done_private(xprt);
94461961e0fSrobinson 	(void) mutex_unlock(&svc_mutex);
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate /*
9497c478bd9Sstevel@tonic-gate  * Mark argument completion.  Release file descriptor.
9507c478bd9Sstevel@tonic-gate  */
9517c478bd9Sstevel@tonic-gate void
95261961e0fSrobinson svc_args_done(SVCXPRT *xprt)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	char	dummy;
9557c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9567c478bd9Sstevel@tonic-gate 	SVCXPRT	*parent = SVCEXT(xprt)->parent;
9577c478bd9Sstevel@tonic-gate 	bool_t	wake_up_poller;
9587c478bd9Sstevel@tonic-gate 	enum	xprt_stat stat;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9617c478bd9Sstevel@tonic-gate 	svc_flags(xprt) |= svc_flags(parent);
9627c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9637c478bd9Sstevel@tonic-gate 	svc_flags(xprt) &= ~SVC_ARGS_CHECK;
9647c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9657c478bd9Sstevel@tonic-gate 	if (svc_failed(xprt) || svc_defunct(parent))
9667c478bd9Sstevel@tonic-gate 		return;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9697c478bd9Sstevel@tonic-gate 	if (svc_type(xprt) == SVC_CONNECTION &&
9707c478bd9Sstevel@tonic-gate 	    (stat = SVC_STAT(xprt)) != XPRT_IDLE) {
9717c478bd9Sstevel@tonic-gate 		if (stat == XPRT_MOREREQS) {
97261961e0fSrobinson 			(void) mutex_lock(&svc_mutex);
9737c478bd9Sstevel@tonic-gate 			svc_pending_fds[svc_last_pending++] = xprt->xp_fd;
9747c478bd9Sstevel@tonic-gate 			if (svc_last_pending > CIRCULAR_BUFSIZE)
9757c478bd9Sstevel@tonic-gate 				svc_last_pending = 0;
9767c478bd9Sstevel@tonic-gate 			svc_total_pending++;
97761961e0fSrobinson 			(void) mutex_unlock(&svc_mutex);
9787c478bd9Sstevel@tonic-gate 			wake_up_poller = FALSE;
9797c478bd9Sstevel@tonic-gate 		} else {
9807c478bd9Sstevel@tonic-gate 			/*
9817c478bd9Sstevel@tonic-gate 			 * connection failed
9827c478bd9Sstevel@tonic-gate 			 */
9837c478bd9Sstevel@tonic-gate 			return;
9847c478bd9Sstevel@tonic-gate 		}
9857c478bd9Sstevel@tonic-gate 	} else {
98661961e0fSrobinson 		(void) rw_wrlock(&svc_fd_lock);
9877c478bd9Sstevel@tonic-gate 		set_pollfd(xprt->xp_fd, MASKVAL);
98861961e0fSrobinson 		(void) rw_unlock(&svc_fd_lock);
9897c478bd9Sstevel@tonic-gate 		wake_up_poller = TRUE;
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	if (!wake_up_poller || !svc_polling) {
9937c478bd9Sstevel@tonic-gate 		/*
9947c478bd9Sstevel@tonic-gate 		 * Wake up any waiting threads.
9957c478bd9Sstevel@tonic-gate 		 */
99661961e0fSrobinson 		(void) mutex_lock(&svc_mutex);
9977c478bd9Sstevel@tonic-gate 		if (svc_waiters > 0) {
99861961e0fSrobinson 			(void) cond_broadcast(&svc_thr_fdwait);
99961961e0fSrobinson 			(void) mutex_unlock(&svc_mutex);
10007c478bd9Sstevel@tonic-gate 			return;
10017c478bd9Sstevel@tonic-gate 		}
100261961e0fSrobinson 		(void) mutex_unlock(&svc_mutex);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * Wake up any polling thread.
10077c478bd9Sstevel@tonic-gate 	 */
10087c478bd9Sstevel@tonic-gate 	if (svc_polling)
10097c478bd9Sstevel@tonic-gate 		(void) write(svc_pipe[1], &dummy, sizeof (dummy));
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate int
10147c478bd9Sstevel@tonic-gate __rpc_legal_connmaxrec(int suggested) {
10157c478bd9Sstevel@tonic-gate 	if (suggested == -1) {
10167c478bd9Sstevel@tonic-gate 		/* Supply default */
10177c478bd9Sstevel@tonic-gate 		return (RPC_MAXDATASIZE + 2*sizeof (uint32_t));
10187c478bd9Sstevel@tonic-gate 	} else if (suggested < 0) {
10197c478bd9Sstevel@tonic-gate 		return (-1);
10207c478bd9Sstevel@tonic-gate 	} else if (suggested > 0) {
10217c478bd9Sstevel@tonic-gate 		/* Round down to multiple of BYTES_PER_XDR_UNIT */
10227c478bd9Sstevel@tonic-gate 		suggested -= suggested % BYTES_PER_XDR_UNIT;
10237c478bd9Sstevel@tonic-gate 		/* If possible, allow for two fragment headers */
10247c478bd9Sstevel@tonic-gate 		if (suggested < MAXINT-(2*sizeof (uint32_t))) {
10257c478bd9Sstevel@tonic-gate 			/* Allow for two fragment headers */
10267c478bd9Sstevel@tonic-gate 			suggested += 2 * sizeof (uint32_t);
10277c478bd9Sstevel@tonic-gate 		} else {
10287c478bd9Sstevel@tonic-gate 			suggested = MAXINT;
10297c478bd9Sstevel@tonic-gate 		}
10307c478bd9Sstevel@tonic-gate 		if (suggested < sizeof (struct rpc_msg)) {
10317c478bd9Sstevel@tonic-gate 			return (-1);
10327c478bd9Sstevel@tonic-gate 		}
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 	return (suggested);
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate bool_t
103961961e0fSrobinson rpc_control(int op, void *info)
10407c478bd9Sstevel@tonic-gate {
10417c478bd9Sstevel@tonic-gate 	int		tmp;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	switch (op) {
10447c478bd9Sstevel@tonic-gate 	case RPC_SVC_MTMODE_SET:
10457c478bd9Sstevel@tonic-gate 		tmp = *((int *)info);
10467c478bd9Sstevel@tonic-gate 		if (tmp != RPC_SVC_MT_NONE && tmp != RPC_SVC_MT_AUTO &&
10477c478bd9Sstevel@tonic-gate 		    tmp != RPC_SVC_MT_USER)
10487c478bd9Sstevel@tonic-gate 			return (FALSE);
10497c478bd9Sstevel@tonic-gate 		if (svc_mt_mode != RPC_SVC_MT_NONE && svc_mt_mode != tmp)
10507c478bd9Sstevel@tonic-gate 			return (FALSE);
10517c478bd9Sstevel@tonic-gate 		svc_mt_mode = tmp;
10527c478bd9Sstevel@tonic-gate 		return (TRUE);
10537c478bd9Sstevel@tonic-gate 	case RPC_SVC_MTMODE_GET:
10547c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_mt_mode;
10557c478bd9Sstevel@tonic-gate 		return (TRUE);
10567c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRMAX_SET:
10577c478bd9Sstevel@tonic-gate 		if ((tmp = *((int *)info)) < 1)
10587c478bd9Sstevel@tonic-gate 			return (FALSE);
105961961e0fSrobinson 		(void) mutex_lock(&svc_mutex);
10607c478bd9Sstevel@tonic-gate 		svc_thr_max = tmp;
106161961e0fSrobinson 		(void) mutex_unlock(&svc_mutex);
10627c478bd9Sstevel@tonic-gate 		return (TRUE);
10637c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRMAX_GET:
10647c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_thr_max;
10657c478bd9Sstevel@tonic-gate 		return (TRUE);
10667c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRTOTAL_GET:
10677c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_thr_total;
10687c478bd9Sstevel@tonic-gate 		return (TRUE);
10697c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRCREATES_GET:
10707c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_thr_total_creates;
10717c478bd9Sstevel@tonic-gate 		return (TRUE);
10727c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRERRORS_GET:
10737c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_thr_total_create_errors;
10747c478bd9Sstevel@tonic-gate 		return (TRUE);
10757c478bd9Sstevel@tonic-gate 	case RPC_SVC_USE_POLLFD:
10767c478bd9Sstevel@tonic-gate 		if (*((int *)info) && !__rpc_use_pollfd_done) {
10777c478bd9Sstevel@tonic-gate 			__rpc_use_pollfd_done = 1;
10787c478bd9Sstevel@tonic-gate 			return (TRUE);
10797c478bd9Sstevel@tonic-gate 		}
10807c478bd9Sstevel@tonic-gate 		return (FALSE);
10817c478bd9Sstevel@tonic-gate 	case RPC_SVC_CONNMAXREC_SET:
10827c478bd9Sstevel@tonic-gate 		tmp = __rpc_legal_connmaxrec(*(int *)info);
10837c478bd9Sstevel@tonic-gate 		if (tmp >= 0) {
10847c478bd9Sstevel@tonic-gate 			__rpc_connmaxrec = tmp;
10857c478bd9Sstevel@tonic-gate 			return (TRUE);
10867c478bd9Sstevel@tonic-gate 		} else {
10877c478bd9Sstevel@tonic-gate 			return (FALSE);
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 	case RPC_SVC_CONNMAXREC_GET:
10907c478bd9Sstevel@tonic-gate 		*((int *)info) = __rpc_connmaxrec;
10917c478bd9Sstevel@tonic-gate 		return (TRUE);
10927c478bd9Sstevel@tonic-gate 	case RPC_SVC_IRTIMEOUT_SET:
10937c478bd9Sstevel@tonic-gate 		tmp = *((int *)info);
10947c478bd9Sstevel@tonic-gate 		if (tmp >= 0) {
10957c478bd9Sstevel@tonic-gate 			__rpc_irtimeout = tmp;
10967c478bd9Sstevel@tonic-gate 			return (TRUE);
10977c478bd9Sstevel@tonic-gate 		} else {
10987c478bd9Sstevel@tonic-gate 			return (FALSE);
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 	/*
11017c478bd9Sstevel@tonic-gate 	 * No mutex necessary as _EXCLBIND_SET will/should only
11027c478bd9Sstevel@tonic-gate 	 * be used before an RPC daemon goes mt-hot.
11037c478bd9Sstevel@tonic-gate 	 */
11047c478bd9Sstevel@tonic-gate 	case __RPC_SVC_EXCLBIND_SET:
11057c478bd9Sstevel@tonic-gate 		if (info) {
11067c478bd9Sstevel@tonic-gate 			__rpc_tp_exclbind = *((bool_t *)info);
11077c478bd9Sstevel@tonic-gate 			return (TRUE);
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 		return (FALSE);
11107c478bd9Sstevel@tonic-gate 	case __RPC_SVC_EXCLBIND_GET:
11117c478bd9Sstevel@tonic-gate 		if (info) {
11127c478bd9Sstevel@tonic-gate 			*((bool_t *)info) = __rpc_tp_exclbind;
11137c478bd9Sstevel@tonic-gate 			return (TRUE);
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 		return (FALSE);
11167c478bd9Sstevel@tonic-gate 
1117*361f55a5SMarcel Telka 	case __RPC_SVC_LSTNBKLOG_SET:
1118*361f55a5SMarcel Telka 		tmp = *(int *)info;
1119*361f55a5SMarcel Telka 		if (tmp > 0) {
1120*361f55a5SMarcel Telka 			__svc_lstnbklog = tmp;
1121*361f55a5SMarcel Telka 			return (TRUE);
1122*361f55a5SMarcel Telka 		}
1123*361f55a5SMarcel Telka 		return (FALSE);
1124*361f55a5SMarcel Telka 	case __RPC_SVC_LSTNBKLOG_GET:
1125*361f55a5SMarcel Telka 		*(int *)info = __svc_lstnbklog;
1126*361f55a5SMarcel Telka 		return (TRUE);
1127*361f55a5SMarcel Telka 
11287c478bd9Sstevel@tonic-gate 	default:
11297c478bd9Sstevel@tonic-gate 		return (FALSE);
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate }
1132