xref: /dragonfly/lib/libc/rpc/svc_vc.c (revision d4ef6694)
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * @(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro
29  * @(#)svc_tcp.c	2.2 88/08/01 4.0 RPCSRC
30  * $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $
31  * $FreeBSD: src/lib/libc/rpc/svc_vc.c,v 1.27 2008/03/30 09:36:17 dfr Exp $
32  */
33 
34 /*
35  * svc_vc.c, Server side for Connection Oriented based RPC.
36  *
37  * Actually implements two flavors of transporter -
38  * a tcp rendezvouser (a listner and connection establisher)
39  * and a record/tcp stream.
40  */
41 
42 #include "namespace.h"
43 #include "reentrant.h"
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/poll.h>
47 #include <sys/socket.h>
48 #include <sys/un.h>
49 #include <sys/time.h>
50 #include <sys/uio.h>
51 #include <netinet/in.h>
52 #include <netinet/tcp.h>
53 
54 #include <assert.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 
63 #include <rpc/rpc.h>
64 
65 #include "rpc_com.h"
66 #include "mt_misc.h"
67 #include "un-namespace.h"
68 
69 static SVCXPRT		*makefd_xprt(int, u_int, u_int);
70 static bool_t		rendezvous_request(SVCXPRT *, struct rpc_msg *);
71 static enum xprt_stat	rendezvous_stat(SVCXPRT *);
72 static void		svc_vc_destroy(SVCXPRT *);
73 static void		__svc_vc_dodestroy (SVCXPRT *);
74 static int		read_vc(void *, void *, int);
75 static int		write_vc(void *, void *, int);
76 static enum xprt_stat	svc_vc_stat(SVCXPRT *);
77 static bool_t		svc_vc_recv(SVCXPRT *, struct rpc_msg *);
78 static bool_t		svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
79 static bool_t		svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
80 static bool_t		svc_vc_reply(SVCXPRT *, struct rpc_msg *);
81 static void		svc_vc_rendezvous_ops(SVCXPRT *);
82 static void		svc_vc_ops(SVCXPRT *);
83 static bool_t		svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
84 static bool_t		svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq,
85 						  void *in);
86 
87 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
88 	u_int sendsize;
89 	u_int recvsize;
90 	int maxrec;
91 };
92 
93 struct cf_conn {  /* kept in xprt->xp_p1 for actual connection */
94 	enum xprt_stat strm_stat;
95 	u_int32_t x_id;
96 	XDR xdrs;
97 	char verf_body[MAX_AUTH_BYTES];
98 	u_int sendsize;
99 	u_int recvsize;
100 	int maxrec;
101 	bool_t nonblock;
102 	struct timeval last_recv_time;
103 };
104 
105 /*
106  * Usage:
107  *	xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
108  *
109  * Creates, registers, and returns a (rpc) tcp based transporter.
110  * Once *xprt is initialized, it is registered as a transporter
111  * see (svc.h, xprt_register).  This routine returns
112  * a NULL if a problem occurred.
113  *
114  * The filedescriptor passed in is expected to refer to a bound, but
115  * not yet connected socket.
116  *
117  * Since streams do buffered io similar to stdio, the caller can specify
118  * how big the send and receive buffers are via the second and third parms;
119  * 0 => use the system default.
120  */
121 SVCXPRT *
122 svc_vc_create(int fd, u_int sendsize, u_int recvsize)
123 {
124 	SVCXPRT *xprt = NULL;
125 	struct cf_rendezvous *r = NULL;
126 	struct __rpc_sockinfo si;
127 	struct sockaddr_storage sslocal;
128 	socklen_t slen;
129 
130 	if (!__rpc_fd2sockinfo(fd, &si))
131 		return NULL;
132 
133 	r = mem_alloc(sizeof(*r));
134 	if (r == NULL) {
135 		warnx("svc_vc_create: out of memory");
136 		goto cleanup_svc_vc_create;
137 	}
138 	r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
139 	r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
140 	r->maxrec = __svc_maxrec;
141 	xprt = mem_alloc(sizeof(SVCXPRT));
142 	if (xprt == NULL) {
143 		warnx("svc_vc_create: out of memory");
144 		goto cleanup_svc_vc_create;
145 	}
146 	xprt->xp_tp = NULL;
147 	xprt->xp_p1 = r;
148 	xprt->xp_p2 = NULL;
149 	xprt->xp_p3 = NULL;
150 	xprt->xp_verf = _null_auth;
151 	svc_vc_rendezvous_ops(xprt);
152 	xprt->xp_port = (u_short)-1;	/* It is the rendezvouser */
153 	xprt->xp_fd = fd;
154 
155 	slen = sizeof (struct sockaddr_storage);
156 	if (_getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
157 		warnx("svc_vc_create: could not retrieve local addr");
158 		goto cleanup_svc_vc_create;
159 	}
160 
161 	xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
162 	xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
163 	if (xprt->xp_ltaddr.buf == NULL) {
164 		warnx("svc_vc_create: no mem for local addr");
165 		goto cleanup_svc_vc_create;
166 	}
167 	memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
168 
169 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
170 	xprt_register(xprt);
171 	return (xprt);
172 cleanup_svc_vc_create:
173 	if (xprt)
174 		mem_free(xprt, sizeof(*xprt));
175 	if (r != NULL)
176 		mem_free(r, sizeof(*r));
177 	return (NULL);
178 }
179 
180 /*
181  * Like svtcp_create(), except the routine takes any *open* UNIX file
182  * descriptor as its first input.
183  */
184 SVCXPRT *
185 svc_fd_create(int fd, u_int sendsize, u_int recvsize)
186 {
187 	struct sockaddr_storage ss;
188 	socklen_t slen;
189 	SVCXPRT *ret;
190 
191 	assert(fd != -1);
192 
193 	ret = makefd_xprt(fd, sendsize, recvsize);
194 	if (ret == NULL)
195 		return NULL;
196 
197 	slen = sizeof (struct sockaddr_storage);
198 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
199 		warnx("svc_fd_create: could not retrieve local addr");
200 		goto freedata;
201 	}
202 	ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
203 	ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
204 	if (ret->xp_ltaddr.buf == NULL) {
205 		warnx("svc_fd_create: no mem for local addr");
206 		goto freedata;
207 	}
208 	memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
209 
210 	slen = sizeof (struct sockaddr_storage);
211 	if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
212 		warnx("svc_fd_create: could not retrieve remote addr");
213 		goto freedata;
214 	}
215 	ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
216 	ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
217 	if (ret->xp_rtaddr.buf == NULL) {
218 		warnx("svc_fd_create: no mem for local addr");
219 		goto freedata;
220 	}
221 	memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
222 #ifdef PORTMAP
223 	if (ss.ss_family == AF_INET || ss.ss_family == AF_LOCAL) {
224 		ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
225 		ret->xp_addrlen = sizeof (struct sockaddr_in);
226 	}
227 #endif				/* PORTMAP */
228 
229 	return ret;
230 
231 freedata:
232 	if (ret->xp_ltaddr.buf != NULL)
233 		mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
234 
235 	return NULL;
236 }
237 
238 static SVCXPRT *
239 makefd_xprt(int fd, u_int sendsize, u_int recvsize)
240 {
241 	SVCXPRT *xprt;
242 	struct cf_conn *cd;
243 	const char *netid;
244 	struct __rpc_sockinfo si;
245 
246 	assert(fd != -1);
247 
248 	xprt = mem_alloc(sizeof(SVCXPRT));
249 	if (xprt == NULL) {
250 		warnx("svc_vc: makefd_xprt: out of memory");
251 		goto done;
252 	}
253 	memset(xprt, 0, sizeof *xprt);
254 	cd = mem_alloc(sizeof(struct cf_conn));
255 	if (cd == NULL) {
256 		warnx("svc_tcp: makefd_xprt: out of memory");
257 		mem_free(xprt, sizeof(SVCXPRT));
258 		xprt = NULL;
259 		goto done;
260 	}
261 	cd->strm_stat = XPRT_IDLE;
262 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
263 	    xprt, read_vc, write_vc);
264 	xprt->xp_p1 = cd;
265 	xprt->xp_verf.oa_base = cd->verf_body;
266 	svc_vc_ops(xprt);  /* truely deals with calls */
267 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
268 	xprt->xp_fd = fd;
269         if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
270 		xprt->xp_netid = strdup(netid);
271 
272 	xprt_register(xprt);
273 done:
274 	return (xprt);
275 }
276 
277 /*ARGSUSED*/
278 static bool_t
279 rendezvous_request(SVCXPRT *xprt, struct rpc_msg *msg)
280 {
281 	int sock, flags;
282 	struct cf_rendezvous *r;
283 	struct cf_conn *cd;
284 	struct sockaddr_storage addr;
285 	socklen_t len;
286 	struct __rpc_sockinfo si;
287 	SVCXPRT *newxprt;
288 	fd_set cleanfds;
289 
290 	assert(xprt != NULL);
291 	assert(msg != NULL);
292 
293 	r = (struct cf_rendezvous *)xprt->xp_p1;
294 again:
295 	len = sizeof addr;
296 	if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
297 	    &len)) < 0) {
298 		if (errno == EINTR)
299 			goto again;
300 		/*
301 		 * Clean out the most idle file descriptor when we're
302 		 * running out.
303 		 */
304 		if (errno == EMFILE || errno == ENFILE) {
305 			cleanfds = svc_fdset;
306 			__svc_clean_idle(&cleanfds, 0, FALSE);
307 			goto again;
308 		}
309 		return (FALSE);
310 	}
311 	/*
312 	 * make a new transporter (re-uses xprt)
313 	 */
314 	newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
315 	newxprt->xp_rtaddr.buf = mem_alloc(len);
316 	if (newxprt->xp_rtaddr.buf == NULL)
317 		return (FALSE);
318 	memcpy(newxprt->xp_rtaddr.buf, &addr, len);
319 	newxprt->xp_rtaddr.len = len;
320 #ifdef PORTMAP
321 	if (addr.ss_family == AF_INET || addr.ss_family == AF_LOCAL) {
322 		newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
323 		newxprt->xp_addrlen = sizeof (struct sockaddr_in);
324 	}
325 #endif				/* PORTMAP */
326 	if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
327 		len = 1;
328 		/* XXX fvdl - is this useful? */
329 		_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
330 	}
331 
332 	cd = (struct cf_conn *)newxprt->xp_p1;
333 
334 	cd->recvsize = r->recvsize;
335 	cd->sendsize = r->sendsize;
336 	cd->maxrec = r->maxrec;
337 
338 	if (cd->maxrec != 0) {
339 		flags = _fcntl(sock, F_GETFL, 0);
340 		if (flags  == -1)
341 			return (FALSE);
342 		if (_fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
343 			return (FALSE);
344 		if (cd->recvsize > cd->maxrec)
345 			cd->recvsize = cd->maxrec;
346 		cd->nonblock = TRUE;
347 		__xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
348 	} else
349 		cd->nonblock = FALSE;
350 
351 	gettimeofday(&cd->last_recv_time, NULL);
352 
353 	return (FALSE); /* there is never an rpc msg to be processed */
354 }
355 
356 /*ARGSUSED*/
357 static enum xprt_stat
358 rendezvous_stat(SVCXPRT *xprt __unused)
359 {
360 
361 	return (XPRT_IDLE);
362 }
363 
364 static void
365 svc_vc_destroy(SVCXPRT *xprt)
366 {
367 	assert(xprt != NULL);
368 
369 	xprt_unregister(xprt);
370 	__svc_vc_dodestroy(xprt);
371 }
372 
373 static void
374 __svc_vc_dodestroy(SVCXPRT *xprt)
375 {
376 	struct cf_conn *cd;
377 	struct cf_rendezvous *r;
378 
379 	cd = (struct cf_conn *)xprt->xp_p1;
380 
381 	if (xprt->xp_fd != RPC_ANYFD)
382 		_close(xprt->xp_fd);
383 	if (xprt->xp_port != 0) {
384 		/* a rendezvouser socket */
385 		r = (struct cf_rendezvous *)xprt->xp_p1;
386 		mem_free(r, sizeof (struct cf_rendezvous));
387 		xprt->xp_port = 0;
388 	} else {
389 		/* an actual connection socket */
390 		XDR_DESTROY(&(cd->xdrs));
391 		mem_free(cd, sizeof(struct cf_conn));
392 	}
393 	if (xprt->xp_rtaddr.buf)
394 		mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
395 	if (xprt->xp_ltaddr.buf)
396 		mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
397 	if (xprt->xp_tp)
398 		free(xprt->xp_tp);
399 	if (xprt->xp_netid)
400 		free(xprt->xp_netid);
401 	mem_free(xprt, sizeof(SVCXPRT));
402 }
403 
404 /*ARGSUSED*/
405 static bool_t
406 svc_vc_control(SVCXPRT *xprt __unused, const u_int rq __unused,
407     void *in __unused)
408 {
409 	return (FALSE);
410 }
411 
412 static bool_t
413 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
414 {
415 	struct cf_rendezvous *cfp;
416 
417 	cfp = (struct cf_rendezvous *)xprt->xp_p1;
418 	if (cfp == NULL)
419 		return (FALSE);
420 	switch (rq) {
421 		case SVCGET_CONNMAXREC:
422 			*(int *)in = cfp->maxrec;
423 			break;
424 		case SVCSET_CONNMAXREC:
425 			cfp->maxrec = *(int *)in;
426 			break;
427 		default:
428 			return (FALSE);
429 	}
430 	return (TRUE);
431 }
432 
433 /*
434  * reads data from the tcp or uip connection.
435  * any error is fatal and the connection is closed.
436  * (And a read of zero bytes is a half closed stream => error.)
437  * All read operations timeout after 35 seconds.  A timeout is
438  * fatal for the connection.
439  */
440 static int
441 read_vc(void *xprtp, void *buf, int len)
442 {
443 	SVCXPRT *xprt;
444 	int sock;
445 	int milliseconds = 35 * 1000;
446 	struct pollfd pollfd;
447 	struct cf_conn *cfp;
448 
449 	xprt = (SVCXPRT *)xprtp;
450 	assert(xprt != NULL);
451 
452 	sock = xprt->xp_fd;
453 
454 	cfp = (struct cf_conn *)xprt->xp_p1;
455 
456 	if (cfp->nonblock) {
457 		len = _read(sock, buf, (size_t)len);
458 		if (len < 0) {
459 			if (errno == EAGAIN)
460 				len = 0;
461 			else
462 				goto fatal_err;
463 		}
464 		if (len != 0)
465 			gettimeofday(&cfp->last_recv_time, NULL);
466 		return len;
467 	}
468 
469 	do {
470 		pollfd.fd = sock;
471 		pollfd.events = POLLIN;
472 		pollfd.revents = 0;
473 		switch (_poll(&pollfd, 1, milliseconds)) {
474 		case -1:
475 			if (errno == EINTR)
476 				continue;
477 			/*FALLTHROUGH*/
478 		case 0:
479 			goto fatal_err;
480 
481 		default:
482 			break;
483 		}
484 	} while ((pollfd.revents & POLLIN) == 0);
485 
486 	if ((len = _read(sock, buf, (size_t)len)) > 0) {
487 		gettimeofday(&cfp->last_recv_time, NULL);
488 		return (len);
489 	}
490 
491 fatal_err:
492 	((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
493 	return (-1);
494 }
495 
496 /*
497  * writes data to the tcp connection.
498  * Any error is fatal and the connection is closed.
499  */
500 static int
501 write_vc(void *xprtp, void *buf, int len)
502 {
503 	SVCXPRT *xprt;
504 	int i, cnt;
505 	struct cf_conn *cd;
506 	struct timeval tv0, tv1;
507 
508 	xprt = (SVCXPRT *)xprtp;
509 	assert(xprt != NULL);
510 
511 	cd = (struct cf_conn *)xprt->xp_p1;
512 
513 	if (cd->nonblock)
514 		gettimeofday(&tv0, NULL);
515 
516 	for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
517 		i = _write(xprt->xp_fd, buf, (size_t)cnt);
518 		if (i  < 0) {
519 			if (errno != EAGAIN || !cd->nonblock) {
520 				cd->strm_stat = XPRT_DIED;
521 				return (-1);
522 			}
523 			if (cd->nonblock && i != cnt) {
524 				/*
525 				 * For non-blocking connections, do not
526 				 * take more than 2 seconds writing the
527 				 * data out.
528 				 *
529 				 * XXX 2 is an arbitrary amount.
530 				 */
531 				gettimeofday(&tv1, NULL);
532 				if (tv1.tv_sec - tv0.tv_sec >= 2) {
533 					cd->strm_stat = XPRT_DIED;
534 					return (-1);
535 				}
536 			}
537 		}
538 	}
539 
540 	return (len);
541 }
542 
543 static enum xprt_stat
544 svc_vc_stat(SVCXPRT *xprt)
545 {
546 	struct cf_conn *cd;
547 
548 	assert(xprt != NULL);
549 
550 	cd = (struct cf_conn *)(xprt->xp_p1);
551 
552 	if (cd->strm_stat == XPRT_DIED)
553 		return (XPRT_DIED);
554 	if (! xdrrec_eof(&(cd->xdrs)))
555 		return (XPRT_MOREREQS);
556 	return (XPRT_IDLE);
557 }
558 
559 static bool_t
560 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg)
561 {
562 	struct cf_conn *cd;
563 	XDR *xdrs;
564 
565 	assert(xprt != NULL);
566 	assert(msg != NULL);
567 
568 	cd = (struct cf_conn *)(xprt->xp_p1);
569 	xdrs = &(cd->xdrs);
570 
571 	if (cd->nonblock) {
572 		if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
573 			return FALSE;
574 	} else {
575 		xdrrec_skiprecord(xdrs);
576 	}
577 
578 	xdrs->x_op = XDR_DECODE;
579 	if (xdr_callmsg(xdrs, msg)) {
580 		cd->x_id = msg->rm_xid;
581 		return (TRUE);
582 	}
583 	cd->strm_stat = XPRT_DIED;
584 	return (FALSE);
585 }
586 
587 static bool_t
588 svc_vc_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
589 {
590 
591 	assert(xprt != NULL);
592 	/* args_ptr may be NULL */
593 	return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
594 	    args_ptr));
595 }
596 
597 static bool_t
598 svc_vc_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
599 {
600 	XDR *xdrs;
601 
602 	assert(xprt != NULL);
603 	/* args_ptr may be NULL */
604 
605 	xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
606 
607 	xdrs->x_op = XDR_FREE;
608 	return ((*xdr_args)(xdrs, args_ptr));
609 }
610 
611 static bool_t
612 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg)
613 {
614 	struct cf_conn *cd;
615 	XDR *xdrs;
616 	bool_t rstat;
617 
618 	assert(xprt != NULL);
619 	assert(msg != NULL);
620 
621 	cd = (struct cf_conn *)(xprt->xp_p1);
622 	xdrs = &(cd->xdrs);
623 
624 	xdrs->x_op = XDR_ENCODE;
625 	msg->rm_xid = cd->x_id;
626 	rstat = xdr_replymsg(xdrs, msg);
627 	xdrrec_endofrecord(xdrs, TRUE);
628 	return (rstat);
629 }
630 
631 static void
632 svc_vc_ops(SVCXPRT *xprt)
633 {
634 	static struct xp_ops ops;
635 	static struct xp_ops2 ops2;
636 
637 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
638 
639 	mutex_lock(&ops_lock);
640 	if (ops.xp_recv == NULL) {
641 		ops.xp_recv = svc_vc_recv;
642 		ops.xp_stat = svc_vc_stat;
643 		ops.xp_getargs = svc_vc_getargs;
644 		ops.xp_reply = svc_vc_reply;
645 		ops.xp_freeargs = svc_vc_freeargs;
646 		ops.xp_destroy = svc_vc_destroy;
647 		ops2.xp_control = svc_vc_control;
648 	}
649 	xprt->xp_ops = &ops;
650 	xprt->xp_ops2 = &ops2;
651 	mutex_unlock(&ops_lock);
652 }
653 
654 static void
655 svc_vc_rendezvous_ops(SVCXPRT *xprt)
656 {
657 	static struct xp_ops ops;
658 	static struct xp_ops2 ops2;
659 
660 	mutex_lock(&ops_lock);
661 	if (ops.xp_recv == NULL) {
662 		ops.xp_recv = rendezvous_request;
663 		ops.xp_stat = rendezvous_stat;
664 		ops.xp_getargs =
665 		    (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
666 		ops.xp_reply =
667 		    (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
668 		ops.xp_freeargs =
669 		    (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
670 		ops.xp_destroy = svc_vc_destroy;
671 		ops2.xp_control = svc_vc_rendezvous_control;
672 	}
673 	xprt->xp_ops = &ops;
674 	xprt->xp_ops2 = &ops2;
675 	mutex_unlock(&ops_lock);
676 }
677 
678 /*
679  * Get the effective UID of the sending process. Used by rpcbind, keyserv
680  * and rpc.yppasswdd on AF_LOCAL.
681  */
682 int
683 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid)
684 {
685 	int sock, ret;
686 	gid_t egid;
687 	uid_t euid;
688 	struct sockaddr *sa;
689 
690 	sock = transp->xp_fd;
691 	sa = (struct sockaddr *)transp->xp_rtaddr.buf;
692 	if (sa->sa_family == AF_LOCAL) {
693 		ret = getpeereid(sock, &euid, &egid);
694 		if (ret == 0)
695 			*uid = euid;
696 		return (ret);
697 	} else
698 		return (-1);
699 }
700 
701 /*
702  * Destroy xprts that have not have had any activity in 'timeout' seconds.
703  * If 'cleanblock' is true, blocking connections (the default) are also
704  * cleaned. If timeout is 0, the least active connection is picked.
705  */
706 bool_t
707 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
708 {
709 	int i, ncleaned;
710 	SVCXPRT *xprt, *least_active;
711 	struct timeval tv, tdiff, tmax;
712 	struct cf_conn *cd;
713 
714 	gettimeofday(&tv, NULL);
715 	tmax.tv_sec = tmax.tv_usec = 0;
716 	least_active = NULL;
717 	rwlock_wrlock(&svc_fd_lock);
718 	for (i = ncleaned = 0; i <= svc_maxfd; i++) {
719 		if (FD_ISSET(i, fds)) {
720 			xprt = __svc_xports[i];
721 			if (xprt == NULL || xprt->xp_ops == NULL ||
722 			    xprt->xp_ops->xp_recv != svc_vc_recv)
723 				continue;
724 			cd = (struct cf_conn *)xprt->xp_p1;
725 			if (!cleanblock && !cd->nonblock)
726 				continue;
727 			if (timeout == 0) {
728 				timersub(&tv, &cd->last_recv_time, &tdiff);
729 				if (timercmp(&tdiff, &tmax, >)) {
730 					tmax = tdiff;
731 					least_active = xprt;
732 				}
733 				continue;
734 			}
735 			if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
736 				__xprt_unregister_unlocked(xprt);
737 				__svc_vc_dodestroy(xprt);
738 				ncleaned++;
739 			}
740 		}
741 	}
742 	if (timeout == 0 && least_active != NULL) {
743 		__xprt_unregister_unlocked(least_active);
744 		__svc_vc_dodestroy(least_active);
745 		ncleaned++;
746 	}
747 	rwlock_unlock(&svc_fd_lock);
748 	return ncleaned > 0 ? TRUE : FALSE;
749 }
750