Imported Upstream version 0.2.5
[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_auth = NULL;
176         xprt->xp_verf = _null_auth;
177         svc_vc_rendezvous_ops(xprt);
178         xprt->xp_port = (u_short)-1;    /* It is the rendezvouser */
179         xprt->xp_fd = fd;
180
181         slen = sizeof (struct sockaddr_storage);
182         if (getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
183                 warnx("svc_vc_create: could not retrieve local addr");
184                 goto cleanup_svc_vc_create;
185         }
186
187         if (!__rpc_set_netbuf(&xprt->xp_ltaddr, &sslocal, sizeof(sslocal))) {
188                 warnx("svc_vc_create: no mem for local addr");
189                 goto cleanup_svc_vc_create;
190         }
191         xprt_register(xprt);
192         return (xprt);
193 cleanup_svc_vc_create:
194         if (r != NULL)
195                 mem_free(r, sizeof(*r));
196         return (NULL);
197 }
198
199 /*
200  * Like svtcp_create(), except the routine takes any *open* UNIX file
201  * descriptor as its first input.
202  */
203 SVCXPRT *
204 svc_fd_create(fd, sendsize, recvsize)
205         int fd;
206         u_int sendsize;
207         u_int recvsize;
208 {
209         struct sockaddr_storage ss;
210         socklen_t slen;
211         SVCXPRT *ret;
212
213         assert(fd != -1);
214
215         ret = makefd_xprt(fd, sendsize, recvsize);
216         if (ret == NULL)
217                 return NULL;
218
219         slen = sizeof (struct sockaddr_storage);
220         if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
221                 warnx("svc_fd_create: could not retrieve local addr");
222                 goto freedata;
223         }
224         if (!__rpc_set_netbuf(&ret->xp_ltaddr, &ss, sizeof(ss))) {
225                 warnx("svc_fd_create: no mem for local addr");
226                 goto freedata;
227         }
228
229         slen = sizeof (struct sockaddr_storage);
230         if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
231                 warnx("svc_fd_create: could not retrieve remote addr");
232                 goto freedata;
233         }
234         if (!__rpc_set_netbuf(&ret->xp_rtaddr, &ss, sizeof(ss))) {
235                 warnx("svc_fd_create: no mem for local addr");
236                 goto freedata;
237         }
238
239         /* Set xp_raddr for compatibility */
240         __xprt_set_raddr(ret, &ss);
241
242         return ret;
243
244 freedata:
245         if (ret->xp_ltaddr.buf != NULL)
246                 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
247
248         return NULL;
249 }
250
251 static SVCXPRT *
252 makefd_xprt(fd, sendsize, recvsize)
253         int fd;
254         u_int sendsize;
255         u_int recvsize;
256 {
257         SVCXPRT *xprt;
258         struct cf_conn *cd;
259         const char *netid;
260         struct __rpc_sockinfo si;
261  
262         assert(fd != -1);
263
264         if (fd >= FD_SETSIZE) {
265                 warnx("svc_vc: makefd_xprt: fd too high\n");
266                 xprt = NULL;
267                 goto done;
268         }
269
270         xprt = mem_alloc(sizeof(SVCXPRT));
271         if (xprt == NULL) {
272                 warnx("svc_vc: makefd_xprt: out of memory");
273                 goto done;
274         }
275         memset(xprt, 0, sizeof *xprt);
276         cd = mem_alloc(sizeof(struct cf_conn));
277         if (cd == NULL) {
278                 warnx("svc_tcp: makefd_xprt: out of memory");
279                 mem_free(xprt, sizeof(SVCXPRT));
280                 xprt = NULL;
281                 goto done;
282         }
283         cd->strm_stat = XPRT_IDLE;
284         xdrrec_create(&(cd->xdrs), sendsize, recvsize,
285             xprt, read_vc, write_vc);
286         xprt->xp_p1 = cd;
287         xprt->xp_auth = NULL;
288         xprt->xp_verf.oa_base = cd->verf_body;
289         svc_vc_ops(xprt);  /* truely deals with calls */
290         xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
291         xprt->xp_fd = fd;
292         if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
293                 xprt->xp_netid = strdup(netid);
294
295         xprt_register(xprt);
296 done:
297         return (xprt);
298 }
299
300 /*ARGSUSED*/
301 static bool_t
302 rendezvous_request(xprt, msg)
303         SVCXPRT *xprt;
304         struct rpc_msg *msg;
305 {
306         int sock, flags;
307         struct cf_rendezvous *r;
308         struct cf_conn *cd;
309         struct sockaddr_storage addr;
310         socklen_t len;
311         struct __rpc_sockinfo si;
312         SVCXPRT *newxprt;
313         fd_set cleanfds;
314
315         assert(xprt != NULL);
316         assert(msg != NULL);
317
318         r = (struct cf_rendezvous *)xprt->xp_p1;
319 again:
320         len = sizeof addr;
321         if ((sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
322             &len)) < 0) {
323                 if (errno == EINTR)
324                         goto again;
325                 /*
326                  * Clean out the most idle file descriptor when we're
327                  * running out.
328                  */
329                 if (errno == EMFILE || errno == ENFILE) {
330                         cleanfds = svc_fdset;
331                         __svc_clean_idle(&cleanfds, 0, FALSE);
332                         goto again;
333                 }
334                 return (FALSE);
335         }
336         /*
337          * make a new transporter (re-uses xprt)
338          */
339
340         newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
341
342         if (!__rpc_set_netbuf(&newxprt->xp_rtaddr, &addr, len))
343                 return (FALSE);
344
345         __xprt_set_raddr(newxprt, &addr);
346
347         if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
348                 len = 1;
349                 /* XXX fvdl - is this useful? */
350                 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
351         }
352
353         cd = (struct cf_conn *)newxprt->xp_p1;
354
355         cd->recvsize = r->recvsize;
356         cd->sendsize = r->sendsize;
357         cd->maxrec = r->maxrec;
358
359         if (cd->maxrec != 0) {
360                 flags = fcntl(sock, F_GETFL, 0);
361                 if (flags  == -1)
362                         return (FALSE);
363                 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
364                         return (FALSE);
365                 if (cd->recvsize > cd->maxrec)
366                         cd->recvsize = cd->maxrec;
367                 cd->nonblock = TRUE;
368                 __xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
369         } else
370                 cd->nonblock = FALSE;
371
372         gettimeofday(&cd->last_recv_time, NULL);
373
374         return (FALSE); /* there is never an rpc msg to be processed */
375 }
376
377 /*ARGSUSED*/
378 static enum xprt_stat
379 rendezvous_stat(xprt)
380         SVCXPRT *xprt;
381 {
382
383         return (XPRT_IDLE);
384 }
385
386 static void
387 svc_vc_destroy(xprt)
388         SVCXPRT *xprt;
389 {
390         assert(xprt != NULL);
391         
392         xprt_unregister(xprt);
393         __svc_vc_dodestroy(xprt);
394 }
395
396 static void
397 __svc_vc_dodestroy(xprt)
398         SVCXPRT *xprt;
399 {
400         struct cf_conn *cd;
401         struct cf_rendezvous *r;
402
403         cd = (struct cf_conn *)xprt->xp_p1;
404
405         if (xprt->xp_fd != RPC_ANYFD)
406                 (void)close(xprt->xp_fd);
407         if (xprt->xp_port != 0) {
408                 /* a rendezvouser socket */
409                 r = (struct cf_rendezvous *)xprt->xp_p1;
410                 mem_free(r, sizeof (struct cf_rendezvous));
411                 xprt->xp_port = 0;
412         } else {
413                 /* an actual connection socket */
414                 XDR_DESTROY(&(cd->xdrs));
415                 mem_free(cd, sizeof(struct cf_conn));
416         }
417         if (xprt->xp_auth != NULL) {
418                 SVCAUTH_DESTROY(xprt->xp_auth);
419                 xprt->xp_auth = NULL;
420         }
421         if (xprt->xp_rtaddr.buf)
422                 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
423         if (xprt->xp_ltaddr.buf)
424                 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
425         if (xprt->xp_tp)
426                 free(xprt->xp_tp);
427         if (xprt->xp_netid)
428                 free(xprt->xp_netid);
429         mem_free(xprt, sizeof(SVCXPRT));
430 }
431
432 /*ARGSUSED*/
433 static bool_t
434 svc_vc_control(xprt, rq, in)
435         SVCXPRT *xprt;
436         const u_int rq;
437         void *in;
438 {
439         return (FALSE);
440 }
441
442 static bool_t
443 svc_vc_rendezvous_control(xprt, rq, in)
444         SVCXPRT *xprt;
445         const u_int rq;
446         void *in;
447 {
448         struct cf_rendezvous *cfp;
449
450         cfp = (struct cf_rendezvous *)xprt->xp_p1;
451         if (cfp == NULL)
452                 return (FALSE);
453         switch (rq) {
454                 case SVCGET_CONNMAXREC:
455                         *(int *)in = cfp->maxrec;
456                         break;
457                 case SVCSET_CONNMAXREC:
458                         cfp->maxrec = *(int *)in;
459                         break;
460                 default:
461                         return (FALSE);
462         }
463         return (TRUE);
464 }
465
466 /*
467  * reads data from the tcp or uip connection.
468  * any error is fatal and the connection is closed.
469  * (And a read of zero bytes is a half closed stream => error.)
470  * All read operations timeout after 35 seconds.  A timeout is
471  * fatal for the connection.
472  */
473 static int
474 read_vc(xprtp, buf, len)
475         void *xprtp;
476         void *buf;
477         int len;
478 {
479         SVCXPRT *xprt;
480         int sock;
481         int milliseconds = 35 * 1000;
482         struct pollfd pollfd;
483         struct cf_conn *cfp;
484
485         xprt = (SVCXPRT *)xprtp;
486         assert(xprt != NULL);
487
488         sock = xprt->xp_fd;
489
490         cfp = (struct cf_conn *)xprt->xp_p1;
491
492         if (cfp->nonblock) {
493                 len = read(sock, buf, (size_t)len);
494                 if (len < 0) {
495                         if (errno == EAGAIN)
496                                 len = 0;
497                         else
498                                 goto fatal_err;
499                 }
500                 if (len != 0)
501                         gettimeofday(&cfp->last_recv_time, NULL);
502                 return len;
503         }
504
505         do {
506                 pollfd.fd = sock;
507                 pollfd.events = POLLIN;
508                 pollfd.revents = 0;
509                 switch (poll(&pollfd, 1, milliseconds)) {
510                 case -1:
511                         if (errno == EINTR)
512                                 continue;
513                         /*FALLTHROUGH*/
514                 case 0:
515                         goto fatal_err;
516
517                 default:
518                         break;
519                 }
520         } while ((pollfd.revents & POLLIN) == 0);
521
522         if ((len = read(sock, buf, (size_t)len)) > 0) {
523                 gettimeofday(&cfp->last_recv_time, NULL);
524                 return (len);
525         }
526
527 fatal_err:
528         ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
529         return (-1);
530 }
531
532 /*
533  * writes data to the tcp connection.
534  * Any error is fatal and the connection is closed.
535  */
536 static int
537 write_vc(xprtp, buf, len)
538         void *xprtp;
539         void *buf;
540         int len;
541 {
542         SVCXPRT *xprt;
543         int i, cnt;
544         struct cf_conn *cd;
545         struct timeval tv0, tv1;
546
547         xprt = (SVCXPRT *)xprtp;
548         assert(xprt != NULL);
549
550         cd = (struct cf_conn *)xprt->xp_p1;
551
552         if (cd->nonblock)
553                 gettimeofday(&tv0, NULL);
554         
555         for (cnt = len; cnt > 0; cnt -= i, buf += i) {
556                 i = write(xprt->xp_fd, buf, (size_t)cnt);
557                 if (i  < 0) {
558                         if (errno != EAGAIN || !cd->nonblock) {
559                                 cd->strm_stat = XPRT_DIED;
560                                 return (-1);
561                         }
562                         if (cd->nonblock && i != cnt) {
563                                 /*
564                                  * For non-blocking connections, do not
565                                  * take more than 2 seconds writing the
566                                  * data out.
567                                  *
568                                  * XXX 2 is an arbitrary amount.
569                                  */
570                                 gettimeofday(&tv1, NULL);
571                                 if (tv1.tv_sec - tv0.tv_sec >= 2) {
572                                         cd->strm_stat = XPRT_DIED;
573                                         return (-1);
574                                 }
575                         }
576                 }
577         }
578
579         return (len);
580 }
581
582 static enum xprt_stat
583 svc_vc_stat(xprt)
584         SVCXPRT *xprt;
585 {
586         struct cf_conn *cd;
587
588         assert(xprt != NULL);
589
590         cd = (struct cf_conn *)(xprt->xp_p1);
591
592         if (cd->strm_stat == XPRT_DIED)
593                 return (XPRT_DIED);
594         if (! xdrrec_eof(&(cd->xdrs)))
595                 return (XPRT_MOREREQS);
596         return (XPRT_IDLE);
597 }
598
599 static bool_t
600 svc_vc_recv(xprt, msg)
601         SVCXPRT *xprt;
602         struct rpc_msg *msg;
603 {
604         struct cf_conn *cd;
605         XDR *xdrs;
606
607         assert(xprt != NULL);
608         assert(msg != NULL);
609
610         cd = (struct cf_conn *)(xprt->xp_p1);
611         xdrs = &(cd->xdrs);
612
613         if (cd->nonblock) {
614                 if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
615                         return FALSE;
616         }
617
618         xdrs->x_op = XDR_DECODE;
619         /*
620          * No need skip records with nonblocking connections
621          */
622         if (cd->nonblock == FALSE)
623                 (void)xdrrec_skiprecord(xdrs);
624         if (xdr_callmsg(xdrs, msg)) {
625                 cd->x_id = msg->rm_xid;
626                 return (TRUE);
627         }
628         cd->strm_stat = XPRT_DIED;
629         return (FALSE);
630 }
631
632 static bool_t
633 svc_vc_getargs(xprt, xdr_args, args_ptr)
634         SVCXPRT *xprt;
635         xdrproc_t xdr_args;
636         void *args_ptr;
637 {
638
639         assert(xprt != NULL);
640         /* args_ptr may be NULL */
641
642         if (! SVCAUTH_UNWRAP(xprt->xp_auth,
643                              &(((struct cf_conn *)(xprt->xp_p1))->xdrs),
644                              xdr_args, args_ptr)) {
645                 return FALSE;  
646         }
647         return TRUE;
648 }
649
650 static bool_t
651 svc_vc_freeargs(xprt, xdr_args, args_ptr)
652         SVCXPRT *xprt;
653         xdrproc_t xdr_args;
654         void *args_ptr;
655 {
656         XDR *xdrs;
657
658         assert(xprt != NULL);
659         /* args_ptr may be NULL */
660
661         xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
662
663         xdrs->x_op = XDR_FREE;
664         return ((*xdr_args)(xdrs, args_ptr));
665 }
666
667 static bool_t
668 svc_vc_reply(xprt, msg)
669         SVCXPRT *xprt;
670         struct rpc_msg *msg;
671 {
672         struct cf_conn *cd;
673         XDR *xdrs;
674         bool_t rstat;
675
676         xdrproc_t xdr_results;
677         caddr_t xdr_location;
678         bool_t has_args;
679
680         assert(xprt != NULL);
681         assert(msg != NULL);
682
683         cd = (struct cf_conn *)(xprt->xp_p1);
684         xdrs = &(cd->xdrs);
685
686         if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
687             msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
688                 has_args = TRUE;
689                 xdr_results = msg->acpted_rply.ar_results.proc;
690                 xdr_location = msg->acpted_rply.ar_results.where;
691
692                 msg->acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
693                 msg->acpted_rply.ar_results.where = NULL;
694         } else
695                 has_args = FALSE;
696
697         xdrs->x_op = XDR_ENCODE;
698         msg->rm_xid = cd->x_id;
699         rstat = FALSE;
700         if (xdr_replymsg(xdrs, msg) &&
701             (!has_args || (xprt->xp_auth &&
702              SVCAUTH_WRAP(xprt->xp_auth, xdrs, xdr_results, xdr_location)))) {
703                 rstat = TRUE;
704         }
705         (void)xdrrec_endofrecord(xdrs, TRUE);
706         return (rstat);
707 }
708
709 static void
710 svc_vc_ops(xprt)
711         SVCXPRT *xprt;
712 {
713         static struct xp_ops ops;
714         static struct xp_ops2 ops2;
715         extern mutex_t ops_lock;
716
717 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
718
719         mutex_lock(&ops_lock);
720         if (ops.xp_recv == NULL) {
721                 ops.xp_recv = svc_vc_recv;
722                 ops.xp_stat = svc_vc_stat;
723                 ops.xp_getargs = svc_vc_getargs;
724                 ops.xp_reply = svc_vc_reply;
725                 ops.xp_freeargs = svc_vc_freeargs;
726                 ops.xp_destroy = svc_vc_destroy;
727                 ops2.xp_control = svc_vc_control;
728         }
729         xprt->xp_ops = &ops;
730         xprt->xp_ops2 = &ops2;
731         mutex_unlock(&ops_lock);
732 }
733
734 static void
735 svc_vc_rendezvous_ops(xprt)
736         SVCXPRT *xprt;
737 {
738         static struct xp_ops ops;
739         static struct xp_ops2 ops2;
740         extern mutex_t ops_lock;
741
742         mutex_lock(&ops_lock);
743         if (ops.xp_recv == NULL) {
744                 ops.xp_recv = rendezvous_request;
745                 ops.xp_stat = rendezvous_stat;
746                 ops.xp_getargs =
747                     (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
748                 ops.xp_reply =
749                     (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
750                 ops.xp_freeargs =
751                     (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
752                 ops.xp_destroy = svc_vc_destroy;
753                 ops2.xp_control = svc_vc_rendezvous_control;
754         }
755         xprt->xp_ops = &ops;
756         xprt->xp_ops2 = &ops2;
757         mutex_unlock(&ops_lock);
758 }
759
760 /*
761  * Get the effective UID of the sending process. Used by rpcbind, keyserv
762  * and rpc.yppasswdd on AF_LOCAL.
763  */
764 int
765 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
766         int sock, ret;
767         gid_t egid;
768         uid_t euid;
769         struct sockaddr *sa;
770
771         sock = transp->xp_fd;
772         sa = (struct sockaddr *)transp->xp_rtaddr.buf;
773         if (sa->sa_family == AF_LOCAL) {
774                 ret = getpeereid(sock, &euid, &egid);
775                 if (ret == 0)
776                         *uid = euid;
777                 return (ret);
778         } else
779                 return (-1);
780 }
781
782 /*
783  * Destroy xprts that have not have had any activity in 'timeout' seconds.
784  * If 'cleanblock' is true, blocking connections (the default) are also
785  * cleaned. If timeout is 0, the least active connection is picked.
786  *
787  * Though this is not a publicly documented interface, some versions of
788  * rpcbind are known to call this function.  Do not alter or remove this
789  * API without changing the library's sonum.
790  */
791 bool_t
792 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
793 {
794         int i, ncleaned;
795         SVCXPRT *xprt, *least_active;
796         struct timeval tv, tdiff, tmax;
797         struct cf_conn *cd;
798
799         gettimeofday(&tv, NULL);
800         tmax.tv_sec = tmax.tv_usec = 0;
801         least_active = NULL;
802         rwlock_wrlock(&svc_fd_lock);
803         for (i = ncleaned = 0; i <= svc_maxfd; i++) {
804                 if (FD_ISSET(i, fds)) {
805                         xprt = __svc_xports[i];
806                         if (xprt == NULL || xprt->xp_ops == NULL ||
807                             xprt->xp_ops->xp_recv != svc_vc_recv)
808                                 continue;
809                         cd = (struct cf_conn *)xprt->xp_p1;
810                         if (!cleanblock && !cd->nonblock)
811                                 continue;
812                         if (timeout == 0) {
813                                 timersub(&tv, &cd->last_recv_time, &tdiff);
814                                 if (timercmp(&tdiff, &tmax, >)) {
815                                         tmax = tdiff;
816                                         least_active = xprt;
817                                 }
818                                 continue;
819                         }
820                         if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
821                                 __xprt_unregister_unlocked(xprt);
822                                 __svc_vc_dodestroy(xprt);
823                                 ncleaned++;
824                         }
825                 }
826         }
827         if (timeout == 0 && least_active != NULL) {
828                 __xprt_unregister_unlocked(least_active);
829                 __svc_vc_dodestroy(least_active);
830                 ncleaned++;
831         }
832         rwlock_unlock(&svc_fd_lock);
833         return ncleaned > 0 ? TRUE : FALSE;
834 }