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