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