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