xref: /netbsd-src/lib/libc/rpc/clnt_dg.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: clnt_dg.c,v 1.20 2005/12/03 15:16:19 yamt 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.20 2005/12/03 15:16:19 yamt 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,
77     const char *, 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 	const char *	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, __UNCONST(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 			cu->cu_error.re_errno = errno;
447 			cu->cu_error.re_status = RPC_CANTRECV;
448 			goto out;
449 		}
450 
451 		gettimeofday(&tv, NULL);
452 		timersub(&tv, &starttime, &time_waited);
453 
454 		/* Check for timeout. */
455 		if (timercmp(&time_waited, &timeout, >)) {
456 			cu->cu_error.re_status = RPC_TIMEDOUT;
457 			goto out;
458 		}
459 
460 		/* Retransmit if necessary. */
461 		if (timercmp(&time_waited, &next_sendtime, >)) {
462 			/* update retransmit_time */
463 			if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
464 				timeradd(&retransmit_time, &retransmit_time,
465 				    &retransmit_time);
466 			timeradd(&next_sendtime, &retransmit_time,
467 			    &next_sendtime);
468 			goto send_again;
469 		}
470 	}
471 
472 	/*
473 	 * now decode and validate the response
474 	 */
475 
476 	xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
477 	ok = xdr_replymsg(&reply_xdrs, &reply_msg);
478 	/* XDR_DESTROY(&reply_xdrs);	save a few cycles on noop destroy */
479 	if (ok) {
480 		if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
481 			(reply_msg.acpted_rply.ar_stat == SUCCESS))
482 			cu->cu_error.re_status = RPC_SUCCESS;
483 		else
484 			_seterr_reply(&reply_msg, &(cu->cu_error));
485 
486 		if (cu->cu_error.re_status == RPC_SUCCESS) {
487 			if (! AUTH_VALIDATE(cl->cl_auth,
488 					    &reply_msg.acpted_rply.ar_verf)) {
489 				cu->cu_error.re_status = RPC_AUTHERROR;
490 				cu->cu_error.re_why = AUTH_INVALIDRESP;
491 			}
492 			if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
493 				xdrs->x_op = XDR_FREE;
494 				(void) xdr_opaque_auth(xdrs,
495 					&(reply_msg.acpted_rply.ar_verf));
496 			}
497 		}		/* end successful completion */
498 		/*
499 		 * If unsuccesful AND error is an authentication error
500 		 * then refresh credentials and try again, else break
501 		 */
502 		else if (cu->cu_error.re_status == RPC_AUTHERROR)
503 			/* maybe our credentials need to be refreshed ... */
504 			if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
505 				nrefreshes--;
506 				goto call_again;
507 			}
508 		/* end of unsuccessful completion */
509 	}	/* end of valid reply message */
510 	else {
511 		cu->cu_error.re_status = RPC_CANTDECODERES;
512 
513 	}
514 out:
515 	if (cu->cu_kq >= 0)
516 		(void)close(cu->cu_kq);
517 	cu->cu_kq = -1;
518 	release_fd_lock(cu->cu_fd, mask);
519 	return (cu->cu_error.re_status);
520 }
521 
522 static void
523 clnt_dg_geterr(cl, errp)
524 	CLIENT *cl;
525 	struct rpc_err *errp;
526 {
527 	struct cu_data *cu;
528 
529 	_DIAGASSERT(cl != NULL);
530 	_DIAGASSERT(errp != NULL);
531 
532 	cu = (struct cu_data *)cl->cl_private;
533 	*errp = cu->cu_error;
534 }
535 
536 static bool_t
537 clnt_dg_freeres(cl, xdr_res, res_ptr)
538 	CLIENT *cl;
539 	xdrproc_t xdr_res;
540 	caddr_t res_ptr;
541 {
542 	struct cu_data *cu;
543 	XDR *xdrs;
544 	bool_t dummy;
545 #ifdef _REENTRANT
546 	sigset_t mask;
547 #endif
548 	sigset_t newmask;
549 
550 	_DIAGASSERT(cl != NULL);
551 	cu = (struct cu_data *)cl->cl_private;
552 	xdrs = &(cu->cu_outxdrs);
553 
554 	sigfillset(&newmask);
555 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
556 	mutex_lock(&clnt_fd_lock);
557 	while (dg_fd_locks[cu->cu_fd])
558 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
559 	xdrs->x_op = XDR_FREE;
560 	dummy = (*xdr_res)(xdrs, res_ptr);
561 	mutex_unlock(&clnt_fd_lock);
562 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
563 	cond_signal(&dg_cv[cu->cu_fd]);
564 	return (dummy);
565 }
566 
567 /*ARGSUSED*/
568 static void
569 clnt_dg_abort(h)
570 	CLIENT *h;
571 {
572 }
573 
574 static bool_t
575 clnt_dg_control(cl, request, info)
576 	CLIENT *cl;
577 	u_int request;
578 	char *info;
579 {
580 	struct cu_data *cu;
581 	struct netbuf *addr;
582 #ifdef _REENTRANT
583 	sigset_t mask;
584 #endif
585 	sigset_t newmask;
586 
587 	_DIAGASSERT(cl != NULL);
588 	/* info is handled below */
589 
590 	cu = (struct cu_data *)cl->cl_private;
591 
592 	sigfillset(&newmask);
593 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
594 	mutex_lock(&clnt_fd_lock);
595 	while (dg_fd_locks[cu->cu_fd])
596 		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
597 	dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
598 	mutex_unlock(&clnt_fd_lock);
599 	switch (request) {
600 	case CLSET_FD_CLOSE:
601 		cu->cu_closeit = TRUE;
602 		release_fd_lock(cu->cu_fd, mask);
603 		return (TRUE);
604 	case CLSET_FD_NCLOSE:
605 		cu->cu_closeit = FALSE;
606 		release_fd_lock(cu->cu_fd, mask);
607 		return (TRUE);
608 	}
609 
610 	/* for other requests which use info */
611 	if (info == NULL) {
612 		release_fd_lock(cu->cu_fd, mask);
613 		return (FALSE);
614 	}
615 	switch (request) {
616 	case CLSET_TIMEOUT:
617 		if (time_not_ok((struct timeval *)(void *)info)) {
618 			release_fd_lock(cu->cu_fd, mask);
619 			return (FALSE);
620 		}
621 		cu->cu_total = *(struct timeval *)(void *)info;
622 		break;
623 	case CLGET_TIMEOUT:
624 		*(struct timeval *)(void *)info = cu->cu_total;
625 		break;
626 	case CLGET_SERVER_ADDR:		/* Give him the fd address */
627 		/* Now obsolete. Only for backward compatibility */
628 		(void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
629 		break;
630 	case CLSET_RETRY_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_wait = *(struct timeval *)(void *)info;
636 		break;
637 	case CLGET_RETRY_TIMEOUT:
638 		*(struct timeval *)(void *)info = cu->cu_wait;
639 		break;
640 	case CLGET_FD:
641 		*(int *)(void *)info = cu->cu_fd;
642 		break;
643 	case CLGET_SVC_ADDR:
644 		addr = (struct netbuf *)(void *)info;
645 		addr->buf = &cu->cu_raddr;
646 		addr->len = cu->cu_rlen;
647 		addr->maxlen = sizeof cu->cu_raddr;
648 		break;
649 	case CLSET_SVC_ADDR:		/* set to new address */
650 		addr = (struct netbuf *)(void *)info;
651 		if (addr->len < sizeof cu->cu_raddr) {
652 			release_fd_lock(cu->cu_fd, mask);
653 			return (FALSE);
654 		}
655 		(void) memcpy(&cu->cu_raddr, addr->buf, (size_t)addr->len);
656 		cu->cu_rlen = addr->len;
657 		break;
658 	case CLGET_XID:
659 		/*
660 		 * use the knowledge that xid is the
661 		 * first element in the call structure *.
662 		 * This will get the xid of the PREVIOUS call
663 		 */
664 		*(u_int32_t *)(void *)info =
665 		    ntohl(*(u_int32_t *)(void *)cu->cu_outbuf);
666 		break;
667 
668 	case CLSET_XID:
669 		/* This will set the xid of the NEXT call */
670 		*(u_int32_t *)(void *)cu->cu_outbuf =
671 		    htonl(*(u_int32_t *)(void *)info - 1);
672 		/* decrement by 1 as clnt_dg_call() increments once */
673 		break;
674 
675 	case CLGET_VERS:
676 		/*
677 		 * This RELIES on the information that, in the call body,
678 		 * the version number field is the fifth field from the
679 		 * begining of the RPC header. MUST be changed if the
680 		 * call_struct is changed
681 		 */
682 		*(u_int32_t *)(void *)info =
683 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
684 		    4 * BYTES_PER_XDR_UNIT));
685 		break;
686 
687 	case CLSET_VERS:
688 		*(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
689 			= htonl(*(u_int32_t *)(void *)info);
690 		break;
691 
692 	case CLGET_PROG:
693 		/*
694 		 * This RELIES on the information that, in the call body,
695 		 * the program number field is the fourth field from the
696 		 * begining of the RPC header. MUST be changed if the
697 		 * call_struct is changed
698 		 */
699 		*(u_int32_t *)(void *)info =
700 		    ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
701 		    3 * BYTES_PER_XDR_UNIT));
702 		break;
703 
704 	case CLSET_PROG:
705 		*(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
706 			= htonl(*(u_int32_t *)(void *)info);
707 		break;
708 
709 	default:
710 		release_fd_lock(cu->cu_fd, mask);
711 		return (FALSE);
712 	}
713 	release_fd_lock(cu->cu_fd, mask);
714 	return (TRUE);
715 }
716 
717 static void
718 clnt_dg_destroy(cl)
719 	CLIENT *cl;
720 {
721 	struct cu_data *cu;
722 	int cu_fd;
723 #ifdef _REENTRANT
724 	sigset_t mask;
725 #endif
726 	sigset_t newmask;
727 
728 	_DIAGASSERT(cl != NULL);
729 
730 	cu = (struct cu_data *)cl->cl_private;
731 	cu_fd = cu->cu_fd;
732 
733 	sigfillset(&newmask);
734 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
735 	mutex_lock(&clnt_fd_lock);
736 	while (dg_fd_locks[cu_fd])
737 		cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
738 	if (cu->cu_closeit)
739 		(void) close(cu_fd);
740 	if (cu->cu_kq >= 0)
741 		(void)close(cu->cu_kq);
742 	XDR_DESTROY(&(cu->cu_outxdrs));
743 	mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
744 	if (cl->cl_netid && cl->cl_netid[0])
745 		mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
746 	if (cl->cl_tp && cl->cl_tp[0])
747 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
748 	mem_free(cl, sizeof (CLIENT));
749 	mutex_unlock(&clnt_fd_lock);
750 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
751 	cond_signal(&dg_cv[cu_fd]);
752 }
753 
754 static struct clnt_ops *
755 clnt_dg_ops()
756 {
757 	static struct clnt_ops ops;
758 #ifdef _REENTRANT
759 	extern mutex_t	ops_lock;
760 	sigset_t mask;
761 #endif
762 	sigset_t newmask;
763 
764 /* VARIABLES PROTECTED BY ops_lock: ops */
765 
766 	sigfillset(&newmask);
767 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
768 	mutex_lock(&ops_lock);
769 	if (ops.cl_call == NULL) {
770 		ops.cl_call = clnt_dg_call;
771 		ops.cl_abort = clnt_dg_abort;
772 		ops.cl_geterr = clnt_dg_geterr;
773 		ops.cl_freeres = clnt_dg_freeres;
774 		ops.cl_destroy = clnt_dg_destroy;
775 		ops.cl_control = clnt_dg_control;
776 	}
777 	mutex_unlock(&ops_lock);
778 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
779 	return (&ops);
780 }
781 
782 /*
783  * Make sure that the time is not garbage.  -1 value is allowed.
784  */
785 static bool_t
786 time_not_ok(t)
787 	struct timeval *t;
788 {
789 
790 	_DIAGASSERT(t != NULL);
791 
792 	return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
793 		t->tv_usec < -1 || t->tv_usec > 1000000);
794 }
795