Home | History | Annotate | Line # | Download | only in rpc
svc_vc.c revision 1.33
      1 /*	$NetBSD: svc_vc.c,v 1.33 2015/11/07 23:09:20 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010, Oracle America, Inc.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are
      8  * met:
      9  *
     10  *     * Redistributions of source code must retain the above copyright
     11  *       notice, this list of conditions and the following disclaimer.
     12  *     * Redistributions in binary form must reproduce the above
     13  *       copyright notice, this list of conditions and the following
     14  *       disclaimer in the documentation and/or other materials
     15  *       provided with the distribution.
     16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  *       contributors may be used to endorse or promote products derived
     18  *       from this software without specific prior written permission.
     19  *
     20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #if defined(LIBC_SCCS) && !defined(lint)
     36 #if 0
     37 static char *sccsid = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
     38 static char *sccsid = "@(#)svc_tcp.c	2.2 88/08/01 4.0 RPCSRC";
     39 #else
     40 __RCSID("$NetBSD: svc_vc.c,v 1.33 2015/11/07 23:09:20 christos Exp $");
     41 #endif
     42 #endif
     43 
     44 /*
     45  * svc_vc.c, Server side for Connection Oriented based RPC.
     46  *
     47  * Actually implements two flavors of transporter -
     48  * a tcp rendezvouser (a listner and connection establisher)
     49  * and a record/tcp stream.
     50  */
     51 
     52 #include "namespace.h"
     53 #include "reentrant.h"
     54 #include <sys/types.h>
     55 #include <sys/param.h>
     56 #include <sys/poll.h>
     57 #include <sys/socket.h>
     58 #include <sys/un.h>
     59 #include <sys/time.h>
     60 #include <netinet/in.h>
     61 
     62 #include <assert.h>
     63 #include <err.h>
     64 #include <errno.h>
     65 #include <fcntl.h>
     66 #include <stdio.h>
     67 #include <stdlib.h>
     68 #include <string.h>
     69 #include <unistd.h>
     70 
     71 #include <rpc/rpc.h>
     72 
     73 #include "svc_fdset.h"
     74 #include "rpc_internal.h"
     75 
     76 #ifdef __weak_alias
     77 __weak_alias(svc_fd_create,_svc_fd_create)
     78 __weak_alias(svc_vc_create,_svc_vc_create)
     79 #endif
     80 
     81 #ifdef _REENTRANT
     82 extern rwlock_t svc_fd_lock;
     83 #endif
     84 
     85 static SVCXPRT *makefd_xprt(int, u_int, u_int);
     86 static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
     87 static enum xprt_stat rendezvous_stat(SVCXPRT *);
     88 static void svc_vc_destroy(SVCXPRT *);
     89 static void __svc_vc_dodestroy(SVCXPRT *);
     90 static int read_vc(caddr_t, caddr_t, int);
     91 static int write_vc(caddr_t, caddr_t, int);
     92 static enum xprt_stat svc_vc_stat(SVCXPRT *);
     93 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *);
     94 static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, caddr_t);
     95 static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, caddr_t);
     96 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *);
     97 static void svc_vc_rendezvous_ops(SVCXPRT *);
     98 static void svc_vc_ops(SVCXPRT *);
     99 static bool_t svc_vc_control(SVCXPRT *, const u_int, void *);
    100 static bool_t svc_vc_rendezvous_control(SVCXPRT *, const u_int, void *);
    101 
    102 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
    103 	u_int sendsize;
    104 	u_int recvsize;
    105 	int maxrec;
    106 };
    107 
    108 struct cf_conn {  /* kept in xprt->xp_p1 for actual connection */
    109 	enum xprt_stat strm_stat;
    110 	u_int32_t x_id;
    111 	XDR xdrs;
    112 	char verf_body[MAX_AUTH_BYTES];
    113 	u_int sendsize;
    114 	u_int recvsize;
    115 	int maxrec;
    116 	bool_t nonblock;
    117 	struct timeval last_recv_time;
    118 };
    119 
    120 /*
    121  * Usage:
    122  *	xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
    123  *
    124  * Creates, registers, and returns a (rpc) tcp based transporter.
    125  * Once *xprt is initialized, it is registered as a transporter
    126  * see (svc.h, xprt_register).  This routine returns
    127  * a NULL if a problem occurred.
    128  *
    129  * The filedescriptor passed in is expected to refer to a bound, but
    130  * not yet connected socket.
    131  *
    132  * Since streams do buffered io similar to stdio, the caller can specify
    133  * how big the send and receive buffers are via the second and third parms;
    134  * 0 => use the system default.
    135  */
    136 SVCXPRT *
    137 svc_vc_create(int fd, u_int sendsize, u_int recvsize)
    138 {
    139 	SVCXPRT *xprt;
    140 	struct cf_rendezvous *r = NULL;
    141 	struct __rpc_sockinfo si;
    142 	struct sockaddr_storage sslocal;
    143 	socklen_t slen;
    144 	int one = 1;
    145 
    146 	if (!__rpc_fd2sockinfo(fd, &si))
    147 		return NULL;
    148 
    149 	r = mem_alloc(sizeof(*r));
    150 	if (r == NULL) {
    151 		warn("%s: out of memory", __func__);
    152 		return NULL;
    153 	}
    154 	r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
    155 	r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
    156 	r->maxrec = __svc_maxrec;
    157 	xprt = mem_alloc(sizeof(SVCXPRT));
    158 	if (xprt == NULL) {
    159 		warn("%s: out of memory", __func__);
    160 		goto cleanup_svc_vc_create;
    161 	}
    162 	xprt->xp_tp = NULL;
    163 	xprt->xp_p1 = (caddr_t)(void *)r;
    164 	xprt->xp_p2 = NULL;
    165 	xprt->xp_p3 = NULL;
    166 	xprt->xp_verf = _null_auth;
    167 	svc_vc_rendezvous_ops(xprt);
    168 	xprt->xp_port = (u_short)-1;	/* It is the rendezvouser */
    169 	xprt->xp_fd = fd;
    170 
    171 	slen = sizeof (struct sockaddr_storage);
    172 	if (getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
    173 		warn("%s: could not retrieve local addr", __func__);
    174 		goto cleanup_svc_vc_create;
    175 	}
    176 
    177 	/*
    178 	 * We want to be able to check credentials on local sockets.
    179 	 */
    180 	if (sslocal.ss_family == AF_LOCAL)
    181 		if (setsockopt(fd, 0, LOCAL_CREDS, &one, (socklen_t)sizeof one)
    182 		    == -1)
    183 			goto cleanup_svc_vc_create;
    184 
    185 	xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
    186 	xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
    187 	if (xprt->xp_ltaddr.buf == NULL) {
    188 		warn("%s: out of memory", __func__);
    189 		goto cleanup_svc_vc_create;
    190 	}
    191 	memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
    192 
    193 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
    194 	if (!xprt_register(xprt))
    195 		goto cleanup_svc_vc_create;
    196 	return xprt;
    197 cleanup_svc_vc_create:
    198 	if (xprt)
    199 		mem_free(xprt, sizeof(*xprt));
    200 	if (r != NULL)
    201 		mem_free(r, sizeof(*r));
    202 	return NULL;
    203 }
    204 
    205 /*
    206  * Like svtcp_create(), except the routine takes any *open* UNIX file
    207  * descriptor as its first input.
    208  */
    209 SVCXPRT *
    210 svc_fd_create(int fd, u_int sendsize, u_int recvsize)
    211 {
    212 	struct sockaddr_storage ss;
    213 	socklen_t slen;
    214 	SVCXPRT *ret;
    215 
    216 	_DIAGASSERT(fd != -1);
    217 
    218 	ret = makefd_xprt(fd, sendsize, recvsize);
    219 	if (ret == NULL)
    220 		return NULL;
    221 
    222 	slen = sizeof (struct sockaddr_storage);
    223 	if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    224 		warn("%s: could not retrieve local addr", __func__);
    225 		goto freedata;
    226 	}
    227 	ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
    228 	ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
    229 	if (ret->xp_ltaddr.buf == NULL) {
    230 		warn("%s: out of memory", __func__);
    231 		goto freedata;
    232 	}
    233 	memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
    234 
    235 	slen = sizeof (struct sockaddr_storage);
    236 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    237 		warn("%s: could not retrieve remote addr", __func__);
    238 		goto freedata;
    239 	}
    240 	ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
    241 	ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
    242 	if (ret->xp_rtaddr.buf == NULL) {
    243 		warn("%s: out of memory", __func__);
    244 		goto freedata;
    245 	}
    246 	memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
    247 #ifdef PORTMAP
    248 	if (ss.ss_family == AF_INET) {
    249 		ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
    250 		ret->xp_addrlen = sizeof (struct sockaddr_in);
    251 	}
    252 #endif
    253 
    254 	return ret;
    255 
    256 freedata:
    257 	if (ret->xp_ltaddr.buf != NULL)
    258 		mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
    259 
    260 	return NULL;
    261 }
    262 
    263 static SVCXPRT *
    264 makefd_xprt(int fd, u_int sendsize, u_int recvsize)
    265 {
    266 	SVCXPRT *xprt;
    267 	struct cf_conn *cd;
    268 	const char *netid;
    269 	struct __rpc_sockinfo si;
    270 
    271 	_DIAGASSERT(fd != -1);
    272 
    273 	xprt = mem_alloc(sizeof(SVCXPRT));
    274 	if (xprt == NULL)
    275 		goto outofmem;
    276 	memset(xprt, 0, sizeof *xprt);
    277 	cd = mem_alloc(sizeof(struct cf_conn));
    278 	if (cd == NULL)
    279 		goto outofmem;
    280 	cd->strm_stat = XPRT_IDLE;
    281 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
    282 	    (caddr_t)(void *)xprt, read_vc, write_vc);
    283 	xprt->xp_p1 = (caddr_t)(void *)cd;
    284 	xprt->xp_verf.oa_base = cd->verf_body;
    285 	svc_vc_ops(xprt);  /* truely deals with calls */
    286 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
    287 	xprt->xp_fd = fd;
    288 	if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
    289 		if ((xprt->xp_netid = strdup(netid)) == NULL)
    290 			goto outofmem;
    291 
    292 	if (!xprt_register(xprt))
    293 		goto out;
    294 	return xprt;
    295 
    296 outofmem:
    297 	warn("svc_tcp: makefd_xprt");
    298 out:
    299 	if (xprt)
    300 		mem_free(xprt, sizeof(SVCXPRT));
    301 	return NULL;
    302 }
    303 
    304 /*ARGSUSED*/
    305 static bool_t
    306 rendezvous_request(SVCXPRT *xprt, struct rpc_msg *msg)
    307 {
    308 	int sock, flags;
    309 	struct cf_rendezvous *r;
    310 	struct cf_conn *cd;
    311 	struct sockaddr_storage addr;
    312 	socklen_t len;
    313 	struct __rpc_sockinfo si;
    314 	SVCXPRT *newxprt;
    315 
    316 	_DIAGASSERT(xprt != NULL);
    317 	_DIAGASSERT(msg != NULL);
    318 
    319 	r = (struct cf_rendezvous *)xprt->xp_p1;
    320 again:
    321 	len = sizeof addr;
    322 	if ((sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
    323 	    &len)) < 0) {
    324 		if (errno == EINTR)
    325 			goto again;
    326 		/*
    327 		 * Clean out the most idle file descriptor when we're
    328 		 * running out.
    329 		 */
    330 		if (errno == EMFILE || errno == ENFILE) {
    331 			if (__svc_clean_idle(NULL, 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 __unused, int timeout, bool_t cleanblock)
    760 {
    761 	int i, ncleaned, *fdmax;
    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 	fdmax = svc_fdset_getmax();
    771 	if (fdmax == NULL)
    772 		return FALSE;
    773 	for (i = ncleaned = 0; i <= *fdmax; i++) {
    774 		switch (svc_fdset_isset(i)) {
    775 		case 0:
    776 		case -1:
    777 			continue;
    778 		default:
    779 			break;
    780 		}
    781 
    782 		xprt = __svc_xports[i];
    783 		if (xprt == NULL || xprt->xp_ops == NULL ||
    784 		    xprt->xp_ops->xp_recv != svc_vc_recv)
    785 			continue;
    786 
    787 		cd = (struct cf_conn *)xprt->xp_p1;
    788 		if (!cleanblock && !cd->nonblock)
    789 			continue;
    790 
    791 		if (timeout == 0) {
    792 			timersub(&tv, &cd->last_recv_time, &tdiff);
    793 			if (timercmp(&tdiff, &tmax, >)) {
    794 				tmax = tdiff;
    795 				least_active = xprt;
    796 			}
    797 			continue;
    798 		}
    799 
    800 		if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
    801 			__xprt_unregister_unlocked(xprt);
    802 			__svc_vc_dodestroy(xprt);
    803 			ncleaned++;
    804 		}
    805 	}
    806 	if (timeout == 0 && least_active != NULL) {
    807 		__xprt_unregister_unlocked(least_active);
    808 		__svc_vc_dodestroy(least_active);
    809 		ncleaned++;
    810 	}
    811 	rwlock_unlock(&svc_fd_lock);
    812 	return ncleaned > 0 ? TRUE : FALSE;
    813 }
    814