10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1914Scasper * Common Development and Distribution License (the "License").
6*1914Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21132Srobinson
220Sstevel@tonic-gate /*
231219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
261219Sraf
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate * California.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * This is the rpc server side idle loop
390Sstevel@tonic-gate * Wait for input, call server program.
400Sstevel@tonic-gate */
410Sstevel@tonic-gate #include "mt.h"
420Sstevel@tonic-gate #include "rpc_mt.h"
430Sstevel@tonic-gate #include <stdlib.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <signal.h>
460Sstevel@tonic-gate #include <rpc/rpc.h>
470Sstevel@tonic-gate #include <errno.h>
480Sstevel@tonic-gate #include <sys/poll.h>
490Sstevel@tonic-gate #include <sys/types.h>
500Sstevel@tonic-gate #include <syslog.h>
510Sstevel@tonic-gate #include <thread.h>
520Sstevel@tonic-gate #include <assert.h>
530Sstevel@tonic-gate #include <libintl.h>
540Sstevel@tonic-gate #include <values.h>
550Sstevel@tonic-gate
560Sstevel@tonic-gate extern const char __nsl_dom[];
570Sstevel@tonic-gate
580Sstevel@tonic-gate extern int __rpc_compress_pollfd(int, pollfd_t *, pollfd_t *);
590Sstevel@tonic-gate extern bool_t __is_a_userfd(int P_fd);
600Sstevel@tonic-gate extern void __destroy_userfd();
610Sstevel@tonic-gate extern void clear_pollfd(int);
620Sstevel@tonic-gate extern void set_pollfd(int /* fd */, short /* events */);
630Sstevel@tonic-gate extern void svc_getreq_poll();
640Sstevel@tonic-gate extern void (*__proc_cleanup_cb)();
650Sstevel@tonic-gate
660Sstevel@tonic-gate static void start_threads();
670Sstevel@tonic-gate static void create_pipe();
680Sstevel@tonic-gate static void clear_pipe();
690Sstevel@tonic-gate static int select_next_pollfd();
700Sstevel@tonic-gate static SVCXPRT *make_xprt_copy();
710Sstevel@tonic-gate static void _svc_run_mt();
720Sstevel@tonic-gate static void _svc_run();
730Sstevel@tonic-gate
740Sstevel@tonic-gate int _svc_prog_dispatch();
750Sstevel@tonic-gate static void _svc_done_private();
760Sstevel@tonic-gate
770Sstevel@tonic-gate extern rwlock_t svc_fd_lock;
780Sstevel@tonic-gate extern mutex_t svc_door_mutex;
790Sstevel@tonic-gate extern cond_t svc_door_waitcv;
800Sstevel@tonic-gate extern int svc_ndoorfds;
810Sstevel@tonic-gate extern void __svc_cleanup_door_xprts();
820Sstevel@tonic-gate extern void __svc_free_xprtlist();
830Sstevel@tonic-gate extern void __svc_getreq_user(struct pollfd *);
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * Maximum fragment size allowed for connection oriented connections.
870Sstevel@tonic-gate * Zero means that no maximum size limit has been requested.
880Sstevel@tonic-gate */
890Sstevel@tonic-gate int __rpc_connmaxrec = 0;
900Sstevel@tonic-gate
910Sstevel@tonic-gate /* Inter-Record Timeout in secs for non-blocked connection RPC */
920Sstevel@tonic-gate int __rpc_irtimeout = 35;
930Sstevel@tonic-gate
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate * Request exclusive access to tcp and udp non-priv ports bound with a
960Sstevel@tonic-gate * wildcard addr.
970Sstevel@tonic-gate */
980Sstevel@tonic-gate bool_t __rpc_tp_exclbind = FALSE;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate * XXX - eventually, all mutexes and their initializations static
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate * Variables used for MT
1060Sstevel@tonic-gate */
1070Sstevel@tonic-gate int svc_mt_mode; /* multi-threading mode */
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate int svc_pipe[2]; /* pipe for breaking out of poll: read(0), write(1) */
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate /* BEGIN PROTECTED BY svc_mutex */
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate static int svc_thr_max = 16; /* default maximum number of threads allowed */
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate static int svc_thr_total; /* current number of threads */
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate static int svc_thr_active; /* current number of threads active */
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /* circular array of file descriptors with pending data */
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate #define CIRCULAR_BUFSIZE 1024
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate static int svc_pending_fds[CIRCULAR_BUFSIZE+1]; /* fds with pending data */
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate static int svc_next_pending; /* next one to be processed */
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate static int svc_last_pending; /* last one in list */
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate static int svc_total_pending; /* total in list */
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate static int svc_thr_total_creates; /* total created - stats */
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate static int svc_thr_total_create_errors; /* total create errors - stats */
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate static int svc_waiters; /* number of waiting threads */
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /* END PROTECTED BY svc_mutex */
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate /* BEGIN PROTECTED BY svc_fd_lock: */
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate int svc_nfds; /* total number of active file descriptors */
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate int svc_nfds_set; /* total number of fd bits set in svc_fdset */
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate int svc_max_fd = 0; /* largest active file descriptor */
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate int svc_npollfds; /* total number of active pollfds */
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate int svc_npollfds_set; /* total number of pollfd set in svc_pollfd */
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate int svc_max_pollfd; /* largest active pollfd so far */
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate int svc_pollfd_allocd; /* number of pollfd structures allocated */
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /* END PROTECTED BY svc_fd_lock: */
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /* BEGIN PROTECTED BY svc_thr_mutex */
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate #define POLLSET_EXTEND 256
1600Sstevel@tonic-gate static int svc_pollset_allocd;
1610Sstevel@tonic-gate static struct pollfd *svc_pollset;
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * array of file descriptors currently active
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate static int svc_polled; /* no of fds polled in last poll() - input */
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate static int svc_pollfds; /* no of active fds in last poll() - output */
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate static int svc_next_pollfd; /* next fd to processin svc_pollset */
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate bool_t svc_polling; /* true if a thread is polling */
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* END PROTECTED BY svc_thr_mutex */
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /* BEGIN PROTECTED BY svc_exit_mutex */
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate static bool_t svc_exit_done = TRUE;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /* END PROTECTED BY svc_exit_mutex */
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * Warlock section
1830Sstevel@tonic-gate */
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_mutex:
1860Sstevel@tonic-gate svc_thr_total, svc_thr_active, svc_pending_fds, svc_next_pending,
1870Sstevel@tonic-gate svc_last_pending, svc_total_pending, svc_thr_total_creates,
1880Sstevel@tonic-gate svc_thr_total_create_errors,
1890Sstevel@tonic-gate svcxprt_list_t::next, svcxprt_ext_t::my_xlist,
1900Sstevel@tonic-gate svc_thr_max, svc_waiters
1910Sstevel@tonic-gate */
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_fd_lock:
1940Sstevel@tonic-gate svc_xports, svc_fdset, svc_nfds, svc_nfds_set, svc_max_fd,
1950Sstevel@tonic-gate svc_pollfd, svc_npollfds, svc_npollfds_set, svc_max_pollfd
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_thr_mutex:
1990Sstevel@tonic-gate svc_pollset, svc_pollfds, svc_next_pollfd, svc_polling
2000Sstevel@tonic-gate svc_pollset_allocd, svc_polled
2010Sstevel@tonic-gate */
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_exit_mutex:
2040Sstevel@tonic-gate svc_exit_done
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /* VARIABLES READABLE WITHOUT LOCK:
2080Sstevel@tonic-gate svc_thr_total, svc_thr_active, svc_thr_total_creates,
2090Sstevel@tonic-gate svc_thr_total_create_errors,
2100Sstevel@tonic-gate svc_xports, svc_nfds, svc_nfds_set, svc_max_fd,
2110Sstevel@tonic-gate svc_npollfds, svc_npollfds_set, svc_max_pollfd,
2120Sstevel@tonic-gate svc_pollfds, svc_next_pollfd, svc_exit_done, svc_polling,
2130Sstevel@tonic-gate svc_thr_max, svc_waiters
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /* VARIABLES PROTECTED BY "program_logic":
2170Sstevel@tonic-gate rpc_msg::, svc_req::, svcxprt_ext_t::flags, svc_mt_mode,
2180Sstevel@tonic-gate svcxprt_ext_t::parent
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /* LOCK ORDER:
2220Sstevel@tonic-gate svc_exit_mutex, svc_thr_mutex, svc_mutex, svc_fd_lock
2230Sstevel@tonic-gate */
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate void
svc_run(void)227132Srobinson svc_run(void)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate /* NO OTHER THREADS ARE RUNNING */
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate svc_exit_done = FALSE;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate while ((svc_npollfds > 0 || svc_ndoorfds > 0) && !svc_exit_done) {
2340Sstevel@tonic-gate if (svc_npollfds > 0) {
2350Sstevel@tonic-gate switch (svc_mt_mode) {
2360Sstevel@tonic-gate case RPC_SVC_MT_NONE:
2370Sstevel@tonic-gate _svc_run();
2380Sstevel@tonic-gate break;
2390Sstevel@tonic-gate default:
2400Sstevel@tonic-gate _svc_run_mt();
2410Sstevel@tonic-gate break;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate continue;
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
246132Srobinson (void) mutex_lock(&svc_door_mutex);
2470Sstevel@tonic-gate if (svc_ndoorfds > 0)
248132Srobinson (void) cond_wait(&svc_door_waitcv, &svc_door_mutex);
249132Srobinson (void) mutex_unlock(&svc_door_mutex);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate * This function causes svc_run() to exit by destroying all
2560Sstevel@tonic-gate * service handles.
2570Sstevel@tonic-gate */
2580Sstevel@tonic-gate void
svc_exit(void)259132Srobinson svc_exit(void)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate SVCXPRT *xprt;
2620Sstevel@tonic-gate int fd;
2630Sstevel@tonic-gate char dummy;
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate /* NO LOCKS HELD */
2660Sstevel@tonic-gate
267132Srobinson (void) mutex_lock(&svc_exit_mutex);
2680Sstevel@tonic-gate if (svc_exit_done) {
269132Srobinson (void) mutex_unlock(&svc_exit_mutex);
2700Sstevel@tonic-gate return;
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate svc_exit_done = TRUE;
2730Sstevel@tonic-gate for (fd = 0; fd < svc_max_pollfd; fd++) {
2740Sstevel@tonic-gate xprt = svc_xports[fd];
2750Sstevel@tonic-gate if (xprt) {
2760Sstevel@tonic-gate SVC_DESTROY(xprt);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate __svc_free_xprtlist();
2800Sstevel@tonic-gate __svc_cleanup_door_xprts();
281132Srobinson (void) mutex_unlock(&svc_exit_mutex);
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate if (svc_mt_mode != RPC_SVC_MT_NONE) {
284132Srobinson (void) mutex_lock(&svc_mutex);
285132Srobinson (void) cond_broadcast(&svc_thr_fdwait);
286132Srobinson (void) mutex_unlock(&svc_mutex);
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate (void) write(svc_pipe[1], &dummy, sizeof (dummy));
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
291132Srobinson (void) mutex_lock(&svc_door_mutex);
292132Srobinson (void) cond_signal(&svc_door_waitcv); /* wake up door dispatching */
293132Srobinson (void) mutex_unlock(&svc_door_mutex);
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate /* destroy reactor information if any */
2960Sstevel@tonic-gate __destroy_userfd();
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate * this funtion is called with svc_fd_lock and svc_thr_mutex
3020Sstevel@tonic-gate */
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate static int
alloc_pollset(int npollfds)3050Sstevel@tonic-gate alloc_pollset(int npollfds)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate if (npollfds > svc_pollset_allocd) {
3080Sstevel@tonic-gate pollfd_t *tmp;
3090Sstevel@tonic-gate do {
3100Sstevel@tonic-gate svc_pollset_allocd += POLLSET_EXTEND;
3110Sstevel@tonic-gate } while (npollfds > svc_pollset_allocd);
3120Sstevel@tonic-gate tmp = realloc(svc_pollset,
3130Sstevel@tonic-gate sizeof (pollfd_t) * svc_pollset_allocd);
3140Sstevel@tonic-gate if (tmp == NULL) {
3150Sstevel@tonic-gate syslog(LOG_ERR, "alloc_pollset: out of memory");
3160Sstevel@tonic-gate return (-1);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate svc_pollset = tmp;
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate return (0);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate static void
_svc_run(void)324132Srobinson _svc_run(void)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate sigset_t set, oldset;
3270Sstevel@tonic-gate int npollfds;
3280Sstevel@tonic-gate int i;
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate /*
3310Sstevel@tonic-gate * Block SIGALRM while doing work. Unblock it while doing poll().
3320Sstevel@tonic-gate * This is so that services like rpc.rstatd can cause the poll()
3330Sstevel@tonic-gate * to be interrupted due to alarm() but that we don't end up in
3340Sstevel@tonic-gate * an MT-unsafe signal handler at an inopportune time.
3350Sstevel@tonic-gate */
3361219Sraf (void) sigemptyset(&set);
3371219Sraf (void) sigaddset(&set, SIGALRM);
3381219Sraf (void) sigprocmask(SIG_BLOCK, &set, &oldset);
3390Sstevel@tonic-gate while (!svc_exit_done) {
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate * Check whether there is any server fd on which we may want
3420Sstevel@tonic-gate * to wait.
3430Sstevel@tonic-gate */
344132Srobinson (void) rw_rdlock(&svc_fd_lock);
3450Sstevel@tonic-gate if (alloc_pollset(svc_npollfds) == -1)
3460Sstevel@tonic-gate break;
3470Sstevel@tonic-gate npollfds = __rpc_compress_pollfd(svc_max_pollfd,
3480Sstevel@tonic-gate svc_pollfd, svc_pollset);
349132Srobinson (void) rw_unlock(&svc_fd_lock);
3500Sstevel@tonic-gate if (npollfds == 0)
3510Sstevel@tonic-gate break; /* None waiting, hence return */
3520Sstevel@tonic-gate
3531219Sraf (void) sigprocmask(SIG_SETMASK, &oldset, NULL);
3540Sstevel@tonic-gate i = poll(svc_pollset, npollfds, -1);
3551219Sraf (void) sigprocmask(SIG_BLOCK, &set, &oldset);
3560Sstevel@tonic-gate switch (i) {
3570Sstevel@tonic-gate case -1:
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate * We ignore all errors, continuing with the assumption
3600Sstevel@tonic-gate * that it was set by the signal handlers (or any
3610Sstevel@tonic-gate * other outside event) and not caused by poll().
3620Sstevel@tonic-gate */
3630Sstevel@tonic-gate case 0:
3640Sstevel@tonic-gate continue;
3650Sstevel@tonic-gate default:
3660Sstevel@tonic-gate svc_getreq_poll(svc_pollset, i);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate }
3691219Sraf (void) sigprocmask(SIG_SETMASK, &oldset, NULL);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate /*
3730Sstevel@tonic-gate * In _svc_run_mt, myfd is linked with mypollfd
3740Sstevel@tonic-gate * svc_pollset[mypollfd].fd == myfd
3750Sstevel@tonic-gate * However, in some cases, the link can not be made, thus we define the
3760Sstevel@tonic-gate * following values for these special cases
3770Sstevel@tonic-gate */
3780Sstevel@tonic-gate enum {
3790Sstevel@tonic-gate INVALID_POLLFD = -200,
3800Sstevel@tonic-gate FD_FROM_PENDING
3810Sstevel@tonic-gate };
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate static void
_svc_run_mt(void)384132Srobinson _svc_run_mt(void)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate int npollfds;
3870Sstevel@tonic-gate int n_polled, dispatch;
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate static bool_t first_time = TRUE;
3900Sstevel@tonic-gate bool_t main_thread = FALSE;
3910Sstevel@tonic-gate int n_new;
3920Sstevel@tonic-gate int myfd, mypollfd;
3930Sstevel@tonic-gate SVCXPRT *parent_xprt, *xprt;
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate * Server is multi-threaded. Do "first time" initializations.
3970Sstevel@tonic-gate * Since only one thread exists in the beginning, there's no
3980Sstevel@tonic-gate * need for mutex protection for first time initializations.
3990Sstevel@tonic-gate */
4000Sstevel@tonic-gate if (first_time) {
4010Sstevel@tonic-gate first_time = FALSE;
4020Sstevel@tonic-gate main_thread = TRUE;
4030Sstevel@tonic-gate svc_thr_total = 1; /* this thread */
4040Sstevel@tonic-gate svc_next_pending = svc_last_pending = 0;
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate * Create a pipe for waking up the poll, if new
4080Sstevel@tonic-gate * descriptors have been added to svc_fdset.
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate create_pipe();
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate /* OTHER THREADS ARE RUNNING */
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate if (svc_exit_done)
4160Sstevel@tonic-gate return;
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate for (;;) {
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * svc_thr_mutex prevents more than one thread from
4210Sstevel@tonic-gate * trying to select a descriptor to process further.
4220Sstevel@tonic-gate * svc_thr_mutex is unlocked after a thread selects
4230Sstevel@tonic-gate * a descriptor on which to receive data. If there are
4240Sstevel@tonic-gate * no such descriptors, the thread will poll with
4250Sstevel@tonic-gate * svc_thr_mutex locked, after unlocking all other
4260Sstevel@tonic-gate * locks. This prevents more than one thread from
4270Sstevel@tonic-gate * trying to poll at the same time.
4280Sstevel@tonic-gate */
429132Srobinson (void) mutex_lock(&svc_thr_mutex);
430132Srobinson (void) mutex_lock(&svc_mutex);
4310Sstevel@tonic-gate continue_with_locks:
4320Sstevel@tonic-gate myfd = -1;
4330Sstevel@tonic-gate mypollfd = INVALID_POLLFD;
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate * Check if there are any descriptors with data pending.
4370Sstevel@tonic-gate */
4380Sstevel@tonic-gate if (svc_total_pending > 0) {
4390Sstevel@tonic-gate myfd = svc_pending_fds[svc_next_pending++];
4400Sstevel@tonic-gate mypollfd = FD_FROM_PENDING;
4410Sstevel@tonic-gate if (svc_next_pending > CIRCULAR_BUFSIZE)
4420Sstevel@tonic-gate svc_next_pending = 0;
4430Sstevel@tonic-gate svc_total_pending--;
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate * Get the next active file descriptor to process.
4480Sstevel@tonic-gate */
4490Sstevel@tonic-gate if (myfd == -1 && svc_pollfds == 0) {
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate * svc_pollset is empty; do polling
4520Sstevel@tonic-gate */
4530Sstevel@tonic-gate svc_polling = TRUE;
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * if there are no file descriptors, return
4570Sstevel@tonic-gate */
458132Srobinson (void) rw_rdlock(&svc_fd_lock);
4590Sstevel@tonic-gate if (svc_npollfds == 0 ||
4600Sstevel@tonic-gate alloc_pollset(svc_npollfds + 1) == -1) {
461132Srobinson (void) rw_unlock(&svc_fd_lock);
4620Sstevel@tonic-gate svc_polling = FALSE;
4630Sstevel@tonic-gate svc_thr_total--;
464132Srobinson (void) mutex_unlock(&svc_mutex);
465132Srobinson (void) mutex_unlock(&svc_thr_mutex);
4660Sstevel@tonic-gate if (!main_thread) {
4670Sstevel@tonic-gate thr_exit(NULL);
4680Sstevel@tonic-gate /* NOTREACHED */
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate break;
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate npollfds = __rpc_compress_pollfd(svc_max_pollfd,
4740Sstevel@tonic-gate svc_pollfd, svc_pollset);
475132Srobinson (void) rw_unlock(&svc_fd_lock);
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate if (npollfds == 0) {
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate * There are file descriptors, but none of them
4800Sstevel@tonic-gate * are available for polling. If this is the
4810Sstevel@tonic-gate * main thread, or if no thread is waiting,
4820Sstevel@tonic-gate * wait on condition variable, otherwise exit.
4830Sstevel@tonic-gate */
4840Sstevel@tonic-gate svc_polling = FALSE;
485132Srobinson (void) mutex_unlock(&svc_thr_mutex);
4860Sstevel@tonic-gate if ((!main_thread) && svc_waiters > 0) {
4870Sstevel@tonic-gate svc_thr_total--;
488132Srobinson (void) mutex_unlock(&svc_mutex);
4890Sstevel@tonic-gate thr_exit(NULL);
4900Sstevel@tonic-gate /* NOTREACHED */
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate while (svc_npollfds_set == 0 &&
4940Sstevel@tonic-gate svc_pollfds == 0 &&
4950Sstevel@tonic-gate svc_total_pending == 0 &&
4960Sstevel@tonic-gate !svc_exit_done) {
4970Sstevel@tonic-gate svc_waiters++;
498132Srobinson (void) cond_wait(&svc_thr_fdwait,
499132Srobinson &svc_mutex);
5000Sstevel@tonic-gate svc_waiters--;
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate * Check exit flag. If this is not the main
5050Sstevel@tonic-gate * thread, exit.
5060Sstevel@tonic-gate */
5070Sstevel@tonic-gate if (svc_exit_done) {
5080Sstevel@tonic-gate svc_thr_total--;
509132Srobinson (void) mutex_unlock(&svc_mutex);
5100Sstevel@tonic-gate if (!main_thread)
5110Sstevel@tonic-gate thr_exit(NULL);
5120Sstevel@tonic-gate break;
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate
515132Srobinson (void) mutex_unlock(&svc_mutex);
5160Sstevel@tonic-gate continue;
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate /*
5200Sstevel@tonic-gate * We're ready to poll. Always set svc_pipe[0]
5210Sstevel@tonic-gate * as the last one, since the poll will occasionally
5220Sstevel@tonic-gate * need to be interrupted. Release svc_mutex for
5230Sstevel@tonic-gate * the duration of the poll, but hold on to
5240Sstevel@tonic-gate * svc_thr_mutex, as we don't want any other thread
5250Sstevel@tonic-gate * to do the same.
5260Sstevel@tonic-gate */
5270Sstevel@tonic-gate svc_pollset[npollfds].fd = svc_pipe[0];
5280Sstevel@tonic-gate svc_pollset[npollfds].events = MASKVAL;
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate do {
5310Sstevel@tonic-gate int i, j;
5320Sstevel@tonic-gate
533132Srobinson (void) mutex_unlock(&svc_mutex);
5340Sstevel@tonic-gate n_polled = poll(svc_pollset, npollfds + 1, -1);
535132Srobinson (void) mutex_lock(&svc_mutex);
5360Sstevel@tonic-gate if (n_polled <= 0)
5370Sstevel@tonic-gate continue;
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate * Check if information returned indicates one
5410Sstevel@tonic-gate * or more closed fd's; find and remove any such
5420Sstevel@tonic-gate * information
5430Sstevel@tonic-gate */
5440Sstevel@tonic-gate for (i = 0; i <= npollfds; i++) {
5450Sstevel@tonic-gate if (svc_pollset[i].revents & POLLNVAL) {
5460Sstevel@tonic-gate /* Overwrite svc_pollset[i] */
5470Sstevel@tonic-gate for (j = i; j < npollfds; j++)
5480Sstevel@tonic-gate svc_pollset[j] =
5490Sstevel@tonic-gate svc_pollset[j + 1];
5500Sstevel@tonic-gate (void) memset(&svc_pollset[j],
5510Sstevel@tonic-gate 0, sizeof (struct pollfd));
5520Sstevel@tonic-gate npollfds--;
5530Sstevel@tonic-gate n_polled--;
5540Sstevel@tonic-gate i--;
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate } while (n_polled <= 0);
5580Sstevel@tonic-gate svc_polling = FALSE;
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate /*
5610Sstevel@tonic-gate * If there's data in the pipe, clear it.
5620Sstevel@tonic-gate */
5630Sstevel@tonic-gate if (svc_pollset[npollfds].revents) {
5640Sstevel@tonic-gate clear_pipe();
5650Sstevel@tonic-gate n_polled--;
5660Sstevel@tonic-gate svc_pollset[npollfds].revents = 0;
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate svc_polled = npollfds;
5690Sstevel@tonic-gate svc_pollfds = n_polled;
5700Sstevel@tonic-gate svc_next_pollfd = 0;
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate /*
5730Sstevel@tonic-gate * Check exit flag.
5740Sstevel@tonic-gate */
5750Sstevel@tonic-gate if (svc_exit_done) {
5760Sstevel@tonic-gate svc_thr_total--;
577132Srobinson (void) mutex_unlock(&svc_mutex);
578132Srobinson (void) mutex_unlock(&svc_thr_mutex);
5790Sstevel@tonic-gate if (!main_thread) {
5800Sstevel@tonic-gate thr_exit(NULL);
5810Sstevel@tonic-gate /* NOTREACHED */
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate break;
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate /*
5870Sstevel@tonic-gate * If no descriptor is active, continue.
5880Sstevel@tonic-gate */
5890Sstevel@tonic-gate if (svc_pollfds == 0)
5900Sstevel@tonic-gate goto continue_with_locks;
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate * If a file descriptor has already not been selected,
5950Sstevel@tonic-gate * choose a file descriptor.
5960Sstevel@tonic-gate * svc_pollfds and svc_next_pollfd are updated.
5970Sstevel@tonic-gate */
5980Sstevel@tonic-gate if (myfd == -1) {
5990Sstevel@tonic-gate if (select_next_pollfd(&myfd, &mypollfd) == -1)
6000Sstevel@tonic-gate goto continue_with_locks;
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate * Check to see if new threads need to be started.
6050Sstevel@tonic-gate * Count of threads that could be gainfully employed is
6060Sstevel@tonic-gate * obtained as follows:
6070Sstevel@tonic-gate * - count 1 for poller
6080Sstevel@tonic-gate * - count 1 for this request
6090Sstevel@tonic-gate * - count active file descriptors (svc_pollfds)
6100Sstevel@tonic-gate * - count pending file descriptors
6110Sstevel@tonic-gate *
6120Sstevel@tonic-gate * (svc_thr_total - svc_thr_active) are already available.
6130Sstevel@tonic-gate * This thread is one of the available threads.
6140Sstevel@tonic-gate *
6150Sstevel@tonic-gate * Number of new threads should not exceed
6160Sstevel@tonic-gate * (svc_thr_max - svc_thr_total).
6170Sstevel@tonic-gate */
6180Sstevel@tonic-gate if (svc_thr_total < svc_thr_max &&
6190Sstevel@tonic-gate svc_mt_mode == RPC_SVC_MT_AUTO && !svc_exit_done) {
6200Sstevel@tonic-gate n_new = 1 + 1 + svc_pollfds + svc_total_pending -
6210Sstevel@tonic-gate (svc_thr_total - svc_thr_active);
6220Sstevel@tonic-gate if (n_new > (svc_thr_max - svc_thr_total))
6230Sstevel@tonic-gate n_new = svc_thr_max - svc_thr_total;
6240Sstevel@tonic-gate if (n_new > 0)
6250Sstevel@tonic-gate start_threads(n_new);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate /*
6290Sstevel@tonic-gate * Get parent xprt. It is possible for the parent service
6300Sstevel@tonic-gate * handle to be destroyed by now, due to a race condition.
6310Sstevel@tonic-gate * Check for this, and if so, log a warning and go on.
6320Sstevel@tonic-gate */
6330Sstevel@tonic-gate parent_xprt = svc_xports[myfd];
6340Sstevel@tonic-gate if (parent_xprt == NULL) {
6350Sstevel@tonic-gate /* Check if it is not a user FD */
6360Sstevel@tonic-gate if (__is_a_userfd(myfd) == TRUE)
6370Sstevel@tonic-gate __svc_getreq_user(&(svc_pollset[mypollfd]));
6380Sstevel@tonic-gate goto continue_with_locks;
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate /* LINTED pointer alignment */
6410Sstevel@tonic-gate if (svc_defunct(parent_xprt) || svc_failed(parent_xprt))
6420Sstevel@tonic-gate goto continue_with_locks;
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate /*
6450Sstevel@tonic-gate * Make a copy of parent xprt, update svc_fdset.
6460Sstevel@tonic-gate */
6470Sstevel@tonic-gate if ((xprt = make_xprt_copy(parent_xprt)) == NULL)
6480Sstevel@tonic-gate goto continue_with_locks;
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate * Keep track of active threads in automatic mode.
6520Sstevel@tonic-gate */
6530Sstevel@tonic-gate if (svc_mt_mode == RPC_SVC_MT_AUTO)
6540Sstevel@tonic-gate svc_thr_active++;
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate /*
6570Sstevel@tonic-gate * Release mutexes so other threads can get going.
6580Sstevel@tonic-gate */
659132Srobinson (void) mutex_unlock(&svc_mutex);
660132Srobinson (void) mutex_unlock(&svc_thr_mutex);
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate /*
6630Sstevel@tonic-gate * Process request.
6640Sstevel@tonic-gate */
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate struct rpc_msg *msg;
6670Sstevel@tonic-gate struct svc_req *r;
6680Sstevel@tonic-gate char *cred_area;
6690Sstevel@tonic-gate
6700Sstevel@tonic-gate /* LINTED pointer alignment */
6710Sstevel@tonic-gate msg = SVCEXT(xprt)->msg;
6720Sstevel@tonic-gate /* LINTED pointer alignment */
6730Sstevel@tonic-gate r = SVCEXT(xprt)->req;
6740Sstevel@tonic-gate /* LINTED pointer alignment */
6750Sstevel@tonic-gate cred_area = SVCEXT(xprt)->cred_area;
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate msg->rm_call.cb_cred.oa_base = cred_area;
6790Sstevel@tonic-gate msg->rm_call.cb_verf.oa_base =
6800Sstevel@tonic-gate &(cred_area[MAX_AUTH_BYTES]);
6810Sstevel@tonic-gate r->rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate /*
6840Sstevel@tonic-gate * receive RPC message
6850Sstevel@tonic-gate */
6860Sstevel@tonic-gate if ((dispatch = SVC_RECV(xprt, msg))) {
6870Sstevel@tonic-gate if (svc_mt_mode != RPC_SVC_MT_NONE)
6880Sstevel@tonic-gate /* LINTED pointer alignment */
6890Sstevel@tonic-gate svc_flags(xprt) |= SVC_ARGS_CHECK;
6900Sstevel@tonic-gate dispatch = _svc_prog_dispatch(xprt, msg, r);
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate * Call cleanup procedure if set.
6940Sstevel@tonic-gate */
6950Sstevel@tonic-gate if (__proc_cleanup_cb != NULL)
6960Sstevel@tonic-gate (*__proc_cleanup_cb)(xprt);
6970Sstevel@tonic-gate } else
6980Sstevel@tonic-gate svc_args_done(xprt);
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate /*
7010Sstevel@tonic-gate * Finish up, if automatic mode, or not dispatched.
7020Sstevel@tonic-gate */
7030Sstevel@tonic-gate if (svc_mt_mode == RPC_SVC_MT_AUTO || !dispatch) {
7040Sstevel@tonic-gate /* LINTED pointer alignment */
7050Sstevel@tonic-gate if (svc_flags(xprt) & SVC_ARGS_CHECK)
7060Sstevel@tonic-gate svc_args_done(xprt);
707132Srobinson (void) mutex_lock(&svc_mutex);
7080Sstevel@tonic-gate _svc_done_private(xprt);
7090Sstevel@tonic-gate if (svc_mt_mode == RPC_SVC_MT_AUTO) {
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate * not active any more
7120Sstevel@tonic-gate */
7130Sstevel@tonic-gate svc_thr_active--;
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate /*
7160Sstevel@tonic-gate * If not main thread, exit unless
7170Sstevel@tonic-gate * there's some immediate work.
7180Sstevel@tonic-gate */
7190Sstevel@tonic-gate if (!main_thread &&
7200Sstevel@tonic-gate svc_pollfds <= 0 &&
7210Sstevel@tonic-gate svc_total_pending <= 0 &&
7220Sstevel@tonic-gate (svc_polling ||
7230Sstevel@tonic-gate svc_waiters > 0)) {
7240Sstevel@tonic-gate svc_thr_total--;
7250Sstevel@tonic-gate if (svc_thr_total ==
7260Sstevel@tonic-gate svc_waiters) {
727132Srobinson (void) cond_broadcast(
7280Sstevel@tonic-gate &svc_thr_fdwait);
7290Sstevel@tonic-gate }
730132Srobinson (void) mutex_unlock(&svc_mutex);
7310Sstevel@tonic-gate thr_exit(NULL);
7320Sstevel@tonic-gate /* NOTREACHED */
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate }
735132Srobinson (void) mutex_unlock(&svc_mutex);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate /*
7440Sstevel@tonic-gate * start_threads() - Start specified number of threads.
7450Sstevel@tonic-gate */
7460Sstevel@tonic-gate static void
start_threads(int num_threads)747132Srobinson start_threads(int num_threads)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate int i;
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate assert(MUTEX_HELD(&svc_mutex));
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate for (i = 0; i < num_threads; i++) {
7540Sstevel@tonic-gate if (thr_create(NULL, 0, (void *(*)(void *))_svc_run_mt, NULL,
7550Sstevel@tonic-gate THR_DETACHED, NULL) == 0) {
7560Sstevel@tonic-gate svc_thr_total++;
7570Sstevel@tonic-gate svc_thr_total_creates++;
7580Sstevel@tonic-gate } else {
7590Sstevel@tonic-gate svc_thr_total_create_errors++;
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate * create_pipe() - create pipe for breaking out of poll.
7670Sstevel@tonic-gate */
7680Sstevel@tonic-gate static void
create_pipe(void)769132Srobinson create_pipe(void)
7700Sstevel@tonic-gate {
7710Sstevel@tonic-gate if (pipe(svc_pipe) == -1) {
7720Sstevel@tonic-gate syslog(LOG_ERR, dgettext(__nsl_dom,
7730Sstevel@tonic-gate "RPC: svc could not create pipe - exiting"));
7740Sstevel@tonic-gate exit(1);
7750Sstevel@tonic-gate }
7761219Sraf if (fcntl(svc_pipe[0], F_SETFL, O_NONBLOCK) == -1) {
7770Sstevel@tonic-gate syslog(LOG_ERR, dgettext(__nsl_dom,
7780Sstevel@tonic-gate "RPC: svc pipe error - exiting"));
7790Sstevel@tonic-gate exit(1);
7800Sstevel@tonic-gate }
7811219Sraf if (fcntl(svc_pipe[1], F_SETFL, O_NONBLOCK) == -1) {
7820Sstevel@tonic-gate syslog(LOG_ERR, dgettext(__nsl_dom,
7830Sstevel@tonic-gate "RPC: svc pipe error - exiting"));
7840Sstevel@tonic-gate exit(1);
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate /*
7900Sstevel@tonic-gate * clear_pipe() - Empty data in pipe.
7910Sstevel@tonic-gate */
7920Sstevel@tonic-gate static void
clear_pipe(void)793132Srobinson clear_pipe(void)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate char buf[16];
7960Sstevel@tonic-gate int i;
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate do {
7990Sstevel@tonic-gate i = read(svc_pipe[0], buf, sizeof (buf));
8000Sstevel@tonic-gate } while (i == sizeof (buf));
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate * select_next_pollfd() - Select the next active fd in svc_pollset.
8060Sstevel@tonic-gate */
8070Sstevel@tonic-gate static int
select_next_pollfd(int * fd,int * pollfdIndex)8080Sstevel@tonic-gate select_next_pollfd(int *fd, int *pollfdIndex)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate int i;
8110Sstevel@tonic-gate
8120Sstevel@tonic-gate assert(MUTEX_HELD(&svc_thr_mutex));
8130Sstevel@tonic-gate assert(MUTEX_HELD(&svc_mutex));
8140Sstevel@tonic-gate
8150Sstevel@tonic-gate for (i = svc_next_pollfd; svc_pollfds > 0 && i < svc_polled;
8160Sstevel@tonic-gate i++) {
8170Sstevel@tonic-gate if (svc_pollset[i].revents) {
8180Sstevel@tonic-gate svc_pollfds--;
8190Sstevel@tonic-gate /*
8200Sstevel@tonic-gate * No more special case for POLLNVAL, because it may
8210Sstevel@tonic-gate * be linked with a user file descriptot callback
8220Sstevel@tonic-gate */
8230Sstevel@tonic-gate svc_next_pollfd = i + 1;
8240Sstevel@tonic-gate
8250Sstevel@tonic-gate *fd = svc_pollset[i].fd;
8260Sstevel@tonic-gate *pollfdIndex = i;
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate return (0);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate svc_next_pollfd = svc_pollfds = 0;
8320Sstevel@tonic-gate *fd = -1;
8330Sstevel@tonic-gate *pollfdIndex = INVALID_POLLFD;
8340Sstevel@tonic-gate return (-1);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate
8380Sstevel@tonic-gate /*
8390Sstevel@tonic-gate * make_xprt_copy() - make a copy of the parent xprt.
8400Sstevel@tonic-gate * Clear fd bit in svc_fdset.
8410Sstevel@tonic-gate */
8420Sstevel@tonic-gate static SVCXPRT *
make_xprt_copy(SVCXPRT * parent)843132Srobinson make_xprt_copy(SVCXPRT *parent)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate /* LINTED pointer alignment */
8460Sstevel@tonic-gate SVCXPRT_LIST *xlist = SVCEXT(parent)->my_xlist;
8470Sstevel@tonic-gate SVCXPRT_LIST *xret;
8480Sstevel@tonic-gate SVCXPRT *xprt;
8490Sstevel@tonic-gate int fd = parent->xp_fd;
8500Sstevel@tonic-gate
8510Sstevel@tonic-gate assert(MUTEX_HELD(&svc_mutex));
8520Sstevel@tonic-gate
8530Sstevel@tonic-gate xret = xlist->next;
8540Sstevel@tonic-gate if (xret) {
8550Sstevel@tonic-gate xlist->next = xret->next;
8560Sstevel@tonic-gate xret->next = NULL;
8570Sstevel@tonic-gate xprt = xret->xprt;
8580Sstevel@tonic-gate /* LINTED pointer alignment */
8590Sstevel@tonic-gate svc_flags(xprt) = svc_flags(parent);
8600Sstevel@tonic-gate } else
8610Sstevel@tonic-gate xprt = svc_copy(parent);
8620Sstevel@tonic-gate
8630Sstevel@tonic-gate if (xprt) {
8640Sstevel@tonic-gate /* LINTED pointer alignment */
8650Sstevel@tonic-gate SVCEXT(parent)->refcnt++;
866132Srobinson (void) rw_wrlock(&svc_fd_lock);
8670Sstevel@tonic-gate clear_pollfd(fd);
868132Srobinson (void) rw_unlock(&svc_fd_lock);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate return (xprt);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate
8730Sstevel@tonic-gate /*
8740Sstevel@tonic-gate * _svc_done_private() - return copies to library.
8750Sstevel@tonic-gate */
8760Sstevel@tonic-gate static void
_svc_done_private(SVCXPRT * xprt)877132Srobinson _svc_done_private(SVCXPRT *xprt)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate SVCXPRT *parent;
8800Sstevel@tonic-gate SVCXPRT_LIST *xhead, *xlist;
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate assert(MUTEX_HELD(&svc_mutex));
8830Sstevel@tonic-gate
8840Sstevel@tonic-gate /* LINTED pointer alignment */
8850Sstevel@tonic-gate if ((parent = SVCEXT(xprt)->parent) == NULL)
8860Sstevel@tonic-gate return;
8870Sstevel@tonic-gate
8880Sstevel@tonic-gate /* LINTED pointer alignment */
8890Sstevel@tonic-gate xhead = SVCEXT(parent)->my_xlist;
8900Sstevel@tonic-gate /* LINTED pointer alignment */
8910Sstevel@tonic-gate xlist = SVCEXT(xprt)->my_xlist;
8920Sstevel@tonic-gate xlist->next = xhead->next;
8930Sstevel@tonic-gate xhead->next = xlist;
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate /* LINTED pointer alignment */
8960Sstevel@tonic-gate SVCEXT(parent)->refcnt--;
8970Sstevel@tonic-gate
8980Sstevel@tonic-gate /*
8990Sstevel@tonic-gate * Propagate any error flags. This is done in both directions to
9000Sstevel@tonic-gate * ensure that if one child gets an error, everyone will see it
9010Sstevel@tonic-gate * (even if there are multiple outstanding children) and the
9020Sstevel@tonic-gate * transport will get closed.
9030Sstevel@tonic-gate */
9040Sstevel@tonic-gate /* LINTED pointer alignment */
9050Sstevel@tonic-gate svc_flags(xprt) |= svc_flags(parent);
9060Sstevel@tonic-gate /* LINTED pointer alignment */
9070Sstevel@tonic-gate if (svc_failed(xprt) || svc_defunct(xprt)) {
9080Sstevel@tonic-gate /* LINTED pointer alignment */
9090Sstevel@tonic-gate svc_flags(parent) |= (svc_flags(xprt) &
9100Sstevel@tonic-gate (SVC_FAILED | SVC_DEFUNCT));
9110Sstevel@tonic-gate /* LINTED pointer alignment */
9120Sstevel@tonic-gate if (SVCEXT(parent)->refcnt == 0)
9130Sstevel@tonic-gate _svc_destroy_private(xprt);
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate
9170Sstevel@tonic-gate void
svc_done(SVCXPRT * xprt)9180Sstevel@tonic-gate svc_done(SVCXPRT *xprt)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate if (svc_mt_mode != RPC_SVC_MT_USER)
9210Sstevel@tonic-gate return;
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate /*
9240Sstevel@tonic-gate * Make sure file descriptor is released in user mode.
9250Sstevel@tonic-gate * If the xprt is a door, do nothing: this work is performed by
9260Sstevel@tonic-gate * svc_door.c's return_xprt_copy() routine, which is basically a
9270Sstevel@tonic-gate * door-specific copy of _svc_done_private().
9280Sstevel@tonic-gate */
9290Sstevel@tonic-gate /* LINTED pointer alignment */
9300Sstevel@tonic-gate if (svc_type(xprt) == SVC_DOOR)
9310Sstevel@tonic-gate return;
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate /* LINTED pointer alignment */
9340Sstevel@tonic-gate if (svc_flags(xprt) & SVC_ARGS_CHECK)
9350Sstevel@tonic-gate svc_args_done(xprt);
9360Sstevel@tonic-gate
937132Srobinson (void) mutex_lock(&svc_mutex);
9380Sstevel@tonic-gate _svc_done_private(xprt);
939132Srobinson (void) mutex_unlock(&svc_mutex);
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate /*
9440Sstevel@tonic-gate * Mark argument completion. Release file descriptor.
9450Sstevel@tonic-gate */
9460Sstevel@tonic-gate void
svc_args_done(SVCXPRT * xprt)947132Srobinson svc_args_done(SVCXPRT *xprt)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate char dummy;
9500Sstevel@tonic-gate /* LINTED pointer alignment */
9510Sstevel@tonic-gate SVCXPRT *parent = SVCEXT(xprt)->parent;
9520Sstevel@tonic-gate bool_t wake_up_poller;
9530Sstevel@tonic-gate enum xprt_stat stat;
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate /* LINTED pointer alignment */
9560Sstevel@tonic-gate svc_flags(xprt) |= svc_flags(parent);
9570Sstevel@tonic-gate /* LINTED pointer alignment */
9580Sstevel@tonic-gate svc_flags(xprt) &= ~SVC_ARGS_CHECK;
9590Sstevel@tonic-gate /* LINTED pointer alignment */
9600Sstevel@tonic-gate if (svc_failed(xprt) || svc_defunct(parent))
9610Sstevel@tonic-gate return;
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate /* LINTED pointer alignment */
9640Sstevel@tonic-gate if (svc_type(xprt) == SVC_CONNECTION &&
9650Sstevel@tonic-gate (stat = SVC_STAT(xprt)) != XPRT_IDLE) {
9660Sstevel@tonic-gate if (stat == XPRT_MOREREQS) {
967132Srobinson (void) mutex_lock(&svc_mutex);
9680Sstevel@tonic-gate svc_pending_fds[svc_last_pending++] = xprt->xp_fd;
9690Sstevel@tonic-gate if (svc_last_pending > CIRCULAR_BUFSIZE)
9700Sstevel@tonic-gate svc_last_pending = 0;
9710Sstevel@tonic-gate svc_total_pending++;
972132Srobinson (void) mutex_unlock(&svc_mutex);
9730Sstevel@tonic-gate wake_up_poller = FALSE;
9740Sstevel@tonic-gate } else {
9750Sstevel@tonic-gate /*
9760Sstevel@tonic-gate * connection failed
9770Sstevel@tonic-gate */
9780Sstevel@tonic-gate return;
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate } else {
981132Srobinson (void) rw_wrlock(&svc_fd_lock);
9820Sstevel@tonic-gate set_pollfd(xprt->xp_fd, MASKVAL);
983132Srobinson (void) rw_unlock(&svc_fd_lock);
9840Sstevel@tonic-gate wake_up_poller = TRUE;
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate
9870Sstevel@tonic-gate if (!wake_up_poller || !svc_polling) {
9880Sstevel@tonic-gate /*
9890Sstevel@tonic-gate * Wake up any waiting threads.
9900Sstevel@tonic-gate */
991132Srobinson (void) mutex_lock(&svc_mutex);
9920Sstevel@tonic-gate if (svc_waiters > 0) {
993132Srobinson (void) cond_broadcast(&svc_thr_fdwait);
994132Srobinson (void) mutex_unlock(&svc_mutex);
9950Sstevel@tonic-gate return;
9960Sstevel@tonic-gate }
997132Srobinson (void) mutex_unlock(&svc_mutex);
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate
10000Sstevel@tonic-gate /*
10010Sstevel@tonic-gate * Wake up any polling thread.
10020Sstevel@tonic-gate */
10030Sstevel@tonic-gate if (svc_polling)
10040Sstevel@tonic-gate (void) write(svc_pipe[1], &dummy, sizeof (dummy));
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate
10070Sstevel@tonic-gate
10080Sstevel@tonic-gate int
__rpc_legal_connmaxrec(int suggested)10090Sstevel@tonic-gate __rpc_legal_connmaxrec(int suggested) {
10100Sstevel@tonic-gate if (suggested == -1) {
10110Sstevel@tonic-gate /* Supply default */
10120Sstevel@tonic-gate return (RPC_MAXDATASIZE + 2*sizeof (uint32_t));
10130Sstevel@tonic-gate } else if (suggested < 0) {
10140Sstevel@tonic-gate return (-1);
10150Sstevel@tonic-gate } else if (suggested > 0) {
10160Sstevel@tonic-gate /* Round down to multiple of BYTES_PER_XDR_UNIT */
10170Sstevel@tonic-gate suggested -= suggested % BYTES_PER_XDR_UNIT;
10180Sstevel@tonic-gate /* If possible, allow for two fragment headers */
10190Sstevel@tonic-gate if (suggested < MAXINT-(2*sizeof (uint32_t))) {
10200Sstevel@tonic-gate /* Allow for two fragment headers */
10210Sstevel@tonic-gate suggested += 2 * sizeof (uint32_t);
10220Sstevel@tonic-gate } else {
10230Sstevel@tonic-gate suggested = MAXINT;
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate if (suggested < sizeof (struct rpc_msg)) {
10260Sstevel@tonic-gate return (-1);
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate return (suggested);
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate bool_t
rpc_control(int op,void * info)1034132Srobinson rpc_control(int op, void *info)
10350Sstevel@tonic-gate {
10360Sstevel@tonic-gate int tmp;
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate switch (op) {
10390Sstevel@tonic-gate case RPC_SVC_MTMODE_SET:
10400Sstevel@tonic-gate tmp = *((int *)info);
10410Sstevel@tonic-gate if (tmp != RPC_SVC_MT_NONE && tmp != RPC_SVC_MT_AUTO &&
10420Sstevel@tonic-gate tmp != RPC_SVC_MT_USER)
10430Sstevel@tonic-gate return (FALSE);
10440Sstevel@tonic-gate if (svc_mt_mode != RPC_SVC_MT_NONE && svc_mt_mode != tmp)
10450Sstevel@tonic-gate return (FALSE);
10460Sstevel@tonic-gate svc_mt_mode = tmp;
10470Sstevel@tonic-gate return (TRUE);
10480Sstevel@tonic-gate case RPC_SVC_MTMODE_GET:
10490Sstevel@tonic-gate *((int *)info) = svc_mt_mode;
10500Sstevel@tonic-gate return (TRUE);
10510Sstevel@tonic-gate case RPC_SVC_THRMAX_SET:
10520Sstevel@tonic-gate if ((tmp = *((int *)info)) < 1)
10530Sstevel@tonic-gate return (FALSE);
1054132Srobinson (void) mutex_lock(&svc_mutex);
10550Sstevel@tonic-gate svc_thr_max = tmp;
1056132Srobinson (void) mutex_unlock(&svc_mutex);
10570Sstevel@tonic-gate return (TRUE);
10580Sstevel@tonic-gate case RPC_SVC_THRMAX_GET:
10590Sstevel@tonic-gate *((int *)info) = svc_thr_max;
10600Sstevel@tonic-gate return (TRUE);
10610Sstevel@tonic-gate case RPC_SVC_THRTOTAL_GET:
10620Sstevel@tonic-gate *((int *)info) = svc_thr_total;
10630Sstevel@tonic-gate return (TRUE);
10640Sstevel@tonic-gate case RPC_SVC_THRCREATES_GET:
10650Sstevel@tonic-gate *((int *)info) = svc_thr_total_creates;
10660Sstevel@tonic-gate return (TRUE);
10670Sstevel@tonic-gate case RPC_SVC_THRERRORS_GET:
10680Sstevel@tonic-gate *((int *)info) = svc_thr_total_create_errors;
10690Sstevel@tonic-gate return (TRUE);
10700Sstevel@tonic-gate case RPC_SVC_USE_POLLFD:
10710Sstevel@tonic-gate if (*((int *)info) && !__rpc_use_pollfd_done) {
10720Sstevel@tonic-gate __rpc_use_pollfd_done = 1;
10730Sstevel@tonic-gate return (TRUE);
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate return (FALSE);
10760Sstevel@tonic-gate case RPC_SVC_CONNMAXREC_SET:
10770Sstevel@tonic-gate tmp = __rpc_legal_connmaxrec(*(int *)info);
10780Sstevel@tonic-gate if (tmp >= 0) {
10790Sstevel@tonic-gate __rpc_connmaxrec = tmp;
10800Sstevel@tonic-gate return (TRUE);
10810Sstevel@tonic-gate } else {
10820Sstevel@tonic-gate return (FALSE);
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate case RPC_SVC_CONNMAXREC_GET:
10850Sstevel@tonic-gate *((int *)info) = __rpc_connmaxrec;
10860Sstevel@tonic-gate return (TRUE);
10870Sstevel@tonic-gate case RPC_SVC_IRTIMEOUT_SET:
10880Sstevel@tonic-gate tmp = *((int *)info);
10890Sstevel@tonic-gate if (tmp >= 0) {
10900Sstevel@tonic-gate __rpc_irtimeout = tmp;
10910Sstevel@tonic-gate return (TRUE);
10920Sstevel@tonic-gate } else {
10930Sstevel@tonic-gate return (FALSE);
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate /*
10960Sstevel@tonic-gate * No mutex necessary as _EXCLBIND_SET will/should only
10970Sstevel@tonic-gate * be used before an RPC daemon goes mt-hot.
10980Sstevel@tonic-gate */
10990Sstevel@tonic-gate case __RPC_SVC_EXCLBIND_SET:
11000Sstevel@tonic-gate if (info) {
11010Sstevel@tonic-gate __rpc_tp_exclbind = *((bool_t *)info);
11020Sstevel@tonic-gate return (TRUE);
11030Sstevel@tonic-gate }
11040Sstevel@tonic-gate return (FALSE);
11050Sstevel@tonic-gate case __RPC_SVC_EXCLBIND_GET:
11060Sstevel@tonic-gate if (info) {
11070Sstevel@tonic-gate *((bool_t *)info) = __rpc_tp_exclbind;
11080Sstevel@tonic-gate return (TRUE);
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate return (FALSE);
11110Sstevel@tonic-gate
11120Sstevel@tonic-gate default:
11130Sstevel@tonic-gate return (FALSE);
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate }
1116