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