]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/rpc/svc_vc.c
MFV r289003:
[FreeBSD/FreeBSD.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  * Copyright (c) 2009, Sun Microsystems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without 
8  * modification, are permitted provided that the following conditions are met:
9  * - Redistributions of source code must retain the above copyright notice, 
10  *   this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice, 
12  *   this list of conditions and the following disclaimer in the documentation 
13  *   and/or other materials provided with the distribution.
14  * - Neither the name of Sun Microsystems, Inc. nor the names of its 
15  *   contributors may be used to endorse or promote products derived 
16  *   from this software without specific prior written permission.
17  * 
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)svc_tcp.c    2.2 88/08/01 4.0 RPCSRC";
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 /*
39  * svc_vc.c, Server side for Connection Oriented based RPC. 
40  *
41  * Actually implements two flavors of transporter -
42  * a tcp rendezvouser (a listner and connection establisher)
43  * and a record/tcp stream.
44  */
45
46 #include "namespace.h"
47 #include "reentrant.h"
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/poll.h>
51 #include <sys/socket.h>
52 #include <sys/un.h>
53 #include <sys/time.h>
54 #include <sys/uio.h>
55 #include <netinet/in.h>
56 #include <netinet/tcp.h>
57
58 #include <assert.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66
67 #include <rpc/rpc.h>
68
69 #include "rpc_com.h"
70 #include "mt_misc.h"
71 #include "un-namespace.h"
72
73 static SVCXPRT *makefd_xprt(int, u_int, u_int);
74 static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
75 static enum xprt_stat rendezvous_stat(SVCXPRT *);
76 static void svc_vc_destroy(SVCXPRT *);
77 static void __svc_vc_dodestroy (SVCXPRT *);
78 static int read_vc(void *, void *, int);
79 static int write_vc(void *, void *, int);
80 static enum xprt_stat svc_vc_stat(SVCXPRT *);
81 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *);
82 static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
83 static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
84 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *);
85 static void svc_vc_rendezvous_ops(SVCXPRT *);
86 static void svc_vc_ops(SVCXPRT *);
87 static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
88 static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
89                                              void *in);
90
91 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
92         u_int sendsize;
93         u_int recvsize;
94         int maxrec;
95 };
96
97 struct cf_conn {  /* kept in xprt->xp_p1 for actual connection */
98         enum xprt_stat strm_stat;
99         u_int32_t x_id;
100         XDR xdrs;
101         char verf_body[MAX_AUTH_BYTES];
102         u_int sendsize;
103         u_int recvsize;
104         int maxrec;
105         bool_t nonblock;
106         struct timeval last_recv_time;
107 };
108
109 /*
110  * Usage:
111  *      xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
112  *
113  * Creates, registers, and returns a (rpc) tcp based transporter.
114  * Once *xprt is initialized, it is registered as a transporter
115  * see (svc.h, xprt_register).  This routine returns
116  * a NULL if a problem occurred.
117  *
118  * The filedescriptor passed in is expected to refer to a bound, but
119  * not yet connected socket.
120  *
121  * Since streams do buffered io similar to stdio, the caller can specify
122  * how big the send and receive buffers are via the second and third parms;
123  * 0 => use the system default.
124  */
125 SVCXPRT *
126 svc_vc_create(int fd, u_int sendsize, u_int recvsize)
127 {
128         SVCXPRT *xprt = NULL;
129         struct cf_rendezvous *r = NULL;
130         struct __rpc_sockinfo si;
131         struct sockaddr_storage sslocal;
132         socklen_t slen;
133
134         if (!__rpc_fd2sockinfo(fd, &si))
135                 return NULL;
136
137         r = mem_alloc(sizeof(*r));
138         if (r == NULL) {
139                 warnx("svc_vc_create: out of memory");
140                 goto cleanup_svc_vc_create;
141         }
142         r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
143         r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
144         r->maxrec = __svc_maxrec;
145         xprt = svc_xprt_alloc();
146         if (xprt == NULL) {
147                 warnx("svc_vc_create: out of memory");
148                 goto cleanup_svc_vc_create;
149         }
150         xprt->xp_p1 = r;
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 = svc_xprt_alloc();
250         if (xprt == NULL) {
251                 warnx("svc_vc: makefd_xprt: out of memory");
252                 goto done;
253         }
254         cd = mem_alloc(sizeof(struct cf_conn));
255         if (cd == NULL) {
256                 warnx("svc_tcp: makefd_xprt: out of memory");
257                 svc_xprt_free(xprt);
258                 xprt = NULL;
259                 goto done;
260         }
261         cd->strm_stat = XPRT_IDLE;
262         xdrrec_create(&(cd->xdrs), sendsize, recvsize,
263             xprt, read_vc, write_vc);
264         xprt->xp_p1 = cd;
265         xprt->xp_verf.oa_base = cd->verf_body;
266         svc_vc_ops(xprt);  /* truely deals with calls */
267         xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
268         xprt->xp_fd = fd;
269         if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
270                 xprt->xp_netid = strdup(netid);
271
272         xprt_register(xprt);
273 done:
274         return (xprt);
275 }
276
277 /*ARGSUSED*/
278 static bool_t
279 rendezvous_request(SVCXPRT *xprt, struct rpc_msg *msg)
280 {
281         int sock, flags;
282         struct cf_rendezvous *r;
283         struct cf_conn *cd;
284         struct sockaddr_storage addr;
285         socklen_t len;
286         struct __rpc_sockinfo si;
287         SVCXPRT *newxprt;
288         fd_set cleanfds;
289
290         assert(xprt != NULL);
291         assert(msg != NULL);
292
293         r = (struct cf_rendezvous *)xprt->xp_p1;
294 again:
295         len = sizeof addr;
296         if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
297             &len)) < 0) {
298                 if (errno == EINTR)
299                         goto again;
300                 /*
301                  * Clean out the most idle file descriptor when we're
302                  * running out.
303                  */
304                 if (errno == EMFILE || errno == ENFILE) {
305                         cleanfds = svc_fdset;
306                         __svc_clean_idle(&cleanfds, 0, FALSE);
307                         goto again;
308                 }
309                 return (FALSE);
310         }
311         /*
312          * make a new transporter (re-uses xprt)
313          */
314         newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
315         newxprt->xp_rtaddr.buf = mem_alloc(len);
316         if (newxprt->xp_rtaddr.buf == NULL)
317                 return (FALSE);
318         memcpy(newxprt->xp_rtaddr.buf, &addr, len);
319         newxprt->xp_rtaddr.len = len;
320 #ifdef PORTMAP
321         if (addr.ss_family == AF_INET || addr.ss_family == AF_LOCAL) {
322                 newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
323                 newxprt->xp_addrlen = sizeof (struct sockaddr_in);
324         }
325 #endif                          /* PORTMAP */
326         if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
327                 len = 1;
328                 /* XXX fvdl - is this useful? */
329                 _setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
330         }
331
332         cd = (struct cf_conn *)newxprt->xp_p1;
333
334         cd->recvsize = r->recvsize;
335         cd->sendsize = r->sendsize;
336         cd->maxrec = r->maxrec;
337
338         if (cd->maxrec != 0) {
339                 flags = _fcntl(sock, F_GETFL, 0);
340                 if (flags  == -1)
341                         return (FALSE);
342                 if (_fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
343                         return (FALSE);
344                 if (cd->recvsize > cd->maxrec)
345                         cd->recvsize = cd->maxrec;
346                 cd->nonblock = TRUE;
347                 __xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
348         } else
349                 cd->nonblock = FALSE;
350
351         gettimeofday(&cd->last_recv_time, NULL);
352
353         return (FALSE); /* there is never an rpc msg to be processed */
354 }
355
356 /*ARGSUSED*/
357 static enum xprt_stat
358 rendezvous_stat(SVCXPRT *xprt)
359 {
360
361         return (XPRT_IDLE);
362 }
363
364 static void
365 svc_vc_destroy(SVCXPRT *xprt)
366 {
367         assert(xprt != NULL);
368         
369         xprt_unregister(xprt);
370         __svc_vc_dodestroy(xprt);
371 }
372
373 static void
374 __svc_vc_dodestroy(SVCXPRT *xprt)
375 {
376         struct cf_conn *cd;
377         struct cf_rendezvous *r;
378
379         cd = (struct cf_conn *)xprt->xp_p1;
380
381         if (xprt->xp_fd != RPC_ANYFD)
382                 (void)_close(xprt->xp_fd);
383         if (xprt->xp_port != 0) {
384                 /* a rendezvouser socket */
385                 r = (struct cf_rendezvous *)xprt->xp_p1;
386                 mem_free(r, sizeof (struct cf_rendezvous));
387                 xprt->xp_port = 0;
388         } else {
389                 /* an actual connection socket */
390                 XDR_DESTROY(&(cd->xdrs));
391                 mem_free(cd, sizeof(struct cf_conn));
392         }
393         if (xprt->xp_rtaddr.buf)
394                 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
395         if (xprt->xp_ltaddr.buf)
396                 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
397         free(xprt->xp_tp);
398         free(xprt->xp_netid);
399         svc_xprt_free(xprt);
400 }
401
402 /*ARGSUSED*/
403 static bool_t
404 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in)
405 {
406         return (FALSE);
407 }
408
409 static bool_t
410 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
411 {
412         struct cf_rendezvous *cfp;
413
414         cfp = (struct cf_rendezvous *)xprt->xp_p1;
415         if (cfp == NULL)
416                 return (FALSE);
417         switch (rq) {
418                 case SVCGET_CONNMAXREC:
419                         *(int *)in = cfp->maxrec;
420                         break;
421                 case SVCSET_CONNMAXREC:
422                         cfp->maxrec = *(int *)in;
423                         break;
424                 default:
425                         return (FALSE);
426         }
427         return (TRUE);
428 }
429
430 /*
431  * reads data from the tcp or uip connection.
432  * any error is fatal and the connection is closed.
433  * (And a read of zero bytes is a half closed stream => error.)
434  * All read operations timeout after 35 seconds.  A timeout is
435  * fatal for the connection.
436  */
437 static int
438 read_vc(void *xprtp, void *buf, int len)
439 {
440         SVCXPRT *xprt;
441         int sock;
442         int milliseconds = 35 * 1000;
443         struct pollfd pollfd;
444         struct cf_conn *cfp;
445
446         xprt = (SVCXPRT *)xprtp;
447         assert(xprt != NULL);
448
449         sock = xprt->xp_fd;
450
451         cfp = (struct cf_conn *)xprt->xp_p1;
452
453         if (cfp->nonblock) {
454                 len = _read(sock, buf, (size_t)len);
455                 if (len < 0) {
456                         if (errno == EAGAIN)
457                                 len = 0;
458                         else
459                                 goto fatal_err;
460                 }
461                 if (len != 0)
462                         gettimeofday(&cfp->last_recv_time, NULL);
463                 return len;
464         }
465
466         do {
467                 pollfd.fd = sock;
468                 pollfd.events = POLLIN;
469                 pollfd.revents = 0;
470                 switch (_poll(&pollfd, 1, milliseconds)) {
471                 case -1:
472                         if (errno == EINTR)
473                                 continue;
474                         /*FALLTHROUGH*/
475                 case 0:
476                         goto fatal_err;
477
478                 default:
479                         break;
480                 }
481         } while ((pollfd.revents & POLLIN) == 0);
482
483         if ((len = _read(sock, buf, (size_t)len)) > 0) {
484                 gettimeofday(&cfp->last_recv_time, NULL);
485                 return (len);
486         }
487
488 fatal_err:
489         ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
490         return (-1);
491 }
492
493 /*
494  * writes data to the tcp connection.
495  * Any error is fatal and the connection is closed.
496  */
497 static int
498 write_vc(void *xprtp, void *buf, int len)
499 {
500         SVCXPRT *xprt;
501         int i, cnt;
502         struct cf_conn *cd;
503         struct timeval tv0, tv1;
504
505         xprt = (SVCXPRT *)xprtp;
506         assert(xprt != NULL);
507
508         cd = (struct cf_conn *)xprt->xp_p1;
509
510         if (cd->nonblock)
511                 gettimeofday(&tv0, NULL);
512         
513         for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
514                 i = _write(xprt->xp_fd, buf, (size_t)cnt);
515                 if (i  < 0) {
516                         if (errno != EAGAIN || !cd->nonblock) {
517                                 cd->strm_stat = XPRT_DIED;
518                                 return (-1);
519                         }
520                         if (cd->nonblock) {
521                                 /*
522                                  * For non-blocking connections, do not
523                                  * take more than 2 seconds writing the
524                                  * data out.
525                                  *
526                                  * XXX 2 is an arbitrary amount.
527                                  */
528                                 gettimeofday(&tv1, NULL);
529                                 if (tv1.tv_sec - tv0.tv_sec >= 2) {
530                                         cd->strm_stat = XPRT_DIED;
531                                         return (-1);
532                                 }
533                         }
534                         i = 0;
535                 }
536         }
537
538         return (len);
539 }
540
541 static enum xprt_stat
542 svc_vc_stat(SVCXPRT *xprt)
543 {
544         struct cf_conn *cd;
545
546         assert(xprt != NULL);
547
548         cd = (struct cf_conn *)(xprt->xp_p1);
549
550         if (cd->strm_stat == XPRT_DIED)
551                 return (XPRT_DIED);
552         if (! xdrrec_eof(&(cd->xdrs)))
553                 return (XPRT_MOREREQS);
554         return (XPRT_IDLE);
555 }
556
557 static bool_t
558 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg)
559 {
560         struct cf_conn *cd;
561         XDR *xdrs;
562
563         assert(xprt != NULL);
564         assert(msg != NULL);
565
566         cd = (struct cf_conn *)(xprt->xp_p1);
567         xdrs = &(cd->xdrs);
568
569         if (cd->nonblock) {
570                 if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
571                         return FALSE;
572         } else {
573                 (void)xdrrec_skiprecord(xdrs);
574         }
575
576         xdrs->x_op = XDR_DECODE;
577         if (xdr_callmsg(xdrs, msg)) {
578                 cd->x_id = msg->rm_xid;
579                 return (TRUE);
580         }
581         cd->strm_stat = XPRT_DIED;
582         return (FALSE);
583 }
584
585 static bool_t
586 svc_vc_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
587 {
588         struct cf_conn *cd;
589
590         assert(xprt != NULL);
591         cd = (struct cf_conn *)(xprt->xp_p1);
592         return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt),
593                 &cd->xdrs, xdr_args, args_ptr));
594 }
595
596 static bool_t
597 svc_vc_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
598 {
599         XDR *xdrs;
600
601         assert(xprt != NULL);
602         /* args_ptr may be NULL */
603
604         xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
605
606         xdrs->x_op = XDR_FREE;
607         return ((*xdr_args)(xdrs, args_ptr));
608 }
609
610 static bool_t
611 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg)
612 {
613         struct cf_conn *cd;
614         XDR *xdrs;
615         bool_t rstat;
616         xdrproc_t xdr_proc;
617         caddr_t xdr_where;
618         u_int pos;
619
620         assert(xprt != NULL);
621         assert(msg != NULL);
622
623         cd = (struct cf_conn *)(xprt->xp_p1);
624         xdrs = &(cd->xdrs);
625
626         xdrs->x_op = XDR_ENCODE;
627         msg->rm_xid = cd->x_id;
628         rstat = TRUE;
629         if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
630             msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
631                 xdr_proc = msg->acpted_rply.ar_results.proc;
632                 xdr_where = msg->acpted_rply.ar_results.where;
633                 msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
634                 msg->acpted_rply.ar_results.where = NULL;
635
636                 pos = XDR_GETPOS(xdrs);
637                 if (!xdr_replymsg(xdrs, msg) ||
638                     !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where)) {
639                         XDR_SETPOS(xdrs, pos);
640                         rstat = FALSE;
641                 }
642         } else {
643                 rstat = xdr_replymsg(xdrs, msg);
644         }
645
646         if (rstat)
647                 (void)xdrrec_endofrecord(xdrs, TRUE);
648
649         return (rstat);
650 }
651
652 static void
653 svc_vc_ops(SVCXPRT *xprt)
654 {
655         static struct xp_ops ops;
656         static struct xp_ops2 ops2;
657
658 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
659
660         mutex_lock(&ops_lock);
661         if (ops.xp_recv == NULL) {
662                 ops.xp_recv = svc_vc_recv;
663                 ops.xp_stat = svc_vc_stat;
664                 ops.xp_getargs = svc_vc_getargs;
665                 ops.xp_reply = svc_vc_reply;
666                 ops.xp_freeargs = svc_vc_freeargs;
667                 ops.xp_destroy = svc_vc_destroy;
668                 ops2.xp_control = svc_vc_control;
669         }
670         xprt->xp_ops = &ops;
671         xprt->xp_ops2 = &ops2;
672         mutex_unlock(&ops_lock);
673 }
674
675 static void
676 svc_vc_rendezvous_ops(SVCXPRT *xprt)
677 {
678         static struct xp_ops ops;
679         static struct xp_ops2 ops2;
680
681         mutex_lock(&ops_lock);
682         if (ops.xp_recv == NULL) {
683                 ops.xp_recv = rendezvous_request;
684                 ops.xp_stat = rendezvous_stat;
685                 ops.xp_getargs =
686                     (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
687                 ops.xp_reply =
688                     (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
689                 ops.xp_freeargs =
690                     (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
691                 ops.xp_destroy = svc_vc_destroy;
692                 ops2.xp_control = svc_vc_rendezvous_control;
693         }
694         xprt->xp_ops = &ops;
695         xprt->xp_ops2 = &ops2;
696         mutex_unlock(&ops_lock);
697 }
698
699 /*
700  * Get the effective UID of the sending process. Used by rpcbind, keyserv
701  * and rpc.yppasswdd on AF_LOCAL.
702  */
703 int
704 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
705         int sock, ret;
706         gid_t egid;
707         uid_t euid;
708         struct sockaddr *sa;
709
710         sock = transp->xp_fd;
711         sa = (struct sockaddr *)transp->xp_rtaddr.buf;
712         if (sa->sa_family == AF_LOCAL) {
713                 ret = getpeereid(sock, &euid, &egid);
714                 if (ret == 0)
715                         *uid = euid;
716                 return (ret);
717         } else
718                 return (-1);
719 }
720
721 /*
722  * Destroy xprts that have not have had any activity in 'timeout' seconds.
723  * If 'cleanblock' is true, blocking connections (the default) are also
724  * cleaned. If timeout is 0, the least active connection is picked.
725  */
726 bool_t
727 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
728 {
729         int i, ncleaned;
730         SVCXPRT *xprt, *least_active;
731         struct timeval tv, tdiff, tmax;
732         struct cf_conn *cd;
733
734         gettimeofday(&tv, NULL);
735         tmax.tv_sec = tmax.tv_usec = 0;
736         least_active = NULL;
737         rwlock_wrlock(&svc_fd_lock);
738         for (i = ncleaned = 0; i <= svc_maxfd; i++) {
739                 if (FD_ISSET(i, fds)) {
740                         xprt = __svc_xports[i];
741                         if (xprt == NULL || xprt->xp_ops == NULL ||
742                             xprt->xp_ops->xp_recv != svc_vc_recv)
743                                 continue;
744                         cd = (struct cf_conn *)xprt->xp_p1;
745                         if (!cleanblock && !cd->nonblock)
746                                 continue;
747                         if (timeout == 0) {
748                                 timersub(&tv, &cd->last_recv_time, &tdiff);
749                                 if (timercmp(&tdiff, &tmax, >)) {
750                                         tmax = tdiff;
751                                         least_active = xprt;
752                                 }
753                                 continue;
754                         }
755                         if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
756                                 __xprt_unregister_unlocked(xprt);
757                                 __svc_vc_dodestroy(xprt);
758                                 ncleaned++;
759                         }
760                 }
761         }
762         if (timeout == 0 && least_active != NULL) {
763                 __xprt_unregister_unlocked(least_active);
764                 __svc_vc_dodestroy(least_active);
765                 ncleaned++;
766         }
767         rwlock_unlock(&svc_fd_lock);
768         return ncleaned > 0 ? TRUE : FALSE;
769 }