2 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
28 * @(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro
29 * @(#)svc_simple.c 2.2 88/08/01 4.0 RPCSRC
30 * $NetBSD: svc_simple.c,v 1.20 2000/07/06 03:10:35 christos Exp $
31 * $FreeBSD: src/lib/libc/rpc/svc_simple.c,v 1.16 2006/09/09 22:32:07 mbr Exp $
32 * $DragonFly: src/lib/libc/rpc/svc_simple.c,v 1.3 2005/11/13 12:27:04 swildner Exp $
35 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
40 * Simplified front end to rpc.
44 * This interface creates a virtual listener for all the services
45 * started thru rpc_reg(). It listens on the same endpoint for
46 * all the services and then executes the corresponding service
47 * for the given prognum and procnum.
50 #include "namespace.h"
51 #include "reentrant.h"
52 #include <sys/types.h>
54 #include <rpc/nettype.h>
59 #include "un-namespace.h"
64 static void universal(struct svc_req
*, SVCXPRT
*);
66 static struct proglst
{
67 char *(*p_progname
)(char *);
75 xdrproc_t p_inproc
, p_outproc
;
76 struct proglst
*p_nxt
;
79 static const char rpc_reg_err
[] = "%s: %s";
80 static const char rpc_reg_msg
[] = "rpc_reg: ";
81 static const char __reg_err1
[] = "can't find appropriate transport";
82 static const char __reg_err2
[] = "can't get protocol info";
83 static const char __reg_err3
[] = "unsupported transport size";
84 static const char __no_mem_str
[] = "out of memory";
87 * For simplified, easy to use kind of rpc interfaces.
88 * nettype indicates the type of transport on which the service will be
89 * listening. Used for conservation of the system resource. Only one
90 * handle is created for all the services (actually one of each netid)
91 * and same xdrbuf is used for same netid. The size of the arguments
92 * is also limited by the recvsize for that transport, even if it is
93 * a COTS transport. This may be wrong, but for cases like these, they
94 * should not use the simplified interfaces like this.
98 rpc_reg(rpcprog_t prognum
, /* program number */
99 rpcvers_t versnum
, /* version number */
100 rpcproc_t procnum
, /* procedure number */
101 char *(*progname
)(char *), /* Server routine */
102 xdrproc_t inproc
, xdrproc_t outproc
, /* in/out XDR procedures */
103 char *nettype
) /* nettype */
105 struct netconfig
*nconf
;
110 if (procnum
== NULLPROC
) {
111 warnx("%s can't reassign procedure number %u", rpc_reg_msg
,
117 nettype
= "netpath"; /* The default behavior */
118 if ((handle
= __rpc_setconf(nettype
)) == NULL
) {
119 warnx(rpc_reg_err
, rpc_reg_msg
, __reg_err1
);
122 /* VARIABLES PROTECTED BY proglst_lock: proglst */
123 mutex_lock(&proglst_lock
);
124 while ((nconf
= __rpc_getconf(handle
)) != NULL
) {
135 xdrbuf
= netid
= NULL
;
136 for (pl
= proglst
; pl
; pl
= pl
->p_nxt
) {
137 if (strcmp(pl
->p_netid
, nconf
->nc_netid
) == 0) {
138 svcxprt
= pl
->p_transp
;
139 xdrbuf
= pl
->p_xdrbuf
;
140 recvsz
= pl
->p_recvsz
;
146 if (svcxprt
== NULL
) {
147 struct __rpc_sockinfo si
;
149 svcxprt
= svc_tli_create(RPC_ANYFD
, nconf
, NULL
, 0, 0);
152 if (!__rpc_fd2sockinfo(svcxprt
->xp_fd
, &si
)) {
153 warnx(rpc_reg_err
, rpc_reg_msg
, __reg_err2
);
154 SVC_DESTROY(svcxprt
);
157 recvsz
= __rpc_get_t_size(si
.si_af
, si
.si_proto
, 0);
159 warnx(rpc_reg_err
, rpc_reg_msg
, __reg_err3
);
160 SVC_DESTROY(svcxprt
);
163 if (((xdrbuf
= malloc((unsigned)recvsz
)) == NULL
) ||
164 ((netid
= strdup(nconf
->nc_netid
)) == NULL
)) {
165 warnx(rpc_reg_err
, rpc_reg_msg
, __no_mem_str
);
170 SVC_DESTROY(svcxprt
);
176 * Check if this (program, version, netid) had already been
177 * registered. The check may save a few RPC calls to rpcbind
179 for (pl
= proglst
; pl
; pl
= pl
->p_nxt
)
180 if ((pl
->p_prognum
== prognum
) &&
181 (pl
->p_versnum
== versnum
) &&
182 (strcmp(pl
->p_netid
, netid
) == 0))
184 if (pl
== NULL
) { /* Not yet */
185 rpcb_unset(prognum
, versnum
, nconf
);
187 /* so that svc_reg does not call rpcb_set() */
191 if (!svc_reg(svcxprt
, prognum
, versnum
, universal
, nconf
)) {
192 warnx("%s couldn't register prog %u vers %u for %s",
193 rpc_reg_msg
, (unsigned)prognum
,
194 (unsigned)versnum
, netid
);
196 SVC_DESTROY(svcxprt
);
203 pl
= malloc(sizeof (struct proglst
));
205 warnx(rpc_reg_err
, rpc_reg_msg
, __no_mem_str
);
207 SVC_DESTROY(svcxprt
);
213 pl
->p_progname
= progname
;
214 pl
->p_prognum
= prognum
;
215 pl
->p_versnum
= versnum
;
216 pl
->p_procnum
= procnum
;
217 pl
->p_inproc
= inproc
;
218 pl
->p_outproc
= outproc
;
219 pl
->p_transp
= svcxprt
;
220 pl
->p_xdrbuf
= xdrbuf
;
221 pl
->p_recvsz
= recvsz
;
227 __rpc_endconf(handle
);
228 mutex_unlock(&proglst_lock
);
231 warnx("%s can't find suitable transport for %s",
232 rpc_reg_msg
, nettype
);
239 * The universal handler for the services registered using registerrpc.
240 * It handles both the connectionless and the connection oriented cases.
244 universal(struct svc_req
*rqstp
, SVCXPRT
*transp
)
254 * enforce "procnum 0 is echo" convention
256 if (rqstp
->rq_proc
== NULLPROC
) {
257 if (svc_sendreply(transp
, (xdrproc_t
) xdr_void
, NULL
) ==
259 warnx("svc_sendreply failed");
263 prog
= rqstp
->rq_prog
;
264 vers
= rqstp
->rq_vers
;
265 proc
= rqstp
->rq_proc
;
266 mutex_lock(&proglst_lock
);
267 for (pl
= proglst
; pl
; pl
= pl
->p_nxt
)
268 if (pl
->p_prognum
== prog
&& pl
->p_procnum
== proc
&&
269 pl
->p_versnum
== vers
&&
270 (strcmp(pl
->p_netid
, transp
->xp_netid
) == 0)) {
271 /* decode arguments into a CLEAN buffer */
272 xdrbuf
= pl
->p_xdrbuf
;
273 /* Zero the arguments: reqd ! */
274 memset(xdrbuf
, 0, sizeof (pl
->p_recvsz
));
276 * Assuming that sizeof (xdrbuf) would be enough
277 * for the arguments; if not then the program
280 if (!svc_getargs(transp
, pl
->p_inproc
, xdrbuf
)) {
281 svcerr_decode(transp
);
282 mutex_unlock(&proglst_lock
);
285 outdata
= (*(pl
->p_progname
))(xdrbuf
);
286 if (outdata
== NULL
&&
287 pl
->p_outproc
!= (xdrproc_t
) xdr_void
){
288 /* there was an error */
289 mutex_unlock(&proglst_lock
);
292 if (!svc_sendreply(transp
, pl
->p_outproc
, outdata
)) {
294 "rpc: rpc_reg trouble replying to prog %u vers %u",
295 (unsigned)prog
, (unsigned)vers
);
296 mutex_unlock(&proglst_lock
);
299 /* free the decoded arguments */
300 svc_freeargs(transp
, pl
->p_inproc
, xdrbuf
);
301 mutex_unlock(&proglst_lock
);
304 mutex_unlock(&proglst_lock
);
305 /* This should never happen */
306 warnx("rpc: rpc_reg: never registered prog %u vers %u",
307 (unsigned)prog
, (unsigned)vers
);