clnt_dg.c revision 92990
174462Salfred/* $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $ */ 274462Salfred 374462Salfred/* 474462Salfred * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 574462Salfred * unrestricted use provided that this legend is included on all tape 674462Salfred * media and as a part of the software program in whole or part. Users 774462Salfred * may copy or modify Sun RPC without charge, but are not authorized 874462Salfred * to license or distribute it to anyone else except as part of a product or 974462Salfred * program developed by the user. 1074462Salfred * 1174462Salfred * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 1274462Salfred * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 1374462Salfred * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 1474462Salfred * 1574462Salfred * Sun RPC is provided with no support and without any obligation on the 1674462Salfred * part of Sun Microsystems, Inc. to assist in its use, correction, 1774462Salfred * modification or enhancement. 1874462Salfred * 1974462Salfred * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 2074462Salfred * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 2174462Salfred * OR ANY PART THEREOF. 2274462Salfred * 2374462Salfred * In no event will Sun Microsystems, Inc. be liable for any lost revenue 2474462Salfred * or profits or other special, indirect and consequential damages, even if 2574462Salfred * Sun has been advised of the possibility of such damages. 2674462Salfred * 2774462Salfred * Sun Microsystems, Inc. 2874462Salfred * 2550 Garcia Avenue 2974462Salfred * Mountain View, California 94043 3074462Salfred */ 3174462Salfred/* 3274462Salfred * Copyright (c) 1986-1991 by Sun Microsystems Inc. 3374462Salfred */ 3474462Salfred 3574462Salfred/* #ident "@(#)clnt_dg.c 1.23 94/04/22 SMI" */ 3674462Salfred 3774462Salfred#if !defined(lint) && defined(SCCSIDS) 3874462Salfredstatic char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro"; 3974462Salfred#endif 4092990Sobrien#include <sys/cdefs.h> 4192990Sobrien__FBSDID("$FreeBSD: head/lib/libc/rpc/clnt_dg.c 92990 2002-03-22 23:18:37Z obrien $"); 4274462Salfred 4374462Salfred/* 4474462Salfred * Implements a connectionless client side RPC. 4574462Salfred */ 4674462Salfred 4775094Siedowse#include "namespace.h" 4874462Salfred#include "reentrant.h" 4974462Salfred#include <sys/poll.h> 5074462Salfred#include <sys/types.h> 5174462Salfred#include <sys/time.h> 5274462Salfred#include <sys/socket.h> 5374462Salfred#include <sys/ioctl.h> 5490868Smike#include <arpa/inet.h> 5574462Salfred#include <rpc/rpc.h> 5674462Salfred#include <errno.h> 5774462Salfred#include <stdlib.h> 5874462Salfred#include <string.h> 5974462Salfred#include <signal.h> 6074462Salfred#include <unistd.h> 6174462Salfred#include <err.h> 6274462Salfred#include "un-namespace.h" 6374462Salfred#include "rpc_com.h" 6474462Salfred 6574462Salfred 6674462Salfred#define RPC_MAX_BACKOFF 30 /* seconds */ 6774462Salfred 6874462Salfred 6992905Sobrienstatic struct clnt_ops *clnt_dg_ops(void); 7092905Sobrienstatic bool_t time_not_ok(struct timeval *); 7192941Sobrienstatic enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, caddr_t, 7292941Sobrien xdrproc_t, caddr_t, struct timeval); 7392905Sobrienstatic void clnt_dg_geterr(CLIENT *, struct rpc_err *); 7492905Sobrienstatic bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, caddr_t); 7592905Sobrienstatic void clnt_dg_abort(CLIENT *); 7692905Sobrienstatic bool_t clnt_dg_control(CLIENT *, u_int, char *); 7792905Sobrienstatic void clnt_dg_destroy(CLIENT *); 7892905Sobrienstatic int __rpc_timeval_to_msec(struct timeval *); 7974462Salfred 8074462Salfred 8174462Salfred 8274462Salfred 8374462Salfred/* 8474462Salfred * This machinery implements per-fd locks for MT-safety. It is not 8574462Salfred * sufficient to do per-CLIENT handle locks for MT-safety because a 8674462Salfred * user may create more than one CLIENT handle with the same fd behind 8774462Salfred * it. Therfore, we allocate an array of flags (dg_fd_locks), protected 8874462Salfred * by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables 8974462Salfred * similarly protected. Dg_fd_lock[fd] == 1 => a call is activte on some 9074462Salfred * CLIENT handle created for that fd. 9174462Salfred * The current implementation holds locks across the entire RPC and reply, 9274462Salfred * including retransmissions. Yes, this is silly, and as soon as this 9374462Salfred * code is proven to work, this should be the first thing fixed. One step 9474462Salfred * at a time. 9574462Salfred */ 9674462Salfredstatic int *dg_fd_locks; 9774462Salfredextern mutex_t clnt_fd_lock; 9874462Salfredstatic cond_t *dg_cv; 9974462Salfred#define release_fd_lock(fd, mask) { \ 10074462Salfred mutex_lock(&clnt_fd_lock); \ 10175144Siedowse dg_fd_locks[fd] = 0; \ 10274462Salfred mutex_unlock(&clnt_fd_lock); \ 10374462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL); \ 10474462Salfred cond_signal(&dg_cv[fd]); \ 10574462Salfred} 10674462Salfred 10774462Salfredstatic const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory"; 10874462Salfred 10974462Salfred/* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */ 11074462Salfred 11174462Salfred/* 11274462Salfred * Private data kept per client handle 11374462Salfred */ 11474462Salfredstruct cu_data { 11574462Salfred int cu_fd; /* connections fd */ 11674462Salfred bool_t cu_closeit; /* opened by library */ 11774462Salfred struct sockaddr_storage cu_raddr; /* remote address */ 11874462Salfred int cu_rlen; 11974462Salfred struct timeval cu_wait; /* retransmit interval */ 12074462Salfred struct timeval cu_total; /* total time for the call */ 12174462Salfred struct rpc_err cu_error; 12274462Salfred XDR cu_outxdrs; 12374462Salfred u_int cu_xdrpos; 12474462Salfred u_int cu_sendsz; /* send size */ 12574462Salfred char *cu_outbuf; 12674462Salfred u_int cu_recvsz; /* recv size */ 12774462Salfred struct pollfd pfdp; 12874879Swpaul int cu_async; 12978678Siedowse int cu_connect; /* Use connect(). */ 13078678Siedowse int cu_connected; /* Have done connect(). */ 13174462Salfred char cu_inbuf[1]; 13274462Salfred}; 13374462Salfred 13474462Salfred/* 13574462Salfred * Connection less client creation returns with client handle parameters. 13674462Salfred * Default options are set, which the user can change using clnt_control(). 13774462Salfred * fd should be open and bound. 13874462Salfred * NB: The rpch->cl_auth is initialized to null authentication. 13974462Salfred * Caller may wish to set this something more useful. 14074462Salfred * 14174462Salfred * sendsz and recvsz are the maximum allowable packet sizes that can be 14274462Salfred * sent and received. Normally they are the same, but they can be 14374462Salfred * changed to improve the program efficiency and buffer allocation. 14474462Salfred * If they are 0, use the transport default. 14574462Salfred * 14674462Salfred * If svcaddr is NULL, returns NULL. 14774462Salfred */ 14874462SalfredCLIENT * 14974462Salfredclnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz) 15074462Salfred int fd; /* open file descriptor */ 15174462Salfred const struct netbuf *svcaddr; /* servers address */ 15274462Salfred rpcprog_t program; /* program number */ 15374462Salfred rpcvers_t version; /* version number */ 15474462Salfred u_int sendsz; /* buffer recv size */ 15574462Salfred u_int recvsz; /* buffer send size */ 15674462Salfred{ 15774462Salfred CLIENT *cl = NULL; /* client handle */ 15874462Salfred struct cu_data *cu = NULL; /* private data */ 15974462Salfred struct timeval now; 16074462Salfred struct rpc_msg call_msg; 16174462Salfred sigset_t mask; 16274462Salfred sigset_t newmask; 16374462Salfred struct __rpc_sockinfo si; 16474462Salfred int one = 1; 16574462Salfred 16674462Salfred sigfillset(&newmask); 16774462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 16874462Salfred mutex_lock(&clnt_fd_lock); 16974462Salfred if (dg_fd_locks == (int *) NULL) { 17074462Salfred int cv_allocsz; 17174462Salfred size_t fd_allocsz; 17274462Salfred int dtbsize = __rpc_dtbsize(); 17374462Salfred 17474462Salfred fd_allocsz = dtbsize * sizeof (int); 17574462Salfred dg_fd_locks = (int *) mem_alloc(fd_allocsz); 17674462Salfred if (dg_fd_locks == (int *) NULL) { 17774462Salfred mutex_unlock(&clnt_fd_lock); 17874462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 17974462Salfred goto err1; 18074462Salfred } else 18174462Salfred memset(dg_fd_locks, '\0', fd_allocsz); 18274462Salfred 18374462Salfred cv_allocsz = dtbsize * sizeof (cond_t); 18474462Salfred dg_cv = (cond_t *) mem_alloc(cv_allocsz); 18574462Salfred if (dg_cv == (cond_t *) NULL) { 18674462Salfred mem_free(dg_fd_locks, fd_allocsz); 18774462Salfred dg_fd_locks = (int *) NULL; 18874462Salfred mutex_unlock(&clnt_fd_lock); 18974462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 19074462Salfred goto err1; 19174462Salfred } else { 19274462Salfred int i; 19374462Salfred 19474462Salfred for (i = 0; i < dtbsize; i++) 19574462Salfred cond_init(&dg_cv[i], 0, (void *) 0); 19674462Salfred } 19774462Salfred } 19874462Salfred 19974462Salfred mutex_unlock(&clnt_fd_lock); 20074462Salfred thr_sigsetmask(SIG_SETMASK, &(mask), NULL); 20174462Salfred 20274462Salfred if (svcaddr == NULL) { 20374462Salfred rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 20474462Salfred return (NULL); 20574462Salfred } 20674462Salfred 20774462Salfred if (!__rpc_fd2sockinfo(fd, &si)) { 20874462Salfred rpc_createerr.cf_stat = RPC_TLIERROR; 20974462Salfred rpc_createerr.cf_error.re_errno = 0; 21074462Salfred return (NULL); 21174462Salfred } 21274462Salfred /* 21374462Salfred * Find the receive and the send size 21474462Salfred */ 21574462Salfred sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); 21674462Salfred recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); 21774462Salfred if ((sendsz == 0) || (recvsz == 0)) { 21874462Salfred rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */ 21974462Salfred rpc_createerr.cf_error.re_errno = 0; 22074462Salfred return (NULL); 22174462Salfred } 22274462Salfred 22374462Salfred if ((cl = mem_alloc(sizeof (CLIENT))) == NULL) 22474462Salfred goto err1; 22574462Salfred /* 22674462Salfred * Should be multiple of 4 for XDR. 22774462Salfred */ 22874462Salfred sendsz = ((sendsz + 3) / 4) * 4; 22974462Salfred recvsz = ((recvsz + 3) / 4) * 4; 23074462Salfred cu = mem_alloc(sizeof (*cu) + sendsz + recvsz); 23174462Salfred if (cu == NULL) 23274462Salfred goto err1; 23374462Salfred (void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len); 23474462Salfred cu->cu_rlen = svcaddr->len; 23574462Salfred cu->cu_outbuf = &cu->cu_inbuf[recvsz]; 23674462Salfred /* Other values can also be set through clnt_control() */ 23774462Salfred cu->cu_wait.tv_sec = 15; /* heuristically chosen */ 23874462Salfred cu->cu_wait.tv_usec = 0; 23974462Salfred cu->cu_total.tv_sec = -1; 24074462Salfred cu->cu_total.tv_usec = -1; 24174462Salfred cu->cu_sendsz = sendsz; 24274462Salfred cu->cu_recvsz = recvsz; 24374879Swpaul cu->cu_async = FALSE; 24478678Siedowse cu->cu_connect = FALSE; 24578678Siedowse cu->cu_connected = FALSE; 24674462Salfred (void) gettimeofday(&now, NULL); 24774462Salfred call_msg.rm_xid = __RPC_GETXID(&now); 24874462Salfred call_msg.rm_call.cb_prog = program; 24974462Salfred call_msg.rm_call.cb_vers = version; 25074462Salfred xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE); 25174462Salfred if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) { 25274462Salfred rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */ 25374462Salfred rpc_createerr.cf_error.re_errno = 0; 25474462Salfred goto err2; 25574462Salfred } 25674462Salfred cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs)); 25774462Salfred 25874462Salfred /* XXX fvdl - do we still want this? */ 25974462Salfred#if 0 26074462Salfred (void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf); 26174462Salfred#endif 26274462Salfred _ioctl(fd, FIONBIO, (char *)(void *)&one); 26374462Salfred 26474462Salfred /* 26574462Salfred * By default, closeit is always FALSE. It is users responsibility 26674462Salfred * to do a close on it, else the user may use clnt_control 26774462Salfred * to let clnt_destroy do it for him/her. 26874462Salfred */ 26974462Salfred cu->cu_closeit = FALSE; 27074462Salfred cu->cu_fd = fd; 27174462Salfred cl->cl_ops = clnt_dg_ops(); 27274462Salfred cl->cl_private = (caddr_t)(void *)cu; 27374462Salfred cl->cl_auth = authnone_create(); 27474462Salfred cl->cl_tp = NULL; 27574462Salfred cl->cl_netid = NULL; 27674462Salfred cu->pfdp.fd = cu->cu_fd; 27774462Salfred cu->pfdp.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND; 27874462Salfred return (cl); 27974462Salfrederr1: 28074462Salfred warnx(mem_err_clnt_dg); 28174462Salfred rpc_createerr.cf_stat = RPC_SYSTEMERROR; 28274462Salfred rpc_createerr.cf_error.re_errno = errno; 28374462Salfrederr2: 28474462Salfred if (cl) { 28574462Salfred mem_free(cl, sizeof (CLIENT)); 28674462Salfred if (cu) 28774462Salfred mem_free(cu, sizeof (*cu) + sendsz + recvsz); 28874462Salfred } 28974462Salfred return (NULL); 29074462Salfred} 29174462Salfred 29274462Salfredstatic enum clnt_stat 29374462Salfredclnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout) 29474462Salfred CLIENT *cl; /* client handle */ 29574462Salfred rpcproc_t proc; /* procedure number */ 29674462Salfred xdrproc_t xargs; /* xdr routine for args */ 29774462Salfred caddr_t argsp; /* pointer to args */ 29874462Salfred xdrproc_t xresults; /* xdr routine for results */ 29974462Salfred caddr_t resultsp; /* pointer to results */ 30074462Salfred struct timeval utimeout; /* seconds to wait before giving up */ 30174462Salfred{ 30274462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 30374462Salfred XDR *xdrs; 30474462Salfred size_t outlen; 30574462Salfred struct rpc_msg reply_msg; 30674462Salfred XDR reply_xdrs; 30774462Salfred struct timeval time_waited; 30874462Salfred bool_t ok; 30974462Salfred int nrefreshes = 2; /* number of times to refresh cred */ 31074462Salfred struct timeval timeout; 31174462Salfred struct timeval retransmit_time; 31274462Salfred struct timeval startime, curtime; 31374462Salfred int firsttimeout = 1; 31478678Siedowse struct sockaddr *sa; 31574462Salfred sigset_t mask; 31674462Salfred sigset_t newmask; 31778678Siedowse socklen_t inlen, salen; 31874462Salfred ssize_t recvlen = 0; 31974462Salfred int rpc_lock_value; 32074879Swpaul u_int32_t xid; 32174462Salfred 32290271Salfred outlen = 0; 32374462Salfred sigfillset(&newmask); 32474462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 32574462Salfred mutex_lock(&clnt_fd_lock); 32674462Salfred while (dg_fd_locks[cu->cu_fd]) 32774462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 32874462Salfred if (__isthreaded) 32974462Salfred rpc_lock_value = 1; 33074462Salfred else 33174462Salfred rpc_lock_value = 0; 33274462Salfred dg_fd_locks[cu->cu_fd] = rpc_lock_value; 33374462Salfred mutex_unlock(&clnt_fd_lock); 33474462Salfred if (cu->cu_total.tv_usec == -1) { 33574462Salfred timeout = utimeout; /* use supplied timeout */ 33674462Salfred } else { 33774462Salfred timeout = cu->cu_total; /* use default timeout */ 33874462Salfred } 33974462Salfred 34078678Siedowse if (cu->cu_connect && !cu->cu_connected) { 34178678Siedowse if (_connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr, 34278678Siedowse cu->cu_rlen) < 0) { 34378678Siedowse release_fd_lock(cu->cu_fd, mask); 34478678Siedowse cu->cu_error.re_errno = errno; 34578678Siedowse return (cu->cu_error.re_status = RPC_CANTSEND); 34678678Siedowse } 34778678Siedowse cu->cu_connected = 1; 34878678Siedowse } 34978678Siedowse if (cu->cu_connected) { 35078678Siedowse sa = NULL; 35178678Siedowse salen = 0; 35278678Siedowse } else { 35378678Siedowse sa = (struct sockaddr *)&cu->cu_raddr; 35478678Siedowse salen = cu->cu_rlen; 35578678Siedowse } 35674462Salfred time_waited.tv_sec = 0; 35774462Salfred time_waited.tv_usec = 0; 35874462Salfred retransmit_time = cu->cu_wait; 35974462Salfred 36074462Salfredcall_again: 36174462Salfred xdrs = &(cu->cu_outxdrs); 36274879Swpaul if (cu->cu_async == TRUE && xargs == NULL) 36374879Swpaul goto get_reply; 36474462Salfred xdrs->x_op = XDR_ENCODE; 36574462Salfred XDR_SETPOS(xdrs, cu->cu_xdrpos); 36674462Salfred /* 36774462Salfred * the transaction is the first thing in the out buffer 36874879Swpaul * XXX Yes, and it's in network byte order, so we should to 36974879Swpaul * be careful when we increment it, shouldn't we. 37074462Salfred */ 37174879Swpaul xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf)); 37274879Swpaul xid++; 37374879Swpaul *(u_int32_t *)(void *)(cu->cu_outbuf) = htonl(xid); 37474879Swpaul 37574462Salfred if ((! XDR_PUTINT32(xdrs, &proc)) || 37674462Salfred (! AUTH_MARSHALL(cl->cl_auth, xdrs)) || 37774462Salfred (! (*xargs)(xdrs, argsp))) { 37874462Salfred release_fd_lock(cu->cu_fd, mask); 37974462Salfred return (cu->cu_error.re_status = RPC_CANTENCODEARGS); 38074462Salfred } 38174462Salfred outlen = (size_t)XDR_GETPOS(xdrs); 38274462Salfred 38374462Salfredsend_again: 38478678Siedowse if (_sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) { 38574462Salfred cu->cu_error.re_errno = errno; 38674462Salfred release_fd_lock(cu->cu_fd, mask); 38774462Salfred return (cu->cu_error.re_status = RPC_CANTSEND); 38874462Salfred } 38974462Salfred 39074462Salfred /* 39174462Salfred * Hack to provide rpc-based message passing 39274462Salfred */ 39374462Salfred if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { 39474462Salfred release_fd_lock(cu->cu_fd, mask); 39574462Salfred return (cu->cu_error.re_status = RPC_TIMEDOUT); 39674462Salfred } 39774879Swpaul 39874879Swpaulget_reply: 39974879Swpaul 40074462Salfred /* 40174462Salfred * sub-optimal code appears here because we have 40274462Salfred * some clock time to spare while the packets are in flight. 40374462Salfred * (We assume that this is actually only executed once.) 40474462Salfred */ 40574462Salfred reply_msg.acpted_rply.ar_verf = _null_auth; 40674462Salfred reply_msg.acpted_rply.ar_results.where = resultsp; 40774462Salfred reply_msg.acpted_rply.ar_results.proc = xresults; 40874462Salfred 40974462Salfred 41074462Salfred for (;;) { 41174462Salfred switch (_poll(&cu->pfdp, 1, 41274462Salfred __rpc_timeval_to_msec(&retransmit_time))) { 41374462Salfred case 0: 41474462Salfred time_waited.tv_sec += retransmit_time.tv_sec; 41574462Salfred time_waited.tv_usec += retransmit_time.tv_usec; 41674462Salfred while (time_waited.tv_usec >= 1000000) { 41774462Salfred time_waited.tv_sec++; 41874462Salfred time_waited.tv_usec -= 1000000; 41974462Salfred } 42074462Salfred /* update retransmit_time */ 42174462Salfred if (retransmit_time.tv_sec < RPC_MAX_BACKOFF) { 42274462Salfred retransmit_time.tv_usec *= 2; 42374462Salfred retransmit_time.tv_sec *= 2; 42474462Salfred while (retransmit_time.tv_usec >= 1000000) { 42574462Salfred retransmit_time.tv_sec++; 42674462Salfred retransmit_time.tv_usec -= 1000000; 42774462Salfred } 42874462Salfred } 42974462Salfred 43074462Salfred if ((time_waited.tv_sec < timeout.tv_sec) || 43174462Salfred ((time_waited.tv_sec == timeout.tv_sec) && 43274462Salfred (time_waited.tv_usec < timeout.tv_usec))) 43374462Salfred goto send_again; 43474462Salfred release_fd_lock(cu->cu_fd, mask); 43574462Salfred return (cu->cu_error.re_status = RPC_TIMEDOUT); 43674462Salfred 43774462Salfred case -1: 43874462Salfred if (errno == EBADF) { 43974462Salfred cu->cu_error.re_errno = errno; 44074462Salfred release_fd_lock(cu->cu_fd, mask); 44174462Salfred return (cu->cu_error.re_status = RPC_CANTRECV); 44274462Salfred } 44374462Salfred if (errno != EINTR) { 44474462Salfred errno = 0; /* reset it */ 44574462Salfred continue; 44674462Salfred } 44774462Salfred /* interrupted by another signal, update time_waited */ 44874462Salfred if (firsttimeout) { 44974462Salfred /* 45074462Salfred * Could have done gettimeofday before clnt_call 45174462Salfred * but that means 1 more system call per each 45274462Salfred * clnt_call, so do it after first time out 45374462Salfred */ 45474462Salfred if (gettimeofday(&startime, 45574462Salfred (struct timezone *) NULL) == -1) { 45674462Salfred errno = 0; 45774462Salfred continue; 45874462Salfred } 45974462Salfred firsttimeout = 0; 46074462Salfred errno = 0; 46174462Salfred continue; 46274462Salfred }; 46374462Salfred if (gettimeofday(&curtime, 46474462Salfred (struct timezone *) NULL) == -1) { 46574462Salfred errno = 0; 46674462Salfred continue; 46774462Salfred }; 46874462Salfred time_waited.tv_sec += curtime.tv_sec - startime.tv_sec; 46974462Salfred time_waited.tv_usec += curtime.tv_usec - 47074462Salfred startime.tv_usec; 47174462Salfred while (time_waited.tv_usec < 0) { 47274462Salfred time_waited.tv_sec--; 47374462Salfred time_waited.tv_usec += 1000000; 47474462Salfred }; 47574462Salfred while (time_waited.tv_usec >= 1000000) { 47674462Salfred time_waited.tv_sec++; 47774462Salfred time_waited.tv_usec -= 1000000; 47874462Salfred } 47974462Salfred startime.tv_sec = curtime.tv_sec; 48074462Salfred startime.tv_usec = curtime.tv_usec; 48174462Salfred if ((time_waited.tv_sec > timeout.tv_sec) || 48274462Salfred ((time_waited.tv_sec == timeout.tv_sec) && 48374462Salfred (time_waited.tv_usec > timeout.tv_usec))) { 48474462Salfred release_fd_lock(cu->cu_fd, mask); 48574462Salfred return (cu->cu_error.re_status = RPC_TIMEDOUT); 48674462Salfred } 48774462Salfred errno = 0; /* reset it */ 48874462Salfred continue; 48974462Salfred }; 49074462Salfred 49174462Salfred if (cu->pfdp.revents & POLLNVAL || (cu->pfdp.revents == 0)) { 49274462Salfred cu->cu_error.re_status = RPC_CANTRECV; 49374462Salfred /* 49474462Salfred * Note: we're faking errno here because we 49574462Salfred * previously would have expected _poll() to 49674462Salfred * return -1 with errno EBADF. Poll(BA_OS) 49774462Salfred * returns 0 and sets the POLLNVAL revents flag 49874462Salfred * instead. 49974462Salfred */ 50074462Salfred cu->cu_error.re_errno = errno = EBADF; 50174462Salfred release_fd_lock(cu->cu_fd, mask); 50274462Salfred return (-1); 50374462Salfred } 50474462Salfred 50574462Salfred /* We have some data now */ 50674462Salfred do { 50774462Salfred if (errno == EINTR) { 50874462Salfred /* 50974462Salfred * Must make sure errno was not already 51074462Salfred * EINTR in case _recvfrom() returns -1. 51174462Salfred */ 51274462Salfred errno = 0; 51374462Salfred } 51474462Salfred recvlen = _recvfrom(cu->cu_fd, cu->cu_inbuf, 51576824Siedowse cu->cu_recvsz, 0, NULL, NULL); 51674462Salfred } while (recvlen < 0 && errno == EINTR); 51774462Salfred if (recvlen < 0) { 51874462Salfred if (errno == EWOULDBLOCK) 51974462Salfred continue; 52074462Salfred cu->cu_error.re_errno = errno; 52174462Salfred release_fd_lock(cu->cu_fd, mask); 52274462Salfred return (cu->cu_error.re_status = RPC_CANTRECV); 52374462Salfred } 52474462Salfred if (recvlen < sizeof (u_int32_t)) 52574462Salfred continue; 52674462Salfred /* see if reply transaction id matches sent id */ 52774879Swpaul if (cu->cu_async == FALSE && 52874879Swpaul *((u_int32_t *)(void *)(cu->cu_inbuf)) != 52974462Salfred *((u_int32_t *)(void *)(cu->cu_outbuf))) 53074462Salfred continue; 53174462Salfred /* we now assume we have the proper reply */ 53274462Salfred break; 53374462Salfred } 53474462Salfred inlen = (socklen_t)recvlen; 53574462Salfred 53674462Salfred /* 53774462Salfred * now decode and validate the response 53874462Salfred */ 53974462Salfred 54074462Salfred xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE); 54174462Salfred ok = xdr_replymsg(&reply_xdrs, &reply_msg); 54274462Salfred /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */ 54374462Salfred if (ok) { 54474462Salfred if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && 54574462Salfred (reply_msg.acpted_rply.ar_stat == SUCCESS)) 54674462Salfred cu->cu_error.re_status = RPC_SUCCESS; 54774462Salfred else 54874462Salfred _seterr_reply(&reply_msg, &(cu->cu_error)); 54974462Salfred 55074462Salfred if (cu->cu_error.re_status == RPC_SUCCESS) { 55174462Salfred if (! AUTH_VALIDATE(cl->cl_auth, 55274462Salfred &reply_msg.acpted_rply.ar_verf)) { 55374462Salfred cu->cu_error.re_status = RPC_AUTHERROR; 55474462Salfred cu->cu_error.re_why = AUTH_INVALIDRESP; 55574462Salfred } 55674462Salfred if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { 55774462Salfred xdrs->x_op = XDR_FREE; 55874462Salfred (void) xdr_opaque_auth(xdrs, 55974462Salfred &(reply_msg.acpted_rply.ar_verf)); 56074462Salfred } 56174462Salfred } /* end successful completion */ 56274462Salfred /* 56374462Salfred * If unsuccesful AND error is an authentication error 56474462Salfred * then refresh credentials and try again, else break 56574462Salfred */ 56674462Salfred else if (cu->cu_error.re_status == RPC_AUTHERROR) 56774462Salfred /* maybe our credentials need to be refreshed ... */ 56874462Salfred if (nrefreshes > 0 && 56974462Salfred AUTH_REFRESH(cl->cl_auth, &reply_msg)) { 57074462Salfred nrefreshes--; 57174462Salfred goto call_again; 57274462Salfred } 57374462Salfred /* end of unsuccessful completion */ 57474462Salfred } /* end of valid reply message */ 57574462Salfred else { 57674462Salfred cu->cu_error.re_status = RPC_CANTDECODERES; 57774462Salfred 57874462Salfred } 57974462Salfred release_fd_lock(cu->cu_fd, mask); 58074462Salfred return (cu->cu_error.re_status); 58174462Salfred} 58274462Salfred 58374462Salfredstatic void 58474462Salfredclnt_dg_geterr(cl, errp) 58574462Salfred CLIENT *cl; 58674462Salfred struct rpc_err *errp; 58774462Salfred{ 58874462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 58974462Salfred 59074462Salfred *errp = cu->cu_error; 59174462Salfred} 59274462Salfred 59374462Salfredstatic bool_t 59474462Salfredclnt_dg_freeres(cl, xdr_res, res_ptr) 59574462Salfred CLIENT *cl; 59674462Salfred xdrproc_t xdr_res; 59774462Salfred caddr_t res_ptr; 59874462Salfred{ 59974462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 60074462Salfred XDR *xdrs = &(cu->cu_outxdrs); 60174462Salfred bool_t dummy; 60274462Salfred sigset_t mask; 60374462Salfred sigset_t newmask; 60474462Salfred 60574462Salfred sigfillset(&newmask); 60674462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 60774462Salfred mutex_lock(&clnt_fd_lock); 60874462Salfred while (dg_fd_locks[cu->cu_fd]) 60974462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 61074462Salfred xdrs->x_op = XDR_FREE; 61174462Salfred dummy = (*xdr_res)(xdrs, res_ptr); 61274462Salfred mutex_unlock(&clnt_fd_lock); 61374462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 61474462Salfred cond_signal(&dg_cv[cu->cu_fd]); 61574462Salfred return (dummy); 61674462Salfred} 61774462Salfred 61874462Salfred/*ARGSUSED*/ 61974462Salfredstatic void 62074462Salfredclnt_dg_abort(h) 62174462Salfred CLIENT *h; 62274462Salfred{ 62374462Salfred} 62474462Salfred 62574462Salfredstatic bool_t 62674462Salfredclnt_dg_control(cl, request, info) 62774462Salfred CLIENT *cl; 62874462Salfred u_int request; 62974462Salfred char *info; 63074462Salfred{ 63174462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 63274462Salfred struct netbuf *addr; 63374462Salfred sigset_t mask; 63474462Salfred sigset_t newmask; 63574462Salfred int rpc_lock_value; 63674462Salfred 63774462Salfred sigfillset(&newmask); 63874462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 63974462Salfred mutex_lock(&clnt_fd_lock); 64074462Salfred while (dg_fd_locks[cu->cu_fd]) 64174462Salfred cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock); 64274462Salfred if (__isthreaded) 64374462Salfred rpc_lock_value = 1; 64474462Salfred else 64574462Salfred rpc_lock_value = 0; 64674462Salfred dg_fd_locks[cu->cu_fd] = rpc_lock_value; 64774462Salfred mutex_unlock(&clnt_fd_lock); 64874462Salfred switch (request) { 64974462Salfred case CLSET_FD_CLOSE: 65074462Salfred cu->cu_closeit = TRUE; 65174462Salfred release_fd_lock(cu->cu_fd, mask); 65274462Salfred return (TRUE); 65374462Salfred case CLSET_FD_NCLOSE: 65474462Salfred cu->cu_closeit = FALSE; 65574462Salfred release_fd_lock(cu->cu_fd, mask); 65674462Salfred return (TRUE); 65774462Salfred } 65874462Salfred 65974462Salfred /* for other requests which use info */ 66074462Salfred if (info == NULL) { 66174462Salfred release_fd_lock(cu->cu_fd, mask); 66274462Salfred return (FALSE); 66374462Salfred } 66474462Salfred switch (request) { 66574462Salfred case CLSET_TIMEOUT: 66674462Salfred if (time_not_ok((struct timeval *)(void *)info)) { 66774462Salfred release_fd_lock(cu->cu_fd, mask); 66874462Salfred return (FALSE); 66974462Salfred } 67074462Salfred cu->cu_total = *(struct timeval *)(void *)info; 67174462Salfred break; 67274462Salfred case CLGET_TIMEOUT: 67374462Salfred *(struct timeval *)(void *)info = cu->cu_total; 67474462Salfred break; 67574462Salfred case CLGET_SERVER_ADDR: /* Give him the fd address */ 67674462Salfred /* Now obsolete. Only for backward compatibility */ 67774462Salfred (void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen); 67874462Salfred break; 67974462Salfred case CLSET_RETRY_TIMEOUT: 68074462Salfred if (time_not_ok((struct timeval *)(void *)info)) { 68174462Salfred release_fd_lock(cu->cu_fd, mask); 68274462Salfred return (FALSE); 68374462Salfred } 68474462Salfred cu->cu_wait = *(struct timeval *)(void *)info; 68574462Salfred break; 68674462Salfred case CLGET_RETRY_TIMEOUT: 68774462Salfred *(struct timeval *)(void *)info = cu->cu_wait; 68874462Salfred break; 68974462Salfred case CLGET_FD: 69074462Salfred *(int *)(void *)info = cu->cu_fd; 69174462Salfred break; 69274462Salfred case CLGET_SVC_ADDR: 69374462Salfred addr = (struct netbuf *)(void *)info; 69474462Salfred addr->buf = &cu->cu_raddr; 69574462Salfred addr->len = cu->cu_rlen; 69674462Salfred addr->maxlen = sizeof cu->cu_raddr; 69774462Salfred break; 69874462Salfred case CLSET_SVC_ADDR: /* set to new address */ 69974462Salfred addr = (struct netbuf *)(void *)info; 70075097Siedowse if (addr->len < sizeof cu->cu_raddr) { 70175097Siedowse release_fd_lock(cu->cu_fd, mask); 70274462Salfred return (FALSE); 70375097Siedowse } 70474462Salfred (void) memcpy(&cu->cu_raddr, addr->buf, addr->len); 70574462Salfred cu->cu_rlen = addr->len; 70674462Salfred break; 70774462Salfred case CLGET_XID: 70874462Salfred /* 70974462Salfred * use the knowledge that xid is the 71074462Salfred * first element in the call structure *. 71174462Salfred * This will get the xid of the PREVIOUS call 71274462Salfred */ 71374462Salfred *(u_int32_t *)(void *)info = 71474462Salfred ntohl(*(u_int32_t *)(void *)cu->cu_outbuf); 71574462Salfred break; 71674462Salfred 71774462Salfred case CLSET_XID: 71874462Salfred /* This will set the xid of the NEXT call */ 71974462Salfred *(u_int32_t *)(void *)cu->cu_outbuf = 72074462Salfred htonl(*(u_int32_t *)(void *)info - 1); 72174462Salfred /* decrement by 1 as clnt_dg_call() increments once */ 72274462Salfred break; 72374462Salfred 72474462Salfred case CLGET_VERS: 72574462Salfred /* 72674462Salfred * This RELIES on the information that, in the call body, 72774462Salfred * the version number field is the fifth field from the 72874462Salfred * begining of the RPC header. MUST be changed if the 72974462Salfred * call_struct is changed 73074462Salfred */ 73174462Salfred *(u_int32_t *)(void *)info = 73274462Salfred ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf + 73374462Salfred 4 * BYTES_PER_XDR_UNIT)); 73474462Salfred break; 73574462Salfred 73674462Salfred case CLSET_VERS: 73774462Salfred *(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT) 73874462Salfred = htonl(*(u_int32_t *)(void *)info); 73974462Salfred break; 74074462Salfred 74174462Salfred case CLGET_PROG: 74274462Salfred /* 74374462Salfred * This RELIES on the information that, in the call body, 74474462Salfred * the program number field is the fourth field from the 74574462Salfred * begining of the RPC header. MUST be changed if the 74674462Salfred * call_struct is changed 74774462Salfred */ 74874462Salfred *(u_int32_t *)(void *)info = 74974462Salfred ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf + 75074462Salfred 3 * BYTES_PER_XDR_UNIT)); 75174462Salfred break; 75274462Salfred 75374462Salfred case CLSET_PROG: 75474462Salfred *(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT) 75574462Salfred = htonl(*(u_int32_t *)(void *)info); 75674462Salfred break; 75774879Swpaul case CLSET_ASYNC: 75874879Swpaul cu->cu_async = *(int *)(void *)info; 75974879Swpaul break; 76078678Siedowse case CLSET_CONNECT: 76178678Siedowse cu->cu_connect = *(int *)(void *)info; 76278678Siedowse break; 76374462Salfred default: 76474462Salfred release_fd_lock(cu->cu_fd, mask); 76574462Salfred return (FALSE); 76674462Salfred } 76774462Salfred release_fd_lock(cu->cu_fd, mask); 76874462Salfred return (TRUE); 76974462Salfred} 77074462Salfred 77174462Salfredstatic void 77274462Salfredclnt_dg_destroy(cl) 77374462Salfred CLIENT *cl; 77474462Salfred{ 77574462Salfred struct cu_data *cu = (struct cu_data *)cl->cl_private; 77674462Salfred int cu_fd = cu->cu_fd; 77774462Salfred sigset_t mask; 77874462Salfred sigset_t newmask; 77974462Salfred 78074462Salfred sigfillset(&newmask); 78174462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 78274462Salfred mutex_lock(&clnt_fd_lock); 78374462Salfred while (dg_fd_locks[cu_fd]) 78474462Salfred cond_wait(&dg_cv[cu_fd], &clnt_fd_lock); 78574462Salfred if (cu->cu_closeit) 78674462Salfred (void)_close(cu_fd); 78774462Salfred XDR_DESTROY(&(cu->cu_outxdrs)); 78874462Salfred mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz)); 78974462Salfred if (cl->cl_netid && cl->cl_netid[0]) 79074462Salfred mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); 79174462Salfred if (cl->cl_tp && cl->cl_tp[0]) 79274462Salfred mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); 79374462Salfred mem_free(cl, sizeof (CLIENT)); 79474462Salfred mutex_unlock(&clnt_fd_lock); 79574462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 79674462Salfred cond_signal(&dg_cv[cu_fd]); 79774462Salfred} 79874462Salfred 79974462Salfredstatic struct clnt_ops * 80074462Salfredclnt_dg_ops() 80174462Salfred{ 80274462Salfred static struct clnt_ops ops; 80374462Salfred extern mutex_t ops_lock; 80474462Salfred sigset_t mask; 80574462Salfred sigset_t newmask; 80674462Salfred 80774462Salfred/* VARIABLES PROTECTED BY ops_lock: ops */ 80874462Salfred 80974462Salfred sigfillset(&newmask); 81074462Salfred thr_sigsetmask(SIG_SETMASK, &newmask, &mask); 81174462Salfred mutex_lock(&ops_lock); 81274462Salfred if (ops.cl_call == NULL) { 81374462Salfred ops.cl_call = clnt_dg_call; 81474462Salfred ops.cl_abort = clnt_dg_abort; 81574462Salfred ops.cl_geterr = clnt_dg_geterr; 81674462Salfred ops.cl_freeres = clnt_dg_freeres; 81774462Salfred ops.cl_destroy = clnt_dg_destroy; 81874462Salfred ops.cl_control = clnt_dg_control; 81974462Salfred } 82074462Salfred mutex_unlock(&ops_lock); 82174462Salfred thr_sigsetmask(SIG_SETMASK, &mask, NULL); 82274462Salfred return (&ops); 82374462Salfred} 82474462Salfred 82574462Salfred/* 82674462Salfred * Make sure that the time is not garbage. -1 value is allowed. 82774462Salfred */ 82874462Salfredstatic bool_t 82974462Salfredtime_not_ok(t) 83074462Salfred struct timeval *t; 83174462Salfred{ 83274462Salfred return (t->tv_sec < -1 || t->tv_sec > 100000000 || 83374462Salfred t->tv_usec < -1 || t->tv_usec > 1000000); 83474462Salfred} 83574462Salfred 83674462Salfred 83774462Salfred/* 83874462Salfred * Convert from timevals (used by select) to milliseconds (used by poll). 83974462Salfred */ 84074462Salfredstatic int 84174462Salfred__rpc_timeval_to_msec(t) 84274462Salfred struct timeval *t; 84374462Salfred{ 84474462Salfred int t1, tmp; 84574462Salfred 84674462Salfred /* 84774462Salfred * We're really returning t->tv_sec * 1000 + (t->tv_usec / 1000) 84874462Salfred * but try to do so efficiently. Note: 1000 = 1024 - 16 - 8. 84974462Salfred */ 85074462Salfred tmp = (int)t->tv_sec << 3; 85174462Salfred t1 = -tmp; 85274462Salfred t1 += t1 << 1; 85374462Salfred t1 += tmp << 7; 85474462Salfred if (t->tv_usec) 85574462Salfred t1 += (int)(t->tv_usec / 1000); 85674462Salfred 85774462Salfred return (t1); 85874462Salfred} 859