usched: Implement LWP lazy migration support.
[dragonfly.git] / lib / libc / rpc / svc_simple.c
blob4a036072f2a58db085680dda321c2dfde8034ee0
1 /*-
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
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.
39 * svc_simple.c
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>
53 #include <rpc/rpc.h>
54 #include <rpc/nettype.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <err.h>
59 #include "un-namespace.h"
61 #include "rpc_com.h"
62 #include "mt_misc.h"
64 static void universal(struct svc_req *, SVCXPRT *);
66 static struct proglst {
67 char *(*p_progname)(char *);
68 rpcprog_t p_prognum;
69 rpcvers_t p_versnum;
70 rpcproc_t p_procnum;
71 SVCXPRT *p_transp;
72 char *p_netid;
73 char *p_xdrbuf;
74 int p_recvsz;
75 xdrproc_t p_inproc, p_outproc;
76 struct proglst *p_nxt;
77 } *proglst;
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.
97 int
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;
106 int done = FALSE;
107 void *handle;
110 if (procnum == NULLPROC) {
111 warnx("%s can't reassign procedure number %u", rpc_reg_msg,
112 NULLPROC);
113 return (-1);
116 if (nettype == NULL)
117 nettype = "netpath"; /* The default behavior */
118 if ((handle = __rpc_setconf(nettype)) == NULL) {
119 warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
120 return (-1);
122 /* VARIABLES PROTECTED BY proglst_lock: proglst */
123 mutex_lock(&proglst_lock);
124 while ((nconf = __rpc_getconf(handle)) != NULL) {
125 struct proglst *pl;
126 SVCXPRT *svcxprt;
127 int madenow;
128 u_int recvsz;
129 char *xdrbuf;
130 char *netid;
132 madenow = FALSE;
133 svcxprt = NULL;
134 recvsz = 0;
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;
141 netid = pl->p_netid;
142 break;
146 if (svcxprt == NULL) {
147 struct __rpc_sockinfo si;
149 svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
150 if (svcxprt == NULL)
151 continue;
152 if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
153 warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
154 SVC_DESTROY(svcxprt);
155 continue;
157 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
158 if (recvsz == 0) {
159 warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
160 SVC_DESTROY(svcxprt);
161 continue;
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);
166 if (xdrbuf != NULL)
167 free(xdrbuf);
168 if (netid != NULL)
169 free(netid);
170 SVC_DESTROY(svcxprt);
171 break;
173 madenow = TRUE;
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))
183 break;
184 if (pl == NULL) { /* Not yet */
185 rpcb_unset(prognum, versnum, nconf);
186 } else {
187 /* so that svc_reg does not call rpcb_set() */
188 nconf = NULL;
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);
195 if (madenow) {
196 SVC_DESTROY(svcxprt);
197 free(xdrbuf);
198 free(netid);
200 continue;
203 pl = malloc(sizeof (struct proglst));
204 if (pl == NULL) {
205 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
206 if (madenow) {
207 SVC_DESTROY(svcxprt);
208 free(xdrbuf);
209 free(netid);
211 break;
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;
222 pl->p_netid = netid;
223 pl->p_nxt = proglst;
224 proglst = pl;
225 done = TRUE;
227 __rpc_endconf(handle);
228 mutex_unlock(&proglst_lock);
230 if (done == FALSE) {
231 warnx("%s can't find suitable transport for %s",
232 rpc_reg_msg, nettype);
233 return (-1);
235 return (0);
239 * The universal handler for the services registered using registerrpc.
240 * It handles both the connectionless and the connection oriented cases.
243 static void
244 universal(struct svc_req *rqstp, SVCXPRT *transp)
246 rpcprog_t prog;
247 rpcvers_t vers;
248 rpcproc_t proc;
249 char *outdata;
250 char *xdrbuf;
251 struct proglst *pl;
254 * enforce "procnum 0 is echo" convention
256 if (rqstp->rq_proc == NULLPROC) {
257 if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
258 FALSE) {
259 warnx("svc_sendreply failed");
261 return;
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
278 * may bomb. BEWARE!
280 if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
281 svcerr_decode(transp);
282 mutex_unlock(&proglst_lock);
283 return;
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);
290 return;
292 if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
293 warnx(
294 "rpc: rpc_reg trouble replying to prog %u vers %u",
295 (unsigned)prog, (unsigned)vers);
296 mutex_unlock(&proglst_lock);
297 return;
299 /* free the decoded arguments */
300 svc_freeargs(transp, pl->p_inproc, xdrbuf);
301 mutex_unlock(&proglst_lock);
302 return;
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);
308 return;