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