Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/svc_vc.c

raw symbols SCIP
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
31 /*
32 * svc_vc.c, Server side for Connection Oriented based RPC.
33 *
34 * Actually implements two flavors of transporter -
35 * a tcp rendezvouser (a listner and connection establisher)
36 * and a record/tcp stream.
37 */
38 #include <pthread.h>
39 #include <reentrant.h>
40 #include <sys/socket.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <poll.h>
44 #include <sys/un.h>
45 #include <sys/time.h>
46 #include <sys/uio.h>
47 #include <netinet/in.h>
48 #include <netinet/tcp.h>
49
50 #include <assert.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include <rpc/rpc.h>
60
61 #include "rpc_com.h"
62
63 #include <getpeereid.h>
64
65
66 extern rwlock_t svc_fd_lock;
67 extern SVCXPRT **__svc_xports;
68 extern int svc_open_fds();
69
70 static SVCXPRT *makefd_xprt(int, u_int, u_int);
71 static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
72 static enum xprt_stat rendezvous_stat(SVCXPRT *);
73 static void svc_vc_destroy(SVCXPRT *);
74 static void __svc_vc_dodestroy (SVCXPRT *);
75 static int read_vc(void *, void *, int);
76 static int write_vc(void *, void *, int);
77 static enum xprt_stat svc_vc_stat(SVCXPRT *);
78 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *);
79 static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
80 static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
81 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *);
82 static void svc_vc_rendezvous_ops(SVCXPRT *);
83 static void svc_vc_ops(SVCXPRT *);
84 static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
85 static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
86 void *in);
87 static int __svc_destroy_idle(int timeout);
88
89 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
90 u_int sendsize;
91 u_int recvsize;
92 int maxrec;
93 };
94
95 struct cf_conn { /* kept in xprt->xp_p1 for actual connection */
96 enum xprt_stat strm_stat;
97 u_int32_t x_id;
98 XDR xdrs;
99 char verf_body[MAX_AUTH_BYTES];
100 u_int sendsize;
101 u_int recvsize;
102 int maxrec;
103 bool_t nonblock;
104 struct timeval last_recv_time;
105 };
106
107 /*
108 * This is used to set xprt->xp_raddr in a way legacy
109 * apps can deal with
110 */
111 void
112 __xprt_set_raddr(SVCXPRT *xprt, const struct sockaddr_storage *ss)
113 {
114 switch (ss->ss_family) {
115 case AF_INET6:
116 memcpy(&xprt->xp_raddr, ss, sizeof(struct sockaddr_in6));
117 xprt->xp_addrlen = sizeof (struct sockaddr_in6);
118 break;
119 case AF_INET:
120 memcpy(&xprt->xp_raddr, ss, sizeof(struct sockaddr_in));
121 xprt->xp_addrlen = sizeof (struct sockaddr_in);
122 break;
123 default:
124 xprt->xp_raddr.sin6_family = AF_UNSPEC;
125 xprt->xp_addrlen = sizeof (struct sockaddr);
126 break;
127 }
128 }
129
130 /*
131 * Usage:
132 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
133 *
134 * Creates, registers, and returns a (rpc) tcp based transporter.
135 * Once *xprt is initialized, it is registered as a transporter
136 * see (svc.h, xprt_register). This routine returns
137 * a NULL if a problem occurred.
138 *
139 * The filedescriptor passed in is expected to refer to a bound, but
140 * not yet connected socket.
141 *
142 * Since streams do buffered io similar to stdio, the caller can specify
143 * how big the send and receive buffers are via the second and third parms;
144 * 0 => use the system default.
145 */
146 SVCXPRT *
147 svc_vc_create(fd, sendsize, recvsize)
148 int fd;
149 u_int sendsize;
150 u_int recvsize;
151 {
152 SVCXPRT *xprt = NULL;
153 SVCXPRT_EXT *ext = NULL;
154 struct cf_rendezvous *r = NULL;
155 struct __rpc_sockinfo si;
156 struct sockaddr_storage sslocal;
157 socklen_t slen;
158
159 r = mem_alloc(sizeof(*r));
160 if (r == NULL) {
161 warnx("svc_vc_create: out of memory");
162 goto cleanup_svc_vc_create;
163 }
164 if (!__rpc_fd2sockinfo(fd, &si)) {
165 warnx("svc_vc_create: __rpc_fd2sockinfo failed");
166 goto cleanup_svc_vc_create;
167 }
168 r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
169 r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
170 r->maxrec = __svc_maxrec;
171 xprt = mem_alloc(sizeof(SVCXPRT));
172 if (xprt == NULL) {
173 warnx("svc_vc_create: out of memory");
174 goto cleanup_svc_vc_create;
175 }
176 ext = mem_alloc(sizeof (*ext));
177 if (ext == NULL) {
178 warnx("svc_vc_create: out of memory");
179 goto cleanup_svc_vc_create;
180 }
181 memset(ext, 0, sizeof (*ext));
182 xprt->xp_tp = NULL;
183 xprt->xp_p1 = r;
184 xprt->xp_p2 = NULL;
185 xprt->xp_p3 = ext;
186 xprt->xp_verf = _null_auth;
187 svc_vc_rendezvous_ops(xprt);
188 xprt->xp_port = (u_short)-1; /* It is the rendezvouser */
189 xprt->xp_fd = fd;
190
191 slen = sizeof (struct sockaddr_storage);
192 if (getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
193 warnx("svc_vc_create: could not retrieve local addr");
194 goto cleanup_svc_vc_create;
195 }
196
197 if (!__rpc_set_netbuf(&xprt->xp_ltaddr, &sslocal, sizeof(sslocal))) {
198 warnx("svc_vc_create: no mem for local addr");
199 goto cleanup_svc_vc_create;
200 }
201 xprt_register(xprt);
202 return (xprt);
203 cleanup_svc_vc_create:
204 if (r != NULL)
205 mem_free(r, sizeof(*r));
206 if (xprt != NULL)
207 mem_free(xprt, sizeof(*xprt));
208 if (ext != NULL)
209 mem_free(ext, sizeof(*ext));
210 return (NULL);
211 }
212
213 /*
214 * Like svtcp_create(), except the routine takes any *open* UNIX file
215 * descriptor as its first input.
216 */
217 SVCXPRT *
218 svc_fd_create(fd, sendsize, recvsize)
219 int fd;
220 u_int sendsize;
221 u_int recvsize;
222 {
223 struct sockaddr_storage ss;
224 socklen_t slen;
225 SVCXPRT *ret;
226
227 assert(fd != -1);
228
229 ret = makefd_xprt(fd, sendsize, recvsize);
230 if (ret == NULL)
231 return NULL;
232
233 slen = sizeof (struct sockaddr_storage);
234 if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
235 warnx("svc_fd_create: could not retrieve local addr");
236 goto freedata;
237 }
238 if (!__rpc_set_netbuf(&ret->xp_ltaddr, &ss, sizeof(ss))) {
239 warnx("svc_fd_create: no mem for local addr");
240 goto freedata;
241 }
242
243 slen = sizeof (struct sockaddr_storage);
244 if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
245 warnx("svc_fd_create: could not retrieve remote addr");
246 goto freedata;
247 }
248 if (!__rpc_set_netbuf(&ret->xp_rtaddr, &ss, sizeof(ss))) {
249 warnx("svc_fd_create: no mem for remote addr");
250 goto freedata;
251 }
252
253 /* Set xp_raddr for compatibility */
254 __xprt_set_raddr(ret, &ss);
255
256 return ret;
257
258 freedata:
259 if (ret->xp_ltaddr.buf != NULL) {
260 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
261 ret->xp_ltaddr.buf = NULL;
262 }
263 return NULL;
264 }
265
266 static SVCXPRT *
267 makefd_xprt(fd, sendsize, recvsize)
268 int fd;
269 u_int sendsize;
270 u_int recvsize;
271 {
272 SVCXPRT *xprt;
273 SVCXPRT_EXT *ext;
274 struct cf_conn *cd;
275 const char *netid;
276 struct __rpc_sockinfo si;
277
278 assert(fd != -1);
279
280 xprt = mem_alloc(sizeof(SVCXPRT));
281 if (xprt == NULL) {
282 warnx("svc_vc: makefd_xprt: out of memory");
283 goto done;
284 }
285 memset(xprt, 0, sizeof *xprt);
286 ext = mem_alloc(sizeof (*ext));
287 if (ext == NULL) {
288 warnx("svc_vc: makefd_xprt: out of memory");
289 mem_free(xprt, sizeof(SVCXPRT));
290 xprt = NULL;
291 goto done;
292 }
293 memset(ext, 0, sizeof (*ext));
294 cd = mem_alloc(sizeof(struct cf_conn));
295 if (cd == NULL) {
296 warnx("svc_vc: makefd_xprt: out of memory");
297 mem_free(ext, sizeof(*ext));
298 mem_free(xprt, sizeof(SVCXPRT));
299 xprt = NULL;
300 goto done;
301 }
302 cd->strm_stat = XPRT_IDLE;
303 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
304 xprt, read_vc, write_vc);
305 xprt->xp_p1 = cd;
306 xprt->xp_p3 = ext;
307 xprt->xp_verf.oa_base = cd->verf_body;
308 svc_vc_ops(xprt); /* truely deals with calls */
309 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
310 xprt->xp_fd = fd;
311 if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
312 xprt->xp_netid = strdup(netid);
313
314 xprt_register(xprt);
315 done:
316 return (xprt);
317 }
318
319
320 /*ARGSUSED*/
321 static bool_t
322 rendezvous_request(xprt, msg)
323 SVCXPRT *xprt;
324 struct rpc_msg *msg;
325 {
326 int sock, flags, nfds, cnt;
327 struct cf_rendezvous *r;
328 struct cf_conn *cd;
329 struct sockaddr_storage addr;
330 socklen_t len;
331 struct __rpc_sockinfo si;
332 SVCXPRT *newxprt;
333
334 assert(xprt != NULL);
335 assert(msg != NULL);
336
337 r = (struct cf_rendezvous *)xprt->xp_p1;
338 again:
339 len = sizeof addr;
340 sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr, &len);
341 if (sock < 0) {
342 if (errno == EINTR)
343 goto again;
344 return (FALSE);
345 }
346 /*
347 * make a new transporter (re-uses xprt)
348 */
349
350 newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
351 if (!newxprt)
352 return (FALSE);
353
354 if (!__rpc_set_netbuf(&newxprt->xp_rtaddr, &addr, len))
355 return (FALSE);
356
357 __xprt_set_raddr(newxprt, &addr);
358
359 if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
360 len = 1;
361 /* XXX fvdl - is this useful? */
362 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
363 }
364
365 cd = (struct cf_conn *)newxprt->xp_p1;
366
367 cd->recvsize = r->recvsize;
368 cd->sendsize = r->sendsize;
369 cd->maxrec = r->maxrec;
370
371 if (cd->maxrec != 0) {
372 flags = fcntl(sock, F_GETFL, 0);
373 if (flags == -1)
374 return (FALSE);
375 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
376 return (FALSE);
377 if (cd->recvsize > cd->maxrec)
378 cd->recvsize = cd->maxrec;
379 cd->nonblock = TRUE;
380 __xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
381 } else
382 cd->nonblock = FALSE;
383
384 gettimeofday(&cd->last_recv_time, NULL);
385
386 nfds = svc_open_fds();
387 if (nfds >= (_rpc_dtablesize() / 5) * 4) {
388 /* destroy idle connections */
389 cnt = __svc_destroy_idle(15);
390 if (cnt == 0) {
391 /* destroy least active */
392 __svc_destroy_idle(0);
393 }
394 }
395
396 return (FALSE); /* there is never an rpc msg to be processed */
397 }
398
399 /*ARGSUSED*/
400 static enum xprt_stat
401 rendezvous_stat(xprt)
402 SVCXPRT *xprt;
403 {
404
405 return (XPRT_IDLE);
406 }
407
408 static void
409 svc_vc_destroy(xprt)
410 SVCXPRT *xprt;
411 {
412 assert(xprt != NULL);
413
414 xprt_unregister(xprt);
415 __svc_vc_dodestroy(xprt);
416 }
417
418 static bool_t
419 __svc_rendezvous_socket(xprt)
420 SVCXPRT *xprt;
421 {
422 return (xprt->xp_ops->xp_recv == rendezvous_request);
423 }
424 static void
425 __svc_vc_dodestroy(xprt)
426 SVCXPRT *xprt;
427 {
428 SVCXPRT_EXT *ext = SVCEXT(xprt);
429 struct cf_conn *cd;
430 struct cf_rendezvous *r;
431
432 cd = (struct cf_conn *)xprt->xp_p1;
433
434 if (xprt->xp_fd != RPC_ANYFD)
435 (void)close(xprt->xp_fd);
436 if (__svc_rendezvous_socket(xprt)) {
437 /* a rendezvouser socket */
438 r = (struct cf_rendezvous *)xprt->xp_p1;
439 mem_free(r, sizeof (struct cf_rendezvous));
440 xprt->xp_port = 0;
441 } else {
442 /* an actual connection socket */
443 XDR_DESTROY(&(cd->xdrs));
444 mem_free(cd, sizeof(struct cf_conn));
445 }
446 if (ext)
447 mem_free(ext, sizeof (*ext));
448 if (xprt->xp_rtaddr.buf)
449 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
450 if (xprt->xp_ltaddr.buf)
451 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
452 if (xprt->xp_tp)
453 free(xprt->xp_tp);
454 if (xprt->xp_netid)
455 free(xprt->xp_netid);
456 mem_free(xprt, sizeof(SVCXPRT));
457 }
458
459 /*ARGSUSED*/
460 static bool_t
461 svc_vc_control(xprt, rq, in)
462 SVCXPRT *xprt;
463 const u_int rq;
464 void *in;
465 {
466 return (FALSE);
467 }
468
469 static bool_t
470 svc_vc_rendezvous_control(xprt, rq, in)
471 SVCXPRT *xprt;
472 const u_int rq;
473 void *in;
474 {
475 struct cf_rendezvous *cfp;
476
477 cfp = (struct cf_rendezvous *)xprt->xp_p1;
478 if (cfp == NULL)
479 return (FALSE);
480 switch (rq) {
481 case SVCGET_CONNMAXREC:
482 *(int *)in = cfp->maxrec;
483 break;
484 case SVCSET_CONNMAXREC:
485 cfp->maxrec = *(int *)in;
486 break;
487 default:
488 return (FALSE);
489 }
490 return (TRUE);
491 }
492
493 /*
494 * reads data from the tcp or uip connection.
495 * any error is fatal and the connection is closed.
496 * (And a read of zero bytes is a half closed stream => error.)
497 * All read operations timeout after 35 seconds. A timeout is
498 * fatal for the connection.
499 */
500 static int
501 read_vc(xprtp, buf, len)
502 void *xprtp;
503 void *buf;
504 int len;
505 {
506 SVCXPRT *xprt;
507 int sock;
508 int milliseconds = 35 * 1000;
509 struct pollfd pollfd;
510 struct cf_conn *cfp;
511
512 xprt = (SVCXPRT *)xprtp;
513 assert(xprt != NULL);
514
515 sock = xprt->xp_fd;
516
517 cfp = (struct cf_conn *)xprt->xp_p1;
518
519 if (cfp->nonblock) {
520 /* Since len == 0 is returned on zero length
521 * read or EOF errno needs to be reset before
522 * the read
523 */
524 errno = 0;
525 len = read(sock, buf, (size_t)len);
526 if (len < 0) {
527 if (errno == EAGAIN || errno == EWOULDBLOCK)
528 len = 0;
529 else
530 goto fatal_err;
531 }
532 if (len != 0)
533 gettimeofday(&cfp->last_recv_time, NULL);
534 return len;
535 }
536
537 do {
538 pollfd.fd = sock;
539 pollfd.events = POLLIN;
540 pollfd.revents = 0;
541 switch (poll(&pollfd, 1, milliseconds)) {
542 case -1:
543 if (errno == EINTR)
544 continue;
545 /*FALLTHROUGH*/
546 case 0:
547 goto fatal_err;
548
549 default:
550 break;
551 }
552 } while ((pollfd.revents & POLLIN) == 0);
553
554 if ((len = read(sock, buf, (size_t)len)) > 0) {
555 gettimeofday(&cfp->last_recv_time, NULL);
556 return (len);
557 }
558
559 fatal_err:
560 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
561 return (-1);
562 }
563
564 /*
565 * writes data to the tcp connection.
566 * Any error is fatal and the connection is closed.
567 */
568 static int
569 write_vc(xprtp, buf, len)
570 void *xprtp;
571 void *buf;
572 int len;
573 {
574 SVCXPRT *xprt;
575 int i, cnt;
576 struct cf_conn *cd;
577 struct timeval tv0, tv1;
578
579 xprt = (SVCXPRT *)xprtp;
580 assert(xprt != NULL);
581
582 cd = (struct cf_conn *)xprt->xp_p1;
583
584 if (cd->nonblock)
585 gettimeofday(&tv0, NULL);
586
587 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
588 i = write(xprt->xp_fd, buf, (size_t)cnt);
589 if (i < 0) {
590 if (errno != EAGAIN || !cd->nonblock) {
591 cd->strm_stat = XPRT_DIED;
592 return (-1);
593 }
594 /*
595 * For non-blocking connections, do not
596 * take more than 2 seconds writing the
597 * data out.
598 *
599 * XXX 2 is an arbitrary amount.
600 */
601 gettimeofday(&tv1, NULL);
602 if (tv1.tv_sec - tv0.tv_sec >= 2) {
603 cd->strm_stat = XPRT_DIED;
604 return (-1);
605 }
606 i = 0; /* Don't change buf and cnt */
607 }
608 }
609
610 return (len);
611 }
612
613 static enum xprt_stat
614 svc_vc_stat(xprt)
615 SVCXPRT *xprt;
616 {
617 struct cf_conn *cd;
618
619 assert(xprt != NULL);
620
621 cd = (struct cf_conn *)(xprt->xp_p1);
622
623 if (cd->strm_stat == XPRT_DIED)
624 return (XPRT_DIED);
625 if (! xdrrec_eof(&(cd->xdrs)))
626 return (XPRT_MOREREQS);
627 return (XPRT_IDLE);
628 }
629
630 static bool_t
631 svc_vc_recv(xprt, msg)
632 SVCXPRT *xprt;
633 struct rpc_msg *msg;
634 {
635 struct cf_conn *cd;
636 XDR *xdrs;
637
638 assert(xprt != NULL);
639 assert(msg != NULL);
640
641 cd = (struct cf_conn *)(xprt->xp_p1);
642 xdrs = &(cd->xdrs);
643
644 if (cd->nonblock) {
645 if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
646 return FALSE;
647 }
648
649 xdrs->x_op = XDR_DECODE;
650 /*
651 * No need skip records with nonblocking connections
652 */
653 if (cd->nonblock == FALSE)
654 (void)xdrrec_skiprecord(xdrs);
655 if (xdr_callmsg(xdrs, msg)) {
656 cd->x_id = msg->rm_xid;
657 return (TRUE);
658 }
659 cd->strm_stat = XPRT_DIED;
660 return (FALSE);
661 }
662
663 static bool_t
664 svc_vc_getargs(xprt, xdr_args, args_ptr)
665 SVCXPRT *xprt;
666 xdrproc_t xdr_args;
667 void *args_ptr;
668 {
669
670 assert(xprt != NULL);
671 /* args_ptr may be NULL */
672
673 if (!SVCAUTH_UNWRAP(&SVC_XP_AUTH(xprt),
674 &(((struct cf_conn *)(xprt->xp_p1))->xdrs),
675 xdr_args, args_ptr)) {
676 return FALSE;
677 }
678 return TRUE;
679 }
680
681 static bool_t
682 svc_vc_freeargs(xprt, xdr_args, args_ptr)
683 SVCXPRT *xprt;
684 xdrproc_t xdr_args;
685 void *args_ptr;
686 {
687 XDR *xdrs;
688
689 assert(xprt != NULL);
690 /* args_ptr may be NULL */
691
692 xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
693
694 xdrs->x_op = XDR_FREE;
695 return ((*xdr_args)(xdrs, args_ptr));
696 }
697
698 static bool_t
699 svc_vc_reply(xprt, msg)
700 SVCXPRT *xprt;
701 struct rpc_msg *msg;
702 {
703 struct cf_conn *cd;
704 XDR *xdrs;
705 bool_t rstat;
706
707 xdrproc_t xdr_results;
708 caddr_t xdr_location;
709 bool_t has_args;
710
711 assert(xprt != NULL);
712 assert(msg != NULL);
713
714 cd = (struct cf_conn *)(xprt->xp_p1);
715 xdrs = &(cd->xdrs);
716
717 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
718 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
719 has_args = TRUE;
720 xdr_results = msg->acpted_rply.ar_results.proc;
721 xdr_location = msg->acpted_rply.ar_results.where;
722
723 msg->acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
724 msg->acpted_rply.ar_results.where = NULL;
725 } else
726 has_args = FALSE;
727
728 xdrs->x_op = XDR_ENCODE;
729 msg->rm_xid = cd->x_id;
730 rstat = FALSE;
731 if (xdr_replymsg(xdrs, msg) &&
732 (!has_args ||
733 SVCAUTH_WRAP(&SVC_XP_AUTH(xprt),
734 xdrs, xdr_results, xdr_location))) {
735 rstat = TRUE;
736 }
737 (void)xdrrec_endofrecord(xdrs, TRUE);
738 return (rstat);
739 }
740
741 static void
742 svc_vc_ops(xprt)
743 SVCXPRT *xprt;
744 {
745 static struct xp_ops ops;
746 static struct xp_ops2 ops2;
747 extern mutex_t ops_lock;
748
749 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
750
751 mutex_lock(&ops_lock);
752 if (ops.xp_recv == NULL) {
753 ops.xp_recv = svc_vc_recv;
754 ops.xp_stat = svc_vc_stat;
755 ops.xp_getargs = svc_vc_getargs;
756 ops.xp_reply = svc_vc_reply;
757 ops.xp_freeargs = svc_vc_freeargs;
758 ops.xp_destroy = svc_vc_destroy;
759 ops2.xp_control = svc_vc_control;
760 }
761 xprt->xp_ops = &ops;
762 xprt->xp_ops2 = &ops2;
763 mutex_unlock(&ops_lock);
764 }
765
766 static void
767 svc_vc_rendezvous_ops(xprt)
768 SVCXPRT *xprt;
769 {
770 static struct xp_ops ops;
771 static struct xp_ops2 ops2;
772 extern mutex_t ops_lock;
773
774 mutex_lock(&ops_lock);
775 if (ops.xp_recv == NULL) {
776 ops.xp_recv = rendezvous_request;
777 ops.xp_stat = rendezvous_stat;
778 ops.xp_getargs =
779 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
780 ops.xp_reply =
781 (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
782 ops.xp_freeargs =
783 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
784 ops.xp_destroy = svc_vc_destroy;
785 ops2.xp_control = svc_vc_rendezvous_control;
786 }
787 xprt->xp_ops = &ops;
788 xprt->xp_ops2 = &ops2;
789 mutex_unlock(&ops_lock);
790 }
791
792 /*
793 * Get the effective UID of the sending process. Used by rpcbind, keyserv
794 * and rpc.yppasswdd on AF_LOCAL.
795 */
796 int
797 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
798 int sock, ret;
799 gid_t egid;
800 uid_t euid;
801 struct sockaddr *sa;
802
803 sock = transp->xp_fd;
804 sa = (struct sockaddr *)transp->xp_rtaddr.buf;
805 if (sa->sa_family == AF_LOCAL) {
806 ret = getpeereid(sock, &euid, &egid);
807 if (ret == 0)
808 *uid = euid;
809 return (ret);
810 } else
811 return (-1);
812 }
813
814 /*
815 * Destroy xprts that have not have had any activity in 'timeout' seconds.
816 * If 'cleanblock' is true, blocking connections (the default) are also
817 * cleaned. If timeout is 0, the least active connection is picked.
818 *
819 * Though this is not a publicly documented interface, some versions of
820 * rpcbind are known to call this function. Do not alter or remove this
821 * API without changing the library's sonum.
822 */
823 /* Since this is an exported interface used by rpcbind, we cannot
824 remove it. But since poll() can handle much more and much higher
825 file descriptors, this code doesn't really work anymore, too.
826 So for now, keep it as dummy function and do nothing to not break
827 existing binaries. If we have ported rpcbind to the poll() interface
828 and find out, that we really need this cleanup stuff (but nobody
829 besides FreeBSD has this), we need to re-implement it using poll().
830 But this means a new function name with different parameters. For
831 ABI/API compatibility, we cannot reuse this one. */
832 bool_t
833 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
834 {
835 return FALSE;
836 }
837
838 static int
839 __svc_destroy_idle(int timeout)
840 {
841 int i, ncleaned = 0;
842 SVCXPRT *xprt, *least_active;
843 struct timeval tv, tdiff, tmax;
844 struct cf_conn *cd;
845
846 gettimeofday(&tv, NULL);
847 tmax.tv_sec = tmax.tv_usec = 0;
848 least_active = NULL;
849 rwlock_wrlock(&svc_fd_lock);
850
851 for (i = 0; i <= svc_max_pollfd; i++) {
852 if (svc_pollfd[i].fd == -1)
853 continue;
854 xprt = __svc_xports[i];
855 if (xprt == NULL || xprt->xp_ops == NULL ||
856 xprt->xp_ops->xp_recv != svc_vc_recv)
857 continue;
858 cd = (struct cf_conn *)xprt->xp_p1;
859 if (!cd->nonblock)
860 continue;
861 if (timeout == 0) {
862 timersub(&tv, &cd->last_recv_time, &tdiff);
863 if (timercmp(&tdiff, &tmax, >)) {
864 tmax = tdiff;
865 least_active = xprt;
866 }
867 continue;
868 }
869 if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
870 __xprt_unregister_unlocked(xprt);
871 __svc_vc_dodestroy(xprt);
872 ncleaned++;
873 }
874 }
875 if (timeout == 0 && least_active != NULL) {
876 __xprt_unregister_unlocked(least_active);
877 __svc_vc_dodestroy(least_active);
878 ncleaned++;
879 }
880 rwlock_unlock(&svc_fd_lock);
881 return (ncleaned);
882 }