clnt_dg.c revision 1.16 1 /* $NetBSD: clnt_dg.c,v 1.16 2005/09/09 15:40:49 christos 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.16 2005/09/09 15:40:49 christos 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
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 pollfd pfdp;
139 char cu_inbuf[1];
140 };
141
142 /*
143 * Connection less client creation returns with client handle parameters.
144 * Default options are set, which the user can change using clnt_control().
145 * fd should be open and bound.
146 * NB: The rpch->cl_auth is initialized to null authentication.
147 * Caller may wish to set this something more useful.
148 *
149 * sendsz and recvsz are the maximum allowable packet sizes that can be
150 * sent and received. Normally they are the same, but they can be
151 * changed to improve the program efficiency and buffer allocation.
152 * If they are 0, use the transport default.
153 *
154 * If svcaddr is NULL, returns NULL.
155 */
156 CLIENT *
157 clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
158 int fd; /* open file descriptor */
159 const struct netbuf *svcaddr; /* servers address */
160 rpcprog_t program; /* program number */
161 rpcvers_t version; /* version number */
162 u_int sendsz; /* buffer recv size */
163 u_int recvsz; /* buffer send size */
164 {
165 CLIENT *cl = NULL; /* client handle */
166 struct cu_data *cu = NULL; /* private data */
167 struct rpc_msg call_msg;
168 #ifdef _REENTRANT
169 sigset_t mask;
170 #endif
171 sigset_t newmask;
172 struct __rpc_sockinfo si;
173 int one = 1;
174
175 sigfillset(&newmask);
176 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
177 mutex_lock(&clnt_fd_lock);
178 if (dg_fd_locks == (int *) NULL) {
179 #ifdef _REENTRANT
180 size_t cv_allocsz;
181 #endif
182 size_t fd_allocsz;
183 int dtbsize = __rpc_dtbsize();
184
185 fd_allocsz = dtbsize * sizeof (int);
186 dg_fd_locks = (int *) mem_alloc(fd_allocsz);
187 if (dg_fd_locks == (int *) NULL) {
188 mutex_unlock(&clnt_fd_lock);
189 thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
190 goto err1;
191 } else
192 memset(dg_fd_locks, '\0', fd_allocsz);
193
194 #ifdef _REENTRANT
195 cv_allocsz = dtbsize * sizeof (cond_t);
196 dg_cv = (cond_t *) mem_alloc(cv_allocsz);
197 if (dg_cv == (cond_t *) NULL) {
198 mem_free(dg_fd_locks, fd_allocsz);
199 dg_fd_locks = (int *) NULL;
200 mutex_unlock(&clnt_fd_lock);
201 thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
202 goto err1;
203 } else {
204 int i;
205
206 for (i = 0; i < dtbsize; i++)
207 cond_init(&dg_cv[i], 0, (void *) 0);
208 }
209 #endif
210 }
211
212 mutex_unlock(&clnt_fd_lock);
213 thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
214
215 if (svcaddr == NULL) {
216 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
217 return (NULL);
218 }
219
220 if (!__rpc_fd2sockinfo(fd, &si)) {
221 rpc_createerr.cf_stat = RPC_TLIERROR;
222 rpc_createerr.cf_error.re_errno = 0;
223 return (NULL);
224 }
225 /*
226 * Find the receive and the send size
227 */
228 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
229 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
230 if ((sendsz == 0) || (recvsz == 0)) {
231 rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
232 rpc_createerr.cf_error.re_errno = 0;
233 return (NULL);
234 }
235
236 if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
237 goto err1;
238 /*
239 * Should be multiple of 4 for XDR.
240 */
241 sendsz = ((sendsz + 3) / 4) * 4;
242 recvsz = ((recvsz + 3) / 4) * 4;
243 cu = malloc(sizeof (*cu) + sendsz + recvsz);
244 if (cu == NULL)
245 goto err1;
246 memset(cu, 0, sizeof(*cu));
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, maskp = &mask;
326 #else
327 sigset_t maskp = NULL;
328 #endif
329 sigset_t newmask;
330 ssize_t recvlen = 0;
331 struct timespec ts;
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 = cu->cu_wait;
353
354 call_again:
355 xdrs = &(cu->cu_outxdrs);
356 xdrs->x_op = XDR_ENCODE;
357 XDR_SETPOS(xdrs, cu->cu_xdrpos);
358 /*
359 * the transaction is the first thing in the out buffer
360 */
361 (*(u_int32_t *)(void *)(cu->cu_outbuf))++;
362 if ((! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
363 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
364 (! (*xargs)(xdrs, argsp))) {
365 release_fd_lock(cu->cu_fd, mask);
366 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
367 }
368 outlen = (size_t)XDR_GETPOS(xdrs);
369
370 send_again:
371 if (sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0,
372 (struct sockaddr *)(void *)&cu->cu_raddr, (socklen_t)cu->cu_rlen)
373 != outlen) {
374 cu->cu_error.re_errno = errno;
375 release_fd_lock(cu->cu_fd, mask);
376 return (cu->cu_error.re_status = RPC_CANTSEND);
377 }
378
379 /*
380 * Hack to provide rpc-based message passing
381 */
382 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
383 release_fd_lock(cu->cu_fd, mask);
384 return (cu->cu_error.re_status = RPC_TIMEDOUT);
385 }
386 /*
387 * sub-optimal code appears here because we have
388 * some clock time to spare while the packets are in flight.
389 * (We assume that this is actually only executed once.)
390 */
391 reply_msg.acpted_rply.ar_verf = _null_auth;
392 reply_msg.acpted_rply.ar_results.where = resultsp;
393 reply_msg.acpted_rply.ar_results.proc = xresults;
394
395
396 for (;;) {
397 TIMEVAL_TO_TIMESPEC(&retransmit_time, &ts);
398 switch (pollts(&cu->pfdp, 1, &ts, maskp)) {
399 case 0:
400 time_waited.tv_sec += retransmit_time.tv_sec;
401 time_waited.tv_usec += retransmit_time.tv_usec;
402 while (time_waited.tv_usec >= 1000000) {
403 time_waited.tv_sec++;
404 time_waited.tv_usec -= 1000000;
405 }
406 /* update retransmit_time */
407 if (retransmit_time.tv_sec < RPC_MAX_BACKOFF) {
408 retransmit_time.tv_usec *= 2;
409 retransmit_time.tv_sec *= 2;
410 while (retransmit_time.tv_usec >= 1000000) {
411 retransmit_time.tv_sec++;
412 retransmit_time.tv_usec -= 1000000;
413 }
414 }
415
416 if ((time_waited.tv_sec < timeout.tv_sec) ||
417 ((time_waited.tv_sec == timeout.tv_sec) &&
418 (time_waited.tv_usec < timeout.tv_usec)))
419 goto send_again;
420 release_fd_lock(cu->cu_fd, mask);
421 return (cu->cu_error.re_status = RPC_TIMEDOUT);
422
423 case -1:
424 if (errno == EBADF) {
425 cu->cu_error.re_errno = errno;
426 release_fd_lock(cu->cu_fd, mask);
427 return (cu->cu_error.re_status = RPC_CANTRECV);
428 }
429 if (errno != EINTR) {
430 errno = 0; /* reset it */
431 continue;
432 }
433 /* interrupted by another signal, update time_waited */
434 if (firsttimeout) {
435 /*
436 * Could have done gettimeofday before clnt_call
437 * but that means 1 more system call per each
438 * clnt_call, so do it after first time out
439 */
440 if (gettimeofday(&startime,
441 (struct timezone *) NULL) == -1) {
442 errno = 0;
443 continue;
444 }
445 firsttimeout = 0;
446 errno = 0;
447 continue;
448 };
449 if (gettimeofday(&curtime,
450 (struct timezone *) NULL) == -1) {
451 errno = 0;
452 continue;
453 };
454 time_waited.tv_sec += curtime.tv_sec - startime.tv_sec;
455 time_waited.tv_usec += curtime.tv_usec -
456 startime.tv_usec;
457 while (time_waited.tv_usec < 0) {
458 time_waited.tv_sec--;
459 time_waited.tv_usec += 1000000;
460 };
461 while (time_waited.tv_usec >= 1000000) {
462 time_waited.tv_sec++;
463 time_waited.tv_usec -= 1000000;
464 }
465 startime.tv_sec = curtime.tv_sec;
466 startime.tv_usec = curtime.tv_usec;
467 if ((time_waited.tv_sec > timeout.tv_sec) ||
468 ((time_waited.tv_sec == timeout.tv_sec) &&
469 (time_waited.tv_usec > timeout.tv_usec))) {
470 release_fd_lock(cu->cu_fd, mask);
471 return (cu->cu_error.re_status = RPC_TIMEDOUT);
472 }
473 #ifdef _REENTRANT
474 if (errno == EINTR) {
475 sigset_t rmask;
476 if (sigpending(&rmask) == -1) {
477 cu->cu_error.re_errno = errno;
478 release_fd_lock(cu->cu_fd, mask);
479 return cu->cu_error.re_status =
480 RPC_SYSTEMERROR;
481 }
482 (void)sigsuspend(&rmask);
483 }
484 #endif
485 errno = 0; /* reset it */
486 continue;
487 };
488
489 if (cu->pfdp.revents & POLLNVAL || (cu->pfdp.revents == 0)) {
490 cu->cu_error.re_status = RPC_CANTRECV;
491 /*
492 * Note: we're faking errno here because we
493 * previously would have expected pollts() to
494 * return -1 with errno EBADF. Poll(BA_OS)
495 * returns 0 and sets the POLLNVAL revents flag
496 * instead.
497 */
498 cu->cu_error.re_errno = errno = EBADF;
499 release_fd_lock(cu->cu_fd, mask);
500 return (-1);
501 }
502
503 /* We have some data now */
504 do {
505 if (errno == EINTR) {
506 /*
507 * Must make sure errno was not already
508 * EINTR in case recvfrom() returns -1.
509 */
510 errno = 0;
511 }
512 recvlen = recvfrom(cu->cu_fd, cu->cu_inbuf,
513 (socklen_t)cu->cu_recvsz, 0, NULL, NULL);
514 } while (recvlen < 0 && errno == EINTR);
515 if (recvlen < 0) {
516 if (errno == EWOULDBLOCK)
517 continue;
518 cu->cu_error.re_errno = errno;
519 release_fd_lock(cu->cu_fd, mask);
520 return (cu->cu_error.re_status = RPC_CANTRECV);
521 }
522 if (recvlen < sizeof (u_int32_t))
523 continue;
524 /* see if reply transaction id matches sent id */
525 if (*((u_int32_t *)(void *)(cu->cu_inbuf)) !=
526 *((u_int32_t *)(void *)(cu->cu_outbuf)))
527 continue;
528 /* we now assume we have the proper reply */
529 break;
530 }
531
532 /*
533 * now decode and validate the response
534 */
535
536 xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
537 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
538 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
539 if (ok) {
540 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
541 (reply_msg.acpted_rply.ar_stat == SUCCESS))
542 cu->cu_error.re_status = RPC_SUCCESS;
543 else
544 _seterr_reply(&reply_msg, &(cu->cu_error));
545
546 if (cu->cu_error.re_status == RPC_SUCCESS) {
547 if (! AUTH_VALIDATE(cl->cl_auth,
548 &reply_msg.acpted_rply.ar_verf)) {
549 cu->cu_error.re_status = RPC_AUTHERROR;
550 cu->cu_error.re_why = AUTH_INVALIDRESP;
551 }
552 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
553 xdrs->x_op = XDR_FREE;
554 (void) xdr_opaque_auth(xdrs,
555 &(reply_msg.acpted_rply.ar_verf));
556 }
557 } /* end successful completion */
558 /*
559 * If unsuccesful AND error is an authentication error
560 * then refresh credentials and try again, else break
561 */
562 else if (cu->cu_error.re_status == RPC_AUTHERROR)
563 /* maybe our credentials need to be refreshed ... */
564 if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
565 nrefreshes--;
566 goto call_again;
567 }
568 /* end of unsuccessful completion */
569 } /* end of valid reply message */
570 else {
571 cu->cu_error.re_status = RPC_CANTDECODERES;
572
573 }
574 release_fd_lock(cu->cu_fd, mask);
575 return (cu->cu_error.re_status);
576 }
577
578 static void
579 clnt_dg_geterr(cl, errp)
580 CLIENT *cl;
581 struct rpc_err *errp;
582 {
583 struct cu_data *cu;
584
585 _DIAGASSERT(cl != NULL);
586 _DIAGASSERT(errp != NULL);
587
588 cu = (struct cu_data *)cl->cl_private;
589 *errp = cu->cu_error;
590 }
591
592 static bool_t
593 clnt_dg_freeres(cl, xdr_res, res_ptr)
594 CLIENT *cl;
595 xdrproc_t xdr_res;
596 caddr_t res_ptr;
597 {
598 struct cu_data *cu;
599 XDR *xdrs;
600 bool_t dummy;
601 #ifdef _REENTRANT
602 sigset_t mask;
603 #endif
604 sigset_t newmask;
605
606 _DIAGASSERT(cl != NULL);
607 cu = (struct cu_data *)cl->cl_private;
608 xdrs = &(cu->cu_outxdrs);
609
610 sigfillset(&newmask);
611 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
612 mutex_lock(&clnt_fd_lock);
613 while (dg_fd_locks[cu->cu_fd])
614 cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
615 xdrs->x_op = XDR_FREE;
616 dummy = (*xdr_res)(xdrs, res_ptr);
617 mutex_unlock(&clnt_fd_lock);
618 thr_sigsetmask(SIG_SETMASK, &mask, NULL);
619 cond_signal(&dg_cv[cu->cu_fd]);
620 return (dummy);
621 }
622
623 /*ARGSUSED*/
624 static void
625 clnt_dg_abort(h)
626 CLIENT *h;
627 {
628 }
629
630 static bool_t
631 clnt_dg_control(cl, request, info)
632 CLIENT *cl;
633 u_int request;
634 char *info;
635 {
636 struct cu_data *cu;
637 struct netbuf *addr;
638 #ifdef _REENTRANT
639 sigset_t mask;
640 #endif
641 sigset_t newmask;
642
643 _DIAGASSERT(cl != NULL);
644 /* info is handled below */
645
646 cu = (struct cu_data *)cl->cl_private;
647
648 sigfillset(&newmask);
649 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
650 mutex_lock(&clnt_fd_lock);
651 while (dg_fd_locks[cu->cu_fd])
652 cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
653 dg_fd_locks[cu->cu_fd] = __rpc_lock_value;
654 mutex_unlock(&clnt_fd_lock);
655 switch (request) {
656 case CLSET_FD_CLOSE:
657 cu->cu_closeit = TRUE;
658 release_fd_lock(cu->cu_fd, mask);
659 return (TRUE);
660 case CLSET_FD_NCLOSE:
661 cu->cu_closeit = FALSE;
662 release_fd_lock(cu->cu_fd, mask);
663 return (TRUE);
664 }
665
666 /* for other requests which use info */
667 if (info == NULL) {
668 release_fd_lock(cu->cu_fd, mask);
669 return (FALSE);
670 }
671 switch (request) {
672 case CLSET_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_total = *(struct timeval *)(void *)info;
678 break;
679 case CLGET_TIMEOUT:
680 *(struct timeval *)(void *)info = cu->cu_total;
681 break;
682 case CLGET_SERVER_ADDR: /* Give him the fd address */
683 /* Now obsolete. Only for backward compatibility */
684 (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
685 break;
686 case CLSET_RETRY_TIMEOUT:
687 if (time_not_ok((struct timeval *)(void *)info)) {
688 release_fd_lock(cu->cu_fd, mask);
689 return (FALSE);
690 }
691 cu->cu_wait = *(struct timeval *)(void *)info;
692 break;
693 case CLGET_RETRY_TIMEOUT:
694 *(struct timeval *)(void *)info = cu->cu_wait;
695 break;
696 case CLGET_FD:
697 *(int *)(void *)info = cu->cu_fd;
698 break;
699 case CLGET_SVC_ADDR:
700 addr = (struct netbuf *)(void *)info;
701 addr->buf = &cu->cu_raddr;
702 addr->len = cu->cu_rlen;
703 addr->maxlen = sizeof cu->cu_raddr;
704 break;
705 case CLSET_SVC_ADDR: /* set to new address */
706 addr = (struct netbuf *)(void *)info;
707 if (addr->len < sizeof cu->cu_raddr) {
708 release_fd_lock(cu->cu_fd, mask);
709 return (FALSE);
710 }
711 (void) memcpy(&cu->cu_raddr, addr->buf, (size_t)addr->len);
712 cu->cu_rlen = addr->len;
713 break;
714 case CLGET_XID:
715 /*
716 * use the knowledge that xid is the
717 * first element in the call structure *.
718 * This will get the xid of the PREVIOUS call
719 */
720 *(u_int32_t *)(void *)info =
721 ntohl(*(u_int32_t *)(void *)cu->cu_outbuf);
722 break;
723
724 case CLSET_XID:
725 /* This will set the xid of the NEXT call */
726 *(u_int32_t *)(void *)cu->cu_outbuf =
727 htonl(*(u_int32_t *)(void *)info - 1);
728 /* decrement by 1 as clnt_dg_call() increments once */
729 break;
730
731 case CLGET_VERS:
732 /*
733 * This RELIES on the information that, in the call body,
734 * the version number field is the fifth field from the
735 * begining of the RPC header. MUST be changed if the
736 * call_struct is changed
737 */
738 *(u_int32_t *)(void *)info =
739 ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
740 4 * BYTES_PER_XDR_UNIT));
741 break;
742
743 case CLSET_VERS:
744 *(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
745 = htonl(*(u_int32_t *)(void *)info);
746 break;
747
748 case CLGET_PROG:
749 /*
750 * This RELIES on the information that, in the call body,
751 * the program number field is the fourth field from the
752 * begining of the RPC header. MUST be changed if the
753 * call_struct is changed
754 */
755 *(u_int32_t *)(void *)info =
756 ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
757 3 * BYTES_PER_XDR_UNIT));
758 break;
759
760 case CLSET_PROG:
761 *(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
762 = htonl(*(u_int32_t *)(void *)info);
763 break;
764
765 default:
766 release_fd_lock(cu->cu_fd, mask);
767 return (FALSE);
768 }
769 release_fd_lock(cu->cu_fd, mask);
770 return (TRUE);
771 }
772
773 static void
774 clnt_dg_destroy(cl)
775 CLIENT *cl;
776 {
777 struct cu_data *cu;
778 int cu_fd;
779 #ifdef _REENTRANT
780 sigset_t mask;
781 #endif
782 sigset_t newmask;
783
784 _DIAGASSERT(cl != NULL);
785
786 cu = (struct cu_data *)cl->cl_private;
787 cu_fd = cu->cu_fd;
788
789 sigfillset(&newmask);
790 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
791 mutex_lock(&clnt_fd_lock);
792 while (dg_fd_locks[cu_fd])
793 cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
794 if (cu->cu_closeit)
795 (void) close(cu_fd);
796 XDR_DESTROY(&(cu->cu_outxdrs));
797 mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
798 if (cl->cl_netid && cl->cl_netid[0])
799 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
800 if (cl->cl_tp && cl->cl_tp[0])
801 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
802 mem_free(cl, sizeof (CLIENT));
803 mutex_unlock(&clnt_fd_lock);
804 thr_sigsetmask(SIG_SETMASK, &mask, NULL);
805 cond_signal(&dg_cv[cu_fd]);
806 }
807
808 static struct clnt_ops *
809 clnt_dg_ops()
810 {
811 static struct clnt_ops ops;
812 #ifdef _REENTRANT
813 extern mutex_t ops_lock;
814 sigset_t mask;
815 #endif
816 sigset_t newmask;
817
818 /* VARIABLES PROTECTED BY ops_lock: ops */
819
820 sigfillset(&newmask);
821 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
822 mutex_lock(&ops_lock);
823 if (ops.cl_call == NULL) {
824 ops.cl_call = clnt_dg_call;
825 ops.cl_abort = clnt_dg_abort;
826 ops.cl_geterr = clnt_dg_geterr;
827 ops.cl_freeres = clnt_dg_freeres;
828 ops.cl_destroy = clnt_dg_destroy;
829 ops.cl_control = clnt_dg_control;
830 }
831 mutex_unlock(&ops_lock);
832 thr_sigsetmask(SIG_SETMASK, &mask, NULL);
833 return (&ops);
834 }
835
836 /*
837 * Make sure that the time is not garbage. -1 value is allowed.
838 */
839 static bool_t
840 time_not_ok(t)
841 struct timeval *t;
842 {
843
844 _DIAGASSERT(t != NULL);
845
846 return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
847 t->tv_usec < -1 || t->tv_usec > 1000000);
848 }
849