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