clnt_vc.c revision 75097
11991Sheppo/* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */ 21991Sheppo/* $FreeBSD: head/lib/libc/rpc/clnt_vc.c 75097 2001-04-02 22:14:13Z iedowse $ */ 31991Sheppo 41991Sheppo/* 51991Sheppo * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 61991Sheppo * unrestricted use provided that this legend is included on all tape 71991Sheppo * media and as a part of the software program in whole or part. Users 81991Sheppo * may copy or modify Sun RPC without charge, but are not authorized 91991Sheppo * to license or distribute it to anyone else except as part of a product or 101991Sheppo * program developed by the user. 111991Sheppo * 121991Sheppo * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 131991Sheppo * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 141991Sheppo * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 151991Sheppo * 161991Sheppo * Sun RPC is provided with no support and without any obligation on the 171991Sheppo * part of Sun Microsystems, Inc. to assist in its use, correction, 181991Sheppo * modification or enhancement. 191991Sheppo * 201991Sheppo * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 211991Sheppo * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 221991Sheppo * OR ANY PART THEREOF. 237799SRichard.Bean@Sun.COM * 241991Sheppo * In no event will Sun Microsystems, Inc. be liable for any lost revenue 251991Sheppo * or profits or other special, indirect and consequential damages, even if 261991Sheppo * Sun has been advised of the possibility of such damages. 271991Sheppo * 281991Sheppo * Sun Microsystems, Inc. 291991Sheppo * 2550 Garcia Avenue 301991Sheppo * Mountain View, California 94043 311991Sheppo */ 321991Sheppo 331991Sheppo#include <sys/cdefs.h> 341991Sheppo#if defined(LIBC_SCCS) && !defined(lint) 351991Sheppostatic char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro"; 361991Sheppostatic char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC"; 371991Sheppostatic char sccsid[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro"; 381991Sheppo#endif 391991Sheppo 401991Sheppo/* 411991Sheppo * clnt_tcp.c, Implements a TCP/IP based, client side RPC. 421991Sheppo * 431991Sheppo * Copyright (C) 1984, Sun Microsystems, Inc. 441991Sheppo * 451991Sheppo * TCP based RPC supports 'batched calls'. 461991Sheppo * A sequence of calls may be batched-up in a send buffer. The rpc call 471991Sheppo * return immediately to the client even though the call was not necessarily 481991Sheppo * sent. The batching occurs if the results' xdr routine is NULL (0) AND 491991Sheppo * the rpc timeout value is zero (see clnt.h, rpc). 501991Sheppo * 511991Sheppo * Clients should NOT casually batch calls that in fact return results; that is, 521991Sheppo * the server side should be aware that a call is batched and not produce any 531991Sheppo * return message. Batched calls that produce many result messages can 541991Sheppo * deadlock (netlock) the client and the server.... 551991Sheppo * 561991Sheppo * Now go hang yourself. 571991Sheppo */ 581991Sheppo 591991Sheppo#include "namespace.h" 601991Sheppo#include "reentrant.h" 611991Sheppo#include <sys/types.h> 621991Sheppo#include <sys/poll.h> 631991Sheppo#include <sys/syslog.h> 641991Sheppo#include <sys/socket.h> 651991Sheppo#include <sys/un.h> 661991Sheppo#include <sys/uio.h> 671991Sheppo 681991Sheppo#include <assert.h> 691991Sheppo#include <err.h> 701991Sheppo#include <errno.h> 711991Sheppo#include <netdb.h> 721991Sheppo#include <stdio.h> 731991Sheppo#include <stdlib.h> 741991Sheppo#include <string.h> 751991Sheppo#include <unistd.h> 761991Sheppo#include <signal.h> 771991Sheppo 781991Sheppo#include <rpc/rpc.h> 791991Sheppo#include "un-namespace.h" 801991Sheppo#include "rpc_com.h" 811991Sheppo 821991Sheppo#define MCALL_MSG_SIZE 24 831991Sheppo 841991Sheppostruct cmessage { 851991Sheppo struct cmsghdr cmsg; 861991Sheppo struct cmsgcred cmcred; 871991Sheppo}; 881991Sheppo 891991Sheppostatic enum clnt_stat clnt_vc_call __P((CLIENT *, rpcproc_t, xdrproc_t, caddr_t, 901991Sheppo xdrproc_t, caddr_t, struct timeval)); 911991Sheppostatic void clnt_vc_geterr __P((CLIENT *, struct rpc_err *)); 921991Sheppostatic bool_t clnt_vc_freeres __P((CLIENT *, xdrproc_t, caddr_t)); 931991Sheppostatic void clnt_vc_abort __P((CLIENT *)); 941991Sheppostatic bool_t clnt_vc_control __P((CLIENT *, u_int, char *)); 951991Sheppostatic void clnt_vc_destroy __P((CLIENT *)); 961991Sheppostatic struct clnt_ops *clnt_vc_ops __P((void)); 977799SRichard.Bean@Sun.COMstatic bool_t time_not_ok __P((struct timeval *)); 981991Sheppostatic int read_vc __P((caddr_t, caddr_t, int)); 991991Sheppostatic int write_vc __P((caddr_t, caddr_t, int)); 1001991Sheppostatic int __msgwrite(int, void *, size_t); 1011991Sheppostatic int __msgread(int, void *, size_t); 1021991Sheppo 1031991Sheppostruct ct_data { 1041991Sheppo int ct_fd; /* connection's fd */ 1051991Sheppo bool_t ct_closeit; /* close it on destroy */ 1061991Sheppo struct timeval ct_wait; /* wait interval in milliseconds */ 1071991Sheppo bool_t ct_waitset; /* wait set by clnt_control? */ 1081991Sheppo struct netbuf ct_addr; /* remote addr */ 1091991Sheppo struct rpc_err ct_error; 1101991Sheppo union { 1111991Sheppo char ct_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */ 1121991Sheppo u_int32_t ct_mcalli; 1131991Sheppo } ct_u; 1141991Sheppo u_int ct_mpos; /* pos after marshal */ 1151991Sheppo XDR ct_xdrs; /* XDR stream */ 1161991Sheppo}; 1171991Sheppo 1181991Sheppo/* 1191991Sheppo * This machinery implements per-fd locks for MT-safety. It is not 1201991Sheppo * sufficient to do per-CLIENT handle locks for MT-safety because a 1211991Sheppo * user may create more than one CLIENT handle with the same fd behind 1221991Sheppo * it. Therfore, we allocate an array of flags (vc_fd_locks), protected 1231991Sheppo * by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables 1241991Sheppo * similarly protected. Vc_fd_lock[fd] == 1 => a call is activte on some 1251991Sheppo * CLIENT handle created for that fd. 1261991Sheppo * The current implementation holds locks across the entire RPC and reply. 1271991Sheppo * Yes, this is silly, and as soon as this code is proven to work, this 1281991Sheppo * should be the first thing fixed. One step at a time. 1291991Sheppo */ 1301991Sheppostatic int *vc_fd_locks; 1311991Sheppoextern mutex_t clnt_fd_lock; 1321991Sheppostatic cond_t *vc_cv; 1331991Sheppo#define release_fd_lock(fd, mask) { \ 1341991Sheppo mutex_lock(&clnt_fd_lock); \ 1351991Sheppo if (__isthreaded) \ 1361991Sheppo vc_fd_locks[fd] = 0; \ 1371991Sheppo mutex_unlock(&clnt_fd_lock); \ 1381991Sheppo thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL); \ 1391991Sheppo cond_signal(&vc_cv[fd]); \ 1401991Sheppo} 1411991Sheppo 1421991Sheppostatic const char clnt_vc_errstr[] = "%s : %s"; 1431991Sheppostatic const char clnt_vc_str[] = "clnt_vc_create"; 1441991Sheppostatic const char clnt_read_vc_str[] = "read_vc"; 1451991Sheppostatic const char __no_mem_str[] = "out of memory"; 1461991Sheppo 1471991Sheppo/* 1481991Sheppo * Create a client handle for a connection. 1491991Sheppo * Default options are set, which the user can change using clnt_control()'s. 1501991Sheppo * The rpc/vc package does buffering similar to stdio, so the client 1511991Sheppo * must pick send and receive buffer sizes, 0 => use the default. 1521991Sheppo * NB: fd is copied into a private area. 1531991Sheppo * NB: The rpch->cl_auth is set null authentication. Caller may wish to 1541991Sheppo * set this something more useful. 1551991Sheppo * 1561991Sheppo * fd should be an open socket 1571991Sheppo */ 1581991SheppoCLIENT * 1591991Sheppoclnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz) 1601991Sheppo int fd; /* open file descriptor */ 1611991Sheppo const struct netbuf *raddr; /* servers address */ 1621991Sheppo rpcprog_t prog; /* program number */ 1631991Sheppo rpcvers_t vers; /* version number */ 1641991Sheppo u_int sendsz; /* buffer recv size */ 1651991Sheppo u_int recvsz; /* buffer send size */ 1661991Sheppo{ 1671991Sheppo CLIENT *cl; /* client handle */ 1681991Sheppo struct ct_data *ct = NULL; /* client handle */ 1691991Sheppo struct timeval now; 1701991Sheppo struct rpc_msg call_msg; 1711991Sheppo static u_int32_t disrupt; 1721991Sheppo sigset_t mask; 1731991Sheppo sigset_t newmask; 1741991Sheppo struct sockaddr_storage ss; 1751991Sheppo socklen_t slen; 1761991Sheppo struct __rpc_sockinfo si; 1771991Sheppo 1781991Sheppo if (disrupt == 0) 1791991Sheppo disrupt = (u_int32_t)(long)raddr; 1801991Sheppo 1811991Sheppo cl = (CLIENT *)mem_alloc(sizeof (*cl)); 1821991Sheppo ct = (struct ct_data *)mem_alloc(sizeof (*ct)); 1831991Sheppo if ((cl == (CLIENT *)NULL) || (ct == (struct ct_data *)NULL)) { 1841991Sheppo (void) syslog(LOG_ERR, clnt_vc_errstr, 1851991Sheppo clnt_vc_str, __no_mem_str); 1861991Sheppo rpc_createerr.cf_stat = RPC_SYSTEMERROR; 1871991Sheppo rpc_createerr.cf_error.re_errno = errno; 1881991Sheppo goto err; 1891991Sheppo } 1901991Sheppo ct->ct_addr.buf = NULL; 1911991Sheppo sigfillset(&newmask); 1921991Sheppo thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 1931991Sheppo mutex_lock(&clnt_fd_lock); 1941991Sheppo if (vc_fd_locks == (int *) NULL) { 1951991Sheppo int cv_allocsz, fd_allocsz; 1961991Sheppo int dtbsize = __rpc_dtbsize(); 1971991Sheppo 1981991Sheppo fd_allocsz = dtbsize * sizeof (int); 1991991Sheppo vc_fd_locks = (int *) mem_alloc(fd_allocsz); 2001991Sheppo if (vc_fd_locks == (int *) NULL) { 2011991Sheppo mutex_unlock(&clnt_fd_lock); 2021991Sheppo thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 2031991Sheppo goto err; 2041991Sheppo } else 2051991Sheppo memset(vc_fd_locks, '\0', fd_allocsz); 2061991Sheppo 2071991Sheppo assert(vc_cv == (cond_t *) NULL); 2081991Sheppo cv_allocsz = dtbsize * sizeof (cond_t); 2091991Sheppo vc_cv = (cond_t *) mem_alloc(cv_allocsz); 2101991Sheppo if (vc_cv == (cond_t *) NULL) { 2111991Sheppo mem_free(vc_fd_locks, fd_allocsz); 2121991Sheppo vc_fd_locks = (int *) NULL; 2132336Snarayan mutex_unlock(&clnt_fd_lock); 2141991Sheppo thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 2151991Sheppo goto err; 2161991Sheppo } else { 2171991Sheppo int i; 2181991Sheppo 2191991Sheppo for (i = 0; i < dtbsize; i++) 2201991Sheppo cond_init(&vc_cv[i], 0, (void *) 0); 2211991Sheppo } 2221991Sheppo } else 2231991Sheppo assert(vc_cv != (cond_t *) NULL); 2241991Sheppo 2251991Sheppo /* 2261991Sheppo * XXX - fvdl connecting while holding a mutex? 2271991Sheppo */ 2281991Sheppo slen = sizeof ss; 2291991Sheppo if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { 2301991Sheppo if (errno != ENOTCONN) { 2311991Sheppo rpc_createerr.cf_stat = RPC_SYSTEMERROR; 2321991Sheppo rpc_createerr.cf_error.re_errno = errno; 2331991Sheppo mutex_unlock(&clnt_fd_lock); 2341991Sheppo thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 2351991Sheppo goto err; 2361991Sheppo } 2371991Sheppo if (_connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){ 2381991Sheppo rpc_createerr.cf_stat = RPC_SYSTEMERROR; 2391991Sheppo rpc_createerr.cf_error.re_errno = errno; 2401991Sheppo mutex_unlock(&clnt_fd_lock); 2411991Sheppo thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 2421991Sheppo goto err; 2431991Sheppo } 2441991Sheppo } 2451991Sheppo mutex_unlock(&clnt_fd_lock); 2461991Sheppo if (!__rpc_fd2sockinfo(fd, &si)) 2471991Sheppo goto err; 2481991Sheppo thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 2491991Sheppo 2501991Sheppo ct->ct_closeit = FALSE; 2511991Sheppo 2521991Sheppo /* 2531991Sheppo * Set up private data struct 2541991Sheppo */ 2551991Sheppo ct->ct_fd = fd; 2561991Sheppo ct->ct_wait.tv_usec = 0; 2574419Snevin ct->ct_waitset = FALSE; 2581991Sheppo ct->ct_addr.buf = malloc(raddr->maxlen); 2591991Sheppo if (ct->ct_addr.buf == NULL) 2601991Sheppo goto err; 2611991Sheppo memcpy(ct->ct_addr.buf, &raddr->buf, raddr->len); 2621991Sheppo ct->ct_addr.len = raddr->maxlen; 2631991Sheppo ct->ct_addr.maxlen = raddr->maxlen; 2644419Snevin 2654419Snevin /* 2661991Sheppo * Initialize call message 2671991Sheppo */ 2681991Sheppo (void)gettimeofday(&now, NULL); 2691991Sheppo call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now); 2701991Sheppo call_msg.rm_direction = CALL; 2711991Sheppo call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 2721991Sheppo call_msg.rm_call.cb_prog = (u_int32_t)prog; 2731991Sheppo call_msg.rm_call.cb_vers = (u_int32_t)vers; 2741991Sheppo 2751991Sheppo /* 2761991Sheppo * pre-serialize the static part of the call msg and stash it away 2771991Sheppo */ 2781991Sheppo xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE, 2791991Sheppo XDR_ENCODE); 2801991Sheppo if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) { 2811991Sheppo if (ct->ct_closeit) { 2821991Sheppo (void)_close(fd); 2831991Sheppo } 2841991Sheppo goto err; 2851991Sheppo } 2861991Sheppo ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs)); 2871991Sheppo XDR_DESTROY(&(ct->ct_xdrs)); 2881991Sheppo 2891991Sheppo /* 2901991Sheppo * Create a client handle which uses xdrrec for serialization 2911991Sheppo * and authnone for authentication. 2921991Sheppo */ 2931991Sheppo cl->cl_ops = clnt_vc_ops(); 2941991Sheppo cl->cl_private = ct; 2951991Sheppo cl->cl_auth = authnone_create(); 2961991Sheppo sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); 2971991Sheppo recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); 2981991Sheppo xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz, 2991991Sheppo cl->cl_private, read_vc, write_vc); 3001991Sheppo return (cl); 3011991Sheppo 3021991Sheppoerr: 3031991Sheppo if (cl) { 3041991Sheppo if (ct) { 3051991Sheppo if (ct->ct_addr.len) 3061991Sheppo mem_free(ct->ct_addr.buf, ct->ct_addr.len); 3071991Sheppo mem_free((caddr_t)ct, sizeof (struct ct_data)); 3081991Sheppo } 3091991Sheppo if (cl) 3101991Sheppo mem_free((caddr_t)cl, sizeof (CLIENT)); 3111991Sheppo } 3121991Sheppo return ((CLIENT *)NULL); 3131991Sheppo} 3141991Sheppo 3151991Sheppostatic enum clnt_stat 3161991Sheppoclnt_vc_call(cl, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout) 3171991Sheppo CLIENT *cl; 3181991Sheppo rpcproc_t proc; 3191991Sheppo xdrproc_t xdr_args; 3201991Sheppo caddr_t args_ptr; 3211991Sheppo xdrproc_t xdr_results; 3221991Sheppo caddr_t results_ptr; 3231991Sheppo struct timeval timeout; 3241991Sheppo{ 3251991Sheppo struct ct_data *ct = (struct ct_data *) cl->cl_private; 3261991Sheppo XDR *xdrs = &(ct->ct_xdrs); 3271991Sheppo struct rpc_msg reply_msg; 3281991Sheppo u_int32_t x_id; 3291991Sheppo u_int32_t *msg_x_id = &ct->ct_u.ct_mcalli; /* yuk */ 3301991Sheppo bool_t shipnow; 3311991Sheppo int refreshes = 2; 3321991Sheppo sigset_t mask, newmask; 3331991Sheppo int rpc_lock_value; 3341991Sheppo 3351991Sheppo assert(cl != NULL); 3361991Sheppo 3371991Sheppo sigfillset(&newmask); 3381991Sheppo thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 3391991Sheppo mutex_lock(&clnt_fd_lock); 3401991Sheppo while (vc_fd_locks[ct->ct_fd]) 3414419Snevin cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock); 3421991Sheppo if (__isthreaded) 3431991Sheppo rpc_lock_value = 1; 3441991Sheppo else 3451991Sheppo rpc_lock_value = 0; 3461991Sheppo vc_fd_locks[ct->ct_fd] = rpc_lock_value; 3471991Sheppo mutex_unlock(&clnt_fd_lock); 3481991Sheppo if (!ct->ct_waitset) { 3491991Sheppo /* If time is not within limits, we ignore it. */ 3501991Sheppo if (time_not_ok(&timeout) == FALSE) 3511991Sheppo ct->ct_wait = timeout; 3521991Sheppo } 3531991Sheppo 3541991Sheppo shipnow = 3551991Sheppo (xdr_results == NULL && timeout.tv_sec == 0 3561991Sheppo && timeout.tv_usec == 0) ? FALSE : TRUE; 3571991Sheppo 3581991Sheppocall_again: 3591991Sheppo xdrs->x_op = XDR_ENCODE; 3601991Sheppo ct->ct_error.re_status = RPC_SUCCESS; 3611991Sheppo x_id = ntohl(--(*msg_x_id)); 3621991Sheppo 3631991Sheppo if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) || 3641991Sheppo (! XDR_PUTINT32(xdrs, &proc)) || 3651991Sheppo (! AUTH_MARSHALL(cl->cl_auth, xdrs)) || 3661991Sheppo (! (*xdr_args)(xdrs, args_ptr))) { 3671991Sheppo if (ct->ct_error.re_status == RPC_SUCCESS) 3681991Sheppo ct->ct_error.re_status = RPC_CANTENCODEARGS; 3691991Sheppo (void)xdrrec_endofrecord(xdrs, TRUE); 3701991Sheppo release_fd_lock(ct->ct_fd, mask); 3711991Sheppo return (ct->ct_error.re_status); 3721991Sheppo } 3731991Sheppo if (! xdrrec_endofrecord(xdrs, shipnow)) { 3741991Sheppo release_fd_lock(ct->ct_fd, mask); 3751991Sheppo return (ct->ct_error.re_status = RPC_CANTSEND); 3761991Sheppo } 3771991Sheppo if (! shipnow) { 3781991Sheppo release_fd_lock(ct->ct_fd, mask); 3791991Sheppo return (RPC_SUCCESS); 3801991Sheppo } 3811991Sheppo /* 3821991Sheppo * Hack to provide rpc-based message passing 3831991Sheppo */ 3841991Sheppo if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { 3851991Sheppo release_fd_lock(ct->ct_fd, mask); 3861991Sheppo return(ct->ct_error.re_status = RPC_TIMEDOUT); 3871991Sheppo } 3881991Sheppo 3891991Sheppo 3901991Sheppo /* 3911991Sheppo * Keep receiving until we get a valid transaction id 3921991Sheppo */ 3931991Sheppo xdrs->x_op = XDR_DECODE; 3941991Sheppo while (TRUE) { 3951991Sheppo reply_msg.acpted_rply.ar_verf = _null_auth; 3961991Sheppo reply_msg.acpted_rply.ar_results.where = NULL; 3971991Sheppo reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; 3981991Sheppo if (! xdrrec_skiprecord(xdrs)) { 3991991Sheppo release_fd_lock(ct->ct_fd, mask); 4001991Sheppo return (ct->ct_error.re_status); 4011991Sheppo } 4021991Sheppo /* now decode and validate the response header */ 4031991Sheppo if (! xdr_replymsg(xdrs, &reply_msg)) { 4041991Sheppo if (ct->ct_error.re_status == RPC_SUCCESS) 4051991Sheppo continue; 4061991Sheppo release_fd_lock(ct->ct_fd, mask); 4071991Sheppo return (ct->ct_error.re_status); 4081991Sheppo } 4091991Sheppo if (reply_msg.rm_xid == x_id) 4101991Sheppo break; 4111991Sheppo } 4121991Sheppo 4131991Sheppo /* 4141991Sheppo * process header 4151991Sheppo */ 4161991Sheppo _seterr_reply(&reply_msg, &(ct->ct_error)); 4171991Sheppo if (ct->ct_error.re_status == RPC_SUCCESS) { 4181991Sheppo if (! AUTH_VALIDATE(cl->cl_auth, 4191991Sheppo &reply_msg.acpted_rply.ar_verf)) { 4201991Sheppo ct->ct_error.re_status = RPC_AUTHERROR; 4211991Sheppo ct->ct_error.re_why = AUTH_INVALIDRESP; 4221991Sheppo } else if (! (*xdr_results)(xdrs, results_ptr)) { 4231991Sheppo if (ct->ct_error.re_status == RPC_SUCCESS) 4241991Sheppo ct->ct_error.re_status = RPC_CANTDECODERES; 4251991Sheppo } 4261991Sheppo /* free verifier ... */ 4271991Sheppo if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { 4281991Sheppo xdrs->x_op = XDR_FREE; 4291991Sheppo (void)xdr_opaque_auth(xdrs, 4301991Sheppo &(reply_msg.acpted_rply.ar_verf)); 4311991Sheppo } 4321991Sheppo } /* end successful completion */ 4331991Sheppo else { 4341991Sheppo /* maybe our credentials need to be refreshed ... */ 4351991Sheppo if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg)) 4361991Sheppo goto call_again; 4371991Sheppo } /* end of unsuccessful completion */ 4381991Sheppo release_fd_lock(ct->ct_fd, mask); 4391991Sheppo return (ct->ct_error.re_status); 4401991Sheppo} 4411991Sheppo 4421991Sheppostatic void 4431991Sheppoclnt_vc_geterr(cl, errp) 4441991Sheppo CLIENT *cl; 4451991Sheppo struct rpc_err *errp; 4461991Sheppo{ 4471991Sheppo struct ct_data *ct; 4481991Sheppo 4491991Sheppo assert(cl != NULL); 4501991Sheppo 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 caddr_t 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 char *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 *)(void *)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 *)(void *)info = ct->ct_fd; 557 break; 558 case CLGET_SVC_ADDR: 559 /* The caller should not free this memory area */ 560 *(struct netbuf *)(void *)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 *)(void *)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 *)(void *)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 *)(void *)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 *)(void *)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 *)(void *)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 *)(void *)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 caddr_t ctp; 664 caddr_t buf; 665 int len; 666{ 667 struct sockaddr sa; 668 socklen_t sal; 669 struct ct_data *ct = (struct ct_data *)(void *)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 caddr_t ctp; 721 caddr_t buf; 722 int len; 723{ 724 struct sockaddr sa; 725 socklen_t sal; 726 struct ct_data *ct = (struct ct_data *)(void *)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 struct cmessage cm; 798 799 bzero((char *)&cm, sizeof(cm)); 800 iov[0].iov_base = buf; 801 iov[0].iov_len = cnt; 802 803 msg.msg_iov = iov; 804 msg.msg_iovlen = 1; 805 msg.msg_name = NULL; 806 msg.msg_namelen = 0; 807 msg.msg_control = (caddr_t)&cm; 808 msg.msg_controllen = sizeof(struct cmessage); 809 msg.msg_flags = 0; 810 811 return(_recvmsg(sock, &msg, 0)); 812} 813 814static int 815__msgwrite(sock, buf, cnt) 816 int sock; 817 void *buf; 818 size_t cnt; 819{ 820 struct iovec iov[1]; 821 struct msghdr msg; 822 struct cmessage cm; 823 824 bzero((char *)&cm, sizeof(cm)); 825 iov[0].iov_base = buf; 826 iov[0].iov_len = cnt; 827 828 cm.cmsg.cmsg_type = SCM_CREDS; 829 cm.cmsg.cmsg_level = SOL_SOCKET; 830 cm.cmsg.cmsg_len = sizeof(struct cmessage); 831 832 msg.msg_iov = iov; 833 msg.msg_iovlen = 1; 834 msg.msg_name = NULL; 835 msg.msg_namelen = 0; 836 msg.msg_control = (caddr_t)&cm; 837 msg.msg_controllen = sizeof(struct cmessage); 838 msg.msg_flags = 0; 839 840 return(_sendmsg(sock, &msg, 0)); 841} 842