Merge remote branch 'crater/vendor/MDOCML' into HEAD
[dragonfly.git] / lib / libc / rpc / svc_vc.c
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;
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)
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, const u_int rq, void *in)
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 }