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