1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* $NetBSD: svc_simple.c,v 1.20 2000/07/06 03:10:35 christos Exp $ */ |
---|
4 | |
---|
5 | /*- |
---|
6 | * Copyright (c) 2009, Sun Microsystems, Inc. |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * - Redistributions of source code must retain the above copyright notice, |
---|
12 | * this list of conditions and the following disclaimer. |
---|
13 | * - Redistributions in binary form must reproduce the above copyright notice, |
---|
14 | * this list of conditions and the following disclaimer in the documentation |
---|
15 | * and/or other materials provided with the distribution. |
---|
16 | * - Neither the name of Sun Microsystems, 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. |
---|
19 | * |
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
30 | * POSSIBILITY OF SUCH DAMAGE. |
---|
31 | */ |
---|
32 | /* |
---|
33 | * Copyright (c) 1986-1991 by Sun Microsystems Inc. |
---|
34 | */ |
---|
35 | |
---|
36 | /* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */ |
---|
37 | #include <sys/cdefs.h> |
---|
38 | __FBSDID("$FreeBSD$"); |
---|
39 | |
---|
40 | /* |
---|
41 | * svc_simple.c |
---|
42 | * Simplified front end to rpc. |
---|
43 | */ |
---|
44 | |
---|
45 | /* |
---|
46 | * This interface creates a virtual listener for all the services |
---|
47 | * started thru rpc_reg(). It listens on the same endpoint for |
---|
48 | * all the services and then executes the corresponding service |
---|
49 | * for the given prognum and procnum. |
---|
50 | */ |
---|
51 | |
---|
52 | #include "namespace.h" |
---|
53 | #include "reentrant.h" |
---|
54 | #include <sys/types.h> |
---|
55 | #include <rpc/rpc.h> |
---|
56 | #include <rpc/nettype.h> |
---|
57 | #include <stdio.h> |
---|
58 | #include <stdlib.h> |
---|
59 | #include <string.h> |
---|
60 | #include <err.h> |
---|
61 | #include "un-namespace.h" |
---|
62 | |
---|
63 | #include "rpc_com.h" |
---|
64 | #include "mt_misc.h" |
---|
65 | |
---|
66 | static void universal(struct svc_req *, SVCXPRT *); |
---|
67 | |
---|
68 | static struct proglst { |
---|
69 | char *(*p_progname)(char *); |
---|
70 | rpcprog_t p_prognum; |
---|
71 | rpcvers_t p_versnum; |
---|
72 | rpcproc_t p_procnum; |
---|
73 | SVCXPRT *p_transp; |
---|
74 | char *p_netid; |
---|
75 | char *p_xdrbuf; |
---|
76 | int p_recvsz; |
---|
77 | xdrproc_t p_inproc, p_outproc; |
---|
78 | struct proglst *p_nxt; |
---|
79 | } *proglst; |
---|
80 | |
---|
81 | static const char rpc_reg_err[] = "%s: %s"; |
---|
82 | static const char rpc_reg_msg[] = "rpc_reg: "; |
---|
83 | static const char __reg_err1[] = "can't find appropriate transport"; |
---|
84 | static const char __reg_err2[] = "can't get protocol info"; |
---|
85 | static const char __reg_err3[] = "unsupported transport size"; |
---|
86 | static const char __no_mem_str[] = "out of memory"; |
---|
87 | |
---|
88 | /* |
---|
89 | * For simplified, easy to use kind of rpc interfaces. |
---|
90 | * nettype indicates the type of transport on which the service will be |
---|
91 | * listening. Used for conservation of the system resource. Only one |
---|
92 | * handle is created for all the services (actually one of each netid) |
---|
93 | * and same xdrbuf is used for same netid. The size of the arguments |
---|
94 | * is also limited by the recvsize for that transport, even if it is |
---|
95 | * a COTS transport. This may be wrong, but for cases like these, they |
---|
96 | * should not use the simplified interfaces like this. |
---|
97 | */ |
---|
98 | |
---|
99 | int |
---|
100 | rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype) |
---|
101 | rpcprog_t prognum; /* program number */ |
---|
102 | rpcvers_t versnum; /* version number */ |
---|
103 | rpcproc_t procnum; /* procedure number */ |
---|
104 | char *(*progname)(char *); /* Server routine */ |
---|
105 | xdrproc_t inproc, outproc; /* in/out XDR procedures */ |
---|
106 | char *nettype; /* nettype */ |
---|
107 | { |
---|
108 | struct netconfig *nconf; |
---|
109 | int done = FALSE; |
---|
110 | void *handle; |
---|
111 | |
---|
112 | |
---|
113 | if (procnum == NULLPROC) { |
---|
114 | warnx("%s can't reassign procedure number %u", rpc_reg_msg, |
---|
115 | NULLPROC); |
---|
116 | return (-1); |
---|
117 | } |
---|
118 | |
---|
119 | if (nettype == NULL) |
---|
120 | nettype = "netpath"; /* The default behavior */ |
---|
121 | if ((handle = __rpc_setconf(nettype)) == NULL) { |
---|
122 | warnx(rpc_reg_err, rpc_reg_msg, __reg_err1); |
---|
123 | return (-1); |
---|
124 | } |
---|
125 | /* VARIABLES PROTECTED BY proglst_lock: proglst */ |
---|
126 | mutex_lock(&proglst_lock); |
---|
127 | while ((nconf = __rpc_getconf(handle)) != NULL) { |
---|
128 | struct proglst *pl; |
---|
129 | SVCXPRT *svcxprt; |
---|
130 | int madenow; |
---|
131 | u_int recvsz; |
---|
132 | char *xdrbuf; |
---|
133 | char *netid; |
---|
134 | |
---|
135 | madenow = FALSE; |
---|
136 | svcxprt = NULL; |
---|
137 | recvsz = 0; |
---|
138 | xdrbuf = netid = NULL; |
---|
139 | for (pl = proglst; pl; pl = pl->p_nxt) { |
---|
140 | if (strcmp(pl->p_netid, nconf->nc_netid) == 0) { |
---|
141 | svcxprt = pl->p_transp; |
---|
142 | xdrbuf = pl->p_xdrbuf; |
---|
143 | recvsz = pl->p_recvsz; |
---|
144 | netid = pl->p_netid; |
---|
145 | break; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | if (svcxprt == NULL) { |
---|
150 | struct __rpc_sockinfo si; |
---|
151 | |
---|
152 | svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); |
---|
153 | if (svcxprt == NULL) |
---|
154 | continue; |
---|
155 | if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) { |
---|
156 | warnx(rpc_reg_err, rpc_reg_msg, __reg_err2); |
---|
157 | SVC_DESTROY(svcxprt); |
---|
158 | continue; |
---|
159 | } |
---|
160 | recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0); |
---|
161 | if (recvsz == 0) { |
---|
162 | warnx(rpc_reg_err, rpc_reg_msg, __reg_err3); |
---|
163 | SVC_DESTROY(svcxprt); |
---|
164 | continue; |
---|
165 | } |
---|
166 | if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) || |
---|
167 | ((netid = strdup(nconf->nc_netid)) == NULL)) { |
---|
168 | warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str); |
---|
169 | if (xdrbuf != NULL) |
---|
170 | free(xdrbuf); |
---|
171 | if (netid != NULL) |
---|
172 | free(netid); |
---|
173 | SVC_DESTROY(svcxprt); |
---|
174 | break; |
---|
175 | } |
---|
176 | madenow = TRUE; |
---|
177 | } |
---|
178 | /* |
---|
179 | * Check if this (program, version, netid) had already been |
---|
180 | * registered. The check may save a few RPC calls to rpcbind |
---|
181 | */ |
---|
182 | for (pl = proglst; pl; pl = pl->p_nxt) |
---|
183 | if ((pl->p_prognum == prognum) && |
---|
184 | (pl->p_versnum == versnum) && |
---|
185 | (strcmp(pl->p_netid, netid) == 0)) |
---|
186 | break; |
---|
187 | if (pl == NULL) { /* Not yet */ |
---|
188 | (void) rpcb_unset(prognum, versnum, nconf); |
---|
189 | } else { |
---|
190 | /* so that svc_reg does not call rpcb_set() */ |
---|
191 | nconf = NULL; |
---|
192 | } |
---|
193 | |
---|
194 | if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) { |
---|
195 | warnx("%s couldn't register prog %u vers %u for %s", |
---|
196 | rpc_reg_msg, (unsigned)prognum, |
---|
197 | (unsigned)versnum, netid); |
---|
198 | if (madenow) { |
---|
199 | SVC_DESTROY(svcxprt); |
---|
200 | free(xdrbuf); |
---|
201 | free(netid); |
---|
202 | } |
---|
203 | continue; |
---|
204 | } |
---|
205 | |
---|
206 | pl = malloc(sizeof (struct proglst)); |
---|
207 | if (pl == NULL) { |
---|
208 | warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str); |
---|
209 | if (madenow) { |
---|
210 | SVC_DESTROY(svcxprt); |
---|
211 | free(xdrbuf); |
---|
212 | free(netid); |
---|
213 | } |
---|
214 | break; |
---|
215 | } |
---|
216 | pl->p_progname = progname; |
---|
217 | pl->p_prognum = prognum; |
---|
218 | pl->p_versnum = versnum; |
---|
219 | pl->p_procnum = procnum; |
---|
220 | pl->p_inproc = inproc; |
---|
221 | pl->p_outproc = outproc; |
---|
222 | pl->p_transp = svcxprt; |
---|
223 | pl->p_xdrbuf = xdrbuf; |
---|
224 | pl->p_recvsz = recvsz; |
---|
225 | pl->p_netid = netid; |
---|
226 | pl->p_nxt = proglst; |
---|
227 | proglst = pl; |
---|
228 | done = TRUE; |
---|
229 | } |
---|
230 | __rpc_endconf(handle); |
---|
231 | mutex_unlock(&proglst_lock); |
---|
232 | |
---|
233 | if (done == FALSE) { |
---|
234 | warnx("%s cant find suitable transport for %s", |
---|
235 | rpc_reg_msg, nettype); |
---|
236 | return (-1); |
---|
237 | } |
---|
238 | return (0); |
---|
239 | } |
---|
240 | |
---|
241 | /* |
---|
242 | * The universal handler for the services registered using registerrpc. |
---|
243 | * It handles both the connectionless and the connection oriented cases. |
---|
244 | */ |
---|
245 | |
---|
246 | static void |
---|
247 | universal(rqstp, transp) |
---|
248 | struct svc_req *rqstp; |
---|
249 | SVCXPRT *transp; |
---|
250 | { |
---|
251 | rpcprog_t prog; |
---|
252 | rpcvers_t vers; |
---|
253 | rpcproc_t proc; |
---|
254 | char *outdata; |
---|
255 | char *xdrbuf; |
---|
256 | struct proglst *pl; |
---|
257 | |
---|
258 | /* |
---|
259 | * enforce "procnum 0 is echo" convention |
---|
260 | */ |
---|
261 | if (rqstp->rq_proc == NULLPROC) { |
---|
262 | if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) == |
---|
263 | FALSE) { |
---|
264 | warnx("svc_sendreply failed"); |
---|
265 | } |
---|
266 | return; |
---|
267 | } |
---|
268 | prog = rqstp->rq_prog; |
---|
269 | vers = rqstp->rq_vers; |
---|
270 | proc = rqstp->rq_proc; |
---|
271 | mutex_lock(&proglst_lock); |
---|
272 | for (pl = proglst; pl; pl = pl->p_nxt) |
---|
273 | if (pl->p_prognum == prog && pl->p_procnum == proc && |
---|
274 | pl->p_versnum == vers && |
---|
275 | (strcmp(pl->p_netid, transp->xp_netid) == 0)) { |
---|
276 | /* decode arguments into a CLEAN buffer */ |
---|
277 | xdrbuf = pl->p_xdrbuf; |
---|
278 | /* Zero the arguments: reqd ! */ |
---|
279 | (void) memset(xdrbuf, 0, sizeof (pl->p_recvsz)); |
---|
280 | /* |
---|
281 | * Assuming that sizeof (xdrbuf) would be enough |
---|
282 | * for the arguments; if not then the program |
---|
283 | * may bomb. BEWARE! |
---|
284 | */ |
---|
285 | if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) { |
---|
286 | svcerr_decode(transp); |
---|
287 | mutex_unlock(&proglst_lock); |
---|
288 | return; |
---|
289 | } |
---|
290 | outdata = (*(pl->p_progname))(xdrbuf); |
---|
291 | if (outdata == NULL && |
---|
292 | pl->p_outproc != (xdrproc_t) xdr_void){ |
---|
293 | /* there was an error */ |
---|
294 | mutex_unlock(&proglst_lock); |
---|
295 | return; |
---|
296 | } |
---|
297 | if (!svc_sendreply(transp, pl->p_outproc, outdata)) { |
---|
298 | warnx( |
---|
299 | "rpc: rpc_reg trouble replying to prog %u vers %u", |
---|
300 | (unsigned)prog, (unsigned)vers); |
---|
301 | mutex_unlock(&proglst_lock); |
---|
302 | return; |
---|
303 | } |
---|
304 | /* free the decoded arguments */ |
---|
305 | (void)svc_freeargs(transp, pl->p_inproc, xdrbuf); |
---|
306 | mutex_unlock(&proglst_lock); |
---|
307 | return; |
---|
308 | } |
---|
309 | mutex_unlock(&proglst_lock); |
---|
310 | /* This should never happen */ |
---|
311 | warnx("rpc: rpc_reg: never registered prog %u vers %u", |
---|
312 | (unsigned)prog, (unsigned)vers); |
---|
313 | return; |
---|
314 | } |
---|