svc_simple.c revision 1.30.24.1 1 /* $NetBSD: svc_simple.c,v 1.30.24.1 2013/03/14 22:03:14 riz Exp $ */
2
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
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.
19 *
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.
32 */
33 /*
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 */
36
37 /* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */
38
39 /*
40 * svc_simple.c
41 * Simplified front end to rpc.
42 */
43
44 /*
45 * This interface creates a virtual listener for all the services
46 * started thru rpc_reg(). It listens on the same endpoint for
47 * all the services and then executes the corresponding service
48 * for the given prognum and procnum.
49 */
50
51 #include <sys/cdefs.h>
52 #if defined(LIBC_SCCS) && !defined(lint)
53 __RCSID("$NetBSD: svc_simple.c,v 1.30.24.1 2013/03/14 22:03:14 riz Exp $");
54 #endif
55
56 #include "namespace.h"
57 #include "reentrant.h"
58 #include <sys/types.h>
59 #include <rpc/rpc.h>
60 #include <rpc/nettype.h>
61 #include <assert.h>
62 #include <err.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66
67 #include "rpc_internal.h"
68
69 #ifdef __weak_alias
70 __weak_alias(rpc_reg,_rpc_reg)
71 #endif
72
73 static void universal __P((struct svc_req *, SVCXPRT *));
74
75 static struct proglst {
76 char *(*p_progname) __P((char *));
77 rpcprog_t p_prognum;
78 rpcvers_t p_versnum;
79 rpcproc_t p_procnum;
80 SVCXPRT *p_transp;
81 char *p_netid;
82 char *p_xdrbuf;
83 int p_recvsz;
84 xdrproc_t p_inproc, p_outproc;
85 struct proglst *p_nxt;
86 } *proglst;
87
88 static const char rpc_reg_err[] = "%s: %s";
89 static const char rpc_reg_msg[] = "rpc_reg: ";
90 static const char __reg_err1[] = "can't find appropriate transport";
91 static const char __reg_err2[] = "can't get protocol info";
92 static const char __reg_err3[] = "unsupported transport size";
93 static const char __no_mem_str[] = "out of memory";
94
95 /*
96 * For simplified, easy to use kind of rpc interfaces.
97 * nettype indicates the type of transport on which the service will be
98 * listening. Used for conservation of the system resource. Only one
99 * handle is created for all the services (actually one of each netid)
100 * and same xdrbuf is used for same netid. The size of the arguments
101 * is also limited by the recvsize for that transport, even if it is
102 * a COTS transport. This may be wrong, but for cases like these, they
103 * should not use the simplified interfaces like this.
104 */
105
106 int
107 rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
108 rpcprog_t prognum; /* program number */
109 rpcvers_t versnum; /* version number */
110 rpcproc_t procnum; /* procedure number */
111 char *(*progname) __P((char *)); /* Server routine */
112 xdrproc_t inproc, outproc; /* in/out XDR procedures */
113 char *nettype; /* nettype */
114 {
115 struct netconfig *nconf;
116 int done = FALSE;
117 void *handle;
118 #ifdef _REENTRANT
119 extern mutex_t proglst_lock;
120 #endif
121
122 if (procnum == NULLPROC) {
123 warnx("%s can't reassign procedure number %u", rpc_reg_msg,
124 NULLPROC);
125 return (-1);
126 }
127
128 if (nettype == NULL)
129 nettype = __UNCONST("netpath"); /* The default behavior */
130 if ((handle = __rpc_setconf(nettype)) == NULL) {
131 warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
132 return (-1);
133 }
134 /* VARIABLES PROTECTED BY proglst_lock: proglst */
135 mutex_lock(&proglst_lock);
136 while ((nconf = __rpc_getconf(handle)) != NULL) {
137 struct proglst *pl;
138 SVCXPRT *svcxprt;
139 int madenow;
140 u_int recvsz;
141 char *xdrbuf;
142 char *netid;
143
144 madenow = FALSE;
145 svcxprt = NULL;
146 recvsz = 0;
147 xdrbuf = NULL;
148 netid = NULL;
149 for (pl = proglst; pl; pl = pl->p_nxt)
150 if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
151 svcxprt = pl->p_transp;
152 xdrbuf = pl->p_xdrbuf;
153 recvsz = pl->p_recvsz;
154 netid = pl->p_netid;
155 break;
156 }
157
158 if (svcxprt == NULL) {
159 struct __rpc_sockinfo si;
160
161 svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
162 if (svcxprt == NULL)
163 continue;
164 if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
165 warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
166 SVC_DESTROY(svcxprt);
167 continue;
168 }
169 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
170 if (recvsz == 0) {
171 warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
172 SVC_DESTROY(svcxprt);
173 continue;
174 }
175 if (((xdrbuf = malloc((size_t)recvsz)) == NULL) ||
176 ((netid = strdup(nconf->nc_netid)) == NULL)) {
177 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
178 if (xdrbuf != NULL)
179 free(xdrbuf);
180 if (netid != NULL)
181 free(netid);
182 SVC_DESTROY(svcxprt);
183 break;
184 }
185 madenow = TRUE;
186 }
187 /*
188 * Check if this (program, version, netid) had already been
189 * registered. The check may save a few RPC calls to rpcbind
190 */
191 for (pl = proglst; pl; pl = pl->p_nxt)
192 if ((pl->p_prognum == prognum) &&
193 (pl->p_versnum == versnum) &&
194 (strcmp(pl->p_netid, netid) == 0))
195 break;
196 if (pl == NULL) { /* Not yet */
197 (void) rpcb_unset(prognum, versnum, nconf);
198 } else {
199 /* so that svc_reg does not call rpcb_set() */
200 nconf = NULL;
201 }
202
203 if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
204 warnx("%s couldn't register prog %u vers %u for %s",
205 rpc_reg_msg, (unsigned)prognum,
206 (unsigned)versnum, netid);
207 if (madenow) {
208 SVC_DESTROY(svcxprt);
209 free(xdrbuf);
210 free(netid);
211 }
212 continue;
213 }
214
215 pl = malloc(sizeof(*pl));
216 if (pl == NULL) {
217 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
218 if (madenow) {
219 SVC_DESTROY(svcxprt);
220 free(xdrbuf);
221 free(netid);
222 }
223 break;
224 }
225 pl->p_progname = progname;
226 pl->p_prognum = prognum;
227 pl->p_versnum = versnum;
228 pl->p_procnum = procnum;
229 pl->p_inproc = inproc;
230 pl->p_outproc = outproc;
231 pl->p_transp = svcxprt;
232 pl->p_xdrbuf = xdrbuf;
233 pl->p_recvsz = recvsz;
234 pl->p_netid = netid;
235 pl->p_nxt = proglst;
236 proglst = pl;
237 done = TRUE;
238 }
239 __rpc_endconf(handle);
240 mutex_unlock(&proglst_lock);
241
242 if (done == FALSE) {
243 warnx("%s cant find suitable transport for %s",
244 rpc_reg_msg, nettype);
245 return (-1);
246 }
247 return (0);
248 }
249
250 /*
251 * The universal handler for the services registered using registerrpc.
252 * It handles both the connectionless and the connection oriented cases.
253 */
254
255 static void
256 universal(rqstp, transp)
257 struct svc_req *rqstp;
258 SVCXPRT *transp;
259 {
260 rpcprog_t prog;
261 rpcvers_t vers;
262 rpcproc_t proc;
263 char *outdata;
264 char *xdrbuf;
265 struct proglst *pl;
266 #ifdef _REENTRANT
267 extern mutex_t proglst_lock;
268 #endif
269
270 _DIAGASSERT(rqstp != NULL);
271 _DIAGASSERT(transp != NULL);
272
273 /*
274 * enforce "procnum 0 is echo" convention
275 */
276 if (rqstp->rq_proc == NULLPROC) {
277 if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
278 FALSE) {
279 warnx("svc_sendreply failed");
280 }
281 return;
282 }
283 prog = rqstp->rq_prog;
284 vers = rqstp->rq_vers;
285 proc = rqstp->rq_proc;
286 mutex_lock(&proglst_lock);
287 for (pl = proglst; pl; pl = pl->p_nxt)
288 if (pl->p_prognum == prog && pl->p_procnum == proc &&
289 pl->p_versnum == vers &&
290 (strcmp(pl->p_netid, transp->xp_netid) == 0)) {
291 /* decode arguments into a CLEAN buffer */
292 xdrbuf = pl->p_xdrbuf;
293 /* Zero the arguments: reqd ! */
294 (void) memset(xdrbuf, 0, (size_t)pl->p_recvsz);
295 /*
296 * Assuming that sizeof (xdrbuf) would be enough
297 * for the arguments; if not then the program
298 * may bomb. BEWARE!
299 */
300 if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
301 svcerr_decode(transp);
302 mutex_unlock(&proglst_lock);
303 return;
304 }
305 outdata = (*(pl->p_progname))(xdrbuf);
306 if (outdata == NULL &&
307 pl->p_outproc != (xdrproc_t) xdr_void){
308 /* there was an error */
309 mutex_unlock(&proglst_lock);
310 return;
311 }
312 if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
313 warnx(
314 "rpc: rpc_reg trouble replying to prog %u vers %u",
315 (unsigned)prog, (unsigned)vers);
316 mutex_unlock(&proglst_lock);
317 return;
318 }
319 /* free the decoded arguments */
320 (void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
321 mutex_unlock(&proglst_lock);
322 return;
323 }
324 mutex_unlock(&proglst_lock);
325 /* This should never happen */
326 warnx("rpc: rpc_reg: never registered prog %u vers %u",
327 (unsigned)prog, (unsigned)vers);
328 return;
329 }
330