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