xref: /illumos-gate/usr/src/lib/libnsl/rpc/svc_run.c (revision e2f4f3da)
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 
101*361f55a5SMarcel Telka /*
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 
1067c478bd9Sstevel@tonic-gate /*
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 /* VARIABLES PROTECTED BY svc_mutex:
1887c478bd9Sstevel@tonic-gate 	svc_thr_total, svc_thr_active, svc_pending_fds, svc_next_pending,
1897c478bd9Sstevel@tonic-gate 	svc_last_pending, svc_total_pending, svc_thr_total_creates,
1907c478bd9Sstevel@tonic-gate 	svc_thr_total_create_errors,
1917c478bd9Sstevel@tonic-gate 	svcxprt_list_t::next, svcxprt_ext_t::my_xlist,
1927c478bd9Sstevel@tonic-gate 	svc_thr_max, svc_waiters
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_fd_lock:
1967c478bd9Sstevel@tonic-gate 	svc_xports, svc_fdset, svc_nfds, svc_nfds_set, svc_max_fd,
1977c478bd9Sstevel@tonic-gate 	svc_pollfd, svc_npollfds, svc_npollfds_set, svc_max_pollfd
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_thr_mutex:
2017c478bd9Sstevel@tonic-gate 	svc_pollset, svc_pollfds, svc_next_pollfd, svc_polling
2027c478bd9Sstevel@tonic-gate 	svc_pollset_allocd, svc_polled
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_exit_mutex:
2067c478bd9Sstevel@tonic-gate 	svc_exit_done
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /* VARIABLES READABLE WITHOUT LOCK:
2107c478bd9Sstevel@tonic-gate 	svc_thr_total, svc_thr_active, svc_thr_total_creates,
2117c478bd9Sstevel@tonic-gate 	svc_thr_total_create_errors,
2127c478bd9Sstevel@tonic-gate 	svc_xports, svc_nfds, svc_nfds_set, svc_max_fd,
2137c478bd9Sstevel@tonic-gate 	svc_npollfds, svc_npollfds_set, svc_max_pollfd,
2147c478bd9Sstevel@tonic-gate 	svc_pollfds, svc_next_pollfd, svc_exit_done, svc_polling,
2157c478bd9Sstevel@tonic-gate 	svc_thr_max, svc_waiters
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY "program_logic":
2197c478bd9Sstevel@tonic-gate 	rpc_msg::, svc_req::, svcxprt_ext_t::flags, svc_mt_mode,
2207c478bd9Sstevel@tonic-gate 	svcxprt_ext_t::parent
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /* LOCK ORDER:
2247c478bd9Sstevel@tonic-gate 	svc_exit_mutex, svc_thr_mutex, svc_mutex, svc_fd_lock
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate void
svc_run(void)22961961e0fSrobinson svc_run(void)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	/* NO OTHER THREADS ARE RUNNING */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	svc_exit_done = FALSE;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	while ((svc_npollfds > 0 || svc_ndoorfds > 0) && !svc_exit_done) {
2367c478bd9Sstevel@tonic-gate 		if (svc_npollfds > 0) {
2377c478bd9Sstevel@tonic-gate 			switch (svc_mt_mode) {
2387c478bd9Sstevel@tonic-gate 			case RPC_SVC_MT_NONE:
2397c478bd9Sstevel@tonic-gate 				_svc_run();
2407c478bd9Sstevel@tonic-gate 				break;
2417c478bd9Sstevel@tonic-gate 			default:
2427c478bd9Sstevel@tonic-gate 				_svc_run_mt();
2437c478bd9Sstevel@tonic-gate 				break;
2447c478bd9Sstevel@tonic-gate 			}
2457c478bd9Sstevel@tonic-gate 			continue;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 
24861961e0fSrobinson 		(void) mutex_lock(&svc_door_mutex);
2497c478bd9Sstevel@tonic-gate 		if (svc_ndoorfds > 0)
25061961e0fSrobinson 			(void) cond_wait(&svc_door_waitcv, &svc_door_mutex);
25161961e0fSrobinson 		(void) mutex_unlock(&svc_door_mutex);
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  *	This function causes svc_run() to exit by destroying all
2587c478bd9Sstevel@tonic-gate  *	service handles.
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate void
svc_exit(void)26161961e0fSrobinson svc_exit(void)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	SVCXPRT	*xprt;
2647c478bd9Sstevel@tonic-gate 	int fd;
2657c478bd9Sstevel@tonic-gate 	char dummy;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* NO LOCKS HELD */
2687c478bd9Sstevel@tonic-gate 
26961961e0fSrobinson 	(void) mutex_lock(&svc_exit_mutex);
2707c478bd9Sstevel@tonic-gate 	if (svc_exit_done) {
27161961e0fSrobinson 		(void) mutex_unlock(&svc_exit_mutex);
2727c478bd9Sstevel@tonic-gate 		return;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	svc_exit_done = TRUE;
2757c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < svc_max_pollfd; fd++) {
2767c478bd9Sstevel@tonic-gate 		xprt = svc_xports[fd];
2777c478bd9Sstevel@tonic-gate 		if (xprt) {
2787c478bd9Sstevel@tonic-gate 			SVC_DESTROY(xprt);
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	__svc_free_xprtlist();
2827c478bd9Sstevel@tonic-gate 	__svc_cleanup_door_xprts();
28361961e0fSrobinson 	(void) mutex_unlock(&svc_exit_mutex);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (svc_mt_mode != RPC_SVC_MT_NONE) {
28661961e0fSrobinson 		(void) mutex_lock(&svc_mutex);
28761961e0fSrobinson 		(void) cond_broadcast(&svc_thr_fdwait);
28861961e0fSrobinson 		(void) mutex_unlock(&svc_mutex);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		(void) write(svc_pipe[1], &dummy, sizeof (dummy));
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
29361961e0fSrobinson 	(void) mutex_lock(&svc_door_mutex);
29461961e0fSrobinson 	(void) cond_signal(&svc_door_waitcv);	/* wake up door dispatching */
29561961e0fSrobinson 	(void) mutex_unlock(&svc_door_mutex);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* destroy reactor information if any */
2987c478bd9Sstevel@tonic-gate 	__destroy_userfd();
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * this funtion is called with svc_fd_lock and svc_thr_mutex
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static int
alloc_pollset(int npollfds)3077c478bd9Sstevel@tonic-gate alloc_pollset(int npollfds)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	if (npollfds > svc_pollset_allocd) {
3107c478bd9Sstevel@tonic-gate 		pollfd_t *tmp;
3117c478bd9Sstevel@tonic-gate 		do {
3127c478bd9Sstevel@tonic-gate 			svc_pollset_allocd += POLLSET_EXTEND;
3137c478bd9Sstevel@tonic-gate 		} while (npollfds > svc_pollset_allocd);
3147c478bd9Sstevel@tonic-gate 		tmp = realloc(svc_pollset,
315*361f55a5SMarcel Telka 		    sizeof (pollfd_t) * svc_pollset_allocd);
3167c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
3177c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "alloc_pollset: out of memory");
3187c478bd9Sstevel@tonic-gate 			return (-1);
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 		svc_pollset = tmp;
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 	return (0);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate static void
_svc_run(void)32661961e0fSrobinson _svc_run(void)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	sigset_t set, oldset;
3297c478bd9Sstevel@tonic-gate 	int npollfds;
3307c478bd9Sstevel@tonic-gate 	int i;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Block SIGALRM while doing work.  Unblock it while doing poll().
3347c478bd9Sstevel@tonic-gate 	 * This is so that services like rpc.rstatd can cause the poll()
3357c478bd9Sstevel@tonic-gate 	 * to be interrupted due to alarm() but that we don't end up in
3367c478bd9Sstevel@tonic-gate 	 * an MT-unsafe signal handler at an inopportune time.
3377c478bd9Sstevel@tonic-gate 	 */
338e8031f0aSraf 	(void) sigemptyset(&set);
339e8031f0aSraf 	(void) sigaddset(&set, SIGALRM);
340e8031f0aSraf 	(void) sigprocmask(SIG_BLOCK, &set, &oldset);
3417c478bd9Sstevel@tonic-gate 	while (!svc_exit_done) {
3427c478bd9Sstevel@tonic-gate 		/*
3437c478bd9Sstevel@tonic-gate 		 * Check whether there is any server fd on which we may want
3447c478bd9Sstevel@tonic-gate 		 * to wait.
3457c478bd9Sstevel@tonic-gate 		 */
34661961e0fSrobinson 		(void) rw_rdlock(&svc_fd_lock);
3477c478bd9Sstevel@tonic-gate 		if (alloc_pollset(svc_npollfds) == -1)
3487c478bd9Sstevel@tonic-gate 			break;
3497c478bd9Sstevel@tonic-gate 		npollfds = __rpc_compress_pollfd(svc_max_pollfd,
350*361f55a5SMarcel Telka 		    svc_pollfd, svc_pollset);
35161961e0fSrobinson 		(void) rw_unlock(&svc_fd_lock);
3527c478bd9Sstevel@tonic-gate 		if (npollfds == 0)
3537c478bd9Sstevel@tonic-gate 			break;	/* None waiting, hence return */
3547c478bd9Sstevel@tonic-gate 
355e8031f0aSraf 		(void) sigprocmask(SIG_SETMASK, &oldset, NULL);
3567c478bd9Sstevel@tonic-gate 		i = poll(svc_pollset, npollfds, -1);
357e8031f0aSraf 		(void) sigprocmask(SIG_BLOCK, &set, &oldset);
3587c478bd9Sstevel@tonic-gate 		switch (i) {
3597c478bd9Sstevel@tonic-gate 		case -1:
3607c478bd9Sstevel@tonic-gate 			/*
3617c478bd9Sstevel@tonic-gate 			 * We ignore all errors, continuing with the assumption
3627c478bd9Sstevel@tonic-gate 			 * that it was set by the signal handlers (or any
3637c478bd9Sstevel@tonic-gate 			 * other outside event) and not caused by poll().
3647c478bd9Sstevel@tonic-gate 			 */
3657c478bd9Sstevel@tonic-gate 		case 0:
3667c478bd9Sstevel@tonic-gate 			continue;
3677c478bd9Sstevel@tonic-gate 		default:
3687c478bd9Sstevel@tonic-gate 			svc_getreq_poll(svc_pollset, i);
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 	}
371e8031f0aSraf 	(void) sigprocmask(SIG_SETMASK, &oldset, NULL);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * In _svc_run_mt, myfd is linked with mypollfd
3767c478bd9Sstevel@tonic-gate  * svc_pollset[mypollfd].fd == myfd
3777c478bd9Sstevel@tonic-gate  * However, in some cases, the link can not be made, thus we define the
3787c478bd9Sstevel@tonic-gate  * following values for these special cases
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate enum {
3817c478bd9Sstevel@tonic-gate 	INVALID_POLLFD	= -200,
3827c478bd9Sstevel@tonic-gate 	FD_FROM_PENDING
3837c478bd9Sstevel@tonic-gate };
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate static void
_svc_run_mt(void)38661961e0fSrobinson _svc_run_mt(void)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	int npollfds;
3897c478bd9Sstevel@tonic-gate 	int n_polled, dispatch;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	static bool_t first_time = TRUE;
3927c478bd9Sstevel@tonic-gate 	bool_t main_thread = FALSE;
3937c478bd9Sstevel@tonic-gate 	int n_new;
3947c478bd9Sstevel@tonic-gate 	int myfd, mypollfd;
3957c478bd9Sstevel@tonic-gate 	SVCXPRT *parent_xprt, *xprt;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 * Server is multi-threaded.  Do "first time" initializations.
3997c478bd9Sstevel@tonic-gate 	 * Since only one thread exists in the beginning, there's no
4007c478bd9Sstevel@tonic-gate 	 * need for mutex protection for first time initializations.
4017c478bd9Sstevel@tonic-gate 	 */
4027c478bd9Sstevel@tonic-gate 	if (first_time) {
4037c478bd9Sstevel@tonic-gate 		first_time = FALSE;
4047c478bd9Sstevel@tonic-gate 		main_thread = TRUE;
4057c478bd9Sstevel@tonic-gate 		svc_thr_total = 1;	/* this thread */
4067c478bd9Sstevel@tonic-gate 		svc_next_pending = svc_last_pending = 0;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		/*
4097c478bd9Sstevel@tonic-gate 		 * Create a pipe for waking up the poll, if new
4107c478bd9Sstevel@tonic-gate 		 * descriptors have been added to svc_fdset.
4117c478bd9Sstevel@tonic-gate 		 */
4127c478bd9Sstevel@tonic-gate 		create_pipe();
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/* OTHER THREADS ARE RUNNING */
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (svc_exit_done)
4187c478bd9Sstevel@tonic-gate 		return;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	for (;;) {
4217c478bd9Sstevel@tonic-gate 		/*
4227c478bd9Sstevel@tonic-gate 		 * svc_thr_mutex prevents more than one thread from
4237c478bd9Sstevel@tonic-gate 		 * trying to select a descriptor to process further.
4247c478bd9Sstevel@tonic-gate 		 * svc_thr_mutex is unlocked after a thread selects
4257c478bd9Sstevel@tonic-gate 		 * a descriptor on which to receive data.  If there are
4267c478bd9Sstevel@tonic-gate 		 * no such descriptors, the thread will poll with
4277c478bd9Sstevel@tonic-gate 		 * svc_thr_mutex locked, after unlocking all other
4287c478bd9Sstevel@tonic-gate 		 * locks.  This prevents more than one thread from
4297c478bd9Sstevel@tonic-gate 		 * trying to poll at the same time.
4307c478bd9Sstevel@tonic-gate 		 */
43161961e0fSrobinson 		(void) mutex_lock(&svc_thr_mutex);
43261961e0fSrobinson 		(void) mutex_lock(&svc_mutex);
4337c478bd9Sstevel@tonic-gate continue_with_locks:
4347c478bd9Sstevel@tonic-gate 		myfd = -1;
4357c478bd9Sstevel@tonic-gate 		mypollfd = INVALID_POLLFD;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 		/*
4387c478bd9Sstevel@tonic-gate 		 * Check if there are any descriptors with data pending.
4397c478bd9Sstevel@tonic-gate 		 */
4407c478bd9Sstevel@tonic-gate 		if (svc_total_pending > 0) {
4417c478bd9Sstevel@tonic-gate 			myfd = svc_pending_fds[svc_next_pending++];
4427c478bd9Sstevel@tonic-gate 			mypollfd = FD_FROM_PENDING;
4437c478bd9Sstevel@tonic-gate 			if (svc_next_pending > CIRCULAR_BUFSIZE)
4447c478bd9Sstevel@tonic-gate 				svc_next_pending = 0;
4457c478bd9Sstevel@tonic-gate 			svc_total_pending--;
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		/*
4497c478bd9Sstevel@tonic-gate 		 * Get the next active file descriptor to process.
4507c478bd9Sstevel@tonic-gate 		 */
4517c478bd9Sstevel@tonic-gate 		if (myfd == -1 && svc_pollfds == 0) {
4527c478bd9Sstevel@tonic-gate 			/*
4537c478bd9Sstevel@tonic-gate 			 * svc_pollset is empty; do polling
4547c478bd9Sstevel@tonic-gate 			 */
4557c478bd9Sstevel@tonic-gate 			svc_polling = TRUE;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 			/*
4587c478bd9Sstevel@tonic-gate 			 * if there are no file descriptors, return
4597c478bd9Sstevel@tonic-gate 			 */
46061961e0fSrobinson 			(void) rw_rdlock(&svc_fd_lock);
4617c478bd9Sstevel@tonic-gate 			if (svc_npollfds == 0 ||
462*361f55a5SMarcel Telka 			    alloc_pollset(svc_npollfds + 1) == -1) {
46361961e0fSrobinson 				(void) rw_unlock(&svc_fd_lock);
4647c478bd9Sstevel@tonic-gate 				svc_polling = FALSE;
4657c478bd9Sstevel@tonic-gate 				svc_thr_total--;
46661961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
46761961e0fSrobinson 				(void) mutex_unlock(&svc_thr_mutex);
4687c478bd9Sstevel@tonic-gate 				if (!main_thread) {
4697c478bd9Sstevel@tonic-gate 					thr_exit(NULL);
4707c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
4717c478bd9Sstevel@tonic-gate 				}
4727c478bd9Sstevel@tonic-gate 				break;
4737c478bd9Sstevel@tonic-gate 			}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			npollfds = __rpc_compress_pollfd(svc_max_pollfd,
476*361f55a5SMarcel Telka 			    svc_pollfd, svc_pollset);
47761961e0fSrobinson 			(void) rw_unlock(&svc_fd_lock);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 			if (npollfds == 0) {
4807c478bd9Sstevel@tonic-gate 				/*
4817c478bd9Sstevel@tonic-gate 				 * There are file descriptors, but none of them
4827c478bd9Sstevel@tonic-gate 				 * are available for polling.  If this is the
4837c478bd9Sstevel@tonic-gate 				 * main thread, or if no thread is waiting,
4847c478bd9Sstevel@tonic-gate 				 * wait on condition variable, otherwise exit.
4857c478bd9Sstevel@tonic-gate 				 */
4867c478bd9Sstevel@tonic-gate 				svc_polling = FALSE;
48761961e0fSrobinson 				(void) mutex_unlock(&svc_thr_mutex);
4887c478bd9Sstevel@tonic-gate 				if ((!main_thread) && svc_waiters > 0) {
4897c478bd9Sstevel@tonic-gate 					svc_thr_total--;
49061961e0fSrobinson 					(void) mutex_unlock(&svc_mutex);
4917c478bd9Sstevel@tonic-gate 					thr_exit(NULL);
4927c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
4937c478bd9Sstevel@tonic-gate 				}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 				while (svc_npollfds_set == 0 &&
496*361f55a5SMarcel Telka 				    svc_pollfds == 0 &&
497*361f55a5SMarcel Telka 				    svc_total_pending == 0 &&
498*361f55a5SMarcel Telka 				    !svc_exit_done) {
4997c478bd9Sstevel@tonic-gate 					svc_waiters++;
50061961e0fSrobinson 					(void) cond_wait(&svc_thr_fdwait,
501*361f55a5SMarcel Telka 					    &svc_mutex);
5027c478bd9Sstevel@tonic-gate 					svc_waiters--;
5037c478bd9Sstevel@tonic-gate 				}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 				/*
5067c478bd9Sstevel@tonic-gate 				 * Check exit flag.  If this is not the main
5077c478bd9Sstevel@tonic-gate 				 * thread, exit.
5087c478bd9Sstevel@tonic-gate 				 */
5097c478bd9Sstevel@tonic-gate 				if (svc_exit_done) {
5107c478bd9Sstevel@tonic-gate 					svc_thr_total--;
51161961e0fSrobinson 					(void) mutex_unlock(&svc_mutex);
5127c478bd9Sstevel@tonic-gate 					if (!main_thread)
5137c478bd9Sstevel@tonic-gate 						thr_exit(NULL);
5147c478bd9Sstevel@tonic-gate 					break;
5157c478bd9Sstevel@tonic-gate 				}
5167c478bd9Sstevel@tonic-gate 
51761961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
5187c478bd9Sstevel@tonic-gate 				continue;
5197c478bd9Sstevel@tonic-gate 			}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 			/*
5227c478bd9Sstevel@tonic-gate 			 * We're ready to poll.  Always set svc_pipe[0]
5237c478bd9Sstevel@tonic-gate 			 * as the last one, since the poll will occasionally
5247c478bd9Sstevel@tonic-gate 			 * need to be interrupted.  Release svc_mutex for
5257c478bd9Sstevel@tonic-gate 			 * the duration of the poll, but hold on to
5267c478bd9Sstevel@tonic-gate 			 * svc_thr_mutex, as we don't want any other thread
5277c478bd9Sstevel@tonic-gate 			 * to do the same.
5287c478bd9Sstevel@tonic-gate 			 */
5297c478bd9Sstevel@tonic-gate 			svc_pollset[npollfds].fd = svc_pipe[0];
5307c478bd9Sstevel@tonic-gate 			svc_pollset[npollfds].events = MASKVAL;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 			do {
5337c478bd9Sstevel@tonic-gate 				int i, j;
5347c478bd9Sstevel@tonic-gate 
53561961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
5367c478bd9Sstevel@tonic-gate 				n_polled = poll(svc_pollset, npollfds + 1, -1);
53761961e0fSrobinson 				(void) mutex_lock(&svc_mutex);
5387c478bd9Sstevel@tonic-gate 				if (n_polled <= 0)
5397c478bd9Sstevel@tonic-gate 					continue;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 				/*
5427c478bd9Sstevel@tonic-gate 				 * Check if information returned indicates one
5437c478bd9Sstevel@tonic-gate 				 * or more closed fd's; find and remove any such
5447c478bd9Sstevel@tonic-gate 				 * information
5457c478bd9Sstevel@tonic-gate 				 */
5467c478bd9Sstevel@tonic-gate 				for (i = 0; i <= npollfds; i++) {
5477c478bd9Sstevel@tonic-gate 					if (svc_pollset[i].revents & POLLNVAL) {
5487c478bd9Sstevel@tonic-gate 						/* Overwrite svc_pollset[i] */
5497c478bd9Sstevel@tonic-gate 						for (j = i; j < npollfds; j++)
5507c478bd9Sstevel@tonic-gate 							svc_pollset[j] =
5517c478bd9Sstevel@tonic-gate 							    svc_pollset[j + 1];
5527c478bd9Sstevel@tonic-gate 						(void) memset(&svc_pollset[j],
5537c478bd9Sstevel@tonic-gate 						    0, sizeof (struct pollfd));
5547c478bd9Sstevel@tonic-gate 						npollfds--;
5557c478bd9Sstevel@tonic-gate 						n_polled--;
5567c478bd9Sstevel@tonic-gate 						i--;
5577c478bd9Sstevel@tonic-gate 					}
5587c478bd9Sstevel@tonic-gate 				}
5597c478bd9Sstevel@tonic-gate 			} while (n_polled <= 0);
5607c478bd9Sstevel@tonic-gate 			svc_polling = FALSE;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 			/*
5637c478bd9Sstevel@tonic-gate 			 * If there's data in the pipe, clear it.
5647c478bd9Sstevel@tonic-gate 			 */
5657c478bd9Sstevel@tonic-gate 			if (svc_pollset[npollfds].revents) {
5667c478bd9Sstevel@tonic-gate 				clear_pipe();
5677c478bd9Sstevel@tonic-gate 				n_polled--;
5687c478bd9Sstevel@tonic-gate 				svc_pollset[npollfds].revents = 0;
5697c478bd9Sstevel@tonic-gate 			}
5707c478bd9Sstevel@tonic-gate 			svc_polled = npollfds;
5717c478bd9Sstevel@tonic-gate 			svc_pollfds = n_polled;
5727c478bd9Sstevel@tonic-gate 			svc_next_pollfd = 0;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 			/*
5757c478bd9Sstevel@tonic-gate 			 * Check exit flag.
5767c478bd9Sstevel@tonic-gate 			 */
5777c478bd9Sstevel@tonic-gate 			if (svc_exit_done) {
5787c478bd9Sstevel@tonic-gate 				svc_thr_total--;
57961961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
58061961e0fSrobinson 				(void) mutex_unlock(&svc_thr_mutex);
5817c478bd9Sstevel@tonic-gate 				if (!main_thread) {
5827c478bd9Sstevel@tonic-gate 					thr_exit(NULL);
5837c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
5847c478bd9Sstevel@tonic-gate 				}
5857c478bd9Sstevel@tonic-gate 				break;
5867c478bd9Sstevel@tonic-gate 			}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 			/*
5897c478bd9Sstevel@tonic-gate 			 * If no descriptor is active, continue.
5907c478bd9Sstevel@tonic-gate 			 */
5917c478bd9Sstevel@tonic-gate 			if (svc_pollfds == 0)
5927c478bd9Sstevel@tonic-gate 				goto continue_with_locks;
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		/*
5967c478bd9Sstevel@tonic-gate 		 * If a file descriptor has already not been selected,
5977c478bd9Sstevel@tonic-gate 		 * choose a file descriptor.
5987c478bd9Sstevel@tonic-gate 		 * svc_pollfds and svc_next_pollfd are updated.
5997c478bd9Sstevel@tonic-gate 		 */
6007c478bd9Sstevel@tonic-gate 		if (myfd == -1) {
6017c478bd9Sstevel@tonic-gate 			if (select_next_pollfd(&myfd, &mypollfd) == -1)
6027c478bd9Sstevel@tonic-gate 				goto continue_with_locks;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		/*
6067c478bd9Sstevel@tonic-gate 		 * Check to see if new threads need to be started.
6077c478bd9Sstevel@tonic-gate 		 * Count of threads that could be gainfully employed is
6087c478bd9Sstevel@tonic-gate 		 * obtained as follows:
6097c478bd9Sstevel@tonic-gate 		 *	- count 1 for poller
6107c478bd9Sstevel@tonic-gate 		 *	- count 1 for this request
6117c478bd9Sstevel@tonic-gate 		 *	- count active file descriptors (svc_pollfds)
6127c478bd9Sstevel@tonic-gate 		 *	- count pending file descriptors
6137c478bd9Sstevel@tonic-gate 		 *
6147c478bd9Sstevel@tonic-gate 		 * (svc_thr_total - svc_thr_active) are already available.
6157c478bd9Sstevel@tonic-gate 		 * This thread is one of the available threads.
6167c478bd9Sstevel@tonic-gate 		 *
6177c478bd9Sstevel@tonic-gate 		 * Number of new threads should not exceed
6187c478bd9Sstevel@tonic-gate 		 *	(svc_thr_max - svc_thr_total).
6197c478bd9Sstevel@tonic-gate 		 */
6207c478bd9Sstevel@tonic-gate 		if (svc_thr_total < svc_thr_max &&
621*361f55a5SMarcel Telka 		    svc_mt_mode == RPC_SVC_MT_AUTO && !svc_exit_done) {
6227c478bd9Sstevel@tonic-gate 			n_new = 1 + 1 + svc_pollfds + svc_total_pending -
623*361f55a5SMarcel Telka 			    (svc_thr_total - svc_thr_active);
6247c478bd9Sstevel@tonic-gate 			if (n_new > (svc_thr_max - svc_thr_total))
6257c478bd9Sstevel@tonic-gate 				n_new = svc_thr_max - svc_thr_total;
6267c478bd9Sstevel@tonic-gate 			if (n_new > 0)
6277c478bd9Sstevel@tonic-gate 				start_threads(n_new);
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		/*
6317c478bd9Sstevel@tonic-gate 		 * Get parent xprt.  It is possible for the parent service
6327c478bd9Sstevel@tonic-gate 		 * handle to be destroyed by now, due to a race condition.
6337c478bd9Sstevel@tonic-gate 		 * Check for this, and if so, log a warning and go on.
6347c478bd9Sstevel@tonic-gate 		 */
6357c478bd9Sstevel@tonic-gate 		parent_xprt = svc_xports[myfd];
6367c478bd9Sstevel@tonic-gate 		if (parent_xprt == NULL) {
6377c478bd9Sstevel@tonic-gate 			/* Check if it is not a user FD */
6387c478bd9Sstevel@tonic-gate 			if (__is_a_userfd(myfd) == TRUE)
6397c478bd9Sstevel@tonic-gate 				__svc_getreq_user(&(svc_pollset[mypollfd]));
6407c478bd9Sstevel@tonic-gate 			goto continue_with_locks;
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6437c478bd9Sstevel@tonic-gate 		if (svc_defunct(parent_xprt) || svc_failed(parent_xprt))
6447c478bd9Sstevel@tonic-gate 			goto continue_with_locks;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		/*
6477c478bd9Sstevel@tonic-gate 		 * Make a copy of parent xprt, update svc_fdset.
6487c478bd9Sstevel@tonic-gate 		 */
6497c478bd9Sstevel@tonic-gate 		if ((xprt = make_xprt_copy(parent_xprt)) == NULL)
6507c478bd9Sstevel@tonic-gate 			goto continue_with_locks;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		/*
6537c478bd9Sstevel@tonic-gate 		 * Keep track of active threads in automatic mode.
6547c478bd9Sstevel@tonic-gate 		 */
6557c478bd9Sstevel@tonic-gate 		if (svc_mt_mode == RPC_SVC_MT_AUTO)
6567c478bd9Sstevel@tonic-gate 			svc_thr_active++;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 		/*
6597c478bd9Sstevel@tonic-gate 		 * Release mutexes so other threads can get going.
6607c478bd9Sstevel@tonic-gate 		 */
66161961e0fSrobinson 		(void) mutex_unlock(&svc_mutex);
66261961e0fSrobinson 		(void) mutex_unlock(&svc_thr_mutex);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		/*
6657c478bd9Sstevel@tonic-gate 		 * Process request.
6667c478bd9Sstevel@tonic-gate 		 */
6677c478bd9Sstevel@tonic-gate 		{
6687c478bd9Sstevel@tonic-gate 			struct rpc_msg *msg;
6697c478bd9Sstevel@tonic-gate 			struct svc_req *r;
6707c478bd9Sstevel@tonic-gate 			char *cred_area;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6737c478bd9Sstevel@tonic-gate 			msg = SVCEXT(xprt)->msg;
6747c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6757c478bd9Sstevel@tonic-gate 			r = SVCEXT(xprt)->req;
6767c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6777c478bd9Sstevel@tonic-gate 			cred_area = SVCEXT(xprt)->cred_area;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 			msg->rm_call.cb_cred.oa_base = cred_area;
6817c478bd9Sstevel@tonic-gate 			msg->rm_call.cb_verf.oa_base =
682*361f55a5SMarcel Telka 			    &(cred_area[MAX_AUTH_BYTES]);
6837c478bd9Sstevel@tonic-gate 			r->rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			/*
6867c478bd9Sstevel@tonic-gate 			 * receive RPC message
6877c478bd9Sstevel@tonic-gate 			 */
6887c478bd9Sstevel@tonic-gate 			if ((dispatch = SVC_RECV(xprt, msg))) {
6897c478bd9Sstevel@tonic-gate 				if (svc_mt_mode != RPC_SVC_MT_NONE)
6907c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
6917c478bd9Sstevel@tonic-gate 					svc_flags(xprt) |= SVC_ARGS_CHECK;
6927c478bd9Sstevel@tonic-gate 				dispatch = _svc_prog_dispatch(xprt, msg, r);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 				/*
6957c478bd9Sstevel@tonic-gate 				 * Call cleanup procedure if set.
6967c478bd9Sstevel@tonic-gate 				 */
6977c478bd9Sstevel@tonic-gate 				if (__proc_cleanup_cb != NULL)
6987c478bd9Sstevel@tonic-gate 					(*__proc_cleanup_cb)(xprt);
6997c478bd9Sstevel@tonic-gate 			} else
7007c478bd9Sstevel@tonic-gate 				svc_args_done(xprt);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 			/*
7037c478bd9Sstevel@tonic-gate 			 * Finish up, if automatic mode, or not dispatched.
7047c478bd9Sstevel@tonic-gate 			 */
7057c478bd9Sstevel@tonic-gate 			if (svc_mt_mode == RPC_SVC_MT_AUTO || !dispatch) {
7067c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
7077c478bd9Sstevel@tonic-gate 				if (svc_flags(xprt) & SVC_ARGS_CHECK)
7087c478bd9Sstevel@tonic-gate 					svc_args_done(xprt);
70961961e0fSrobinson 				(void) mutex_lock(&svc_mutex);
7107c478bd9Sstevel@tonic-gate 				_svc_done_private(xprt);
7117c478bd9Sstevel@tonic-gate 				if (svc_mt_mode == RPC_SVC_MT_AUTO) {
7127c478bd9Sstevel@tonic-gate 					/*
7137c478bd9Sstevel@tonic-gate 					 * not active any more
7147c478bd9Sstevel@tonic-gate 					 */
7157c478bd9Sstevel@tonic-gate 					svc_thr_active--;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 					/*
7187c478bd9Sstevel@tonic-gate 					 * If not main thread, exit unless
7197c478bd9Sstevel@tonic-gate 					 * there's some immediate work.
7207c478bd9Sstevel@tonic-gate 					 */
7217c478bd9Sstevel@tonic-gate 					if (!main_thread &&
722*361f55a5SMarcel Telka 					    svc_pollfds <= 0 &&
723*361f55a5SMarcel Telka 					    svc_total_pending <= 0 &&
724*361f55a5SMarcel Telka 					    (svc_polling ||
725*361f55a5SMarcel Telka 					    svc_waiters > 0)) {
7267c478bd9Sstevel@tonic-gate 						svc_thr_total--;
7277c478bd9Sstevel@tonic-gate 						if (svc_thr_total ==
7287c478bd9Sstevel@tonic-gate 						    svc_waiters) {
72961961e0fSrobinson 							(void) cond_broadcast(
7307c478bd9Sstevel@tonic-gate 							    &svc_thr_fdwait);
7317c478bd9Sstevel@tonic-gate 						}
73261961e0fSrobinson 						(void) mutex_unlock(&svc_mutex);
7337c478bd9Sstevel@tonic-gate 						thr_exit(NULL);
7347c478bd9Sstevel@tonic-gate 						/* NOTREACHED */
7357c478bd9Sstevel@tonic-gate 					}
7367c478bd9Sstevel@tonic-gate 				}
73761961e0fSrobinson 				(void) mutex_unlock(&svc_mutex);
7387c478bd9Sstevel@tonic-gate 			}
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate  * start_threads() - Start specified number of threads.
7477c478bd9Sstevel@tonic-gate  */
7487c478bd9Sstevel@tonic-gate static void
start_threads(int num_threads)74961961e0fSrobinson start_threads(int num_threads)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	int		i;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_mutex));
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_threads; i++) {
7567c478bd9Sstevel@tonic-gate 		if (thr_create(NULL, 0, (void *(*)(void *))_svc_run_mt, NULL,
7577c478bd9Sstevel@tonic-gate 		    THR_DETACHED, NULL) == 0) {
7587c478bd9Sstevel@tonic-gate 			svc_thr_total++;
7597c478bd9Sstevel@tonic-gate 			svc_thr_total_creates++;
7607c478bd9Sstevel@tonic-gate 		} else {
7617c478bd9Sstevel@tonic-gate 			svc_thr_total_create_errors++;
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate /*
7687c478bd9Sstevel@tonic-gate  * create_pipe() - create pipe for breaking out of poll.
7697c478bd9Sstevel@tonic-gate  */
7707c478bd9Sstevel@tonic-gate static void
create_pipe(void)77161961e0fSrobinson create_pipe(void)
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate 	if (pipe(svc_pipe) == -1) {
7747c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(__nsl_dom,
775*361f55a5SMarcel Telka 		    "RPC: svc could not create pipe - exiting"));
7767c478bd9Sstevel@tonic-gate 		exit(1);
7777c478bd9Sstevel@tonic-gate 	}
778e8031f0aSraf 	if (fcntl(svc_pipe[0], F_SETFL, O_NONBLOCK) == -1) {
7797c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(__nsl_dom,
780*361f55a5SMarcel Telka 		    "RPC: svc pipe error - exiting"));
7817c478bd9Sstevel@tonic-gate 		exit(1);
7827c478bd9Sstevel@tonic-gate 	}
783e8031f0aSraf 	if (fcntl(svc_pipe[1], F_SETFL, O_NONBLOCK) == -1) {
7847c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(__nsl_dom,
785*361f55a5SMarcel Telka 		    "RPC: svc pipe error - exiting"));
7867c478bd9Sstevel@tonic-gate 		exit(1);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate /*
7927c478bd9Sstevel@tonic-gate  * clear_pipe() - Empty data in pipe.
7937c478bd9Sstevel@tonic-gate  */
7947c478bd9Sstevel@tonic-gate static void
clear_pipe(void)79561961e0fSrobinson clear_pipe(void)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	char	buf[16];
7987c478bd9Sstevel@tonic-gate 	int	i;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	do {
8017c478bd9Sstevel@tonic-gate 		i = read(svc_pipe[0], buf, sizeof (buf));
8027c478bd9Sstevel@tonic-gate 	} while (i == sizeof (buf));
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate /*
8077c478bd9Sstevel@tonic-gate  * select_next_pollfd() - Select the next active fd in svc_pollset.
8087c478bd9Sstevel@tonic-gate  */
8097c478bd9Sstevel@tonic-gate static int
select_next_pollfd(int * fd,int * pollfdIndex)8107c478bd9Sstevel@tonic-gate select_next_pollfd(int *fd, int *pollfdIndex)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	int i;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_thr_mutex));
8157c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_mutex));
8167c478bd9Sstevel@tonic-gate 
817*361f55a5SMarcel Telka 	for (i = svc_next_pollfd; svc_pollfds > 0 && i < svc_polled; i++) {
8187c478bd9Sstevel@tonic-gate 		if (svc_pollset[i].revents) {
8197c478bd9Sstevel@tonic-gate 			svc_pollfds--;
8207c478bd9Sstevel@tonic-gate 			/*
8217c478bd9Sstevel@tonic-gate 			 * No more special case for POLLNVAL, because it may
8227c478bd9Sstevel@tonic-gate 			 * be linked with a user file descriptot callback
8237c478bd9Sstevel@tonic-gate 			 */
8247c478bd9Sstevel@tonic-gate 			svc_next_pollfd = i + 1;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 			*fd = svc_pollset[i].fd;
8277c478bd9Sstevel@tonic-gate 			*pollfdIndex = i;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 			return (0);
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 	svc_next_pollfd = svc_pollfds = 0;
8337c478bd9Sstevel@tonic-gate 	*fd = -1;
8347c478bd9Sstevel@tonic-gate 	*pollfdIndex = INVALID_POLLFD;
8357c478bd9Sstevel@tonic-gate 	return (-1);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate  * make_xprt_copy() - make a copy of the parent xprt.
8417c478bd9Sstevel@tonic-gate  * Clear fd bit in svc_fdset.
8427c478bd9Sstevel@tonic-gate  */
8437c478bd9Sstevel@tonic-gate static SVCXPRT *
make_xprt_copy(SVCXPRT * parent)84461961e0fSrobinson make_xprt_copy(SVCXPRT *parent)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8477c478bd9Sstevel@tonic-gate 	SVCXPRT_LIST	*xlist = SVCEXT(parent)->my_xlist;
8487c478bd9Sstevel@tonic-gate 	SVCXPRT_LIST	*xret;
8497c478bd9Sstevel@tonic-gate 	SVCXPRT		*xprt;
8507c478bd9Sstevel@tonic-gate 	int		fd = parent->xp_fd;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_mutex));
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	xret = xlist->next;
8557c478bd9Sstevel@tonic-gate 	if (xret) {
8567c478bd9Sstevel@tonic-gate 		xlist->next = xret->next;
8577c478bd9Sstevel@tonic-gate 		xret->next = NULL;
8587c478bd9Sstevel@tonic-gate 		xprt = xret->xprt;
8597c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8607c478bd9Sstevel@tonic-gate 		svc_flags(xprt) = svc_flags(parent);
8617c478bd9Sstevel@tonic-gate 	} else
8627c478bd9Sstevel@tonic-gate 		xprt = svc_copy(parent);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if (xprt) {
8657c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8667c478bd9Sstevel@tonic-gate 		SVCEXT(parent)->refcnt++;
86761961e0fSrobinson 		(void) rw_wrlock(&svc_fd_lock);
8687c478bd9Sstevel@tonic-gate 		clear_pollfd(fd);
86961961e0fSrobinson 		(void) rw_unlock(&svc_fd_lock);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 	return (xprt);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate  * _svc_done_private() - return copies to library.
8767c478bd9Sstevel@tonic-gate  */
8777c478bd9Sstevel@tonic-gate static void
_svc_done_private(SVCXPRT * xprt)87861961e0fSrobinson _svc_done_private(SVCXPRT *xprt)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	SVCXPRT		*parent;
8817c478bd9Sstevel@tonic-gate 	SVCXPRT_LIST	*xhead, *xlist;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&svc_mutex));
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8867c478bd9Sstevel@tonic-gate 	if ((parent = SVCEXT(xprt)->parent) == NULL)
8877c478bd9Sstevel@tonic-gate 		return;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8907c478bd9Sstevel@tonic-gate 	xhead = SVCEXT(parent)->my_xlist;
8917c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8927c478bd9Sstevel@tonic-gate 	xlist = SVCEXT(xprt)->my_xlist;
8937c478bd9Sstevel@tonic-gate 	xlist->next = xhead->next;
8947c478bd9Sstevel@tonic-gate 	xhead->next = xlist;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
8977c478bd9Sstevel@tonic-gate 	SVCEXT(parent)->refcnt--;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/*
9007c478bd9Sstevel@tonic-gate 	 * Propagate any error flags.  This is done in both directions to
9017c478bd9Sstevel@tonic-gate 	 * ensure that if one child gets an error, everyone will see it
9027c478bd9Sstevel@tonic-gate 	 * (even if there are multiple outstanding children) and the
9037c478bd9Sstevel@tonic-gate 	 * transport will get closed.
9047c478bd9Sstevel@tonic-gate 	 */
9057c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9067c478bd9Sstevel@tonic-gate 	svc_flags(xprt) |= svc_flags(parent);
9077c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9087c478bd9Sstevel@tonic-gate 	if (svc_failed(xprt) || svc_defunct(xprt)) {
9097c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9107c478bd9Sstevel@tonic-gate 		svc_flags(parent) |= (svc_flags(xprt) &
911*361f55a5SMarcel Telka 		    (SVC_FAILED | SVC_DEFUNCT));
9127c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9137c478bd9Sstevel@tonic-gate 		if (SVCEXT(parent)->refcnt == 0)
9147c478bd9Sstevel@tonic-gate 			_svc_destroy_private(xprt);
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate void
svc_done(SVCXPRT * xprt)9197c478bd9Sstevel@tonic-gate svc_done(SVCXPRT *xprt)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 	if (svc_mt_mode != RPC_SVC_MT_USER)
9227c478bd9Sstevel@tonic-gate 		return;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/*
9257c478bd9Sstevel@tonic-gate 	 * Make sure file descriptor is released in user mode.
9267c478bd9Sstevel@tonic-gate 	 * If the xprt is a door, do nothing: this work is performed by
9277c478bd9Sstevel@tonic-gate 	 * svc_door.c's return_xprt_copy() routine, which is basically a
9287c478bd9Sstevel@tonic-gate 	 * door-specific copy of _svc_done_private().
9297c478bd9Sstevel@tonic-gate 	 */
9307c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9317c478bd9Sstevel@tonic-gate 	if (svc_type(xprt) == SVC_DOOR)
9327c478bd9Sstevel@tonic-gate 		return;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9357c478bd9Sstevel@tonic-gate 	if (svc_flags(xprt) & SVC_ARGS_CHECK)
9367c478bd9Sstevel@tonic-gate 		svc_args_done(xprt);
9377c478bd9Sstevel@tonic-gate 
93861961e0fSrobinson 	(void) mutex_lock(&svc_mutex);
9397c478bd9Sstevel@tonic-gate 	_svc_done_private(xprt);
94061961e0fSrobinson 	(void) mutex_unlock(&svc_mutex);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*
9457c478bd9Sstevel@tonic-gate  * Mark argument completion.  Release file descriptor.
9467c478bd9Sstevel@tonic-gate  */
9477c478bd9Sstevel@tonic-gate void
svc_args_done(SVCXPRT * xprt)94861961e0fSrobinson svc_args_done(SVCXPRT *xprt)
9497c478bd9Sstevel@tonic-gate {
9507c478bd9Sstevel@tonic-gate 	char	dummy;
9517c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9527c478bd9Sstevel@tonic-gate 	SVCXPRT	*parent = SVCEXT(xprt)->parent;
9537c478bd9Sstevel@tonic-gate 	bool_t	wake_up_poller;
9547c478bd9Sstevel@tonic-gate 	enum	xprt_stat stat;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9577c478bd9Sstevel@tonic-gate 	svc_flags(xprt) |= svc_flags(parent);
9587c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9597c478bd9Sstevel@tonic-gate 	svc_flags(xprt) &= ~SVC_ARGS_CHECK;
9607c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9617c478bd9Sstevel@tonic-gate 	if (svc_failed(xprt) || svc_defunct(parent))
9627c478bd9Sstevel@tonic-gate 		return;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
9657c478bd9Sstevel@tonic-gate 	if (svc_type(xprt) == SVC_CONNECTION &&
966*361f55a5SMarcel Telka 	    (stat = SVC_STAT(xprt)) != XPRT_IDLE) {
9677c478bd9Sstevel@tonic-gate 		if (stat == XPRT_MOREREQS) {
96861961e0fSrobinson 			(void) mutex_lock(&svc_mutex);
9697c478bd9Sstevel@tonic-gate 			svc_pending_fds[svc_last_pending++] = xprt->xp_fd;
9707c478bd9Sstevel@tonic-gate 			if (svc_last_pending > CIRCULAR_BUFSIZE)
9717c478bd9Sstevel@tonic-gate 				svc_last_pending = 0;
9727c478bd9Sstevel@tonic-gate 			svc_total_pending++;
97361961e0fSrobinson 			(void) mutex_unlock(&svc_mutex);
9747c478bd9Sstevel@tonic-gate 			wake_up_poller = FALSE;
9757c478bd9Sstevel@tonic-gate 		} else {
9767c478bd9Sstevel@tonic-gate 			/*
9777c478bd9Sstevel@tonic-gate 			 * connection failed
9787c478bd9Sstevel@tonic-gate 			 */
9797c478bd9Sstevel@tonic-gate 			return;
9807c478bd9Sstevel@tonic-gate 		}
9817c478bd9Sstevel@tonic-gate 	} else {
98261961e0fSrobinson 		(void) rw_wrlock(&svc_fd_lock);
9837c478bd9Sstevel@tonic-gate 		set_pollfd(xprt->xp_fd, MASKVAL);
98461961e0fSrobinson 		(void) rw_unlock(&svc_fd_lock);
9857c478bd9Sstevel@tonic-gate 		wake_up_poller = TRUE;
9867c478bd9Sstevel@tonic-gate 	}
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	if (!wake_up_poller || !svc_polling) {
9897c478bd9Sstevel@tonic-gate 		/*
9907c478bd9Sstevel@tonic-gate 		 * Wake up any waiting threads.
9917c478bd9Sstevel@tonic-gate 		 */
99261961e0fSrobinson 		(void) mutex_lock(&svc_mutex);
9937c478bd9Sstevel@tonic-gate 		if (svc_waiters > 0) {
99461961e0fSrobinson 			(void) cond_broadcast(&svc_thr_fdwait);
99561961e0fSrobinson 			(void) mutex_unlock(&svc_mutex);
9967c478bd9Sstevel@tonic-gate 			return;
9977c478bd9Sstevel@tonic-gate 		}
99861961e0fSrobinson 		(void) mutex_unlock(&svc_mutex);
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	/*
10027c478bd9Sstevel@tonic-gate 	 * Wake up any polling thread.
10037c478bd9Sstevel@tonic-gate 	 */
10047c478bd9Sstevel@tonic-gate 	if (svc_polling)
10057c478bd9Sstevel@tonic-gate 		(void) write(svc_pipe[1], &dummy, sizeof (dummy));
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate int
__rpc_legal_connmaxrec(int suggested)10107c478bd9Sstevel@tonic-gate __rpc_legal_connmaxrec(int suggested) {
10117c478bd9Sstevel@tonic-gate 	if (suggested == -1) {
10127c478bd9Sstevel@tonic-gate 		/* Supply default */
10137c478bd9Sstevel@tonic-gate 		return (RPC_MAXDATASIZE + 2*sizeof (uint32_t));
10147c478bd9Sstevel@tonic-gate 	} else if (suggested < 0) {
10157c478bd9Sstevel@tonic-gate 		return (-1);
10167c478bd9Sstevel@tonic-gate 	} else if (suggested > 0) {
10177c478bd9Sstevel@tonic-gate 		/* Round down to multiple of BYTES_PER_XDR_UNIT */
10187c478bd9Sstevel@tonic-gate 		suggested -= suggested % BYTES_PER_XDR_UNIT;
10197c478bd9Sstevel@tonic-gate 		/* If possible, allow for two fragment headers */
10207c478bd9Sstevel@tonic-gate 		if (suggested < MAXINT-(2*sizeof (uint32_t))) {
10217c478bd9Sstevel@tonic-gate 			/* Allow for two fragment headers */
10227c478bd9Sstevel@tonic-gate 			suggested += 2 * sizeof (uint32_t);
10237c478bd9Sstevel@tonic-gate 		} else {
10247c478bd9Sstevel@tonic-gate 			suggested = MAXINT;
10257c478bd9Sstevel@tonic-gate 		}
10267c478bd9Sstevel@tonic-gate 		if (suggested < sizeof (struct rpc_msg)) {
10277c478bd9Sstevel@tonic-gate 			return (-1);
10287c478bd9Sstevel@tonic-gate 		}
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 	return (suggested);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate bool_t
rpc_control(int op,void * info)103561961e0fSrobinson rpc_control(int op, void *info)
10367c478bd9Sstevel@tonic-gate {
10377c478bd9Sstevel@tonic-gate 	int		tmp;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	switch (op) {
10407c478bd9Sstevel@tonic-gate 	case RPC_SVC_MTMODE_SET:
10417c478bd9Sstevel@tonic-gate 		tmp = *((int *)info);
10427c478bd9Sstevel@tonic-gate 		if (tmp != RPC_SVC_MT_NONE && tmp != RPC_SVC_MT_AUTO &&
1043*361f55a5SMarcel Telka 		    tmp != RPC_SVC_MT_USER)
10447c478bd9Sstevel@tonic-gate 			return (FALSE);
10457c478bd9Sstevel@tonic-gate 		if (svc_mt_mode != RPC_SVC_MT_NONE && svc_mt_mode != tmp)
10467c478bd9Sstevel@tonic-gate 			return (FALSE);
10477c478bd9Sstevel@tonic-gate 		svc_mt_mode = tmp;
10487c478bd9Sstevel@tonic-gate 		return (TRUE);
10497c478bd9Sstevel@tonic-gate 	case RPC_SVC_MTMODE_GET:
10507c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_mt_mode;
10517c478bd9Sstevel@tonic-gate 		return (TRUE);
10527c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRMAX_SET:
10537c478bd9Sstevel@tonic-gate 		if ((tmp = *((int *)info)) < 1)
10547c478bd9Sstevel@tonic-gate 			return (FALSE);
105561961e0fSrobinson 		(void) mutex_lock(&svc_mutex);
10567c478bd9Sstevel@tonic-gate 		svc_thr_max = tmp;
105761961e0fSrobinson 		(void) mutex_unlock(&svc_mutex);
10587c478bd9Sstevel@tonic-gate 		return (TRUE);
10597c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRMAX_GET:
10607c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_thr_max;
10617c478bd9Sstevel@tonic-gate 		return (TRUE);
10627c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRTOTAL_GET:
10637c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_thr_total;
10647c478bd9Sstevel@tonic-gate 		return (TRUE);
10657c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRCREATES_GET:
10667c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_thr_total_creates;
10677c478bd9Sstevel@tonic-gate 		return (TRUE);
10687c478bd9Sstevel@tonic-gate 	case RPC_SVC_THRERRORS_GET:
10697c478bd9Sstevel@tonic-gate 		*((int *)info) = svc_thr_total_create_errors;
10707c478bd9Sstevel@tonic-gate 		return (TRUE);
10717c478bd9Sstevel@tonic-gate 	case RPC_SVC_USE_POLLFD:
10727c478bd9Sstevel@tonic-gate 		if (*((int *)info) && !__rpc_use_pollfd_done) {
10737c478bd9Sstevel@tonic-gate 			__rpc_use_pollfd_done = 1;
10747c478bd9Sstevel@tonic-gate 			return (TRUE);
10757c478bd9Sstevel@tonic-gate 		}
10767c478bd9Sstevel@tonic-gate 		return (FALSE);
10777c478bd9Sstevel@tonic-gate 	case RPC_SVC_CONNMAXREC_SET:
10787c478bd9Sstevel@tonic-gate 		tmp = __rpc_legal_connmaxrec(*(int *)info);
10797c478bd9Sstevel@tonic-gate 		if (tmp >= 0) {
10807c478bd9Sstevel@tonic-gate 			__rpc_connmaxrec = tmp;
10817c478bd9Sstevel@tonic-gate 			return (TRUE);
10827c478bd9Sstevel@tonic-gate 		} else {
10837c478bd9Sstevel@tonic-gate 			return (FALSE);
10847c478bd9Sstevel@tonic-gate 		}
10857c478bd9Sstevel@tonic-gate 	case RPC_SVC_CONNMAXREC_GET:
10867c478bd9Sstevel@tonic-gate 		*((int *)info) = __rpc_connmaxrec;
10877c478bd9Sstevel@tonic-gate 		return (TRUE);
10887c478bd9Sstevel@tonic-gate 	case RPC_SVC_IRTIMEOUT_SET:
10897c478bd9Sstevel@tonic-gate 		tmp = *((int *)info);
10907c478bd9Sstevel@tonic-gate 		if (tmp >= 0) {
10917c478bd9Sstevel@tonic-gate 			__rpc_irtimeout = tmp;
10927c478bd9Sstevel@tonic-gate 			return (TRUE);
10937c478bd9Sstevel@tonic-gate 		} else {
10947c478bd9Sstevel@tonic-gate 			return (FALSE);
10957c478bd9Sstevel@tonic-gate 		}
10967c478bd9Sstevel@tonic-gate 	/*
10977c478bd9Sstevel@tonic-gate 	 * No mutex necessary as _EXCLBIND_SET will/should only
10987c478bd9Sstevel@tonic-gate 	 * be used before an RPC daemon goes mt-hot.
10997c478bd9Sstevel@tonic-gate 	 */
11007c478bd9Sstevel@tonic-gate 	case __RPC_SVC_EXCLBIND_SET:
11017c478bd9Sstevel@tonic-gate 		if (info) {
11027c478bd9Sstevel@tonic-gate 			__rpc_tp_exclbind = *((bool_t *)info);
11037c478bd9Sstevel@tonic-gate 			return (TRUE);
11047c478bd9Sstevel@tonic-gate 		}
11057c478bd9Sstevel@tonic-gate 		return (FALSE);
11067c478bd9Sstevel@tonic-gate 	case __RPC_SVC_EXCLBIND_GET:
11077c478bd9Sstevel@tonic-gate 		if (info) {
11087c478bd9Sstevel@tonic-gate 			*((bool_t *)info) = __rpc_tp_exclbind;
11097c478bd9Sstevel@tonic-gate 			return (TRUE);
11107c478bd9Sstevel@tonic-gate 		}
11117c478bd9Sstevel@tonic-gate 		return (FALSE);
11127c478bd9Sstevel@tonic-gate 
1113*361f55a5SMarcel Telka 	case __RPC_SVC_LSTNBKLOG_SET:
1114*361f55a5SMarcel Telka 		tmp = *(int *)info;
1115*361f55a5SMarcel Telka 		if (tmp > 0) {
1116*361f55a5SMarcel Telka 			__svc_lstnbklog = tmp;
1117*361f55a5SMarcel Telka 			return (TRUE);
1118*361f55a5SMarcel Telka 		}
1119*361f55a5SMarcel Telka 		return (FALSE);
1120*361f55a5SMarcel Telka 	case __RPC_SVC_LSTNBKLOG_GET:
1121*361f55a5SMarcel Telka 		*(int *)info = __svc_lstnbklog;
1122*361f55a5SMarcel Telka 		return (TRUE);
1123*361f55a5SMarcel Telka 
11247c478bd9Sstevel@tonic-gate 	default:
11257c478bd9Sstevel@tonic-gate 		return (FALSE);
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate }
1128