xref: /minix/lib/libc/rpc/clnt_dg.c (revision 84d9c625)
1*84d9c625SLionel Sambuc /*	$NetBSD: clnt_dg.c,v 1.29 2013/05/07 21:08:44 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc  * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras  *
6*84d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc  * met:
92fe8fb19SBen Gras  *
10*84d9c625SLionel Sambuc  *     * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc  *       notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc  *     * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc  *       copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc  *       disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc  *       provided with the distribution.
16*84d9c625SLionel Sambuc  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc  *       contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc  *       from this software without specific prior written permission.
192fe8fb19SBen Gras  *
20*84d9c625SLionel Sambuc  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras  */
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
352fe8fb19SBen Gras  */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras /* #ident	"@(#)clnt_dg.c	1.23	94/04/22 SMI" */
382fe8fb19SBen Gras 
392fe8fb19SBen Gras #include <sys/cdefs.h>
402fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
412fe8fb19SBen Gras #if 0
422fe8fb19SBen Gras static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
432fe8fb19SBen Gras #else
44*84d9c625SLionel Sambuc __RCSID("$NetBSD: clnt_dg.c,v 1.29 2013/05/07 21:08:44 christos Exp $");
452fe8fb19SBen Gras #endif
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras 
482fe8fb19SBen Gras /*
492fe8fb19SBen Gras  * Implements a connectionless client side RPC.
502fe8fb19SBen Gras  */
512fe8fb19SBen Gras 
522fe8fb19SBen Gras #include "namespace.h"
532fe8fb19SBen Gras #include "reentrant.h"
542fe8fb19SBen Gras #include <sys/poll.h>
552fe8fb19SBen Gras #include <sys/types.h>
562fe8fb19SBen Gras #include <sys/time.h>
572fe8fb19SBen Gras #include <sys/socket.h>
582fe8fb19SBen Gras #include <sys/ioctl.h>
592fe8fb19SBen Gras #include <rpc/rpc.h>
602fe8fb19SBen Gras #include <assert.h>
612fe8fb19SBen Gras #include <errno.h>
622fe8fb19SBen Gras #include <stdlib.h>
632fe8fb19SBen Gras #include <string.h>
642fe8fb19SBen Gras #include <signal.h>
652fe8fb19SBen Gras #include <unistd.h>
662fe8fb19SBen Gras #include <err.h>
67*84d9c625SLionel Sambuc 
68*84d9c625SLionel Sambuc #include "svc_fdset.h"
692fe8fb19SBen Gras #include "rpc_internal.h"
702fe8fb19SBen Gras 
712fe8fb19SBen Gras #ifdef __weak_alias
722fe8fb19SBen Gras __weak_alias(clnt_dg_create,_clnt_dg_create)
732fe8fb19SBen Gras #endif
742fe8fb19SBen Gras 
752fe8fb19SBen Gras #define	RPC_MAX_BACKOFF		30 /* seconds */
762fe8fb19SBen Gras 
772fe8fb19SBen Gras 
78f14fb602SLionel Sambuc static struct clnt_ops *clnt_dg_ops(void);
79f14fb602SLionel Sambuc static bool_t time_not_ok(struct timeval *);
80f14fb602SLionel Sambuc static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t,
81f14fb602SLionel Sambuc     const char *, xdrproc_t, caddr_t, struct timeval);
82f14fb602SLionel Sambuc static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
83f14fb602SLionel Sambuc static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, caddr_t);
84f14fb602SLionel Sambuc static void clnt_dg_abort(CLIENT *);
85f14fb602SLionel Sambuc static bool_t clnt_dg_control(CLIENT *, u_int, char *);
86f14fb602SLionel Sambuc static void clnt_dg_destroy(CLIENT *);
872fe8fb19SBen Gras 
882fe8fb19SBen Gras 
892fe8fb19SBen Gras 
902fe8fb19SBen Gras 
912fe8fb19SBen Gras /*
922fe8fb19SBen Gras  *	This machinery implements per-fd locks for MT-safety.  It is not
932fe8fb19SBen Gras  *	sufficient to do per-CLIENT handle locks for MT-safety because a
942fe8fb19SBen Gras  *	user may create more than one CLIENT handle with the same fd behind
952fe8fb19SBen Gras  *	it.  Therfore, we allocate an array of flags (dg_fd_locks), protected
962fe8fb19SBen Gras  *	by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables
972fe8fb19SBen Gras  *	similarly protected.  Dg_fd_lock[fd] == 1 => a call is activte on some
982fe8fb19SBen Gras  *	CLIENT handle created for that fd.
992fe8fb19SBen Gras  *	The current implementation holds locks across the entire RPC and reply,
1002fe8fb19SBen Gras  *	including retransmissions.  Yes, this is silly, and as soon as this
1012fe8fb19SBen Gras  *	code is proven to work, this should be the first thing fixed.  One step
1022fe8fb19SBen Gras  *	at a time.
1032fe8fb19SBen Gras  */
1042fe8fb19SBen Gras static int	*dg_fd_locks;
1052fe8fb19SBen Gras #ifdef _REENTRANT
1062fe8fb19SBen Gras #define __rpc_lock_value __isthreaded;
1072fe8fb19SBen Gras extern mutex_t clnt_fd_lock;
1082fe8fb19SBen Gras static cond_t	*dg_cv;
1092fe8fb19SBen Gras #define	release_fd_lock(fd, mask) {		\
1102fe8fb19SBen Gras 	mutex_lock(&clnt_fd_lock);	\
1112fe8fb19SBen Gras 	dg_fd_locks[fd] = 0;		\
1122fe8fb19SBen Gras 	mutex_unlock(&clnt_fd_lock);	\
1132fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);	\
1142fe8fb19SBen Gras 	cond_signal(&dg_cv[fd]);	\
1152fe8fb19SBen Gras }
1162fe8fb19SBen Gras #else
1172fe8fb19SBen Gras #define release_fd_lock(fd,mask)
1182fe8fb19SBen Gras #define __rpc_lock_value 0
1192fe8fb19SBen Gras #endif
1202fe8fb19SBen Gras 
1212fe8fb19SBen Gras static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
1222fe8fb19SBen Gras 
1232fe8fb19SBen Gras /* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */
1242fe8fb19SBen Gras 
1252fe8fb19SBen Gras /*
1262fe8fb19SBen Gras  * Private data kept per client handle
1272fe8fb19SBen Gras  */
1282fe8fb19SBen Gras struct cu_data {
1292fe8fb19SBen Gras 	int			cu_fd;		/* connections fd */
1302fe8fb19SBen Gras 	bool_t			cu_closeit;	/* opened by library */
1312fe8fb19SBen Gras 	struct sockaddr_storage	cu_raddr;	/* remote address */
1322fe8fb19SBen Gras 	int			cu_rlen;
1332fe8fb19SBen Gras 	struct timeval		cu_wait;	/* retransmit interval */
1342fe8fb19SBen Gras 	struct timeval		cu_total;	/* total time for the call */
1352fe8fb19SBen Gras 	struct rpc_err		cu_error;
1362fe8fb19SBen Gras 	XDR			cu_outxdrs;
1372fe8fb19SBen Gras 	u_int			cu_xdrpos;
1382fe8fb19SBen Gras 	u_int			cu_sendsz;	/* send size */
1392fe8fb19SBen Gras 	char			*cu_outbuf;
1402fe8fb19SBen Gras 	u_int			cu_recvsz;	/* recv size */
1412fe8fb19SBen Gras 	struct pollfd		cu_pfdp;
1422fe8fb19SBen Gras 	char			cu_inbuf[1];
1432fe8fb19SBen Gras };
1442fe8fb19SBen Gras 
1452fe8fb19SBen Gras /*
1462fe8fb19SBen Gras  * Connection less client creation returns with client handle parameters.
1472fe8fb19SBen Gras  * Default options are set, which the user can change using clnt_control().
1482fe8fb19SBen Gras  * fd should be open and bound.
1492fe8fb19SBen Gras  * NB: The rpch->cl_auth is initialized to null authentication.
1502fe8fb19SBen Gras  * 	Caller may wish to set this something more useful.
1512fe8fb19SBen Gras  *
1522fe8fb19SBen Gras  * sendsz and recvsz are the maximum allowable packet sizes that can be
1532fe8fb19SBen Gras  * sent and received. Normally they are the same, but they can be
1542fe8fb19SBen Gras  * changed to improve the program efficiency and buffer allocation.
1552fe8fb19SBen Gras  * If they are 0, use the transport default.
1562fe8fb19SBen Gras  *
1572fe8fb19SBen Gras  * If svcaddr is NULL, returns NULL.
1582fe8fb19SBen Gras  */
1592fe8fb19SBen Gras CLIENT *
clnt_dg_create(int fd,const struct netbuf * svcaddr,rpcprog_t program,rpcvers_t version,u_int sendsz,u_int recvsz)160f14fb602SLionel Sambuc clnt_dg_create(
161f14fb602SLionel Sambuc 	int fd,				/* open file descriptor */
162f14fb602SLionel Sambuc 	const struct netbuf *svcaddr,	/* servers address */
163f14fb602SLionel Sambuc 	rpcprog_t program,		/* program number */
164f14fb602SLionel Sambuc 	rpcvers_t version,		/* version number */
165f14fb602SLionel Sambuc 	u_int sendsz,			/* buffer recv size */
166f14fb602SLionel Sambuc 	u_int recvsz)			/* buffer send size */
1672fe8fb19SBen Gras {
1682fe8fb19SBen Gras 	CLIENT *cl = NULL;		/* client handle */
1692fe8fb19SBen Gras 	struct cu_data *cu = NULL;	/* private data */
1702fe8fb19SBen Gras 	struct rpc_msg call_msg;
1712fe8fb19SBen Gras #ifdef _REENTRANT
1722fe8fb19SBen Gras 	sigset_t mask;
1732fe8fb19SBen Gras #endif
1742fe8fb19SBen Gras 	sigset_t newmask;
1752fe8fb19SBen Gras 	struct __rpc_sockinfo si;
1762fe8fb19SBen Gras 	int one = 1;
1772fe8fb19SBen Gras 
178*84d9c625SLionel Sambuc 	__clnt_sigfillset(&newmask);
1792fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
1802fe8fb19SBen Gras 	mutex_lock(&clnt_fd_lock);
1812fe8fb19SBen Gras 	if (dg_fd_locks == NULL) {
1822fe8fb19SBen Gras #ifdef _REENTRANT
1832fe8fb19SBen Gras 		size_t cv_allocsz;
1842fe8fb19SBen Gras #endif
1852fe8fb19SBen Gras 		size_t fd_allocsz;
1862fe8fb19SBen Gras 		int dtbsize = __rpc_dtbsize();
1872fe8fb19SBen Gras 
1882fe8fb19SBen Gras 		fd_allocsz = dtbsize * sizeof (int);
1892fe8fb19SBen Gras 		dg_fd_locks = mem_alloc(fd_allocsz);
1902fe8fb19SBen Gras 		if (dg_fd_locks == NULL) {
191*84d9c625SLionel Sambuc 			goto err0;
1922fe8fb19SBen Gras 		} else
1932fe8fb19SBen Gras 			memset(dg_fd_locks, '\0', fd_allocsz);
1942fe8fb19SBen Gras 
1952fe8fb19SBen Gras #ifdef _REENTRANT
1962fe8fb19SBen Gras 		cv_allocsz = dtbsize * sizeof (cond_t);
1972fe8fb19SBen Gras 		dg_cv = mem_alloc(cv_allocsz);
1982fe8fb19SBen Gras 		if (dg_cv == NULL) {
1992fe8fb19SBen Gras 			mem_free(dg_fd_locks, fd_allocsz);
2002fe8fb19SBen Gras 			dg_fd_locks = NULL;
201*84d9c625SLionel Sambuc 			goto err0;
2022fe8fb19SBen Gras 		} else {
2032fe8fb19SBen Gras 			int i;
2042fe8fb19SBen Gras 
2052fe8fb19SBen Gras 			for (i = 0; i < dtbsize; i++)
2062fe8fb19SBen Gras 				cond_init(&dg_cv[i], 0, (void *) 0);
2072fe8fb19SBen Gras 		}
2082fe8fb19SBen Gras #endif
2092fe8fb19SBen Gras 	}
2102fe8fb19SBen Gras 
2112fe8fb19SBen Gras 	mutex_unlock(&clnt_fd_lock);
2122fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
2132fe8fb19SBen Gras 
2142fe8fb19SBen Gras 	if (svcaddr == NULL) {
2152fe8fb19SBen Gras 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
2162fe8fb19SBen Gras 		return (NULL);
2172fe8fb19SBen Gras 	}
2182fe8fb19SBen Gras 
2192fe8fb19SBen Gras 	if (!__rpc_fd2sockinfo(fd, &si)) {
2202fe8fb19SBen Gras 		rpc_createerr.cf_stat = RPC_TLIERROR;
2212fe8fb19SBen Gras 		rpc_createerr.cf_error.re_errno = 0;
2222fe8fb19SBen Gras 		return (NULL);
2232fe8fb19SBen Gras 	}
2242fe8fb19SBen Gras 	/*
2252fe8fb19SBen Gras 	 * Find the receive and the send size
2262fe8fb19SBen Gras 	 */
2272fe8fb19SBen Gras 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
2282fe8fb19SBen Gras 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
2292fe8fb19SBen Gras 	if ((sendsz == 0) || (recvsz == 0)) {
2302fe8fb19SBen Gras 		rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
2312fe8fb19SBen Gras 		rpc_createerr.cf_error.re_errno = 0;
2322fe8fb19SBen Gras 		return (NULL);
2332fe8fb19SBen Gras 	}
2342fe8fb19SBen Gras 
2352fe8fb19SBen Gras 	if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
2362fe8fb19SBen Gras 		goto err1;
2372fe8fb19SBen Gras 	/*
2382fe8fb19SBen Gras 	 * Should be multiple of 4 for XDR.
2392fe8fb19SBen Gras 	 */
2402fe8fb19SBen Gras 	sendsz = ((sendsz + 3) / 4) * 4;
2412fe8fb19SBen Gras 	recvsz = ((recvsz + 3) / 4) * 4;
2422fe8fb19SBen Gras 	cu = malloc(sizeof (*cu) + sendsz + recvsz);
2432fe8fb19SBen Gras 	if (cu == NULL)
2442fe8fb19SBen Gras 		goto err1;
2452fe8fb19SBen Gras 	memset(cu, 0, sizeof(*cu));
2462fe8fb19SBen Gras 	(void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len);
2472fe8fb19SBen Gras 	cu->cu_rlen = svcaddr->len;
2482fe8fb19SBen Gras 	cu->cu_outbuf = &cu->cu_inbuf[recvsz];
2492fe8fb19SBen Gras 	/* Other values can also be set through clnt_control() */
250*84d9c625SLionel Sambuc #ifdef RUMP_RPC
2512fe8fb19SBen Gras 	cu->cu_wait.tv_sec = 15;	/* heuristically chosen */
2522fe8fb19SBen Gras 	cu->cu_wait.tv_usec = 0;
253*84d9c625SLionel Sambuc #else
254*84d9c625SLionel Sambuc 	cu->cu_wait.tv_sec = 0;		/* for testing, 10x / second */
255*84d9c625SLionel Sambuc 	cu->cu_wait.tv_usec = 100000;
256*84d9c625SLionel Sambuc #endif
2572fe8fb19SBen Gras 	cu->cu_total.tv_sec = -1;
2582fe8fb19SBen Gras 	cu->cu_total.tv_usec = -1;
2592fe8fb19SBen Gras 	cu->cu_sendsz = sendsz;
2602fe8fb19SBen Gras 	cu->cu_recvsz = recvsz;
2612fe8fb19SBen Gras 	call_msg.rm_xid = __RPC_GETXID();
2622fe8fb19SBen Gras 	call_msg.rm_call.cb_prog = program;
2632fe8fb19SBen Gras 	call_msg.rm_call.cb_vers = version;
2642fe8fb19SBen Gras 	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
2652fe8fb19SBen Gras 	if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
2662fe8fb19SBen Gras 		rpc_createerr.cf_stat = RPC_CANTENCODEARGS;  /* XXX */
2672fe8fb19SBen Gras 		rpc_createerr.cf_error.re_errno = 0;
2682fe8fb19SBen Gras 		goto err2;
2692fe8fb19SBen Gras 	}
2702fe8fb19SBen Gras 	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
2712fe8fb19SBen Gras 
2722fe8fb19SBen Gras 	/* XXX fvdl - do we still want this? */
2732fe8fb19SBen Gras #if 0
2742fe8fb19SBen Gras 	(void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
2752fe8fb19SBen Gras #endif
2762fe8fb19SBen Gras 	ioctl(fd, FIONBIO, (char *)(void *)&one);
2772fe8fb19SBen Gras 
2782fe8fb19SBen Gras 	/*
2792fe8fb19SBen Gras 	 * By default, closeit is always FALSE. It is users responsibility
2802fe8fb19SBen Gras 	 * to do a close on it, else the user may use clnt_control
2812fe8fb19SBen Gras 	 * to let clnt_destroy do it for him/her.
2822fe8fb19SBen Gras 	 */
2832fe8fb19SBen Gras 	cu->cu_closeit = FALSE;
2842fe8fb19SBen Gras 	cu->cu_fd = fd;
2852fe8fb19SBen Gras 	cu->cu_pfdp.fd = cu->cu_fd;
2862fe8fb19SBen Gras 	cu->cu_pfdp.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
2872fe8fb19SBen Gras 	cl->cl_ops = clnt_dg_ops();
2882fe8fb19SBen Gras 	cl->cl_private = (caddr_t)(void *)cu;
2892fe8fb19SBen Gras 	cl->cl_auth = authnone_create();
2902fe8fb19SBen Gras 	cl->cl_tp = NULL;
2912fe8fb19SBen Gras 	cl->cl_netid = NULL;
2922fe8fb19SBen Gras 	return (cl);
293*84d9c625SLionel Sambuc err0:
294*84d9c625SLionel Sambuc 	mutex_unlock(&clnt_fd_lock);
295*84d9c625SLionel Sambuc 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
2962fe8fb19SBen Gras err1:
2972fe8fb19SBen Gras 	warnx(mem_err_clnt_dg);
2982fe8fb19SBen Gras 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2992fe8fb19SBen Gras 	rpc_createerr.cf_error.re_errno = errno;
3002fe8fb19SBen Gras err2:
3012fe8fb19SBen Gras 	if (cl) {
3022fe8fb19SBen Gras 		mem_free(cl, sizeof (CLIENT));
3032fe8fb19SBen Gras 		if (cu)
3042fe8fb19SBen Gras 			mem_free(cu, sizeof (*cu) + sendsz + recvsz);
3052fe8fb19SBen Gras 	}
3062fe8fb19SBen Gras 	return (NULL);
3072fe8fb19SBen Gras }
3082fe8fb19SBen Gras 
3092fe8fb19SBen Gras static enum clnt_stat
clnt_dg_call(CLIENT * cl,rpcproc_t proc,xdrproc_t xargs,const char * argsp,xdrproc_t xresults,caddr_t resultsp,struct timeval utimeout)310f14fb602SLionel Sambuc clnt_dg_call(
311f14fb602SLionel Sambuc 	CLIENT *	cl,		/* client handle */
312f14fb602SLionel Sambuc 	rpcproc_t	proc,		/* procedure number */
313f14fb602SLionel Sambuc 	xdrproc_t	xargs,		/* xdr routine for args */
314f14fb602SLionel Sambuc 	const char *	argsp,		/* pointer to args */
315f14fb602SLionel Sambuc 	xdrproc_t	xresults,	/* xdr routine for results */
316f14fb602SLionel Sambuc 	caddr_t		resultsp,	/* pointer to results */
317f14fb602SLionel Sambuc 	struct timeval	utimeout)	/* seconds to wait before giving up */
3182fe8fb19SBen Gras {
3192fe8fb19SBen Gras 	struct cu_data *cu;
3202fe8fb19SBen Gras 	XDR *xdrs;
3212fe8fb19SBen Gras 	size_t outlen;
3222fe8fb19SBen Gras 	struct rpc_msg reply_msg;
3232fe8fb19SBen Gras 	XDR reply_xdrs;
3242fe8fb19SBen Gras 	bool_t ok;
3252fe8fb19SBen Gras 	int nrefreshes = 2;		/* number of times to refresh cred */
3262fe8fb19SBen Gras 	struct timeval timeout;
3272fe8fb19SBen Gras 	struct timeval retransmit_time;
3282fe8fb19SBen Gras 	struct timeval next_sendtime, starttime, time_waited, tv;
3292fe8fb19SBen Gras #ifdef _REENTRANT
3302fe8fb19SBen Gras 	sigset_t mask, *maskp = &mask;
3312fe8fb19SBen Gras #else
3322fe8fb19SBen Gras 	sigset_t *maskp = NULL;
3332fe8fb19SBen Gras #endif
3342fe8fb19SBen Gras 	sigset_t newmask;
3352fe8fb19SBen Gras 	ssize_t recvlen = 0;
3362fe8fb19SBen Gras 	struct timespec ts;
3372fe8fb19SBen Gras 	int n;
3382fe8fb19SBen Gras 
3392fe8fb19SBen Gras 	_DIAGASSERT(cl != NULL);
3402fe8fb19SBen Gras 
3412fe8fb19SBen Gras 	cu = (struct cu_data *)cl->cl_private;
3422fe8fb19SBen Gras 
343*84d9c625SLionel Sambuc 	__clnt_sigfillset(&newmask);
3442fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
3452fe8fb19SBen Gras 	mutex_lock(&clnt_fd_lock);
3462fe8fb19SBen Gras 	while (dg_fd_locks[cu->cu_fd])
3472fe8fb19SBen Gras 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
3482fe8fb19SBen Gras 	dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
3492fe8fb19SBen Gras 	mutex_unlock(&clnt_fd_lock);
3502fe8fb19SBen Gras 	if (cu->cu_total.tv_usec == -1) {
3512fe8fb19SBen Gras 		timeout = utimeout;	/* use supplied timeout */
3522fe8fb19SBen Gras 	} else {
3532fe8fb19SBen Gras 		timeout = cu->cu_total;	/* use default timeout */
3542fe8fb19SBen Gras 	}
3552fe8fb19SBen Gras 
3562fe8fb19SBen Gras 	time_waited.tv_sec = 0;
3572fe8fb19SBen Gras 	time_waited.tv_usec = 0;
3582fe8fb19SBen Gras 	retransmit_time = next_sendtime = cu->cu_wait;
3592fe8fb19SBen Gras 	gettimeofday(&starttime, NULL);
3602fe8fb19SBen Gras 
3612fe8fb19SBen Gras call_again:
3622fe8fb19SBen Gras 	xdrs = &(cu->cu_outxdrs);
3632fe8fb19SBen Gras 	xdrs->x_op = XDR_ENCODE;
3642fe8fb19SBen Gras 	XDR_SETPOS(xdrs, cu->cu_xdrpos);
3652fe8fb19SBen Gras 	/*
3662fe8fb19SBen Gras 	 * the transaction is the first thing in the out buffer
3672fe8fb19SBen Gras 	 */
3682fe8fb19SBen Gras 	(*(u_int32_t *)(void *)(cu->cu_outbuf))++;
3692fe8fb19SBen Gras 	if ((! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
3702fe8fb19SBen Gras 	    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
3712fe8fb19SBen Gras 	    (! (*xargs)(xdrs, __UNCONST(argsp)))) {
3722fe8fb19SBen Gras 		cu->cu_error.re_status = RPC_CANTENCODEARGS;
3732fe8fb19SBen Gras 		goto out;
3742fe8fb19SBen Gras 	}
3752fe8fb19SBen Gras 	outlen = (size_t)XDR_GETPOS(xdrs);
3762fe8fb19SBen Gras 
3772fe8fb19SBen Gras send_again:
3782fe8fb19SBen Gras 	if ((size_t)sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0,
3792fe8fb19SBen Gras 	    (struct sockaddr *)(void *)&cu->cu_raddr, (socklen_t)cu->cu_rlen)
3802fe8fb19SBen Gras 	    != outlen) {
3812fe8fb19SBen Gras 		cu->cu_error.re_errno = errno;
3822fe8fb19SBen Gras 		cu->cu_error.re_status = RPC_CANTSEND;
3832fe8fb19SBen Gras 		goto out;
3842fe8fb19SBen Gras 	}
3852fe8fb19SBen Gras 
3862fe8fb19SBen Gras 	/*
3872fe8fb19SBen Gras 	 * Hack to provide rpc-based message passing
3882fe8fb19SBen Gras 	 */
3892fe8fb19SBen Gras 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
3902fe8fb19SBen Gras 		cu->cu_error.re_status = RPC_TIMEDOUT;
3912fe8fb19SBen Gras 		goto out;
3922fe8fb19SBen Gras 	}
3932fe8fb19SBen Gras 	/*
3942fe8fb19SBen Gras 	 * sub-optimal code appears here because we have
3952fe8fb19SBen Gras 	 * some clock time to spare while the packets are in flight.
3962fe8fb19SBen Gras 	 * (We assume that this is actually only executed once.)
3972fe8fb19SBen Gras 	 */
3982fe8fb19SBen Gras 	reply_msg.acpted_rply.ar_verf = _null_auth;
3992fe8fb19SBen Gras 	reply_msg.acpted_rply.ar_results.where = resultsp;
4002fe8fb19SBen Gras 	reply_msg.acpted_rply.ar_results.proc = xresults;
4012fe8fb19SBen Gras 
4022fe8fb19SBen Gras 
4032fe8fb19SBen Gras 	for (;;) {
4042fe8fb19SBen Gras 		/* Decide how long to wait. */
4052fe8fb19SBen Gras 		if (timercmp(&next_sendtime, &timeout, <))
4062fe8fb19SBen Gras 			timersub(&next_sendtime, &time_waited, &tv);
4072fe8fb19SBen Gras 		else
4082fe8fb19SBen Gras 			timersub(&timeout, &time_waited, &tv);
4092fe8fb19SBen Gras 		if (tv.tv_sec < 0 || tv.tv_usec < 0)
4102fe8fb19SBen Gras 			tv.tv_sec = tv.tv_usec = 0;
4112fe8fb19SBen Gras 		TIMEVAL_TO_TIMESPEC(&tv, &ts);
4122fe8fb19SBen Gras 
4132fe8fb19SBen Gras 		n = pollts(&cu->cu_pfdp, 1, &ts, maskp);
4142fe8fb19SBen Gras 		if (n == 1) {
4152fe8fb19SBen Gras 			/* We have some data now */
4162fe8fb19SBen Gras 			do {
4172fe8fb19SBen Gras 				recvlen = recvfrom(cu->cu_fd, cu->cu_inbuf,
4182fe8fb19SBen Gras 				    cu->cu_recvsz, 0, NULL, NULL);
4192fe8fb19SBen Gras 			} while (recvlen < 0 && errno == EINTR);
4202fe8fb19SBen Gras 
4212fe8fb19SBen Gras 			if (recvlen < 0 && errno != EWOULDBLOCK) {
4222fe8fb19SBen Gras 				cu->cu_error.re_errno = errno;
4232fe8fb19SBen Gras 				cu->cu_error.re_status = RPC_CANTRECV;
4242fe8fb19SBen Gras 				goto out;
4252fe8fb19SBen Gras 			}
426f14fb602SLionel Sambuc 			if (recvlen >= (ssize_t)sizeof(uint32_t)) {
427f14fb602SLionel Sambuc 				if (memcmp(cu->cu_inbuf, cu->cu_outbuf,
428f14fb602SLionel Sambuc 				    sizeof(uint32_t)) == 0)
429f14fb602SLionel Sambuc 					/* Assume we have the proper reply. */
4302fe8fb19SBen Gras 					break;
4312fe8fb19SBen Gras 			}
4322fe8fb19SBen Gras 		}
4332fe8fb19SBen Gras 		if (n == -1) {
4342fe8fb19SBen Gras 			cu->cu_error.re_errno = errno;
4352fe8fb19SBen Gras 			cu->cu_error.re_status = RPC_CANTRECV;
4362fe8fb19SBen Gras 			goto out;
4372fe8fb19SBen Gras 		}
4382fe8fb19SBen Gras 
4392fe8fb19SBen Gras 		gettimeofday(&tv, NULL);
4402fe8fb19SBen Gras 		timersub(&tv, &starttime, &time_waited);
4412fe8fb19SBen Gras 
4422fe8fb19SBen Gras 		/* Check for timeout. */
4432fe8fb19SBen Gras 		if (timercmp(&time_waited, &timeout, >)) {
4442fe8fb19SBen Gras 			cu->cu_error.re_status = RPC_TIMEDOUT;
4452fe8fb19SBen Gras 			goto out;
4462fe8fb19SBen Gras 		}
4472fe8fb19SBen Gras 
4482fe8fb19SBen Gras 		/* Retransmit if necessary. */
4492fe8fb19SBen Gras 		if (timercmp(&time_waited, &next_sendtime, >)) {
4502fe8fb19SBen Gras 			/* update retransmit_time */
4512fe8fb19SBen Gras 			if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
4522fe8fb19SBen Gras 				timeradd(&retransmit_time, &retransmit_time,
4532fe8fb19SBen Gras 				    &retransmit_time);
4542fe8fb19SBen Gras 			timeradd(&next_sendtime, &retransmit_time,
4552fe8fb19SBen Gras 			    &next_sendtime);
4562fe8fb19SBen Gras 			goto send_again;
4572fe8fb19SBen Gras 		}
4582fe8fb19SBen Gras 	}
4592fe8fb19SBen Gras 
4602fe8fb19SBen Gras 	/*
4612fe8fb19SBen Gras 	 * now decode and validate the response
4622fe8fb19SBen Gras 	 */
4632fe8fb19SBen Gras 
4642fe8fb19SBen Gras 	xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
4652fe8fb19SBen Gras 	ok = xdr_replymsg(&reply_xdrs, &reply_msg);
4662fe8fb19SBen Gras 	/* XDR_DESTROY(&reply_xdrs);	save a few cycles on noop destroy */
4672fe8fb19SBen Gras 	if (ok) {
4682fe8fb19SBen Gras 		if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
4692fe8fb19SBen Gras 			(reply_msg.acpted_rply.ar_stat == SUCCESS))
4702fe8fb19SBen Gras 			cu->cu_error.re_status = RPC_SUCCESS;
4712fe8fb19SBen Gras 		else
4722fe8fb19SBen Gras 			_seterr_reply(&reply_msg, &(cu->cu_error));
4732fe8fb19SBen Gras 
4742fe8fb19SBen Gras 		if (cu->cu_error.re_status == RPC_SUCCESS) {
4752fe8fb19SBen Gras 			if (! AUTH_VALIDATE(cl->cl_auth,
4762fe8fb19SBen Gras 					    &reply_msg.acpted_rply.ar_verf)) {
4772fe8fb19SBen Gras 				cu->cu_error.re_status = RPC_AUTHERROR;
4782fe8fb19SBen Gras 				cu->cu_error.re_why = AUTH_INVALIDRESP;
4792fe8fb19SBen Gras 			}
4802fe8fb19SBen Gras 			if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
4812fe8fb19SBen Gras 				xdrs->x_op = XDR_FREE;
4822fe8fb19SBen Gras 				(void) xdr_opaque_auth(xdrs,
4832fe8fb19SBen Gras 					&(reply_msg.acpted_rply.ar_verf));
4842fe8fb19SBen Gras 			}
4852fe8fb19SBen Gras 		}		/* end successful completion */
4862fe8fb19SBen Gras 		/*
4872fe8fb19SBen Gras 		 * If unsuccesful AND error is an authentication error
4882fe8fb19SBen Gras 		 * then refresh credentials and try again, else break
4892fe8fb19SBen Gras 		 */
4902fe8fb19SBen Gras 		else if (cu->cu_error.re_status == RPC_AUTHERROR)
4912fe8fb19SBen Gras 			/* maybe our credentials need to be refreshed ... */
4922fe8fb19SBen Gras 			if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
4932fe8fb19SBen Gras 				nrefreshes--;
4942fe8fb19SBen Gras 				goto call_again;
4952fe8fb19SBen Gras 			}
4962fe8fb19SBen Gras 		/* end of unsuccessful completion */
4972fe8fb19SBen Gras 	}	/* end of valid reply message */
4982fe8fb19SBen Gras 	else {
4992fe8fb19SBen Gras 		cu->cu_error.re_status = RPC_CANTDECODERES;
5002fe8fb19SBen Gras 
5012fe8fb19SBen Gras 	}
5022fe8fb19SBen Gras out:
5032fe8fb19SBen Gras 	release_fd_lock(cu->cu_fd, mask);
5042fe8fb19SBen Gras 	return (cu->cu_error.re_status);
5052fe8fb19SBen Gras }
5062fe8fb19SBen Gras 
5072fe8fb19SBen Gras static void
clnt_dg_geterr(CLIENT * cl,struct rpc_err * errp)508f14fb602SLionel Sambuc clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
5092fe8fb19SBen Gras {
5102fe8fb19SBen Gras 	struct cu_data *cu;
5112fe8fb19SBen Gras 
5122fe8fb19SBen Gras 	_DIAGASSERT(cl != NULL);
5132fe8fb19SBen Gras 	_DIAGASSERT(errp != NULL);
5142fe8fb19SBen Gras 
5152fe8fb19SBen Gras 	cu = (struct cu_data *)cl->cl_private;
5162fe8fb19SBen Gras 	*errp = cu->cu_error;
5172fe8fb19SBen Gras }
5182fe8fb19SBen Gras 
5192fe8fb19SBen Gras static bool_t
clnt_dg_freeres(CLIENT * cl,xdrproc_t xdr_res,caddr_t res_ptr)520f14fb602SLionel Sambuc clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
5212fe8fb19SBen Gras {
5222fe8fb19SBen Gras 	struct cu_data *cu;
5232fe8fb19SBen Gras 	XDR *xdrs;
5242fe8fb19SBen Gras 	bool_t dummy;
5252fe8fb19SBen Gras #ifdef _REENTRANT
5262fe8fb19SBen Gras 	sigset_t mask;
5272fe8fb19SBen Gras #endif
5282fe8fb19SBen Gras 	sigset_t newmask;
5292fe8fb19SBen Gras 
5302fe8fb19SBen Gras 	_DIAGASSERT(cl != NULL);
5312fe8fb19SBen Gras 	cu = (struct cu_data *)cl->cl_private;
5322fe8fb19SBen Gras 	xdrs = &(cu->cu_outxdrs);
5332fe8fb19SBen Gras 
534*84d9c625SLionel Sambuc 	__clnt_sigfillset(&newmask);
5352fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
5362fe8fb19SBen Gras 	mutex_lock(&clnt_fd_lock);
5372fe8fb19SBen Gras 	while (dg_fd_locks[cu->cu_fd])
5382fe8fb19SBen Gras 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
5392fe8fb19SBen Gras 	xdrs->x_op = XDR_FREE;
5402fe8fb19SBen Gras 	dummy = (*xdr_res)(xdrs, res_ptr);
5412fe8fb19SBen Gras 	mutex_unlock(&clnt_fd_lock);
5422fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
5432fe8fb19SBen Gras 	cond_signal(&dg_cv[cu->cu_fd]);
5442fe8fb19SBen Gras 	return (dummy);
5452fe8fb19SBen Gras }
5462fe8fb19SBen Gras 
5472fe8fb19SBen Gras /*ARGSUSED*/
5482fe8fb19SBen Gras static void
clnt_dg_abort(CLIENT * h)549f14fb602SLionel Sambuc clnt_dg_abort(CLIENT *h)
5502fe8fb19SBen Gras {
5512fe8fb19SBen Gras }
5522fe8fb19SBen Gras 
5532fe8fb19SBen Gras static bool_t
clnt_dg_control(CLIENT * cl,u_int request,char * info)554f14fb602SLionel Sambuc clnt_dg_control(CLIENT *cl, u_int request, char *info)
5552fe8fb19SBen Gras {
5562fe8fb19SBen Gras 	struct cu_data *cu;
5572fe8fb19SBen Gras 	struct netbuf *addr;
5582fe8fb19SBen Gras #ifdef _REENTRANT
5592fe8fb19SBen Gras 	sigset_t mask;
5602fe8fb19SBen Gras #endif
5612fe8fb19SBen Gras 	sigset_t newmask;
5622fe8fb19SBen Gras 
5632fe8fb19SBen Gras 	_DIAGASSERT(cl != NULL);
5642fe8fb19SBen Gras 	/* info is handled below */
5652fe8fb19SBen Gras 
5662fe8fb19SBen Gras 	cu = (struct cu_data *)cl->cl_private;
5672fe8fb19SBen Gras 
568*84d9c625SLionel Sambuc 	__clnt_sigfillset(&newmask);
5692fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
5702fe8fb19SBen Gras 	mutex_lock(&clnt_fd_lock);
5712fe8fb19SBen Gras 	while (dg_fd_locks[cu->cu_fd])
5722fe8fb19SBen Gras 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
5732fe8fb19SBen Gras 	dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
5742fe8fb19SBen Gras 	mutex_unlock(&clnt_fd_lock);
5752fe8fb19SBen Gras 	switch (request) {
5762fe8fb19SBen Gras 	case CLSET_FD_CLOSE:
5772fe8fb19SBen Gras 		cu->cu_closeit = TRUE;
5782fe8fb19SBen Gras 		release_fd_lock(cu->cu_fd, mask);
5792fe8fb19SBen Gras 		return (TRUE);
5802fe8fb19SBen Gras 	case CLSET_FD_NCLOSE:
5812fe8fb19SBen Gras 		cu->cu_closeit = FALSE;
5822fe8fb19SBen Gras 		release_fd_lock(cu->cu_fd, mask);
5832fe8fb19SBen Gras 		return (TRUE);
5842fe8fb19SBen Gras 	}
5852fe8fb19SBen Gras 
5862fe8fb19SBen Gras 	/* for other requests which use info */
5872fe8fb19SBen Gras 	if (info == NULL) {
5882fe8fb19SBen Gras 		release_fd_lock(cu->cu_fd, mask);
5892fe8fb19SBen Gras 		return (FALSE);
5902fe8fb19SBen Gras 	}
5912fe8fb19SBen Gras 	switch (request) {
5922fe8fb19SBen Gras 	case CLSET_TIMEOUT:
5932fe8fb19SBen Gras 		if (time_not_ok((struct timeval *)(void *)info)) {
5942fe8fb19SBen Gras 			release_fd_lock(cu->cu_fd, mask);
5952fe8fb19SBen Gras 			return (FALSE);
5962fe8fb19SBen Gras 		}
5972fe8fb19SBen Gras 		cu->cu_total = *(struct timeval *)(void *)info;
5982fe8fb19SBen Gras 		break;
5992fe8fb19SBen Gras 	case CLGET_TIMEOUT:
6002fe8fb19SBen Gras 		*(struct timeval *)(void *)info = cu->cu_total;
6012fe8fb19SBen Gras 		break;
6022fe8fb19SBen Gras 	case CLGET_SERVER_ADDR:		/* Give him the fd address */
6032fe8fb19SBen Gras 		/* Now obsolete. Only for backward compatibility */
6042fe8fb19SBen Gras 		(void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
6052fe8fb19SBen Gras 		break;
6062fe8fb19SBen Gras 	case CLSET_RETRY_TIMEOUT:
6072fe8fb19SBen Gras 		if (time_not_ok((struct timeval *)(void *)info)) {
6082fe8fb19SBen Gras 			release_fd_lock(cu->cu_fd, mask);
6092fe8fb19SBen Gras 			return (FALSE);
6102fe8fb19SBen Gras 		}
6112fe8fb19SBen Gras 		cu->cu_wait = *(struct timeval *)(void *)info;
6122fe8fb19SBen Gras 		break;
6132fe8fb19SBen Gras 	case CLGET_RETRY_TIMEOUT:
6142fe8fb19SBen Gras 		*(struct timeval *)(void *)info = cu->cu_wait;
6152fe8fb19SBen Gras 		break;
6162fe8fb19SBen Gras 	case CLGET_FD:
6172fe8fb19SBen Gras 		*(int *)(void *)info = cu->cu_fd;
6182fe8fb19SBen Gras 		break;
6192fe8fb19SBen Gras 	case CLGET_SVC_ADDR:
6202fe8fb19SBen Gras 		addr = (struct netbuf *)(void *)info;
6212fe8fb19SBen Gras 		addr->buf = &cu->cu_raddr;
6222fe8fb19SBen Gras 		addr->len = cu->cu_rlen;
6232fe8fb19SBen Gras 		addr->maxlen = sizeof cu->cu_raddr;
6242fe8fb19SBen Gras 		break;
6252fe8fb19SBen Gras 	case CLSET_SVC_ADDR:		/* set to new address */
6262fe8fb19SBen Gras 		addr = (struct netbuf *)(void *)info;
6272fe8fb19SBen Gras 		if (addr->len < sizeof cu->cu_raddr) {
6282fe8fb19SBen Gras 			release_fd_lock(cu->cu_fd, mask);
6292fe8fb19SBen Gras 			return (FALSE);
6302fe8fb19SBen Gras 		}
6312fe8fb19SBen Gras 		(void) memcpy(&cu->cu_raddr, addr->buf, (size_t)addr->len);
6322fe8fb19SBen Gras 		cu->cu_rlen = addr->len;
6332fe8fb19SBen Gras 		break;
6342fe8fb19SBen Gras 	case CLGET_XID:
6352fe8fb19SBen Gras 		/*
6362fe8fb19SBen Gras 		 * use the knowledge that xid is the
6372fe8fb19SBen Gras 		 * first element in the call structure *.
6382fe8fb19SBen Gras 		 * This will get the xid of the PREVIOUS call
6392fe8fb19SBen Gras 		 */
6402fe8fb19SBen Gras 		*(u_int32_t *)(void *)info =
6412fe8fb19SBen Gras 		    ntohl(*(u_int32_t *)(void *)cu->cu_outbuf);
6422fe8fb19SBen Gras 		break;
6432fe8fb19SBen Gras 
6442fe8fb19SBen Gras 	case CLSET_XID:
6452fe8fb19SBen Gras 		/* This will set the xid of the NEXT call */
6462fe8fb19SBen Gras 		*(u_int32_t *)(void *)cu->cu_outbuf =
6472fe8fb19SBen Gras 		    htonl(*(u_int32_t *)(void *)info - 1);
6482fe8fb19SBen Gras 		/* decrement by 1 as clnt_dg_call() increments once */
6492fe8fb19SBen Gras 		break;
6502fe8fb19SBen Gras 
6512fe8fb19SBen Gras 	case CLGET_VERS:
6522fe8fb19SBen Gras 		/*
6532fe8fb19SBen Gras 		 * This RELIES on the information that, in the call body,
6542fe8fb19SBen Gras 		 * the version number field is the fifth field from the
6552fe8fb19SBen Gras 		 * begining of the RPC header. MUST be changed if the
6562fe8fb19SBen Gras 		 * call_struct is changed
6572fe8fb19SBen Gras 		 */
6582fe8fb19SBen Gras 		*(u_int32_t *)(void *)info =
6592fe8fb19SBen Gras 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
6602fe8fb19SBen Gras 		    4 * BYTES_PER_XDR_UNIT));
6612fe8fb19SBen Gras 		break;
6622fe8fb19SBen Gras 
6632fe8fb19SBen Gras 	case CLSET_VERS:
6642fe8fb19SBen Gras 		*(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
6652fe8fb19SBen Gras 			= htonl(*(u_int32_t *)(void *)info);
6662fe8fb19SBen Gras 		break;
6672fe8fb19SBen Gras 
6682fe8fb19SBen Gras 	case CLGET_PROG:
6692fe8fb19SBen Gras 		/*
6702fe8fb19SBen Gras 		 * This RELIES on the information that, in the call body,
6712fe8fb19SBen Gras 		 * the program number field is the fourth field from the
6722fe8fb19SBen Gras 		 * begining of the RPC header. MUST be changed if the
6732fe8fb19SBen Gras 		 * call_struct is changed
6742fe8fb19SBen Gras 		 */
6752fe8fb19SBen Gras 		*(u_int32_t *)(void *)info =
6762fe8fb19SBen Gras 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
6772fe8fb19SBen Gras 		    3 * BYTES_PER_XDR_UNIT));
6782fe8fb19SBen Gras 		break;
6792fe8fb19SBen Gras 
6802fe8fb19SBen Gras 	case CLSET_PROG:
6812fe8fb19SBen Gras 		*(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
6822fe8fb19SBen Gras 			= htonl(*(u_int32_t *)(void *)info);
6832fe8fb19SBen Gras 		break;
6842fe8fb19SBen Gras 
6852fe8fb19SBen Gras 	default:
6862fe8fb19SBen Gras 		release_fd_lock(cu->cu_fd, mask);
6872fe8fb19SBen Gras 		return (FALSE);
6882fe8fb19SBen Gras 	}
6892fe8fb19SBen Gras 	release_fd_lock(cu->cu_fd, mask);
6902fe8fb19SBen Gras 	return (TRUE);
6912fe8fb19SBen Gras }
6922fe8fb19SBen Gras 
6932fe8fb19SBen Gras static void
clnt_dg_destroy(CLIENT * cl)694f14fb602SLionel Sambuc clnt_dg_destroy(CLIENT *cl)
6952fe8fb19SBen Gras {
6962fe8fb19SBen Gras 	struct cu_data *cu;
6972fe8fb19SBen Gras 	int cu_fd;
6982fe8fb19SBen Gras #ifdef _REENTRANT
6992fe8fb19SBen Gras 	sigset_t mask;
7002fe8fb19SBen Gras #endif
7012fe8fb19SBen Gras 	sigset_t newmask;
7022fe8fb19SBen Gras 
7032fe8fb19SBen Gras 	_DIAGASSERT(cl != NULL);
7042fe8fb19SBen Gras 
7052fe8fb19SBen Gras 	cu = (struct cu_data *)cl->cl_private;
7062fe8fb19SBen Gras 	cu_fd = cu->cu_fd;
7072fe8fb19SBen Gras 
708*84d9c625SLionel Sambuc 	__clnt_sigfillset(&newmask);
7092fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
7102fe8fb19SBen Gras 	mutex_lock(&clnt_fd_lock);
7112fe8fb19SBen Gras 	while (dg_fd_locks[cu_fd])
7122fe8fb19SBen Gras 		cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
7132fe8fb19SBen Gras 	if (cu->cu_closeit)
7142fe8fb19SBen Gras 		(void) close(cu_fd);
7152fe8fb19SBen Gras 	XDR_DESTROY(&(cu->cu_outxdrs));
7162fe8fb19SBen Gras 	mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
7172fe8fb19SBen Gras 	if (cl->cl_netid && cl->cl_netid[0])
7182fe8fb19SBen Gras 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
7192fe8fb19SBen Gras 	if (cl->cl_tp && cl->cl_tp[0])
7202fe8fb19SBen Gras 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
7212fe8fb19SBen Gras 	mem_free(cl, sizeof (CLIENT));
7222fe8fb19SBen Gras 	mutex_unlock(&clnt_fd_lock);
7232fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
7242fe8fb19SBen Gras 	cond_signal(&dg_cv[cu_fd]);
7252fe8fb19SBen Gras }
7262fe8fb19SBen Gras 
7272fe8fb19SBen Gras static struct clnt_ops *
clnt_dg_ops(void)728f14fb602SLionel Sambuc clnt_dg_ops(void)
7292fe8fb19SBen Gras {
7302fe8fb19SBen Gras 	static struct clnt_ops ops;
7312fe8fb19SBen Gras #ifdef _REENTRANT
7322fe8fb19SBen Gras 	extern mutex_t	ops_lock;
7332fe8fb19SBen Gras 	sigset_t mask;
7342fe8fb19SBen Gras #endif
7352fe8fb19SBen Gras 	sigset_t newmask;
7362fe8fb19SBen Gras 
7372fe8fb19SBen Gras /* VARIABLES PROTECTED BY ops_lock: ops */
7382fe8fb19SBen Gras 
739*84d9c625SLionel Sambuc 	__clnt_sigfillset(&newmask);
7402fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
7412fe8fb19SBen Gras 	mutex_lock(&ops_lock);
7422fe8fb19SBen Gras 	if (ops.cl_call == NULL) {
7432fe8fb19SBen Gras 		ops.cl_call = clnt_dg_call;
7442fe8fb19SBen Gras 		ops.cl_abort = clnt_dg_abort;
7452fe8fb19SBen Gras 		ops.cl_geterr = clnt_dg_geterr;
7462fe8fb19SBen Gras 		ops.cl_freeres = clnt_dg_freeres;
7472fe8fb19SBen Gras 		ops.cl_destroy = clnt_dg_destroy;
7482fe8fb19SBen Gras 		ops.cl_control = clnt_dg_control;
7492fe8fb19SBen Gras 	}
7502fe8fb19SBen Gras 	mutex_unlock(&ops_lock);
7512fe8fb19SBen Gras 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
7522fe8fb19SBen Gras 	return (&ops);
7532fe8fb19SBen Gras }
7542fe8fb19SBen Gras 
7552fe8fb19SBen Gras /*
7562fe8fb19SBen Gras  * Make sure that the time is not garbage.  -1 value is allowed.
7572fe8fb19SBen Gras  */
7582fe8fb19SBen Gras static bool_t
time_not_ok(struct timeval * t)759f14fb602SLionel Sambuc time_not_ok(struct timeval *t)
7602fe8fb19SBen Gras {
7612fe8fb19SBen Gras 
7622fe8fb19SBen Gras 	_DIAGASSERT(t != NULL);
7632fe8fb19SBen Gras 
7642fe8fb19SBen Gras 	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
7652fe8fb19SBen Gras 		t->tv_usec < -1 || t->tv_usec > 1000000);
7662fe8fb19SBen Gras }
767