1ba96d07fShrs /*-
2ba96d07fShrs * Copyright (c) 2009, Sun Microsystems, Inc.
3ba96d07fShrs * All rights reserved.
4ce0e08e2SPeter Avalos *
5ba96d07fShrs * Redistribution and use in source and binary forms, with or without
6ba96d07fShrs * modification, are permitted provided that the following conditions are met:
7ba96d07fShrs * - Redistributions of source code must retain the above copyright notice,
8ba96d07fShrs * this list of conditions and the following disclaimer.
9ba96d07fShrs * - Redistributions in binary form must reproduce the above copyright notice,
10ba96d07fShrs * this list of conditions and the following disclaimer in the documentation
11ba96d07fShrs * and/or other materials provided with the distribution.
12ba96d07fShrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
13ba96d07fShrs * contributors may be used to endorse or promote products derived
14ba96d07fShrs * from this software without specific prior written permission.
15ce0e08e2SPeter Avalos *
16ba96d07fShrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17ba96d07fShrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ba96d07fShrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ba96d07fShrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20ba96d07fShrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21ba96d07fShrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22ba96d07fShrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23ba96d07fShrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24ba96d07fShrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25ba96d07fShrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26ba96d07fShrs * POSSIBILITY OF SUCH DAMAGE.
27ce0e08e2SPeter Avalos *
28ce0e08e2SPeter Avalos * @(#)clnt_dg.c 1.23 94/04/22 SMI; 1.19 89/03/16 Copyr 1988 Sun Micro
29ce0e08e2SPeter Avalos * $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $
30ce0e08e2SPeter Avalos * $FreeBSD: src/lib/libc/rpc/clnt_dg.c,v 1.18 2006/02/27 22:10:58 deischen Exp $
31ce0e08e2SPeter Avalos */
32ce0e08e2SPeter Avalos /*
33ce0e08e2SPeter Avalos * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34ce0e08e2SPeter Avalos */
35ce0e08e2SPeter Avalos
36ce0e08e2SPeter Avalos /*
37ce0e08e2SPeter Avalos * Implements a connectionless client side RPC.
38ce0e08e2SPeter Avalos */
39ce0e08e2SPeter Avalos
40ce0e08e2SPeter Avalos #include "namespace.h"
41ce0e08e2SPeter Avalos #include "reentrant.h"
42*ed183f8cSSascha Wildner #include <sys/param.h>
43ce0e08e2SPeter Avalos #include <sys/event.h>
44ce0e08e2SPeter Avalos #include <sys/time.h>
45ce0e08e2SPeter Avalos #include <sys/socket.h>
46ce0e08e2SPeter Avalos #include <sys/ioctl.h>
47ce0e08e2SPeter Avalos #include <arpa/inet.h>
48ce0e08e2SPeter Avalos #include <rpc/rpc.h>
49ce0e08e2SPeter Avalos #include <errno.h>
50ce0e08e2SPeter Avalos #include <stdlib.h>
51ce0e08e2SPeter Avalos #include <string.h>
52ce0e08e2SPeter Avalos #include <signal.h>
53ce0e08e2SPeter Avalos #include <unistd.h>
54ce0e08e2SPeter Avalos #include <err.h>
55ce0e08e2SPeter Avalos #include "un-namespace.h"
56ce0e08e2SPeter Avalos #include "rpc_com.h"
57ce0e08e2SPeter Avalos #include "mt_misc.h"
58ce0e08e2SPeter Avalos
59ce0e08e2SPeter Avalos
60ce0e08e2SPeter Avalos #define RPC_MAX_BACKOFF 30 /* seconds */
61ce0e08e2SPeter Avalos
62ce0e08e2SPeter Avalos
63ce0e08e2SPeter Avalos static void clnt_dg_abort(CLIENT *);
64ce0e08e2SPeter Avalos static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
65ce0e08e2SPeter Avalos xdrproc_t, void *, struct timeval);
66ce0e08e2SPeter Avalos static bool_t clnt_dg_control(CLIENT *, u_int, void *);
67ce0e08e2SPeter Avalos static void clnt_dg_destroy(CLIENT *);
68ce0e08e2SPeter Avalos static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
69ce0e08e2SPeter Avalos static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
70ce0e08e2SPeter Avalos static struct clnt_ops *clnt_dg_ops(void);
71ce0e08e2SPeter Avalos static bool_t time_not_ok(struct timeval *);
72ce0e08e2SPeter Avalos
73ce0e08e2SPeter Avalos
74ce0e08e2SPeter Avalos /*
75ce0e08e2SPeter Avalos * This machinery implements per-fd locks for MT-safety. It is not
76ce0e08e2SPeter Avalos * sufficient to do per-CLIENT handle locks for MT-safety because a
77ce0e08e2SPeter Avalos * user may create more than one CLIENT handle with the same fd behind
78ce0e08e2SPeter Avalos * it. Therfore, we allocate an array of flags (dg_fd_locks), protected
79ce0e08e2SPeter Avalos * by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables
80ce0e08e2SPeter Avalos * similarly protected. Dg_fd_lock[fd] == 1 => a call is activte on some
81ce0e08e2SPeter Avalos * CLIENT handle created for that fd.
82ce0e08e2SPeter Avalos * The current implementation holds locks across the entire RPC and reply,
83ce0e08e2SPeter Avalos * including retransmissions. Yes, this is silly, and as soon as this
84ce0e08e2SPeter Avalos * code is proven to work, this should be the first thing fixed. One step
85ce0e08e2SPeter Avalos * at a time.
86ce0e08e2SPeter Avalos */
87ce0e08e2SPeter Avalos static int *dg_fd_locks;
88ce0e08e2SPeter Avalos static cond_t *dg_cv;
89ce0e08e2SPeter Avalos #define release_fd_lock(fd, mask) { \
90ce0e08e2SPeter Avalos mutex_lock(&clnt_fd_lock); \
91ce0e08e2SPeter Avalos dg_fd_locks[fd] = 0; \
92ce0e08e2SPeter Avalos mutex_unlock(&clnt_fd_lock); \
93ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \
94ce0e08e2SPeter Avalos cond_signal(&dg_cv[fd]); \
95ce0e08e2SPeter Avalos }
96ce0e08e2SPeter Avalos
97ce0e08e2SPeter Avalos static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
98ce0e08e2SPeter Avalos
99ce0e08e2SPeter Avalos /* VARIABLES PROTECTED BY clnt_fd_lock: dg_fd_locks, dg_cv */
100ce0e08e2SPeter Avalos
101ce0e08e2SPeter Avalos /*
102ce0e08e2SPeter Avalos * Private data kept per client handle
103ce0e08e2SPeter Avalos */
104ce0e08e2SPeter Avalos struct cu_data {
105ce0e08e2SPeter Avalos int cu_fd; /* connections fd */
106ce0e08e2SPeter Avalos bool_t cu_closeit; /* opened by library */
107ce0e08e2SPeter Avalos struct sockaddr_storage cu_raddr; /* remote address */
108ce0e08e2SPeter Avalos int cu_rlen;
109ce0e08e2SPeter Avalos struct timeval cu_wait; /* retransmit interval */
110ce0e08e2SPeter Avalos struct timeval cu_total; /* total time for the call */
111ce0e08e2SPeter Avalos struct rpc_err cu_error;
112ce0e08e2SPeter Avalos XDR cu_outxdrs;
113ce0e08e2SPeter Avalos u_int cu_xdrpos;
114ce0e08e2SPeter Avalos u_int cu_sendsz; /* send size */
115ce0e08e2SPeter Avalos char *cu_outbuf;
116ce0e08e2SPeter Avalos u_int cu_recvsz; /* recv size */
117ce0e08e2SPeter Avalos int cu_async;
118ce0e08e2SPeter Avalos int cu_connect; /* Use connect(). */
119ce0e08e2SPeter Avalos int cu_connected; /* Have done connect(). */
120ce0e08e2SPeter Avalos struct kevent cu_kin;
121ce0e08e2SPeter Avalos int cu_kq;
122ce0e08e2SPeter Avalos char cu_inbuf[1];
123ce0e08e2SPeter Avalos };
124ce0e08e2SPeter Avalos
125ce0e08e2SPeter Avalos /*
126ce0e08e2SPeter Avalos * Connection less client creation returns with client handle parameters.
127ce0e08e2SPeter Avalos * Default options are set, which the user can change using clnt_control().
128ce0e08e2SPeter Avalos * fd should be open and bound.
129ce0e08e2SPeter Avalos * NB: The rpch->cl_auth is initialized to null authentication.
130ce0e08e2SPeter Avalos * Caller may wish to set this something more useful.
131ce0e08e2SPeter Avalos *
132ce0e08e2SPeter Avalos * sendsz and recvsz are the maximum allowable packet sizes that can be
133ce0e08e2SPeter Avalos * sent and received. Normally they are the same, but they can be
134ce0e08e2SPeter Avalos * changed to improve the program efficiency and buffer allocation.
135ce0e08e2SPeter Avalos * If they are 0, use the transport default.
136ce0e08e2SPeter Avalos *
137ce0e08e2SPeter Avalos * If svcaddr is NULL, returns NULL.
138ce0e08e2SPeter Avalos */
139ce0e08e2SPeter Avalos CLIENT *
clnt_dg_create(int fd,const struct netbuf * svcaddr,rpcprog_t program,rpcvers_t version,u_int sendsz,u_int recvsz)140ce0e08e2SPeter Avalos clnt_dg_create(int fd, /* open file descriptor */
141ce0e08e2SPeter Avalos const struct netbuf *svcaddr, /* servers address */
142ce0e08e2SPeter Avalos rpcprog_t program, /* program number */
143ce0e08e2SPeter Avalos rpcvers_t version, /* version number */
144ce0e08e2SPeter Avalos u_int sendsz, /* buffer recv size */
145ce0e08e2SPeter Avalos u_int recvsz) /* buffer send size */
146ce0e08e2SPeter Avalos {
147ce0e08e2SPeter Avalos CLIENT *cl = NULL; /* client handle */
148ce0e08e2SPeter Avalos struct cu_data *cu = NULL; /* private data */
149ce0e08e2SPeter Avalos struct timeval now;
150ce0e08e2SPeter Avalos struct rpc_msg call_msg;
151ce0e08e2SPeter Avalos sigset_t mask;
152ce0e08e2SPeter Avalos sigset_t newmask;
153ce0e08e2SPeter Avalos struct __rpc_sockinfo si;
154ce0e08e2SPeter Avalos int one = 1;
155ce0e08e2SPeter Avalos
156ce0e08e2SPeter Avalos sigfillset(&newmask);
157ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
158ce0e08e2SPeter Avalos mutex_lock(&clnt_fd_lock);
1592038fb68SSascha Wildner if (dg_fd_locks == NULL) {
160ce0e08e2SPeter Avalos int cv_allocsz;
161ce0e08e2SPeter Avalos size_t fd_allocsz;
162ce0e08e2SPeter Avalos int dtbsize = __rpc_dtbsize();
163ce0e08e2SPeter Avalos
164ce0e08e2SPeter Avalos fd_allocsz = dtbsize * sizeof (int);
165ce0e08e2SPeter Avalos dg_fd_locks = (int *) mem_alloc(fd_allocsz);
1662038fb68SSascha Wildner if (dg_fd_locks == NULL) {
167ce0e08e2SPeter Avalos mutex_unlock(&clnt_fd_lock);
168ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
169ce0e08e2SPeter Avalos goto err1;
170ce0e08e2SPeter Avalos } else
171ce0e08e2SPeter Avalos memset(dg_fd_locks, '\0', fd_allocsz);
172ce0e08e2SPeter Avalos
173ce0e08e2SPeter Avalos cv_allocsz = dtbsize * sizeof (cond_t);
174ce0e08e2SPeter Avalos dg_cv = (cond_t *) mem_alloc(cv_allocsz);
1752038fb68SSascha Wildner if (dg_cv == NULL) {
176ce0e08e2SPeter Avalos mem_free(dg_fd_locks, fd_allocsz);
1772038fb68SSascha Wildner dg_fd_locks = NULL;
178ce0e08e2SPeter Avalos mutex_unlock(&clnt_fd_lock);
179ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
180ce0e08e2SPeter Avalos goto err1;
181ce0e08e2SPeter Avalos } else {
182ce0e08e2SPeter Avalos int i;
183ce0e08e2SPeter Avalos
184ce0e08e2SPeter Avalos for (i = 0; i < dtbsize; i++)
185902ec341SSascha Wildner cond_init(&dg_cv[i], 0, NULL);
186ce0e08e2SPeter Avalos }
187ce0e08e2SPeter Avalos }
188ce0e08e2SPeter Avalos
189ce0e08e2SPeter Avalos mutex_unlock(&clnt_fd_lock);
190ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
191ce0e08e2SPeter Avalos
192ce0e08e2SPeter Avalos if (svcaddr == NULL) {
193ce0e08e2SPeter Avalos rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
194ce0e08e2SPeter Avalos return (NULL);
195ce0e08e2SPeter Avalos }
196ce0e08e2SPeter Avalos
197ce0e08e2SPeter Avalos if (!__rpc_fd2sockinfo(fd, &si)) {
198ce0e08e2SPeter Avalos rpc_createerr.cf_stat = RPC_TLIERROR;
199ce0e08e2SPeter Avalos rpc_createerr.cf_error.re_errno = 0;
200ce0e08e2SPeter Avalos return (NULL);
201ce0e08e2SPeter Avalos }
202ce0e08e2SPeter Avalos /*
203ce0e08e2SPeter Avalos * Find the receive and the send size
204ce0e08e2SPeter Avalos */
205ce0e08e2SPeter Avalos sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
206ce0e08e2SPeter Avalos recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
207ce0e08e2SPeter Avalos if ((sendsz == 0) || (recvsz == 0)) {
208ce0e08e2SPeter Avalos rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
209ce0e08e2SPeter Avalos rpc_createerr.cf_error.re_errno = 0;
210ce0e08e2SPeter Avalos return (NULL);
211ce0e08e2SPeter Avalos }
212ce0e08e2SPeter Avalos
213ce0e08e2SPeter Avalos if ((cl = mem_alloc(sizeof (CLIENT))) == NULL)
214ce0e08e2SPeter Avalos goto err1;
215ce0e08e2SPeter Avalos /*
216ce0e08e2SPeter Avalos * Should be multiple of 4 for XDR.
217ce0e08e2SPeter Avalos */
218*ed183f8cSSascha Wildner sendsz = rounddown(sendsz + 3, 4);
219*ed183f8cSSascha Wildner recvsz = rounddown(recvsz + 3, 4);
220ce0e08e2SPeter Avalos cu = mem_alloc(sizeof (*cu) + sendsz + recvsz);
221ce0e08e2SPeter Avalos if (cu == NULL)
222ce0e08e2SPeter Avalos goto err1;
223ce0e08e2SPeter Avalos memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len);
224ce0e08e2SPeter Avalos cu->cu_rlen = svcaddr->len;
225ce0e08e2SPeter Avalos cu->cu_outbuf = &cu->cu_inbuf[recvsz];
226ce0e08e2SPeter Avalos /* Other values can also be set through clnt_control() */
227ce0e08e2SPeter Avalos cu->cu_wait.tv_sec = 15; /* heuristically chosen */
228ce0e08e2SPeter Avalos cu->cu_wait.tv_usec = 0;
229ce0e08e2SPeter Avalos cu->cu_total.tv_sec = -1;
230ce0e08e2SPeter Avalos cu->cu_total.tv_usec = -1;
231ce0e08e2SPeter Avalos cu->cu_sendsz = sendsz;
232ce0e08e2SPeter Avalos cu->cu_recvsz = recvsz;
233ce0e08e2SPeter Avalos cu->cu_async = FALSE;
234ce0e08e2SPeter Avalos cu->cu_connect = FALSE;
235ce0e08e2SPeter Avalos cu->cu_connected = FALSE;
236ce0e08e2SPeter Avalos gettimeofday(&now, NULL);
237ce0e08e2SPeter Avalos call_msg.rm_xid = __RPC_GETXID(&now);
238ce0e08e2SPeter Avalos call_msg.rm_call.cb_prog = program;
239ce0e08e2SPeter Avalos call_msg.rm_call.cb_vers = version;
240ce0e08e2SPeter Avalos xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
241ce0e08e2SPeter Avalos if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
242ce0e08e2SPeter Avalos rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */
243ce0e08e2SPeter Avalos rpc_createerr.cf_error.re_errno = 0;
244ce0e08e2SPeter Avalos goto err2;
245ce0e08e2SPeter Avalos }
246ce0e08e2SPeter Avalos cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
247ce0e08e2SPeter Avalos
248ce0e08e2SPeter Avalos /* XXX fvdl - do we still want this? */
249ce0e08e2SPeter Avalos #if 0
250ce0e08e2SPeter Avalos bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
251ce0e08e2SPeter Avalos #endif
252ce0e08e2SPeter Avalos _ioctl(fd, FIONBIO, (char *)(void *)&one);
253ce0e08e2SPeter Avalos
254ce0e08e2SPeter Avalos /*
255ce0e08e2SPeter Avalos * By default, closeit is always FALSE. It is users responsibility
256ce0e08e2SPeter Avalos * to do a close on it, else the user may use clnt_control
257ce0e08e2SPeter Avalos * to let clnt_destroy do it for him/her.
258ce0e08e2SPeter Avalos */
259ce0e08e2SPeter Avalos cu->cu_closeit = FALSE;
260ce0e08e2SPeter Avalos cu->cu_fd = fd;
261ce0e08e2SPeter Avalos cl->cl_ops = clnt_dg_ops();
262ce0e08e2SPeter Avalos cl->cl_private = (caddr_t)(void *)cu;
263ce0e08e2SPeter Avalos cl->cl_auth = authnone_create();
264ce0e08e2SPeter Avalos cl->cl_tp = NULL;
265ce0e08e2SPeter Avalos cl->cl_netid = NULL;
266ce0e08e2SPeter Avalos cu->cu_kq = -1;
267ce0e08e2SPeter Avalos EV_SET(&cu->cu_kin, cu->cu_fd, EVFILT_READ, EV_ADD, 0, 0, 0);
268ce0e08e2SPeter Avalos return (cl);
269ce0e08e2SPeter Avalos err1:
270ce0e08e2SPeter Avalos warnx(mem_err_clnt_dg);
271ce0e08e2SPeter Avalos rpc_createerr.cf_stat = RPC_SYSTEMERROR;
272ce0e08e2SPeter Avalos rpc_createerr.cf_error.re_errno = errno;
273ce0e08e2SPeter Avalos err2:
274ce0e08e2SPeter Avalos if (cl) {
275ce0e08e2SPeter Avalos mem_free(cl, sizeof (CLIENT));
276ce0e08e2SPeter Avalos if (cu)
277ce0e08e2SPeter Avalos mem_free(cu, sizeof (*cu) + sendsz + recvsz);
278ce0e08e2SPeter Avalos }
279ce0e08e2SPeter Avalos return (NULL);
280ce0e08e2SPeter Avalos }
281ce0e08e2SPeter Avalos
282ce0e08e2SPeter Avalos static enum clnt_stat
clnt_dg_call(CLIENT * cl,rpcproc_t proc,xdrproc_t xargs,void * argsp,xdrproc_t xresults,void * resultsp,struct timeval utimeout)283ce0e08e2SPeter Avalos clnt_dg_call(CLIENT *cl, /* client handle */
284ce0e08e2SPeter Avalos rpcproc_t proc, /* procedure number */
285ce0e08e2SPeter Avalos xdrproc_t xargs, /* xdr routine for args */
286ce0e08e2SPeter Avalos void *argsp, /* pointer to args */
287ce0e08e2SPeter Avalos xdrproc_t xresults, /* xdr routine for results */
288ce0e08e2SPeter Avalos void *resultsp, /* pointer to results */
289ce0e08e2SPeter Avalos struct timeval utimeout) /* seconds to wait before giving up */
290ce0e08e2SPeter Avalos {
291ce0e08e2SPeter Avalos struct cu_data *cu = (struct cu_data *)cl->cl_private;
292ce0e08e2SPeter Avalos XDR *xdrs;
293ce0e08e2SPeter Avalos size_t outlen = 0;
294ce0e08e2SPeter Avalos struct rpc_msg reply_msg;
295ce0e08e2SPeter Avalos XDR reply_xdrs;
296ce0e08e2SPeter Avalos bool_t ok;
297ce0e08e2SPeter Avalos int nrefreshes = 2; /* number of times to refresh cred */
298ce0e08e2SPeter Avalos struct timeval timeout;
299ce0e08e2SPeter Avalos struct timeval retransmit_time;
300ce0e08e2SPeter Avalos struct timeval next_sendtime, starttime, time_waited, tv;
301ce0e08e2SPeter Avalos struct timespec ts;
302ce0e08e2SPeter Avalos struct kevent kv;
303ce0e08e2SPeter Avalos struct sockaddr *sa;
304ce0e08e2SPeter Avalos sigset_t mask;
305ce0e08e2SPeter Avalos sigset_t newmask;
306cf515c3aSJohn Marino socklen_t salen;
307ce0e08e2SPeter Avalos ssize_t recvlen = 0;
308ce0e08e2SPeter Avalos int kin_len, n, rpc_lock_value;
309ce0e08e2SPeter Avalos u_int32_t xid;
310ce0e08e2SPeter Avalos
311ce0e08e2SPeter Avalos outlen = 0;
312ce0e08e2SPeter Avalos sigfillset(&newmask);
313ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
314ce0e08e2SPeter Avalos mutex_lock(&clnt_fd_lock);
315ce0e08e2SPeter Avalos while (dg_fd_locks[cu->cu_fd])
316ce0e08e2SPeter Avalos cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
317ce0e08e2SPeter Avalos if (__isthreaded)
318ce0e08e2SPeter Avalos rpc_lock_value = 1;
319ce0e08e2SPeter Avalos else
320ce0e08e2SPeter Avalos rpc_lock_value = 0;
321ce0e08e2SPeter Avalos dg_fd_locks[cu->cu_fd] = rpc_lock_value;
322ce0e08e2SPeter Avalos mutex_unlock(&clnt_fd_lock);
323ce0e08e2SPeter Avalos if (cu->cu_total.tv_usec == -1) {
324ce0e08e2SPeter Avalos timeout = utimeout; /* use supplied timeout */
325ce0e08e2SPeter Avalos } else {
326ce0e08e2SPeter Avalos timeout = cu->cu_total; /* use default timeout */
327ce0e08e2SPeter Avalos }
328ce0e08e2SPeter Avalos
329ce0e08e2SPeter Avalos if (cu->cu_connect && !cu->cu_connected) {
330ce0e08e2SPeter Avalos if (_connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr,
331ce0e08e2SPeter Avalos cu->cu_rlen) < 0) {
332ce0e08e2SPeter Avalos cu->cu_error.re_errno = errno;
333ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_CANTSEND;
334ce0e08e2SPeter Avalos goto out;
335ce0e08e2SPeter Avalos }
336ce0e08e2SPeter Avalos cu->cu_connected = 1;
337ce0e08e2SPeter Avalos }
338ce0e08e2SPeter Avalos if (cu->cu_connected) {
339ce0e08e2SPeter Avalos sa = NULL;
340ce0e08e2SPeter Avalos salen = 0;
341ce0e08e2SPeter Avalos } else {
342ce0e08e2SPeter Avalos sa = (struct sockaddr *)&cu->cu_raddr;
343ce0e08e2SPeter Avalos salen = cu->cu_rlen;
344ce0e08e2SPeter Avalos }
345ce0e08e2SPeter Avalos time_waited.tv_sec = 0;
346ce0e08e2SPeter Avalos time_waited.tv_usec = 0;
347ce0e08e2SPeter Avalos retransmit_time = next_sendtime = cu->cu_wait;
348ce0e08e2SPeter Avalos gettimeofday(&starttime, NULL);
349ce0e08e2SPeter Avalos
350ce0e08e2SPeter Avalos /* Clean up in case the last call ended in a longjmp(3) call. */
351ce0e08e2SPeter Avalos if (cu->cu_kq >= 0)
352ce0e08e2SPeter Avalos _close(cu->cu_kq);
353ce0e08e2SPeter Avalos if ((cu->cu_kq = kqueue()) < 0) {
354ce0e08e2SPeter Avalos cu->cu_error.re_errno = errno;
355ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_CANTSEND;
356ce0e08e2SPeter Avalos goto out;
357ce0e08e2SPeter Avalos }
358ce0e08e2SPeter Avalos kin_len = 1;
359ce0e08e2SPeter Avalos
360ce0e08e2SPeter Avalos call_again:
361ce0e08e2SPeter Avalos xdrs = &(cu->cu_outxdrs);
362ce0e08e2SPeter Avalos if (cu->cu_async == TRUE && xargs == NULL)
363ce0e08e2SPeter Avalos goto get_reply;
364ce0e08e2SPeter Avalos xdrs->x_op = XDR_ENCODE;
365ce0e08e2SPeter Avalos XDR_SETPOS(xdrs, cu->cu_xdrpos);
366ce0e08e2SPeter Avalos /*
367ce0e08e2SPeter Avalos * the transaction is the first thing in the out buffer
368ce0e08e2SPeter Avalos * XXX Yes, and it's in network byte order, so we should to
369ce0e08e2SPeter Avalos * be careful when we increment it, shouldn't we.
370ce0e08e2SPeter Avalos */
371ce0e08e2SPeter Avalos xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf));
372ce0e08e2SPeter Avalos xid++;
373ce0e08e2SPeter Avalos *(u_int32_t *)(void *)(cu->cu_outbuf) = htonl(xid);
374ce0e08e2SPeter Avalos
375ce0e08e2SPeter Avalos if ((! XDR_PUTINT32(xdrs, &proc)) ||
376ce0e08e2SPeter Avalos (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
377ce0e08e2SPeter Avalos (! (*xargs)(xdrs, argsp))) {
378ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_CANTENCODEARGS;
379ce0e08e2SPeter Avalos goto out;
380ce0e08e2SPeter Avalos }
381ce0e08e2SPeter Avalos outlen = (size_t)XDR_GETPOS(xdrs);
382ce0e08e2SPeter Avalos
383ce0e08e2SPeter Avalos send_again:
384ce0e08e2SPeter Avalos if (_sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) {
385ce0e08e2SPeter Avalos cu->cu_error.re_errno = errno;
386ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_CANTSEND;
387ce0e08e2SPeter Avalos goto out;
388ce0e08e2SPeter Avalos }
389ce0e08e2SPeter Avalos
390ce0e08e2SPeter Avalos /*
391ce0e08e2SPeter Avalos * Hack to provide rpc-based message passing
392ce0e08e2SPeter Avalos */
393ce0e08e2SPeter Avalos if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
394ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_TIMEDOUT;
395ce0e08e2SPeter Avalos goto out;
396ce0e08e2SPeter Avalos }
397ce0e08e2SPeter Avalos
398ce0e08e2SPeter Avalos get_reply:
399ce0e08e2SPeter Avalos
400ce0e08e2SPeter Avalos /*
401ce0e08e2SPeter Avalos * sub-optimal code appears here because we have
402ce0e08e2SPeter Avalos * some clock time to spare while the packets are in flight.
403ce0e08e2SPeter Avalos * (We assume that this is actually only executed once.)
404ce0e08e2SPeter Avalos */
405ce0e08e2SPeter Avalos reply_msg.acpted_rply.ar_verf = _null_auth;
406ce0e08e2SPeter Avalos reply_msg.acpted_rply.ar_results.where = resultsp;
407ce0e08e2SPeter Avalos reply_msg.acpted_rply.ar_results.proc = xresults;
408ce0e08e2SPeter Avalos
409ce0e08e2SPeter Avalos for (;;) {
410ce0e08e2SPeter Avalos /* Decide how long to wait. */
411ce0e08e2SPeter Avalos if (timercmp(&next_sendtime, &timeout, <))
412ce0e08e2SPeter Avalos timersub(&next_sendtime, &time_waited, &tv);
413ce0e08e2SPeter Avalos else
414ce0e08e2SPeter Avalos timersub(&timeout, &time_waited, &tv);
415ce0e08e2SPeter Avalos if (tv.tv_sec < 0 || tv.tv_usec < 0)
416ce0e08e2SPeter Avalos tv.tv_sec = tv.tv_usec = 0;
417ce0e08e2SPeter Avalos TIMEVAL_TO_TIMESPEC(&tv, &ts);
418ce0e08e2SPeter Avalos
419ce0e08e2SPeter Avalos n = _kevent(cu->cu_kq, &cu->cu_kin, kin_len, &kv, 1, &ts);
420ce0e08e2SPeter Avalos /* We don't need to register the event again. */
421ce0e08e2SPeter Avalos kin_len = 0;
422ce0e08e2SPeter Avalos
423ce0e08e2SPeter Avalos if (n == 1) {
424ce0e08e2SPeter Avalos if (kv.flags & EV_ERROR) {
425ce0e08e2SPeter Avalos cu->cu_error.re_errno = kv.data;
426ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_CANTRECV;
427ce0e08e2SPeter Avalos goto out;
428ce0e08e2SPeter Avalos }
429ce0e08e2SPeter Avalos /* We have some data now */
430ce0e08e2SPeter Avalos do {
431ce0e08e2SPeter Avalos recvlen = _recvfrom(cu->cu_fd, cu->cu_inbuf,
432ce0e08e2SPeter Avalos cu->cu_recvsz, 0, NULL, NULL);
433ce0e08e2SPeter Avalos } while (recvlen < 0 && errno == EINTR);
434ce0e08e2SPeter Avalos if (recvlen < 0 && errno != EWOULDBLOCK) {
435ce0e08e2SPeter Avalos cu->cu_error.re_errno = errno;
436ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_CANTRECV;
437ce0e08e2SPeter Avalos goto out;
438ce0e08e2SPeter Avalos }
439ce0e08e2SPeter Avalos if (recvlen >= sizeof(u_int32_t) &&
440ce0e08e2SPeter Avalos (cu->cu_async == TRUE ||
441ce0e08e2SPeter Avalos *((u_int32_t *)(void *)(cu->cu_inbuf)) ==
442ce0e08e2SPeter Avalos *((u_int32_t *)(void *)(cu->cu_outbuf)))) {
443ce0e08e2SPeter Avalos /* We now assume we have the proper reply. */
444ce0e08e2SPeter Avalos break;
445ce0e08e2SPeter Avalos }
446ce0e08e2SPeter Avalos }
447ce0e08e2SPeter Avalos if (n == -1 && errno != EINTR) {
448ce0e08e2SPeter Avalos cu->cu_error.re_errno = errno;
449ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_CANTRECV;
450ce0e08e2SPeter Avalos goto out;
451ce0e08e2SPeter Avalos }
452ce0e08e2SPeter Avalos gettimeofday(&tv, NULL);
453ce0e08e2SPeter Avalos timersub(&tv, &starttime, &time_waited);
454ce0e08e2SPeter Avalos
455ce0e08e2SPeter Avalos /* Check for timeout. */
456ce0e08e2SPeter Avalos if (timercmp(&time_waited, &timeout, >)) {
457ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_TIMEDOUT;
458ce0e08e2SPeter Avalos goto out;
459ce0e08e2SPeter Avalos }
460ce0e08e2SPeter Avalos
461ce0e08e2SPeter Avalos /* Retransmit if necessary. */
462ce0e08e2SPeter Avalos if (timercmp(&time_waited, &next_sendtime, >)) {
463ce0e08e2SPeter Avalos /* update retransmit_time */
464ce0e08e2SPeter Avalos if (retransmit_time.tv_sec < RPC_MAX_BACKOFF)
465ce0e08e2SPeter Avalos timeradd(&retransmit_time, &retransmit_time,
466ce0e08e2SPeter Avalos &retransmit_time);
467ce0e08e2SPeter Avalos timeradd(&next_sendtime, &retransmit_time,
468ce0e08e2SPeter Avalos &next_sendtime);
469ce0e08e2SPeter Avalos goto send_again;
470ce0e08e2SPeter Avalos }
471ce0e08e2SPeter Avalos }
472ce0e08e2SPeter Avalos
473ce0e08e2SPeter Avalos /*
474ce0e08e2SPeter Avalos * now decode and validate the response
475ce0e08e2SPeter Avalos */
476ce0e08e2SPeter Avalos
477ce0e08e2SPeter Avalos xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE);
478ce0e08e2SPeter Avalos ok = xdr_replymsg(&reply_xdrs, &reply_msg);
479ce0e08e2SPeter Avalos /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
480ce0e08e2SPeter Avalos if (ok) {
481ce0e08e2SPeter Avalos if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
482ce0e08e2SPeter Avalos (reply_msg.acpted_rply.ar_stat == SUCCESS))
483ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_SUCCESS;
484ce0e08e2SPeter Avalos else
485ce0e08e2SPeter Avalos _seterr_reply(&reply_msg, &(cu->cu_error));
486ce0e08e2SPeter Avalos
487ce0e08e2SPeter Avalos if (cu->cu_error.re_status == RPC_SUCCESS) {
488ce0e08e2SPeter Avalos if (! AUTH_VALIDATE(cl->cl_auth,
489ce0e08e2SPeter Avalos &reply_msg.acpted_rply.ar_verf)) {
490ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_AUTHERROR;
491ce0e08e2SPeter Avalos cu->cu_error.re_why = AUTH_INVALIDRESP;
492ce0e08e2SPeter Avalos }
493ce0e08e2SPeter Avalos if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
494ce0e08e2SPeter Avalos xdrs->x_op = XDR_FREE;
495ce0e08e2SPeter Avalos xdr_opaque_auth(xdrs,
496ce0e08e2SPeter Avalos &(reply_msg.acpted_rply.ar_verf));
497ce0e08e2SPeter Avalos }
498ce0e08e2SPeter Avalos } /* end successful completion */
499ce0e08e2SPeter Avalos /*
500ce0e08e2SPeter Avalos * If unsuccesful AND error is an authentication error
501ce0e08e2SPeter Avalos * then refresh credentials and try again, else break
502ce0e08e2SPeter Avalos */
503ce0e08e2SPeter Avalos else if (cu->cu_error.re_status == RPC_AUTHERROR)
504ce0e08e2SPeter Avalos /* maybe our credentials need to be refreshed ... */
505ce0e08e2SPeter Avalos if (nrefreshes > 0 &&
506ce0e08e2SPeter Avalos AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
507ce0e08e2SPeter Avalos nrefreshes--;
508ce0e08e2SPeter Avalos goto call_again;
509ce0e08e2SPeter Avalos }
510ce0e08e2SPeter Avalos /* end of unsuccessful completion */
511ce0e08e2SPeter Avalos } /* end of valid reply message */
512ce0e08e2SPeter Avalos else {
513ce0e08e2SPeter Avalos cu->cu_error.re_status = RPC_CANTDECODERES;
514ce0e08e2SPeter Avalos
515ce0e08e2SPeter Avalos }
516ce0e08e2SPeter Avalos out:
517ce0e08e2SPeter Avalos if (cu->cu_kq >= 0)
518ce0e08e2SPeter Avalos _close(cu->cu_kq);
519ce0e08e2SPeter Avalos cu->cu_kq = -1;
520ce0e08e2SPeter Avalos release_fd_lock(cu->cu_fd, mask);
521ce0e08e2SPeter Avalos return (cu->cu_error.re_status);
522ce0e08e2SPeter Avalos }
523ce0e08e2SPeter Avalos
524ce0e08e2SPeter Avalos static void
clnt_dg_geterr(CLIENT * cl,struct rpc_err * errp)525ce0e08e2SPeter Avalos clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
526ce0e08e2SPeter Avalos {
527ce0e08e2SPeter Avalos struct cu_data *cu = (struct cu_data *)cl->cl_private;
528ce0e08e2SPeter Avalos
529ce0e08e2SPeter Avalos *errp = cu->cu_error;
530ce0e08e2SPeter Avalos }
531ce0e08e2SPeter Avalos
532ce0e08e2SPeter Avalos static bool_t
clnt_dg_freeres(CLIENT * cl,xdrproc_t xdr_res,void * res_ptr)533ce0e08e2SPeter Avalos clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
534ce0e08e2SPeter Avalos {
535ce0e08e2SPeter Avalos struct cu_data *cu = (struct cu_data *)cl->cl_private;
536ce0e08e2SPeter Avalos XDR *xdrs = &(cu->cu_outxdrs);
537ce0e08e2SPeter Avalos bool_t dummy;
538ce0e08e2SPeter Avalos sigset_t mask;
539ce0e08e2SPeter Avalos sigset_t newmask;
540ce0e08e2SPeter Avalos
541ce0e08e2SPeter Avalos sigfillset(&newmask);
542ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
543ce0e08e2SPeter Avalos mutex_lock(&clnt_fd_lock);
544ce0e08e2SPeter Avalos while (dg_fd_locks[cu->cu_fd])
545ce0e08e2SPeter Avalos cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
546ce0e08e2SPeter Avalos xdrs->x_op = XDR_FREE;
547ce0e08e2SPeter Avalos dummy = (*xdr_res)(xdrs, res_ptr);
548ce0e08e2SPeter Avalos mutex_unlock(&clnt_fd_lock);
549ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &mask, NULL);
550ce0e08e2SPeter Avalos cond_signal(&dg_cv[cu->cu_fd]);
551ce0e08e2SPeter Avalos return (dummy);
552ce0e08e2SPeter Avalos }
553ce0e08e2SPeter Avalos
554ce0e08e2SPeter Avalos /*ARGSUSED*/
555ce0e08e2SPeter Avalos static void
clnt_dg_abort(CLIENT * h __unused)5566d7019e6SSascha Wildner clnt_dg_abort(CLIENT *h __unused)
557ce0e08e2SPeter Avalos {
558ce0e08e2SPeter Avalos }
559ce0e08e2SPeter Avalos
560ce0e08e2SPeter Avalos static bool_t
clnt_dg_control(CLIENT * cl,u_int request,void * info)561ce0e08e2SPeter Avalos clnt_dg_control(CLIENT *cl, u_int request, void *info)
562ce0e08e2SPeter Avalos {
563ce0e08e2SPeter Avalos struct cu_data *cu = (struct cu_data *)cl->cl_private;
564ce0e08e2SPeter Avalos struct netbuf *addr;
565ce0e08e2SPeter Avalos sigset_t mask;
566ce0e08e2SPeter Avalos sigset_t newmask;
567ce0e08e2SPeter Avalos int rpc_lock_value;
568ce0e08e2SPeter Avalos
569ce0e08e2SPeter Avalos sigfillset(&newmask);
570ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
571ce0e08e2SPeter Avalos mutex_lock(&clnt_fd_lock);
572ce0e08e2SPeter Avalos while (dg_fd_locks[cu->cu_fd])
573ce0e08e2SPeter Avalos cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
574ce0e08e2SPeter Avalos if (__isthreaded)
575ce0e08e2SPeter Avalos rpc_lock_value = 1;
576ce0e08e2SPeter Avalos else
577ce0e08e2SPeter Avalos rpc_lock_value = 0;
578ce0e08e2SPeter Avalos dg_fd_locks[cu->cu_fd] = rpc_lock_value;
579ce0e08e2SPeter Avalos mutex_unlock(&clnt_fd_lock);
580ce0e08e2SPeter Avalos switch (request) {
581ce0e08e2SPeter Avalos case CLSET_FD_CLOSE:
582ce0e08e2SPeter Avalos cu->cu_closeit = TRUE;
583ce0e08e2SPeter Avalos release_fd_lock(cu->cu_fd, mask);
584ce0e08e2SPeter Avalos return (TRUE);
585ce0e08e2SPeter Avalos case CLSET_FD_NCLOSE:
586ce0e08e2SPeter Avalos cu->cu_closeit = FALSE;
587ce0e08e2SPeter Avalos release_fd_lock(cu->cu_fd, mask);
588ce0e08e2SPeter Avalos return (TRUE);
589ce0e08e2SPeter Avalos }
590ce0e08e2SPeter Avalos
591ce0e08e2SPeter Avalos /* for other requests which use info */
592ce0e08e2SPeter Avalos if (info == NULL) {
593ce0e08e2SPeter Avalos release_fd_lock(cu->cu_fd, mask);
594ce0e08e2SPeter Avalos return (FALSE);
595ce0e08e2SPeter Avalos }
596ce0e08e2SPeter Avalos switch (request) {
597ce0e08e2SPeter Avalos case CLSET_TIMEOUT:
598ce0e08e2SPeter Avalos if (time_not_ok((struct timeval *)info)) {
599ce0e08e2SPeter Avalos release_fd_lock(cu->cu_fd, mask);
600ce0e08e2SPeter Avalos return (FALSE);
601ce0e08e2SPeter Avalos }
602ce0e08e2SPeter Avalos cu->cu_total = *(struct timeval *)info;
603ce0e08e2SPeter Avalos break;
604ce0e08e2SPeter Avalos case CLGET_TIMEOUT:
605ce0e08e2SPeter Avalos *(struct timeval *)info = cu->cu_total;
606ce0e08e2SPeter Avalos break;
607ce0e08e2SPeter Avalos case CLGET_SERVER_ADDR: /* Give him the fd address */
608ce0e08e2SPeter Avalos /* Now obsolete. Only for backward compatibility */
609ce0e08e2SPeter Avalos memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen);
610ce0e08e2SPeter Avalos break;
611ce0e08e2SPeter Avalos case CLSET_RETRY_TIMEOUT:
612ce0e08e2SPeter Avalos if (time_not_ok((struct timeval *)info)) {
613ce0e08e2SPeter Avalos release_fd_lock(cu->cu_fd, mask);
614ce0e08e2SPeter Avalos return (FALSE);
615ce0e08e2SPeter Avalos }
616ce0e08e2SPeter Avalos cu->cu_wait = *(struct timeval *)info;
617ce0e08e2SPeter Avalos break;
618ce0e08e2SPeter Avalos case CLGET_RETRY_TIMEOUT:
619ce0e08e2SPeter Avalos *(struct timeval *)info = cu->cu_wait;
620ce0e08e2SPeter Avalos break;
621ce0e08e2SPeter Avalos case CLGET_FD:
622ce0e08e2SPeter Avalos *(int *)info = cu->cu_fd;
623ce0e08e2SPeter Avalos break;
624ce0e08e2SPeter Avalos case CLGET_SVC_ADDR:
625ce0e08e2SPeter Avalos addr = (struct netbuf *)info;
626ce0e08e2SPeter Avalos addr->buf = &cu->cu_raddr;
627ce0e08e2SPeter Avalos addr->len = cu->cu_rlen;
628ce0e08e2SPeter Avalos addr->maxlen = sizeof cu->cu_raddr;
629ce0e08e2SPeter Avalos break;
630ce0e08e2SPeter Avalos case CLSET_SVC_ADDR: /* set to new address */
631ce0e08e2SPeter Avalos addr = (struct netbuf *)info;
632ce0e08e2SPeter Avalos if (addr->len < sizeof cu->cu_raddr) {
633ce0e08e2SPeter Avalos release_fd_lock(cu->cu_fd, mask);
634ce0e08e2SPeter Avalos return (FALSE);
635ce0e08e2SPeter Avalos }
636ce0e08e2SPeter Avalos memcpy(&cu->cu_raddr, addr->buf, addr->len);
637ce0e08e2SPeter Avalos cu->cu_rlen = addr->len;
638ce0e08e2SPeter Avalos break;
639ce0e08e2SPeter Avalos case CLGET_XID:
640ce0e08e2SPeter Avalos /*
641ce0e08e2SPeter Avalos * use the knowledge that xid is the
642ce0e08e2SPeter Avalos * first element in the call structure *.
643ce0e08e2SPeter Avalos * This will get the xid of the PREVIOUS call
644ce0e08e2SPeter Avalos */
645ce0e08e2SPeter Avalos *(u_int32_t *)info =
646ce0e08e2SPeter Avalos ntohl(*(u_int32_t *)(void *)cu->cu_outbuf);
647ce0e08e2SPeter Avalos break;
648ce0e08e2SPeter Avalos
649ce0e08e2SPeter Avalos case CLSET_XID:
650ce0e08e2SPeter Avalos /* This will set the xid of the NEXT call */
651ce0e08e2SPeter Avalos *(u_int32_t *)(void *)cu->cu_outbuf =
652ce0e08e2SPeter Avalos htonl(*(u_int32_t *)info - 1);
653ce0e08e2SPeter Avalos /* decrement by 1 as clnt_dg_call() increments once */
654ce0e08e2SPeter Avalos break;
655ce0e08e2SPeter Avalos
656ce0e08e2SPeter Avalos case CLGET_VERS:
657ce0e08e2SPeter Avalos /*
658ce0e08e2SPeter Avalos * This RELIES on the information that, in the call body,
659ce0e08e2SPeter Avalos * the version number field is the fifth field from the
660ce0e08e2SPeter Avalos * begining of the RPC header. MUST be changed if the
661ce0e08e2SPeter Avalos * call_struct is changed
662ce0e08e2SPeter Avalos */
663ce0e08e2SPeter Avalos *(u_int32_t *)info =
664ce0e08e2SPeter Avalos ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
665ce0e08e2SPeter Avalos 4 * BYTES_PER_XDR_UNIT));
666ce0e08e2SPeter Avalos break;
667ce0e08e2SPeter Avalos
668ce0e08e2SPeter Avalos case CLSET_VERS:
669ce0e08e2SPeter Avalos *(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
670ce0e08e2SPeter Avalos = htonl(*(u_int32_t *)info);
671ce0e08e2SPeter Avalos break;
672ce0e08e2SPeter Avalos
673ce0e08e2SPeter Avalos case CLGET_PROG:
674ce0e08e2SPeter Avalos /*
675ce0e08e2SPeter Avalos * This RELIES on the information that, in the call body,
676ce0e08e2SPeter Avalos * the program number field is the fourth field from the
677ce0e08e2SPeter Avalos * begining of the RPC header. MUST be changed if the
678ce0e08e2SPeter Avalos * call_struct is changed
679ce0e08e2SPeter Avalos */
680ce0e08e2SPeter Avalos *(u_int32_t *)info =
681ce0e08e2SPeter Avalos ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf +
682ce0e08e2SPeter Avalos 3 * BYTES_PER_XDR_UNIT));
683ce0e08e2SPeter Avalos break;
684ce0e08e2SPeter Avalos
685ce0e08e2SPeter Avalos case CLSET_PROG:
686ce0e08e2SPeter Avalos *(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
687ce0e08e2SPeter Avalos = htonl(*(u_int32_t *)info);
688ce0e08e2SPeter Avalos break;
689ce0e08e2SPeter Avalos case CLSET_ASYNC:
690ce0e08e2SPeter Avalos cu->cu_async = *(int *)info;
691ce0e08e2SPeter Avalos break;
692ce0e08e2SPeter Avalos case CLSET_CONNECT:
693ce0e08e2SPeter Avalos cu->cu_connect = *(int *)info;
694ce0e08e2SPeter Avalos break;
695ce0e08e2SPeter Avalos default:
696ce0e08e2SPeter Avalos release_fd_lock(cu->cu_fd, mask);
697ce0e08e2SPeter Avalos return (FALSE);
698ce0e08e2SPeter Avalos }
699ce0e08e2SPeter Avalos release_fd_lock(cu->cu_fd, mask);
700ce0e08e2SPeter Avalos return (TRUE);
701ce0e08e2SPeter Avalos }
702ce0e08e2SPeter Avalos
703ce0e08e2SPeter Avalos static void
clnt_dg_destroy(CLIENT * cl)704ce0e08e2SPeter Avalos clnt_dg_destroy(CLIENT *cl)
705ce0e08e2SPeter Avalos {
706ce0e08e2SPeter Avalos struct cu_data *cu = (struct cu_data *)cl->cl_private;
707ce0e08e2SPeter Avalos int cu_fd = cu->cu_fd;
708ce0e08e2SPeter Avalos sigset_t mask;
709ce0e08e2SPeter Avalos sigset_t newmask;
710ce0e08e2SPeter Avalos
711ce0e08e2SPeter Avalos sigfillset(&newmask);
712ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
713ce0e08e2SPeter Avalos mutex_lock(&clnt_fd_lock);
714ce0e08e2SPeter Avalos while (dg_fd_locks[cu_fd])
715ce0e08e2SPeter Avalos cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
716ce0e08e2SPeter Avalos if (cu->cu_closeit)
717ce0e08e2SPeter Avalos _close(cu_fd);
718ce0e08e2SPeter Avalos if (cu->cu_kq >= 0)
719ce0e08e2SPeter Avalos _close(cu->cu_kq);
720ce0e08e2SPeter Avalos XDR_DESTROY(&(cu->cu_outxdrs));
721ce0e08e2SPeter Avalos mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
722ce0e08e2SPeter Avalos if (cl->cl_netid && cl->cl_netid[0])
723ce0e08e2SPeter Avalos mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
724ce0e08e2SPeter Avalos if (cl->cl_tp && cl->cl_tp[0])
725ce0e08e2SPeter Avalos mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
726ce0e08e2SPeter Avalos mem_free(cl, sizeof (CLIENT));
727ce0e08e2SPeter Avalos mutex_unlock(&clnt_fd_lock);
728ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &mask, NULL);
729ce0e08e2SPeter Avalos cond_signal(&dg_cv[cu_fd]);
730ce0e08e2SPeter Avalos }
731ce0e08e2SPeter Avalos
732ce0e08e2SPeter Avalos static struct clnt_ops *
clnt_dg_ops(void)733ce0e08e2SPeter Avalos clnt_dg_ops(void)
734ce0e08e2SPeter Avalos {
735ce0e08e2SPeter Avalos static struct clnt_ops ops;
736ce0e08e2SPeter Avalos sigset_t mask;
737ce0e08e2SPeter Avalos sigset_t newmask;
738ce0e08e2SPeter Avalos
739ce0e08e2SPeter Avalos /* VARIABLES PROTECTED BY ops_lock: ops */
740ce0e08e2SPeter Avalos
741ce0e08e2SPeter Avalos sigfillset(&newmask);
742ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
743ce0e08e2SPeter Avalos mutex_lock(&ops_lock);
744ce0e08e2SPeter Avalos if (ops.cl_call == NULL) {
745ce0e08e2SPeter Avalos ops.cl_call = clnt_dg_call;
746ce0e08e2SPeter Avalos ops.cl_abort = clnt_dg_abort;
747ce0e08e2SPeter Avalos ops.cl_geterr = clnt_dg_geterr;
748ce0e08e2SPeter Avalos ops.cl_freeres = clnt_dg_freeres;
749ce0e08e2SPeter Avalos ops.cl_destroy = clnt_dg_destroy;
750ce0e08e2SPeter Avalos ops.cl_control = clnt_dg_control;
751ce0e08e2SPeter Avalos }
752ce0e08e2SPeter Avalos mutex_unlock(&ops_lock);
753ce0e08e2SPeter Avalos thr_sigsetmask(SIG_SETMASK, &mask, NULL);
754ce0e08e2SPeter Avalos return (&ops);
755ce0e08e2SPeter Avalos }
756ce0e08e2SPeter Avalos
757ce0e08e2SPeter Avalos /*
758ce0e08e2SPeter Avalos * Make sure that the time is not garbage. -1 value is allowed.
759ce0e08e2SPeter Avalos */
760ce0e08e2SPeter Avalos static bool_t
time_not_ok(struct timeval * t)761ce0e08e2SPeter Avalos time_not_ok(struct timeval *t)
762ce0e08e2SPeter Avalos {
763ce0e08e2SPeter Avalos return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
764ce0e08e2SPeter Avalos t->tv_usec < -1 || t->tv_usec > 1000000);
765ce0e08e2SPeter Avalos }
766