1 /* $NetBSD: svc_simple.c,v 1.33 2013/03/11 20:19:29 tron Exp $ */
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
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 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
37 /* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */
41 * Simplified front end to rpc.
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.
51 #include <sys/cdefs.h>
52 #if defined(LIBC_SCCS) && !defined(lint)
53 __RCSID("$NetBSD: svc_simple.c,v 1.33 2013/03/11 20:19:29 tron Exp $");
56 #include "namespace.h"
57 #include "reentrant.h"
58 #include <sys/types.h>
60 #include <rpc/nettype.h>
67 #include "rpc_internal.h"
70 __weak_alias(rpc_reg
,_rpc_reg
)
73 static void universal(struct svc_req
*, SVCXPRT
*);
75 static struct proglst
{
76 char *(*p_progname
)(char *);
84 xdrproc_t p_inproc
, p_outproc
;
85 struct proglst
*p_nxt
;
88 static const char __reg_err1
[] = "can't find appropriate transport";
89 static const char __reg_err2
[] = "can't get protocol info";
90 static const char __reg_err3
[] = "unsupported transport size";
91 static const char __no_mem_str
[] = "out of memory";
94 * For simplified, easy to use kind of rpc interfaces.
95 * nettype indicates the type of transport on which the service will be
96 * listening. Used for conservation of the system resource. Only one
97 * handle is created for all the services (actually one of each netid)
98 * and same xdrbuf is used for same netid. The size of the arguments
99 * is also limited by the recvsize for that transport, even if it is
100 * a COTS transport. This may be wrong, but for cases like these, they
101 * should not use the simplified interfaces like this.
106 rpcprog_t prognum
, /* program number */
107 rpcvers_t versnum
, /* version number */
108 rpcproc_t procnum
, /* procedure number */
109 char *(*progname
)(char *), /* Server routine */
110 xdrproc_t inproc
, /* in XDR procedure */
111 xdrproc_t outproc
, /* out XDR procedure */
112 char *nettype
) /* nettype */
114 struct netconfig
*nconf
;
118 extern mutex_t proglst_lock
;
121 if (procnum
== NULLPROC
) {
122 warnx("%s: can't reassign procedure number %u", __func__
,
128 nettype
= __UNCONST("netpath"); /* The default behavior */
129 if ((handle
= __rpc_setconf(nettype
)) == NULL
) {
130 warnx("%s: %s", __func__
, __reg_err1
);
133 /* VARIABLES PROTECTED BY proglst_lock: proglst */
134 mutex_lock(&proglst_lock
);
135 while ((nconf
= __rpc_getconf(handle
)) != NULL
) {
148 for (pl
= proglst
; pl
; pl
= pl
->p_nxt
)
149 if (strcmp(pl
->p_netid
, nconf
->nc_netid
) == 0) {
150 svcxprt
= pl
->p_transp
;
151 xdrbuf
= pl
->p_xdrbuf
;
152 recvsz
= pl
->p_recvsz
;
157 if (svcxprt
== NULL
) {
158 struct __rpc_sockinfo si
;
160 svcxprt
= svc_tli_create(RPC_ANYFD
, nconf
, NULL
, 0, 0);
163 if (!__rpc_fd2sockinfo(svcxprt
->xp_fd
, &si
)) {
164 warnx("%s: %s", __func__
, __reg_err2
);
165 SVC_DESTROY(svcxprt
);
168 recvsz
= __rpc_get_t_size(si
.si_af
, si
.si_proto
, 0);
170 warnx("%s: %s", __func__
, __reg_err3
);
171 SVC_DESTROY(svcxprt
);
174 if (((xdrbuf
= mem_alloc((size_t)recvsz
)) == NULL
) ||
175 ((netid
= strdup(nconf
->nc_netid
)) == NULL
)) {
176 warnx("%s: %s", __func__
, __no_mem_str
);
181 SVC_DESTROY(svcxprt
);
187 * Check if this (program, version, netid) had already been
188 * registered. The check may save a few RPC calls to rpcbind
190 for (pl
= proglst
; pl
; pl
= pl
->p_nxt
)
191 if ((pl
->p_prognum
== prognum
) &&
192 (pl
->p_versnum
== versnum
) &&
193 (strcmp(pl
->p_netid
, netid
) == 0))
195 if (pl
== NULL
) { /* Not yet */
196 (void) rpcb_unset(prognum
, versnum
, nconf
);
198 /* so that svc_reg does not call rpcb_set() */
202 if (!svc_reg(svcxprt
, prognum
, versnum
, universal
, nconf
)) {
203 warnx("%s: couldn't register prog %u vers %u for %s",
204 __func__
, (unsigned)prognum
,
205 (unsigned)versnum
, netid
);
207 SVC_DESTROY(svcxprt
);
214 pl
= malloc(sizeof(*pl
));
216 warn("%s: %s", __func__
, __no_mem_str
);
218 SVC_DESTROY(svcxprt
);
224 pl
->p_progname
= progname
;
225 pl
->p_prognum
= prognum
;
226 pl
->p_versnum
= versnum
;
227 pl
->p_procnum
= procnum
;
228 pl
->p_inproc
= inproc
;
229 pl
->p_outproc
= outproc
;
230 pl
->p_transp
= svcxprt
;
231 pl
->p_xdrbuf
= xdrbuf
;
232 pl
->p_recvsz
= recvsz
;
238 __rpc_endconf(handle
);
239 mutex_unlock(&proglst_lock
);
242 warnx("%s: can't find suitable transport for %s",
250 * The universal handler for the services registered using registerrpc.
251 * It handles both the connectionless and the connection oriented cases.
255 universal(struct svc_req
*rqstp
, SVCXPRT
*transp
)
264 extern mutex_t proglst_lock
;
267 _DIAGASSERT(rqstp
!= NULL
);
268 _DIAGASSERT(transp
!= NULL
);
271 * enforce "procnum 0 is echo" convention
273 if (rqstp
->rq_proc
== NULLPROC
) {
274 if (svc_sendreply(transp
, (xdrproc_t
) xdr_void
, NULL
) ==
276 warnx("%s: svc_sendreply failed", __func__
);
280 prog
= rqstp
->rq_prog
;
281 vers
= rqstp
->rq_vers
;
282 proc
= rqstp
->rq_proc
;
283 mutex_lock(&proglst_lock
);
284 for (pl
= proglst
; pl
; pl
= pl
->p_nxt
)
285 if (pl
->p_prognum
== prog
&& pl
->p_procnum
== proc
&&
286 pl
->p_versnum
== vers
&&
287 (strcmp(pl
->p_netid
, transp
->xp_netid
) == 0)) {
288 /* decode arguments into a CLEAN buffer */
289 xdrbuf
= pl
->p_xdrbuf
;
290 /* Zero the arguments: reqd ! */
291 (void) memset(xdrbuf
, 0, (size_t)pl
->p_recvsz
);
293 * Assuming that sizeof (xdrbuf) would be enough
294 * for the arguments; if not then the program
297 if (!svc_getargs(transp
, pl
->p_inproc
, xdrbuf
)) {
298 svcerr_decode(transp
);
299 mutex_unlock(&proglst_lock
);
302 outdata
= (*(pl
->p_progname
))(xdrbuf
);
303 if (outdata
== NULL
&&
304 pl
->p_outproc
!= (xdrproc_t
) xdr_void
){
305 /* there was an error */
306 mutex_unlock(&proglst_lock
);
309 if (!svc_sendreply(transp
, pl
->p_outproc
, outdata
)) {
310 warnx("%s: trouble replying to prog %u vers %u",
311 __func__
, (unsigned)prog
, (unsigned)vers
);
312 mutex_unlock(&proglst_lock
);
315 /* free the decoded arguments */
316 (void) svc_freeargs(transp
, pl
->p_inproc
, xdrbuf
);
317 mutex_unlock(&proglst_lock
);
320 mutex_unlock(&proglst_lock
);
321 /* This should never happen */
322 warnx("%s: never registered prog %u vers %u", __func__
,
323 (unsigned)prog
, (unsigned)vers
);