Home | History | Annotate | Line # | Download | only in rpc
svc_vc.c revision 1.21.8.2
      1  1.21.8.2  christos /*	$NetBSD: svc_vc.c,v 1.21.8.2 2008/04/25 17:44:45 christos Exp $	*/
      2  1.21.8.2  christos 
      3  1.21.8.2  christos /*
      4  1.21.8.2  christos  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  1.21.8.2  christos  * unrestricted use provided that this legend is included on all tape
      6  1.21.8.2  christos  * media and as a part of the software program in whole or part.  Users
      7  1.21.8.2  christos  * may copy or modify Sun RPC without charge, but are not authorized
      8  1.21.8.2  christos  * to license or distribute it to anyone else except as part of a product or
      9  1.21.8.2  christos  * program developed by the user.
     10  1.21.8.2  christos  *
     11  1.21.8.2  christos  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  1.21.8.2  christos  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  1.21.8.2  christos  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  1.21.8.2  christos  *
     15  1.21.8.2  christos  * Sun RPC is provided with no support and without any obligation on the
     16  1.21.8.2  christos  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  1.21.8.2  christos  * modification or enhancement.
     18  1.21.8.2  christos  *
     19  1.21.8.2  christos  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  1.21.8.2  christos  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  1.21.8.2  christos  * OR ANY PART THEREOF.
     22  1.21.8.2  christos  *
     23  1.21.8.2  christos  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  1.21.8.2  christos  * or profits or other special, indirect and consequential damages, even if
     25  1.21.8.2  christos  * Sun has been advised of the possibility of such damages.
     26  1.21.8.2  christos  *
     27  1.21.8.2  christos  * Sun Microsystems, Inc.
     28  1.21.8.2  christos  * 2550 Garcia Avenue
     29  1.21.8.2  christos  * Mountain View, California  94043
     30  1.21.8.2  christos  */
     31  1.21.8.2  christos 
     32  1.21.8.2  christos #include <sys/cdefs.h>
     33  1.21.8.2  christos #if defined(LIBC_SCCS) && !defined(lint)
     34  1.21.8.2  christos #if 0
     35  1.21.8.2  christos static char *sccsid = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
     36  1.21.8.2  christos static char *sccsid = "@(#)svc_tcp.c	2.2 88/08/01 4.0 RPCSRC";
     37  1.21.8.2  christos #else
     38  1.21.8.2  christos __RCSID("$NetBSD: svc_vc.c,v 1.21.8.2 2008/04/25 17:44:45 christos Exp $");
     39  1.21.8.2  christos #endif
     40  1.21.8.2  christos #endif
     41  1.21.8.2  christos 
     42  1.21.8.2  christos /*
     43  1.21.8.2  christos  * svc_vc.c, Server side for Connection Oriented based RPC.
     44  1.21.8.2  christos  *
     45  1.21.8.2  christos  * Actually implements two flavors of transporter -
     46  1.21.8.2  christos  * a tcp rendezvouser (a listner and connection establisher)
     47  1.21.8.2  christos  * and a record/tcp stream.
     48  1.21.8.2  christos  */
     49  1.21.8.2  christos 
     50  1.21.8.2  christos #include "namespace.h"
     51  1.21.8.2  christos #include "reentrant.h"
     52  1.21.8.2  christos #include <sys/types.h>
     53  1.21.8.2  christos #include <sys/param.h>
     54  1.21.8.2  christos #include <sys/poll.h>
     55  1.21.8.2  christos #include <sys/socket.h>
     56  1.21.8.2  christos #include <sys/un.h>
     57  1.21.8.2  christos #include <sys/time.h>
     58  1.21.8.2  christos #include <netinet/in.h>
     59  1.21.8.2  christos 
     60  1.21.8.2  christos #include <assert.h>
     61  1.21.8.2  christos #include <err.h>
     62  1.21.8.2  christos #include <errno.h>
     63  1.21.8.2  christos #include <fcntl.h>
     64  1.21.8.2  christos #include <stdio.h>
     65  1.21.8.2  christos #include <stdlib.h>
     66  1.21.8.2  christos #include <string.h>
     67  1.21.8.2  christos #include <unistd.h>
     68  1.21.8.2  christos 
     69  1.21.8.2  christos #include <rpc/rpc.h>
     70  1.21.8.2  christos 
     71  1.21.8.2  christos #include "rpc_internal.h"
     72  1.21.8.2  christos 
     73  1.21.8.2  christos #ifdef __weak_alias
     74  1.21.8.2  christos __weak_alias(svc_fd_create,_svc_fd_create)
     75  1.21.8.2  christos __weak_alias(svc_vc_create,_svc_vc_create)
     76  1.21.8.2  christos #endif
     77  1.21.8.2  christos 
     78  1.21.8.2  christos #ifdef _REENTRANT
     79  1.21.8.2  christos extern rwlock_t svc_fd_lock;
     80  1.21.8.2  christos #endif
     81  1.21.8.2  christos 
     82  1.21.8.2  christos static SVCXPRT *makefd_xprt __P((int, u_int, u_int));
     83  1.21.8.2  christos static bool_t rendezvous_request __P((SVCXPRT *, struct rpc_msg *));
     84  1.21.8.2  christos static enum xprt_stat rendezvous_stat __P((SVCXPRT *));
     85  1.21.8.2  christos static void svc_vc_destroy __P((SVCXPRT *));
     86  1.21.8.2  christos static void __svc_vc_dodestroy __P((SVCXPRT *));
     87  1.21.8.2  christos static int read_vc __P((caddr_t, caddr_t, int));
     88  1.21.8.2  christos static int write_vc __P((caddr_t, caddr_t, int));
     89  1.21.8.2  christos static enum xprt_stat svc_vc_stat __P((SVCXPRT *));
     90  1.21.8.2  christos static bool_t svc_vc_recv __P((SVCXPRT *, struct rpc_msg *));
     91  1.21.8.2  christos static bool_t svc_vc_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     92  1.21.8.2  christos static bool_t svc_vc_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     93  1.21.8.2  christos static bool_t svc_vc_reply __P((SVCXPRT *, struct rpc_msg *));
     94  1.21.8.2  christos static void svc_vc_rendezvous_ops __P((SVCXPRT *));
     95  1.21.8.2  christos static void svc_vc_ops __P((SVCXPRT *));
     96  1.21.8.2  christos static bool_t svc_vc_control __P((SVCXPRT *, const u_int, void *));
     97  1.21.8.2  christos static bool_t svc_vc_rendezvous_control __P((SVCXPRT *, const u_int,
     98  1.21.8.2  christos 					     void *));
     99  1.21.8.2  christos 
    100  1.21.8.2  christos struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
    101  1.21.8.2  christos 	u_int sendsize;
    102  1.21.8.2  christos 	u_int recvsize;
    103  1.21.8.2  christos 	int maxrec;
    104  1.21.8.2  christos };
    105  1.21.8.2  christos 
    106  1.21.8.2  christos struct cf_conn {  /* kept in xprt->xp_p1 for actual connection */
    107  1.21.8.2  christos 	enum xprt_stat strm_stat;
    108  1.21.8.2  christos 	u_int32_t x_id;
    109  1.21.8.2  christos 	XDR xdrs;
    110  1.21.8.2  christos 	char verf_body[MAX_AUTH_BYTES];
    111  1.21.8.2  christos 	u_int sendsize;
    112  1.21.8.2  christos 	u_int recvsize;
    113  1.21.8.2  christos 	int maxrec;
    114  1.21.8.2  christos 	bool_t nonblock;
    115  1.21.8.2  christos 	struct timeval last_recv_time;
    116  1.21.8.2  christos };
    117  1.21.8.2  christos 
    118  1.21.8.2  christos /*
    119  1.21.8.2  christos  * Usage:
    120  1.21.8.2  christos  *	xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
    121  1.21.8.2  christos  *
    122  1.21.8.2  christos  * Creates, registers, and returns a (rpc) tcp based transporter.
    123  1.21.8.2  christos  * Once *xprt is initialized, it is registered as a transporter
    124  1.21.8.2  christos  * see (svc.h, xprt_register).  This routine returns
    125  1.21.8.2  christos  * a NULL if a problem occurred.
    126  1.21.8.2  christos  *
    127  1.21.8.2  christos  * The filedescriptor passed in is expected to refer to a bound, but
    128  1.21.8.2  christos  * not yet connected socket.
    129  1.21.8.2  christos  *
    130  1.21.8.2  christos  * Since streams do buffered io similar to stdio, the caller can specify
    131  1.21.8.2  christos  * how big the send and receive buffers are via the second and third parms;
    132  1.21.8.2  christos  * 0 => use the system default.
    133  1.21.8.2  christos  */
    134  1.21.8.2  christos SVCXPRT *
    135  1.21.8.2  christos svc_vc_create(fd, sendsize, recvsize)
    136  1.21.8.2  christos 	int fd;
    137  1.21.8.2  christos 	u_int sendsize;
    138  1.21.8.2  christos 	u_int recvsize;
    139  1.21.8.2  christos {
    140  1.21.8.2  christos 	SVCXPRT *xprt;
    141  1.21.8.2  christos 	struct cf_rendezvous *r = NULL;
    142  1.21.8.2  christos 	struct __rpc_sockinfo si;
    143  1.21.8.2  christos 	struct sockaddr_storage sslocal;
    144  1.21.8.2  christos 	socklen_t slen;
    145  1.21.8.2  christos 	int one = 1;
    146  1.21.8.2  christos 
    147  1.21.8.2  christos 	if (!__rpc_fd2sockinfo(fd, &si))
    148  1.21.8.2  christos 		return NULL;
    149  1.21.8.2  christos 
    150  1.21.8.2  christos 	r = mem_alloc(sizeof(*r));
    151  1.21.8.2  christos 	if (r == NULL) {
    152  1.21.8.2  christos 		warnx("svc_vc_create: out of memory");
    153  1.21.8.2  christos 		return NULL;
    154  1.21.8.2  christos 	}
    155  1.21.8.2  christos 	r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
    156  1.21.8.2  christos 	r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
    157  1.21.8.2  christos 	r->maxrec = __svc_maxrec;
    158  1.21.8.2  christos 	xprt = mem_alloc(sizeof(SVCXPRT));
    159  1.21.8.2  christos 	if (xprt == NULL) {
    160  1.21.8.2  christos 		warnx("svc_vc_create: out of memory");
    161  1.21.8.2  christos 		goto cleanup_svc_vc_create;
    162  1.21.8.2  christos 	}
    163  1.21.8.2  christos 	xprt->xp_tp = NULL;
    164  1.21.8.2  christos 	xprt->xp_p1 = (caddr_t)(void *)r;
    165  1.21.8.2  christos 	xprt->xp_p2 = NULL;
    166  1.21.8.2  christos 	xprt->xp_p3 = NULL;
    167  1.21.8.2  christos 	xprt->xp_verf = _null_auth;
    168  1.21.8.2  christos 	svc_vc_rendezvous_ops(xprt);
    169  1.21.8.2  christos 	xprt->xp_port = (u_short)-1;	/* It is the rendezvouser */
    170  1.21.8.2  christos 	xprt->xp_fd = fd;
    171  1.21.8.2  christos 
    172  1.21.8.2  christos 	slen = sizeof (struct sockaddr_storage);
    173  1.21.8.2  christos 	if (getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
    174  1.21.8.2  christos 		warnx("svc_vc_create: could not retrieve local addr");
    175  1.21.8.2  christos 		goto cleanup_svc_vc_create;
    176  1.21.8.2  christos 	}
    177  1.21.8.2  christos 
    178  1.21.8.2  christos 	/*
    179  1.21.8.2  christos 	 * We want to be able to check credentials on local sockets.
    180  1.21.8.2  christos 	 */
    181  1.21.8.2  christos 	if (sslocal.ss_family == AF_LOCAL)
    182  1.21.8.2  christos 		if (setsockopt(fd, 0, LOCAL_CREDS, &one, sizeof one) < 0)
    183  1.21.8.2  christos 			goto cleanup_svc_vc_create;
    184  1.21.8.2  christos 
    185  1.21.8.2  christos 	xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
    186  1.21.8.2  christos 	xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
    187  1.21.8.2  christos 	if (xprt->xp_ltaddr.buf == NULL) {
    188  1.21.8.2  christos 		warnx("svc_vc_create: no mem for local addr");
    189  1.21.8.2  christos 		goto cleanup_svc_vc_create;
    190  1.21.8.2  christos 	}
    191  1.21.8.2  christos 	memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
    192  1.21.8.2  christos 
    193  1.21.8.2  christos 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
    194  1.21.8.2  christos 	xprt_register(xprt);
    195  1.21.8.2  christos 	return (xprt);
    196  1.21.8.2  christos cleanup_svc_vc_create:
    197  1.21.8.2  christos 	if (xprt)
    198  1.21.8.2  christos 		mem_free(xprt, sizeof(*xprt));
    199  1.21.8.2  christos 	if (r != NULL)
    200  1.21.8.2  christos 		mem_free(r, sizeof(*r));
    201  1.21.8.2  christos 	return (NULL);
    202  1.21.8.2  christos }
    203  1.21.8.2  christos 
    204  1.21.8.2  christos /*
    205  1.21.8.2  christos  * Like svtcp_create(), except the routine takes any *open* UNIX file
    206  1.21.8.2  christos  * descriptor as its first input.
    207  1.21.8.2  christos  */
    208  1.21.8.2  christos SVCXPRT *
    209  1.21.8.2  christos svc_fd_create(fd, sendsize, recvsize)
    210  1.21.8.2  christos 	int fd;
    211  1.21.8.2  christos 	u_int sendsize;
    212  1.21.8.2  christos 	u_int recvsize;
    213  1.21.8.2  christos {
    214  1.21.8.2  christos 	struct sockaddr_storage ss;
    215  1.21.8.2  christos 	socklen_t slen;
    216  1.21.8.2  christos 	SVCXPRT *ret;
    217  1.21.8.2  christos 
    218  1.21.8.2  christos 	_DIAGASSERT(fd != -1);
    219  1.21.8.2  christos 
    220  1.21.8.2  christos 	ret = makefd_xprt(fd, sendsize, recvsize);
    221  1.21.8.2  christos 	if (ret == NULL)
    222  1.21.8.2  christos 		return NULL;
    223  1.21.8.2  christos 
    224  1.21.8.2  christos 	slen = sizeof (struct sockaddr_storage);
    225  1.21.8.2  christos 	if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    226  1.21.8.2  christos 		warnx("svc_fd_create: could not retrieve local addr");
    227  1.21.8.2  christos 		goto freedata;
    228  1.21.8.2  christos 	}
    229  1.21.8.2  christos 	ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
    230  1.21.8.2  christos 	ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
    231  1.21.8.2  christos 	if (ret->xp_ltaddr.buf == NULL) {
    232  1.21.8.2  christos 		warnx("svc_fd_create: no mem for local addr");
    233  1.21.8.2  christos 		goto freedata;
    234  1.21.8.2  christos 	}
    235  1.21.8.2  christos 	memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
    236  1.21.8.2  christos 
    237  1.21.8.2  christos 	slen = sizeof (struct sockaddr_storage);
    238  1.21.8.2  christos 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    239  1.21.8.2  christos 		warnx("svc_fd_create: could not retrieve remote addr");
    240  1.21.8.2  christos 		goto freedata;
    241  1.21.8.2  christos 	}
    242  1.21.8.2  christos 	ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
    243  1.21.8.2  christos 	ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
    244  1.21.8.2  christos 	if (ret->xp_rtaddr.buf == NULL) {
    245  1.21.8.2  christos 		warnx("svc_fd_create: no mem for local addr");
    246  1.21.8.2  christos 		goto freedata;
    247  1.21.8.2  christos 	}
    248  1.21.8.2  christos 	memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
    249  1.21.8.2  christos #ifdef PORTMAP
    250  1.21.8.2  christos 	if (ss.ss_family == AF_INET) {
    251  1.21.8.2  christos 		ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
    252  1.21.8.2  christos 		ret->xp_addrlen = sizeof (struct sockaddr_in);
    253  1.21.8.2  christos 	}
    254  1.21.8.2  christos #endif
    255  1.21.8.2  christos 
    256  1.21.8.2  christos 	return ret;
    257  1.21.8.2  christos 
    258  1.21.8.2  christos freedata:
    259  1.21.8.2  christos 	if (ret->xp_ltaddr.buf != NULL)
    260  1.21.8.2  christos 		mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
    261  1.21.8.2  christos 
    262  1.21.8.2  christos 	return NULL;
    263  1.21.8.2  christos }
    264  1.21.8.2  christos 
    265  1.21.8.2  christos static SVCXPRT *
    266  1.21.8.2  christos makefd_xprt(fd, sendsize, recvsize)
    267  1.21.8.2  christos 	int fd;
    268  1.21.8.2  christos 	u_int sendsize;
    269  1.21.8.2  christos 	u_int recvsize;
    270  1.21.8.2  christos {
    271  1.21.8.2  christos 	SVCXPRT *xprt;
    272  1.21.8.2  christos 	struct cf_conn *cd;
    273  1.21.8.2  christos 	const char *netid;
    274  1.21.8.2  christos 	struct __rpc_sockinfo si;
    275  1.21.8.2  christos 
    276  1.21.8.2  christos 	_DIAGASSERT(fd != -1);
    277  1.21.8.2  christos 
    278  1.21.8.2  christos 	xprt = mem_alloc(sizeof(SVCXPRT));
    279  1.21.8.2  christos 	if (xprt == NULL)
    280  1.21.8.2  christos 		goto out;
    281  1.21.8.2  christos 	memset(xprt, 0, sizeof *xprt);
    282  1.21.8.2  christos 	cd = mem_alloc(sizeof(struct cf_conn));
    283  1.21.8.2  christos 	if (cd == NULL)
    284  1.21.8.2  christos 		goto out;
    285  1.21.8.2  christos 	cd->strm_stat = XPRT_IDLE;
    286  1.21.8.2  christos 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
    287  1.21.8.2  christos 	    (caddr_t)(void *)xprt, read_vc, write_vc);
    288  1.21.8.2  christos 	xprt->xp_p1 = (caddr_t)(void *)cd;
    289  1.21.8.2  christos 	xprt->xp_verf.oa_base = cd->verf_body;
    290  1.21.8.2  christos 	svc_vc_ops(xprt);  /* truely deals with calls */
    291  1.21.8.2  christos 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
    292  1.21.8.2  christos 	xprt->xp_fd = fd;
    293  1.21.8.2  christos 	if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
    294  1.21.8.2  christos 		if ((xprt->xp_netid = strdup(netid)) == NULL)
    295  1.21.8.2  christos 			goto out;
    296  1.21.8.2  christos 
    297  1.21.8.2  christos 	xprt_register(xprt);
    298  1.21.8.2  christos 	return (xprt);
    299  1.21.8.2  christos out:
    300  1.21.8.2  christos 	warn("svc_tcp: makefd_xprt");
    301  1.21.8.2  christos 	if (xprt)
    302  1.21.8.2  christos 		mem_free(xprt, sizeof(SVCXPRT));
    303  1.21.8.2  christos 	return NULL;
    304  1.21.8.2  christos }
    305  1.21.8.2  christos 
    306  1.21.8.2  christos /*ARGSUSED*/
    307  1.21.8.2  christos static bool_t
    308  1.21.8.2  christos rendezvous_request(xprt, msg)
    309  1.21.8.2  christos 	SVCXPRT *xprt;
    310  1.21.8.2  christos 	struct rpc_msg *msg;
    311  1.21.8.2  christos {
    312  1.21.8.2  christos 	int sock, flags;
    313  1.21.8.2  christos 	struct cf_rendezvous *r;
    314  1.21.8.2  christos 	struct cf_conn *cd;
    315  1.21.8.2  christos 	struct sockaddr_storage addr;
    316  1.21.8.2  christos 	socklen_t len;
    317  1.21.8.2  christos 	struct __rpc_sockinfo si;
    318  1.21.8.2  christos 	SVCXPRT *newxprt;
    319  1.21.8.2  christos 	fd_set cleanfds;
    320  1.21.8.2  christos 
    321  1.21.8.2  christos 	_DIAGASSERT(xprt != NULL);
    322  1.21.8.2  christos 	_DIAGASSERT(msg != NULL);
    323  1.21.8.2  christos 
    324  1.21.8.2  christos 	r = (struct cf_rendezvous *)xprt->xp_p1;
    325  1.21.8.2  christos again:
    326  1.21.8.2  christos 	len = sizeof addr;
    327  1.21.8.2  christos 	if ((sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
    328  1.21.8.2  christos 	    &len)) < 0) {
    329  1.21.8.2  christos 		if (errno == EINTR)
    330  1.21.8.2  christos 			goto again;
    331  1.21.8.2  christos 		/*
    332  1.21.8.2  christos 		 * Clean out the most idle file descriptor when we're
    333  1.21.8.2  christos 		 * running out.
    334  1.21.8.2  christos 		 */
    335  1.21.8.2  christos 		if (errno == EMFILE || errno == ENFILE) {
    336  1.21.8.2  christos 			cleanfds = svc_fdset;
    337  1.21.8.2  christos 			if (__svc_clean_idle(&cleanfds, 0, FALSE))
    338  1.21.8.2  christos 				goto again;
    339  1.21.8.2  christos 		}
    340  1.21.8.2  christos 		return (FALSE);
    341  1.21.8.2  christos 	}
    342  1.21.8.2  christos 	/*
    343  1.21.8.2  christos 	 * make a new transporter (re-uses xprt)
    344  1.21.8.2  christos 	 */
    345  1.21.8.2  christos 	newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
    346  1.21.8.2  christos 	if (newxprt == NULL)
    347  1.21.8.2  christos 		goto out;
    348  1.21.8.2  christos 	newxprt->xp_rtaddr.buf = mem_alloc(len);
    349  1.21.8.2  christos 	if (newxprt->xp_rtaddr.buf == NULL)
    350  1.21.8.2  christos 		goto out;
    351  1.21.8.2  christos 	memcpy(newxprt->xp_rtaddr.buf, &addr, len);
    352  1.21.8.2  christos 	newxprt->xp_rtaddr.len = len;
    353  1.21.8.2  christos #ifdef PORTMAP
    354  1.21.8.2  christos 	if (addr.ss_family == AF_INET) {
    355  1.21.8.2  christos 		newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
    356  1.21.8.2  christos 		newxprt->xp_addrlen = sizeof (struct sockaddr_in);
    357  1.21.8.2  christos 	}
    358  1.21.8.2  christos #endif
    359  1.21.8.2  christos 	if (__rpc_fd2sockinfo(sock, &si))
    360  1.21.8.2  christos 		__rpc_setnodelay(sock, &si);
    361  1.21.8.2  christos 
    362  1.21.8.2  christos 	cd = (struct cf_conn *)newxprt->xp_p1;
    363  1.21.8.2  christos 
    364  1.21.8.2  christos 	cd->recvsize = r->recvsize;
    365  1.21.8.2  christos 	cd->sendsize = r->sendsize;
    366  1.21.8.2  christos 	cd->maxrec = r->maxrec;
    367  1.21.8.2  christos 
    368  1.21.8.2  christos 	if (cd->maxrec != 0) {
    369  1.21.8.2  christos 		flags = fcntl(sock, F_GETFL, 0);
    370  1.21.8.2  christos 		if (flags  == -1)
    371  1.21.8.2  christos 			goto out;
    372  1.21.8.2  christos 		if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
    373  1.21.8.2  christos 			goto out;
    374  1.21.8.2  christos 		if (cd->recvsize > cd->maxrec)
    375  1.21.8.2  christos 			cd->recvsize = cd->maxrec;
    376  1.21.8.2  christos 		cd->nonblock = TRUE;
    377  1.21.8.2  christos 		__xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
    378  1.21.8.2  christos 	} else
    379  1.21.8.2  christos 		cd->nonblock = FALSE;
    380  1.21.8.2  christos 
    381  1.21.8.2  christos 	(void)gettimeofday(&cd->last_recv_time, NULL);
    382  1.21.8.2  christos 
    383  1.21.8.2  christos 	return (FALSE); /* there is never an rpc msg to be processed */
    384  1.21.8.2  christos out:
    385  1.21.8.2  christos 	(void)close(sock);
    386  1.21.8.2  christos 	return (FALSE); /* there was an error */
    387  1.21.8.2  christos }
    388  1.21.8.2  christos 
    389  1.21.8.2  christos /*ARGSUSED*/
    390  1.21.8.2  christos static enum xprt_stat
    391  1.21.8.2  christos rendezvous_stat(xprt)
    392  1.21.8.2  christos 	SVCXPRT *xprt;
    393  1.21.8.2  christos {
    394  1.21.8.2  christos 
    395  1.21.8.2  christos 	return (XPRT_IDLE);
    396  1.21.8.2  christos }
    397  1.21.8.2  christos 
    398  1.21.8.2  christos static void
    399  1.21.8.2  christos svc_vc_destroy(xprt)
    400  1.21.8.2  christos 	SVCXPRT *xprt;
    401  1.21.8.2  christos {
    402  1.21.8.2  christos 	_DIAGASSERT(xprt != NULL);
    403  1.21.8.2  christos 
    404  1.21.8.2  christos 	xprt_unregister(xprt);
    405  1.21.8.2  christos 	__svc_vc_dodestroy(xprt);
    406  1.21.8.2  christos }
    407  1.21.8.2  christos 
    408  1.21.8.2  christos static void
    409  1.21.8.2  christos __svc_vc_dodestroy(xprt)
    410  1.21.8.2  christos 	SVCXPRT *xprt;
    411  1.21.8.2  christos {
    412  1.21.8.2  christos 	struct cf_conn *cd;
    413  1.21.8.2  christos 	struct cf_rendezvous *r;
    414  1.21.8.2  christos 
    415  1.21.8.2  christos 	cd = (struct cf_conn *)xprt->xp_p1;
    416  1.21.8.2  christos 
    417  1.21.8.2  christos 	if (xprt->xp_fd != RPC_ANYFD)
    418  1.21.8.2  christos 		(void)close(xprt->xp_fd);
    419  1.21.8.2  christos 	if (xprt->xp_port != 0) {
    420  1.21.8.2  christos 		/* a rendezvouser socket */
    421  1.21.8.2  christos 		r = (struct cf_rendezvous *)xprt->xp_p1;
    422  1.21.8.2  christos 		mem_free(r, sizeof (struct cf_rendezvous));
    423  1.21.8.2  christos 		xprt->xp_port = 0;
    424  1.21.8.2  christos 	} else {
    425  1.21.8.2  christos 		/* an actual connection socket */
    426  1.21.8.2  christos 		XDR_DESTROY(&(cd->xdrs));
    427  1.21.8.2  christos 		mem_free(cd, sizeof(struct cf_conn));
    428  1.21.8.2  christos 	}
    429  1.21.8.2  christos 	if (xprt->xp_rtaddr.buf)
    430  1.21.8.2  christos 		mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
    431  1.21.8.2  christos 	if (xprt->xp_ltaddr.buf)
    432  1.21.8.2  christos 		mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
    433  1.21.8.2  christos 	if (xprt->xp_tp)
    434  1.21.8.2  christos 		free(xprt->xp_tp);
    435  1.21.8.2  christos 	if (xprt->xp_netid)
    436  1.21.8.2  christos 		free(xprt->xp_netid);
    437  1.21.8.2  christos 	mem_free(xprt, sizeof(SVCXPRT));
    438  1.21.8.2  christos }
    439  1.21.8.2  christos 
    440  1.21.8.2  christos /*ARGSUSED*/
    441  1.21.8.2  christos static bool_t
    442  1.21.8.2  christos svc_vc_control(xprt, rq, in)
    443  1.21.8.2  christos 	SVCXPRT *xprt;
    444  1.21.8.2  christos 	const u_int rq;
    445  1.21.8.2  christos 	void *in;
    446  1.21.8.2  christos {
    447  1.21.8.2  christos 	return (FALSE);
    448  1.21.8.2  christos }
    449  1.21.8.2  christos 
    450  1.21.8.2  christos /*ARGSUSED*/
    451  1.21.8.2  christos static bool_t
    452  1.21.8.2  christos svc_vc_rendezvous_control(xprt, rq, in)
    453  1.21.8.2  christos 	SVCXPRT *xprt;
    454  1.21.8.2  christos 	const u_int rq;
    455  1.21.8.2  christos 	void *in;
    456  1.21.8.2  christos {
    457  1.21.8.2  christos 	struct cf_rendezvous *cfp;
    458  1.21.8.2  christos 
    459  1.21.8.2  christos 	cfp = (struct cf_rendezvous *)xprt->xp_p1;
    460  1.21.8.2  christos 	if (cfp == NULL)
    461  1.21.8.2  christos 		return (FALSE);
    462  1.21.8.2  christos 	switch (rq) {
    463  1.21.8.2  christos 		case SVCGET_CONNMAXREC:
    464  1.21.8.2  christos 			*(int *)in = cfp->maxrec;
    465  1.21.8.2  christos 			break;
    466  1.21.8.2  christos 		case SVCSET_CONNMAXREC:
    467  1.21.8.2  christos 			cfp->maxrec = *(int *)in;
    468  1.21.8.2  christos 			break;
    469  1.21.8.2  christos 		default:
    470  1.21.8.2  christos 			return (FALSE);
    471  1.21.8.2  christos 	}
    472  1.21.8.2  christos 	return (TRUE);
    473  1.21.8.2  christos }
    474  1.21.8.2  christos 
    475  1.21.8.2  christos /*
    476  1.21.8.2  christos  * reads data from the tcp connection.
    477  1.21.8.2  christos  * any error is fatal and the connection is closed.
    478  1.21.8.2  christos  * (And a read of zero bytes is a half closed stream => error.)
    479  1.21.8.2  christos  * All read operations timeout after 35 seconds.  A timeout is
    480  1.21.8.2  christos  * fatal for the connection.
    481  1.21.8.2  christos  */
    482  1.21.8.2  christos static int
    483  1.21.8.2  christos read_vc(xprtp, buf, len)
    484  1.21.8.2  christos 	caddr_t xprtp;
    485  1.21.8.2  christos 	caddr_t buf;
    486  1.21.8.2  christos 	int len;
    487  1.21.8.2  christos {
    488  1.21.8.2  christos 	SVCXPRT *xprt;
    489  1.21.8.2  christos 	int sock;
    490  1.21.8.2  christos 	struct pollfd pollfd;
    491  1.21.8.2  christos 	struct sockaddr *sa;
    492  1.21.8.2  christos 	struct msghdr msg;
    493  1.21.8.2  christos 	struct cmsghdr *cmp;
    494  1.21.8.2  christos 	void *crmsg = NULL;
    495  1.21.8.2  christos 	struct sockcred *sc;
    496  1.21.8.2  christos 	socklen_t crmsgsize;
    497  1.21.8.2  christos 	struct cf_conn *cfp;
    498  1.21.8.2  christos 	static const struct timespec ts = { 35, 0 };
    499  1.21.8.2  christos 
    500  1.21.8.2  christos 	xprt = (SVCXPRT *)(void *)xprtp;
    501  1.21.8.2  christos 	_DIAGASSERT(xprt != NULL);
    502  1.21.8.2  christos 
    503  1.21.8.2  christos 	sock = xprt->xp_fd;
    504  1.21.8.2  christos 
    505  1.21.8.2  christos 	sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
    506  1.21.8.2  christos 	if (sa->sa_family == AF_LOCAL && xprt->xp_p2 == NULL) {
    507  1.21.8.2  christos 		memset(&msg, 0, sizeof msg);
    508  1.21.8.2  christos 		crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
    509  1.21.8.2  christos 		crmsg = malloc(crmsgsize);
    510  1.21.8.2  christos 		if (crmsg == NULL)
    511  1.21.8.2  christos 			goto fatal_err;
    512  1.21.8.2  christos 		memset(crmsg, 0, crmsgsize);
    513  1.21.8.2  christos 
    514  1.21.8.2  christos 		msg.msg_control = crmsg;
    515  1.21.8.2  christos 		msg.msg_controllen = crmsgsize;
    516  1.21.8.2  christos 
    517  1.21.8.2  christos 		if (recvmsg(sock, &msg, 0) < 0)
    518  1.21.8.2  christos 			goto fatal_err;
    519  1.21.8.2  christos 
    520  1.21.8.2  christos 		if (msg.msg_controllen == 0 ||
    521  1.21.8.2  christos 		    (msg.msg_flags & MSG_CTRUNC) != 0)
    522  1.21.8.2  christos 			goto fatal_err;
    523  1.21.8.2  christos 
    524  1.21.8.2  christos 		cmp = CMSG_FIRSTHDR(&msg);
    525  1.21.8.2  christos 		if (cmp->cmsg_level != SOL_SOCKET ||
    526  1.21.8.2  christos 		    cmp->cmsg_type != SCM_CREDS)
    527  1.21.8.2  christos 			goto fatal_err;
    528  1.21.8.2  christos 
    529  1.21.8.2  christos 		sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
    530  1.21.8.2  christos 
    531  1.21.8.2  christos 		xprt->xp_p2 = mem_alloc(SOCKCREDSIZE(sc->sc_ngroups));
    532  1.21.8.2  christos 		if (xprt->xp_p2 == NULL)
    533  1.21.8.2  christos 			goto fatal_err;
    534  1.21.8.2  christos 
    535  1.21.8.2  christos 		memcpy(xprt->xp_p2, sc, SOCKCREDSIZE(sc->sc_ngroups));
    536  1.21.8.2  christos 		free(crmsg);
    537  1.21.8.2  christos 		crmsg = NULL;
    538  1.21.8.2  christos 	}
    539  1.21.8.2  christos 
    540  1.21.8.2  christos 	cfp = (struct cf_conn *)xprt->xp_p1;
    541  1.21.8.2  christos 
    542  1.21.8.2  christos 	if (cfp->nonblock) {
    543  1.21.8.2  christos 		len = read(sock, buf, (size_t)len);
    544  1.21.8.2  christos 		if (len < 0) {
    545  1.21.8.2  christos 			if (errno == EAGAIN)
    546  1.21.8.2  christos 				len = 0;
    547  1.21.8.2  christos 			else
    548  1.21.8.2  christos 				goto fatal_err;
    549  1.21.8.2  christos 		}
    550  1.21.8.2  christos 		if (len != 0)
    551  1.21.8.2  christos 			gettimeofday(&cfp->last_recv_time, NULL);
    552  1.21.8.2  christos 		return len;
    553  1.21.8.2  christos 	}
    554  1.21.8.2  christos 
    555  1.21.8.2  christos 	do {
    556  1.21.8.2  christos 		pollfd.fd = sock;
    557  1.21.8.2  christos 		pollfd.events = POLLIN;
    558  1.21.8.2  christos 		switch (pollts(&pollfd, 1, &ts, NULL)) {
    559  1.21.8.2  christos 		case -1:
    560  1.21.8.2  christos 			if (errno == EINTR) {
    561  1.21.8.2  christos 				continue;
    562  1.21.8.2  christos 			}
    563  1.21.8.2  christos 			/*FALLTHROUGH*/
    564  1.21.8.2  christos 		case 0:
    565  1.21.8.2  christos 			goto fatal_err;
    566  1.21.8.2  christos 
    567  1.21.8.2  christos 		default:
    568  1.21.8.2  christos 			break;
    569  1.21.8.2  christos 		}
    570  1.21.8.2  christos 	} while ((pollfd.revents & POLLIN) == 0);
    571  1.21.8.2  christos 
    572  1.21.8.2  christos 	if ((len = read(sock, buf, (size_t)len)) > 0) {
    573  1.21.8.2  christos 		gettimeofday(&cfp->last_recv_time, NULL);
    574  1.21.8.2  christos 		return (len);
    575  1.21.8.2  christos 	}
    576  1.21.8.2  christos 
    577  1.21.8.2  christos fatal_err:
    578  1.21.8.2  christos 	if (crmsg != NULL)
    579  1.21.8.2  christos 		free(crmsg);
    580  1.21.8.2  christos 	((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
    581  1.21.8.2  christos 	return (-1);
    582  1.21.8.2  christos }
    583  1.21.8.2  christos 
    584  1.21.8.2  christos /*
    585  1.21.8.2  christos  * writes data to the tcp connection.
    586  1.21.8.2  christos  * Any error is fatal and the connection is closed.
    587  1.21.8.2  christos  */
    588  1.21.8.2  christos static int
    589  1.21.8.2  christos write_vc(xprtp, buf, len)
    590  1.21.8.2  christos 	caddr_t xprtp;
    591  1.21.8.2  christos 	caddr_t buf;
    592  1.21.8.2  christos 	int len;
    593  1.21.8.2  christos {
    594  1.21.8.2  christos 	SVCXPRT *xprt;
    595  1.21.8.2  christos 	int i, cnt;
    596  1.21.8.2  christos 	struct cf_conn *cd;
    597  1.21.8.2  christos 	struct timeval tv0, tv1;
    598  1.21.8.2  christos 
    599  1.21.8.2  christos 	xprt = (SVCXPRT *)(void *)xprtp;
    600  1.21.8.2  christos 	_DIAGASSERT(xprt != NULL);
    601  1.21.8.2  christos 
    602  1.21.8.2  christos 	cd = (struct cf_conn *)xprt->xp_p1;
    603  1.21.8.2  christos 
    604  1.21.8.2  christos 	if (cd->nonblock)
    605  1.21.8.2  christos 		gettimeofday(&tv0, NULL);
    606  1.21.8.2  christos 
    607  1.21.8.2  christos 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
    608  1.21.8.2  christos 		if ((i = write(xprt->xp_fd, buf, (size_t)cnt)) < 0) {
    609  1.21.8.2  christos 			if (errno != EAGAIN || !cd->nonblock) {
    610  1.21.8.2  christos 				cd->strm_stat = XPRT_DIED;
    611  1.21.8.2  christos 				return (-1);
    612  1.21.8.2  christos 			}
    613  1.21.8.2  christos 			if (cd->nonblock && i != cnt) {
    614  1.21.8.2  christos 				/*
    615  1.21.8.2  christos 				 * For non-blocking connections, do not
    616  1.21.8.2  christos 				 * take more than 2 seconds writing the
    617  1.21.8.2  christos 				 * data out.
    618  1.21.8.2  christos 				 *
    619  1.21.8.2  christos 				 * XXX 2 is an arbitrary amount.
    620  1.21.8.2  christos 				 */
    621  1.21.8.2  christos 				gettimeofday(&tv1, NULL);
    622  1.21.8.2  christos 				if (tv1.tv_sec - tv0.tv_sec >= 2) {
    623  1.21.8.2  christos 					cd->strm_stat = XPRT_DIED;
    624  1.21.8.2  christos 					return (-1);
    625  1.21.8.2  christos 				}
    626  1.21.8.2  christos 			}
    627  1.21.8.2  christos 		}
    628  1.21.8.2  christos 	}
    629  1.21.8.2  christos 	return (len);
    630  1.21.8.2  christos }
    631  1.21.8.2  christos 
    632  1.21.8.2  christos static enum xprt_stat
    633  1.21.8.2  christos svc_vc_stat(xprt)
    634  1.21.8.2  christos 	SVCXPRT *xprt;
    635  1.21.8.2  christos {
    636  1.21.8.2  christos 	struct cf_conn *cd;
    637  1.21.8.2  christos 
    638  1.21.8.2  christos 	_DIAGASSERT(xprt != NULL);
    639  1.21.8.2  christos 
    640  1.21.8.2  christos 	cd = (struct cf_conn *)(xprt->xp_p1);
    641  1.21.8.2  christos 
    642  1.21.8.2  christos 	if (cd->strm_stat == XPRT_DIED)
    643  1.21.8.2  christos 		return (XPRT_DIED);
    644  1.21.8.2  christos 	if (! xdrrec_eof(&(cd->xdrs)))
    645  1.21.8.2  christos 		return (XPRT_MOREREQS);
    646  1.21.8.2  christos 	return (XPRT_IDLE);
    647  1.21.8.2  christos }
    648  1.21.8.2  christos 
    649  1.21.8.2  christos static bool_t
    650  1.21.8.2  christos svc_vc_recv(xprt, msg)
    651  1.21.8.2  christos 	SVCXPRT *xprt;
    652  1.21.8.2  christos 	struct rpc_msg *msg;
    653  1.21.8.2  christos {
    654  1.21.8.2  christos 	struct cf_conn *cd;
    655  1.21.8.2  christos 	XDR *xdrs;
    656  1.21.8.2  christos 
    657  1.21.8.2  christos 	_DIAGASSERT(xprt != NULL);
    658  1.21.8.2  christos 	_DIAGASSERT(msg != NULL);
    659  1.21.8.2  christos 
    660  1.21.8.2  christos 	cd = (struct cf_conn *)(xprt->xp_p1);
    661  1.21.8.2  christos 	xdrs = &(cd->xdrs);
    662  1.21.8.2  christos 
    663  1.21.8.2  christos 	if (cd->nonblock) {
    664  1.21.8.2  christos 		if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
    665  1.21.8.2  christos 			return FALSE;
    666  1.21.8.2  christos 	}
    667  1.21.8.2  christos 
    668  1.21.8.2  christos 	xdrs->x_op = XDR_DECODE;
    669  1.21.8.2  christos 	(void)xdrrec_skiprecord(xdrs);
    670  1.21.8.2  christos 
    671  1.21.8.2  christos 	if (xdr_callmsg(xdrs, msg)) {
    672  1.21.8.2  christos 		cd->x_id = msg->rm_xid;
    673  1.21.8.2  christos 		return (TRUE);
    674  1.21.8.2  christos 	}
    675  1.21.8.2  christos 	cd->strm_stat = XPRT_DIED;
    676  1.21.8.2  christos 	return (FALSE);
    677  1.21.8.2  christos }
    678  1.21.8.2  christos 
    679  1.21.8.2  christos static bool_t
    680  1.21.8.2  christos svc_vc_getargs(xprt, xdr_args, args_ptr)
    681  1.21.8.2  christos 	SVCXPRT *xprt;
    682  1.21.8.2  christos 	xdrproc_t xdr_args;
    683  1.21.8.2  christos 	caddr_t args_ptr;
    684  1.21.8.2  christos {
    685  1.21.8.2  christos 
    686  1.21.8.2  christos 	_DIAGASSERT(xprt != NULL);
    687  1.21.8.2  christos 	/* args_ptr may be NULL */
    688  1.21.8.2  christos 
    689  1.21.8.2  christos 	return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
    690  1.21.8.2  christos 	    args_ptr));
    691  1.21.8.2  christos }
    692  1.21.8.2  christos 
    693  1.21.8.2  christos static bool_t
    694  1.21.8.2  christos svc_vc_freeargs(xprt, xdr_args, args_ptr)
    695  1.21.8.2  christos 	SVCXPRT *xprt;
    696  1.21.8.2  christos 	xdrproc_t xdr_args;
    697  1.21.8.2  christos 	caddr_t args_ptr;
    698  1.21.8.2  christos {
    699  1.21.8.2  christos 	XDR *xdrs;
    700  1.21.8.2  christos 
    701  1.21.8.2  christos 	_DIAGASSERT(xprt != NULL);
    702  1.21.8.2  christos 	/* args_ptr may be NULL */
    703  1.21.8.2  christos 
    704  1.21.8.2  christos 	xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
    705  1.21.8.2  christos 
    706  1.21.8.2  christos 	xdrs->x_op = XDR_FREE;
    707  1.21.8.2  christos 	return ((*xdr_args)(xdrs, args_ptr));
    708  1.21.8.2  christos }
    709  1.21.8.2  christos 
    710  1.21.8.2  christos static bool_t
    711  1.21.8.2  christos svc_vc_reply(xprt, msg)
    712  1.21.8.2  christos 	SVCXPRT *xprt;
    713  1.21.8.2  christos 	struct rpc_msg *msg;
    714  1.21.8.2  christos {
    715  1.21.8.2  christos 	struct cf_conn *cd;
    716  1.21.8.2  christos 	XDR *xdrs;
    717  1.21.8.2  christos 	bool_t rstat;
    718  1.21.8.2  christos 
    719  1.21.8.2  christos 	_DIAGASSERT(xprt != NULL);
    720  1.21.8.2  christos 	_DIAGASSERT(msg != NULL);
    721  1.21.8.2  christos 
    722  1.21.8.2  christos 	cd = (struct cf_conn *)(xprt->xp_p1);
    723  1.21.8.2  christos 	xdrs = &(cd->xdrs);
    724  1.21.8.2  christos 
    725  1.21.8.2  christos 	xdrs->x_op = XDR_ENCODE;
    726  1.21.8.2  christos 	msg->rm_xid = cd->x_id;
    727  1.21.8.2  christos 	rstat = xdr_replymsg(xdrs, msg);
    728  1.21.8.2  christos 	(void)xdrrec_endofrecord(xdrs, TRUE);
    729  1.21.8.2  christos 	return (rstat);
    730  1.21.8.2  christos }
    731  1.21.8.2  christos 
    732  1.21.8.2  christos static void
    733  1.21.8.2  christos svc_vc_ops(xprt)
    734  1.21.8.2  christos 	SVCXPRT *xprt;
    735  1.21.8.2  christos {
    736  1.21.8.2  christos 	static struct xp_ops ops;
    737  1.21.8.2  christos 	static struct xp_ops2 ops2;
    738  1.21.8.2  christos #ifdef _REENTRANT
    739  1.21.8.2  christos 	extern mutex_t ops_lock;
    740  1.21.8.2  christos #endif
    741  1.21.8.2  christos 
    742  1.21.8.2  christos /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
    743  1.21.8.2  christos 
    744  1.21.8.2  christos 	mutex_lock(&ops_lock);
    745  1.21.8.2  christos 	if (ops.xp_recv == NULL) {
    746  1.21.8.2  christos 		ops.xp_recv = svc_vc_recv;
    747  1.21.8.2  christos 		ops.xp_stat = svc_vc_stat;
    748  1.21.8.2  christos 		ops.xp_getargs = svc_vc_getargs;
    749  1.21.8.2  christos 		ops.xp_reply = svc_vc_reply;
    750  1.21.8.2  christos 		ops.xp_freeargs = svc_vc_freeargs;
    751  1.21.8.2  christos 		ops.xp_destroy = svc_vc_destroy;
    752  1.21.8.2  christos 		ops2.xp_control = svc_vc_control;
    753  1.21.8.2  christos 	}
    754  1.21.8.2  christos 	xprt->xp_ops = &ops;
    755  1.21.8.2  christos 	xprt->xp_ops2 = &ops2;
    756  1.21.8.2  christos 	mutex_unlock(&ops_lock);
    757  1.21.8.2  christos }
    758  1.21.8.2  christos 
    759  1.21.8.2  christos static void
    760  1.21.8.2  christos svc_vc_rendezvous_ops(xprt)
    761  1.21.8.2  christos 	SVCXPRT *xprt;
    762  1.21.8.2  christos {
    763  1.21.8.2  christos 	static struct xp_ops ops;
    764  1.21.8.2  christos 	static struct xp_ops2 ops2;
    765  1.21.8.2  christos #ifdef _REENTRANT
    766  1.21.8.2  christos 	extern mutex_t ops_lock;
    767  1.21.8.2  christos #endif
    768  1.21.8.2  christos /* XXXGCC vax compiler unhappy otherwise */
    769  1.21.8.2  christos #ifdef __vax__
    770  1.21.8.2  christos extern void abort(void);
    771  1.21.8.2  christos #endif
    772  1.21.8.2  christos 
    773  1.21.8.2  christos 	mutex_lock(&ops_lock);
    774  1.21.8.2  christos 	if (ops.xp_recv == NULL) {
    775  1.21.8.2  christos 		ops.xp_recv = rendezvous_request;
    776  1.21.8.2  christos 		ops.xp_stat = rendezvous_stat;
    777  1.21.8.2  christos 		ops.xp_getargs =
    778  1.21.8.2  christos 		    (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort;
    779  1.21.8.2  christos 		ops.xp_reply =
    780  1.21.8.2  christos 		    (bool_t (*) __P((SVCXPRT *, struct rpc_msg *)))abort;
    781  1.21.8.2  christos 		ops.xp_freeargs =
    782  1.21.8.2  christos 		    (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort;
    783  1.21.8.2  christos 		ops.xp_destroy = svc_vc_destroy;
    784  1.21.8.2  christos 		ops2.xp_control = svc_vc_rendezvous_control;
    785  1.21.8.2  christos 	}
    786  1.21.8.2  christos 	xprt->xp_ops = &ops;
    787  1.21.8.2  christos 	xprt->xp_ops2 = &ops2;
    788  1.21.8.2  christos 	mutex_unlock(&ops_lock);
    789  1.21.8.2  christos }
    790  1.21.8.2  christos 
    791  1.21.8.2  christos /*
    792  1.21.8.2  christos  * Destroy xprts that have not have had any activity in 'timeout' seconds.
    793  1.21.8.2  christos  * If 'cleanblock' is true, blocking connections (the default) are also
    794  1.21.8.2  christos  * cleaned. If timeout is 0, the least active connection is picked.
    795  1.21.8.2  christos  */
    796  1.21.8.2  christos bool_t
    797  1.21.8.2  christos __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
    798  1.21.8.2  christos {
    799  1.21.8.2  christos 	int i, ncleaned;
    800  1.21.8.2  christos 	SVCXPRT *xprt, *least_active;
    801  1.21.8.2  christos 	struct timeval tv, tdiff, tmax;
    802  1.21.8.2  christos 	struct cf_conn *cd;
    803  1.21.8.2  christos 
    804  1.21.8.2  christos 	gettimeofday(&tv, NULL);
    805  1.21.8.2  christos 	tmax.tv_sec = tmax.tv_usec = 0;
    806  1.21.8.2  christos 	least_active = NULL;
    807  1.21.8.2  christos 	rwlock_wrlock(&svc_fd_lock);
    808  1.21.8.2  christos 	for (i = ncleaned = 0; i <= svc_maxfd; i++) {
    809  1.21.8.2  christos 		if (FD_ISSET(i, fds)) {
    810  1.21.8.2  christos 			xprt = __svc_xports[i];
    811  1.21.8.2  christos 			if (xprt == NULL || xprt->xp_ops == NULL ||
    812  1.21.8.2  christos 			    xprt->xp_ops->xp_recv != svc_vc_recv)
    813  1.21.8.2  christos 				continue;
    814  1.21.8.2  christos 			cd = (struct cf_conn *)xprt->xp_p1;
    815  1.21.8.2  christos 			if (!cleanblock && !cd->nonblock)
    816  1.21.8.2  christos 				continue;
    817  1.21.8.2  christos 			if (timeout == 0) {
    818  1.21.8.2  christos 				timersub(&tv, &cd->last_recv_time, &tdiff);
    819  1.21.8.2  christos 				if (timercmp(&tdiff, &tmax, >)) {
    820  1.21.8.2  christos 					tmax = tdiff;
    821  1.21.8.2  christos 					least_active = xprt;
    822  1.21.8.2  christos 				}
    823  1.21.8.2  christos 				continue;
    824  1.21.8.2  christos 			}
    825  1.21.8.2  christos 			if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
    826  1.21.8.2  christos 				__xprt_unregister_unlocked(xprt);
    827  1.21.8.2  christos 				__svc_vc_dodestroy(xprt);
    828  1.21.8.2  christos 				ncleaned++;
    829  1.21.8.2  christos 			}
    830  1.21.8.2  christos 		}
    831  1.21.8.2  christos 	}
    832  1.21.8.2  christos 	if (timeout == 0 && least_active != NULL) {
    833  1.21.8.2  christos 		__xprt_unregister_unlocked(least_active);
    834  1.21.8.2  christos 		__svc_vc_dodestroy(least_active);
    835  1.21.8.2  christos 		ncleaned++;
    836  1.21.8.2  christos 	}
    837  1.21.8.2  christos 	rwlock_unlock(&svc_fd_lock);
    838  1.21.8.2  christos 	return ncleaned > 0 ? TRUE : FALSE;
    839  1.21.8.2  christos }
    840