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