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