xref: /netbsd/lib/libc/rpc/clnt_vc.c (revision 0e6570fc)
1*0e6570fcSandvar /*	$NetBSD: clnt_vc.c,v 1.28 2022/01/24 09:14:36 andvar Exp $	*/
27df0ccbaSfvdl 
37df0ccbaSfvdl /*
415c1b43eStron  * Copyright (c) 2010, Oracle America, Inc.
57df0ccbaSfvdl  *
615c1b43eStron  * Redistribution and use in source and binary forms, with or without
715c1b43eStron  * modification, are permitted provided that the following conditions are
815c1b43eStron  * met:
97df0ccbaSfvdl  *
1015c1b43eStron  *     * Redistributions of source code must retain the above copyright
1115c1b43eStron  *       notice, this list of conditions and the following disclaimer.
1215c1b43eStron  *     * Redistributions in binary form must reproduce the above
1315c1b43eStron  *       copyright notice, this list of conditions and the following
1415c1b43eStron  *       disclaimer in the documentation and/or other materials
1515c1b43eStron  *       provided with the distribution.
1615c1b43eStron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
1715c1b43eStron  *       contributors may be used to endorse or promote products derived
1815c1b43eStron  *       from this software without specific prior written permission.
197df0ccbaSfvdl  *
2015c1b43eStron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2115c1b43eStron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2215c1b43eStron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2315c1b43eStron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2415c1b43eStron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2515c1b43eStron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2615c1b43eStron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2715c1b43eStron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2815c1b43eStron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2915c1b43eStron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3015c1b43eStron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3115c1b43eStron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
327df0ccbaSfvdl  */
337df0ccbaSfvdl 
347df0ccbaSfvdl #include <sys/cdefs.h>
357df0ccbaSfvdl #if defined(LIBC_SCCS) && !defined(lint)
367df0ccbaSfvdl #if 0
377df0ccbaSfvdl static char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
387df0ccbaSfvdl static char *sccsid = "@(#)clnt_tcp.c	2.2 88/08/01 4.0 RPCSRC";
397df0ccbaSfvdl static char sccsid[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
407df0ccbaSfvdl #else
41*0e6570fcSandvar __RCSID("$NetBSD: clnt_vc.c,v 1.28 2022/01/24 09:14:36 andvar Exp $");
427df0ccbaSfvdl #endif
437df0ccbaSfvdl #endif
447df0ccbaSfvdl 
457df0ccbaSfvdl /*
467df0ccbaSfvdl  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
477df0ccbaSfvdl  *
487df0ccbaSfvdl  * Copyright (C) 1984, Sun Microsystems, Inc.
497df0ccbaSfvdl  *
507df0ccbaSfvdl  * TCP based RPC supports 'batched calls'.
517df0ccbaSfvdl  * A sequence of calls may be batched-up in a send buffer.  The rpc call
527df0ccbaSfvdl  * return immediately to the client even though the call was not necessarily
537df0ccbaSfvdl  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
547df0ccbaSfvdl  * the rpc timeout value is zero (see clnt.h, rpc).
557df0ccbaSfvdl  *
567df0ccbaSfvdl  * Clients should NOT casually batch calls that in fact return results; that is,
577df0ccbaSfvdl  * the server side should be aware that a call is batched and not produce any
587df0ccbaSfvdl  * return message.  Batched calls that produce many result messages can
597df0ccbaSfvdl  * deadlock (netlock) the client and the server....
607df0ccbaSfvdl  *
617df0ccbaSfvdl  * Now go hang yourself.
627df0ccbaSfvdl  */
637df0ccbaSfvdl 
647df0ccbaSfvdl #include "namespace.h"
657df0ccbaSfvdl #include "reentrant.h"
667df0ccbaSfvdl #include <sys/types.h>
677df0ccbaSfvdl #include <sys/poll.h>
687df0ccbaSfvdl #include <sys/socket.h>
697df0ccbaSfvdl 
707df0ccbaSfvdl #include <assert.h>
717df0ccbaSfvdl #include <err.h>
727df0ccbaSfvdl #include <errno.h>
737df0ccbaSfvdl #include <netdb.h>
747df0ccbaSfvdl #include <stdio.h>
757df0ccbaSfvdl #include <stdlib.h>
7611e5c6ccSthorpej #include <string.h>
777df0ccbaSfvdl #include <unistd.h>
787df0ccbaSfvdl #include <signal.h>
797df0ccbaSfvdl 
807df0ccbaSfvdl #include <rpc/rpc.h>
817df0ccbaSfvdl 
826c0ae2b0Schristos #include "svc_fdset.h"
8379d5b270Sfvdl #include "rpc_internal.h"
847df0ccbaSfvdl 
857df0ccbaSfvdl #ifdef __weak_alias
867df0ccbaSfvdl __weak_alias(clnt_vc_create,_clnt_vc_create)
877df0ccbaSfvdl #endif
887df0ccbaSfvdl 
897df0ccbaSfvdl #define MCALL_MSG_SIZE 24
907df0ccbaSfvdl 
91e826745eSchristos static enum clnt_stat clnt_vc_call(CLIENT *, rpcproc_t, xdrproc_t,
92e826745eSchristos     const char *, xdrproc_t, caddr_t, struct timeval);
93e826745eSchristos static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
94e826745eSchristos static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, caddr_t);
95e826745eSchristos static void clnt_vc_abort(CLIENT *);
96e826745eSchristos static bool_t clnt_vc_control(CLIENT *, u_int, char *);
97e826745eSchristos static void clnt_vc_destroy(CLIENT *);
98e826745eSchristos static struct clnt_ops *clnt_vc_ops(void);
99e826745eSchristos static bool_t time_not_ok(struct timeval *);
100e826745eSchristos static int read_vc(caddr_t, caddr_t, int);
101e826745eSchristos static int write_vc(caddr_t, caddr_t, int);
1027df0ccbaSfvdl 
1037df0ccbaSfvdl struct ct_data {
1047df0ccbaSfvdl 	int		ct_fd;
1057df0ccbaSfvdl 	bool_t		ct_closeit;
1067df0ccbaSfvdl 	struct timeval	ct_wait;
1077df0ccbaSfvdl 	bool_t          ct_waitset;       /* wait set by clnt_control? */
1087df0ccbaSfvdl 	struct netbuf	ct_addr;
1097df0ccbaSfvdl 	struct rpc_err	ct_error;
1107df0ccbaSfvdl 	union {
1117df0ccbaSfvdl 		char	ct_mcallc[MCALL_MSG_SIZE];	/* marshalled callmsg */
1127df0ccbaSfvdl 		u_int32_t ct_mcalli;
1137df0ccbaSfvdl 	} ct_u;
1147df0ccbaSfvdl 	u_int		ct_mpos;			/* pos after marshal */
1157df0ccbaSfvdl 	XDR		ct_xdrs;
1167df0ccbaSfvdl };
1177df0ccbaSfvdl 
1187df0ccbaSfvdl /*
1197df0ccbaSfvdl  *      This machinery implements per-fd locks for MT-safety.  It is not
1207df0ccbaSfvdl  *      sufficient to do per-CLIENT handle locks for MT-safety because a
1217df0ccbaSfvdl  *      user may create more than one CLIENT handle with the same fd behind
122b47c7056Sandvar  *      it.  Therefore, we allocate an array of flags (vc_fd_locks), protected
1237df0ccbaSfvdl  *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
124b47c7056Sandvar  *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is active on some
1257df0ccbaSfvdl  *      CLIENT handle created for that fd.
1267df0ccbaSfvdl  *      The current implementation holds locks across the entire RPC and reply.
1277df0ccbaSfvdl  *      Yes, this is silly, and as soon as this code is proven to work, this
1287df0ccbaSfvdl  *      should be the first thing fixed.  One step at a time.
1297df0ccbaSfvdl  */
1303fdac2b8Sthorpej #ifdef _REENTRANT
1317df0ccbaSfvdl static int      *vc_fd_locks;
1323fdac2b8Sthorpej #define __rpc_lock_value __isthreaded;
1337df0ccbaSfvdl extern mutex_t  clnt_fd_lock;
1347df0ccbaSfvdl static cond_t   *vc_cv;
1357df0ccbaSfvdl #define release_fd_lock(fd, mask) {             \
1367df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);      \
1377df0ccbaSfvdl 	vc_fd_locks[fd] = 0;            \
1387df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);    \
139c9cdc302Schristos 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);        \
1407df0ccbaSfvdl 	cond_signal(&vc_cv[fd]);        \
1417df0ccbaSfvdl }
1427df0ccbaSfvdl #else
1437df0ccbaSfvdl #define release_fd_lock(fd,mask)
1447df0ccbaSfvdl #define __rpc_lock_value 0
1457df0ccbaSfvdl #endif
1467df0ccbaSfvdl 
14737a0afb7Schristos static __inline void
htonlp(void * dst,const void * src,uint32_t incr)1487dfb80e1Schristos htonlp(void *dst, const void *src, uint32_t incr)
14937a0afb7Schristos {
15037a0afb7Schristos #if 0
15137a0afb7Schristos 	uint32_t tmp;
15237a0afb7Schristos 	memcpy(&tmp, src, sizeof(tmp));
1537dfb80e1Schristos 	tmp = htonl(tmp + incr);
15437a0afb7Schristos 	memcpy(dst, &tmp, sizeof(tmp));
15537a0afb7Schristos #else
15637a0afb7Schristos 	/* We are aligned, so we think */
1577dfb80e1Schristos 	*(uint32_t *)dst = htonl(*(const uint32_t *)src + incr);
15837a0afb7Schristos #endif
15937a0afb7Schristos }
16037a0afb7Schristos 
16137a0afb7Schristos static __inline void
ntohlp(void * dst,const void * src)16237a0afb7Schristos ntohlp(void *dst, const void *src)
16337a0afb7Schristos {
16437a0afb7Schristos #if 0
16537a0afb7Schristos 	uint32_t tmp;
16637a0afb7Schristos 	memcpy(&tmp, src, sizeof(tmp));
16737a0afb7Schristos 	tmp = ntohl(tmp);
16837a0afb7Schristos 	memcpy(dst, &tmp, sizeof(tmp));
16937a0afb7Schristos #else
17037a0afb7Schristos 	/* We are aligned, so we think */
17137a0afb7Schristos 	*(uint32_t *)dst = htonl(*(const uint32_t *)src);
17237a0afb7Schristos #endif
17337a0afb7Schristos }
1747df0ccbaSfvdl 
1757df0ccbaSfvdl /*
1767df0ccbaSfvdl  * Create a client handle for a connection.
1777df0ccbaSfvdl  * Default options are set, which the user can change using clnt_control()'s.
1787df0ccbaSfvdl  * The rpc/vc package does buffering similar to stdio, so the client
1797df0ccbaSfvdl  * must pick send and receive buffer sizes, 0 => use the default.
1807df0ccbaSfvdl  * NB: fd is copied into a private area.
1817df0ccbaSfvdl  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
1827df0ccbaSfvdl  * set this something more useful.
1837df0ccbaSfvdl  *
1847df0ccbaSfvdl  * fd should be an open socket
1857df0ccbaSfvdl  */
1867df0ccbaSfvdl CLIENT *
clnt_vc_create(int fd,const struct netbuf * raddr,rpcprog_t prog,rpcvers_t vers,u_int sendsz,u_int recvsz)187e826745eSchristos clnt_vc_create(
188e826745eSchristos 	int fd,
189e826745eSchristos 	const struct netbuf *raddr,
190e826745eSchristos 	rpcprog_t prog,
191e826745eSchristos 	rpcvers_t vers,
192e826745eSchristos 	u_int sendsz,
193e826745eSchristos 	u_int recvsz
194e826745eSchristos )
1957df0ccbaSfvdl {
1967df0ccbaSfvdl 	CLIENT *h;
1977df0ccbaSfvdl 	struct ct_data *ct = NULL;
1987df0ccbaSfvdl 	struct rpc_msg call_msg;
1993fdac2b8Sthorpej #ifdef _REENTRANT
2007df0ccbaSfvdl 	sigset_t mask;
2017df0ccbaSfvdl #endif
2027df0ccbaSfvdl 	sigset_t newmask;
2037df0ccbaSfvdl 	struct sockaddr_storage ss;
2047df0ccbaSfvdl 	socklen_t slen;
2057df0ccbaSfvdl 	struct __rpc_sockinfo si;
2067df0ccbaSfvdl 
2070e8cfd8fSlukem 	_DIAGASSERT(raddr != NULL);
2080e8cfd8fSlukem 
209deb154d2Schristos 	h  = mem_alloc(sizeof(*h));
2107df0ccbaSfvdl 	if (h == NULL) {
2117df0ccbaSfvdl 		warnx("clnt_vc_create: out of memory");
2127df0ccbaSfvdl 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2137df0ccbaSfvdl 		rpc_createerr.cf_error.re_errno = errno;
2147df0ccbaSfvdl 		goto fooy;
2157df0ccbaSfvdl 	}
216deb154d2Schristos 	ct = mem_alloc(sizeof(*ct));
2177df0ccbaSfvdl 	if (ct == NULL) {
2187df0ccbaSfvdl 		warnx("clnt_vc_create: out of memory");
2197df0ccbaSfvdl 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2207df0ccbaSfvdl 		rpc_createerr.cf_error.re_errno = errno;
2217df0ccbaSfvdl 		goto fooy;
2227df0ccbaSfvdl 	}
2237df0ccbaSfvdl 
224e3584f0cSchristos 	__clnt_sigfillset(&newmask);
2257df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
2263fdac2b8Sthorpej #ifdef _REENTRANT
2277df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
228c9cdc302Schristos 	if (vc_fd_locks == NULL) {
2293fdac2b8Sthorpej 		size_t cv_allocsz, fd_allocsz;
2307df0ccbaSfvdl 		int dtbsize = __rpc_dtbsize();
2317df0ccbaSfvdl 
2327df0ccbaSfvdl 		fd_allocsz = dtbsize * sizeof (int);
233c9cdc302Schristos 		vc_fd_locks = mem_alloc(fd_allocsz);
234c9cdc302Schristos 		if (vc_fd_locks == NULL) {
235e3584f0cSchristos 			goto blooy;
2367df0ccbaSfvdl 		} else
2377df0ccbaSfvdl 			memset(vc_fd_locks, '\0', fd_allocsz);
2387df0ccbaSfvdl 
23930399256Suebayasi 		_DIAGASSERT(vc_cv == NULL);
2407df0ccbaSfvdl 		cv_allocsz = dtbsize * sizeof (cond_t);
241c9cdc302Schristos 		vc_cv = mem_alloc(cv_allocsz);
242c9cdc302Schristos 		if (vc_cv == NULL) {
2437df0ccbaSfvdl 			mem_free(vc_fd_locks, fd_allocsz);
244c9cdc302Schristos 			vc_fd_locks = NULL;
245e3584f0cSchristos 			goto blooy;
2467df0ccbaSfvdl 		} else {
2477df0ccbaSfvdl 			int i;
2487df0ccbaSfvdl 
2497df0ccbaSfvdl 			for (i = 0; i < dtbsize; i++)
2507df0ccbaSfvdl 				cond_init(&vc_cv[i], 0, (void *) 0);
2517df0ccbaSfvdl 		}
2527df0ccbaSfvdl 	} else
25330399256Suebayasi 		_DIAGASSERT(vc_cv != NULL);
2547df0ccbaSfvdl #endif
2557df0ccbaSfvdl 
2567df0ccbaSfvdl 	/*
2577df0ccbaSfvdl 	 * XXX - fvdl connecting while holding a mutex?
2587df0ccbaSfvdl 	 */
2597df0ccbaSfvdl 	slen = sizeof ss;
260deb154d2Schristos 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
2617df0ccbaSfvdl 		if (errno != ENOTCONN) {
2627df0ccbaSfvdl 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2637df0ccbaSfvdl 			rpc_createerr.cf_error.re_errno = errno;
264e3584f0cSchristos 			goto blooy;
2657df0ccbaSfvdl 		}
2667df0ccbaSfvdl 		if (connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
2677df0ccbaSfvdl 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2687df0ccbaSfvdl 			rpc_createerr.cf_error.re_errno = errno;
269e3584f0cSchristos 			goto blooy;
2707df0ccbaSfvdl 		}
2717df0ccbaSfvdl 	}
2727df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
273a781fb5fSdrochner 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
2747df0ccbaSfvdl 	if (!__rpc_fd2sockinfo(fd, &si))
2757df0ccbaSfvdl 		goto fooy;
2767df0ccbaSfvdl 
2777df0ccbaSfvdl 	ct->ct_closeit = FALSE;
2787df0ccbaSfvdl 
2797df0ccbaSfvdl 	/*
2807df0ccbaSfvdl 	 * Set up private data struct
2817df0ccbaSfvdl 	 */
2827df0ccbaSfvdl 	ct->ct_fd = fd;
2837df0ccbaSfvdl 	ct->ct_wait.tv_usec = 0;
2847df0ccbaSfvdl 	ct->ct_waitset = FALSE;
2851466914fSchristos 	ct->ct_addr.buf = malloc((size_t)raddr->maxlen);
2867df0ccbaSfvdl 	if (ct->ct_addr.buf == NULL)
2877df0ccbaSfvdl 		goto fooy;
288b973ac40Schristos 	memcpy(ct->ct_addr.buf, raddr->buf, (size_t)raddr->len);
289b973ac40Schristos 	ct->ct_addr.len = raddr->len;
2907df0ccbaSfvdl 	ct->ct_addr.maxlen = raddr->maxlen;
2917df0ccbaSfvdl 
2927df0ccbaSfvdl 	/*
2937df0ccbaSfvdl 	 * Initialize call message
2947df0ccbaSfvdl 	 */
2958b08fa0dSitojun 	call_msg.rm_xid = __RPC_GETXID();
2967df0ccbaSfvdl 	call_msg.rm_direction = CALL;
2977df0ccbaSfvdl 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
2987df0ccbaSfvdl 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
2997df0ccbaSfvdl 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
3007df0ccbaSfvdl 
3017df0ccbaSfvdl 	/*
3027df0ccbaSfvdl 	 * pre-serialize the static part of the call msg and stash it away
3037df0ccbaSfvdl 	 */
3047df0ccbaSfvdl 	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
3057df0ccbaSfvdl 	    XDR_ENCODE);
3067df0ccbaSfvdl 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
3077df0ccbaSfvdl 		if (ct->ct_closeit) {
3087df0ccbaSfvdl 			(void)close(fd);
3097df0ccbaSfvdl 		}
3107df0ccbaSfvdl 		goto fooy;
3117df0ccbaSfvdl 	}
3127df0ccbaSfvdl 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
3137df0ccbaSfvdl 	XDR_DESTROY(&(ct->ct_xdrs));
3147df0ccbaSfvdl 
3157df0ccbaSfvdl 	/*
3167df0ccbaSfvdl 	 * Create a client handle which uses xdrrec for serialization
3177df0ccbaSfvdl 	 * and authnone for authentication.
3187df0ccbaSfvdl 	 */
3197df0ccbaSfvdl 	h->cl_ops = clnt_vc_ops();
3207df0ccbaSfvdl 	h->cl_private = ct;
3217df0ccbaSfvdl 	h->cl_auth = authnone_create();
3227df0ccbaSfvdl 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
3237df0ccbaSfvdl 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
3247df0ccbaSfvdl 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
3257df0ccbaSfvdl 	    h->cl_private, read_vc, write_vc);
3267df0ccbaSfvdl 	return (h);
3277df0ccbaSfvdl 
328e3584f0cSchristos blooy:
329e3584f0cSchristos 	mutex_unlock(&clnt_fd_lock);
330e3584f0cSchristos 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
3317df0ccbaSfvdl fooy:
3327df0ccbaSfvdl 	/*
3337df0ccbaSfvdl 	 * Something goofed, free stuff and barf
3347df0ccbaSfvdl 	 */
3357df0ccbaSfvdl 	if (ct)
3367df0ccbaSfvdl 		mem_free(ct, sizeof(struct ct_data));
3377df0ccbaSfvdl 	if (h)
3387df0ccbaSfvdl 		mem_free(h, sizeof(CLIENT));
339deb154d2Schristos 	return (NULL);
3407df0ccbaSfvdl }
3417df0ccbaSfvdl 
3427df0ccbaSfvdl static enum clnt_stat
clnt_vc_call(CLIENT * h,rpcproc_t proc,xdrproc_t xdr_args,const char * args_ptr,xdrproc_t xdr_results,caddr_t results_ptr,struct timeval timeout)343e826745eSchristos clnt_vc_call(
344e826745eSchristos 	CLIENT *h,
345e826745eSchristos 	rpcproc_t proc,
346e826745eSchristos 	xdrproc_t xdr_args,
347e826745eSchristos 	const char *args_ptr,
348e826745eSchristos 	xdrproc_t xdr_results,
349e826745eSchristos 	caddr_t results_ptr,
350e826745eSchristos 	struct timeval timeout
351e826745eSchristos )
3527df0ccbaSfvdl {
3537df0ccbaSfvdl 	struct ct_data *ct;
3547df0ccbaSfvdl 	XDR *xdrs;
3557df0ccbaSfvdl 	struct rpc_msg reply_msg;
3567df0ccbaSfvdl 	u_int32_t x_id;
3577df0ccbaSfvdl 	u_int32_t *msg_x_id;
3587df0ccbaSfvdl 	bool_t shipnow;
3597df0ccbaSfvdl 	int refreshes = 2;
3603fdac2b8Sthorpej #ifdef _REENTRANT
3617df0ccbaSfvdl 	sigset_t mask, newmask;
3627df0ccbaSfvdl #endif
3637df0ccbaSfvdl 
3647df0ccbaSfvdl 	_DIAGASSERT(h != NULL);
3657df0ccbaSfvdl 
3663fdac2b8Sthorpej 	ct = (struct ct_data *) h->cl_private;
3673fdac2b8Sthorpej 
3683fdac2b8Sthorpej #ifdef _REENTRANT
369e3584f0cSchristos 	__clnt_sigfillset(&newmask);
3707df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
3717df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
3727df0ccbaSfvdl 	while (vc_fd_locks[ct->ct_fd])
3737df0ccbaSfvdl 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
3743fdac2b8Sthorpej 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
3757df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
3767df0ccbaSfvdl #endif
3777df0ccbaSfvdl 
3787df0ccbaSfvdl 	xdrs = &(ct->ct_xdrs);
3797df0ccbaSfvdl 	msg_x_id = &ct->ct_u.ct_mcalli;
3807df0ccbaSfvdl 
3817df0ccbaSfvdl 	if (!ct->ct_waitset) {
3827df0ccbaSfvdl 		if (time_not_ok(&timeout) == FALSE)
3837df0ccbaSfvdl 		ct->ct_wait = timeout;
3847df0ccbaSfvdl 	}
3857df0ccbaSfvdl 
3867df0ccbaSfvdl 	shipnow =
387deb154d2Schristos 	    (xdr_results == NULL && timeout.tv_sec == 0
3887df0ccbaSfvdl 	    && timeout.tv_usec == 0) ? FALSE : TRUE;
3897df0ccbaSfvdl 
3907df0ccbaSfvdl call_again:
3917df0ccbaSfvdl 	xdrs->x_op = XDR_ENCODE;
3927df0ccbaSfvdl 	ct->ct_error.re_status = RPC_SUCCESS;
3937df0ccbaSfvdl 	x_id = ntohl(--(*msg_x_id));
3947df0ccbaSfvdl 	if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
395128bd71fSchristos 	    (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
3967df0ccbaSfvdl 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
397347f995cSyamt 	    (! (*xdr_args)(xdrs, __UNCONST(args_ptr)))) {
3987df0ccbaSfvdl 		if (ct->ct_error.re_status == RPC_SUCCESS)
3997df0ccbaSfvdl 			ct->ct_error.re_status = RPC_CANTENCODEARGS;
4007df0ccbaSfvdl 		(void)xdrrec_endofrecord(xdrs, TRUE);
4017df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
4027df0ccbaSfvdl 		return (ct->ct_error.re_status);
4037df0ccbaSfvdl 	}
4047df0ccbaSfvdl 	if (! xdrrec_endofrecord(xdrs, shipnow)) {
4057df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
4067df0ccbaSfvdl 		return (ct->ct_error.re_status = RPC_CANTSEND);
4077df0ccbaSfvdl 	}
4087df0ccbaSfvdl 	if (! shipnow) {
4097df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
4107df0ccbaSfvdl 		return (RPC_SUCCESS);
4117df0ccbaSfvdl 	}
4127df0ccbaSfvdl 	/*
4137df0ccbaSfvdl 	 * Hack to provide rpc-based message passing
4147df0ccbaSfvdl 	 */
4157df0ccbaSfvdl 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
4167df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
4177df0ccbaSfvdl 		return(ct->ct_error.re_status = RPC_TIMEDOUT);
4187df0ccbaSfvdl 	}
4197df0ccbaSfvdl 
4207df0ccbaSfvdl 
4217df0ccbaSfvdl 	/*
4227df0ccbaSfvdl 	 * Keep receiving until we get a valid transaction id
4237df0ccbaSfvdl 	 */
4247df0ccbaSfvdl 	xdrs->x_op = XDR_DECODE;
4257df0ccbaSfvdl 	for (;;) {
4267df0ccbaSfvdl 		reply_msg.acpted_rply.ar_verf = _null_auth;
4277df0ccbaSfvdl 		reply_msg.acpted_rply.ar_results.where = NULL;
4287df0ccbaSfvdl 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
4297df0ccbaSfvdl 		if (! xdrrec_skiprecord(xdrs)) {
4307df0ccbaSfvdl 			release_fd_lock(ct->ct_fd, mask);
4317df0ccbaSfvdl 			return (ct->ct_error.re_status);
4327df0ccbaSfvdl 		}
4337df0ccbaSfvdl 		/* now decode and validate the response header */
4347df0ccbaSfvdl 		if (! xdr_replymsg(xdrs, &reply_msg)) {
4357df0ccbaSfvdl 			if (ct->ct_error.re_status == RPC_SUCCESS)
4367df0ccbaSfvdl 				continue;
4377df0ccbaSfvdl 			release_fd_lock(ct->ct_fd, mask);
4387df0ccbaSfvdl 			return (ct->ct_error.re_status);
4397df0ccbaSfvdl 		}
4407df0ccbaSfvdl 		if (reply_msg.rm_xid == x_id)
4417df0ccbaSfvdl 			break;
4427df0ccbaSfvdl 	}
4437df0ccbaSfvdl 
4447df0ccbaSfvdl 	/*
4457df0ccbaSfvdl 	 * process header
4467df0ccbaSfvdl 	 */
4477df0ccbaSfvdl 	_seterr_reply(&reply_msg, &(ct->ct_error));
4487df0ccbaSfvdl 	if (ct->ct_error.re_status == RPC_SUCCESS) {
4497df0ccbaSfvdl 		if (! AUTH_VALIDATE(h->cl_auth,
4507df0ccbaSfvdl 		    &reply_msg.acpted_rply.ar_verf)) {
4517df0ccbaSfvdl 			ct->ct_error.re_status = RPC_AUTHERROR;
4527df0ccbaSfvdl 			ct->ct_error.re_why = AUTH_INVALIDRESP;
4537df0ccbaSfvdl 		} else if (! (*xdr_results)(xdrs, results_ptr)) {
4547df0ccbaSfvdl 			if (ct->ct_error.re_status == RPC_SUCCESS)
4557df0ccbaSfvdl 				ct->ct_error.re_status = RPC_CANTDECODERES;
4567df0ccbaSfvdl 		}
4577df0ccbaSfvdl 		/* free verifier ... */
4587df0ccbaSfvdl 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
4597df0ccbaSfvdl 			xdrs->x_op = XDR_FREE;
4607df0ccbaSfvdl 			(void)xdr_opaque_auth(xdrs,
4617df0ccbaSfvdl 			    &(reply_msg.acpted_rply.ar_verf));
4627df0ccbaSfvdl 		}
4637df0ccbaSfvdl 	}  /* end successful completion */
4647df0ccbaSfvdl 	else {
4657df0ccbaSfvdl 		/* maybe our credentials need to be refreshed ... */
4667df0ccbaSfvdl 		if (refreshes-- && AUTH_REFRESH(h->cl_auth))
4677df0ccbaSfvdl 			goto call_again;
4687df0ccbaSfvdl 	}  /* end of unsuccessful completion */
4697df0ccbaSfvdl 	release_fd_lock(ct->ct_fd, mask);
4707df0ccbaSfvdl 	return (ct->ct_error.re_status);
4717df0ccbaSfvdl }
4727df0ccbaSfvdl 
4737df0ccbaSfvdl static void
clnt_vc_geterr(CLIENT * h,struct rpc_err * errp)474e826745eSchristos clnt_vc_geterr(
475e826745eSchristos 	CLIENT *h,
476e826745eSchristos 	struct rpc_err *errp
477e826745eSchristos )
4787df0ccbaSfvdl {
4797df0ccbaSfvdl 	struct ct_data *ct;
4807df0ccbaSfvdl 
4817df0ccbaSfvdl 	_DIAGASSERT(h != NULL);
4827df0ccbaSfvdl 	_DIAGASSERT(errp != NULL);
4837df0ccbaSfvdl 
4847df0ccbaSfvdl 	ct = (struct ct_data *) h->cl_private;
4857df0ccbaSfvdl 	*errp = ct->ct_error;
4867df0ccbaSfvdl }
4877df0ccbaSfvdl 
4887df0ccbaSfvdl static bool_t
clnt_vc_freeres(CLIENT * cl,xdrproc_t xdr_res,caddr_t res_ptr)489e826745eSchristos clnt_vc_freeres(
490e826745eSchristos 	CLIENT *cl,
491e826745eSchristos 	xdrproc_t xdr_res,
492e826745eSchristos 	caddr_t res_ptr
493e826745eSchristos )
4947df0ccbaSfvdl {
4957df0ccbaSfvdl 	struct ct_data *ct;
4967df0ccbaSfvdl 	XDR *xdrs;
4977df0ccbaSfvdl 	bool_t dummy;
4983fdac2b8Sthorpej #ifdef _REENTRANT
4997df0ccbaSfvdl 	sigset_t mask;
5007df0ccbaSfvdl #endif
5017df0ccbaSfvdl 	sigset_t newmask;
5027df0ccbaSfvdl 
5037df0ccbaSfvdl 	_DIAGASSERT(cl != NULL);
5047df0ccbaSfvdl 
5057df0ccbaSfvdl 	ct = (struct ct_data *)cl->cl_private;
5067df0ccbaSfvdl 	xdrs = &(ct->ct_xdrs);
5077df0ccbaSfvdl 
508e3584f0cSchristos 	__clnt_sigfillset(&newmask);
5097df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
5107df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
5113fdac2b8Sthorpej #ifdef _REENTRANT
5127df0ccbaSfvdl 	while (vc_fd_locks[ct->ct_fd])
5137df0ccbaSfvdl 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
5147df0ccbaSfvdl #endif
5157df0ccbaSfvdl 
5167df0ccbaSfvdl 	xdrs->x_op = XDR_FREE;
5177df0ccbaSfvdl 	dummy = (*xdr_res)(xdrs, res_ptr);
5187df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
5197df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
5207df0ccbaSfvdl 	cond_signal(&vc_cv[ct->ct_fd]);
5217df0ccbaSfvdl 
5227df0ccbaSfvdl 	return dummy;
5237df0ccbaSfvdl }
5247df0ccbaSfvdl 
5257df0ccbaSfvdl /*ARGSUSED*/
5267df0ccbaSfvdl static void
clnt_vc_abort(CLIENT * cl)527e826745eSchristos clnt_vc_abort(CLIENT *cl)
5287df0ccbaSfvdl {
5297df0ccbaSfvdl }
5307df0ccbaSfvdl 
5317df0ccbaSfvdl static bool_t
clnt_vc_control(CLIENT * cl,u_int request,char * info)532e826745eSchristos clnt_vc_control(
533e826745eSchristos 	CLIENT *cl,
534e826745eSchristos 	u_int request,
535e826745eSchristos 	char *info
536e826745eSchristos )
5377df0ccbaSfvdl {
5387df0ccbaSfvdl 	struct ct_data *ct;
5397df0ccbaSfvdl 	void *infop = info;
5403fdac2b8Sthorpej #ifdef _REENTRANT
5417df0ccbaSfvdl 	sigset_t mask;
5427df0ccbaSfvdl #endif
5437df0ccbaSfvdl 	sigset_t newmask;
5447df0ccbaSfvdl 
5457df0ccbaSfvdl 	_DIAGASSERT(cl != NULL);
5467df0ccbaSfvdl 
5477df0ccbaSfvdl 	ct = (struct ct_data *)cl->cl_private;
5487df0ccbaSfvdl 
549e3584f0cSchristos 	__clnt_sigfillset(&newmask);
5507df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
5517df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
5523fdac2b8Sthorpej #ifdef _REENTRANT
5537df0ccbaSfvdl 	while (vc_fd_locks[ct->ct_fd])
5547df0ccbaSfvdl 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
5557df0ccbaSfvdl 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
5567df0ccbaSfvdl #endif
5577df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
5587df0ccbaSfvdl 
5597df0ccbaSfvdl 	switch (request) {
5607df0ccbaSfvdl 	case CLSET_FD_CLOSE:
5617df0ccbaSfvdl 		ct->ct_closeit = TRUE;
5627df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
5637df0ccbaSfvdl 		return (TRUE);
5647df0ccbaSfvdl 	case CLSET_FD_NCLOSE:
5657df0ccbaSfvdl 		ct->ct_closeit = FALSE;
5667df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
5677df0ccbaSfvdl 		return (TRUE);
5687df0ccbaSfvdl 	default:
5697df0ccbaSfvdl 		break;
5707df0ccbaSfvdl 	}
5717df0ccbaSfvdl 
5727df0ccbaSfvdl 	/* for other requests which use info */
5737df0ccbaSfvdl 	if (info == NULL) {
5747df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
5757df0ccbaSfvdl 		return (FALSE);
5767df0ccbaSfvdl 	}
5777df0ccbaSfvdl 	switch (request) {
5787df0ccbaSfvdl 	case CLSET_TIMEOUT:
579deb154d2Schristos 		if (time_not_ok((struct timeval *)(void *)info)) {
5807df0ccbaSfvdl 			release_fd_lock(ct->ct_fd, mask);
5817df0ccbaSfvdl 			return (FALSE);
5827df0ccbaSfvdl 		}
5837df0ccbaSfvdl 		ct->ct_wait = *(struct timeval *)infop;
5847df0ccbaSfvdl 		ct->ct_waitset = TRUE;
5857df0ccbaSfvdl 		break;
5867df0ccbaSfvdl 	case CLGET_TIMEOUT:
5877df0ccbaSfvdl 		*(struct timeval *)infop = ct->ct_wait;
5887df0ccbaSfvdl 		break;
5897df0ccbaSfvdl 	case CLGET_SERVER_ADDR:
590deb154d2Schristos 		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
5917df0ccbaSfvdl 		break;
5927df0ccbaSfvdl 	case CLGET_FD:
593deb154d2Schristos 		*(int *)(void *)info = ct->ct_fd;
5947df0ccbaSfvdl 		break;
5957df0ccbaSfvdl 	case CLGET_SVC_ADDR:
5967df0ccbaSfvdl 		/* The caller should not free this memory area */
597deb154d2Schristos 		*(struct netbuf *)(void *)info = ct->ct_addr;
5987df0ccbaSfvdl 		break;
5997df0ccbaSfvdl 	case CLSET_SVC_ADDR:		/* set to new address */
6007df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
6017df0ccbaSfvdl 		return (FALSE);
6027df0ccbaSfvdl 	case CLGET_XID:
6037df0ccbaSfvdl 		/*
6047df0ccbaSfvdl 		 * use the knowledge that xid is the
6057df0ccbaSfvdl 		 * first element in the call structure
6067df0ccbaSfvdl 		 * This will get the xid of the PREVIOUS call
6077df0ccbaSfvdl 		 */
60837a0afb7Schristos 		ntohlp(info, &ct->ct_u.ct_mcalli);
6097df0ccbaSfvdl 		break;
6107df0ccbaSfvdl 	case CLSET_XID:
6117df0ccbaSfvdl 		/* This will set the xid of the NEXT call */
6127df0ccbaSfvdl 		/* increment by 1 as clnt_vc_call() decrements once */
6137dfb80e1Schristos 		htonlp(&ct->ct_u.ct_mcalli, info, 1);
6147df0ccbaSfvdl 		break;
6157df0ccbaSfvdl 	case CLGET_VERS:
6167df0ccbaSfvdl 		/*
6177df0ccbaSfvdl 		 * This RELIES on the information that, in the call body,
6187df0ccbaSfvdl 		 * the version number field is the fifth field from the
619*0e6570fcSandvar 		 * beginning of the RPC header. MUST be changed if the
6207df0ccbaSfvdl 		 * call_struct is changed
6217df0ccbaSfvdl 		 */
62237a0afb7Schristos 		ntohlp(info, ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT);
6237df0ccbaSfvdl 		break;
6247df0ccbaSfvdl 
6257df0ccbaSfvdl 	case CLSET_VERS:
6267dfb80e1Schristos 		htonlp(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT, info, 0);
6277df0ccbaSfvdl 		break;
6287df0ccbaSfvdl 
6297df0ccbaSfvdl 	case CLGET_PROG:
6307df0ccbaSfvdl 		/*
6317df0ccbaSfvdl 		 * This RELIES on the information that, in the call body,
6327df0ccbaSfvdl 		 * the program number field is the fourth field from the
633*0e6570fcSandvar 		 * beginning of the RPC header. MUST be changed if the
6347df0ccbaSfvdl 		 * call_struct is changed
6357df0ccbaSfvdl 		 */
63637a0afb7Schristos 		ntohlp(info, ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT);
6377df0ccbaSfvdl 		break;
6387df0ccbaSfvdl 
6397df0ccbaSfvdl 	case CLSET_PROG:
6407dfb80e1Schristos 		htonlp(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT, info, 0);
6417df0ccbaSfvdl 		break;
6427df0ccbaSfvdl 
6437df0ccbaSfvdl 	default:
6447df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
6457df0ccbaSfvdl 		return (FALSE);
6467df0ccbaSfvdl 	}
6477df0ccbaSfvdl 	release_fd_lock(ct->ct_fd, mask);
6487df0ccbaSfvdl 	return (TRUE);
6497df0ccbaSfvdl }
6507df0ccbaSfvdl 
6517df0ccbaSfvdl 
6527df0ccbaSfvdl static void
clnt_vc_destroy(CLIENT * cl)653e826745eSchristos clnt_vc_destroy(CLIENT *cl)
6547df0ccbaSfvdl {
6557df0ccbaSfvdl 	struct ct_data *ct;
6563fdac2b8Sthorpej #ifdef _REENTRANT
6573fdac2b8Sthorpej 	int ct_fd;
6587df0ccbaSfvdl 	sigset_t mask;
6597df0ccbaSfvdl #endif
6607df0ccbaSfvdl 	sigset_t newmask;
6617df0ccbaSfvdl 
6627df0ccbaSfvdl 	_DIAGASSERT(cl != NULL);
6637df0ccbaSfvdl 
6647df0ccbaSfvdl 	ct = (struct ct_data *) cl->cl_private;
6657df0ccbaSfvdl 
666e3584f0cSchristos 	__clnt_sigfillset(&newmask);
6677df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
6687df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
6693fdac2b8Sthorpej #ifdef _REENTRANT
67023f19850Schristos 	ct_fd = ct->ct_fd;
6717df0ccbaSfvdl 	while (vc_fd_locks[ct_fd])
6727df0ccbaSfvdl 		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
6737df0ccbaSfvdl #endif
6747df0ccbaSfvdl 	if (ct->ct_closeit && ct->ct_fd != -1) {
6757df0ccbaSfvdl 		(void)close(ct->ct_fd);
6767df0ccbaSfvdl 	}
6777df0ccbaSfvdl 	XDR_DESTROY(&(ct->ct_xdrs));
6787df0ccbaSfvdl 	if (ct->ct_addr.buf)
6797df0ccbaSfvdl 		free(ct->ct_addr.buf);
6807df0ccbaSfvdl 	mem_free(ct, sizeof(struct ct_data));
6817df0ccbaSfvdl 	mem_free(cl, sizeof(CLIENT));
6827df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
6837df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
6847df0ccbaSfvdl 
6857df0ccbaSfvdl 	cond_signal(&vc_cv[ct_fd]);
6867df0ccbaSfvdl }
6877df0ccbaSfvdl 
6887df0ccbaSfvdl /*
6897df0ccbaSfvdl  * Interface between xdr serializer and tcp connection.
6907df0ccbaSfvdl  * Behaves like the system calls, read & write, but keeps some error state
6917df0ccbaSfvdl  * around for the rpc level.
6927df0ccbaSfvdl  */
6937df0ccbaSfvdl static int
read_vc(char * ctp,char * buf,int len)694e826745eSchristos read_vc(char *ctp, char *buf, int len)
6957df0ccbaSfvdl {
6967df0ccbaSfvdl 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
6977df0ccbaSfvdl 	struct pollfd fd;
69868f654dcSchristos 	struct timespec ts;
699e826745eSchristos 	ssize_t nread;
7007df0ccbaSfvdl 
7017df0ccbaSfvdl 	if (len == 0)
7027df0ccbaSfvdl 		return (0);
70368f654dcSchristos 
70468f654dcSchristos 	TIMEVAL_TO_TIMESPEC(&ct->ct_wait, &ts);
7057df0ccbaSfvdl 	fd.fd = ct->ct_fd;
7067df0ccbaSfvdl 	fd.events = POLLIN;
7077df0ccbaSfvdl 	for (;;) {
70868f654dcSchristos 		switch (pollts(&fd, 1, &ts, NULL)) {
7097df0ccbaSfvdl 		case 0:
7107df0ccbaSfvdl 			ct->ct_error.re_status = RPC_TIMEDOUT;
7117df0ccbaSfvdl 			return (-1);
7127df0ccbaSfvdl 
7137df0ccbaSfvdl 		case -1:
7147df0ccbaSfvdl 			if (errno == EINTR)
7157df0ccbaSfvdl 				continue;
7167df0ccbaSfvdl 			ct->ct_error.re_status = RPC_CANTRECV;
7177df0ccbaSfvdl 			ct->ct_error.re_errno = errno;
7187df0ccbaSfvdl 			return (-1);
7197df0ccbaSfvdl 		}
7207df0ccbaSfvdl 		break;
7217df0ccbaSfvdl 	}
722e826745eSchristos 	switch (nread = read(ct->ct_fd, buf, (size_t)len)) {
7237df0ccbaSfvdl 
7247df0ccbaSfvdl 	case 0:
7257df0ccbaSfvdl 		/* premature eof */
7267df0ccbaSfvdl 		ct->ct_error.re_errno = ECONNRESET;
7277df0ccbaSfvdl 		ct->ct_error.re_status = RPC_CANTRECV;
7284e3724b4Schristos 		nread = -1;  /* it's really an error */
7297df0ccbaSfvdl 		break;
7307df0ccbaSfvdl 
7317df0ccbaSfvdl 	case -1:
7327df0ccbaSfvdl 		ct->ct_error.re_errno = errno;
7337df0ccbaSfvdl 		ct->ct_error.re_status = RPC_CANTRECV;
7347df0ccbaSfvdl 		break;
7357df0ccbaSfvdl 	}
736e826745eSchristos 	return (int)nread;
7377df0ccbaSfvdl }
7387df0ccbaSfvdl 
7397df0ccbaSfvdl static int
write_vc(char * ctp,char * buf,int len)740e826745eSchristos write_vc(char *ctp, char *buf, int len)
7417df0ccbaSfvdl {
7427df0ccbaSfvdl 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
743e826745eSchristos 	ssize_t i;
744e826745eSchristos 	size_t cnt;
7457df0ccbaSfvdl 
7467df0ccbaSfvdl 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
747e826745eSchristos 		if ((i = write(ct->ct_fd, buf, cnt)) == -1) {
7487df0ccbaSfvdl 			ct->ct_error.re_errno = errno;
7497df0ccbaSfvdl 			ct->ct_error.re_status = RPC_CANTSEND;
7507df0ccbaSfvdl 			return (-1);
7517df0ccbaSfvdl 		}
7527df0ccbaSfvdl 	}
753e826745eSchristos 	return len;
7547df0ccbaSfvdl }
7557df0ccbaSfvdl 
7567df0ccbaSfvdl static struct clnt_ops *
clnt_vc_ops(void)757e826745eSchristos clnt_vc_ops(void)
7587df0ccbaSfvdl {
7597df0ccbaSfvdl 	static struct clnt_ops ops;
7603fdac2b8Sthorpej #ifdef _REENTRANT
7617df0ccbaSfvdl 	extern mutex_t  ops_lock;
7627df0ccbaSfvdl 	sigset_t mask;
7637df0ccbaSfvdl #endif
7647df0ccbaSfvdl 	sigset_t newmask;
7657df0ccbaSfvdl 
7667df0ccbaSfvdl 	/* VARIABLES PROTECTED BY ops_lock: ops */
7677df0ccbaSfvdl 
768e3584f0cSchristos 	__clnt_sigfillset(&newmask);
7697df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
7707df0ccbaSfvdl 	mutex_lock(&ops_lock);
7717df0ccbaSfvdl 	if (ops.cl_call == NULL) {
7727df0ccbaSfvdl 		ops.cl_call = clnt_vc_call;
7737df0ccbaSfvdl 		ops.cl_abort = clnt_vc_abort;
7747df0ccbaSfvdl 		ops.cl_geterr = clnt_vc_geterr;
7757df0ccbaSfvdl 		ops.cl_freeres = clnt_vc_freeres;
7767df0ccbaSfvdl 		ops.cl_destroy = clnt_vc_destroy;
7777df0ccbaSfvdl 		ops.cl_control = clnt_vc_control;
7787df0ccbaSfvdl 	}
7797df0ccbaSfvdl 	mutex_unlock(&ops_lock);
7807df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
7817df0ccbaSfvdl 	return (&ops);
7827df0ccbaSfvdl }
7837df0ccbaSfvdl 
7847df0ccbaSfvdl /*
7857df0ccbaSfvdl  * Make sure that the time is not garbage.   -1 value is disallowed.
7867df0ccbaSfvdl  * Note this is different from time_not_ok in clnt_dg.c
7877df0ccbaSfvdl  */
7887df0ccbaSfvdl static bool_t
time_not_ok(struct timeval * t)789e826745eSchristos time_not_ok(struct timeval *t)
7907df0ccbaSfvdl {
7910e8cfd8fSlukem 
7920e8cfd8fSlukem 	_DIAGASSERT(t != NULL);
7930e8cfd8fSlukem 
7947df0ccbaSfvdl 	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
7957df0ccbaSfvdl 		t->tv_usec <= -1 || t->tv_usec > 1000000);
7967df0ccbaSfvdl }
797