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