xref: /dragonfly/lib/libc/rpc/svc_vc.c (revision e0b1d537)
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  */
34 
35 /*
36  * svc_vc.c, Server side for Connection Oriented based RPC.
37  *
38  * Actually implements two flavors of transporter -
39  * a tcp rendezvouser (a listner and connection establisher)
40  * and a record/tcp stream.
41  */
42 
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/poll.h>
48 #include <sys/socket.h>
49 #include <sys/un.h>
50 #include <sys/time.h>
51 #include <sys/uio.h>
52 #include <netinet/in.h>
53 #include <netinet/tcp.h>
54 
55 #include <assert.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include <rpc/rpc.h>
65 
66 #include "rpc_com.h"
67 #include "mt_misc.h"
68 #include "un-namespace.h"
69 
70 static SVCXPRT		*makefd_xprt(int, u_int, u_int);
71 static bool_t		rendezvous_request(SVCXPRT *, struct rpc_msg *);
72 static enum xprt_stat	rendezvous_stat(SVCXPRT *);
73 static void		svc_vc_destroy(SVCXPRT *);
74 static void		__svc_vc_dodestroy (SVCXPRT *);
75 static int		read_vc(void *, void *, int);
76 static int		write_vc(void *, void *, int);
77 static enum xprt_stat	svc_vc_stat(SVCXPRT *);
78 static bool_t		svc_vc_recv(SVCXPRT *, struct rpc_msg *);
79 static bool_t		svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
80 static bool_t		svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
81 static bool_t		svc_vc_reply(SVCXPRT *, struct rpc_msg *);
82 static void		svc_vc_rendezvous_ops(SVCXPRT *);
83 static void		svc_vc_ops(SVCXPRT *);
84 static bool_t		svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
85 static bool_t		svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq,
86 						  void *in);
87 
88 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
89 	u_int sendsize;
90 	u_int recvsize;
91 	int maxrec;
92 };
93 
94 struct cf_conn {  /* kept in xprt->xp_p1 for actual connection */
95 	enum xprt_stat strm_stat;
96 	u_int32_t x_id;
97 	XDR xdrs;
98 	char verf_body[MAX_AUTH_BYTES];
99 	u_int sendsize;
100 	u_int recvsize;
101 	int maxrec;
102 	bool_t nonblock;
103 	struct timeval last_recv_time;
104 };
105 
106 /*
107  * Usage:
108  *	xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
109  *
110  * Creates, registers, and returns a (rpc) tcp based transporter.
111  * Once *xprt is initialized, it is registered as a transporter
112  * see (svc.h, xprt_register).  This routine returns
113  * a NULL if a problem occurred.
114  *
115  * The filedescriptor passed in is expected to refer to a bound, but
116  * not yet connected socket.
117  *
118  * Since streams do buffered io similar to stdio, the caller can specify
119  * how big the send and receive buffers are via the second and third parms;
120  * 0 => use the system default.
121  */
122 SVCXPRT *
123 svc_vc_create(int fd, u_int sendsize, u_int recvsize)
124 {
125 	SVCXPRT *xprt = NULL;
126 	struct cf_rendezvous *r = NULL;
127 	struct __rpc_sockinfo si;
128 	struct sockaddr_storage sslocal;
129 	socklen_t slen;
130 
131 	if (!__rpc_fd2sockinfo(fd, &si))
132 		return NULL;
133 
134 	r = mem_alloc(sizeof(*r));
135 	if (r == NULL) {
136 		warnx("svc_vc_create: out of memory");
137 		goto cleanup_svc_vc_create;
138 	}
139 	r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
140 	r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
141 	r->maxrec = __svc_maxrec;
142 	xprt = mem_alloc(sizeof(SVCXPRT));
143 	if (xprt == NULL) {
144 		warnx("svc_vc_create: out of memory");
145 		goto cleanup_svc_vc_create;
146 	}
147 	xprt->xp_tp = NULL;
148 	xprt->xp_p1 = r;
149 	xprt->xp_p2 = NULL;
150 	xprt->xp_p3 = NULL;
151 	xprt->xp_verf = _null_auth;
152 	svc_vc_rendezvous_ops(xprt);
153 	xprt->xp_port = (u_short)-1;	/* It is the rendezvouser */
154 	xprt->xp_fd = fd;
155 
156 	slen = sizeof (struct sockaddr_storage);
157 	if (_getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
158 		warnx("svc_vc_create: could not retrieve local addr");
159 		goto cleanup_svc_vc_create;
160 	}
161 
162 	xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
163 	xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
164 	if (xprt->xp_ltaddr.buf == NULL) {
165 		warnx("svc_vc_create: no mem for local addr");
166 		goto cleanup_svc_vc_create;
167 	}
168 	memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
169 
170 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
171 	xprt_register(xprt);
172 	return (xprt);
173 cleanup_svc_vc_create:
174 	if (xprt)
175 		mem_free(xprt, sizeof(*xprt));
176 	if (r != NULL)
177 		mem_free(r, sizeof(*r));
178 	return (NULL);
179 }
180 
181 /*
182  * Like svtcp_create(), except the routine takes any *open* UNIX file
183  * descriptor as its first input.
184  */
185 SVCXPRT *
186 svc_fd_create(int fd, u_int sendsize, u_int recvsize)
187 {
188 	struct sockaddr_storage ss;
189 	socklen_t slen;
190 	SVCXPRT *ret;
191 
192 	assert(fd != -1);
193 
194 	ret = makefd_xprt(fd, sendsize, recvsize);
195 	if (ret == NULL)
196 		return NULL;
197 
198 	slen = sizeof (struct sockaddr_storage);
199 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
200 		warnx("svc_fd_create: could not retrieve local addr");
201 		goto freedata;
202 	}
203 	ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
204 	ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
205 	if (ret->xp_ltaddr.buf == NULL) {
206 		warnx("svc_fd_create: no mem for local addr");
207 		goto freedata;
208 	}
209 	memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
210 
211 	slen = sizeof (struct sockaddr_storage);
212 	if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
213 		warnx("svc_fd_create: could not retrieve remote addr");
214 		goto freedata;
215 	}
216 	ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
217 	ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
218 	if (ret->xp_rtaddr.buf == NULL) {
219 		warnx("svc_fd_create: no mem for local addr");
220 		goto freedata;
221 	}
222 	memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
223 #ifdef PORTMAP
224 	if (ss.ss_family == AF_INET || ss.ss_family == AF_LOCAL) {
225 		ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
226 		ret->xp_addrlen = sizeof (struct sockaddr_in);
227 	}
228 #endif				/* PORTMAP */
229 
230 	return ret;
231 
232 freedata:
233 	if (ret->xp_ltaddr.buf != NULL)
234 		mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
235 
236 	return NULL;
237 }
238 
239 static SVCXPRT *
240 makefd_xprt(int fd, u_int sendsize, u_int recvsize)
241 {
242 	SVCXPRT *xprt;
243 	struct cf_conn *cd;
244 	const char *netid;
245 	struct __rpc_sockinfo si;
246 
247 	assert(fd != -1);
248 
249 	xprt = mem_alloc(sizeof(SVCXPRT));
250 	if (xprt == NULL) {
251 		warnx("svc_vc: makefd_xprt: out of memory");
252 		goto done;
253 	}
254 	memset(xprt, 0, sizeof *xprt);
255 	cd = mem_alloc(sizeof(struct cf_conn));
256 	if (cd == NULL) {
257 		warnx("svc_tcp: makefd_xprt: out of memory");
258 		mem_free(xprt, sizeof(SVCXPRT));
259 		xprt = NULL;
260 		goto done;
261 	}
262 	cd->strm_stat = XPRT_IDLE;
263 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
264 	    xprt, read_vc, write_vc);
265 	xprt->xp_p1 = cd;
266 	xprt->xp_verf.oa_base = cd->verf_body;
267 	svc_vc_ops(xprt);  /* truely deals with calls */
268 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
269 	xprt->xp_fd = fd;
270         if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
271 		xprt->xp_netid = strdup(netid);
272 
273 	xprt_register(xprt);
274 done:
275 	return (xprt);
276 }
277 
278 /*ARGSUSED*/
279 static bool_t
280 rendezvous_request(SVCXPRT *xprt, struct rpc_msg *msg)
281 {
282 	int sock, flags;
283 	struct cf_rendezvous *r;
284 	struct cf_conn *cd;
285 	struct sockaddr_storage addr;
286 	socklen_t len;
287 	struct __rpc_sockinfo si;
288 	SVCXPRT *newxprt;
289 	fd_set cleanfds;
290 
291 	assert(xprt != NULL);
292 	assert(msg != NULL);
293 
294 	r = (struct cf_rendezvous *)xprt->xp_p1;
295 again:
296 	len = sizeof addr;
297 	if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
298 	    &len)) < 0) {
299 		if (errno == EINTR)
300 			goto again;
301 		/*
302 		 * Clean out the most idle file descriptor when we're
303 		 * running out.
304 		 */
305 		if (errno == EMFILE || errno == ENFILE) {
306 			cleanfds = svc_fdset;
307 			__svc_clean_idle(&cleanfds, 0, FALSE);
308 			goto again;
309 		}
310 		return (FALSE);
311 	}
312 	/*
313 	 * make a new transporter (re-uses xprt)
314 	 */
315 	newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
316 	newxprt->xp_rtaddr.buf = mem_alloc(len);
317 	if (newxprt->xp_rtaddr.buf == NULL)
318 		return (FALSE);
319 	memcpy(newxprt->xp_rtaddr.buf, &addr, len);
320 	newxprt->xp_rtaddr.len = len;
321 #ifdef PORTMAP
322 	if (addr.ss_family == AF_INET || addr.ss_family == AF_LOCAL) {
323 		newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
324 		newxprt->xp_addrlen = sizeof (struct sockaddr_in);
325 	}
326 #endif				/* PORTMAP */
327 	if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
328 		len = 1;
329 		/* XXX fvdl - is this useful? */
330 		_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
331 	}
332 
333 	cd = (struct cf_conn *)newxprt->xp_p1;
334 
335 	cd->recvsize = r->recvsize;
336 	cd->sendsize = r->sendsize;
337 	cd->maxrec = r->maxrec;
338 
339 	if (cd->maxrec != 0) {
340 		flags = _fcntl(sock, F_GETFL, 0);
341 		if (flags  == -1)
342 			return (FALSE);
343 		if (_fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
344 			return (FALSE);
345 		if (cd->recvsize > cd->maxrec)
346 			cd->recvsize = cd->maxrec;
347 		cd->nonblock = TRUE;
348 		__xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
349 	} else
350 		cd->nonblock = FALSE;
351 
352 	gettimeofday(&cd->last_recv_time, NULL);
353 
354 	return (FALSE); /* there is never an rpc msg to be processed */
355 }
356 
357 /*ARGSUSED*/
358 static enum xprt_stat
359 rendezvous_stat(SVCXPRT *xprt __unused)
360 {
361 
362 	return (XPRT_IDLE);
363 }
364 
365 static void
366 svc_vc_destroy(SVCXPRT *xprt)
367 {
368 	assert(xprt != NULL);
369 
370 	xprt_unregister(xprt);
371 	__svc_vc_dodestroy(xprt);
372 }
373 
374 static void
375 __svc_vc_dodestroy(SVCXPRT *xprt)
376 {
377 	struct cf_conn *cd;
378 	struct cf_rendezvous *r;
379 
380 	cd = (struct cf_conn *)xprt->xp_p1;
381 
382 	if (xprt->xp_fd != RPC_ANYFD)
383 		_close(xprt->xp_fd);
384 	if (xprt->xp_port != 0) {
385 		/* a rendezvouser socket */
386 		r = (struct cf_rendezvous *)xprt->xp_p1;
387 		mem_free(r, sizeof (struct cf_rendezvous));
388 		xprt->xp_port = 0;
389 	} else {
390 		/* an actual connection socket */
391 		XDR_DESTROY(&(cd->xdrs));
392 		mem_free(cd, sizeof(struct cf_conn));
393 	}
394 	if (xprt->xp_rtaddr.buf)
395 		mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
396 	if (xprt->xp_ltaddr.buf)
397 		mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
398 	if (xprt->xp_tp)
399 		free(xprt->xp_tp);
400 	if (xprt->xp_netid)
401 		free(xprt->xp_netid);
402 	mem_free(xprt, sizeof(SVCXPRT));
403 }
404 
405 /*ARGSUSED*/
406 static bool_t
407 svc_vc_control(SVCXPRT *xprt __unused, const u_int rq __unused,
408     void *in __unused)
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