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