Home | History | Annotate | Line # | Download | only in rpc
svc_vc.c revision 1.32
      1 /*	$NetBSD: svc_vc.c,v 1.32 2015/11/07 17:34:33 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.32 2015/11/07 17:34:33 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 			fd_set *cleanfds = svc_fdset_copy(svc_fdset_get());
    332 			int rv = 0;
    333 			if (cleanfds)
    334 				rv = __svc_clean_idle(cleanfds, 0, FALSE);
    335 			free(cleanfds);
    336 			if (rv)
    337 				goto again;
    338 		}
    339 		return FALSE;
    340 	}
    341 	/*
    342 	 * make a new transporter (re-uses xprt)
    343 	 */
    344 	newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
    345 	if (newxprt == NULL)
    346 		goto out;
    347 	newxprt->xp_rtaddr.buf = mem_alloc(len);
    348 	if (newxprt->xp_rtaddr.buf == NULL)
    349 		goto out;
    350 	memcpy(newxprt->xp_rtaddr.buf, &addr, len);
    351 	newxprt->xp_rtaddr.len = len;
    352 #ifdef PORTMAP
    353 	if (addr.ss_family == AF_INET) {
    354 		newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
    355 		newxprt->xp_addrlen = sizeof (struct sockaddr_in);
    356 	}
    357 #endif
    358 	if (__rpc_fd2sockinfo(sock, &si))
    359 		__rpc_setnodelay(sock, &si);
    360 
    361 	cd = (struct cf_conn *)newxprt->xp_p1;
    362 
    363 	cd->recvsize = r->recvsize;
    364 	cd->sendsize = r->sendsize;
    365 	cd->maxrec = r->maxrec;
    366 
    367 	if (cd->maxrec != 0) {
    368 		flags = fcntl(sock, F_GETFL, 0);
    369 		if (flags  == -1)
    370 			goto out;
    371 		if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
    372 			goto out;
    373 		if (cd->recvsize > (u_int)cd->maxrec)
    374 			cd->recvsize = cd->maxrec;
    375 		cd->nonblock = TRUE;
    376 		__xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
    377 	} else
    378 		cd->nonblock = FALSE;
    379 
    380 	(void)gettimeofday(&cd->last_recv_time, NULL);
    381 
    382 	return FALSE; /* there is never an rpc msg to be processed */
    383 out:
    384 	(void)close(sock);
    385 	return FALSE; /* there was an error */
    386 }
    387 
    388 /*ARGSUSED*/
    389 static enum xprt_stat
    390 rendezvous_stat(SVCXPRT *xprt)
    391 {
    392 
    393 	return XPRT_IDLE;
    394 }
    395 
    396 static void
    397 svc_vc_destroy(SVCXPRT *xprt)
    398 {
    399 	_DIAGASSERT(xprt != NULL);
    400 
    401 	xprt_unregister(xprt);
    402 	__svc_vc_dodestroy(xprt);
    403 }
    404 
    405 static void
    406 __svc_vc_dodestroy(SVCXPRT *xprt)
    407 {
    408 	struct cf_conn *cd;
    409 	struct cf_rendezvous *r;
    410 
    411 	cd = (struct cf_conn *)xprt->xp_p1;
    412 
    413 	if (xprt->xp_fd != RPC_ANYFD)
    414 		(void)close(xprt->xp_fd);
    415 	if (xprt->xp_port != 0) {
    416 		/* a rendezvouser socket */
    417 		r = (struct cf_rendezvous *)xprt->xp_p1;
    418 		mem_free(r, sizeof (struct cf_rendezvous));
    419 		xprt->xp_port = 0;
    420 	} else {
    421 		/* an actual connection socket */
    422 		XDR_DESTROY(&(cd->xdrs));
    423 		mem_free(cd, sizeof(struct cf_conn));
    424 	}
    425 	if (xprt->xp_rtaddr.buf)
    426 		mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
    427 	if (xprt->xp_ltaddr.buf)
    428 		mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
    429 	if (xprt->xp_tp)
    430 		free(xprt->xp_tp);
    431 	if (xprt->xp_netid)
    432 		free(xprt->xp_netid);
    433 	mem_free(xprt, sizeof(SVCXPRT));
    434 }
    435 
    436 /*ARGSUSED*/
    437 static bool_t
    438 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in)
    439 {
    440 	return FALSE;
    441 }
    442 
    443 /*ARGSUSED*/
    444 static bool_t
    445 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
    446 {
    447 	struct cf_rendezvous *cfp;
    448 
    449 	cfp = (struct cf_rendezvous *)xprt->xp_p1;
    450 	if (cfp == NULL)
    451 		return FALSE;
    452 	switch (rq) {
    453 		case SVCGET_CONNMAXREC:
    454 			*(int *)in = cfp->maxrec;
    455 			break;
    456 		case SVCSET_CONNMAXREC:
    457 			cfp->maxrec = *(int *)in;
    458 			break;
    459 		default:
    460 			return FALSE;
    461 	}
    462 	return TRUE;
    463 }
    464 
    465 /*
    466  * reads data from the tcp connection.
    467  * any error is fatal and the connection is closed.
    468  * (And a read of zero bytes is a half closed stream => error.)
    469  * All read operations timeout after 35 seconds.  A timeout is
    470  * fatal for the connection.
    471  */
    472 static int
    473 read_vc(caddr_t xprtp, caddr_t buf, int len)
    474 {
    475 	SVCXPRT *xprt;
    476 	int sock;
    477 	struct pollfd pollfd;
    478 	struct sockaddr *sa;
    479 	struct msghdr msg;
    480 	struct cmsghdr *cmp;
    481 	void *crmsg = NULL;
    482 	struct sockcred *sc;
    483 	socklen_t crmsgsize;
    484 	struct cf_conn *cfp;
    485 	static const struct timespec ts = { 35, 0 };
    486 
    487 	xprt = (SVCXPRT *)(void *)xprtp;
    488 	_DIAGASSERT(xprt != NULL);
    489 
    490 	sock = xprt->xp_fd;
    491 
    492 	sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
    493 	if (sa->sa_family == AF_LOCAL && xprt->xp_p2 == NULL) {
    494 		memset(&msg, 0, sizeof msg);
    495 		crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
    496 		crmsg = malloc(crmsgsize);
    497 		if (crmsg == NULL)
    498 			goto fatal_err;
    499 		memset(crmsg, 0, crmsgsize);
    500 
    501 		msg.msg_control = crmsg;
    502 		msg.msg_controllen = crmsgsize;
    503 
    504 		if (recvmsg(sock, &msg, 0) < 0)
    505 			goto fatal_err;
    506 
    507 		if (msg.msg_controllen == 0 ||
    508 		    (msg.msg_flags & MSG_CTRUNC) != 0)
    509 			goto fatal_err;
    510 
    511 		cmp = CMSG_FIRSTHDR(&msg);
    512 		if (cmp->cmsg_level != SOL_SOCKET ||
    513 		    cmp->cmsg_type != SCM_CREDS)
    514 			goto fatal_err;
    515 
    516 		sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
    517 
    518 		xprt->xp_p2 = mem_alloc(SOCKCREDSIZE(sc->sc_ngroups));
    519 		if (xprt->xp_p2 == NULL)
    520 			goto fatal_err;
    521 
    522 		memcpy(xprt->xp_p2, sc, SOCKCREDSIZE(sc->sc_ngroups));
    523 		free(crmsg);
    524 		crmsg = NULL;
    525 	}
    526 
    527 	cfp = (struct cf_conn *)xprt->xp_p1;
    528 
    529 	if (cfp->nonblock) {
    530 		len = (int)read(sock, buf, (size_t)len);
    531 		if (len < 0) {
    532 			if (errno == EAGAIN)
    533 				len = 0;
    534 			else
    535 				goto fatal_err;
    536 		}
    537 		if (len != 0)
    538 			gettimeofday(&cfp->last_recv_time, NULL);
    539 		return len;
    540 	}
    541 
    542 	do {
    543 		pollfd.fd = sock;
    544 		pollfd.events = POLLIN;
    545 		switch (pollts(&pollfd, 1, &ts, NULL)) {
    546 		case -1:
    547 			if (errno == EINTR) {
    548 				continue;
    549 			}
    550 			/*FALLTHROUGH*/
    551 		case 0:
    552 			goto fatal_err;
    553 
    554 		default:
    555 			break;
    556 		}
    557 	} while ((pollfd.revents & POLLIN) == 0);
    558 
    559 	if ((len = (int)read(sock, buf, (size_t)len)) > 0) {
    560 		gettimeofday(&cfp->last_recv_time, NULL);
    561 		return len;
    562 	}
    563 
    564 fatal_err:
    565 	if (crmsg != NULL)
    566 		free(crmsg);
    567 	((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
    568 	return -1;
    569 }
    570 
    571 /*
    572  * writes data to the tcp connection.
    573  * Any error is fatal and the connection is closed.
    574  */
    575 static int
    576 write_vc(caddr_t xprtp, caddr_t buf, int len)
    577 {
    578 	SVCXPRT *xprt;
    579 	int i, cnt;
    580 	struct cf_conn *cd;
    581 	struct timeval tv0, tv1;
    582 
    583 	xprt = (SVCXPRT *)(void *)xprtp;
    584 	_DIAGASSERT(xprt != NULL);
    585 
    586 	cd = (struct cf_conn *)xprt->xp_p1;
    587 
    588 	if (cd->nonblock)
    589 		gettimeofday(&tv0, NULL);
    590 
    591 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
    592 		if ((i = (int)write(xprt->xp_fd, buf, (size_t)cnt)) < 0) {
    593 			if (errno != EAGAIN || !cd->nonblock) {
    594 				cd->strm_stat = XPRT_DIED;
    595 				return -1;
    596 			}
    597 			if (cd->nonblock) {
    598 				/*
    599 				 * For non-blocking connections, do not
    600 				 * take more than 2 seconds writing the
    601 				 * data out.
    602 				 *
    603 				 * XXX 2 is an arbitrary amount.
    604 				 */
    605 				gettimeofday(&tv1, NULL);
    606 				if (tv1.tv_sec - tv0.tv_sec >= 2) {
    607 					cd->strm_stat = XPRT_DIED;
    608 					return -1;
    609 				}
    610 			}
    611 			i = 0;
    612 		}
    613 	}
    614 	return len;
    615 }
    616 
    617 static enum xprt_stat
    618 svc_vc_stat(SVCXPRT *xprt)
    619 {
    620 	struct cf_conn *cd;
    621 
    622 	_DIAGASSERT(xprt != NULL);
    623 
    624 	cd = (struct cf_conn *)(xprt->xp_p1);
    625 
    626 	if (cd->strm_stat == XPRT_DIED)
    627 		return XPRT_DIED;
    628 	if (! xdrrec_eof(&(cd->xdrs)))
    629 		return XPRT_MOREREQS;
    630 	return XPRT_IDLE;
    631 }
    632 
    633 static bool_t
    634 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg)
    635 {
    636 	struct cf_conn *cd;
    637 	XDR *xdrs;
    638 
    639 	_DIAGASSERT(xprt != NULL);
    640 	_DIAGASSERT(msg != NULL);
    641 
    642 	cd = (struct cf_conn *)(xprt->xp_p1);
    643 	xdrs = &(cd->xdrs);
    644 
    645 	if (cd->nonblock) {
    646 		if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
    647 			return FALSE;
    648 	}
    649 
    650 	xdrs->x_op = XDR_DECODE;
    651 	(void)xdrrec_skiprecord(xdrs);
    652 
    653 	if (xdr_callmsg(xdrs, msg)) {
    654 		cd->x_id = msg->rm_xid;
    655 		return TRUE;
    656 	}
    657 	cd->strm_stat = XPRT_DIED;
    658 	return FALSE;
    659 }
    660 
    661 static bool_t
    662 svc_vc_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
    663 {
    664 
    665 	_DIAGASSERT(xprt != NULL);
    666 	/* args_ptr may be NULL */
    667 
    668 	return (*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
    669 	    args_ptr);
    670 }
    671 
    672 static bool_t
    673 svc_vc_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
    674 {
    675 	XDR *xdrs;
    676 
    677 	_DIAGASSERT(xprt != NULL);
    678 	/* args_ptr may be NULL */
    679 
    680 	xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
    681 
    682 	xdrs->x_op = XDR_FREE;
    683 	return (*xdr_args)(xdrs, args_ptr);
    684 }
    685 
    686 static bool_t
    687 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg)
    688 {
    689 	struct cf_conn *cd;
    690 	XDR *xdrs;
    691 	bool_t rstat;
    692 
    693 	_DIAGASSERT(xprt != NULL);
    694 	_DIAGASSERT(msg != NULL);
    695 
    696 	cd = (struct cf_conn *)(xprt->xp_p1);
    697 	xdrs = &(cd->xdrs);
    698 
    699 	xdrs->x_op = XDR_ENCODE;
    700 	msg->rm_xid = cd->x_id;
    701 	rstat = xdr_replymsg(xdrs, msg);
    702 	(void)xdrrec_endofrecord(xdrs, TRUE);
    703 	return rstat;
    704 }
    705 
    706 static void
    707 svc_vc_ops(SVCXPRT *xprt)
    708 {
    709 	static struct xp_ops ops;
    710 	static struct xp_ops2 ops2;
    711 #ifdef _REENTRANT
    712 	extern mutex_t ops_lock;
    713 #endif
    714 
    715 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
    716 
    717 	mutex_lock(&ops_lock);
    718 	if (ops.xp_recv == NULL) {
    719 		ops.xp_recv = svc_vc_recv;
    720 		ops.xp_stat = svc_vc_stat;
    721 		ops.xp_getargs = svc_vc_getargs;
    722 		ops.xp_reply = svc_vc_reply;
    723 		ops.xp_freeargs = svc_vc_freeargs;
    724 		ops.xp_destroy = svc_vc_destroy;
    725 		ops2.xp_control = svc_vc_control;
    726 	}
    727 	xprt->xp_ops = &ops;
    728 	xprt->xp_ops2 = &ops2;
    729 	mutex_unlock(&ops_lock);
    730 }
    731 
    732 static void
    733 svc_vc_rendezvous_ops(SVCXPRT *xprt)
    734 {
    735 	static struct xp_ops ops;
    736 	static struct xp_ops2 ops2;
    737 #ifdef _REENTRANT
    738 	extern mutex_t ops_lock;
    739 #endif
    740 	mutex_lock(&ops_lock);
    741 	if (ops.xp_recv == NULL) {
    742 		ops.xp_recv = rendezvous_request;
    743 		ops.xp_stat = rendezvous_stat;
    744 		ops.xp_getargs =
    745 		    (bool_t (*)(SVCXPRT *, xdrproc_t, caddr_t))abort;
    746 		ops.xp_reply =
    747 		    (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
    748 		ops.xp_freeargs =
    749 		    (bool_t (*)(SVCXPRT *, xdrproc_t, caddr_t))abort;
    750 		ops.xp_destroy = svc_vc_destroy;
    751 		ops2.xp_control = svc_vc_rendezvous_control;
    752 	}
    753 	xprt->xp_ops = &ops;
    754 	xprt->xp_ops2 = &ops2;
    755 	mutex_unlock(&ops_lock);
    756 }
    757 
    758 /*
    759  * Destroy xprts that have not have had any activity in 'timeout' seconds.
    760  * If 'cleanblock' is true, blocking connections (the default) are also
    761  * cleaned. If timeout is 0, the least active connection is picked.
    762  */
    763 bool_t
    764 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
    765 {
    766 	int i, ncleaned, *fdmax;
    767 	SVCXPRT *xprt, *least_active;
    768 	struct timeval tv, tdiff, tmax;
    769 	struct cf_conn *cd;
    770 
    771 	gettimeofday(&tv, NULL);
    772 	tmax.tv_sec = tmax.tv_usec = 0;
    773 	least_active = NULL;
    774 	rwlock_wrlock(&svc_fd_lock);
    775 	fdmax = svc_fdset_getmax();
    776 	if (fdmax == NULL)
    777 		return FALSE;
    778 	for (i = ncleaned = 0; i <= *fdmax; i++) {
    779 		switch (svc_fdset_isset(i)) {
    780 		case 0:
    781 		case -1:
    782 			continue;
    783 		default:
    784 			break;
    785 		}
    786 
    787 		xprt = __svc_xports[i];
    788 		if (xprt == NULL || xprt->xp_ops == NULL ||
    789 		    xprt->xp_ops->xp_recv != svc_vc_recv)
    790 			continue;
    791 
    792 		cd = (struct cf_conn *)xprt->xp_p1;
    793 		if (!cleanblock && !cd->nonblock)
    794 			continue;
    795 
    796 		if (timeout == 0) {
    797 			timersub(&tv, &cd->last_recv_time, &tdiff);
    798 			if (timercmp(&tdiff, &tmax, >)) {
    799 				tmax = tdiff;
    800 				least_active = xprt;
    801 			}
    802 			continue;
    803 		}
    804 
    805 		if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
    806 			__xprt_unregister_unlocked(xprt);
    807 			__svc_vc_dodestroy(xprt);
    808 			ncleaned++;
    809 		}
    810 	}
    811 	if (timeout == 0 && least_active != NULL) {
    812 		__xprt_unregister_unlocked(least_active);
    813 		__svc_vc_dodestroy(least_active);
    814 		ncleaned++;
    815 	}
    816 	rwlock_unlock(&svc_fd_lock);
    817 	return ncleaned > 0 ? TRUE : FALSE;
    818 }
    819