1 /* $NetBSD: svc_vc.c,v 1.24 2011/02/04 17:38:15 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.24 2011/02/04 17:38:15 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 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, sizeof one) < 0) 179 goto cleanup_svc_vc_create; 180 181 xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len; 182 xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len); 183 if (xprt->xp_ltaddr.buf == NULL) { 184 warnx("svc_vc_create: no mem for local addr"); 185 goto cleanup_svc_vc_create; 186 } 187 memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len); 188 189 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage); 190 xprt_register(xprt); 191 return xprt; 192 cleanup_svc_vc_create: 193 if (xprt) 194 mem_free(xprt, sizeof(*xprt)); 195 if (r != NULL) 196 mem_free(r, sizeof(*r)); 197 return NULL; 198 } 199 200 /* 201 * Like svtcp_create(), except the routine takes any *open* UNIX file 202 * descriptor as its first input. 203 */ 204 SVCXPRT * 205 svc_fd_create(int fd, u_int sendsize, u_int recvsize) 206 { 207 struct sockaddr_storage ss; 208 socklen_t slen; 209 SVCXPRT *ret; 210 211 _DIAGASSERT(fd != -1); 212 213 ret = makefd_xprt(fd, sendsize, recvsize); 214 if (ret == NULL) 215 return NULL; 216 217 slen = sizeof (struct sockaddr_storage); 218 if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { 219 warnx("svc_fd_create: could not retrieve local addr"); 220 goto freedata; 221 } 222 ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len; 223 ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len); 224 if (ret->xp_ltaddr.buf == NULL) { 225 warnx("svc_fd_create: no mem for local addr"); 226 goto freedata; 227 } 228 memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len); 229 230 slen = sizeof (struct sockaddr_storage); 231 if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { 232 warnx("svc_fd_create: could not retrieve remote addr"); 233 goto freedata; 234 } 235 ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len; 236 ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len); 237 if (ret->xp_rtaddr.buf == NULL) { 238 warnx("svc_fd_create: no mem for local addr"); 239 goto freedata; 240 } 241 memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len); 242 #ifdef PORTMAP 243 if (ss.ss_family == AF_INET) { 244 ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf; 245 ret->xp_addrlen = sizeof (struct sockaddr_in); 246 } 247 #endif 248 249 return ret; 250 251 freedata: 252 if (ret->xp_ltaddr.buf != NULL) 253 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen); 254 255 return NULL; 256 } 257 258 static SVCXPRT * 259 makefd_xprt(int fd, u_int sendsize, u_int recvsize) 260 { 261 SVCXPRT *xprt; 262 struct cf_conn *cd; 263 const char *netid; 264 struct __rpc_sockinfo si; 265 266 _DIAGASSERT(fd != -1); 267 268 xprt = mem_alloc(sizeof(SVCXPRT)); 269 if (xprt == NULL) 270 goto out; 271 memset(xprt, 0, sizeof *xprt); 272 cd = mem_alloc(sizeof(struct cf_conn)); 273 if (cd == NULL) 274 goto out; 275 cd->strm_stat = XPRT_IDLE; 276 xdrrec_create(&(cd->xdrs), sendsize, recvsize, 277 (caddr_t)(void *)xprt, read_vc, write_vc); 278 xprt->xp_p1 = (caddr_t)(void *)cd; 279 xprt->xp_verf.oa_base = cd->verf_body; 280 svc_vc_ops(xprt); /* truely deals with calls */ 281 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */ 282 xprt->xp_fd = fd; 283 if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid)) 284 if ((xprt->xp_netid = strdup(netid)) == NULL) 285 goto out; 286 287 xprt_register(xprt); 288 return xprt; 289 out: 290 warn("svc_tcp: makefd_xprt"); 291 if (xprt) 292 mem_free(xprt, sizeof(SVCXPRT)); 293 return NULL; 294 } 295 296 /*ARGSUSED*/ 297 static bool_t 298 rendezvous_request(SVCXPRT *xprt, struct rpc_msg *msg) 299 { 300 int sock, flags; 301 struct cf_rendezvous *r; 302 struct cf_conn *cd; 303 struct sockaddr_storage addr; 304 socklen_t len; 305 struct __rpc_sockinfo si; 306 SVCXPRT *newxprt; 307 fd_set cleanfds; 308 309 _DIAGASSERT(xprt != NULL); 310 _DIAGASSERT(msg != NULL); 311 312 r = (struct cf_rendezvous *)xprt->xp_p1; 313 again: 314 len = sizeof addr; 315 if ((sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr, 316 &len)) < 0) { 317 if (errno == EINTR) 318 goto again; 319 /* 320 * Clean out the most idle file descriptor when we're 321 * running out. 322 */ 323 if (errno == EMFILE || errno == ENFILE) { 324 cleanfds = svc_fdset; 325 if (__svc_clean_idle(&cleanfds, 0, FALSE)) 326 goto again; 327 } 328 return FALSE; 329 } 330 /* 331 * make a new transporter (re-uses xprt) 332 */ 333 newxprt = makefd_xprt(sock, r->sendsize, r->recvsize); 334 if (newxprt == NULL) 335 goto out; 336 newxprt->xp_rtaddr.buf = mem_alloc(len); 337 if (newxprt->xp_rtaddr.buf == NULL) 338 goto out; 339 memcpy(newxprt->xp_rtaddr.buf, &addr, len); 340 newxprt->xp_rtaddr.len = len; 341 #ifdef PORTMAP 342 if (addr.ss_family == AF_INET) { 343 newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf; 344 newxprt->xp_addrlen = sizeof (struct sockaddr_in); 345 } 346 #endif 347 if (__rpc_fd2sockinfo(sock, &si)) 348 __rpc_setnodelay(sock, &si); 349 350 cd = (struct cf_conn *)newxprt->xp_p1; 351 352 cd->recvsize = r->recvsize; 353 cd->sendsize = r->sendsize; 354 cd->maxrec = r->maxrec; 355 356 if (cd->maxrec != 0) { 357 flags = fcntl(sock, F_GETFL, 0); 358 if (flags == -1) 359 goto out; 360 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) 361 goto out; 362 if (cd->recvsize > (u_int)cd->maxrec) 363 cd->recvsize = cd->maxrec; 364 cd->nonblock = TRUE; 365 __xdrrec_setnonblock(&cd->xdrs, cd->maxrec); 366 } else 367 cd->nonblock = FALSE; 368 369 (void)gettimeofday(&cd->last_recv_time, NULL); 370 371 return FALSE; /* there is never an rpc msg to be processed */ 372 out: 373 (void)close(sock); 374 return FALSE; /* there was an error */ 375 } 376 377 /*ARGSUSED*/ 378 static enum xprt_stat 379 rendezvous_stat(SVCXPRT *xprt) 380 { 381 382 return XPRT_IDLE; 383 } 384 385 static void 386 svc_vc_destroy(SVCXPRT *xprt) 387 { 388 _DIAGASSERT(xprt != NULL); 389 390 xprt_unregister(xprt); 391 __svc_vc_dodestroy(xprt); 392 } 393 394 static void 395 __svc_vc_dodestroy(SVCXPRT *xprt) 396 { 397 struct cf_conn *cd; 398 struct cf_rendezvous *r; 399 400 cd = (struct cf_conn *)xprt->xp_p1; 401 402 if (xprt->xp_fd != RPC_ANYFD) 403 (void)close(xprt->xp_fd); 404 if (xprt->xp_port != 0) { 405 /* a rendezvouser socket */ 406 r = (struct cf_rendezvous *)xprt->xp_p1; 407 mem_free(r, sizeof (struct cf_rendezvous)); 408 xprt->xp_port = 0; 409 } else { 410 /* an actual connection socket */ 411 XDR_DESTROY(&(cd->xdrs)); 412 mem_free(cd, sizeof(struct cf_conn)); 413 } 414 if (xprt->xp_rtaddr.buf) 415 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen); 416 if (xprt->xp_ltaddr.buf) 417 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen); 418 if (xprt->xp_tp) 419 free(xprt->xp_tp); 420 if (xprt->xp_netid) 421 free(xprt->xp_netid); 422 mem_free(xprt, sizeof(SVCXPRT)); 423 } 424 425 /*ARGSUSED*/ 426 static bool_t 427 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in) 428 { 429 return FALSE; 430 } 431 432 /*ARGSUSED*/ 433 static bool_t 434 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, 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 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 */ 461 static int 462 read_vc(caddr_t xprtp, caddr_t buf, int len) 463 { 464 SVCXPRT *xprt; 465 int sock; 466 struct pollfd pollfd; 467 struct sockaddr *sa; 468 struct msghdr msg; 469 struct cmsghdr *cmp; 470 void *crmsg = NULL; 471 struct sockcred *sc; 472 socklen_t crmsgsize; 473 struct cf_conn *cfp; 474 static const struct timespec ts = { 35, 0 }; 475 476 xprt = (SVCXPRT *)(void *)xprtp; 477 _DIAGASSERT(xprt != NULL); 478 479 sock = xprt->xp_fd; 480 481 sa = (struct sockaddr *)xprt->xp_rtaddr.buf; 482 if (sa->sa_family == AF_LOCAL && xprt->xp_p2 == NULL) { 483 memset(&msg, 0, sizeof msg); 484 crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS)); 485 crmsg = malloc(crmsgsize); 486 if (crmsg == NULL) 487 goto fatal_err; 488 memset(crmsg, 0, crmsgsize); 489 490 msg.msg_control = crmsg; 491 msg.msg_controllen = crmsgsize; 492 493 if (recvmsg(sock, &msg, 0) < 0) 494 goto fatal_err; 495 496 if (msg.msg_controllen == 0 || 497 (msg.msg_flags & MSG_CTRUNC) != 0) 498 goto fatal_err; 499 500 cmp = CMSG_FIRSTHDR(&msg); 501 if (cmp->cmsg_level != SOL_SOCKET || 502 cmp->cmsg_type != SCM_CREDS) 503 goto fatal_err; 504 505 sc = (struct sockcred *)(void *)CMSG_DATA(cmp); 506 507 xprt->xp_p2 = mem_alloc(SOCKCREDSIZE(sc->sc_ngroups)); 508 if (xprt->xp_p2 == NULL) 509 goto fatal_err; 510 511 memcpy(xprt->xp_p2, sc, SOCKCREDSIZE(sc->sc_ngroups)); 512 free(crmsg); 513 crmsg = NULL; 514 } 515 516 cfp = (struct cf_conn *)xprt->xp_p1; 517 518 if (cfp->nonblock) { 519 len = read(sock, buf, (size_t)len); 520 if (len < 0) { 521 if (errno == EAGAIN) 522 len = 0; 523 else 524 goto fatal_err; 525 } 526 if (len != 0) 527 gettimeofday(&cfp->last_recv_time, NULL); 528 return len; 529 } 530 531 do { 532 pollfd.fd = sock; 533 pollfd.events = POLLIN; 534 switch (pollts(&pollfd, 1, &ts, NULL)) { 535 case -1: 536 if (errno == EINTR) { 537 continue; 538 } 539 /*FALLTHROUGH*/ 540 case 0: 541 goto fatal_err; 542 543 default: 544 break; 545 } 546 } while ((pollfd.revents & POLLIN) == 0); 547 548 if ((len = read(sock, buf, (size_t)len)) > 0) { 549 gettimeofday(&cfp->last_recv_time, NULL); 550 return len; 551 } 552 553 fatal_err: 554 if (crmsg != NULL) 555 free(crmsg); 556 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED; 557 return -1; 558 } 559 560 /* 561 * writes data to the tcp connection. 562 * Any error is fatal and the connection is closed. 563 */ 564 static int 565 write_vc(caddr_t xprtp, caddr_t buf, int len) 566 { 567 SVCXPRT *xprt; 568 int i, cnt; 569 struct cf_conn *cd; 570 struct timeval tv0, tv1; 571 572 xprt = (SVCXPRT *)(void *)xprtp; 573 _DIAGASSERT(xprt != NULL); 574 575 cd = (struct cf_conn *)xprt->xp_p1; 576 577 if (cd->nonblock) 578 gettimeofday(&tv0, NULL); 579 580 for (cnt = len; cnt > 0; cnt -= i, buf += i) { 581 if ((i = write(xprt->xp_fd, buf, (size_t)cnt)) < 0) { 582 if (errno != EAGAIN || !cd->nonblock) { 583 cd->strm_stat = XPRT_DIED; 584 return -1; 585 } 586 if (cd->nonblock) { 587 /* 588 * For non-blocking connections, do not 589 * take more than 2 seconds writing the 590 * data out. 591 * 592 * XXX 2 is an arbitrary amount. 593 */ 594 gettimeofday(&tv1, NULL); 595 if (tv1.tv_sec - tv0.tv_sec >= 2) { 596 cd->strm_stat = XPRT_DIED; 597 return -1; 598 } 599 } 600 i = 0; 601 } 602 } 603 return len; 604 } 605 606 static enum xprt_stat 607 svc_vc_stat(SVCXPRT *xprt) 608 { 609 struct cf_conn *cd; 610 611 _DIAGASSERT(xprt != NULL); 612 613 cd = (struct cf_conn *)(xprt->xp_p1); 614 615 if (cd->strm_stat == XPRT_DIED) 616 return XPRT_DIED; 617 if (! xdrrec_eof(&(cd->xdrs))) 618 return XPRT_MOREREQS; 619 return XPRT_IDLE; 620 } 621 622 static bool_t 623 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg) 624 { 625 struct cf_conn *cd; 626 XDR *xdrs; 627 628 _DIAGASSERT(xprt != NULL); 629 _DIAGASSERT(msg != NULL); 630 631 cd = (struct cf_conn *)(xprt->xp_p1); 632 xdrs = &(cd->xdrs); 633 634 if (cd->nonblock) { 635 if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE)) 636 return FALSE; 637 } 638 639 xdrs->x_op = XDR_DECODE; 640 (void)xdrrec_skiprecord(xdrs); 641 642 if (xdr_callmsg(xdrs, msg)) { 643 cd->x_id = msg->rm_xid; 644 return TRUE; 645 } 646 cd->strm_stat = XPRT_DIED; 647 return FALSE; 648 } 649 650 static bool_t 651 svc_vc_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr) 652 { 653 654 _DIAGASSERT(xprt != NULL); 655 /* args_ptr may be NULL */ 656 657 return (*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs), 658 args_ptr); 659 } 660 661 static bool_t 662 svc_vc_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr) 663 { 664 XDR *xdrs; 665 666 _DIAGASSERT(xprt != NULL); 667 /* args_ptr may be NULL */ 668 669 xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs); 670 671 xdrs->x_op = XDR_FREE; 672 return (*xdr_args)(xdrs, args_ptr); 673 } 674 675 static bool_t 676 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg) 677 { 678 struct cf_conn *cd; 679 XDR *xdrs; 680 bool_t rstat; 681 682 _DIAGASSERT(xprt != NULL); 683 _DIAGASSERT(msg != NULL); 684 685 cd = (struct cf_conn *)(xprt->xp_p1); 686 xdrs = &(cd->xdrs); 687 688 xdrs->x_op = XDR_ENCODE; 689 msg->rm_xid = cd->x_id; 690 rstat = xdr_replymsg(xdrs, msg); 691 (void)xdrrec_endofrecord(xdrs, TRUE); 692 return rstat; 693 } 694 695 static void 696 svc_vc_ops(SVCXPRT *xprt) 697 { 698 static struct xp_ops ops; 699 static struct xp_ops2 ops2; 700 #ifdef _REENTRANT 701 extern mutex_t ops_lock; 702 #endif 703 704 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */ 705 706 mutex_lock(&ops_lock); 707 if (ops.xp_recv == NULL) { 708 ops.xp_recv = svc_vc_recv; 709 ops.xp_stat = svc_vc_stat; 710 ops.xp_getargs = svc_vc_getargs; 711 ops.xp_reply = svc_vc_reply; 712 ops.xp_freeargs = svc_vc_freeargs; 713 ops.xp_destroy = svc_vc_destroy; 714 ops2.xp_control = svc_vc_control; 715 } 716 xprt->xp_ops = &ops; 717 xprt->xp_ops2 = &ops2; 718 mutex_unlock(&ops_lock); 719 } 720 721 static void 722 svc_vc_rendezvous_ops(xprt) 723 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