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