Deleted Added
sdiff udiff text old ( 156090 ) new ( 162196 )
full compact
1/* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32#if defined(LIBC_SCCS) && !defined(lint)
33static char *sccsid2 = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
34static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC";
35#endif
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/lib/libc/rpc/svc_vc.c 156090 2006-02-27 22:10:59Z deischen $");
38
39/*
40 * svc_vc.c, Server side for Connection Oriented based RPC.
41 *
42 * Actually implements two flavors of transporter -
43 * a tcp rendezvouser (a listner and connection establisher)
44 * and a record/tcp stream.
45 */
46
47#include "namespace.h"
48#include "reentrant.h"
49#include <sys/types.h>
50#include <sys/param.h>
51#include <sys/poll.h>
52#include <sys/socket.h>
53#include <sys/un.h>
54#include <sys/time.h>
55#include <sys/uio.h>
56#include <netinet/in.h>
57#include <netinet/tcp.h>
58
59#include <assert.h>
60#include <err.h>
61#include <errno.h>
62#include <fcntl.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <unistd.h>
67
68#include <rpc/rpc.h>
69
70#include "rpc_com.h"
71#include "mt_misc.h"
72#include "un-namespace.h"
73
74static SVCXPRT *makefd_xprt(int, u_int, u_int);
75static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
76static enum xprt_stat rendezvous_stat(SVCXPRT *);
77static void svc_vc_destroy(SVCXPRT *);
78static void __svc_vc_dodestroy (SVCXPRT *);
79static int read_vc(void *, void *, int);
80static int write_vc(void *, void *, int);
81static enum xprt_stat svc_vc_stat(SVCXPRT *);
82static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *);
83static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
84static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
85static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *);
86static void svc_vc_rendezvous_ops(SVCXPRT *);
87static void svc_vc_ops(SVCXPRT *);
88static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
89static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
90 void *in);
91
92struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
93 u_int sendsize;
94 u_int recvsize;
95 int maxrec;
96};
97
98struct cf_conn { /* kept in xprt->xp_p1 for actual connection */
99 enum xprt_stat strm_stat;
100 u_int32_t x_id;
101 XDR xdrs;
102 char verf_body[MAX_AUTH_BYTES];
103 u_int sendsize;
104 u_int recvsize;
105 int maxrec;
106 bool_t nonblock;
107 struct timeval last_recv_time;
108};
109
110/*
111 * Usage:
112 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
113 *
114 * Creates, registers, and returns a (rpc) tcp based transporter.
115 * Once *xprt is initialized, it is registered as a transporter
116 * see (svc.h, xprt_register). This routine returns
117 * a NULL if a problem occurred.
118 *
119 * The filedescriptor passed in is expected to refer to a bound, but
120 * not yet connected socket.
121 *
122 * Since streams do buffered io similar to stdio, the caller can specify
123 * how big the send and receive buffers are via the second and third parms;
124 * 0 => use the system default.
125 */
126SVCXPRT *
127svc_vc_create(fd, sendsize, recvsize)
128 int fd;
129 u_int sendsize;
130 u_int recvsize;
131{
132 SVCXPRT *xprt;
133 struct cf_rendezvous *r = NULL;
134 struct __rpc_sockinfo si;
135 struct sockaddr_storage sslocal;
136 socklen_t slen;
137
138 r = mem_alloc(sizeof(*r));
139 if (r == NULL) {
140 warnx("svc_vc_create: out of memory");
141 goto cleanup_svc_vc_create;
142 }
143 if (!__rpc_fd2sockinfo(fd, &si))
144 return NULL;
145 r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
146 r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
147 r->maxrec = __svc_maxrec;
148 xprt = mem_alloc(sizeof(SVCXPRT));
149 if (xprt == NULL) {
150 warnx("svc_vc_create: out of memory");
151 goto cleanup_svc_vc_create;
152 }
153 xprt->xp_tp = NULL;
154 xprt->xp_p1 = r;
155 xprt->xp_p2 = NULL;
156 xprt->xp_p3 = NULL;
157 xprt->xp_verf = _null_auth;
158 svc_vc_rendezvous_ops(xprt);
159 xprt->xp_port = (u_short)-1; /* It is the rendezvouser */
160 xprt->xp_fd = fd;
161
162 slen = sizeof (struct sockaddr_storage);
163 if (_getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
164 warnx("svc_vc_create: could not retrieve local addr");
165 goto cleanup_svc_vc_create;
166 }
167
168 xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
169 xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
170 if (xprt->xp_ltaddr.buf == NULL) {
171 warnx("svc_vc_create: no mem for local addr");
172 goto cleanup_svc_vc_create;
173 }
174 memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
175
176 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
177 xprt_register(xprt);
178 return (xprt);
179cleanup_svc_vc_create:
180 if (r != NULL)
181 mem_free(r, sizeof(*r));
182 return (NULL);
183}
184
185/*
186 * Like svtcp_create(), except the routine takes any *open* UNIX file
187 * descriptor as its first input.
188 */
189SVCXPRT *
190svc_fd_create(fd, sendsize, recvsize)
191 int fd;
192 u_int sendsize;
193 u_int recvsize;
194{
195 struct sockaddr_storage ss;
196 socklen_t slen;
197 SVCXPRT *ret;
198
199 assert(fd != -1);
200
201 ret = makefd_xprt(fd, sendsize, recvsize);
202 if (ret == NULL)
203 return NULL;
204
205 slen = sizeof (struct sockaddr_storage);
206 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
207 warnx("svc_fd_create: could not retrieve local addr");
208 goto freedata;
209 }
210 ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
211 ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
212 if (ret->xp_ltaddr.buf == NULL) {
213 warnx("svc_fd_create: no mem for local addr");
214 goto freedata;
215 }
216 memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
217
218 slen = sizeof (struct sockaddr_storage);
219 if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
220 warnx("svc_fd_create: could not retrieve remote addr");
221 goto freedata;
222 }
223 ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
224 ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
225 if (ret->xp_rtaddr.buf == NULL) {
226 warnx("svc_fd_create: no mem for local addr");
227 goto freedata;
228 }
229 memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
230#ifdef PORTMAP
231 if (ss.ss_family == AF_INET || ss.ss_family == AF_LOCAL) {
232 ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
233 ret->xp_addrlen = sizeof (struct sockaddr_in);
234 }
235#endif /* PORTMAP */
236
237 return ret;
238
239freedata:
240 if (ret->xp_ltaddr.buf != NULL)
241 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
242
243 return NULL;
244}
245
246static SVCXPRT *
247makefd_xprt(fd, sendsize, recvsize)
248 int fd;
249 u_int sendsize;
250 u_int recvsize;
251{
252 SVCXPRT *xprt;
253 struct cf_conn *cd;
254 const char *netid;
255 struct __rpc_sockinfo si;
256
257 assert(fd != -1);
258
259 xprt = mem_alloc(sizeof(SVCXPRT));
260 if (xprt == NULL) {
261 warnx("svc_vc: makefd_xprt: out of memory");
262 goto done;
263 }
264 memset(xprt, 0, sizeof *xprt);
265 cd = mem_alloc(sizeof(struct cf_conn));
266 if (cd == NULL) {
267 warnx("svc_tcp: makefd_xprt: out of memory");
268 mem_free(xprt, sizeof(SVCXPRT));
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 mem_free(xprt, sizeof(SVCXPRT));
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 && i != cnt) {
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 }
565 }
566
567 return (len);
568}
569
570static enum xprt_stat
571svc_vc_stat(xprt)
572 SVCXPRT *xprt;
573{
574 struct cf_conn *cd;
575
576 assert(xprt != NULL);
577
578 cd = (struct cf_conn *)(xprt->xp_p1);
579
580 if (cd->strm_stat == XPRT_DIED)
581 return (XPRT_DIED);
582 if (! xdrrec_eof(&(cd->xdrs)))
583 return (XPRT_MOREREQS);
584 return (XPRT_IDLE);
585}
586
587static bool_t
588svc_vc_recv(xprt, msg)
589 SVCXPRT *xprt;
590 struct rpc_msg *msg;
591{
592 struct cf_conn *cd;
593 XDR *xdrs;
594
595 assert(xprt != NULL);
596 assert(msg != NULL);
597
598 cd = (struct cf_conn *)(xprt->xp_p1);
599 xdrs = &(cd->xdrs);
600
601 if (cd->nonblock) {
602 if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
603 return FALSE;
604 }
605
606 xdrs->x_op = XDR_DECODE;
607 (void)xdrrec_skiprecord(xdrs);
608 if (xdr_callmsg(xdrs, msg)) {
609 cd->x_id = msg->rm_xid;
610 return (TRUE);
611 }
612 cd->strm_stat = XPRT_DIED;
613 return (FALSE);
614}
615
616static bool_t
617svc_vc_getargs(xprt, xdr_args, args_ptr)
618 SVCXPRT *xprt;
619 xdrproc_t xdr_args;
620 void *args_ptr;
621{
622
623 assert(xprt != NULL);
624 /* args_ptr may be NULL */
625 return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
626 args_ptr));
627}
628
629static bool_t
630svc_vc_freeargs(xprt, xdr_args, args_ptr)
631 SVCXPRT *xprt;
632 xdrproc_t xdr_args;
633 void *args_ptr;
634{
635 XDR *xdrs;
636
637 assert(xprt != NULL);
638 /* args_ptr may be NULL */
639
640 xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
641
642 xdrs->x_op = XDR_FREE;
643 return ((*xdr_args)(xdrs, args_ptr));
644}
645
646static bool_t
647svc_vc_reply(xprt, msg)
648 SVCXPRT *xprt;
649 struct rpc_msg *msg;
650{
651 struct cf_conn *cd;
652 XDR *xdrs;
653 bool_t rstat;
654
655 assert(xprt != NULL);
656 assert(msg != NULL);
657
658 cd = (struct cf_conn *)(xprt->xp_p1);
659 xdrs = &(cd->xdrs);
660
661 xdrs->x_op = XDR_ENCODE;
662 msg->rm_xid = cd->x_id;
663 rstat = xdr_replymsg(xdrs, msg);
664 (void)xdrrec_endofrecord(xdrs, TRUE);
665 return (rstat);
666}
667
668static void
669svc_vc_ops(xprt)
670 SVCXPRT *xprt;
671{
672 static struct xp_ops ops;
673 static struct xp_ops2 ops2;
674
675/* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
676
677 mutex_lock(&ops_lock);
678 if (ops.xp_recv == NULL) {
679 ops.xp_recv = svc_vc_recv;
680 ops.xp_stat = svc_vc_stat;
681 ops.xp_getargs = svc_vc_getargs;
682 ops.xp_reply = svc_vc_reply;
683 ops.xp_freeargs = svc_vc_freeargs;
684 ops.xp_destroy = svc_vc_destroy;
685 ops2.xp_control = svc_vc_control;
686 }
687 xprt->xp_ops = &ops;
688 xprt->xp_ops2 = &ops2;
689 mutex_unlock(&ops_lock);
690}
691
692static void
693svc_vc_rendezvous_ops(xprt)
694 SVCXPRT *xprt;
695{
696 static struct xp_ops ops;
697 static struct xp_ops2 ops2;
698
699 mutex_lock(&ops_lock);
700 if (ops.xp_recv == NULL) {
701 ops.xp_recv = rendezvous_request;
702 ops.xp_stat = rendezvous_stat;
703 ops.xp_getargs =
704 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
705 ops.xp_reply =
706 (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
707 ops.xp_freeargs =
708 (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
709 ops.xp_destroy = svc_vc_destroy;
710 ops2.xp_control = svc_vc_rendezvous_control;
711 }
712 xprt->xp_ops = &ops;
713 xprt->xp_ops2 = &ops2;
714 mutex_unlock(&ops_lock);
715}
716
717/*
718 * Get the effective UID of the sending process. Used by rpcbind, keyserv
719 * and rpc.yppasswdd on AF_LOCAL.
720 */
721int
722__rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
723 int sock, ret;
724 gid_t egid;
725 uid_t euid;
726 struct sockaddr *sa;
727
728 sock = transp->xp_fd;
729 sa = (struct sockaddr *)transp->xp_rtaddr.buf;
730 if (sa->sa_family == AF_LOCAL) {
731 ret = getpeereid(sock, &euid, &egid);
732 if (ret == 0)
733 *uid = euid;
734 return (ret);
735 } else
736 return (-1);
737}
738
739/*
740 * Destroy xprts that have not have had any activity in 'timeout' seconds.
741 * If 'cleanblock' is true, blocking connections (the default) are also
742 * cleaned. If timeout is 0, the least active connection is picked.
743 */
744bool_t
745__svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
746{
747 int i, ncleaned;
748 SVCXPRT *xprt, *least_active;
749 struct timeval tv, tdiff, tmax;
750 struct cf_conn *cd;
751
752 gettimeofday(&tv, NULL);
753 tmax.tv_sec = tmax.tv_usec = 0;
754 least_active = NULL;
755 rwlock_wrlock(&svc_fd_lock);
756 for (i = ncleaned = 0; i <= svc_maxfd; i++) {
757 if (FD_ISSET(i, fds)) {
758 xprt = __svc_xports[i];
759 if (xprt == NULL || xprt->xp_ops == NULL ||
760 xprt->xp_ops->xp_recv != svc_vc_recv)
761 continue;
762 cd = (struct cf_conn *)xprt->xp_p1;
763 if (!cleanblock && !cd->nonblock)
764 continue;
765 if (timeout == 0) {
766 timersub(&tv, &cd->last_recv_time, &tdiff);
767 if (timercmp(&tdiff, &tmax, >)) {
768 tmax = tdiff;
769 least_active = xprt;
770 }
771 continue;
772 }
773 if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
774 __xprt_unregister_unlocked(xprt);
775 __svc_vc_dodestroy(xprt);
776 ncleaned++;
777 }
778 }
779 }
780 if (timeout == 0 && least_active != NULL) {
781 __xprt_unregister_unlocked(least_active);
782 __svc_vc_dodestroy(least_active);
783 ncleaned++;
784 }
785 rwlock_unlock(&svc_fd_lock);
786 return ncleaned > 0 ? TRUE : FALSE;
787}