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