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