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