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