source: rtems-libbsd/freebsd/lib/libc/rpc/svc_vc.c @ f41a394

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was 60b1d40, checked in by Sebastian Huber <sebastian.huber@…>, on 06/09/16 at 08:23:57

RPC(3): Import from FreeBSD

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