Debian Codegraph @

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