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