xref: /netbsd-src/lib/libc/rpc/clnt_vc.c (revision 85f5e301f5b0e5c7695c7222d16769f8f55f6aeb)
1*85f5e301Schristos /*	$NetBSD: clnt_vc.c,v 1.29 2024/01/23 17:24:38 christos Exp $	*/
27df0ccbaSfvdl 
37df0ccbaSfvdl /*
447c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
57df0ccbaSfvdl  *
647c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
747c0e0c3Stron  * modification, are permitted provided that the following conditions are
847c0e0c3Stron  * met:
97df0ccbaSfvdl  *
1047c0e0c3Stron  *     * Redistributions of source code must retain the above copyright
1147c0e0c3Stron  *       notice, this list of conditions and the following disclaimer.
1247c0e0c3Stron  *     * Redistributions in binary form must reproduce the above
1347c0e0c3Stron  *       copyright notice, this list of conditions and the following
1447c0e0c3Stron  *       disclaimer in the documentation and/or other materials
1547c0e0c3Stron  *       provided with the distribution.
1647c0e0c3Stron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
1747c0e0c3Stron  *       contributors may be used to endorse or promote products derived
1847c0e0c3Stron  *       from this software without specific prior written permission.
197df0ccbaSfvdl  *
2047c0e0c3Stron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2147c0e0c3Stron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2247c0e0c3Stron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2347c0e0c3Stron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2447c0e0c3Stron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2547c0e0c3Stron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2647c0e0c3Stron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2747c0e0c3Stron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2847c0e0c3Stron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2947c0e0c3Stron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3047c0e0c3Stron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3147c0e0c3Stron  *   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*85f5e301Schristos __RCSID("$NetBSD: clnt_vc.c,v 1.29 2024/01/23 17:24:38 christos 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 
822dec884dSchristos #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 
91c5e820caSchristos static enum clnt_stat clnt_vc_call(CLIENT *, rpcproc_t, xdrproc_t,
92c5e820caSchristos     const char *, xdrproc_t, caddr_t, struct timeval);
93c5e820caSchristos static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
94c5e820caSchristos static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, caddr_t);
95c5e820caSchristos static void clnt_vc_abort(CLIENT *);
96c5e820caSchristos static bool_t clnt_vc_control(CLIENT *, u_int, char *);
97c5e820caSchristos static void clnt_vc_destroy(CLIENT *);
98c5e820caSchristos static struct clnt_ops *clnt_vc_ops(void);
99c5e820caSchristos static bool_t time_not_ok(struct timeval *);
100c5e820caSchristos static int read_vc(caddr_t, caddr_t, int);
101c5e820caSchristos 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
122c69f42d3Sandvar  *      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
124c69f42d3Sandvar  *      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 static cond_t   *vc_cv;
1347df0ccbaSfvdl #define release_fd_lock(fd, mask) {             \
1357df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);      \
1367df0ccbaSfvdl 	vc_fd_locks[fd] = 0;            \
1377df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);    \
138c9cdc302Schristos 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);        \
1397df0ccbaSfvdl 	cond_signal(&vc_cv[fd]);        \
1407df0ccbaSfvdl }
1417df0ccbaSfvdl #else
1427df0ccbaSfvdl #define release_fd_lock(fd,mask)
1437df0ccbaSfvdl #define __rpc_lock_value 0
1447df0ccbaSfvdl #endif
1457df0ccbaSfvdl 
146fc8781baSchristos static __inline void
htonlp(void * dst,const void * src,uint32_t incr)147002a6c32Schristos htonlp(void *dst, const void *src, uint32_t incr)
148fc8781baSchristos {
149fc8781baSchristos #if 0
150fc8781baSchristos 	uint32_t tmp;
151fc8781baSchristos 	memcpy(&tmp, src, sizeof(tmp));
152002a6c32Schristos 	tmp = htonl(tmp + incr);
153fc8781baSchristos 	memcpy(dst, &tmp, sizeof(tmp));
154fc8781baSchristos #else
155fc8781baSchristos 	/* We are aligned, so we think */
156002a6c32Schristos 	*(uint32_t *)dst = htonl(*(const uint32_t *)src + incr);
157fc8781baSchristos #endif
158fc8781baSchristos }
159fc8781baSchristos 
160fc8781baSchristos static __inline void
ntohlp(void * dst,const void * src)161fc8781baSchristos ntohlp(void *dst, const void *src)
162fc8781baSchristos {
163fc8781baSchristos #if 0
164fc8781baSchristos 	uint32_t tmp;
165fc8781baSchristos 	memcpy(&tmp, src, sizeof(tmp));
166fc8781baSchristos 	tmp = ntohl(tmp);
167fc8781baSchristos 	memcpy(dst, &tmp, sizeof(tmp));
168fc8781baSchristos #else
169fc8781baSchristos 	/* We are aligned, so we think */
170fc8781baSchristos 	*(uint32_t *)dst = htonl(*(const uint32_t *)src);
171fc8781baSchristos #endif
172fc8781baSchristos }
1737df0ccbaSfvdl 
1747df0ccbaSfvdl /*
1757df0ccbaSfvdl  * Create a client handle for a connection.
1767df0ccbaSfvdl  * Default options are set, which the user can change using clnt_control()'s.
1777df0ccbaSfvdl  * The rpc/vc package does buffering similar to stdio, so the client
1787df0ccbaSfvdl  * must pick send and receive buffer sizes, 0 => use the default.
1797df0ccbaSfvdl  * NB: fd is copied into a private area.
1807df0ccbaSfvdl  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
1817df0ccbaSfvdl  * set this something more useful.
1827df0ccbaSfvdl  *
1837df0ccbaSfvdl  * fd should be an open socket
1847df0ccbaSfvdl  */
1857df0ccbaSfvdl CLIENT *
clnt_vc_create(int fd,const struct netbuf * raddr,rpcprog_t prog,rpcvers_t vers,u_int sendsz,u_int recvsz)186c5e820caSchristos clnt_vc_create(
187c5e820caSchristos 	int fd,
188c5e820caSchristos 	const struct netbuf *raddr,
189c5e820caSchristos 	rpcprog_t prog,
190c5e820caSchristos 	rpcvers_t vers,
191c5e820caSchristos 	u_int sendsz,
192c5e820caSchristos 	u_int recvsz
193c5e820caSchristos )
1947df0ccbaSfvdl {
1957df0ccbaSfvdl 	CLIENT *h;
1967df0ccbaSfvdl 	struct ct_data *ct = NULL;
1977df0ccbaSfvdl 	struct rpc_msg call_msg;
1983fdac2b8Sthorpej #ifdef _REENTRANT
1997df0ccbaSfvdl 	sigset_t mask;
2007df0ccbaSfvdl #endif
2017df0ccbaSfvdl 	sigset_t newmask;
2027df0ccbaSfvdl 	struct sockaddr_storage ss;
2037df0ccbaSfvdl 	socklen_t slen;
2047df0ccbaSfvdl 	struct __rpc_sockinfo si;
2057df0ccbaSfvdl 
2060e8cfd8fSlukem 	_DIAGASSERT(raddr != NULL);
2070e8cfd8fSlukem 
208deb154d2Schristos 	h  = mem_alloc(sizeof(*h));
2097df0ccbaSfvdl 	if (h == NULL) {
2107df0ccbaSfvdl 		warnx("clnt_vc_create: out of memory");
2117df0ccbaSfvdl 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2127df0ccbaSfvdl 		rpc_createerr.cf_error.re_errno = errno;
2137df0ccbaSfvdl 		goto fooy;
2147df0ccbaSfvdl 	}
215deb154d2Schristos 	ct = mem_alloc(sizeof(*ct));
2167df0ccbaSfvdl 	if (ct == NULL) {
2177df0ccbaSfvdl 		warnx("clnt_vc_create: out of memory");
2187df0ccbaSfvdl 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2197df0ccbaSfvdl 		rpc_createerr.cf_error.re_errno = errno;
2207df0ccbaSfvdl 		goto fooy;
2217df0ccbaSfvdl 	}
2227df0ccbaSfvdl 
2231eb9e03aSchristos 	__clnt_sigfillset(&newmask);
2247df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
2253fdac2b8Sthorpej #ifdef _REENTRANT
2267df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
227c9cdc302Schristos 	if (vc_fd_locks == NULL) {
2283fdac2b8Sthorpej 		size_t cv_allocsz, fd_allocsz;
2297df0ccbaSfvdl 		int dtbsize = __rpc_dtbsize();
2307df0ccbaSfvdl 
2317df0ccbaSfvdl 		fd_allocsz = dtbsize * sizeof (int);
232c9cdc302Schristos 		vc_fd_locks = mem_alloc(fd_allocsz);
233c9cdc302Schristos 		if (vc_fd_locks == NULL) {
2341eb9e03aSchristos 			goto blooy;
2357df0ccbaSfvdl 		} else
2367df0ccbaSfvdl 			memset(vc_fd_locks, '\0', fd_allocsz);
2377df0ccbaSfvdl 
23830399256Suebayasi 		_DIAGASSERT(vc_cv == NULL);
2397df0ccbaSfvdl 		cv_allocsz = dtbsize * sizeof (cond_t);
240c9cdc302Schristos 		vc_cv = mem_alloc(cv_allocsz);
241c9cdc302Schristos 		if (vc_cv == NULL) {
2427df0ccbaSfvdl 			mem_free(vc_fd_locks, fd_allocsz);
243c9cdc302Schristos 			vc_fd_locks = NULL;
2441eb9e03aSchristos 			goto blooy;
2457df0ccbaSfvdl 		} else {
2467df0ccbaSfvdl 			int i;
2477df0ccbaSfvdl 
2487df0ccbaSfvdl 			for (i = 0; i < dtbsize; i++)
2497df0ccbaSfvdl 				cond_init(&vc_cv[i], 0, (void *) 0);
2507df0ccbaSfvdl 		}
2517df0ccbaSfvdl 	} else
25230399256Suebayasi 		_DIAGASSERT(vc_cv != NULL);
2537df0ccbaSfvdl #endif
2547df0ccbaSfvdl 
2557df0ccbaSfvdl 	/*
2567df0ccbaSfvdl 	 * XXX - fvdl connecting while holding a mutex?
2577df0ccbaSfvdl 	 */
2587df0ccbaSfvdl 	slen = sizeof ss;
259deb154d2Schristos 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
2607df0ccbaSfvdl 		if (errno != ENOTCONN) {
2617df0ccbaSfvdl 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2627df0ccbaSfvdl 			rpc_createerr.cf_error.re_errno = errno;
2631eb9e03aSchristos 			goto blooy;
2647df0ccbaSfvdl 		}
2657df0ccbaSfvdl 		if (connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
2667df0ccbaSfvdl 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2677df0ccbaSfvdl 			rpc_createerr.cf_error.re_errno = errno;
2681eb9e03aSchristos 			goto blooy;
2697df0ccbaSfvdl 		}
2707df0ccbaSfvdl 	}
2717df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
272a781fb5fSdrochner 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
2737df0ccbaSfvdl 	if (!__rpc_fd2sockinfo(fd, &si))
2747df0ccbaSfvdl 		goto fooy;
2757df0ccbaSfvdl 
2767df0ccbaSfvdl 	ct->ct_closeit = FALSE;
2777df0ccbaSfvdl 
2787df0ccbaSfvdl 	/*
2797df0ccbaSfvdl 	 * Set up private data struct
2807df0ccbaSfvdl 	 */
2817df0ccbaSfvdl 	ct->ct_fd = fd;
2827df0ccbaSfvdl 	ct->ct_wait.tv_usec = 0;
2837df0ccbaSfvdl 	ct->ct_waitset = FALSE;
2841466914fSchristos 	ct->ct_addr.buf = malloc((size_t)raddr->maxlen);
2857df0ccbaSfvdl 	if (ct->ct_addr.buf == NULL)
2867df0ccbaSfvdl 		goto fooy;
2877ecc2916Schristos 	memcpy(ct->ct_addr.buf, raddr->buf, (size_t)raddr->len);
2887ecc2916Schristos 	ct->ct_addr.len = raddr->len;
2897df0ccbaSfvdl 	ct->ct_addr.maxlen = raddr->maxlen;
2907df0ccbaSfvdl 
2917df0ccbaSfvdl 	/*
2927df0ccbaSfvdl 	 * Initialize call message
2937df0ccbaSfvdl 	 */
2948b08fa0dSitojun 	call_msg.rm_xid = __RPC_GETXID();
2957df0ccbaSfvdl 	call_msg.rm_direction = CALL;
2967df0ccbaSfvdl 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
2977df0ccbaSfvdl 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
2987df0ccbaSfvdl 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
2997df0ccbaSfvdl 
3007df0ccbaSfvdl 	/*
3017df0ccbaSfvdl 	 * pre-serialize the static part of the call msg and stash it away
3027df0ccbaSfvdl 	 */
3037df0ccbaSfvdl 	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
3047df0ccbaSfvdl 	    XDR_ENCODE);
3057df0ccbaSfvdl 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
3067df0ccbaSfvdl 		if (ct->ct_closeit) {
3077df0ccbaSfvdl 			(void)close(fd);
3087df0ccbaSfvdl 		}
3097df0ccbaSfvdl 		goto fooy;
3107df0ccbaSfvdl 	}
3117df0ccbaSfvdl 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
3127df0ccbaSfvdl 	XDR_DESTROY(&(ct->ct_xdrs));
3137df0ccbaSfvdl 
3147df0ccbaSfvdl 	/*
3157df0ccbaSfvdl 	 * Create a client handle which uses xdrrec for serialization
3167df0ccbaSfvdl 	 * and authnone for authentication.
3177df0ccbaSfvdl 	 */
3187df0ccbaSfvdl 	h->cl_ops = clnt_vc_ops();
3197df0ccbaSfvdl 	h->cl_private = ct;
3207df0ccbaSfvdl 	h->cl_auth = authnone_create();
3217df0ccbaSfvdl 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
3227df0ccbaSfvdl 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
3237df0ccbaSfvdl 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
3247df0ccbaSfvdl 	    h->cl_private, read_vc, write_vc);
3257df0ccbaSfvdl 	return (h);
3267df0ccbaSfvdl 
3271eb9e03aSchristos blooy:
3281eb9e03aSchristos 	mutex_unlock(&clnt_fd_lock);
3291eb9e03aSchristos 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
3307df0ccbaSfvdl fooy:
3317df0ccbaSfvdl 	/*
3327df0ccbaSfvdl 	 * Something goofed, free stuff and barf
3337df0ccbaSfvdl 	 */
3347df0ccbaSfvdl 	if (ct)
3357df0ccbaSfvdl 		mem_free(ct, sizeof(struct ct_data));
3367df0ccbaSfvdl 	if (h)
3377df0ccbaSfvdl 		mem_free(h, sizeof(CLIENT));
338deb154d2Schristos 	return (NULL);
3397df0ccbaSfvdl }
3407df0ccbaSfvdl 
3417df0ccbaSfvdl 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)342c5e820caSchristos clnt_vc_call(
343c5e820caSchristos 	CLIENT *h,
344c5e820caSchristos 	rpcproc_t proc,
345c5e820caSchristos 	xdrproc_t xdr_args,
346c5e820caSchristos 	const char *args_ptr,
347c5e820caSchristos 	xdrproc_t xdr_results,
348c5e820caSchristos 	caddr_t results_ptr,
349c5e820caSchristos 	struct timeval timeout
350c5e820caSchristos )
3517df0ccbaSfvdl {
3527df0ccbaSfvdl 	struct ct_data *ct;
3537df0ccbaSfvdl 	XDR *xdrs;
3547df0ccbaSfvdl 	struct rpc_msg reply_msg;
3557df0ccbaSfvdl 	u_int32_t x_id;
3567df0ccbaSfvdl 	u_int32_t *msg_x_id;
3577df0ccbaSfvdl 	bool_t shipnow;
3587df0ccbaSfvdl 	int refreshes = 2;
3593fdac2b8Sthorpej #ifdef _REENTRANT
3607df0ccbaSfvdl 	sigset_t mask, newmask;
3617df0ccbaSfvdl #endif
3627df0ccbaSfvdl 
3637df0ccbaSfvdl 	_DIAGASSERT(h != NULL);
3647df0ccbaSfvdl 
3653fdac2b8Sthorpej 	ct = (struct ct_data *) h->cl_private;
3663fdac2b8Sthorpej 
3673fdac2b8Sthorpej #ifdef _REENTRANT
3681eb9e03aSchristos 	__clnt_sigfillset(&newmask);
3697df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
3707df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
3717df0ccbaSfvdl 	while (vc_fd_locks[ct->ct_fd])
3727df0ccbaSfvdl 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
3733fdac2b8Sthorpej 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
3747df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
3757df0ccbaSfvdl #endif
3767df0ccbaSfvdl 
3777df0ccbaSfvdl 	xdrs = &(ct->ct_xdrs);
3787df0ccbaSfvdl 	msg_x_id = &ct->ct_u.ct_mcalli;
3797df0ccbaSfvdl 
3807df0ccbaSfvdl 	if (!ct->ct_waitset) {
3817df0ccbaSfvdl 		if (time_not_ok(&timeout) == FALSE)
3827df0ccbaSfvdl 		ct->ct_wait = timeout;
3837df0ccbaSfvdl 	}
3847df0ccbaSfvdl 
3857df0ccbaSfvdl 	shipnow =
386deb154d2Schristos 	    (xdr_results == NULL && timeout.tv_sec == 0
3877df0ccbaSfvdl 	    && timeout.tv_usec == 0) ? FALSE : TRUE;
3887df0ccbaSfvdl 
3897df0ccbaSfvdl call_again:
3907df0ccbaSfvdl 	xdrs->x_op = XDR_ENCODE;
3917df0ccbaSfvdl 	ct->ct_error.re_status = RPC_SUCCESS;
3927df0ccbaSfvdl 	x_id = ntohl(--(*msg_x_id));
3937df0ccbaSfvdl 	if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
394128bd71fSchristos 	    (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
3957df0ccbaSfvdl 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
396347f995cSyamt 	    (! (*xdr_args)(xdrs, __UNCONST(args_ptr)))) {
3977df0ccbaSfvdl 		if (ct->ct_error.re_status == RPC_SUCCESS)
3987df0ccbaSfvdl 			ct->ct_error.re_status = RPC_CANTENCODEARGS;
3997df0ccbaSfvdl 		(void)xdrrec_endofrecord(xdrs, TRUE);
4007df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
4017df0ccbaSfvdl 		return (ct->ct_error.re_status);
4027df0ccbaSfvdl 	}
4037df0ccbaSfvdl 	if (! xdrrec_endofrecord(xdrs, shipnow)) {
4047df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
4057df0ccbaSfvdl 		return (ct->ct_error.re_status = RPC_CANTSEND);
4067df0ccbaSfvdl 	}
4077df0ccbaSfvdl 	if (! shipnow) {
4087df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
4097df0ccbaSfvdl 		return (RPC_SUCCESS);
4107df0ccbaSfvdl 	}
4117df0ccbaSfvdl 	/*
4127df0ccbaSfvdl 	 * Hack to provide rpc-based message passing
4137df0ccbaSfvdl 	 */
4147df0ccbaSfvdl 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
4157df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
4167df0ccbaSfvdl 		return(ct->ct_error.re_status = RPC_TIMEDOUT);
4177df0ccbaSfvdl 	}
4187df0ccbaSfvdl 
4197df0ccbaSfvdl 
4207df0ccbaSfvdl 	/*
4217df0ccbaSfvdl 	 * Keep receiving until we get a valid transaction id
4227df0ccbaSfvdl 	 */
4237df0ccbaSfvdl 	xdrs->x_op = XDR_DECODE;
4247df0ccbaSfvdl 	for (;;) {
4257df0ccbaSfvdl 		reply_msg.acpted_rply.ar_verf = _null_auth;
4267df0ccbaSfvdl 		reply_msg.acpted_rply.ar_results.where = NULL;
4277df0ccbaSfvdl 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
4287df0ccbaSfvdl 		if (! xdrrec_skiprecord(xdrs)) {
4297df0ccbaSfvdl 			release_fd_lock(ct->ct_fd, mask);
4307df0ccbaSfvdl 			return (ct->ct_error.re_status);
4317df0ccbaSfvdl 		}
4327df0ccbaSfvdl 		/* now decode and validate the response header */
4337df0ccbaSfvdl 		if (! xdr_replymsg(xdrs, &reply_msg)) {
4347df0ccbaSfvdl 			if (ct->ct_error.re_status == RPC_SUCCESS)
4357df0ccbaSfvdl 				continue;
4367df0ccbaSfvdl 			release_fd_lock(ct->ct_fd, mask);
4377df0ccbaSfvdl 			return (ct->ct_error.re_status);
4387df0ccbaSfvdl 		}
4397df0ccbaSfvdl 		if (reply_msg.rm_xid == x_id)
4407df0ccbaSfvdl 			break;
4417df0ccbaSfvdl 	}
4427df0ccbaSfvdl 
4437df0ccbaSfvdl 	/*
4447df0ccbaSfvdl 	 * process header
4457df0ccbaSfvdl 	 */
4467df0ccbaSfvdl 	_seterr_reply(&reply_msg, &(ct->ct_error));
4477df0ccbaSfvdl 	if (ct->ct_error.re_status == RPC_SUCCESS) {
4487df0ccbaSfvdl 		if (! AUTH_VALIDATE(h->cl_auth,
4497df0ccbaSfvdl 		    &reply_msg.acpted_rply.ar_verf)) {
4507df0ccbaSfvdl 			ct->ct_error.re_status = RPC_AUTHERROR;
4517df0ccbaSfvdl 			ct->ct_error.re_why = AUTH_INVALIDRESP;
4527df0ccbaSfvdl 		} else if (! (*xdr_results)(xdrs, results_ptr)) {
4537df0ccbaSfvdl 			if (ct->ct_error.re_status == RPC_SUCCESS)
4547df0ccbaSfvdl 				ct->ct_error.re_status = RPC_CANTDECODERES;
4557df0ccbaSfvdl 		}
4567df0ccbaSfvdl 		/* free verifier ... */
4577df0ccbaSfvdl 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
4587df0ccbaSfvdl 			xdrs->x_op = XDR_FREE;
4597df0ccbaSfvdl 			(void)xdr_opaque_auth(xdrs,
4607df0ccbaSfvdl 			    &(reply_msg.acpted_rply.ar_verf));
4617df0ccbaSfvdl 		}
4627df0ccbaSfvdl 	}  /* end successful completion */
4637df0ccbaSfvdl 	else {
4647df0ccbaSfvdl 		/* maybe our credentials need to be refreshed ... */
4657df0ccbaSfvdl 		if (refreshes-- && AUTH_REFRESH(h->cl_auth))
4667df0ccbaSfvdl 			goto call_again;
4677df0ccbaSfvdl 	}  /* end of unsuccessful completion */
4687df0ccbaSfvdl 	release_fd_lock(ct->ct_fd, mask);
4697df0ccbaSfvdl 	return (ct->ct_error.re_status);
4707df0ccbaSfvdl }
4717df0ccbaSfvdl 
4727df0ccbaSfvdl static void
clnt_vc_geterr(CLIENT * h,struct rpc_err * errp)473c5e820caSchristos clnt_vc_geterr(
474c5e820caSchristos 	CLIENT *h,
475c5e820caSchristos 	struct rpc_err *errp
476c5e820caSchristos )
4777df0ccbaSfvdl {
4787df0ccbaSfvdl 	struct ct_data *ct;
4797df0ccbaSfvdl 
4807df0ccbaSfvdl 	_DIAGASSERT(h != NULL);
4817df0ccbaSfvdl 	_DIAGASSERT(errp != NULL);
4827df0ccbaSfvdl 
4837df0ccbaSfvdl 	ct = (struct ct_data *) h->cl_private;
4847df0ccbaSfvdl 	*errp = ct->ct_error;
4857df0ccbaSfvdl }
4867df0ccbaSfvdl 
4877df0ccbaSfvdl static bool_t
clnt_vc_freeres(CLIENT * cl,xdrproc_t xdr_res,caddr_t res_ptr)488c5e820caSchristos clnt_vc_freeres(
489c5e820caSchristos 	CLIENT *cl,
490c5e820caSchristos 	xdrproc_t xdr_res,
491c5e820caSchristos 	caddr_t res_ptr
492c5e820caSchristos )
4937df0ccbaSfvdl {
4947df0ccbaSfvdl 	struct ct_data *ct;
4957df0ccbaSfvdl 	XDR *xdrs;
4967df0ccbaSfvdl 	bool_t dummy;
4973fdac2b8Sthorpej #ifdef _REENTRANT
4987df0ccbaSfvdl 	sigset_t mask;
4997df0ccbaSfvdl #endif
5007df0ccbaSfvdl 	sigset_t newmask;
5017df0ccbaSfvdl 
5027df0ccbaSfvdl 	_DIAGASSERT(cl != NULL);
5037df0ccbaSfvdl 
5047df0ccbaSfvdl 	ct = (struct ct_data *)cl->cl_private;
5057df0ccbaSfvdl 	xdrs = &(ct->ct_xdrs);
5067df0ccbaSfvdl 
5071eb9e03aSchristos 	__clnt_sigfillset(&newmask);
5087df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
5097df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
5103fdac2b8Sthorpej #ifdef _REENTRANT
5117df0ccbaSfvdl 	while (vc_fd_locks[ct->ct_fd])
5127df0ccbaSfvdl 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
5137df0ccbaSfvdl #endif
5147df0ccbaSfvdl 
5157df0ccbaSfvdl 	xdrs->x_op = XDR_FREE;
5167df0ccbaSfvdl 	dummy = (*xdr_res)(xdrs, res_ptr);
5177df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
5187df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
5197df0ccbaSfvdl 	cond_signal(&vc_cv[ct->ct_fd]);
5207df0ccbaSfvdl 
5217df0ccbaSfvdl 	return dummy;
5227df0ccbaSfvdl }
5237df0ccbaSfvdl 
5247df0ccbaSfvdl /*ARGSUSED*/
5257df0ccbaSfvdl static void
clnt_vc_abort(CLIENT * cl)526c5e820caSchristos clnt_vc_abort(CLIENT *cl)
5277df0ccbaSfvdl {
5287df0ccbaSfvdl }
5297df0ccbaSfvdl 
5307df0ccbaSfvdl static bool_t
clnt_vc_control(CLIENT * cl,u_int request,char * info)531c5e820caSchristos clnt_vc_control(
532c5e820caSchristos 	CLIENT *cl,
533c5e820caSchristos 	u_int request,
534c5e820caSchristos 	char *info
535c5e820caSchristos )
5367df0ccbaSfvdl {
5377df0ccbaSfvdl 	struct ct_data *ct;
5387df0ccbaSfvdl 	void *infop = info;
5393fdac2b8Sthorpej #ifdef _REENTRANT
5407df0ccbaSfvdl 	sigset_t mask;
5417df0ccbaSfvdl #endif
5427df0ccbaSfvdl 	sigset_t newmask;
5437df0ccbaSfvdl 
5447df0ccbaSfvdl 	_DIAGASSERT(cl != NULL);
5457df0ccbaSfvdl 
5467df0ccbaSfvdl 	ct = (struct ct_data *)cl->cl_private;
5477df0ccbaSfvdl 
5481eb9e03aSchristos 	__clnt_sigfillset(&newmask);
5497df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
5507df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
5513fdac2b8Sthorpej #ifdef _REENTRANT
5527df0ccbaSfvdl 	while (vc_fd_locks[ct->ct_fd])
5537df0ccbaSfvdl 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
5547df0ccbaSfvdl 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
5557df0ccbaSfvdl #endif
5567df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
5577df0ccbaSfvdl 
5587df0ccbaSfvdl 	switch (request) {
5597df0ccbaSfvdl 	case CLSET_FD_CLOSE:
5607df0ccbaSfvdl 		ct->ct_closeit = TRUE;
5617df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
5627df0ccbaSfvdl 		return (TRUE);
5637df0ccbaSfvdl 	case CLSET_FD_NCLOSE:
5647df0ccbaSfvdl 		ct->ct_closeit = FALSE;
5657df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
5667df0ccbaSfvdl 		return (TRUE);
5677df0ccbaSfvdl 	default:
5687df0ccbaSfvdl 		break;
5697df0ccbaSfvdl 	}
5707df0ccbaSfvdl 
5717df0ccbaSfvdl 	/* for other requests which use info */
5727df0ccbaSfvdl 	if (info == NULL) {
5737df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
5747df0ccbaSfvdl 		return (FALSE);
5757df0ccbaSfvdl 	}
5767df0ccbaSfvdl 	switch (request) {
5777df0ccbaSfvdl 	case CLSET_TIMEOUT:
578deb154d2Schristos 		if (time_not_ok((struct timeval *)(void *)info)) {
5797df0ccbaSfvdl 			release_fd_lock(ct->ct_fd, mask);
5807df0ccbaSfvdl 			return (FALSE);
5817df0ccbaSfvdl 		}
5827df0ccbaSfvdl 		ct->ct_wait = *(struct timeval *)infop;
5837df0ccbaSfvdl 		ct->ct_waitset = TRUE;
5847df0ccbaSfvdl 		break;
5857df0ccbaSfvdl 	case CLGET_TIMEOUT:
5867df0ccbaSfvdl 		*(struct timeval *)infop = ct->ct_wait;
5877df0ccbaSfvdl 		break;
5887df0ccbaSfvdl 	case CLGET_SERVER_ADDR:
589deb154d2Schristos 		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
5907df0ccbaSfvdl 		break;
5917df0ccbaSfvdl 	case CLGET_FD:
592deb154d2Schristos 		*(int *)(void *)info = ct->ct_fd;
5937df0ccbaSfvdl 		break;
5947df0ccbaSfvdl 	case CLGET_SVC_ADDR:
5957df0ccbaSfvdl 		/* The caller should not free this memory area */
596deb154d2Schristos 		*(struct netbuf *)(void *)info = ct->ct_addr;
5977df0ccbaSfvdl 		break;
5987df0ccbaSfvdl 	case CLSET_SVC_ADDR:		/* set to new address */
5997df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
6007df0ccbaSfvdl 		return (FALSE);
6017df0ccbaSfvdl 	case CLGET_XID:
6027df0ccbaSfvdl 		/*
6037df0ccbaSfvdl 		 * use the knowledge that xid is the
6047df0ccbaSfvdl 		 * first element in the call structure
6057df0ccbaSfvdl 		 * This will get the xid of the PREVIOUS call
6067df0ccbaSfvdl 		 */
607fc8781baSchristos 		ntohlp(info, &ct->ct_u.ct_mcalli);
6087df0ccbaSfvdl 		break;
6097df0ccbaSfvdl 	case CLSET_XID:
6107df0ccbaSfvdl 		/* This will set the xid of the NEXT call */
6117df0ccbaSfvdl 		/* increment by 1 as clnt_vc_call() decrements once */
612002a6c32Schristos 		htonlp(&ct->ct_u.ct_mcalli, info, 1);
6137df0ccbaSfvdl 		break;
6147df0ccbaSfvdl 	case CLGET_VERS:
6157df0ccbaSfvdl 		/*
6167df0ccbaSfvdl 		 * This RELIES on the information that, in the call body,
6177df0ccbaSfvdl 		 * the version number field is the fifth field from the
618492c086fSandvar 		 * beginning of the RPC header. MUST be changed if the
6197df0ccbaSfvdl 		 * call_struct is changed
6207df0ccbaSfvdl 		 */
621fc8781baSchristos 		ntohlp(info, ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT);
6227df0ccbaSfvdl 		break;
6237df0ccbaSfvdl 
6247df0ccbaSfvdl 	case CLSET_VERS:
625002a6c32Schristos 		htonlp(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT, info, 0);
6267df0ccbaSfvdl 		break;
6277df0ccbaSfvdl 
6287df0ccbaSfvdl 	case CLGET_PROG:
6297df0ccbaSfvdl 		/*
6307df0ccbaSfvdl 		 * This RELIES on the information that, in the call body,
6317df0ccbaSfvdl 		 * the program number field is the fourth field from the
632492c086fSandvar 		 * beginning of the RPC header. MUST be changed if the
6337df0ccbaSfvdl 		 * call_struct is changed
6347df0ccbaSfvdl 		 */
635fc8781baSchristos 		ntohlp(info, ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT);
6367df0ccbaSfvdl 		break;
6377df0ccbaSfvdl 
6387df0ccbaSfvdl 	case CLSET_PROG:
639002a6c32Schristos 		htonlp(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT, info, 0);
6407df0ccbaSfvdl 		break;
6417df0ccbaSfvdl 
6427df0ccbaSfvdl 	default:
6437df0ccbaSfvdl 		release_fd_lock(ct->ct_fd, mask);
6447df0ccbaSfvdl 		return (FALSE);
6457df0ccbaSfvdl 	}
6467df0ccbaSfvdl 	release_fd_lock(ct->ct_fd, mask);
6477df0ccbaSfvdl 	return (TRUE);
6487df0ccbaSfvdl }
6497df0ccbaSfvdl 
6507df0ccbaSfvdl 
6517df0ccbaSfvdl static void
clnt_vc_destroy(CLIENT * cl)652c5e820caSchristos clnt_vc_destroy(CLIENT *cl)
6537df0ccbaSfvdl {
6547df0ccbaSfvdl 	struct ct_data *ct;
6553fdac2b8Sthorpej #ifdef _REENTRANT
6563fdac2b8Sthorpej 	int ct_fd;
6577df0ccbaSfvdl 	sigset_t mask;
6587df0ccbaSfvdl #endif
6597df0ccbaSfvdl 	sigset_t newmask;
6607df0ccbaSfvdl 
6617df0ccbaSfvdl 	_DIAGASSERT(cl != NULL);
6627df0ccbaSfvdl 
6637df0ccbaSfvdl 	ct = (struct ct_data *) cl->cl_private;
6647df0ccbaSfvdl 
6651eb9e03aSchristos 	__clnt_sigfillset(&newmask);
6667df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
6677df0ccbaSfvdl 	mutex_lock(&clnt_fd_lock);
6683fdac2b8Sthorpej #ifdef _REENTRANT
66926ba8048Schristos 	ct_fd = ct->ct_fd;
6707df0ccbaSfvdl 	while (vc_fd_locks[ct_fd])
6717df0ccbaSfvdl 		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
6727df0ccbaSfvdl #endif
6737df0ccbaSfvdl 	if (ct->ct_closeit && ct->ct_fd != -1) {
6747df0ccbaSfvdl 		(void)close(ct->ct_fd);
6757df0ccbaSfvdl 	}
6767df0ccbaSfvdl 	XDR_DESTROY(&(ct->ct_xdrs));
6777df0ccbaSfvdl 	if (ct->ct_addr.buf)
6787df0ccbaSfvdl 		free(ct->ct_addr.buf);
6797df0ccbaSfvdl 	mem_free(ct, sizeof(struct ct_data));
6807df0ccbaSfvdl 	mem_free(cl, sizeof(CLIENT));
6817df0ccbaSfvdl 	mutex_unlock(&clnt_fd_lock);
6827df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
6837df0ccbaSfvdl 
6847df0ccbaSfvdl 	cond_signal(&vc_cv[ct_fd]);
6857df0ccbaSfvdl }
6867df0ccbaSfvdl 
6877df0ccbaSfvdl /*
6887df0ccbaSfvdl  * Interface between xdr serializer and tcp connection.
6897df0ccbaSfvdl  * Behaves like the system calls, read & write, but keeps some error state
6907df0ccbaSfvdl  * around for the rpc level.
6917df0ccbaSfvdl  */
6927df0ccbaSfvdl static int
read_vc(char * ctp,char * buf,int len)693c5e820caSchristos read_vc(char *ctp, char *buf, int len)
6947df0ccbaSfvdl {
6957df0ccbaSfvdl 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
6967df0ccbaSfvdl 	struct pollfd fd;
69768f654dcSchristos 	struct timespec ts;
698c5e820caSchristos 	ssize_t nread;
6997df0ccbaSfvdl 
7007df0ccbaSfvdl 	if (len == 0)
7017df0ccbaSfvdl 		return (0);
70268f654dcSchristos 
70368f654dcSchristos 	TIMEVAL_TO_TIMESPEC(&ct->ct_wait, &ts);
7047df0ccbaSfvdl 	fd.fd = ct->ct_fd;
7057df0ccbaSfvdl 	fd.events = POLLIN;
7067df0ccbaSfvdl 	for (;;) {
70768f654dcSchristos 		switch (pollts(&fd, 1, &ts, NULL)) {
7087df0ccbaSfvdl 		case 0:
7097df0ccbaSfvdl 			ct->ct_error.re_status = RPC_TIMEDOUT;
7107df0ccbaSfvdl 			return (-1);
7117df0ccbaSfvdl 
7127df0ccbaSfvdl 		case -1:
7137df0ccbaSfvdl 			if (errno == EINTR)
7147df0ccbaSfvdl 				continue;
7157df0ccbaSfvdl 			ct->ct_error.re_status = RPC_CANTRECV;
7167df0ccbaSfvdl 			ct->ct_error.re_errno = errno;
7177df0ccbaSfvdl 			return (-1);
7187df0ccbaSfvdl 		}
7197df0ccbaSfvdl 		break;
7207df0ccbaSfvdl 	}
721c5e820caSchristos 	switch (nread = read(ct->ct_fd, buf, (size_t)len)) {
7227df0ccbaSfvdl 
7237df0ccbaSfvdl 	case 0:
7247df0ccbaSfvdl 		/* premature eof */
7257df0ccbaSfvdl 		ct->ct_error.re_errno = ECONNRESET;
7267df0ccbaSfvdl 		ct->ct_error.re_status = RPC_CANTRECV;
727b3e96d66Schristos 		nread = -1;  /* it's really an error */
7287df0ccbaSfvdl 		break;
7297df0ccbaSfvdl 
7307df0ccbaSfvdl 	case -1:
7317df0ccbaSfvdl 		ct->ct_error.re_errno = errno;
7327df0ccbaSfvdl 		ct->ct_error.re_status = RPC_CANTRECV;
7337df0ccbaSfvdl 		break;
7347df0ccbaSfvdl 	}
735c5e820caSchristos 	return (int)nread;
7367df0ccbaSfvdl }
7377df0ccbaSfvdl 
7387df0ccbaSfvdl static int
write_vc(char * ctp,char * buf,int len)739c5e820caSchristos write_vc(char *ctp, char *buf, int len)
7407df0ccbaSfvdl {
7417df0ccbaSfvdl 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
742c5e820caSchristos 	ssize_t i;
743c5e820caSchristos 	size_t cnt;
7447df0ccbaSfvdl 
7457df0ccbaSfvdl 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
746c5e820caSchristos 		if ((i = write(ct->ct_fd, buf, cnt)) == -1) {
7477df0ccbaSfvdl 			ct->ct_error.re_errno = errno;
7487df0ccbaSfvdl 			ct->ct_error.re_status = RPC_CANTSEND;
7497df0ccbaSfvdl 			return (-1);
7507df0ccbaSfvdl 		}
7517df0ccbaSfvdl 	}
752c5e820caSchristos 	return len;
7537df0ccbaSfvdl }
7547df0ccbaSfvdl 
7557df0ccbaSfvdl static struct clnt_ops *
clnt_vc_ops(void)756c5e820caSchristos clnt_vc_ops(void)
7577df0ccbaSfvdl {
7587df0ccbaSfvdl 	static struct clnt_ops ops;
7593fdac2b8Sthorpej #ifdef _REENTRANT
7607df0ccbaSfvdl 	sigset_t mask;
7617df0ccbaSfvdl #endif
7627df0ccbaSfvdl 	sigset_t newmask;
7637df0ccbaSfvdl 
7647df0ccbaSfvdl 	/* VARIABLES PROTECTED BY ops_lock: ops */
7657df0ccbaSfvdl 
7661eb9e03aSchristos 	__clnt_sigfillset(&newmask);
7677df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
7687df0ccbaSfvdl 	mutex_lock(&ops_lock);
7697df0ccbaSfvdl 	if (ops.cl_call == NULL) {
7707df0ccbaSfvdl 		ops.cl_call = clnt_vc_call;
7717df0ccbaSfvdl 		ops.cl_abort = clnt_vc_abort;
7727df0ccbaSfvdl 		ops.cl_geterr = clnt_vc_geterr;
7737df0ccbaSfvdl 		ops.cl_freeres = clnt_vc_freeres;
7747df0ccbaSfvdl 		ops.cl_destroy = clnt_vc_destroy;
7757df0ccbaSfvdl 		ops.cl_control = clnt_vc_control;
7767df0ccbaSfvdl 	}
7777df0ccbaSfvdl 	mutex_unlock(&ops_lock);
7787df0ccbaSfvdl 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
7797df0ccbaSfvdl 	return (&ops);
7807df0ccbaSfvdl }
7817df0ccbaSfvdl 
7827df0ccbaSfvdl /*
7837df0ccbaSfvdl  * Make sure that the time is not garbage.   -1 value is disallowed.
7847df0ccbaSfvdl  * Note this is different from time_not_ok in clnt_dg.c
7857df0ccbaSfvdl  */
7867df0ccbaSfvdl static bool_t
time_not_ok(struct timeval * t)787c5e820caSchristos time_not_ok(struct timeval *t)
7887df0ccbaSfvdl {
7890e8cfd8fSlukem 
7900e8cfd8fSlukem 	_DIAGASSERT(t != NULL);
7910e8cfd8fSlukem 
7927df0ccbaSfvdl 	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
7937df0ccbaSfvdl 		t->tv_usec <= -1 || t->tv_usec > 1000000);
7947df0ccbaSfvdl }
795