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.
29 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32 #include <sys/cdefs.h>
36 * Simplified front end to rpc.
40 * This interface creates a virtual listener for all the services
41 * started thru rpc_reg(). It listens on the same endpoint for
42 * all the services and then executes the corresponding service
43 * for the given prognum and procnum.
46 #include <reentrant.h>
47 #include <sys/types.h>
49 #include <rpc/nettype.h>
57 static void universal(struct svc_req *, SVCXPRT *);
59 static struct proglst {
60 char *(*p_progname)(char *);
68 xdrproc_t p_inproc, p_outproc;
69 struct proglst *p_nxt;
72 static const char rpc_reg_err[] = "%s: %s";
73 static const char rpc_reg_msg[] = "rpc_reg: ";
74 static const char __reg_err1[] = "can't find appropriate transport";
75 static const char __reg_err2[] = "can't get protocol info";
76 static const char __reg_err3[] = "unsupported transport size";
77 static const char __no_mem_str[] = "out of memory";
80 * For simplified, easy to use kind of rpc interfaces.
81 * nettype indicates the type of transport on which the service will be
82 * listening. Used for conservation of the system resource. Only one
83 * handle is created for all the services (actually one of each netid)
84 * and same xdrbuf is used for same netid. The size of the arguments
85 * is also limited by the recvsize for that transport, even if it is
86 * a COTS transport. This may be wrong, but for cases like these, they
87 * should not use the simplified interfaces like this.
91 rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
92 rpcprog_t prognum; /* program number */
93 rpcvers_t versnum; /* version number */
94 rpcproc_t procnum; /* procedure number */
95 char *(*progname)(char *); /* Server routine */
96 xdrproc_t inproc, outproc; /* in/out XDR procedures */
97 char *nettype; /* nettype */
99 struct netconfig *nconf;
102 extern mutex_t proglst_lock;
104 if (procnum == NULLPROC) {
105 warnx("%s can't reassign procedure number %u", rpc_reg_msg,
111 nettype = "netpath"; /* The default behavior */
112 if ((handle = __rpc_setconf(nettype)) == NULL) {
113 warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
116 /* VARIABLES PROTECTED BY proglst_lock: proglst */
117 mutex_lock(&proglst_lock);
118 while ((nconf = __rpc_getconf(handle)) != NULL) {
129 xdrbuf = netid = NULL;
130 for (pl = proglst; pl; pl = pl->p_nxt) {
131 if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
132 svcxprt = pl->p_transp;
133 xdrbuf = pl->p_xdrbuf;
134 recvsz = pl->p_recvsz;
140 if (svcxprt == NULL) {
141 struct __rpc_sockinfo si;
143 svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
146 if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
147 warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
148 SVC_DESTROY(svcxprt);
151 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
153 warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
154 SVC_DESTROY(svcxprt);
157 if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) ||
158 ((netid = strdup(nconf->nc_netid)) == NULL)) {
159 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
160 SVC_DESTROY(svcxprt);
166 * Check if this (program, version, netid) had already been
167 * registered. The check may save a few RPC calls to rpcbind
169 for (pl = proglst; pl; pl = pl->p_nxt)
170 if ((pl->p_prognum == prognum) &&
171 (pl->p_versnum == versnum) &&
172 (strcmp(pl->p_netid, netid) == 0))
174 if (pl == NULL) { /* Not yet */
175 (void) rpcb_unset(prognum, versnum, nconf);
177 /* so that svc_reg does not call rpcb_set() */
181 if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
182 warnx("%s couldn't register prog %u vers %u for %s",
183 rpc_reg_msg, (unsigned)prognum,
184 (unsigned)versnum, netid);
186 SVC_DESTROY(svcxprt);
193 pl = malloc(sizeof (struct proglst));
195 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
197 SVC_DESTROY(svcxprt);
203 pl->p_progname = progname;
204 pl->p_prognum = prognum;
205 pl->p_versnum = versnum;
206 pl->p_procnum = procnum;
207 pl->p_inproc = inproc;
208 pl->p_outproc = outproc;
209 pl->p_transp = svcxprt;
210 pl->p_xdrbuf = xdrbuf;
211 pl->p_recvsz = recvsz;
217 __rpc_endconf(handle);
218 mutex_unlock(&proglst_lock);
221 warnx("%s cant find suitable transport for %s",
222 rpc_reg_msg, nettype);
229 * The universal handler for the services registered using registerrpc.
230 * It handles both the connectionless and the connection oriented cases.
234 universal(rqstp, transp)
235 struct svc_req *rqstp;
244 extern mutex_t proglst_lock;
247 * enforce "procnum 0 is echo" convention
249 if (rqstp->rq_proc == NULLPROC) {
250 if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
252 warnx("svc_sendreply failed");
256 prog = rqstp->rq_prog;
257 vers = rqstp->rq_vers;
258 proc = rqstp->rq_proc;
259 mutex_lock(&proglst_lock);
260 for (pl = proglst; pl; pl = pl->p_nxt)
261 if (pl->p_prognum == prog && pl->p_procnum == proc &&
262 pl->p_versnum == vers &&
263 (strcmp(pl->p_netid, transp->xp_netid) == 0)) {
264 /* decode arguments into a CLEAN buffer */
265 xdrbuf = pl->p_xdrbuf;
266 /* Zero the arguments: reqd ! */
267 (void) memset(xdrbuf, 0, sizeof (pl->p_recvsz));
269 * Assuming that sizeof (xdrbuf) would be enough
270 * for the arguments; if not then the program
273 if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
274 svcerr_decode(transp);
275 mutex_unlock(&proglst_lock);
278 outdata = (*(pl->p_progname))(xdrbuf);
279 if (outdata == NULL &&
280 pl->p_outproc != (xdrproc_t) xdr_void){
281 /* there was an error */
282 mutex_unlock(&proglst_lock);
285 if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
287 "rpc: rpc_reg trouble replying to prog %u vers %u",
288 (unsigned)prog, (unsigned)vers);
289 mutex_unlock(&proglst_lock);
292 /* free the decoded arguments */
293 (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
294 mutex_unlock(&proglst_lock);
297 mutex_unlock(&proglst_lock);
298 /* This should never happen */
299 warnx("rpc: rpc_reg: never registered prog %u vers %u",
300 (unsigned)prog, (unsigned)vers);