1 /* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";
34 static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
42 * Copyright (C) 1984, Sun Microsystems, Inc.
44 * TCP based RPC supports 'batched calls'.
45 * A sequence of calls may be batched-up in a send buffer. The rpc call
46 * return immediately to the client even though the call was not necessarily
47 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
48 * the rpc timeout value is zero (see clnt.h, rpc).
50 * Clients should NOT casually batch calls that in fact return results; that is,
51 * the server side should be aware that a call is batched and not produce any
52 * return message. Batched calls that produce many result messages can
53 * deadlock (netlock) the client and the server....
55 * Now go hang yourself.
58 #include <sys/param.h>
59 #include <sys/systm.h>
61 #include <sys/malloc.h>
63 #include <sys/mutex.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/syslog.h>
75 #include <netinet/tcp.h>
78 #include <rpc/rpc_com.h>
80 #define MCALL_MSG_SIZE 24
84 struct cmsgcred cmcred;
87 static enum clnt_stat clnt_vc_call(CLIENT *, struct rpc_callextra *,
88 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
89 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
90 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
91 static void clnt_vc_abort(CLIENT *);
92 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
93 static void clnt_vc_close(CLIENT *);
94 static void clnt_vc_destroy(CLIENT *);
95 static bool_t time_not_ok(struct timeval *);
96 static int clnt_vc_soupcall(struct socket *so, void *arg, int waitflag);
98 static struct clnt_ops clnt_vc_ops = {
99 .cl_call = clnt_vc_call,
100 .cl_abort = clnt_vc_abort,
101 .cl_geterr = clnt_vc_geterr,
102 .cl_freeres = clnt_vc_freeres,
103 .cl_close = clnt_vc_close,
104 .cl_destroy = clnt_vc_destroy,
105 .cl_control = clnt_vc_control
109 * A pending RPC request which awaits a reply. Requests which have
110 * received their reply will have cr_xid set to zero and cr_mrep to
111 * the mbuf chain of the reply.
114 TAILQ_ENTRY(ct_request) cr_link;
115 uint32_t cr_xid; /* XID of request */
116 struct mbuf *cr_mrep; /* reply received by upcall */
117 int cr_error; /* any error from upcall */
118 char cr_verf[MAX_AUTH_BYTES]; /* reply verf */
121 TAILQ_HEAD(ct_request_list, ct_request);
125 int ct_threads; /* number of threads in clnt_vc_call */
126 bool_t ct_closing; /* TRUE if we are closing */
127 bool_t ct_closed; /* TRUE if we are closed */
128 struct socket *ct_socket; /* connection socket */
129 bool_t ct_closeit; /* close it on destroy */
130 struct timeval ct_wait; /* wait interval in milliseconds */
131 struct sockaddr_storage ct_addr; /* remote addr */
132 struct rpc_err ct_error;
134 char ct_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */
135 size_t ct_mpos; /* pos after marshal */
136 const char *ct_waitchan;
138 struct mbuf *ct_record; /* current reply record */
139 size_t ct_record_resid; /* how much left of reply to read */
140 bool_t ct_record_eor; /* true if reading last fragment */
141 struct ct_request_list ct_pending;
142 int ct_upcallrefs; /* Ref cnt of upcalls in prog. */
145 static void clnt_vc_upcallsdone(struct ct_data *);
148 * Create a client handle for a connection.
149 * Default options are set, which the user can change using clnt_control()'s.
150 * The rpc/vc package does buffering similar to stdio, so the client
151 * must pick send and receive buffer sizes, 0 => use the default.
152 * NB: fd is copied into a private area.
153 * NB: The rpch->cl_auth is set null authentication. Caller may wish to
154 * set this something more useful.
156 * fd should be an open socket
160 struct socket *so, /* open file descriptor */
161 struct sockaddr *raddr, /* servers address */
162 const rpcprog_t prog, /* program number */
163 const rpcvers_t vers, /* version number */
164 size_t sendsz, /* buffer recv size */
165 size_t recvsz, /* buffer send size */
166 int intrflag) /* interruptible */
168 CLIENT *cl; /* client handle */
169 struct ct_data *ct = NULL; /* client handle */
171 struct rpc_msg call_msg;
172 static uint32_t disrupt;
173 struct __rpc_sockinfo si;
175 int error, interrupted, one = 1, sleep_flag;
179 disrupt = (uint32_t)(long)raddr;
181 cl = (CLIENT *)mem_alloc(sizeof (*cl));
182 ct = (struct ct_data *)mem_alloc(sizeof (*ct));
184 mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
186 ct->ct_closing = FALSE;
187 ct->ct_closed = FALSE;
188 ct->ct_upcallrefs = 0;
190 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
191 error = soconnect(so, raddr, curthread);
196 sleep_flag |= (PCATCH | PBDRY);
197 while ((so->so_state & SS_ISCONNECTING)
198 && so->so_error == 0) {
199 error = msleep(&so->so_timeo, SOCK_MTX(so),
200 sleep_flag, "connec", 0);
202 if (error == EINTR || error == ERESTART)
208 error = so->so_error;
214 so->so_state &= ~SS_ISCONNECTING;
215 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
216 rpc_createerr.cf_error.re_errno = error;
221 if (!__rpc_socket2sockinfo(so, &si)) {
225 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
226 bzero(&sopt, sizeof(sopt));
227 sopt.sopt_dir = SOPT_SET;
228 sopt.sopt_level = SOL_SOCKET;
229 sopt.sopt_name = SO_KEEPALIVE;
230 sopt.sopt_val = &one;
231 sopt.sopt_valsize = sizeof(one);
235 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
236 bzero(&sopt, sizeof(sopt));
237 sopt.sopt_dir = SOPT_SET;
238 sopt.sopt_level = IPPROTO_TCP;
239 sopt.sopt_name = TCP_NODELAY;
240 sopt.sopt_val = &one;
241 sopt.sopt_valsize = sizeof(one);
245 ct->ct_closeit = FALSE;
248 * Set up private data struct
251 ct->ct_wait.tv_sec = -1;
252 ct->ct_wait.tv_usec = -1;
253 memcpy(&ct->ct_addr, raddr, raddr->sa_len);
256 * Initialize call message
259 ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
260 call_msg.rm_xid = ct->ct_xid;
261 call_msg.rm_direction = CALL;
262 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
263 call_msg.rm_call.cb_prog = (uint32_t)prog;
264 call_msg.rm_call.cb_vers = (uint32_t)vers;
267 * pre-serialize the static part of the call msg and stash it away
269 xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
271 if (! xdr_callhdr(&xdrs, &call_msg)) {
272 if (ct->ct_closeit) {
273 soclose(ct->ct_socket);
277 ct->ct_mpos = XDR_GETPOS(&xdrs);
279 ct->ct_waitchan = "rpcrecv";
283 * Create a client handle which uses xdrrec for serialization
284 * and authnone for authentication.
286 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
287 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
288 error = soreserve(ct->ct_socket, sendsz, recvsz);
290 if (ct->ct_closeit) {
291 soclose(ct->ct_socket);
296 cl->cl_ops = &clnt_vc_ops;
298 cl->cl_auth = authnone_create();
300 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
301 soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct);
302 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
304 ct->ct_record = NULL;
305 ct->ct_record_resid = 0;
306 TAILQ_INIT(&ct->ct_pending);
312 mtx_destroy(&ct->ct_lock);
313 mem_free(ct, sizeof (struct ct_data));
316 mem_free(cl, sizeof (CLIENT));
318 return ((CLIENT *)NULL);
321 static enum clnt_stat
323 CLIENT *cl, /* client handle */
324 struct rpc_callextra *ext, /* call metadata */
325 rpcproc_t proc, /* procedure number */
326 struct mbuf *args, /* pointer to args */
327 struct mbuf **resultsp, /* pointer to results */
328 struct timeval utimeout)
330 struct ct_data *ct = (struct ct_data *) cl->cl_private;
332 struct rpc_err *errp;
335 struct rpc_msg reply_msg;
337 int nrefreshes = 2; /* number of times to refresh cred */
338 struct timeval timeout;
340 struct mbuf *mreq = NULL, *results;
341 struct ct_request *cr;
344 cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
346 mtx_lock(&ct->ct_lock);
348 if (ct->ct_closing || ct->ct_closed) {
349 mtx_unlock(&ct->ct_lock);
351 return (RPC_CANTSEND);
360 errp = &ct->ct_error;
366 if (ct->ct_wait.tv_usec == -1) {
367 timeout = utimeout; /* use supplied timeout */
369 timeout = ct->ct_wait; /* use default timeout */
373 mtx_assert(&ct->ct_lock, MA_OWNED);
378 mtx_unlock(&ct->ct_lock);
381 * Leave space to pre-pend the record mark.
383 MGETHDR(mreq, M_WAIT, MT_DATA);
384 mreq->m_data += sizeof(uint32_t);
385 KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
386 ("RPC header too big"));
387 bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
388 mreq->m_len = ct->ct_mpos;
391 * The XID is the first thing in the request.
393 *mtod(mreq, uint32_t *) = htonl(xid);
395 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
397 errp->re_status = stat = RPC_SUCCESS;
399 if ((! XDR_PUTINT32(&xdrs, &proc)) ||
400 (! AUTH_MARSHALL(auth, xid, &xdrs,
401 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
402 errp->re_status = stat = RPC_CANTENCODEARGS;
403 mtx_lock(&ct->ct_lock);
406 mreq->m_pkthdr.len = m_length(mreq, NULL);
409 * Prepend a record marker containing the packet length.
411 M_PREPEND(mreq, sizeof(uint32_t), M_WAIT);
412 *mtod(mreq, uint32_t *) =
413 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
416 mtx_lock(&ct->ct_lock);
418 * Check to see if the other end has already started to close down
419 * the connection. The upcall will have set ct_error.re_status
420 * to RPC_CANTRECV if this is the case.
421 * If the other end starts to close down the connection after this
422 * point, it will be detected later when cr_error is checked,
423 * since the request is in the ct_pending queue.
425 if (ct->ct_error.re_status == RPC_CANTRECV) {
426 if (errp != &ct->ct_error) {
427 errp->re_errno = ct->ct_error.re_errno;
428 errp->re_status = RPC_CANTRECV;
433 TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
434 mtx_unlock(&ct->ct_lock);
437 * sosend consumes mreq.
439 error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
441 if (error == EMSGSIZE) {
442 SOCKBUF_LOCK(&ct->ct_socket->so_snd);
443 sbwait(&ct->ct_socket->so_snd);
444 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd);
445 AUTH_VALIDATE(auth, xid, NULL, NULL);
446 mtx_lock(&ct->ct_lock);
447 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
451 reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
452 reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
453 reply_msg.acpted_rply.ar_verf.oa_length = 0;
454 reply_msg.acpted_rply.ar_results.where = NULL;
455 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
457 mtx_lock(&ct->ct_lock);
459 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
460 errp->re_errno = error;
461 errp->re_status = stat = RPC_CANTSEND;
466 * Check to see if we got an upcall while waiting for the
467 * lock. In both these cases, the request has been removed
468 * from ct->ct_pending.
471 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
472 errp->re_errno = cr->cr_error;
473 errp->re_status = stat = RPC_CANTRECV;
477 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
482 * Hack to provide rpc-based message passing
484 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
485 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
486 errp->re_status = stat = RPC_TIMEDOUT;
490 error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
493 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
497 * The sleep returned an error so our request is still
498 * on the list. Turn the error code into an
499 * appropriate client status.
501 errp->re_errno = error;
513 errp->re_status = stat;
517 * We were woken up by the upcall. If the
518 * upcall had a receive error, report that,
519 * otherwise we have a reply.
522 errp->re_errno = cr->cr_error;
523 errp->re_status = stat = RPC_CANTRECV;
530 * Now decode and validate the response. We need to drop the
531 * lock since xdr_replymsg may end up sleeping in malloc.
533 mtx_unlock(&ct->ct_lock);
535 if (ext && ext->rc_feedback)
536 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
538 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
539 ok = xdr_replymsg(&xdrs, &reply_msg);
543 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
544 (reply_msg.acpted_rply.ar_stat == SUCCESS))
545 errp->re_status = stat = RPC_SUCCESS;
547 stat = _seterr_reply(&reply_msg, errp);
549 if (stat == RPC_SUCCESS) {
550 results = xdrmbuf_getall(&xdrs);
551 if (!AUTH_VALIDATE(auth, xid,
552 &reply_msg.acpted_rply.ar_verf,
554 errp->re_status = stat = RPC_AUTHERROR;
555 errp->re_why = AUTH_INVALIDRESP;
558 ("auth validated but no result"));
561 } /* end successful completion */
563 * If unsuccesful AND error is an authentication error
564 * then refresh credentials and try again, else break
566 else if (stat == RPC_AUTHERROR)
567 /* maybe our credentials need to be refreshed ... */
568 if (nrefreshes > 0 &&
569 AUTH_REFRESH(auth, &reply_msg)) {
572 mtx_lock(&ct->ct_lock);
575 /* end of unsuccessful completion */
576 } /* end of valid reply message */
578 errp->re_status = stat = RPC_CANTDECODERES;
581 mtx_lock(&ct->ct_lock);
583 mtx_assert(&ct->ct_lock, MA_OWNED);
585 KASSERT(stat != RPC_SUCCESS || *resultsp,
586 ("RPC_SUCCESS without reply"));
591 m_freem(cr->cr_mrep);
597 mtx_unlock(&ct->ct_lock);
599 if (auth && stat != RPC_SUCCESS)
600 AUTH_VALIDATE(auth, xid, NULL, NULL);
608 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
610 struct ct_data *ct = (struct ct_data *) cl->cl_private;
612 *errp = ct->ct_error;
616 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
621 xdrs.x_op = XDR_FREE;
622 dummy = (*xdr_res)(&xdrs, res_ptr);
629 clnt_vc_abort(CLIENT *cl)
634 clnt_vc_control(CLIENT *cl, u_int request, void *info)
636 struct ct_data *ct = (struct ct_data *)cl->cl_private;
639 mtx_lock(&ct->ct_lock);
643 ct->ct_closeit = TRUE;
644 mtx_unlock(&ct->ct_lock);
646 case CLSET_FD_NCLOSE:
647 ct->ct_closeit = FALSE;
648 mtx_unlock(&ct->ct_lock);
654 /* for other requests which use info */
656 mtx_unlock(&ct->ct_lock);
661 if (time_not_ok((struct timeval *)info)) {
662 mtx_unlock(&ct->ct_lock);
665 ct->ct_wait = *(struct timeval *)infop;
668 *(struct timeval *)infop = ct->ct_wait;
670 case CLGET_SERVER_ADDR:
671 (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len);
675 * Slightly different semantics to userland - we use
676 * sockaddr instead of netbuf.
678 memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len);
680 case CLSET_SVC_ADDR: /* set to new address */
681 mtx_unlock(&ct->ct_lock);
684 *(uint32_t *)info = ct->ct_xid;
687 /* This will set the xid of the NEXT call */
688 /* decrement by 1 as clnt_vc_call() increments once */
689 ct->ct_xid = *(uint32_t *)info - 1;
693 * This RELIES on the information that, in the call body,
694 * the version number field is the fifth field from the
695 * begining of the RPC header. MUST be changed if the
696 * call_struct is changed
699 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
700 4 * BYTES_PER_XDR_UNIT));
704 *(uint32_t *)(void *)(ct->ct_mcallc +
705 4 * BYTES_PER_XDR_UNIT) =
706 htonl(*(uint32_t *)info);
711 * This RELIES on the information that, in the call body,
712 * the program number field is the fourth field from the
713 * begining of the RPC header. MUST be changed if the
714 * call_struct is changed
717 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
718 3 * BYTES_PER_XDR_UNIT));
722 *(uint32_t *)(void *)(ct->ct_mcallc +
723 3 * BYTES_PER_XDR_UNIT) =
724 htonl(*(uint32_t *)info);
728 ct->ct_waitchan = (const char *)info;
732 *(const char **) info = ct->ct_waitchan;
735 case CLSET_INTERRUPTIBLE:
737 ct->ct_waitflag = PCATCH | PBDRY;
742 case CLGET_INTERRUPTIBLE:
744 *(int *) info = TRUE;
746 *(int *) info = FALSE;
750 mtx_unlock(&ct->ct_lock);
754 mtx_unlock(&ct->ct_lock);
759 clnt_vc_close(CLIENT *cl)
761 struct ct_data *ct = (struct ct_data *) cl->cl_private;
762 struct ct_request *cr;
764 mtx_lock(&ct->ct_lock);
767 mtx_unlock(&ct->ct_lock);
771 if (ct->ct_closing) {
772 while (ct->ct_closing)
773 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
774 KASSERT(ct->ct_closed, ("client should be closed"));
775 mtx_unlock(&ct->ct_lock);
780 ct->ct_closing = TRUE;
781 mtx_unlock(&ct->ct_lock);
783 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
784 soupcall_clear(ct->ct_socket, SO_RCV);
785 clnt_vc_upcallsdone(ct);
786 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
789 * Abort any pending requests and wait until everyone
790 * has finished with clnt_vc_call.
792 mtx_lock(&ct->ct_lock);
793 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
795 cr->cr_error = ESHUTDOWN;
799 while (ct->ct_threads)
800 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
803 ct->ct_closing = FALSE;
804 ct->ct_closed = TRUE;
805 mtx_unlock(&ct->ct_lock);
810 clnt_vc_destroy(CLIENT *cl)
812 struct ct_data *ct = (struct ct_data *) cl->cl_private;
813 struct socket *so = NULL;
817 mtx_lock(&ct->ct_lock);
820 if (ct->ct_closeit) {
825 mtx_unlock(&ct->ct_lock);
827 mtx_destroy(&ct->ct_lock);
829 soshutdown(so, SHUT_WR);
832 mem_free(ct, sizeof(struct ct_data));
833 if (cl->cl_netid && cl->cl_netid[0])
834 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
835 if (cl->cl_tp && cl->cl_tp[0])
836 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
837 mem_free(cl, sizeof(CLIENT));
841 * Make sure that the time is not garbage. -1 value is disallowed.
842 * Note this is different from time_not_ok in clnt_dg.c
845 time_not_ok(struct timeval *t)
847 return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
848 t->tv_usec <= -1 || t->tv_usec > 1000000);
852 clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
854 struct ct_data *ct = (struct ct_data *) arg;
857 struct ct_request *cr;
858 int error, rcvflag, foundreq;
859 uint32_t xid, header;
863 uio.uio_td = curthread;
866 * If ct_record_resid is zero, we are waiting for a
869 if (ct->ct_record_resid == 0) {
872 * Make sure there is either a whole record
873 * mark in the buffer or there is some other
877 if (so->so_rcv.sb_cc >= sizeof(uint32_t)
878 || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
885 SOCKBUF_UNLOCK(&so->so_rcv);
886 uio.uio_resid = sizeof(uint32_t);
888 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
889 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
890 SOCKBUF_LOCK(&so->so_rcv);
892 if (error == EWOULDBLOCK)
896 * If there was an error, wake up all pending
899 if (error || uio.uio_resid > 0) {
901 mtx_lock(&ct->ct_lock);
904 * We must have got EOF trying
905 * to read from the stream.
909 ct->ct_error.re_status = RPC_CANTRECV;
910 ct->ct_error.re_errno = error;
911 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
912 cr->cr_error = error;
915 mtx_unlock(&ct->ct_lock);
918 m_copydata(m, 0, sizeof(uint32_t), (char *)&header);
919 header = ntohl(header);
920 ct->ct_record = NULL;
921 ct->ct_record_resid = header & 0x7fffffff;
922 ct->ct_record_eor = ((header & 0x80000000) != 0);
926 * Wait until the socket has the whole record
930 if (so->so_rcv.sb_cc >= ct->ct_record_resid
931 || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
939 * We have the record mark. Read as much as
940 * the socket has buffered up to the end of
943 SOCKBUF_UNLOCK(&so->so_rcv);
944 uio.uio_resid = ct->ct_record_resid;
946 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
947 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
948 SOCKBUF_LOCK(&so->so_rcv);
950 if (error == EWOULDBLOCK)
953 if (error || uio.uio_resid == ct->ct_record_resid)
957 * If we have part of the record already,
958 * chain this bit onto the end.
961 m_last(ct->ct_record)->m_next = m;
965 ct->ct_record_resid = uio.uio_resid;
968 * If we have the entire record, see if we can
969 * match it to a request.
971 if (ct->ct_record_resid == 0
972 && ct->ct_record_eor) {
974 * The XID is in the first uint32_t of
977 if (ct->ct_record->m_len < sizeof(xid) &&
978 m_length(ct->ct_record, NULL) <
980 m_freem(ct->ct_record);
983 m_copydata(ct->ct_record, 0, sizeof(xid),
987 mtx_lock(&ct->ct_lock);
989 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
990 if (cr->cr_xid == xid) {
997 * that we will ignore
1002 cr->cr_mrep = ct->ct_record;
1009 mtx_unlock(&ct->ct_lock);
1012 m_freem(ct->ct_record);
1013 ct->ct_record = NULL;
1017 ct->ct_upcallrefs--;
1018 if (ct->ct_upcallrefs < 0)
1019 panic("rpcvc upcall refcnt");
1020 if (ct->ct_upcallrefs == 0)
1021 wakeup(&ct->ct_upcallrefs);
1026 * Wait for all upcalls in progress to complete.
1029 clnt_vc_upcallsdone(struct ct_data *ct)
1032 SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv);
1034 while (ct->ct_upcallrefs > 0)
1035 (void) msleep(&ct->ct_upcallrefs,
1036 SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);