xref: /netbsd-src/lib/libc/rpc/clnt_vc.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: clnt_vc.c,v 1.10 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 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)clnt_tcp.c	2.2 88/08/01 4.0 RPCSRC";
37 static char sccsid[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
38 #else
39 __RCSID("$NetBSD: clnt_vc.c,v 1.10 2003/09/09 00:22:17 itojun Exp $");
40 #endif
41 #endif
42 
43 /*
44  * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
45  *
46  * Copyright (C) 1984, Sun Microsystems, Inc.
47  *
48  * TCP based RPC supports 'batched calls'.
49  * A sequence of calls may be batched-up in a send buffer.  The rpc call
50  * return immediately to the client even though the call was not necessarily
51  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
52  * the rpc timeout value is zero (see clnt.h, rpc).
53  *
54  * Clients should NOT casually batch calls that in fact return results; that is,
55  * the server side should be aware that a call is batched and not produce any
56  * return message.  Batched calls that produce many result messages can
57  * deadlock (netlock) the client and the server....
58  *
59  * Now go hang yourself.
60  */
61 
62 #include "namespace.h"
63 #include "reentrant.h"
64 #include <sys/types.h>
65 #include <sys/poll.h>
66 #include <sys/socket.h>
67 
68 #include <assert.h>
69 #include <err.h>
70 #include <errno.h>
71 #include <netdb.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76 #include <signal.h>
77 
78 #include <rpc/rpc.h>
79 
80 #include "rpc_internal.h"
81 
82 #ifdef __weak_alias
83 __weak_alias(clnt_vc_create,_clnt_vc_create)
84 #endif
85 
86 #define MCALL_MSG_SIZE 24
87 
88 static enum clnt_stat clnt_vc_call __P((CLIENT *, rpcproc_t, xdrproc_t, caddr_t,
89     xdrproc_t, caddr_t, struct timeval));
90 static void clnt_vc_geterr __P((CLIENT *, struct rpc_err *));
91 static bool_t clnt_vc_freeres __P((CLIENT *, xdrproc_t, caddr_t));
92 static void clnt_vc_abort __P((CLIENT *));
93 static bool_t clnt_vc_control __P((CLIENT *, u_int, char *));
94 static void clnt_vc_destroy __P((CLIENT *));
95 static struct clnt_ops *clnt_vc_ops __P((void));
96 static bool_t time_not_ok __P((struct timeval *));
97 static int read_vc __P((caddr_t, caddr_t, int));
98 static int write_vc __P((caddr_t, caddr_t, int));
99 
100 struct ct_data {
101 	int		ct_fd;
102 	bool_t		ct_closeit;
103 	struct timeval	ct_wait;
104 	bool_t          ct_waitset;       /* wait set by clnt_control? */
105 	struct netbuf	ct_addr;
106 	struct rpc_err	ct_error;
107 	union {
108 		char	ct_mcallc[MCALL_MSG_SIZE];	/* marshalled callmsg */
109 		u_int32_t ct_mcalli;
110 	} ct_u;
111 	u_int		ct_mpos;			/* pos after marshal */
112 	XDR		ct_xdrs;
113 };
114 
115 /*
116  *      This machinery implements per-fd locks for MT-safety.  It is not
117  *      sufficient to do per-CLIENT handle locks for MT-safety because a
118  *      user may create more than one CLIENT handle with the same fd behind
119  *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
120  *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
121  *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is activte on some
122  *      CLIENT handle created for that fd.
123  *      The current implementation holds locks across the entire RPC and reply.
124  *      Yes, this is silly, and as soon as this code is proven to work, this
125  *      should be the first thing fixed.  One step at a time.
126  */
127 #ifdef _REENTRANT
128 static int      *vc_fd_locks;
129 extern int __isthreaded;
130 #define __rpc_lock_value __isthreaded;
131 extern mutex_t  clnt_fd_lock;
132 static cond_t   *vc_cv;
133 #define release_fd_lock(fd, mask) {             \
134 	mutex_lock(&clnt_fd_lock);      \
135 	vc_fd_locks[fd] = 0;            \
136 	mutex_unlock(&clnt_fd_lock);    \
137 	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);        \
138 	cond_signal(&vc_cv[fd]);        \
139 }
140 #else
141 #define release_fd_lock(fd,mask)
142 #define __rpc_lock_value 0
143 #endif
144 
145 
146 /*
147  * Create a client handle for a connection.
148  * Default options are set, which the user can change using clnt_control()'s.
149  * The rpc/vc package does buffering similar to stdio, so the client
150  * must pick send and receive buffer sizes, 0 => use the default.
151  * NB: fd is copied into a private area.
152  * NB: The rpch->cl_auth is set null authentication. Caller may wish to
153  * set this something more useful.
154  *
155  * fd should be an open socket
156  */
157 CLIENT *
158 clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
159 	int fd;
160 	const struct netbuf *raddr;
161 	rpcprog_t prog;
162 	rpcvers_t vers;
163 	u_int sendsz;
164 	u_int recvsz;
165 {
166 	CLIENT *h;
167 	struct ct_data *ct = NULL;
168 	struct rpc_msg call_msg;
169 #ifdef _REENTRANT
170 	sigset_t mask;
171 #endif
172 	sigset_t newmask;
173 	struct sockaddr_storage ss;
174 	socklen_t slen;
175 	struct __rpc_sockinfo si;
176 
177 	_DIAGASSERT(raddr != NULL);
178 
179 	h  = mem_alloc(sizeof(*h));
180 	if (h == NULL) {
181 		warnx("clnt_vc_create: out of memory");
182 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
183 		rpc_createerr.cf_error.re_errno = errno;
184 		goto fooy;
185 	}
186 	ct = mem_alloc(sizeof(*ct));
187 	if (ct == NULL) {
188 		warnx("clnt_vc_create: out of memory");
189 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
190 		rpc_createerr.cf_error.re_errno = errno;
191 		goto fooy;
192 	}
193 
194 	sigfillset(&newmask);
195 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
196 #ifdef _REENTRANT
197 	mutex_lock(&clnt_fd_lock);
198 	if (vc_fd_locks == (int *) NULL) {
199 		size_t cv_allocsz, fd_allocsz;
200 		int dtbsize = __rpc_dtbsize();
201 
202 		fd_allocsz = dtbsize * sizeof (int);
203 		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
204 		if (vc_fd_locks == (int *) NULL) {
205 			mutex_unlock(&clnt_fd_lock);
206 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
207 			goto fooy;
208 		} else
209 			memset(vc_fd_locks, '\0', fd_allocsz);
210 
211 		assert(vc_cv == (cond_t *) NULL);
212 		cv_allocsz = dtbsize * sizeof (cond_t);
213 		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
214 		if (vc_cv == (cond_t *) NULL) {
215 			mem_free(vc_fd_locks, fd_allocsz);
216 			vc_fd_locks = (int *) NULL;
217 			mutex_unlock(&clnt_fd_lock);
218 			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
219 			goto fooy;
220 		} else {
221 			int i;
222 
223 			for (i = 0; i < dtbsize; i++)
224 				cond_init(&vc_cv[i], 0, (void *) 0);
225 		}
226 	} else
227 		assert(vc_cv != (cond_t *) NULL);
228 #endif
229 
230 	/*
231 	 * XXX - fvdl connecting while holding a mutex?
232 	 */
233 	slen = sizeof ss;
234 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
235 		if (errno != ENOTCONN) {
236 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
237 			rpc_createerr.cf_error.re_errno = errno;
238 			mutex_unlock(&clnt_fd_lock);
239 			goto fooy;
240 		}
241 		if (connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
242 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
243 			rpc_createerr.cf_error.re_errno = errno;
244 			mutex_unlock(&clnt_fd_lock);
245 			goto fooy;
246 		}
247 	}
248 	mutex_unlock(&clnt_fd_lock);
249 	if (!__rpc_fd2sockinfo(fd, &si))
250 		goto fooy;
251 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
252 
253 	ct->ct_closeit = FALSE;
254 
255 	/*
256 	 * Set up private data struct
257 	 */
258 	ct->ct_fd = fd;
259 	ct->ct_wait.tv_usec = 0;
260 	ct->ct_waitset = FALSE;
261 	ct->ct_addr.buf = malloc(raddr->maxlen);
262 	if (ct->ct_addr.buf == NULL)
263 		goto fooy;
264 	memcpy(ct->ct_addr.buf, &raddr->buf, raddr->len);
265 	ct->ct_addr.len = raddr->maxlen;
266 	ct->ct_addr.maxlen = raddr->maxlen;
267 
268 	/*
269 	 * Initialize call message
270 	 */
271 	call_msg.rm_xid = __RPC_GETXID();
272 	call_msg.rm_direction = CALL;
273 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
274 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
275 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
276 
277 	/*
278 	 * pre-serialize the static part of the call msg and stash it away
279 	 */
280 	xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
281 	    XDR_ENCODE);
282 	if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
283 		if (ct->ct_closeit) {
284 			(void)close(fd);
285 		}
286 		goto fooy;
287 	}
288 	ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
289 	XDR_DESTROY(&(ct->ct_xdrs));
290 
291 	/*
292 	 * Create a client handle which uses xdrrec for serialization
293 	 * and authnone for authentication.
294 	 */
295 	h->cl_ops = clnt_vc_ops();
296 	h->cl_private = ct;
297 	h->cl_auth = authnone_create();
298 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
299 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
300 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
301 	    h->cl_private, read_vc, write_vc);
302 	return (h);
303 
304 fooy:
305 	/*
306 	 * Something goofed, free stuff and barf
307 	 */
308 	if (ct)
309 		mem_free(ct, sizeof(struct ct_data));
310 	if (h)
311 		mem_free(h, sizeof(CLIENT));
312 	return (NULL);
313 }
314 
315 static enum clnt_stat
316 clnt_vc_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
317 	CLIENT *h;
318 	rpcproc_t proc;
319 	xdrproc_t xdr_args;
320 	caddr_t args_ptr;
321 	xdrproc_t xdr_results;
322 	caddr_t results_ptr;
323 	struct timeval timeout;
324 {
325 	struct ct_data *ct;
326 	XDR *xdrs;
327 	struct rpc_msg reply_msg;
328 	u_int32_t x_id;
329 	u_int32_t *msg_x_id;
330 	bool_t shipnow;
331 	int refreshes = 2;
332 #ifdef _REENTRANT
333 	sigset_t mask, newmask;
334 #endif
335 
336 	_DIAGASSERT(h != NULL);
337 
338 	ct = (struct ct_data *) h->cl_private;
339 
340 #ifdef _REENTRANT
341 	sigfillset(&newmask);
342 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
343 	mutex_lock(&clnt_fd_lock);
344 	while (vc_fd_locks[ct->ct_fd])
345 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
346 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
347 	mutex_unlock(&clnt_fd_lock);
348 #endif
349 
350 	xdrs = &(ct->ct_xdrs);
351 	msg_x_id = &ct->ct_u.ct_mcalli;
352 
353 	if (!ct->ct_waitset) {
354 		if (time_not_ok(&timeout) == FALSE)
355 		ct->ct_wait = timeout;
356 	}
357 
358 	shipnow =
359 	    (xdr_results == NULL && timeout.tv_sec == 0
360 	    && timeout.tv_usec == 0) ? FALSE : TRUE;
361 
362 call_again:
363 	xdrs->x_op = XDR_ENCODE;
364 	ct->ct_error.re_status = RPC_SUCCESS;
365 	x_id = ntohl(--(*msg_x_id));
366 	if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
367 	    (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
368 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
369 	    (! (*xdr_args)(xdrs, args_ptr))) {
370 		if (ct->ct_error.re_status == RPC_SUCCESS)
371 			ct->ct_error.re_status = RPC_CANTENCODEARGS;
372 		(void)xdrrec_endofrecord(xdrs, TRUE);
373 		release_fd_lock(ct->ct_fd, mask);
374 		return (ct->ct_error.re_status);
375 	}
376 	if (! xdrrec_endofrecord(xdrs, shipnow)) {
377 		release_fd_lock(ct->ct_fd, mask);
378 		return (ct->ct_error.re_status = RPC_CANTSEND);
379 	}
380 	if (! shipnow) {
381 		release_fd_lock(ct->ct_fd, mask);
382 		return (RPC_SUCCESS);
383 	}
384 	/*
385 	 * Hack to provide rpc-based message passing
386 	 */
387 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
388 		release_fd_lock(ct->ct_fd, mask);
389 		return(ct->ct_error.re_status = RPC_TIMEDOUT);
390 	}
391 
392 
393 	/*
394 	 * Keep receiving until we get a valid transaction id
395 	 */
396 	xdrs->x_op = XDR_DECODE;
397 	for (;;) {
398 		reply_msg.acpted_rply.ar_verf = _null_auth;
399 		reply_msg.acpted_rply.ar_results.where = NULL;
400 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
401 		if (! xdrrec_skiprecord(xdrs)) {
402 			release_fd_lock(ct->ct_fd, mask);
403 			return (ct->ct_error.re_status);
404 		}
405 		/* now decode and validate the response header */
406 		if (! xdr_replymsg(xdrs, &reply_msg)) {
407 			if (ct->ct_error.re_status == RPC_SUCCESS)
408 				continue;
409 			release_fd_lock(ct->ct_fd, mask);
410 			return (ct->ct_error.re_status);
411 		}
412 		if (reply_msg.rm_xid == x_id)
413 			break;
414 	}
415 
416 	/*
417 	 * process header
418 	 */
419 	_seterr_reply(&reply_msg, &(ct->ct_error));
420 	if (ct->ct_error.re_status == RPC_SUCCESS) {
421 		if (! AUTH_VALIDATE(h->cl_auth,
422 		    &reply_msg.acpted_rply.ar_verf)) {
423 			ct->ct_error.re_status = RPC_AUTHERROR;
424 			ct->ct_error.re_why = AUTH_INVALIDRESP;
425 		} else if (! (*xdr_results)(xdrs, results_ptr)) {
426 			if (ct->ct_error.re_status == RPC_SUCCESS)
427 				ct->ct_error.re_status = RPC_CANTDECODERES;
428 		}
429 		/* free verifier ... */
430 		if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
431 			xdrs->x_op = XDR_FREE;
432 			(void)xdr_opaque_auth(xdrs,
433 			    &(reply_msg.acpted_rply.ar_verf));
434 		}
435 	}  /* end successful completion */
436 	else {
437 		/* maybe our credentials need to be refreshed ... */
438 		if (refreshes-- && AUTH_REFRESH(h->cl_auth))
439 			goto call_again;
440 	}  /* end of unsuccessful completion */
441 	release_fd_lock(ct->ct_fd, mask);
442 	return (ct->ct_error.re_status);
443 }
444 
445 static void
446 clnt_vc_geterr(h, errp)
447 	CLIENT *h;
448 	struct rpc_err *errp;
449 {
450 	struct ct_data *ct;
451 
452 	_DIAGASSERT(h != NULL);
453 	_DIAGASSERT(errp != NULL);
454 
455 	ct = (struct ct_data *) h->cl_private;
456 	*errp = ct->ct_error;
457 }
458 
459 static bool_t
460 clnt_vc_freeres(cl, xdr_res, res_ptr)
461 	CLIENT *cl;
462 	xdrproc_t xdr_res;
463 	caddr_t res_ptr;
464 {
465 	struct ct_data *ct;
466 	XDR *xdrs;
467 	bool_t dummy;
468 #ifdef _REENTRANT
469 	sigset_t mask;
470 #endif
471 	sigset_t newmask;
472 
473 	_DIAGASSERT(cl != NULL);
474 
475 	ct = (struct ct_data *)cl->cl_private;
476 	xdrs = &(ct->ct_xdrs);
477 
478 	sigfillset(&newmask);
479 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
480 	mutex_lock(&clnt_fd_lock);
481 #ifdef _REENTRANT
482 	while (vc_fd_locks[ct->ct_fd])
483 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
484 #endif
485 
486 	xdrs->x_op = XDR_FREE;
487 	dummy = (*xdr_res)(xdrs, res_ptr);
488 	mutex_unlock(&clnt_fd_lock);
489 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
490 	cond_signal(&vc_cv[ct->ct_fd]);
491 
492 	return dummy;
493 }
494 
495 /*ARGSUSED*/
496 static void
497 clnt_vc_abort(cl)
498 	CLIENT *cl;
499 {
500 }
501 
502 static bool_t
503 clnt_vc_control(cl, request, info)
504 	CLIENT *cl;
505 	u_int request;
506 	char *info;
507 {
508 	struct ct_data *ct;
509 	void *infop = info;
510 #ifdef _REENTRANT
511 	sigset_t mask;
512 #endif
513 	sigset_t newmask;
514 
515 	_DIAGASSERT(cl != NULL);
516 
517 	ct = (struct ct_data *)cl->cl_private;
518 
519 	sigfillset(&newmask);
520 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
521 	mutex_lock(&clnt_fd_lock);
522 #ifdef _REENTRANT
523 	while (vc_fd_locks[ct->ct_fd])
524 		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
525 	vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
526 #endif
527 	mutex_unlock(&clnt_fd_lock);
528 
529 	switch (request) {
530 	case CLSET_FD_CLOSE:
531 		ct->ct_closeit = TRUE;
532 		release_fd_lock(ct->ct_fd, mask);
533 		return (TRUE);
534 	case CLSET_FD_NCLOSE:
535 		ct->ct_closeit = FALSE;
536 		release_fd_lock(ct->ct_fd, mask);
537 		return (TRUE);
538 	default:
539 		break;
540 	}
541 
542 	/* for other requests which use info */
543 	if (info == NULL) {
544 		release_fd_lock(ct->ct_fd, mask);
545 		return (FALSE);
546 	}
547 	switch (request) {
548 	case CLSET_TIMEOUT:
549 		if (time_not_ok((struct timeval *)(void *)info)) {
550 			release_fd_lock(ct->ct_fd, mask);
551 			return (FALSE);
552 		}
553 		ct->ct_wait = *(struct timeval *)infop;
554 		ct->ct_waitset = TRUE;
555 		break;
556 	case CLGET_TIMEOUT:
557 		*(struct timeval *)infop = ct->ct_wait;
558 		break;
559 	case CLGET_SERVER_ADDR:
560 		(void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
561 		break;
562 	case CLGET_FD:
563 		*(int *)(void *)info = ct->ct_fd;
564 		break;
565 	case CLGET_SVC_ADDR:
566 		/* The caller should not free this memory area */
567 		*(struct netbuf *)(void *)info = ct->ct_addr;
568 		break;
569 	case CLSET_SVC_ADDR:		/* set to new address */
570 		release_fd_lock(ct->ct_fd, mask);
571 		return (FALSE);
572 	case CLGET_XID:
573 		/*
574 		 * use the knowledge that xid is the
575 		 * first element in the call structure
576 		 * This will get the xid of the PREVIOUS call
577 		 */
578 		*(u_int32_t *)(void *)info =
579 		    ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
580 		break;
581 	case CLSET_XID:
582 		/* This will set the xid of the NEXT call */
583 		*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
584 		    htonl(*((u_int32_t *)(void *)info) + 1);
585 		/* increment by 1 as clnt_vc_call() decrements once */
586 		break;
587 	case CLGET_VERS:
588 		/*
589 		 * This RELIES on the information that, in the call body,
590 		 * the version number field is the fifth field from the
591 		 * begining of the RPC header. MUST be changed if the
592 		 * call_struct is changed
593 		 */
594 		*(u_int32_t *)(void *)info =
595 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
596 		    4 * BYTES_PER_XDR_UNIT));
597 		break;
598 
599 	case CLSET_VERS:
600 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
601 		    4 * BYTES_PER_XDR_UNIT) =
602 		    htonl(*(u_int32_t *)(void *)info);
603 		break;
604 
605 	case CLGET_PROG:
606 		/*
607 		 * This RELIES on the information that, in the call body,
608 		 * the program number field is the fourth field from the
609 		 * begining of the RPC header. MUST be changed if the
610 		 * call_struct is changed
611 		 */
612 		*(u_int32_t *)(void *)info =
613 		    ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
614 		    3 * BYTES_PER_XDR_UNIT));
615 		break;
616 
617 	case CLSET_PROG:
618 		*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
619 		    3 * BYTES_PER_XDR_UNIT) =
620 		    htonl(*(u_int32_t *)(void *)info);
621 		break;
622 
623 	default:
624 		release_fd_lock(ct->ct_fd, mask);
625 		return (FALSE);
626 	}
627 	release_fd_lock(ct->ct_fd, mask);
628 	return (TRUE);
629 }
630 
631 
632 static void
633 clnt_vc_destroy(cl)
634 	CLIENT *cl;
635 {
636 	struct ct_data *ct;
637 #ifdef _REENTRANT
638 	int ct_fd;
639 	sigset_t mask;
640 #endif
641 	sigset_t newmask;
642 
643 	_DIAGASSERT(cl != NULL);
644 
645 	ct = (struct ct_data *) cl->cl_private;
646 	ct_fd = ct->ct_fd;
647 
648 	sigfillset(&newmask);
649 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
650 	mutex_lock(&clnt_fd_lock);
651 #ifdef _REENTRANT
652 	while (vc_fd_locks[ct_fd])
653 		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
654 #endif
655 	if (ct->ct_closeit && ct->ct_fd != -1) {
656 		(void)close(ct->ct_fd);
657 	}
658 	XDR_DESTROY(&(ct->ct_xdrs));
659 	if (ct->ct_addr.buf)
660 		free(ct->ct_addr.buf);
661 	mem_free(ct, sizeof(struct ct_data));
662 	mem_free(cl, sizeof(CLIENT));
663 	mutex_unlock(&clnt_fd_lock);
664 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
665 
666 	cond_signal(&vc_cv[ct_fd]);
667 }
668 
669 /*
670  * Interface between xdr serializer and tcp connection.
671  * Behaves like the system calls, read & write, but keeps some error state
672  * around for the rpc level.
673  */
674 static int
675 read_vc(ctp, buf, len)
676 	caddr_t ctp;
677 	caddr_t buf;
678 	int len;
679 {
680 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
681 	struct pollfd fd;
682 	int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) +
683 	    (ct->ct_wait.tv_usec / 1000));
684 
685 	if (len == 0)
686 		return (0);
687 	fd.fd = ct->ct_fd;
688 	fd.events = POLLIN;
689 	for (;;) {
690 		switch (poll(&fd, 1, milliseconds)) {
691 		case 0:
692 			ct->ct_error.re_status = RPC_TIMEDOUT;
693 			return (-1);
694 
695 		case -1:
696 			if (errno == EINTR)
697 				continue;
698 			ct->ct_error.re_status = RPC_CANTRECV;
699 			ct->ct_error.re_errno = errno;
700 			return (-1);
701 		}
702 		break;
703 	}
704 	switch (len = read(ct->ct_fd, buf, (size_t)len)) {
705 
706 	case 0:
707 		/* premature eof */
708 		ct->ct_error.re_errno = ECONNRESET;
709 		ct->ct_error.re_status = RPC_CANTRECV;
710 		len = -1;  /* it's really an error */
711 		break;
712 
713 	case -1:
714 		ct->ct_error.re_errno = errno;
715 		ct->ct_error.re_status = RPC_CANTRECV;
716 		break;
717 	}
718 	return (len);
719 }
720 
721 static int
722 write_vc(ctp, buf, len)
723 	caddr_t ctp;
724 	caddr_t buf;
725 	int len;
726 {
727 	struct ct_data *ct = (struct ct_data *)(void *)ctp;
728 	int i, cnt;
729 
730 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
731 		if ((i = write(ct->ct_fd, buf, (size_t)cnt)) == -1) {
732 			ct->ct_error.re_errno = errno;
733 			ct->ct_error.re_status = RPC_CANTSEND;
734 			return (-1);
735 		}
736 	}
737 	return (len);
738 }
739 
740 static struct clnt_ops *
741 clnt_vc_ops()
742 {
743 	static struct clnt_ops ops;
744 #ifdef _REENTRANT
745 	extern mutex_t  ops_lock;
746 	sigset_t mask;
747 #endif
748 	sigset_t newmask;
749 
750 	/* VARIABLES PROTECTED BY ops_lock: ops */
751 
752 	sigfillset(&newmask);
753 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
754 	mutex_lock(&ops_lock);
755 	if (ops.cl_call == NULL) {
756 		ops.cl_call = clnt_vc_call;
757 		ops.cl_abort = clnt_vc_abort;
758 		ops.cl_geterr = clnt_vc_geterr;
759 		ops.cl_freeres = clnt_vc_freeres;
760 		ops.cl_destroy = clnt_vc_destroy;
761 		ops.cl_control = clnt_vc_control;
762 	}
763 	mutex_unlock(&ops_lock);
764 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
765 	return (&ops);
766 }
767 
768 /*
769  * Make sure that the time is not garbage.   -1 value is disallowed.
770  * Note this is different from time_not_ok in clnt_dg.c
771  */
772 static bool_t
773 time_not_ok(t)
774 	struct timeval *t;
775 {
776 
777 	_DIAGASSERT(t != NULL);
778 
779 	return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
780 		t->tv_usec <= -1 || t->tv_usec > 1000000);
781 }
782