support_become_root: Enable file creation in user namespaces
[glibc.git] / sunrpc / clnt_raw.c
blobd62a11a2cadf3fab4e52dc26906b34a6d97d5801
1 /*
2 * clnt_raw.c
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.
33 * Memory based rpc for simple testing and timing.
34 * Interface to create an rpc client and server in the same process.
35 * This lets us simulate rpc and get round trip overhead, without
36 * any interference from the kernel.
39 #include <rpc/rpc.h>
40 #include <rpc/svc.h>
41 #include <rpc/xdr.h>
42 #include <libintl.h>
43 #include <shlib-compat.h>
45 #define MCALL_MSG_SIZE 24
48 * This is the "network" we will be moving stuff over.
50 struct clntraw_private_s
52 CLIENT client_object;
53 XDR xdr_stream;
54 char _raw_buf[UDPMSGSIZE];
55 union
57 char msg[MCALL_MSG_SIZE];
58 u_long rm_xid;
59 } mashl_callmsg;
60 u_int mcnt;
62 #ifdef _RPC_THREAD_SAFE_
63 #define clntraw_private RPC_THREAD_VARIABLE(clntraw_private_s)
64 #else
65 static struct clntraw_private_s *clntraw_private;
66 #endif
68 static enum clnt_stat clntraw_call (CLIENT *, u_long, xdrproc_t, caddr_t,
69 xdrproc_t, caddr_t, struct timeval);
70 static void clntraw_abort (void);
71 static void clntraw_geterr (CLIENT *, struct rpc_err *);
72 static bool_t clntraw_freeres (CLIENT *, xdrproc_t, caddr_t);
73 static bool_t clntraw_control (CLIENT *, int, char *);
74 static void clntraw_destroy (CLIENT *);
76 static const struct clnt_ops client_ops =
78 clntraw_call,
79 clntraw_abort,
80 clntraw_geterr,
81 clntraw_freeres,
82 clntraw_destroy,
83 clntraw_control
87 * Create a client handle for memory based rpc.
89 CLIENT *
90 clntraw_create (u_long prog, u_long vers)
92 struct clntraw_private_s *clp = clntraw_private;
93 struct rpc_msg call_msg;
94 XDR *xdrs;
95 CLIENT *client;
97 if (clp == 0)
99 clp = (struct clntraw_private_s *) calloc (1, sizeof (*clp));
100 if (clp == 0)
101 return (0);
102 clntraw_private = clp;
104 xdrs = &clp->xdr_stream;
105 client = &clp->client_object;
107 * pre-serialize the static part of the call msg and stash it away
109 call_msg.rm_direction = CALL;
110 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
111 call_msg.rm_call.cb_prog = prog;
112 call_msg.rm_call.cb_vers = vers;
113 xdrmem_create (xdrs, clp->mashl_callmsg.msg, MCALL_MSG_SIZE, XDR_ENCODE);
114 if (!xdr_callhdr (xdrs, &call_msg))
116 perror (_ ("clnt_raw.c: fatal header serialization error"));
118 clp->mcnt = XDR_GETPOS (xdrs);
119 XDR_DESTROY (xdrs);
122 * Set xdrmem for client/server shared buffer
124 xdrmem_create (xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
127 * create client handle
129 client->cl_ops = (struct clnt_ops *) &client_ops;
130 client->cl_auth = authnone_create ();
131 return client;
133 libc_hidden_nolink_sunrpc (clntraw_create, GLIBC_2_0)
135 static enum clnt_stat
136 clntraw_call (CLIENT *h, u_long proc, xdrproc_t xargs, caddr_t argsp,
137 xdrproc_t xresults, caddr_t resultsp, struct timeval timeout)
139 struct clntraw_private_s *clp = clntraw_private;
140 XDR *xdrs = &clp->xdr_stream;
141 struct rpc_msg msg;
142 enum clnt_stat status;
143 struct rpc_err error;
145 if (clp == NULL)
146 return RPC_FAILED;
147 call_again:
149 * send request
151 xdrs->x_op = XDR_ENCODE;
152 XDR_SETPOS (xdrs, 0);
153 /* Just checking the union definition to access rm_xid is correct. */
154 if (offsetof (struct rpc_msg, rm_xid) != 0)
155 abort ();
156 clp->mashl_callmsg.rm_xid++;
157 if ((!XDR_PUTBYTES (xdrs, clp->mashl_callmsg.msg, clp->mcnt)) ||
158 (!XDR_PUTLONG (xdrs, (long *) &proc)) ||
159 (!AUTH_MARSHALL (h->cl_auth, xdrs)) ||
160 (!(*xargs) (xdrs, argsp)))
162 return (RPC_CANTENCODEARGS);
164 (void) XDR_GETPOS (xdrs); /* called just to cause overhead */
167 * We have to call server input routine here because this is
168 * all going on in one process. Yuk.
170 svc_getreq (1);
173 * get results
175 xdrs->x_op = XDR_DECODE;
176 XDR_SETPOS (xdrs, 0);
177 msg.acpted_rply.ar_verf = _null_auth;
178 msg.acpted_rply.ar_results.where = resultsp;
179 msg.acpted_rply.ar_results.proc = xresults;
180 if (!xdr_replymsg (xdrs, &msg))
181 return RPC_CANTDECODERES;
182 _seterr_reply (&msg, &error);
183 status = error.re_status;
185 if (status == RPC_SUCCESS)
187 if (!AUTH_VALIDATE (h->cl_auth, &msg.acpted_rply.ar_verf))
189 status = RPC_AUTHERROR;
191 } /* end successful completion */
192 else
194 if (AUTH_REFRESH (h->cl_auth))
195 goto call_again;
196 } /* end of unsuccessful completion */
198 if (status == RPC_SUCCESS)
200 if (!AUTH_VALIDATE (h->cl_auth, &msg.acpted_rply.ar_verf))
202 status = RPC_AUTHERROR;
204 if (msg.acpted_rply.ar_verf.oa_base != NULL)
206 xdrs->x_op = XDR_FREE;
207 (void) xdr_opaque_auth (xdrs, &(msg.acpted_rply.ar_verf));
211 return status;
214 static void
215 clntraw_geterr (CLIENT *cl, struct rpc_err *err)
220 static bool_t
221 clntraw_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
223 struct clntraw_private_s *clp = clntraw_private;
224 XDR *xdrs = &clp->xdr_stream;
225 bool_t rval;
227 if (clp == NULL)
229 rval = (bool_t) RPC_FAILED;
230 return rval;
232 xdrs->x_op = XDR_FREE;
233 return (*xdr_res) (xdrs, res_ptr);
236 static void
237 clntraw_abort (void)
241 static bool_t
242 clntraw_control (CLIENT *cl, int i, char *c)
244 return FALSE;
247 static void
248 clntraw_destroy (CLIENT *cl)