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