PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

lib/libc/rpc/svc_vc.c

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