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