Home | History | Annotate | Line # | Download | only in rpc
clnt_dg.c revision 1.1
      1  1.1  fvdl /*	$NetBSD: clnt_dg.c,v 1.1 2000/06/02 23:11:07 fvdl Exp $	*/
      2  1.1  fvdl 
      3  1.1  fvdl /*
      4  1.1  fvdl  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  1.1  fvdl  * unrestricted use provided that this legend is included on all tape
      6  1.1  fvdl  * media and as a part of the software program in whole or part.  Users
      7  1.1  fvdl  * may copy or modify Sun RPC without charge, but are not authorized
      8  1.1  fvdl  * to license or distribute it to anyone else except as part of a product or
      9  1.1  fvdl  * program developed by the user.
     10  1.1  fvdl  *
     11  1.1  fvdl  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  1.1  fvdl  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  1.1  fvdl  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  1.1  fvdl  *
     15  1.1  fvdl  * Sun RPC is provided with no support and without any obligation on the
     16  1.1  fvdl  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  1.1  fvdl  * modification or enhancement.
     18  1.1  fvdl  *
     19  1.1  fvdl  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  1.1  fvdl  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  1.1  fvdl  * OR ANY PART THEREOF.
     22  1.1  fvdl  *
     23  1.1  fvdl  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  1.1  fvdl  * or profits or other special, indirect and consequential damages, even if
     25  1.1  fvdl  * Sun has been advised of the possibility of such damages.
     26  1.1  fvdl  *
     27  1.1  fvdl  * Sun Microsystems, Inc.
     28  1.1  fvdl  * 2550 Garcia Avenue
     29  1.1  fvdl  * Mountain View, California  94043
     30  1.1  fvdl  */
     31  1.1  fvdl /*
     32  1.1  fvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     33  1.1  fvdl  */
     34  1.1  fvdl 
     35  1.1  fvdl /* #ident	"@(#)clnt_dg.c	1.23	94/04/22 SMI" */
     36  1.1  fvdl 
     37  1.1  fvdl #if 0
     38  1.1  fvdl #if !defined(lint) && defined(SCCSIDS)
     39  1.1  fvdl static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
     40  1.1  fvdl #endif
     41  1.1  fvdl #endif
     42  1.1  fvdl 
     43  1.1  fvdl /*
     44  1.1  fvdl  * Implements a connectionless client side RPC.
     45  1.1  fvdl  */
     46  1.1  fvdl 
     47  1.1  fvdl #include "namespace.h"
     48  1.1  fvdl #include "reentrant.h"
     49  1.1  fvdl #include <sys/poll.h>
     50  1.1  fvdl #include <sys/types.h>
     51  1.1  fvdl #include <sys/time.h>
     52  1.1  fvdl #include <sys/socket.h>
     53  1.1  fvdl #include <sys/ioctl.h>
     54  1.1  fvdl #include <rpc/rpc.h>
     55  1.1  fvdl #include <errno.h>
     56  1.1  fvdl #include <stdlib.h>
     57  1.1  fvdl #include <signal.h>
     58  1.1  fvdl #include <unistd.h>
     59  1.1  fvdl #include <err.h>
     60  1.1  fvdl #include "rpc_com.h"
     61  1.1  fvdl 
     62  1.1  fvdl #ifdef __weak_alias
     63  1.1  fvdl __weak_alias(clnt_dg_create,_clnt_dg_create)
     64  1.1  fvdl #endif
     65  1.1  fvdl 
     66  1.1  fvdl #define	RPC_MAX_BACKOFF		30 /* seconds */
     67  1.1  fvdl 
     68  1.1  fvdl 
     69  1.1  fvdl static struct clnt_ops *clnt_dg_ops __P((void));
     70  1.1  fvdl static bool_t time_not_ok __P((struct timeval *));
     71  1.1  fvdl static enum clnt_stat clnt_dg_call __P((CLIENT *, rpcproc_t, xdrproc_t, caddr_t,
     72  1.1  fvdl 					xdrproc_t, caddr_t, struct timeval));
     73  1.1  fvdl static void clnt_dg_geterr __P((CLIENT *, struct rpc_err *));
     74  1.1  fvdl static bool_t clnt_dg_freeres __P((CLIENT *, xdrproc_t, caddr_t));
     75  1.1  fvdl static void clnt_dg_abort __P((CLIENT *));
     76  1.1  fvdl static bool_t clnt_dg_control __P((CLIENT *, u_int, char *));
     77  1.1  fvdl static void clnt_dg_destroy __P((CLIENT *));
     78  1.1  fvdl static int __rpc_timeval_to_msec __P((struct timeval *));
     79  1.1  fvdl 
     80  1.1  fvdl 
     81  1.1  fvdl 
     82  1.1  fvdl 
     83  1.1  fvdl /*
     84  1.1  fvdl  *	This machinery implements per-fd locks for MT-safety.  It is not
     85  1.1  fvdl  *	sufficient to do per-CLIENT handle locks for MT-safety because a
     86  1.1  fvdl  *	user may create more than one CLIENT handle with the same fd behind
     87  1.1  fvdl  *	it.  Therfore, we allocate an array of flags (dg_fd_locks), protected
     88  1.1  fvdl  *	by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables
     89  1.1  fvdl  *	similarly protected.  Dg_fd_lock[fd] == 1 => a call is activte on some
     90  1.1  fvdl  *	CLIENT handle created for that fd.
     91  1.1  fvdl  *	The current implementation holds locks across the entire RPC and reply,
     92  1.1  fvdl  *	including retransmissions.  Yes, this is silly, and as soon as this
     93  1.1  fvdl  *	code is proven to work, this should be the first thing fixed.  One step
     94  1.1  fvdl  *	at a time.
     95  1.1  fvdl  */
     96  1.1  fvdl static int	*dg_fd_locks;
     97  1.1  fvdl #ifdef __REENT
     98  1.1  fvdl extern int __rpc_lock_value;
     99  1.1  fvdl extern mutex_t clnt_fd_lock;
    100  1.1  fvdl static cond_t	*dg_cv;
    101  1.1  fvdl #define	release_fd_lock(fd, mask) {		\
    102  1.1  fvdl 	mutex_lock(&clnt_fd_lock);	\
    103  1.1  fvdl 	dg_fd_locks[fd] = 0;		\
    104  1.1  fvdl 	mutex_unlock(&clnt_fd_lock);	\
    105  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);	\
    106  1.1  fvdl 	cond_signal(&dg_cv[fd]);	\
    107  1.1  fvdl }
    108  1.1  fvdl #else
    109  1.1  fvdl #define release_fd_lock(fd,mask)
    110  1.1  fvdl #define __rpc_lock_value 0
    111  1.1  fvdl #endif
    112  1.1  fvdl 
    113  1.1  fvdl static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
    114  1.1  fvdl 
    115  1.1  fvdl /* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */
    116  1.1  fvdl 
    117  1.1  fvdl /*
    118  1.1  fvdl  * Private data kept per client handle
    119  1.1  fvdl  */
    120  1.1  fvdl struct cu_data {
    121  1.1  fvdl 	int			cu_fd;		/* connections fd */
    122  1.1  fvdl 	bool_t			cu_closeit;	/* opened by library */
    123  1.1  fvdl 	struct sockaddr_storage	cu_raddr;	/* remote address */
    124  1.1  fvdl 	int			cu_rlen;
    125  1.1  fvdl 	struct timeval		cu_wait;	/* retransmit interval */
    126  1.1  fvdl 	struct timeval		cu_total;	/* total time for the call */
    127  1.1  fvdl 	struct rpc_err		cu_error;
    128  1.1  fvdl 	XDR			cu_outxdrs;
    129  1.1  fvdl 	u_int			cu_xdrpos;
    130  1.1  fvdl 	u_int			cu_sendsz;	/* send size */
    131  1.1  fvdl 	char			*cu_outbuf;
    132  1.1  fvdl 	u_int			cu_recvsz;	/* recv size */
    133  1.1  fvdl 	struct pollfd		pfdp;
    134  1.1  fvdl 	char			cu_inbuf[1];
    135  1.1  fvdl };
    136  1.1  fvdl 
    137  1.1  fvdl /*
    138  1.1  fvdl  * Connection less client creation returns with client handle parameters.
    139  1.1  fvdl  * Default options are set, which the user can change using clnt_control().
    140  1.1  fvdl  * fd should be open and bound.
    141  1.1  fvdl  * NB: The rpch->cl_auth is initialized to null authentication.
    142  1.1  fvdl  * 	Caller may wish to set this something more useful.
    143  1.1  fvdl  *
    144  1.1  fvdl  * sendsz and recvsz are the maximum allowable packet sizes that can be
    145  1.1  fvdl  * sent and received. Normally they are the same, but they can be
    146  1.1  fvdl  * changed to improve the program efficiency and buffer allocation.
    147  1.1  fvdl  * If they are 0, use the transport default.
    148  1.1  fvdl  *
    149  1.1  fvdl  * If svcaddr is NULL, returns NULL.
    150  1.1  fvdl  */
    151  1.1  fvdl CLIENT *
    152  1.1  fvdl clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
    153  1.1  fvdl 	int fd;				/* open file descriptor */
    154  1.1  fvdl 	const struct netbuf *svcaddr;		/* servers address */
    155  1.1  fvdl 	rpcprog_t program;			/* program number */
    156  1.1  fvdl 	rpcvers_t version;			/* version number */
    157  1.1  fvdl 	u_int sendsz;			/* buffer recv size */
    158  1.1  fvdl 	u_int recvsz;			/* buffer send size */
    159  1.1  fvdl {
    160  1.1  fvdl 	CLIENT *cl = NULL;			/* client handle */
    161  1.1  fvdl 	register struct cu_data *cu = NULL;	/* private data */
    162  1.1  fvdl 	struct timeval now;
    163  1.1  fvdl 	struct rpc_msg call_msg;
    164  1.1  fvdl #ifdef __REENT
    165  1.1  fvdl 	sigset_t mask;
    166  1.1  fvdl #endif
    167  1.1  fvdl 	sigset_t newmask;
    168  1.1  fvdl 	struct __rpc_sockinfo si;
    169  1.1  fvdl 	int one = 1;
    170  1.1  fvdl 
    171  1.1  fvdl 	sigfillset(&newmask);
    172  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    173  1.1  fvdl 	mutex_lock(&clnt_fd_lock);
    174  1.1  fvdl 	if (dg_fd_locks == (int *) NULL) {
    175  1.1  fvdl #ifdef __REENT
    176  1.1  fvdl 		int cv_allocsz;
    177  1.1  fvdl #endif
    178  1.1  fvdl 		int fd_allocsz;
    179  1.1  fvdl 		int dtbsize = __rpc_dtbsize();
    180  1.1  fvdl 
    181  1.1  fvdl 		fd_allocsz = dtbsize * sizeof (int);
    182  1.1  fvdl 		dg_fd_locks = (int *) mem_alloc(fd_allocsz);
    183  1.1  fvdl 		if (dg_fd_locks == (int *) NULL) {
    184  1.1  fvdl 			mutex_unlock(&clnt_fd_lock);
    185  1.1  fvdl 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    186  1.1  fvdl 			goto err1;
    187  1.1  fvdl 		} else
    188  1.1  fvdl 			memset(dg_fd_locks, '\0', fd_allocsz);
    189  1.1  fvdl 
    190  1.1  fvdl #ifdef __REENT
    191  1.1  fvdl 		cv_allocsz = dtbsize * sizeof (cond_t);
    192  1.1  fvdl 		dg_cv = (cond_t *) mem_alloc(cv_allocsz);
    193  1.1  fvdl 		if (dg_cv == (cond_t *) NULL) {
    194  1.1  fvdl 			mem_free(dg_fd_locks, fd_allocsz);
    195  1.1  fvdl 			dg_fd_locks = (int *) NULL;
    196  1.1  fvdl 			mutex_unlock(&clnt_fd_lock);
    197  1.1  fvdl 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    198  1.1  fvdl 			goto err1;
    199  1.1  fvdl 		} else {
    200  1.1  fvdl 			int i;
    201  1.1  fvdl 
    202  1.1  fvdl 			for (i = 0; i < dtbsize; i++)
    203  1.1  fvdl 				cond_init(&dg_cv[i], 0, (void *) 0);
    204  1.1  fvdl 		}
    205  1.1  fvdl #endif
    206  1.1  fvdl 	}
    207  1.1  fvdl 
    208  1.1  fvdl 	mutex_unlock(&clnt_fd_lock);
    209  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
    210  1.1  fvdl 
    211  1.1  fvdl 	if (svcaddr == (struct netbuf *)NULL) {
    212  1.1  fvdl 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
    213  1.1  fvdl 		return ((CLIENT *)NULL);
    214  1.1  fvdl 	}
    215  1.1  fvdl 
    216  1.1  fvdl 	if (!__rpc_fd2sockinfo(fd, &si)) {
    217  1.1  fvdl 		rpc_createerr.cf_stat = RPC_TLIERROR;
    218  1.1  fvdl 		rpc_createerr.cf_error.re_errno = 0;
    219  1.1  fvdl 		return ((CLIENT *)NULL);
    220  1.1  fvdl 	}
    221  1.1  fvdl 	/*
    222  1.1  fvdl 	 * Find the receive and the send size
    223  1.1  fvdl 	 */
    224  1.1  fvdl 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
    225  1.1  fvdl 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
    226  1.1  fvdl 	if ((sendsz == 0) || (recvsz == 0)) {
    227  1.1  fvdl 		rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
    228  1.1  fvdl 		rpc_createerr.cf_error.re_errno = 0;
    229  1.1  fvdl 		return ((CLIENT *)NULL);
    230  1.1  fvdl 	}
    231  1.1  fvdl 
    232  1.1  fvdl 	if ((cl = (CLIENT *)mem_alloc(sizeof (CLIENT))) == (CLIENT *)NULL)
    233  1.1  fvdl 		goto err1;
    234  1.1  fvdl 	/*
    235  1.1  fvdl 	 * Should be multiple of 4 for XDR.
    236  1.1  fvdl 	 */
    237  1.1  fvdl 	sendsz = ((sendsz + 3) / 4) * 4;
    238  1.1  fvdl 	recvsz = ((recvsz + 3) / 4) * 4;
    239  1.1  fvdl 	cu = (struct cu_data *)mem_alloc(sizeof (*cu) + sendsz + recvsz);
    240  1.1  fvdl 	if (cu == (struct cu_data *)NULL)
    241  1.1  fvdl 		goto err1;
    242  1.1  fvdl 	(void) memcpy(&cu->cu_raddr, svcaddr->buf, (int)svcaddr->len);
    243  1.1  fvdl 	cu->cu_rlen = svcaddr->len;
    244  1.1  fvdl 	cu->cu_outbuf = &cu->cu_inbuf[recvsz];
    245  1.1  fvdl 	/* Other values can also be set through clnt_control() */
    246  1.1  fvdl 	cu->cu_wait.tv_sec = 15;	/* heuristically chosen */
    247  1.1  fvdl 	cu->cu_wait.tv_usec = 0;
    248  1.1  fvdl 	cu->cu_total.tv_sec = -1;
    249  1.1  fvdl 	cu->cu_total.tv_usec = -1;
    250  1.1  fvdl 	cu->cu_sendsz = sendsz;
    251  1.1  fvdl 	cu->cu_recvsz = recvsz;
    252  1.1  fvdl 	(void) gettimeofday(&now, (struct timezone *)NULL);
    253  1.1  fvdl 	call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
    254  1.1  fvdl 	call_msg.rm_call.cb_prog = program;
    255  1.1  fvdl 	call_msg.rm_call.cb_vers = version;
    256  1.1  fvdl 	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
    257  1.1  fvdl 	if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
    258  1.1  fvdl 		rpc_createerr.cf_stat = RPC_CANTENCODEARGS;  /* XXX */
    259  1.1  fvdl 		rpc_createerr.cf_error.re_errno = 0;
    260  1.1  fvdl 		goto err2;
    261  1.1  fvdl 	}
    262  1.1  fvdl 	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
    263  1.1  fvdl 
    264  1.1  fvdl 	/* XXX fvdl - do we still want this? */
    265  1.1  fvdl #if 0
    266  1.1  fvdl 	(void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
    267  1.1  fvdl #endif
    268  1.1  fvdl 	ioctl(fd, FIONBIO, (char *)(void *)&one);
    269  1.1  fvdl 
    270  1.1  fvdl 	/*
    271  1.1  fvdl 	 * By default, closeit is always FALSE. It is users responsibility
    272  1.1  fvdl 	 * to do a close on it, else the user may use clnt_control
    273  1.1  fvdl 	 * to let clnt_destroy do it for him/her.
    274  1.1  fvdl 	 */
    275  1.1  fvdl 	cu->cu_closeit = FALSE;
    276  1.1  fvdl 	cu->cu_fd = fd;
    277  1.1  fvdl 	cl->cl_ops = clnt_dg_ops();
    278  1.1  fvdl 	cl->cl_private = (caddr_t)cu;
    279  1.1  fvdl 	cl->cl_auth = authnone_create();
    280  1.1  fvdl 	cl->cl_tp = (char *) NULL;
    281  1.1  fvdl 	cl->cl_netid = (char *) NULL;
    282  1.1  fvdl 	cu->pfdp.fd = cu->cu_fd;
    283  1.1  fvdl 	cu->pfdp.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
    284  1.1  fvdl 	return (cl);
    285  1.1  fvdl err1:
    286  1.1  fvdl 	warnx(mem_err_clnt_dg);
    287  1.1  fvdl 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    288  1.1  fvdl 	rpc_createerr.cf_error.re_errno = errno;
    289  1.1  fvdl err2:
    290  1.1  fvdl 	if (cl) {
    291  1.1  fvdl 		mem_free((caddr_t)cl, sizeof (CLIENT));
    292  1.1  fvdl 		if (cu)
    293  1.1  fvdl 			mem_free((caddr_t)cu, sizeof (*cu) + sendsz + recvsz);
    294  1.1  fvdl 	}
    295  1.1  fvdl 	return ((CLIENT *)NULL);
    296  1.1  fvdl }
    297  1.1  fvdl 
    298  1.1  fvdl static enum clnt_stat
    299  1.1  fvdl clnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
    300  1.1  fvdl 	register CLIENT	*cl;		/* client handle */
    301  1.1  fvdl 	rpcproc_t	proc;		/* procedure number */
    302  1.1  fvdl 	xdrproc_t	xargs;		/* xdr routine for args */
    303  1.1  fvdl 	caddr_t		argsp;		/* pointer to args */
    304  1.1  fvdl 	xdrproc_t	xresults;	/* xdr routine for results */
    305  1.1  fvdl 	caddr_t		resultsp;	/* pointer to results */
    306  1.1  fvdl 	struct timeval	utimeout;	/* seconds to wait before giving up */
    307  1.1  fvdl {
    308  1.1  fvdl 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
    309  1.1  fvdl 	register XDR *xdrs;
    310  1.1  fvdl 	register int outlen;
    311  1.1  fvdl 	struct rpc_msg reply_msg;
    312  1.1  fvdl 	XDR reply_xdrs;
    313  1.1  fvdl 	struct timeval time_waited;
    314  1.1  fvdl 	bool_t ok;
    315  1.1  fvdl 	int nrefreshes = 2;		/* number of times to refresh cred */
    316  1.1  fvdl 	struct timeval timeout;
    317  1.1  fvdl 	struct timeval retransmit_time;
    318  1.1  fvdl 	struct timeval startime, curtime;
    319  1.1  fvdl 	int firsttimeout = 1;
    320  1.1  fvdl #ifdef __REENT
    321  1.1  fvdl 	int dtbsize = __rpc_dtbsize();
    322  1.1  fvdl 	sigset_t mask;
    323  1.1  fvdl #endif
    324  1.1  fvdl 	sigset_t newmask;
    325  1.1  fvdl 	int fromlen, inlen = 0;
    326  1.1  fvdl 
    327  1.1  fvdl 	sigfillset(&newmask);
    328  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    329  1.1  fvdl 	mutex_lock(&clnt_fd_lock);
    330  1.1  fvdl 	while (dg_fd_locks[cu->cu_fd])
    331  1.1  fvdl 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
    332  1.1  fvdl 	dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
    333  1.1  fvdl 	mutex_unlock(&clnt_fd_lock);
    334  1.1  fvdl 	if (cu->cu_total.tv_usec == -1) {
    335  1.1  fvdl 		timeout = utimeout;	/* use supplied timeout */
    336  1.1  fvdl 	} else {
    337  1.1  fvdl 		timeout = cu->cu_total;	/* use default timeout */
    338  1.1  fvdl 	}
    339  1.1  fvdl 
    340  1.1  fvdl 	time_waited.tv_sec = 0;
    341  1.1  fvdl 	time_waited.tv_usec = 0;
    342  1.1  fvdl 	retransmit_time = cu->cu_wait;
    343  1.1  fvdl 
    344  1.1  fvdl call_again:
    345  1.1  fvdl 	xdrs = &(cu->cu_outxdrs);
    346  1.1  fvdl 	xdrs->x_op = XDR_ENCODE;
    347  1.1  fvdl 	XDR_SETPOS(xdrs, cu->cu_xdrpos);
    348  1.1  fvdl 	/*
    349  1.1  fvdl 	 * the transaction is the first thing in the out buffer
    350  1.1  fvdl 	 */
    351  1.1  fvdl 	(*(u_int32_t *)(cu->cu_outbuf))++;
    352  1.1  fvdl 	if ((! XDR_PUTLONG(xdrs, (long *)&proc)) ||
    353  1.1  fvdl 	    (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
    354  1.1  fvdl 	    (! (*xargs)(xdrs, argsp))) {
    355  1.1  fvdl 		release_fd_lock(cu->cu_fd, mask);
    356  1.1  fvdl 		return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
    357  1.1  fvdl 	}
    358  1.1  fvdl 	outlen = (int)XDR_GETPOS(xdrs);
    359  1.1  fvdl 
    360  1.1  fvdl send_again:
    361  1.1  fvdl 	if (sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0,
    362  1.1  fvdl 	    (struct sockaddr *)&cu->cu_raddr, (socklen_t)cu->cu_rlen)
    363  1.1  fvdl 	    != outlen) {
    364  1.1  fvdl 		cu->cu_error.re_errno = errno;
    365  1.1  fvdl 		release_fd_lock(cu->cu_fd, mask);
    366  1.1  fvdl 		return (cu->cu_error.re_status = RPC_CANTSEND);
    367  1.1  fvdl 	}
    368  1.1  fvdl 
    369  1.1  fvdl 	/*
    370  1.1  fvdl 	 * Hack to provide rpc-based message passing
    371  1.1  fvdl 	 */
    372  1.1  fvdl 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
    373  1.1  fvdl 		release_fd_lock(cu->cu_fd, mask);
    374  1.1  fvdl 		return (cu->cu_error.re_status = RPC_TIMEDOUT);
    375  1.1  fvdl 	}
    376  1.1  fvdl 	/*
    377  1.1  fvdl 	 * sub-optimal code appears here because we have
    378  1.1  fvdl 	 * some clock time to spare while the packets are in flight.
    379  1.1  fvdl 	 * (We assume that this is actually only executed once.)
    380  1.1  fvdl 	 */
    381  1.1  fvdl 	reply_msg.acpted_rply.ar_verf = _null_auth;
    382  1.1  fvdl 	reply_msg.acpted_rply.ar_results.where = resultsp;
    383  1.1  fvdl 	reply_msg.acpted_rply.ar_results.proc = xresults;
    384  1.1  fvdl 
    385  1.1  fvdl 
    386  1.1  fvdl 	for (;;) {
    387  1.1  fvdl 		int fds;
    388  1.1  fvdl 
    389  1.1  fvdl 		switch (fds = poll(&cu->pfdp, 1,
    390  1.1  fvdl 				__rpc_timeval_to_msec(&retransmit_time))) {
    391  1.1  fvdl 		case 0:
    392  1.1  fvdl 			time_waited.tv_sec += retransmit_time.tv_sec;
    393  1.1  fvdl 			time_waited.tv_usec += retransmit_time.tv_usec;
    394  1.1  fvdl 			while (time_waited.tv_usec >= 1000000) {
    395  1.1  fvdl 				time_waited.tv_sec++;
    396  1.1  fvdl 				time_waited.tv_usec -= 1000000;
    397  1.1  fvdl 			}
    398  1.1  fvdl 			/* update retransmit_time */
    399  1.1  fvdl 			if (retransmit_time.tv_sec < RPC_MAX_BACKOFF) {
    400  1.1  fvdl 				retransmit_time.tv_usec *= 2;
    401  1.1  fvdl 				retransmit_time.tv_sec *= 2;
    402  1.1  fvdl 				while (retransmit_time.tv_usec >= 1000000) {
    403  1.1  fvdl 					retransmit_time.tv_sec++;
    404  1.1  fvdl 					retransmit_time.tv_usec -= 1000000;
    405  1.1  fvdl 				}
    406  1.1  fvdl 			}
    407  1.1  fvdl 
    408  1.1  fvdl 			if ((time_waited.tv_sec < timeout.tv_sec) ||
    409  1.1  fvdl 			    ((time_waited.tv_sec == timeout.tv_sec) &&
    410  1.1  fvdl 				(time_waited.tv_usec < timeout.tv_usec)))
    411  1.1  fvdl 				goto send_again;
    412  1.1  fvdl 			release_fd_lock(cu->cu_fd, mask);
    413  1.1  fvdl 			return (cu->cu_error.re_status = RPC_TIMEDOUT);
    414  1.1  fvdl 
    415  1.1  fvdl 		case -1:
    416  1.1  fvdl 			if (errno == EBADF) {
    417  1.1  fvdl 				cu->cu_error.re_errno = errno;
    418  1.1  fvdl 				release_fd_lock(cu->cu_fd, mask);
    419  1.1  fvdl 				return (cu->cu_error.re_status = RPC_CANTRECV);
    420  1.1  fvdl 			}
    421  1.1  fvdl 			if (errno != EINTR) {
    422  1.1  fvdl 				errno = 0; /* reset it */
    423  1.1  fvdl 				continue;
    424  1.1  fvdl 			}
    425  1.1  fvdl 			/* interrupted by another signal, update time_waited */
    426  1.1  fvdl 			if (firsttimeout) {
    427  1.1  fvdl 				/*
    428  1.1  fvdl 				 * Could have done gettimeofday before clnt_call
    429  1.1  fvdl 				 * but that means 1 more system call per each
    430  1.1  fvdl 				 * clnt_call, so do it after first time out
    431  1.1  fvdl 				 */
    432  1.1  fvdl 				if (gettimeofday(&startime,
    433  1.1  fvdl 					(struct timezone *) NULL) == -1) {
    434  1.1  fvdl 					errno = 0;
    435  1.1  fvdl 					continue;
    436  1.1  fvdl 				}
    437  1.1  fvdl 				firsttimeout = 0;
    438  1.1  fvdl 				errno = 0;
    439  1.1  fvdl 				continue;
    440  1.1  fvdl 			};
    441  1.1  fvdl 			if (gettimeofday(&curtime,
    442  1.1  fvdl 				(struct timezone *) NULL) == -1) {
    443  1.1  fvdl 				errno = 0;
    444  1.1  fvdl 				continue;
    445  1.1  fvdl 			};
    446  1.1  fvdl 			time_waited.tv_sec += curtime.tv_sec - startime.tv_sec;
    447  1.1  fvdl 			time_waited.tv_usec += curtime.tv_usec -
    448  1.1  fvdl 							startime.tv_usec;
    449  1.1  fvdl 			while (time_waited.tv_usec < 0) {
    450  1.1  fvdl 				time_waited.tv_sec--;
    451  1.1  fvdl 				time_waited.tv_usec += 1000000;
    452  1.1  fvdl 			};
    453  1.1  fvdl 			while (time_waited.tv_usec >= 1000000) {
    454  1.1  fvdl 				time_waited.tv_sec++;
    455  1.1  fvdl 				time_waited.tv_usec -= 1000000;
    456  1.1  fvdl 			}
    457  1.1  fvdl 			startime.tv_sec = curtime.tv_sec;
    458  1.1  fvdl 			startime.tv_usec = curtime.tv_usec;
    459  1.1  fvdl 			if ((time_waited.tv_sec > timeout.tv_sec) ||
    460  1.1  fvdl 				((time_waited.tv_sec == timeout.tv_sec) &&
    461  1.1  fvdl 				(time_waited.tv_usec > timeout.tv_usec))) {
    462  1.1  fvdl 				release_fd_lock(cu->cu_fd, mask);
    463  1.1  fvdl 				return (cu->cu_error.re_status = RPC_TIMEDOUT);
    464  1.1  fvdl 			}
    465  1.1  fvdl 			errno = 0; /* reset it */
    466  1.1  fvdl 			continue;
    467  1.1  fvdl 		};
    468  1.1  fvdl 
    469  1.1  fvdl 		if (cu->pfdp.revents & POLLNVAL || (cu->pfdp.revents == 0)) {
    470  1.1  fvdl 			cu->cu_error.re_status = RPC_CANTRECV;
    471  1.1  fvdl 			/*
    472  1.1  fvdl 			 *	Note:  we're faking errno here because we
    473  1.1  fvdl 			 *	previously would have expected poll() to
    474  1.1  fvdl 			 *	return -1 with errno EBADF.  Poll(BA_OS)
    475  1.1  fvdl 			 *	returns 0 and sets the POLLNVAL revents flag
    476  1.1  fvdl 			 *	instead.
    477  1.1  fvdl 			 */
    478  1.1  fvdl 			cu->cu_error.re_errno = errno = EBADF;
    479  1.1  fvdl 			release_fd_lock(cu->cu_fd, mask);
    480  1.1  fvdl 			return (-1);
    481  1.1  fvdl 		}
    482  1.1  fvdl 
    483  1.1  fvdl 		/* We have some data now */
    484  1.1  fvdl 		do {
    485  1.1  fvdl 			if (errno == EINTR) {
    486  1.1  fvdl 				/*
    487  1.1  fvdl 				 * Must make sure errno was not already
    488  1.1  fvdl 				 * EINTR in case recvfrom() returns -1.
    489  1.1  fvdl 				 */
    490  1.1  fvdl 				errno = 0;
    491  1.1  fvdl 			}
    492  1.1  fvdl 			fromlen = sizeof (struct sockaddr_storage);
    493  1.1  fvdl 			inlen = recvfrom(cu->cu_fd, cu->cu_inbuf,
    494  1.1  fvdl 			    cu->cu_recvsz, 0, (struct sockaddr *)&cu->cu_raddr,
    495  1.1  fvdl 			    &fromlen);
    496  1.1  fvdl 		} while (inlen < 0 && errno == EINTR);
    497  1.1  fvdl 		if (inlen < 0) {
    498  1.1  fvdl 			if (errno == EWOULDBLOCK)
    499  1.1  fvdl 				continue;
    500  1.1  fvdl 			cu->cu_error.re_errno = errno;
    501  1.1  fvdl 			release_fd_lock(cu->cu_fd, mask);
    502  1.1  fvdl 			return (cu->cu_error.re_status = RPC_CANTRECV);
    503  1.1  fvdl 		}
    504  1.1  fvdl 		if (inlen < sizeof (u_int32_t))
    505  1.1  fvdl 			continue;
    506  1.1  fvdl 		/* see if reply transaction id matches sent id */
    507  1.1  fvdl 		if (*((u_int32_t *)(cu->cu_inbuf)) !=
    508  1.1  fvdl 		    *((u_int32_t *)(cu->cu_outbuf)))
    509  1.1  fvdl 			continue;
    510  1.1  fvdl 		/* we now assume we have the proper reply */
    511  1.1  fvdl 		break;
    512  1.1  fvdl 	}
    513  1.1  fvdl 
    514  1.1  fvdl 	/*
    515  1.1  fvdl 	 * now decode and validate the response
    516  1.1  fvdl 	 */
    517  1.1  fvdl 
    518  1.1  fvdl 	xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
    519  1.1  fvdl 	ok = xdr_replymsg(&reply_xdrs, &reply_msg);
    520  1.1  fvdl 	/* XDR_DESTROY(&reply_xdrs);	save a few cycles on noop destroy */
    521  1.1  fvdl 	if (ok) {
    522  1.1  fvdl 		if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    523  1.1  fvdl 			(reply_msg.acpted_rply.ar_stat == SUCCESS))
    524  1.1  fvdl 			cu->cu_error.re_status = RPC_SUCCESS;
    525  1.1  fvdl 		else
    526  1.1  fvdl 			_seterr_reply(&reply_msg, &(cu->cu_error));
    527  1.1  fvdl 
    528  1.1  fvdl 		if (cu->cu_error.re_status == RPC_SUCCESS) {
    529  1.1  fvdl 			if (! AUTH_VALIDATE(cl->cl_auth,
    530  1.1  fvdl 					    &reply_msg.acpted_rply.ar_verf)) {
    531  1.1  fvdl 				cu->cu_error.re_status = RPC_AUTHERROR;
    532  1.1  fvdl 				cu->cu_error.re_why = AUTH_INVALIDRESP;
    533  1.1  fvdl 			}
    534  1.1  fvdl 			if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
    535  1.1  fvdl 				xdrs->x_op = XDR_FREE;
    536  1.1  fvdl 				(void) xdr_opaque_auth(xdrs,
    537  1.1  fvdl 					&(reply_msg.acpted_rply.ar_verf));
    538  1.1  fvdl 			}
    539  1.1  fvdl 		}		/* end successful completion */
    540  1.1  fvdl 		/*
    541  1.1  fvdl 		 * If unsuccesful AND error is an authentication error
    542  1.1  fvdl 		 * then refresh credentials and try again, else break
    543  1.1  fvdl 		 */
    544  1.1  fvdl 		else if (cu->cu_error.re_status == RPC_AUTHERROR)
    545  1.1  fvdl 			/* maybe our credentials need to be refreshed ... */
    546  1.1  fvdl 			if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
    547  1.1  fvdl 				nrefreshes--;
    548  1.1  fvdl 				goto call_again;
    549  1.1  fvdl 			}
    550  1.1  fvdl 		/* end of unsuccessful completion */
    551  1.1  fvdl 	}	/* end of valid reply message */
    552  1.1  fvdl 	else {
    553  1.1  fvdl 		cu->cu_error.re_status = RPC_CANTDECODERES;
    554  1.1  fvdl 
    555  1.1  fvdl 	}
    556  1.1  fvdl 	release_fd_lock(cu->cu_fd, mask);
    557  1.1  fvdl 	return (cu->cu_error.re_status);
    558  1.1  fvdl }
    559  1.1  fvdl 
    560  1.1  fvdl static void
    561  1.1  fvdl clnt_dg_geterr(cl, errp)
    562  1.1  fvdl 	CLIENT *cl;
    563  1.1  fvdl 	struct rpc_err *errp;
    564  1.1  fvdl {
    565  1.1  fvdl 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
    566  1.1  fvdl 
    567  1.1  fvdl 	*errp = cu->cu_error;
    568  1.1  fvdl }
    569  1.1  fvdl 
    570  1.1  fvdl static bool_t
    571  1.1  fvdl clnt_dg_freeres(cl, xdr_res, res_ptr)
    572  1.1  fvdl 	CLIENT *cl;
    573  1.1  fvdl 	xdrproc_t xdr_res;
    574  1.1  fvdl 	caddr_t res_ptr;
    575  1.1  fvdl {
    576  1.1  fvdl 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
    577  1.1  fvdl 	register XDR *xdrs = &(cu->cu_outxdrs);
    578  1.1  fvdl 	bool_t dummy;
    579  1.1  fvdl #ifdef __REENT
    580  1.1  fvdl 	sigset_t mask;
    581  1.1  fvdl #endif
    582  1.1  fvdl 	sigset_t newmask;
    583  1.1  fvdl 
    584  1.1  fvdl 	sigfillset(&newmask);
    585  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    586  1.1  fvdl 	mutex_lock(&clnt_fd_lock);
    587  1.1  fvdl 	while (dg_fd_locks[cu->cu_fd])
    588  1.1  fvdl 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
    589  1.1  fvdl 	xdrs->x_op = XDR_FREE;
    590  1.1  fvdl 	dummy = (*xdr_res)(xdrs, res_ptr);
    591  1.1  fvdl 	mutex_unlock(&clnt_fd_lock);
    592  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
    593  1.1  fvdl 	cond_signal(&dg_cv[cu->cu_fd]);
    594  1.1  fvdl 	return (dummy);
    595  1.1  fvdl }
    596  1.1  fvdl 
    597  1.1  fvdl /*ARGSUSED*/
    598  1.1  fvdl static void
    599  1.1  fvdl clnt_dg_abort(h)
    600  1.1  fvdl 	CLIENT *h;
    601  1.1  fvdl {
    602  1.1  fvdl }
    603  1.1  fvdl 
    604  1.1  fvdl static bool_t
    605  1.1  fvdl clnt_dg_control(cl, request, info)
    606  1.1  fvdl 	CLIENT *cl;
    607  1.1  fvdl 	u_int request;
    608  1.1  fvdl 	char *info;
    609  1.1  fvdl {
    610  1.1  fvdl 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
    611  1.1  fvdl 	struct netbuf *addr;
    612  1.1  fvdl #ifdef __REENT
    613  1.1  fvdl 	sigset_t mask;
    614  1.1  fvdl #endif
    615  1.1  fvdl 	sigset_t newmask;
    616  1.1  fvdl 
    617  1.1  fvdl 	sigfillset(&newmask);
    618  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    619  1.1  fvdl 	mutex_lock(&clnt_fd_lock);
    620  1.1  fvdl 	while (dg_fd_locks[cu->cu_fd])
    621  1.1  fvdl 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
    622  1.1  fvdl 	dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
    623  1.1  fvdl 	mutex_unlock(&clnt_fd_lock);
    624  1.1  fvdl 	switch (request) {
    625  1.1  fvdl 	case CLSET_FD_CLOSE:
    626  1.1  fvdl 		cu->cu_closeit = TRUE;
    627  1.1  fvdl 		release_fd_lock(cu->cu_fd, mask);
    628  1.1  fvdl 		return (TRUE);
    629  1.1  fvdl 	case CLSET_FD_NCLOSE:
    630  1.1  fvdl 		cu->cu_closeit = FALSE;
    631  1.1  fvdl 		release_fd_lock(cu->cu_fd, mask);
    632  1.1  fvdl 		return (TRUE);
    633  1.1  fvdl 	}
    634  1.1  fvdl 
    635  1.1  fvdl 	/* for other requests which use info */
    636  1.1  fvdl 	if (info == NULL) {
    637  1.1  fvdl 		release_fd_lock(cu->cu_fd, mask);
    638  1.1  fvdl 		return (FALSE);
    639  1.1  fvdl 	}
    640  1.1  fvdl 	switch (request) {
    641  1.1  fvdl 	case CLSET_TIMEOUT:
    642  1.1  fvdl 		if (time_not_ok((struct timeval *)info)) {
    643  1.1  fvdl 			release_fd_lock(cu->cu_fd, mask);
    644  1.1  fvdl 			return (FALSE);
    645  1.1  fvdl 		}
    646  1.1  fvdl 		cu->cu_total = *(struct timeval *)info;
    647  1.1  fvdl 		break;
    648  1.1  fvdl 	case CLGET_TIMEOUT:
    649  1.1  fvdl 		*(struct timeval *)info = cu->cu_total;
    650  1.1  fvdl 		break;
    651  1.1  fvdl 	case CLGET_SERVER_ADDR:		/* Give him the fd address */
    652  1.1  fvdl 		/* Now obsolete. Only for backward compatibility */
    653  1.1  fvdl 		(void) memcpy(info, &cu->cu_raddr, cu->cu_rlen);
    654  1.1  fvdl 		break;
    655  1.1  fvdl 	case CLSET_RETRY_TIMEOUT:
    656  1.1  fvdl 		if (time_not_ok((struct timeval *)info)) {
    657  1.1  fvdl 			release_fd_lock(cu->cu_fd, mask);
    658  1.1  fvdl 			return (FALSE);
    659  1.1  fvdl 		}
    660  1.1  fvdl 		cu->cu_wait = *(struct timeval *)info;
    661  1.1  fvdl 		break;
    662  1.1  fvdl 	case CLGET_RETRY_TIMEOUT:
    663  1.1  fvdl 		*(struct timeval *)info = cu->cu_wait;
    664  1.1  fvdl 		break;
    665  1.1  fvdl 	case CLGET_FD:
    666  1.1  fvdl 		*(int *)info = cu->cu_fd;
    667  1.1  fvdl 		break;
    668  1.1  fvdl 	case CLGET_SVC_ADDR:
    669  1.1  fvdl 		addr = (struct netbuf *)info;
    670  1.1  fvdl 		addr->buf = &cu->cu_raddr;
    671  1.1  fvdl 		addr->len = cu->cu_rlen;
    672  1.1  fvdl 		addr->maxlen = sizeof cu->cu_raddr;
    673  1.1  fvdl 		break;
    674  1.1  fvdl 	case CLSET_SVC_ADDR:		/* set to new address */
    675  1.1  fvdl 		addr = (struct netbuf *)info;
    676  1.1  fvdl 		if (addr->len < sizeof cu->cu_raddr)
    677  1.1  fvdl 			return (FALSE);
    678  1.1  fvdl 		(void) memcpy(&cu->cu_raddr, addr->buf, addr->len);
    679  1.1  fvdl 		cu->cu_rlen = addr->len;
    680  1.1  fvdl 		break;
    681  1.1  fvdl 	case CLGET_XID:
    682  1.1  fvdl 		/*
    683  1.1  fvdl 		 * use the knowledge that xid is the
    684  1.1  fvdl 		 * first element in the call structure *.
    685  1.1  fvdl 		 * This will get the xid of the PREVIOUS call
    686  1.1  fvdl 		 */
    687  1.1  fvdl 		*(u_int32_t *)info = ntohl(*(u_int32_t *)cu->cu_outbuf);
    688  1.1  fvdl 		break;
    689  1.1  fvdl 
    690  1.1  fvdl 	case CLSET_XID:
    691  1.1  fvdl 		/* This will set the xid of the NEXT call */
    692  1.1  fvdl 		*(u_int32_t *)cu->cu_outbuf =  htonl(*(u_int32_t *)info - 1);
    693  1.1  fvdl 		/* decrement by 1 as clnt_dg_call() increments once */
    694  1.1  fvdl 		break;
    695  1.1  fvdl 
    696  1.1  fvdl 	case CLGET_VERS:
    697  1.1  fvdl 		/*
    698  1.1  fvdl 		 * This RELIES on the information that, in the call body,
    699  1.1  fvdl 		 * the version number field is the fifth field from the
    700  1.1  fvdl 		 * begining of the RPC header. MUST be changed if the
    701  1.1  fvdl 		 * call_struct is changed
    702  1.1  fvdl 		 */
    703  1.1  fvdl 		*(u_int32_t *)info = ntohl(*(u_int32_t *)(cu->cu_outbuf +
    704  1.1  fvdl 						    4 * BYTES_PER_XDR_UNIT));
    705  1.1  fvdl 		break;
    706  1.1  fvdl 
    707  1.1  fvdl 	case CLSET_VERS:
    708  1.1  fvdl 		*(u_int32_t *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
    709  1.1  fvdl 			= htonl(*(u_int32_t *)info);
    710  1.1  fvdl 		break;
    711  1.1  fvdl 
    712  1.1  fvdl 	case CLGET_PROG:
    713  1.1  fvdl 		/*
    714  1.1  fvdl 		 * This RELIES on the information that, in the call body,
    715  1.1  fvdl 		 * the program number field is the fourth field from the
    716  1.1  fvdl 		 * begining of the RPC header. MUST be changed if the
    717  1.1  fvdl 		 * call_struct is changed
    718  1.1  fvdl 		 */
    719  1.1  fvdl 		*(u_int32_t *)info = ntohl(*(u_int32_t *)(cu->cu_outbuf +
    720  1.1  fvdl 						    3 * BYTES_PER_XDR_UNIT));
    721  1.1  fvdl 		break;
    722  1.1  fvdl 
    723  1.1  fvdl 	case CLSET_PROG:
    724  1.1  fvdl 		*(u_int32_t *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
    725  1.1  fvdl 			= htonl(*(u_int32_t *)info);
    726  1.1  fvdl 		break;
    727  1.1  fvdl 
    728  1.1  fvdl 	default:
    729  1.1  fvdl 		release_fd_lock(cu->cu_fd, mask);
    730  1.1  fvdl 		return (FALSE);
    731  1.1  fvdl 	}
    732  1.1  fvdl 	release_fd_lock(cu->cu_fd, mask);
    733  1.1  fvdl 	return (TRUE);
    734  1.1  fvdl }
    735  1.1  fvdl 
    736  1.1  fvdl static void
    737  1.1  fvdl clnt_dg_destroy(cl)
    738  1.1  fvdl 	CLIENT *cl;
    739  1.1  fvdl {
    740  1.1  fvdl 	register struct cu_data *cu = (struct cu_data *)cl->cl_private;
    741  1.1  fvdl 	int cu_fd = cu->cu_fd;
    742  1.1  fvdl #ifdef __REENT
    743  1.1  fvdl 	sigset_t mask;
    744  1.1  fvdl #endif
    745  1.1  fvdl 	sigset_t newmask;
    746  1.1  fvdl 
    747  1.1  fvdl 	sigfillset(&newmask);
    748  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    749  1.1  fvdl 	mutex_lock(&clnt_fd_lock);
    750  1.1  fvdl 	while (dg_fd_locks[cu_fd])
    751  1.1  fvdl 		cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
    752  1.1  fvdl 	if (cu->cu_closeit)
    753  1.1  fvdl 		(void) close(cu_fd);
    754  1.1  fvdl 	XDR_DESTROY(&(cu->cu_outxdrs));
    755  1.1  fvdl 	mem_free((caddr_t)cu,
    756  1.1  fvdl 		(sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
    757  1.1  fvdl 	if (cl->cl_netid && cl->cl_netid[0])
    758  1.1  fvdl 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
    759  1.1  fvdl 	if (cl->cl_tp && cl->cl_tp[0])
    760  1.1  fvdl 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
    761  1.1  fvdl 	mem_free((caddr_t)cl, sizeof (CLIENT));
    762  1.1  fvdl 	mutex_unlock(&clnt_fd_lock);
    763  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
    764  1.1  fvdl 	cond_signal(&dg_cv[cu_fd]);
    765  1.1  fvdl }
    766  1.1  fvdl 
    767  1.1  fvdl static struct clnt_ops *
    768  1.1  fvdl clnt_dg_ops()
    769  1.1  fvdl {
    770  1.1  fvdl 	static struct clnt_ops ops;
    771  1.1  fvdl #ifdef __REENT
    772  1.1  fvdl 	extern mutex_t	ops_lock;
    773  1.1  fvdl 	sigset_t mask;
    774  1.1  fvdl #endif
    775  1.1  fvdl 	sigset_t newmask;
    776  1.1  fvdl 
    777  1.1  fvdl /* VARIABLES PROTECTED BY ops_lock: ops */
    778  1.1  fvdl 
    779  1.1  fvdl 	sigfillset(&newmask);
    780  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
    781  1.1  fvdl 	mutex_lock(&ops_lock);
    782  1.1  fvdl 	if (ops.cl_call == NULL) {
    783  1.1  fvdl 		ops.cl_call = clnt_dg_call;
    784  1.1  fvdl 		ops.cl_abort = clnt_dg_abort;
    785  1.1  fvdl 		ops.cl_geterr = clnt_dg_geterr;
    786  1.1  fvdl 		ops.cl_freeres = clnt_dg_freeres;
    787  1.1  fvdl 		ops.cl_destroy = clnt_dg_destroy;
    788  1.1  fvdl 		ops.cl_control = clnt_dg_control;
    789  1.1  fvdl 	}
    790  1.1  fvdl 	mutex_unlock(&ops_lock);
    791  1.1  fvdl 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
    792  1.1  fvdl 	return (&ops);
    793  1.1  fvdl }
    794  1.1  fvdl 
    795  1.1  fvdl /*
    796  1.1  fvdl  * Make sure that the time is not garbage.  -1 value is allowed.
    797  1.1  fvdl  */
    798  1.1  fvdl static bool_t
    799  1.1  fvdl time_not_ok(t)
    800  1.1  fvdl 	struct timeval *t;
    801  1.1  fvdl {
    802  1.1  fvdl 	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
    803  1.1  fvdl 		t->tv_usec < -1 || t->tv_usec > 1000000);
    804  1.1  fvdl }
    805  1.1  fvdl 
    806  1.1  fvdl 
    807  1.1  fvdl /*
    808  1.1  fvdl  *	Convert from timevals (used by select) to milliseconds (used by poll).
    809  1.1  fvdl  */
    810  1.1  fvdl static int
    811  1.1  fvdl __rpc_timeval_to_msec(t)
    812  1.1  fvdl 	struct timeval	*t;
    813  1.1  fvdl {
    814  1.1  fvdl 	int	t1, tmp;
    815  1.1  fvdl 
    816  1.1  fvdl 	/*
    817  1.1  fvdl 	 *	We're really returning t->tv_sec * 1000 + (t->tv_usec / 1000)
    818  1.1  fvdl 	 *	but try to do so efficiently.  Note:  1000 = 1024 - 16 - 8.
    819  1.1  fvdl 	 */
    820  1.1  fvdl 	tmp = t->tv_sec << 3;
    821  1.1  fvdl 	t1 = -tmp;
    822  1.1  fvdl 	t1 += t1 << 1;
    823  1.1  fvdl 	t1 += tmp << 7;
    824  1.1  fvdl 	if (t->tv_usec)
    825  1.1  fvdl 		t1 += t->tv_usec / 1000;
    826  1.1  fvdl 
    827  1.1  fvdl 	return (t1);
    828  1.1  fvdl }
    829