tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / rpc / clnt_raw.c
blob556e91c0558bf17b86f889f77284110efcf282d7
1 /* $NetBSD: clnt_raw.c,v 1.32 2013/03/11 20:19:29 tron Exp $ */
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC";
39 #else
40 __RCSID("$NetBSD: clnt_raw.c,v 1.32 2013/03/11 20:19:29 tron Exp $");
41 #endif
42 #endif
45 * clnt_raw.c
47 * Copyright (C) 1984, Sun Microsystems, Inc.
49 * Memory based rpc for simple testing and timing.
50 * Interface to create an rpc client and server in the same process.
51 * This lets us similate rpc and get round trip overhead, without
52 * any interference from the kernel.
55 #include "namespace.h"
56 #include "reentrant.h"
57 #include <assert.h>
58 #include <err.h>
59 #include <stdio.h>
60 #include <stdlib.h>
62 #include <rpc/rpc.h>
63 #include <rpc/raw.h>
65 #ifdef __weak_alias
66 __weak_alias(clntraw_create,_clntraw_create)
67 __weak_alias(clnt_raw_create,_clnt_raw_create)
68 #endif
70 #ifdef _REENTRANT
71 extern mutex_t clntraw_lock;
72 #endif
74 #define MCALL_MSG_SIZE 24
77 * This is the "network" we will be moving stuff over.
79 static struct clntraw_private {
80 CLIENT client_object;
81 XDR xdr_stream;
82 char *_raw_buf;
83 union {
84 struct rpc_msg mashl_rpcmsg;
85 char mashl_callmsg[MCALL_MSG_SIZE];
86 } u;
87 u_int mcnt;
88 } *clntraw_private;
90 static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t,
91 const char *, xdrproc_t, caddr_t, struct timeval);
92 static void clnt_raw_geterr(CLIENT *, struct rpc_err *);
93 static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, caddr_t);
94 static void clnt_raw_abort(CLIENT *);
95 static bool_t clnt_raw_control(CLIENT *, u_int, char *);
96 static void clnt_raw_destroy(CLIENT *);
97 static struct clnt_ops *clnt_raw_ops(void);
100 * Create a client handle for memory based rpc.
102 CLIENT *
103 clnt_raw_create(rpcprog_t prog, rpcvers_t vers)
105 struct clntraw_private *clp = clntraw_private;
106 struct rpc_msg call_msg;
107 XDR *xdrs = &clp->xdr_stream;
108 CLIENT *client = &clp->client_object;
110 mutex_lock(&clntraw_lock);
111 if (clp == NULL) {
112 clp = calloc((size_t)1, sizeof (*clp));
113 if (clp == NULL)
114 goto out;
115 if (__rpc_rawcombuf == NULL)
116 __rpc_rawcombuf =
117 malloc(UDPMSGSIZE);
118 if (__rpc_rawcombuf == NULL)
119 goto out;
120 clp->_raw_buf = __rpc_rawcombuf;
121 clntraw_private = clp;
124 * pre-serialize the static part of the call msg and stash it away
126 call_msg.rm_direction = CALL;
127 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
128 /* XXX: prog and vers have been long historically :-( */
129 call_msg.rm_call.cb_prog = (u_int32_t)prog;
130 call_msg.rm_call.cb_vers = (u_int32_t)vers;
131 xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
132 if (! xdr_callhdr(xdrs, &call_msg))
133 warnx("%s: Fatal header serialization error", __func__);
134 clp->mcnt = XDR_GETPOS(xdrs);
135 XDR_DESTROY(xdrs);
138 * Set xdrmem for client/server shared buffer
140 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
143 * create client handle
145 client->cl_ops = clnt_raw_ops();
146 client->cl_auth = authnone_create();
147 mutex_unlock(&clntraw_lock);
148 return (client);
149 out:
150 if (clp)
151 free(clp);
152 mutex_unlock(&clntraw_lock);
153 return NULL;
157 /* ARGSUSED */
158 static enum clnt_stat
159 clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, const char *argsp,
160 xdrproc_t xresults, caddr_t resultsp, struct timeval timeout)
162 struct clntraw_private *clp = clntraw_private;
163 XDR *xdrs = &clp->xdr_stream;
164 struct rpc_msg msg;
165 enum clnt_stat status;
166 struct rpc_err error;
168 _DIAGASSERT(h != NULL);
170 mutex_lock(&clntraw_lock);
171 if (clp == NULL) {
172 mutex_unlock(&clntraw_lock);
173 return (RPC_FAILED);
175 mutex_unlock(&clntraw_lock);
177 call_again:
179 * send request
181 xdrs->x_op = XDR_ENCODE;
182 XDR_SETPOS(xdrs, 0);
183 clp->u.mashl_rpcmsg.rm_xid ++ ;
184 if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
185 (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
186 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
187 (! (*xargs)(xdrs, __UNCONST(argsp)))) {
188 return (RPC_CANTENCODEARGS);
190 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */
193 * We have to call server input routine here because this is
194 * all going on in one process. Yuk.
196 svc_getreq_common(FD_SETSIZE);
199 * get results
201 xdrs->x_op = XDR_DECODE;
202 XDR_SETPOS(xdrs, 0);
203 msg.acpted_rply.ar_verf = _null_auth;
204 msg.acpted_rply.ar_results.where = resultsp;
205 msg.acpted_rply.ar_results.proc = xresults;
206 if (! xdr_replymsg(xdrs, &msg)) {
208 * It's possible for xdr_replymsg() to fail partway
209 * through its attempt to decode the result from the
210 * server. If this happens, it will leave the reply
211 * structure partially populated with dynamically
212 * allocated memory. (This can happen if someone uses
213 * clntudp_bufcreate() to create a CLIENT handle and
214 * specifies a receive buffer size that is too small.)
215 * This memory must be free()ed to avoid a leak.
217 int op = xdrs->x_op;
218 xdrs->x_op = XDR_FREE;
219 xdr_replymsg(xdrs, &msg);
220 xdrs->x_op = op;
221 return (RPC_CANTDECODERES);
223 _seterr_reply(&msg, &error);
224 status = error.re_status;
226 if (status == RPC_SUCCESS) {
227 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
228 status = RPC_AUTHERROR;
230 } /* end successful completion */
231 else {
232 if (AUTH_REFRESH(h->cl_auth))
233 goto call_again;
234 } /* end of unsuccessful completion */
236 if (status == RPC_SUCCESS) {
237 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
238 status = RPC_AUTHERROR;
240 if (msg.acpted_rply.ar_verf.oa_base != NULL) {
241 xdrs->x_op = XDR_FREE;
242 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
246 return (status);
249 /*ARGSUSED*/
250 static void
251 clnt_raw_geterr(CLIENT *cl, struct rpc_err *error)
256 /* ARGSUSED */
257 static bool_t
258 clnt_raw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
260 struct clntraw_private *clp = clntraw_private;
261 XDR *xdrs = &clp->xdr_stream;
262 bool_t rval;
264 mutex_lock(&clntraw_lock);
265 if (clp == NULL) {
266 rval = (bool_t) RPC_FAILED;
267 mutex_unlock(&clntraw_lock);
268 return (rval);
270 mutex_unlock(&clntraw_lock);
271 xdrs->x_op = XDR_FREE;
272 return ((*xdr_res)(xdrs, res_ptr));
275 /*ARGSUSED*/
276 static void
277 clnt_raw_abort(CLIENT *cl)
281 /*ARGSUSED*/
282 static bool_t
283 clnt_raw_control(CLIENT *cl, u_int ui, char *str)
285 return (FALSE);
288 /*ARGSUSED*/
289 static void
290 clnt_raw_destroy(CLIENT *cl)
294 static struct clnt_ops *
295 clnt_raw_ops(void)
297 static struct clnt_ops ops;
298 #ifdef _REENTRANT
299 extern mutex_t ops_lock;
300 #endif
302 /* VARIABLES PROTECTED BY ops_lock: ops */
304 mutex_lock(&ops_lock);
305 if (ops.cl_call == NULL) {
306 ops.cl_call = clnt_raw_call;
307 ops.cl_abort = clnt_raw_abort;
308 ops.cl_geterr = clnt_raw_geterr;
309 ops.cl_freeres = clnt_raw_freeres;
310 ops.cl_destroy = clnt_raw_destroy;
311 ops.cl_control = clnt_raw_control;
313 mutex_unlock(&ops_lock);
314 return (&ops);