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