clnt_vc.c revision 1.28 1 1.28 andvar /* $NetBSD: clnt_vc.c,v 1.28 2022/01/24 09:14:36 andvar Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.21 tron * Copyright (c) 2010, Oracle America, Inc.
5 1.21 tron *
6 1.21 tron * Redistribution and use in source and binary forms, with or without
7 1.21 tron * modification, are permitted provided that the following conditions are
8 1.21 tron * met:
9 1.21 tron *
10 1.21 tron * * Redistributions of source code must retain the above copyright
11 1.21 tron * notice, this list of conditions and the following disclaimer.
12 1.21 tron * * Redistributions in binary form must reproduce the above
13 1.21 tron * copyright notice, this list of conditions and the following
14 1.21 tron * disclaimer in the documentation and/or other materials
15 1.21 tron * provided with the distribution.
16 1.21 tron * * Neither the name of the "Oracle America, Inc." nor the names of its
17 1.21 tron * contributors may be used to endorse or promote products derived
18 1.21 tron * from this software without specific prior written permission.
19 1.21 tron *
20 1.21 tron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.21 tron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.21 tron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.21 tron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.21 tron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 1.21 tron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.21 tron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.21 tron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.21 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.21 tron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.21 tron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.21 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 #include <sys/cdefs.h>
35 1.1 fvdl #if defined(LIBC_SCCS) && !defined(lint)
36 1.1 fvdl #if 0
37 1.1 fvdl static char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
38 1.1 fvdl static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";
39 1.1 fvdl static char sccsid[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
40 1.1 fvdl #else
41 1.28 andvar __RCSID("$NetBSD: clnt_vc.c,v 1.28 2022/01/24 09:14:36 andvar Exp $");
42 1.1 fvdl #endif
43 1.1 fvdl #endif
44 1.1 fvdl
45 1.1 fvdl /*
46 1.1 fvdl * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
47 1.1 fvdl *
48 1.1 fvdl * Copyright (C) 1984, Sun Microsystems, Inc.
49 1.1 fvdl *
50 1.1 fvdl * TCP based RPC supports 'batched calls'.
51 1.1 fvdl * A sequence of calls may be batched-up in a send buffer. The rpc call
52 1.1 fvdl * return immediately to the client even though the call was not necessarily
53 1.1 fvdl * sent. The batching occurs if the results' xdr routine is NULL (0) AND
54 1.1 fvdl * the rpc timeout value is zero (see clnt.h, rpc).
55 1.1 fvdl *
56 1.1 fvdl * Clients should NOT casually batch calls that in fact return results; that is,
57 1.1 fvdl * the server side should be aware that a call is batched and not produce any
58 1.1 fvdl * return message. Batched calls that produce many result messages can
59 1.1 fvdl * deadlock (netlock) the client and the server....
60 1.1 fvdl *
61 1.1 fvdl * Now go hang yourself.
62 1.1 fvdl */
63 1.1 fvdl
64 1.1 fvdl #include "namespace.h"
65 1.1 fvdl #include "reentrant.h"
66 1.1 fvdl #include <sys/types.h>
67 1.1 fvdl #include <sys/poll.h>
68 1.1 fvdl #include <sys/socket.h>
69 1.1 fvdl
70 1.1 fvdl #include <assert.h>
71 1.1 fvdl #include <err.h>
72 1.1 fvdl #include <errno.h>
73 1.1 fvdl #include <netdb.h>
74 1.1 fvdl #include <stdio.h>
75 1.1 fvdl #include <stdlib.h>
76 1.2 thorpej #include <string.h>
77 1.1 fvdl #include <unistd.h>
78 1.1 fvdl #include <signal.h>
79 1.1 fvdl
80 1.1 fvdl #include <rpc/rpc.h>
81 1.1 fvdl
82 1.20 christos #include "svc_fdset.h"
83 1.8 fvdl #include "rpc_internal.h"
84 1.1 fvdl
85 1.1 fvdl #ifdef __weak_alias
86 1.1 fvdl __weak_alias(clnt_vc_create,_clnt_vc_create)
87 1.1 fvdl #endif
88 1.1 fvdl
89 1.1 fvdl #define MCALL_MSG_SIZE 24
90 1.1 fvdl
91 1.18 christos static enum clnt_stat clnt_vc_call(CLIENT *, rpcproc_t, xdrproc_t,
92 1.18 christos const char *, xdrproc_t, caddr_t, struct timeval);
93 1.18 christos static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
94 1.18 christos static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, caddr_t);
95 1.18 christos static void clnt_vc_abort(CLIENT *);
96 1.18 christos static bool_t clnt_vc_control(CLIENT *, u_int, char *);
97 1.18 christos static void clnt_vc_destroy(CLIENT *);
98 1.18 christos static struct clnt_ops *clnt_vc_ops(void);
99 1.18 christos static bool_t time_not_ok(struct timeval *);
100 1.18 christos static int read_vc(caddr_t, caddr_t, int);
101 1.18 christos static int write_vc(caddr_t, caddr_t, int);
102 1.1 fvdl
103 1.1 fvdl struct ct_data {
104 1.1 fvdl int ct_fd;
105 1.1 fvdl bool_t ct_closeit;
106 1.1 fvdl struct timeval ct_wait;
107 1.1 fvdl bool_t ct_waitset; /* wait set by clnt_control? */
108 1.1 fvdl struct netbuf ct_addr;
109 1.1 fvdl struct rpc_err ct_error;
110 1.1 fvdl union {
111 1.1 fvdl char ct_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */
112 1.1 fvdl u_int32_t ct_mcalli;
113 1.1 fvdl } ct_u;
114 1.1 fvdl u_int ct_mpos; /* pos after marshal */
115 1.1 fvdl XDR ct_xdrs;
116 1.1 fvdl };
117 1.1 fvdl
118 1.1 fvdl /*
119 1.1 fvdl * This machinery implements per-fd locks for MT-safety. It is not
120 1.1 fvdl * sufficient to do per-CLIENT handle locks for MT-safety because a
121 1.1 fvdl * user may create more than one CLIENT handle with the same fd behind
122 1.27 andvar * it. Therefore, we allocate an array of flags (vc_fd_locks), protected
123 1.1 fvdl * by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
124 1.27 andvar * similarly protected. Vc_fd_lock[fd] == 1 => a call is active on some
125 1.1 fvdl * CLIENT handle created for that fd.
126 1.1 fvdl * The current implementation holds locks across the entire RPC and reply.
127 1.1 fvdl * Yes, this is silly, and as soon as this code is proven to work, this
128 1.1 fvdl * should be the first thing fixed. One step at a time.
129 1.1 fvdl */
130 1.9 thorpej #ifdef _REENTRANT
131 1.1 fvdl static int *vc_fd_locks;
132 1.9 thorpej #define __rpc_lock_value __isthreaded;
133 1.1 fvdl extern mutex_t clnt_fd_lock;
134 1.1 fvdl static cond_t *vc_cv;
135 1.1 fvdl #define release_fd_lock(fd, mask) { \
136 1.1 fvdl mutex_lock(&clnt_fd_lock); \
137 1.1 fvdl vc_fd_locks[fd] = 0; \
138 1.1 fvdl mutex_unlock(&clnt_fd_lock); \
139 1.15 christos thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \
140 1.1 fvdl cond_signal(&vc_cv[fd]); \
141 1.1 fvdl }
142 1.1 fvdl #else
143 1.1 fvdl #define release_fd_lock(fd,mask)
144 1.1 fvdl #define __rpc_lock_value 0
145 1.1 fvdl #endif
146 1.1 fvdl
147 1.24 christos static __inline void
148 1.25 christos htonlp(void *dst, const void *src, uint32_t incr)
149 1.24 christos {
150 1.24 christos #if 0
151 1.24 christos uint32_t tmp;
152 1.24 christos memcpy(&tmp, src, sizeof(tmp));
153 1.25 christos tmp = htonl(tmp + incr);
154 1.24 christos memcpy(dst, &tmp, sizeof(tmp));
155 1.24 christos #else
156 1.24 christos /* We are aligned, so we think */
157 1.25 christos *(uint32_t *)dst = htonl(*(const uint32_t *)src + incr);
158 1.24 christos #endif
159 1.24 christos }
160 1.24 christos
161 1.24 christos static __inline void
162 1.24 christos ntohlp(void *dst, const void *src)
163 1.24 christos {
164 1.24 christos #if 0
165 1.24 christos uint32_t tmp;
166 1.24 christos memcpy(&tmp, src, sizeof(tmp));
167 1.24 christos tmp = ntohl(tmp);
168 1.24 christos memcpy(dst, &tmp, sizeof(tmp));
169 1.24 christos #else
170 1.24 christos /* We are aligned, so we think */
171 1.24 christos *(uint32_t *)dst = htonl(*(const uint32_t *)src);
172 1.24 christos #endif
173 1.24 christos }
174 1.1 fvdl
175 1.1 fvdl /*
176 1.1 fvdl * Create a client handle for a connection.
177 1.1 fvdl * Default options are set, which the user can change using clnt_control()'s.
178 1.1 fvdl * The rpc/vc package does buffering similar to stdio, so the client
179 1.1 fvdl * must pick send and receive buffer sizes, 0 => use the default.
180 1.1 fvdl * NB: fd is copied into a private area.
181 1.1 fvdl * NB: The rpch->cl_auth is set null authentication. Caller may wish to
182 1.1 fvdl * set this something more useful.
183 1.1 fvdl *
184 1.1 fvdl * fd should be an open socket
185 1.1 fvdl */
186 1.1 fvdl CLIENT *
187 1.18 christos clnt_vc_create(
188 1.18 christos int fd,
189 1.18 christos const struct netbuf *raddr,
190 1.18 christos rpcprog_t prog,
191 1.18 christos rpcvers_t vers,
192 1.18 christos u_int sendsz,
193 1.18 christos u_int recvsz
194 1.18 christos )
195 1.1 fvdl {
196 1.1 fvdl CLIENT *h;
197 1.1 fvdl struct ct_data *ct = NULL;
198 1.1 fvdl struct rpc_msg call_msg;
199 1.9 thorpej #ifdef _REENTRANT
200 1.1 fvdl sigset_t mask;
201 1.1 fvdl #endif
202 1.1 fvdl sigset_t newmask;
203 1.1 fvdl struct sockaddr_storage ss;
204 1.1 fvdl socklen_t slen;
205 1.1 fvdl struct __rpc_sockinfo si;
206 1.1 fvdl
207 1.7 lukem _DIAGASSERT(raddr != NULL);
208 1.7 lukem
209 1.3 christos h = mem_alloc(sizeof(*h));
210 1.1 fvdl if (h == NULL) {
211 1.1 fvdl warnx("clnt_vc_create: out of memory");
212 1.1 fvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
213 1.1 fvdl rpc_createerr.cf_error.re_errno = errno;
214 1.1 fvdl goto fooy;
215 1.1 fvdl }
216 1.3 christos ct = mem_alloc(sizeof(*ct));
217 1.1 fvdl if (ct == NULL) {
218 1.1 fvdl warnx("clnt_vc_create: out of memory");
219 1.1 fvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
220 1.1 fvdl rpc_createerr.cf_error.re_errno = errno;
221 1.1 fvdl goto fooy;
222 1.1 fvdl }
223 1.1 fvdl
224 1.23 christos __clnt_sigfillset(&newmask);
225 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
226 1.9 thorpej #ifdef _REENTRANT
227 1.1 fvdl mutex_lock(&clnt_fd_lock);
228 1.15 christos if (vc_fd_locks == NULL) {
229 1.9 thorpej size_t cv_allocsz, fd_allocsz;
230 1.1 fvdl int dtbsize = __rpc_dtbsize();
231 1.1 fvdl
232 1.1 fvdl fd_allocsz = dtbsize * sizeof (int);
233 1.15 christos vc_fd_locks = mem_alloc(fd_allocsz);
234 1.15 christos if (vc_fd_locks == NULL) {
235 1.23 christos goto blooy;
236 1.1 fvdl } else
237 1.1 fvdl memset(vc_fd_locks, '\0', fd_allocsz);
238 1.1 fvdl
239 1.16 uebayasi _DIAGASSERT(vc_cv == NULL);
240 1.1 fvdl cv_allocsz = dtbsize * sizeof (cond_t);
241 1.15 christos vc_cv = mem_alloc(cv_allocsz);
242 1.15 christos if (vc_cv == NULL) {
243 1.1 fvdl mem_free(vc_fd_locks, fd_allocsz);
244 1.15 christos vc_fd_locks = NULL;
245 1.23 christos goto blooy;
246 1.1 fvdl } else {
247 1.1 fvdl int i;
248 1.1 fvdl
249 1.1 fvdl for (i = 0; i < dtbsize; i++)
250 1.1 fvdl cond_init(&vc_cv[i], 0, (void *) 0);
251 1.1 fvdl }
252 1.1 fvdl } else
253 1.16 uebayasi _DIAGASSERT(vc_cv != NULL);
254 1.1 fvdl #endif
255 1.1 fvdl
256 1.1 fvdl /*
257 1.1 fvdl * XXX - fvdl connecting while holding a mutex?
258 1.1 fvdl */
259 1.1 fvdl slen = sizeof ss;
260 1.3 christos if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
261 1.1 fvdl if (errno != ENOTCONN) {
262 1.1 fvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
263 1.1 fvdl rpc_createerr.cf_error.re_errno = errno;
264 1.23 christos goto blooy;
265 1.1 fvdl }
266 1.1 fvdl if (connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){
267 1.1 fvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
268 1.1 fvdl rpc_createerr.cf_error.re_errno = errno;
269 1.23 christos goto blooy;
270 1.1 fvdl }
271 1.1 fvdl }
272 1.1 fvdl mutex_unlock(&clnt_fd_lock);
273 1.14 drochner thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
274 1.1 fvdl if (!__rpc_fd2sockinfo(fd, &si))
275 1.1 fvdl goto fooy;
276 1.1 fvdl
277 1.1 fvdl ct->ct_closeit = FALSE;
278 1.1 fvdl
279 1.1 fvdl /*
280 1.1 fvdl * Set up private data struct
281 1.1 fvdl */
282 1.1 fvdl ct->ct_fd = fd;
283 1.1 fvdl ct->ct_wait.tv_usec = 0;
284 1.1 fvdl ct->ct_waitset = FALSE;
285 1.11 christos ct->ct_addr.buf = malloc((size_t)raddr->maxlen);
286 1.1 fvdl if (ct->ct_addr.buf == NULL)
287 1.1 fvdl goto fooy;
288 1.19 christos memcpy(ct->ct_addr.buf, raddr->buf, (size_t)raddr->len);
289 1.19 christos ct->ct_addr.len = raddr->len;
290 1.1 fvdl ct->ct_addr.maxlen = raddr->maxlen;
291 1.1 fvdl
292 1.1 fvdl /*
293 1.1 fvdl * Initialize call message
294 1.1 fvdl */
295 1.10 itojun call_msg.rm_xid = __RPC_GETXID();
296 1.1 fvdl call_msg.rm_direction = CALL;
297 1.1 fvdl call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
298 1.1 fvdl call_msg.rm_call.cb_prog = (u_int32_t)prog;
299 1.1 fvdl call_msg.rm_call.cb_vers = (u_int32_t)vers;
300 1.1 fvdl
301 1.1 fvdl /*
302 1.1 fvdl * pre-serialize the static part of the call msg and stash it away
303 1.1 fvdl */
304 1.1 fvdl xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
305 1.1 fvdl XDR_ENCODE);
306 1.1 fvdl if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
307 1.1 fvdl if (ct->ct_closeit) {
308 1.1 fvdl (void)close(fd);
309 1.1 fvdl }
310 1.1 fvdl goto fooy;
311 1.1 fvdl }
312 1.1 fvdl ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
313 1.1 fvdl XDR_DESTROY(&(ct->ct_xdrs));
314 1.1 fvdl
315 1.1 fvdl /*
316 1.1 fvdl * Create a client handle which uses xdrrec for serialization
317 1.1 fvdl * and authnone for authentication.
318 1.1 fvdl */
319 1.1 fvdl h->cl_ops = clnt_vc_ops();
320 1.1 fvdl h->cl_private = ct;
321 1.1 fvdl h->cl_auth = authnone_create();
322 1.1 fvdl sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
323 1.1 fvdl recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
324 1.1 fvdl xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
325 1.1 fvdl h->cl_private, read_vc, write_vc);
326 1.1 fvdl return (h);
327 1.1 fvdl
328 1.23 christos blooy:
329 1.23 christos mutex_unlock(&clnt_fd_lock);
330 1.23 christos thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
331 1.1 fvdl fooy:
332 1.1 fvdl /*
333 1.1 fvdl * Something goofed, free stuff and barf
334 1.1 fvdl */
335 1.1 fvdl if (ct)
336 1.1 fvdl mem_free(ct, sizeof(struct ct_data));
337 1.1 fvdl if (h)
338 1.1 fvdl mem_free(h, sizeof(CLIENT));
339 1.3 christos return (NULL);
340 1.1 fvdl }
341 1.1 fvdl
342 1.1 fvdl static enum clnt_stat
343 1.18 christos clnt_vc_call(
344 1.18 christos CLIENT *h,
345 1.18 christos rpcproc_t proc,
346 1.18 christos xdrproc_t xdr_args,
347 1.18 christos const char *args_ptr,
348 1.18 christos xdrproc_t xdr_results,
349 1.18 christos caddr_t results_ptr,
350 1.18 christos struct timeval timeout
351 1.18 christos )
352 1.1 fvdl {
353 1.1 fvdl struct ct_data *ct;
354 1.1 fvdl XDR *xdrs;
355 1.1 fvdl struct rpc_msg reply_msg;
356 1.1 fvdl u_int32_t x_id;
357 1.1 fvdl u_int32_t *msg_x_id;
358 1.1 fvdl bool_t shipnow;
359 1.1 fvdl int refreshes = 2;
360 1.9 thorpej #ifdef _REENTRANT
361 1.1 fvdl sigset_t mask, newmask;
362 1.1 fvdl #endif
363 1.1 fvdl
364 1.1 fvdl _DIAGASSERT(h != NULL);
365 1.1 fvdl
366 1.9 thorpej ct = (struct ct_data *) h->cl_private;
367 1.9 thorpej
368 1.9 thorpej #ifdef _REENTRANT
369 1.23 christos __clnt_sigfillset(&newmask);
370 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
371 1.1 fvdl mutex_lock(&clnt_fd_lock);
372 1.1 fvdl while (vc_fd_locks[ct->ct_fd])
373 1.1 fvdl cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
374 1.9 thorpej vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
375 1.1 fvdl mutex_unlock(&clnt_fd_lock);
376 1.1 fvdl #endif
377 1.1 fvdl
378 1.1 fvdl xdrs = &(ct->ct_xdrs);
379 1.1 fvdl msg_x_id = &ct->ct_u.ct_mcalli;
380 1.1 fvdl
381 1.1 fvdl if (!ct->ct_waitset) {
382 1.1 fvdl if (time_not_ok(&timeout) == FALSE)
383 1.1 fvdl ct->ct_wait = timeout;
384 1.1 fvdl }
385 1.1 fvdl
386 1.1 fvdl shipnow =
387 1.3 christos (xdr_results == NULL && timeout.tv_sec == 0
388 1.1 fvdl && timeout.tv_usec == 0) ? FALSE : TRUE;
389 1.1 fvdl
390 1.1 fvdl call_again:
391 1.1 fvdl xdrs->x_op = XDR_ENCODE;
392 1.1 fvdl ct->ct_error.re_status = RPC_SUCCESS;
393 1.1 fvdl x_id = ntohl(--(*msg_x_id));
394 1.1 fvdl if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) ||
395 1.6 christos (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
396 1.1 fvdl (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
397 1.13 yamt (! (*xdr_args)(xdrs, __UNCONST(args_ptr)))) {
398 1.1 fvdl if (ct->ct_error.re_status == RPC_SUCCESS)
399 1.1 fvdl ct->ct_error.re_status = RPC_CANTENCODEARGS;
400 1.1 fvdl (void)xdrrec_endofrecord(xdrs, TRUE);
401 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
402 1.1 fvdl return (ct->ct_error.re_status);
403 1.1 fvdl }
404 1.1 fvdl if (! xdrrec_endofrecord(xdrs, shipnow)) {
405 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
406 1.1 fvdl return (ct->ct_error.re_status = RPC_CANTSEND);
407 1.1 fvdl }
408 1.1 fvdl if (! shipnow) {
409 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
410 1.1 fvdl return (RPC_SUCCESS);
411 1.1 fvdl }
412 1.1 fvdl /*
413 1.1 fvdl * Hack to provide rpc-based message passing
414 1.1 fvdl */
415 1.1 fvdl if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
416 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
417 1.1 fvdl return(ct->ct_error.re_status = RPC_TIMEDOUT);
418 1.1 fvdl }
419 1.1 fvdl
420 1.1 fvdl
421 1.1 fvdl /*
422 1.1 fvdl * Keep receiving until we get a valid transaction id
423 1.1 fvdl */
424 1.1 fvdl xdrs->x_op = XDR_DECODE;
425 1.1 fvdl for (;;) {
426 1.1 fvdl reply_msg.acpted_rply.ar_verf = _null_auth;
427 1.1 fvdl reply_msg.acpted_rply.ar_results.where = NULL;
428 1.1 fvdl reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
429 1.1 fvdl if (! xdrrec_skiprecord(xdrs)) {
430 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
431 1.1 fvdl return (ct->ct_error.re_status);
432 1.1 fvdl }
433 1.1 fvdl /* now decode and validate the response header */
434 1.1 fvdl if (! xdr_replymsg(xdrs, &reply_msg)) {
435 1.1 fvdl if (ct->ct_error.re_status == RPC_SUCCESS)
436 1.1 fvdl continue;
437 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
438 1.1 fvdl return (ct->ct_error.re_status);
439 1.1 fvdl }
440 1.1 fvdl if (reply_msg.rm_xid == x_id)
441 1.1 fvdl break;
442 1.1 fvdl }
443 1.1 fvdl
444 1.1 fvdl /*
445 1.1 fvdl * process header
446 1.1 fvdl */
447 1.1 fvdl _seterr_reply(&reply_msg, &(ct->ct_error));
448 1.1 fvdl if (ct->ct_error.re_status == RPC_SUCCESS) {
449 1.1 fvdl if (! AUTH_VALIDATE(h->cl_auth,
450 1.1 fvdl &reply_msg.acpted_rply.ar_verf)) {
451 1.1 fvdl ct->ct_error.re_status = RPC_AUTHERROR;
452 1.1 fvdl ct->ct_error.re_why = AUTH_INVALIDRESP;
453 1.1 fvdl } else if (! (*xdr_results)(xdrs, results_ptr)) {
454 1.1 fvdl if (ct->ct_error.re_status == RPC_SUCCESS)
455 1.1 fvdl ct->ct_error.re_status = RPC_CANTDECODERES;
456 1.1 fvdl }
457 1.1 fvdl /* free verifier ... */
458 1.1 fvdl if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
459 1.1 fvdl xdrs->x_op = XDR_FREE;
460 1.1 fvdl (void)xdr_opaque_auth(xdrs,
461 1.1 fvdl &(reply_msg.acpted_rply.ar_verf));
462 1.1 fvdl }
463 1.1 fvdl } /* end successful completion */
464 1.1 fvdl else {
465 1.1 fvdl /* maybe our credentials need to be refreshed ... */
466 1.1 fvdl if (refreshes-- && AUTH_REFRESH(h->cl_auth))
467 1.1 fvdl goto call_again;
468 1.1 fvdl } /* end of unsuccessful completion */
469 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
470 1.1 fvdl return (ct->ct_error.re_status);
471 1.1 fvdl }
472 1.1 fvdl
473 1.1 fvdl static void
474 1.18 christos clnt_vc_geterr(
475 1.18 christos CLIENT *h,
476 1.18 christos struct rpc_err *errp
477 1.18 christos )
478 1.1 fvdl {
479 1.1 fvdl struct ct_data *ct;
480 1.1 fvdl
481 1.1 fvdl _DIAGASSERT(h != NULL);
482 1.1 fvdl _DIAGASSERT(errp != NULL);
483 1.1 fvdl
484 1.1 fvdl ct = (struct ct_data *) h->cl_private;
485 1.1 fvdl *errp = ct->ct_error;
486 1.1 fvdl }
487 1.1 fvdl
488 1.1 fvdl static bool_t
489 1.18 christos clnt_vc_freeres(
490 1.18 christos CLIENT *cl,
491 1.18 christos xdrproc_t xdr_res,
492 1.18 christos caddr_t res_ptr
493 1.18 christos )
494 1.1 fvdl {
495 1.1 fvdl struct ct_data *ct;
496 1.1 fvdl XDR *xdrs;
497 1.1 fvdl bool_t dummy;
498 1.9 thorpej #ifdef _REENTRANT
499 1.1 fvdl sigset_t mask;
500 1.1 fvdl #endif
501 1.1 fvdl sigset_t newmask;
502 1.1 fvdl
503 1.1 fvdl _DIAGASSERT(cl != NULL);
504 1.1 fvdl
505 1.1 fvdl ct = (struct ct_data *)cl->cl_private;
506 1.1 fvdl xdrs = &(ct->ct_xdrs);
507 1.1 fvdl
508 1.23 christos __clnt_sigfillset(&newmask);
509 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
510 1.1 fvdl mutex_lock(&clnt_fd_lock);
511 1.9 thorpej #ifdef _REENTRANT
512 1.1 fvdl while (vc_fd_locks[ct->ct_fd])
513 1.1 fvdl cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
514 1.1 fvdl #endif
515 1.1 fvdl
516 1.1 fvdl xdrs->x_op = XDR_FREE;
517 1.1 fvdl dummy = (*xdr_res)(xdrs, res_ptr);
518 1.1 fvdl mutex_unlock(&clnt_fd_lock);
519 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
520 1.1 fvdl cond_signal(&vc_cv[ct->ct_fd]);
521 1.1 fvdl
522 1.1 fvdl return dummy;
523 1.1 fvdl }
524 1.1 fvdl
525 1.1 fvdl /*ARGSUSED*/
526 1.1 fvdl static void
527 1.18 christos clnt_vc_abort(CLIENT *cl)
528 1.1 fvdl {
529 1.1 fvdl }
530 1.1 fvdl
531 1.1 fvdl static bool_t
532 1.18 christos clnt_vc_control(
533 1.18 christos CLIENT *cl,
534 1.18 christos u_int request,
535 1.18 christos char *info
536 1.18 christos )
537 1.1 fvdl {
538 1.1 fvdl struct ct_data *ct;
539 1.1 fvdl void *infop = info;
540 1.9 thorpej #ifdef _REENTRANT
541 1.1 fvdl sigset_t mask;
542 1.1 fvdl #endif
543 1.1 fvdl sigset_t newmask;
544 1.1 fvdl
545 1.1 fvdl _DIAGASSERT(cl != NULL);
546 1.1 fvdl
547 1.1 fvdl ct = (struct ct_data *)cl->cl_private;
548 1.1 fvdl
549 1.23 christos __clnt_sigfillset(&newmask);
550 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
551 1.1 fvdl mutex_lock(&clnt_fd_lock);
552 1.9 thorpej #ifdef _REENTRANT
553 1.1 fvdl while (vc_fd_locks[ct->ct_fd])
554 1.1 fvdl cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
555 1.1 fvdl vc_fd_locks[ct->ct_fd] = __rpc_lock_value;
556 1.1 fvdl #endif
557 1.1 fvdl mutex_unlock(&clnt_fd_lock);
558 1.1 fvdl
559 1.1 fvdl switch (request) {
560 1.1 fvdl case CLSET_FD_CLOSE:
561 1.1 fvdl ct->ct_closeit = TRUE;
562 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
563 1.1 fvdl return (TRUE);
564 1.1 fvdl case CLSET_FD_NCLOSE:
565 1.1 fvdl ct->ct_closeit = FALSE;
566 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
567 1.1 fvdl return (TRUE);
568 1.1 fvdl default:
569 1.1 fvdl break;
570 1.1 fvdl }
571 1.1 fvdl
572 1.1 fvdl /* for other requests which use info */
573 1.1 fvdl if (info == NULL) {
574 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
575 1.1 fvdl return (FALSE);
576 1.1 fvdl }
577 1.1 fvdl switch (request) {
578 1.1 fvdl case CLSET_TIMEOUT:
579 1.3 christos if (time_not_ok((struct timeval *)(void *)info)) {
580 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
581 1.1 fvdl return (FALSE);
582 1.1 fvdl }
583 1.1 fvdl ct->ct_wait = *(struct timeval *)infop;
584 1.1 fvdl ct->ct_waitset = TRUE;
585 1.1 fvdl break;
586 1.1 fvdl case CLGET_TIMEOUT:
587 1.1 fvdl *(struct timeval *)infop = ct->ct_wait;
588 1.1 fvdl break;
589 1.1 fvdl case CLGET_SERVER_ADDR:
590 1.3 christos (void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len);
591 1.1 fvdl break;
592 1.1 fvdl case CLGET_FD:
593 1.3 christos *(int *)(void *)info = ct->ct_fd;
594 1.1 fvdl break;
595 1.1 fvdl case CLGET_SVC_ADDR:
596 1.1 fvdl /* The caller should not free this memory area */
597 1.3 christos *(struct netbuf *)(void *)info = ct->ct_addr;
598 1.1 fvdl break;
599 1.1 fvdl case CLSET_SVC_ADDR: /* set to new address */
600 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
601 1.1 fvdl return (FALSE);
602 1.1 fvdl case CLGET_XID:
603 1.1 fvdl /*
604 1.1 fvdl * use the knowledge that xid is the
605 1.1 fvdl * first element in the call structure
606 1.1 fvdl * This will get the xid of the PREVIOUS call
607 1.1 fvdl */
608 1.24 christos ntohlp(info, &ct->ct_u.ct_mcalli);
609 1.1 fvdl break;
610 1.1 fvdl case CLSET_XID:
611 1.1 fvdl /* This will set the xid of the NEXT call */
612 1.1 fvdl /* increment by 1 as clnt_vc_call() decrements once */
613 1.25 christos htonlp(&ct->ct_u.ct_mcalli, info, 1);
614 1.1 fvdl break;
615 1.1 fvdl case CLGET_VERS:
616 1.1 fvdl /*
617 1.1 fvdl * This RELIES on the information that, in the call body,
618 1.1 fvdl * the version number field is the fifth field from the
619 1.28 andvar * beginning of the RPC header. MUST be changed if the
620 1.1 fvdl * call_struct is changed
621 1.1 fvdl */
622 1.24 christos ntohlp(info, ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT);
623 1.1 fvdl break;
624 1.1 fvdl
625 1.1 fvdl case CLSET_VERS:
626 1.25 christos htonlp(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT, info, 0);
627 1.1 fvdl break;
628 1.1 fvdl
629 1.1 fvdl case CLGET_PROG:
630 1.1 fvdl /*
631 1.1 fvdl * This RELIES on the information that, in the call body,
632 1.1 fvdl * the program number field is the fourth field from the
633 1.28 andvar * beginning of the RPC header. MUST be changed if the
634 1.1 fvdl * call_struct is changed
635 1.1 fvdl */
636 1.24 christos ntohlp(info, ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT);
637 1.1 fvdl break;
638 1.1 fvdl
639 1.1 fvdl case CLSET_PROG:
640 1.25 christos htonlp(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT, info, 0);
641 1.1 fvdl break;
642 1.1 fvdl
643 1.1 fvdl default:
644 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
645 1.1 fvdl return (FALSE);
646 1.1 fvdl }
647 1.1 fvdl release_fd_lock(ct->ct_fd, mask);
648 1.1 fvdl return (TRUE);
649 1.1 fvdl }
650 1.1 fvdl
651 1.1 fvdl
652 1.1 fvdl static void
653 1.18 christos clnt_vc_destroy(CLIENT *cl)
654 1.1 fvdl {
655 1.1 fvdl struct ct_data *ct;
656 1.9 thorpej #ifdef _REENTRANT
657 1.9 thorpej int ct_fd;
658 1.1 fvdl sigset_t mask;
659 1.1 fvdl #endif
660 1.1 fvdl sigset_t newmask;
661 1.1 fvdl
662 1.1 fvdl _DIAGASSERT(cl != NULL);
663 1.1 fvdl
664 1.1 fvdl ct = (struct ct_data *) cl->cl_private;
665 1.1 fvdl
666 1.23 christos __clnt_sigfillset(&newmask);
667 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
668 1.1 fvdl mutex_lock(&clnt_fd_lock);
669 1.9 thorpej #ifdef _REENTRANT
670 1.26 christos ct_fd = ct->ct_fd;
671 1.1 fvdl while (vc_fd_locks[ct_fd])
672 1.1 fvdl cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
673 1.1 fvdl #endif
674 1.1 fvdl if (ct->ct_closeit && ct->ct_fd != -1) {
675 1.1 fvdl (void)close(ct->ct_fd);
676 1.1 fvdl }
677 1.1 fvdl XDR_DESTROY(&(ct->ct_xdrs));
678 1.1 fvdl if (ct->ct_addr.buf)
679 1.1 fvdl free(ct->ct_addr.buf);
680 1.1 fvdl mem_free(ct, sizeof(struct ct_data));
681 1.1 fvdl mem_free(cl, sizeof(CLIENT));
682 1.1 fvdl mutex_unlock(&clnt_fd_lock);
683 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
684 1.1 fvdl
685 1.1 fvdl cond_signal(&vc_cv[ct_fd]);
686 1.1 fvdl }
687 1.1 fvdl
688 1.1 fvdl /*
689 1.1 fvdl * Interface between xdr serializer and tcp connection.
690 1.1 fvdl * Behaves like the system calls, read & write, but keeps some error state
691 1.1 fvdl * around for the rpc level.
692 1.1 fvdl */
693 1.1 fvdl static int
694 1.18 christos read_vc(char *ctp, char *buf, int len)
695 1.1 fvdl {
696 1.1 fvdl struct ct_data *ct = (struct ct_data *)(void *)ctp;
697 1.1 fvdl struct pollfd fd;
698 1.12 christos struct timespec ts;
699 1.18 christos ssize_t nread;
700 1.1 fvdl
701 1.1 fvdl if (len == 0)
702 1.1 fvdl return (0);
703 1.12 christos
704 1.12 christos TIMEVAL_TO_TIMESPEC(&ct->ct_wait, &ts);
705 1.1 fvdl fd.fd = ct->ct_fd;
706 1.1 fvdl fd.events = POLLIN;
707 1.1 fvdl for (;;) {
708 1.12 christos switch (pollts(&fd, 1, &ts, NULL)) {
709 1.1 fvdl case 0:
710 1.1 fvdl ct->ct_error.re_status = RPC_TIMEDOUT;
711 1.1 fvdl return (-1);
712 1.1 fvdl
713 1.1 fvdl case -1:
714 1.1 fvdl if (errno == EINTR)
715 1.1 fvdl continue;
716 1.1 fvdl ct->ct_error.re_status = RPC_CANTRECV;
717 1.1 fvdl ct->ct_error.re_errno = errno;
718 1.1 fvdl return (-1);
719 1.1 fvdl }
720 1.1 fvdl break;
721 1.1 fvdl }
722 1.18 christos switch (nread = read(ct->ct_fd, buf, (size_t)len)) {
723 1.1 fvdl
724 1.1 fvdl case 0:
725 1.1 fvdl /* premature eof */
726 1.1 fvdl ct->ct_error.re_errno = ECONNRESET;
727 1.1 fvdl ct->ct_error.re_status = RPC_CANTRECV;
728 1.22 christos nread = -1; /* it's really an error */
729 1.1 fvdl break;
730 1.1 fvdl
731 1.1 fvdl case -1:
732 1.1 fvdl ct->ct_error.re_errno = errno;
733 1.1 fvdl ct->ct_error.re_status = RPC_CANTRECV;
734 1.1 fvdl break;
735 1.1 fvdl }
736 1.18 christos return (int)nread;
737 1.1 fvdl }
738 1.1 fvdl
739 1.1 fvdl static int
740 1.18 christos write_vc(char *ctp, char *buf, int len)
741 1.1 fvdl {
742 1.1 fvdl struct ct_data *ct = (struct ct_data *)(void *)ctp;
743 1.18 christos ssize_t i;
744 1.18 christos size_t cnt;
745 1.1 fvdl
746 1.1 fvdl for (cnt = len; cnt > 0; cnt -= i, buf += i) {
747 1.18 christos if ((i = write(ct->ct_fd, buf, cnt)) == -1) {
748 1.1 fvdl ct->ct_error.re_errno = errno;
749 1.1 fvdl ct->ct_error.re_status = RPC_CANTSEND;
750 1.1 fvdl return (-1);
751 1.1 fvdl }
752 1.1 fvdl }
753 1.18 christos return len;
754 1.1 fvdl }
755 1.1 fvdl
756 1.1 fvdl static struct clnt_ops *
757 1.18 christos clnt_vc_ops(void)
758 1.1 fvdl {
759 1.1 fvdl static struct clnt_ops ops;
760 1.9 thorpej #ifdef _REENTRANT
761 1.1 fvdl extern mutex_t ops_lock;
762 1.1 fvdl sigset_t mask;
763 1.1 fvdl #endif
764 1.1 fvdl sigset_t newmask;
765 1.1 fvdl
766 1.1 fvdl /* VARIABLES PROTECTED BY ops_lock: ops */
767 1.1 fvdl
768 1.23 christos __clnt_sigfillset(&newmask);
769 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
770 1.1 fvdl mutex_lock(&ops_lock);
771 1.1 fvdl if (ops.cl_call == NULL) {
772 1.1 fvdl ops.cl_call = clnt_vc_call;
773 1.1 fvdl ops.cl_abort = clnt_vc_abort;
774 1.1 fvdl ops.cl_geterr = clnt_vc_geterr;
775 1.1 fvdl ops.cl_freeres = clnt_vc_freeres;
776 1.1 fvdl ops.cl_destroy = clnt_vc_destroy;
777 1.1 fvdl ops.cl_control = clnt_vc_control;
778 1.1 fvdl }
779 1.1 fvdl mutex_unlock(&ops_lock);
780 1.1 fvdl thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
781 1.1 fvdl return (&ops);
782 1.1 fvdl }
783 1.1 fvdl
784 1.1 fvdl /*
785 1.1 fvdl * Make sure that the time is not garbage. -1 value is disallowed.
786 1.1 fvdl * Note this is different from time_not_ok in clnt_dg.c
787 1.1 fvdl */
788 1.1 fvdl static bool_t
789 1.18 christos time_not_ok(struct timeval *t)
790 1.1 fvdl {
791 1.7 lukem
792 1.7 lukem _DIAGASSERT(t != NULL);
793 1.7 lukem
794 1.1 fvdl return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
795 1.1 fvdl t->tv_usec <= -1 || t->tv_usec > 1000000);
796 1.1 fvdl }
797