Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/clnt_dg.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /*
29 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
30 */
31
32 /*
33 * Implements a connectionless client side RPC.
34 */
35 #include <pthread.h>
36 #include <reentrant.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <stdint.h>
40 #include <poll.h>
41
42 #include <sys/time.h>
43
44 #include <sys/ioctl.h>
45 #include <rpc/clnt.h>
46 #include <arpa/inet.h>
47 #include <rpc/rpc.h>
48 #include <rpc/xdr.h>
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <signal.h>
53 #include <unistd.h>
54 #include <err.h>
55 #include "rpc_com.h"
56 #include "clnt_fd_locks.h"
57
58 #ifdef IP_RECVERR
59 #include <asm/types.h>
60 #include <linux/errqueue.h>
61 #include <sys/uio.h>
62 #endif
63
64 #ifdef HAVE_RPCSEC_GSS
65 #include <rpc/auth_gss.h>
66 #endif
67
68 #define MAX_DEFAULT_FDS 20000
69
70 static struct clnt_ops *clnt_dg_ops(void);
71 static bool_t time_not_ok(struct timeval *);
72 static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
73 xdrproc_t, void *, struct timeval);
74 static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
75 static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
76 static void clnt_dg_abort(CLIENT *);
77 static bool_t clnt_dg_control(CLIENT *, u_int, void *);
78 static void clnt_dg_destroy(CLIENT *);
79
80
81 /*
82 * This machinery implements per-fd locks for MT-safety. It is not
83 * sufficient to do per-CLIENT handle locks for MT-safety because a
84 * user may create more than one CLIENT handle with the same fd behind
85 * it.
86 *
87 * We keep track of a list of per-fd locks, protected by the clnt_fd_lock
88 * mutex. Each per-fd lock consists of a predicate indicating whether is
89 * active or not: fd_lock->active == TRUE => a call is active on some
90 * CLIENT handle created for that fd. Each fd predicate is guarded by a
91 * condition variable so that the global mutex can be unlocked while
92 * waiting for the predicate to change.
93 *
94 * The current implementation holds locks across the entire RPC and reply,
95 * including retransmissions. Yes, this is silly, and as soon as this
96 * code is proven to work, this should be the first thing fixed. One step
97 * at a time.
98 */
99 static fd_locks_t *dg_fd_locks;
100 extern mutex_t clnt_fd_lock;
101 #define release_fd_lock(fd_lock, mask) { \
102 mutex_lock(&clnt_fd_lock); \
103 fd_lock->active = FALSE; \
104 fd_lock->pending--; \
105 thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \
106 cond_signal(&fd_lock->cv); \
107 mutex_unlock(&clnt_fd_lock); \
108 }
109
110 static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
111
112 /* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */
113
114 /*
115 * Private data kept per client handle
116 */
117 struct cu_data {
118 int cu_fd; /* connections fd */
119 fd_lock_t *cu_fd_lock;
120 bool_t cu_closeit; /* opened by library */
121 struct sockaddr_storage cu_raddr; /* remote address */
122 int cu_rlen;
123 struct timeval cu_wait; /* retransmit interval */
124 struct timeval cu_total; /* total time for the call */
125 struct rpc_err cu_error;
126 XDR cu_outxdrs;
127 u_int cu_xdrpos;
128 u_int cu_sendsz; /* send size */
129 char *cu_outbuf;
130 u_int cu_recvsz; /* recv size */
131 int cu_async;
132 int cu_connect; /* Use connect(). */
133 int cu_connected; /* Have done connect(). */
134 char cu_inbuf[1];
135 };
136
137 /*
138 * Connection less client creation returns with client handle parameters.
139 * Default options are set, which the user can change using clnt_control().
140 * fd should be open and bound.
141 * NB: The rpch->cl_auth is initialized to null authentication.
142 * Caller may wish to set this something more useful.
143 *
144 * sendsz and recvsz are the maximum allowable packet sizes that can be
145 * sent and received. Normally they are the same, but they can be
146 * changed to improve the program efficiency and buffer allocation.
147 * If they are 0, use the transport default.
148 *
149 * If svcaddr is NULL, returns NULL.
150 */
151 CLIENT *
152 clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
153 int fd; /* open file descriptor */
154 const struct netbuf *svcaddr; /* servers address */
155 rpcprog_t program; /* program number */
156 rpcvers_t version; /* version number */
157 u_int sendsz; /* buffer recv size */
158 u_int recvsz; /* buffer send size */
159 {
160 CLIENT *cl = NULL; /* client handle */
161 struct cu_data *cu = NULL; /* private data */
162 struct timeval now;
163 struct rpc_msg call_msg;
164 sigset_t mask;
165 sigset_t newmask;
166 struct __rpc_sockinfo si;
167 int one = 1;
168 fd_lock_t *fd_lock;
169
170 sigfillset(&newmask);
171 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
172 mutex_lock(&clnt_fd_lock);
173 if (dg_fd_locks == (fd_locks_t *) NULL) {
174 dg_fd_locks = fd_locks_init();
175 if (dg_fd_locks == (fd_locks_t *) NULL) {
176 mutex_unlock(&clnt_fd_lock);
177 goto err1;
178 }
179 }
180 fd_lock = fd_lock_create(fd, dg_fd_locks);
181 if (fd_lock == (fd_lock_t *) NULL) {
182 mutex_unlock(&clnt_fd_lock);
183 goto err1;
184 }
185
186 mutex_unlock(&clnt_fd_lock);
187 thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
188
189 if (svcaddr == NULL) {
190 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
191 return (NULL);
192 }
193
194 if (!__rpc_fd2sockinfo(fd, &si)) {
195 rpc_createerr.cf_stat = RPC_TLIERROR;
196 rpc_createerr.cf_error.re_errno = 0;
197 return (NULL);
198 }
199 /*
200 * Find the receive and the send size
201 */
202 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
203 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
204 if ((sendsz == 0) || (recvsz == 0)) {
205 rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
206 rpc_createerr.cf_error.re_errno = 0;
207 return (NULL);
208 }
209
210 if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
211 goto err1;
212 /*
213 * Should be multiple of 4 for XDR.
214 */
215 sendsz = ((sendsz + 3) / 4) * 4;
216 recvsz = ((recvsz + 3) / 4) * 4;
217 cu = mem_alloc(sizeof (*cu) + sendsz + recvsz);
218 if (cu == NULL)
219 goto err1;
220 (void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len);
221 cu->cu_rlen = svcaddr->len;
222 cu->cu_outbuf = &cu->cu_inbuf[recvsz];
223 /* Other values can also be set through clnt_control() */
224 cu->cu_wait.tv_sec = 15; /* heuristically chosen */
225 cu->cu_wait.tv_usec = 0;
226 cu->cu_total.tv_sec = -1;
227 cu->cu_total.tv_usec = -1;
228 cu->cu_sendsz = sendsz;
229 cu->cu_recvsz = recvsz;
230 cu->cu_async = FALSE;
231 cu->cu_connect = FALSE;
232 cu->cu_connected = FALSE;
233 (void) gettimeofday(&now, NULL);
234 call_msg.rm_xid = __RPC_GETXID(&now);
235 call_msg.rm_call.cb_prog = program;
236 call_msg.rm_call.cb_vers = version;
237 xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
238 if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
239 rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */
240 rpc_createerr.cf_error.re_errno = 0;
241 goto err2;
242 }
243 cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
244
245 /* XXX fvdl - do we still want this? */
246 #if 0
247 (void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
248 #endif
249 #ifdef IP_RECVERR
250 {
251 int on = 1;
252 setsockopt(fd, SOL_IP, IP_RECVERR, &on, sizeof(on));
253 }
254 #endif
255 ioctl(fd, FIONBIO, (char *)(void *)&one);
256 /*
257 * By default, closeit is always FALSE. It is users responsibility
258 * to do a close on it, else the user may use clnt_control
259 * to let clnt_destroy do it for him/her.
260 */
261 cu->cu_closeit = FALSE;
262 cu->cu_fd = fd;
263 cu->cu_fd_lock = fd_lock;
264 cl->cl_ops = clnt_dg_ops();
265 cl->cl_private = (caddr_t)(void *)cu;
266 cl->cl_auth = authnone_create();
267 cl->cl_tp = NULL;
268 cl->cl_netid = NULL;
269
270 return (cl);
271 err1:
272 warnx(mem_err_clnt_dg);
273 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
274 rpc_createerr.cf_error.re_errno = errno;
275 err2:
276 if (cl) {
277 mem_free(cl, sizeof (CLIENT));
278 if (cu)
279 mem_free(cu, sizeof (*cu) + sendsz + recvsz);
280 }
281 return (NULL);
282 }
283
284 static enum clnt_stat
285 clnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
286 CLIENT *cl; /* client handle */
287 rpcproc_t proc; /* procedure number */
288 xdrproc_t xargs; /* xdr routine for args */
289 void *argsp; /* pointer to args */
290 xdrproc_t xresults; /* xdr routine for results */
291 void *resultsp; /* pointer to results */
292 struct timeval utimeout; /* seconds to wait before giving up */
293 {
294 struct cu_data *cu = (struct cu_data *)cl->cl_private;
295 XDR *xdrs;
296 size_t outlen = 0;
297 struct rpc_msg reply_msg;
298 XDR reply_xdrs;
299 bool_t ok;
300 int nrefreshes = 2; /* number of times to refresh cred */
301 struct timeval timeout;
302 struct pollfd fd;
303 int total_time, nextsend_time, tv=0;
304 struct sockaddr *sa;
305 sigset_t mask;
306 sigset_t newmask;
307 socklen_t salen;
308 ssize_t recvlen = 0;
309 u_int32_t xid, inval, outval;
310
311 outlen = 0;
312 sigfillset(&newmask);
313 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
314 mutex_lock(&clnt_fd_lock);
315 cu->cu_fd_lock->pending++;
316 while (cu->cu_fd_lock->active)
317 cond_wait(&cu->cu_fd_lock->cv, &clnt_fd_lock);
318 cu->cu_fd_lock->active = TRUE;
319 mutex_unlock(&clnt_fd_lock);
320 if (cu->cu_total.tv_usec == -1) {
321 timeout = utimeout; /* use supplied timeout */
322 } else {
323 timeout = cu->cu_total; /* use default timeout */
324 }
325 total_time = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
326 nextsend_time = cu->cu_wait.tv_sec * 1000 + cu->cu_wait.tv_usec / 1000;
327
328 if (cu->cu_connect && !cu->cu_connected) {
329 if (connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr,
330 cu->cu_rlen) < 0) {
331 cu->cu_error.re_errno = errno;
332 cu->cu_error.re_status = RPC_CANTSEND;
333 goto out;
334 }
335 cu->cu_connected = 1;
336 }
337 if (cu->cu_connected) {
338 sa = NULL;
339 salen = 0;
340 } else {
341 sa = (struct sockaddr *)&cu->cu_raddr;
342 salen = cu->cu_rlen;
343 }
344
345 #ifdef HAVE_RPCSEC_GSS
346 if (is_authgss_client(cl))
347 nrefreshes = 0;
348 #endif
349
350 /* Clean up in case the last call ended in a longjmp(3) call. */
351 call_again:
352 xdrs = &(cu->cu_outxdrs);
353 if (cu->cu_async == TRUE && xargs == NULL)
354 goto get_reply;
355 xdrs->x_op = XDR_ENCODE;
356 XDR_SETPOS(xdrs, cu->cu_xdrpos);
357 /*
358 * the transaction is the first thing in the out buffer
359 * XXX Yes, and it's in network byte order, so we should to
360 * be careful when we increment it, shouldn't we.
361 */
362 xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf));
363 xid++;
364 *(u_int32_t *)(void *)(cu->cu_outbuf) = htonl(xid);
365
366 if ((! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
367 (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
368 (! AUTH_WRAP(cl->cl_auth, xdrs, xargs, argsp))) {
369 cu->cu_error.re_status = RPC_CANTENCODEARGS;
370 goto out;
371 }
372 outlen = (size_t)XDR_GETPOS(xdrs);
373
374 /*
375 * Hack to provide rpc-based message passing
376 */
377 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
378 cu->cu_error.re_status = RPC_TIMEDOUT;
379 goto out;
380 }
381
382 send_again:
383 if (total_time <= 0) {
384 cu->cu_error.re_status = RPC_TIMEDOUT;
385 goto out;
386 }
387 nextsend_time = cu->cu_wait.tv_sec * 1000 + cu->cu_wait.tv_usec / 1000;
388 if (sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) {
389 cu->cu_error.re_errno = errno;
390 cu->cu_error.re_status = RPC_CANTSEND;
391 goto out;
392 }
393
394 get_reply:
395
396 /*
397 * sub-optimal code appears here because we have
398 * some clock time to spare while the packets are in flight.
399 * (We assume that this is actually only executed once.)
400 */
401 reply_msg.acpted_rply.ar_verf = _null_auth;
402 reply_msg.acpted_rply.ar_results.where = NULL;
403 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
404
405 fd.fd = cu->cu_fd;
406 fd.events = POLLIN;
407 fd.revents = 0;
408 while (total_time > 0) {
409 tv = total_time < nextsend_time ? total_time : nextsend_time;
410 switch (poll(&fd, 1, tv)) {
411 case 0:
412 total_time -= tv;
413 goto send_again;
414 case -1:
415 if (errno == EINTR)
416 continue;
417 cu->cu_error.re_status = RPC_CANTRECV;
418 cu->cu_error.re_errno = errno;
419 goto out;
420 }
421 break;
422 }
423 #ifdef IP_RECVERR
424 if (fd.revents & POLLERR)
425 {
426 struct msghdr msg;
427 struct cmsghdr *cmsg;
428 struct sock_extended_err *e;
429 struct sockaddr_in err_addr;
430 struct sockaddr_in *sin = (struct sockaddr_in *)&cu->cu_raddr;
431 struct iovec iov;
432 char *cbuf = (char *) mem_alloc((outlen + 256));
433 int ret;
434
435 if (cbuf == NULL)
436 {
437 cu->cu_error.re_errno = errno;
438 return (cu->cu_error.re_status = RPC_CANTRECV);
439 }
440 iov.iov_base = cbuf + 256;
441 iov.iov_len = outlen;
442 msg.msg_name = (void *) &err_addr;
443 msg.msg_namelen = sizeof (err_addr);
444 msg.msg_iov = &iov;
445 msg.msg_iovlen = 1;
446 msg.msg_flags = 0;
447 msg.msg_control = cbuf;
448 msg.msg_controllen = 256;
449 ret = recvmsg (cu->cu_fd, &msg, MSG_ERRQUEUE);
450 if (ret >= 0
451 && memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
452 && (msg.msg_flags & MSG_ERRQUEUE)
453 && ((msg.msg_namelen == 0
454 && ret >= 12)
455 || (msg.msg_namelen == sizeof (err_addr)
456 && err_addr.sin_family == AF_INET
457 && memcmp (&err_addr.sin_addr, &sin->sin_addr,
458 sizeof (err_addr.sin_addr)) == 0
459 && err_addr.sin_port == sin->sin_port)))
460 for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
461 cmsg = CMSG_NXTHDR (&msg, cmsg))
462 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
463 {
464 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
465 cu->cu_error.re_errno = e->ee_errno;
466 mem_free(cbuf, (outlen + 256));
467 release_fd_lock(cu->cu_fd_lock, mask);
468 return (cu->cu_error.re_status = RPC_CANTRECV);
469 }
470 mem_free(cbuf, (outlen + 256));
471 }
472 #endif
473
474 /* We have some data now */
475 do {
476 recvlen = recvfrom(cu->cu_fd, cu->cu_inbuf,
477 cu->cu_recvsz, 0, NULL, NULL);
478 } while (recvlen < 0 && errno == EINTR);
479 if (recvlen < 0 && errno != EWOULDBLOCK) {
480 cu->cu_error.re_errno = errno;
481 cu->cu_error.re_status = RPC_CANTRECV;
482 goto out;
483 }
484
485 if (recvlen < sizeof(u_int32_t)) {
486 total_time -= tv;
487 goto send_again;
488 }
489
490 if (cu->cu_async == FALSE) {
491 memcpy(&inval, cu->cu_inbuf, sizeof(u_int32_t));
492 memcpy(&outval, cu->cu_outbuf, sizeof(u_int32_t));
493 if (inval != outval) {
494 total_time -= tv;
495 goto send_again;
496 }
497 }
498
499 /*
500 * now decode and validate the response
501 */
502
503 xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
504 ok = xdr_replymsg(&reply_xdrs, &reply_msg);
505 /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
506 if (ok) {
507 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
508 (reply_msg.acpted_rply.ar_stat == SUCCESS))
509 cu->cu_error.re_status = RPC_SUCCESS;
510 else
511 _seterr_reply(&reply_msg, &(cu->cu_error));
512
513 if (cu->cu_error.re_status == RPC_SUCCESS) {
514 if (! AUTH_VALIDATE(cl->cl_auth,
515 &reply_msg.acpted_rply.ar_verf)) {
516 cu->cu_error.re_status = RPC_AUTHERROR;
517 cu->cu_error.re_why = AUTH_INVALIDRESP;
518 } else if (! AUTH_UNWRAP(cl->cl_auth, &reply_xdrs,
519 xresults, resultsp)) {
520 if (cu->cu_error.re_status == RPC_SUCCESS)
521 cu->cu_error.re_status = RPC_CANTDECODERES;
522 }
523 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
524 xdrs->x_op = XDR_FREE;
525 (void) xdr_opaque_auth(xdrs,
526 &(reply_msg.acpted_rply.ar_verf));
527 }
528 } /* end successful completion */
529 /*
530 * If unsuccesful AND error is an authentication error
531 * then refresh credentials and try again, else break
532 */
533 else if (cu->cu_error.re_status == RPC_AUTHERROR)
534 /* maybe our credentials need to be refreshed ... */
535 if (nrefreshes > 0 &&
536 AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
537 nrefreshes--;
538 goto call_again;
539 }
540 /* end of unsuccessful completion */
541 } /* end of valid reply message */
542 else {
543 cu->cu_error.re_status = RPC_CANTDECODERES;
544
545 }
546 out:
547 release_fd_lock(cu->cu_fd_lock, mask);
548 return (cu->cu_error.re_status);
549 }
550
551 static void
552 clnt_dg_geterr(cl, errp)
553 CLIENT *cl;
554 struct rpc_err *errp;
555 {
556 struct cu_data *cu = (struct cu_data *)cl->cl_private;
557
558 *errp = cu->cu_error;
559 }
560
561 static bool_t
562 clnt_dg_freeres(cl, xdr_res, res_ptr)
563 CLIENT *cl;
564 xdrproc_t xdr_res;
565 void *res_ptr;
566 {
567 struct cu_data *cu = (struct cu_data *)cl->cl_private;
568 XDR *xdrs = &(cu->cu_outxdrs);
569 bool_t dummy;
570 sigset_t mask;
571 sigset_t newmask;
572
573 sigfillset(&newmask);
574 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
575 mutex_lock(&clnt_fd_lock);
576 cu->cu_fd_lock->pending++;
577 while (cu->cu_fd_lock->active)
578 cond_wait(&cu->cu_fd_lock->cv, &clnt_fd_lock);
579 xdrs->x_op = XDR_FREE;
580 dummy = (*xdr_res)(xdrs, res_ptr);
581 cu->cu_fd_lock->pending--;
582 thr_sigsetmask(SIG_SETMASK, &mask, NULL);
583 cond_signal(&cu->cu_fd_lock->cv);
584 mutex_unlock(&clnt_fd_lock);
585 return (dummy);
586 }
587
588 /*ARGSUSED*/
589 static void
590 clnt_dg_abort(h)
591 CLIENT *h;
592 {
593 }
594
595 static bool_t
596 clnt_dg_control(cl, request, info)
597 CLIENT *cl;
598 u_int request;
599 void *info;
600 {
601 struct cu_data *cu = (struct cu_data *)cl->cl_private;
602 struct netbuf *addr;
603 sigset_t mask;
604 sigset_t newmask;
605
606 sigfillset(&newmask);
607 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
608 mutex_lock(&clnt_fd_lock);
609 cu->cu_fd_lock->pending++;
610 while (cu->cu_fd_lock->active)
611 cond_wait(&cu->cu_fd_lock->cv, &clnt_fd_lock);
612 cu->cu_fd_lock->active = TRUE;
613 mutex_unlock(&clnt_fd_lock);
614 switch (request) {
615 case CLSET_FD_CLOSE:
616 cu->cu_closeit = TRUE;
617 release_fd_lock(cu->cu_fd_lock, mask);
618 return (TRUE);
619 case CLSET_FD_NCLOSE:
620 cu->cu_closeit = FALSE;
621 release_fd_lock(cu->cu_fd_lock, mask);
622 return (TRUE);
623 }
624
625 /* for other requests which use info */
626 if (info == NULL) {
627 release_fd_lock(cu->cu_fd_lock, mask);
628 return (FALSE);
629 }
630 switch (request) {
631 case CLSET_TIMEOUT:
632 if (time_not_ok((struct timeval *)info)) {
633 release_fd_lock(cu->cu_fd_lock, mask);
634 return (FALSE);
635 }
636 cu->cu_total = *(struct timeval *)info;
637 break;
638 case CLGET_TIMEOUT:
639 *(struct timeval *)info = cu->cu_total;
640 break;
641 case CLGET_SERVER_ADDR: /* Give him the fd address */
642 /* Now obsolete. Only for backward compatibility */
643 (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
644 break;
645 case CLSET_RETRY_TIMEOUT:
646 if (time_not_ok((struct timeval *)info)) {
647 release_fd_lock(cu->cu_fd_lock, mask);
648 return (FALSE);
649 }
650 cu->cu_wait = *(struct timeval *)info;
651 break;
652 case CLGET_RETRY_TIMEOUT:
653 *(struct timeval *)info = cu->cu_wait;
654 break;
655 case CLGET_FD:
656 *(int *)info = cu->cu_fd;
657 break;
658 case CLGET_SVC_ADDR:
659 addr = (struct netbuf *)info;
660 addr->buf = &cu->cu_raddr;
661 addr->len = cu->cu_rlen;
662 addr->maxlen = sizeof cu->cu_raddr;
663 break;
664 case CLSET_SVC_ADDR: /* set to new address */
665 addr = (struct netbuf *)info;
666 if (addr->len < sizeof cu->cu_raddr) {
667 release_fd_lock(cu->cu_fd_lock, mask);
668 return (FALSE);
669 }
670 (void) memcpy(&cu->cu_raddr, addr->buf, addr->len);
671 cu->cu_rlen = addr->len;
672 break;
673 case CLGET_XID:
674 /*
675 * use the knowledge that xid is the
676 * first element in the call structure *.
677 * This will get the xid of the PREVIOUS call
678 */
679 *(u_int32_t *)info =
680 ntohl(*(u_int32_t *)(void *)cu->cu_outbuf);
681 break;
682
683 case CLSET_XID:
684 /* This will set the xid of the NEXT call */
685 *(u_int32_t *)(void *)cu->cu_outbuf =
686 htonl(*(u_int32_t *)info - 1);
687 /* decrement by 1 as clnt_dg_call() increments once */
688 break;
689
690 case CLGET_VERS:
691 /*
692 * This RELIES on the information that, in the call body,
693 * the version number field is the fifth field from the
694 * begining of the RPC header. MUST be changed if the
695 * call_struct is changed
696 */
697 *(u_int32_t *)info =
698 ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
699 4 * BYTES_PER_XDR_UNIT));
700 break;
701
702 case CLSET_VERS:
703 *(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
704 = htonl(*(u_int32_t *)info);
705 break;
706
707 case CLGET_PROG:
708 /*
709 * This RELIES on the information that, in the call body,
710 * the program number field is the fourth field from the
711 * begining of the RPC header. MUST be changed if the
712 * call_struct is changed
713 */
714 *(u_int32_t *)info =
715 ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
716 3 * BYTES_PER_XDR_UNIT));
717 break;
718
719 case CLSET_PROG:
720 *(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
721 = htonl(*(u_int32_t *)info);
722 break;
723 case CLSET_ASYNC:
724 cu->cu_async = *(int *)info;
725 break;
726 case CLSET_CONNECT:
727 cu->cu_connect = *(int *)info;
728 break;
729 default:
730 release_fd_lock(cu->cu_fd_lock, mask);
731 return (FALSE);
732 }
733 release_fd_lock(cu->cu_fd_lock, mask);
734 return (TRUE);
735 }
736
737 static void
738 clnt_dg_destroy(cl)
739 CLIENT *cl;
740 {
741 struct cu_data *cu = (struct cu_data *)cl->cl_private;
742 int cu_fd = cu->cu_fd;
743 fd_lock_t *cu_fd_lock = cu->cu_fd_lock;
744 sigset_t mask;
745 sigset_t newmask;
746
747 sigfillset(&newmask);
748 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
749 mutex_lock(&clnt_fd_lock);
750 /* wait until all pending operations on client are completed. */
751 while (cu_fd_lock->pending > 0) {
752 /* If a blocked operation can be awakened, then do it. */
753 if (cu_fd_lock->active == FALSE)
754 cond_signal(&cu_fd_lock->cv);
755 /* keep waiting... */
756 cond_wait(&cu_fd_lock->cv, &clnt_fd_lock);
757 }
758 if (cu->cu_closeit)
759 (void)close(cu_fd);
760 XDR_DESTROY(&(cu->cu_outxdrs));
761 mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
762 if (cl->cl_netid && cl->cl_netid[0])
763 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
764 if (cl->cl_tp && cl->cl_tp[0])
765 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
766 mem_free(cl, sizeof (CLIENT));
767 cond_signal(&cu_fd_lock->cv);
768 fd_lock_destroy(cu_fd, cu_fd_lock, dg_fd_locks);
769 mutex_unlock(&clnt_fd_lock);
770 thr_sigsetmask(SIG_SETMASK, &mask, NULL);
771 }
772
773 static struct clnt_ops *
774 clnt_dg_ops()
775 {
776 static struct clnt_ops ops;
777 extern mutex_t ops_lock;
778 sigset_t mask;
779 sigset_t newmask;
780
781 /* VARIABLES PROTECTED BY ops_lock: ops */
782
783 sigfillset(&newmask);
784 thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
785 mutex_lock(&ops_lock);
786 if (ops.cl_call == NULL) {
787 ops.cl_call = clnt_dg_call;
788 ops.cl_abort = clnt_dg_abort;
789 ops.cl_geterr = clnt_dg_geterr;
790 ops.cl_freeres = clnt_dg_freeres;
791 ops.cl_destroy = clnt_dg_destroy;
792 ops.cl_control = clnt_dg_control;
793 }
794 mutex_unlock(&ops_lock);
795 thr_sigsetmask(SIG_SETMASK, &mask, NULL);
796 return (&ops);
797 }
798
799 /*
800 * Make sure that the time is not garbage. -1 value is allowed.
801 */
802 static bool_t
803 time_not_ok(t)
804 struct timeval *t;
805 {
806 return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
807 t->tv_usec < -1 || t->tv_usec > 1000000);
808 }
809