2001-01-04 17:42:18 +03:00
|
|
|
/* $NetBSD: svc_simple.c,v 1.21 2001/01/04 14:42:22 lukem Exp $ */
|
1995-02-25 06:01:33 +03:00
|
|
|
|
1993-10-07 10:29:33 +03:00
|
|
|
/*
|
|
|
|
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
|
|
|
|
* unrestricted use provided that this legend is included on all tape
|
|
|
|
* media and as a part of the software program in whole or part. Users
|
|
|
|
* may copy or modify Sun RPC without charge, but are not authorized
|
|
|
|
* to license or distribute it to anyone else except as part of a product or
|
|
|
|
* program developed by the user.
|
|
|
|
*
|
|
|
|
* SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
|
|
|
|
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
|
|
|
|
*
|
|
|
|
* Sun RPC is provided with no support and without any obligation on the
|
|
|
|
* part of Sun Microsystems, Inc. to assist in its use, correction,
|
|
|
|
* modification or enhancement.
|
|
|
|
*
|
|
|
|
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
|
|
|
|
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
|
|
|
|
* OR ANY PART THEREOF.
|
|
|
|
*
|
|
|
|
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
|
|
|
|
* or profits or other special, indirect and consequential damages, even if
|
|
|
|
* Sun has been advised of the possibility of such damages.
|
|
|
|
*
|
|
|
|
* Sun Microsystems, Inc.
|
|
|
|
* 2550 Garcia Avenue
|
|
|
|
* Mountain View, California 94043
|
|
|
|
*/
|
2000-06-03 03:11:05 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1986-1991 by Sun Microsystems Inc.
|
|
|
|
*/
|
1993-10-07 10:29:33 +03:00
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
/* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */
|
1993-10-07 10:29:33 +03:00
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
/*
|
1993-10-07 10:29:33 +03:00
|
|
|
* svc_simple.c
|
|
|
|
* Simplified front end to rpc.
|
|
|
|
*/
|
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
/*
|
|
|
|
* This interface creates a virtual listener for all the services
|
|
|
|
* started thru rpc_reg(). It listens on the same endpoint for
|
|
|
|
* all the services and then executes the corresponding service
|
|
|
|
* for the given prognum and procnum.
|
|
|
|
*/
|
1998-02-13 08:52:11 +03:00
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
#include "namespace.h"
|
|
|
|
#include "reentrant.h"
|
1998-02-13 08:52:11 +03:00
|
|
|
#include <sys/types.h>
|
2000-06-03 03:11:05 +04:00
|
|
|
#include <rpc/rpc.h>
|
2000-07-06 07:10:34 +04:00
|
|
|
#include <rpc/nettype.h>
|
2001-01-04 17:42:18 +03:00
|
|
|
#include <assert.h>
|
|
|
|
#include <err.h>
|
1993-10-07 10:29:33 +03:00
|
|
|
#include <stdio.h>
|
1994-12-04 04:12:39 +03:00
|
|
|
#include <stdlib.h>
|
1995-04-14 23:48:20 +04:00
|
|
|
#include <string.h>
|
1998-02-13 08:52:11 +03:00
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
#include "rpc_com.h"
|
1993-10-07 10:29:33 +03:00
|
|
|
|
1997-07-21 18:06:24 +04:00
|
|
|
#ifdef __weak_alias
|
2000-06-03 03:11:05 +04:00
|
|
|
__weak_alias(rpc_reg,_rpc_reg)
|
1997-07-21 18:06:24 +04:00
|
|
|
#endif
|
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
static void universal __P((struct svc_req *, SVCXPRT *));
|
|
|
|
|
1993-10-07 10:29:33 +03:00
|
|
|
static struct proglst {
|
2000-06-03 03:11:05 +04:00
|
|
|
char *(*p_progname) __P((char *));
|
|
|
|
rpcprog_t p_prognum;
|
|
|
|
rpcvers_t p_versnum;
|
|
|
|
rpcproc_t p_procnum;
|
|
|
|
SVCXPRT *p_transp;
|
|
|
|
char *p_netid;
|
|
|
|
char *p_xdrbuf;
|
|
|
|
int p_recvsz;
|
1993-10-07 10:29:33 +03:00
|
|
|
xdrproc_t p_inproc, p_outproc;
|
|
|
|
struct proglst *p_nxt;
|
|
|
|
} *proglst;
|
1997-07-14 00:13:02 +04:00
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
static const char rpc_reg_err[] = "%s: %s";
|
|
|
|
static const char rpc_reg_msg[] = "rpc_reg: ";
|
|
|
|
static const char __reg_err1[] = "can't find appropriate transport";
|
|
|
|
static const char __reg_err2[] = "can't get protocol info";
|
|
|
|
static const char __reg_err3[] = "unsupported transport size";
|
|
|
|
static const char __no_mem_str[] = "out of memory";
|
1993-10-07 10:29:33 +03:00
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
/*
|
|
|
|
* For simplified, easy to use kind of rpc interfaces.
|
|
|
|
* nettype indicates the type of transport on which the service will be
|
|
|
|
* listening. Used for conservation of the system resource. Only one
|
|
|
|
* handle is created for all the services (actually one of each netid)
|
|
|
|
* and same xdrbuf is used for same netid. The size of the arguments
|
|
|
|
* is also limited by the recvsize for that transport, even if it is
|
|
|
|
* a COTS transport. This may be wrong, but for cases like these, they
|
|
|
|
* should not use the simplified interfaces like this.
|
|
|
|
*/
|
1997-07-14 00:13:02 +04:00
|
|
|
|
1996-03-30 02:00:47 +03:00
|
|
|
int
|
2000-06-03 03:11:05 +04:00
|
|
|
rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
|
|
|
|
rpcprog_t prognum; /* program number */
|
|
|
|
rpcvers_t versnum; /* version number */
|
|
|
|
rpcproc_t procnum; /* procedure number */
|
|
|
|
char *(*progname) __P((char *)); /* Server routine */
|
|
|
|
xdrproc_t inproc, outproc; /* in/out XDR procedures */
|
|
|
|
char *nettype; /* nettype */
|
1993-10-07 10:29:33 +03:00
|
|
|
{
|
2000-06-03 03:11:05 +04:00
|
|
|
struct netconfig *nconf;
|
|
|
|
int done = FALSE;
|
|
|
|
void *handle;
|
|
|
|
#ifdef __REENT
|
|
|
|
extern mutex_t proglst_lock;
|
|
|
|
#endif
|
|
|
|
|
1993-10-07 10:29:33 +03:00
|
|
|
if (procnum == NULLPROC) {
|
2000-06-03 03:11:05 +04:00
|
|
|
warnx("%s can't reassign procedure number %u", rpc_reg_msg,
|
|
|
|
NULLPROC);
|
1993-10-07 10:29:33 +03:00
|
|
|
return (-1);
|
|
|
|
}
|
2000-06-03 03:11:05 +04:00
|
|
|
|
|
|
|
if (nettype == NULL)
|
|
|
|
nettype = "netpath"; /* The default behavior */
|
|
|
|
if ((handle = __rpc_setconf(nettype)) == NULL) {
|
|
|
|
warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
|
1993-10-07 10:29:33 +03:00
|
|
|
return (-1);
|
|
|
|
}
|
2000-06-03 03:11:05 +04:00
|
|
|
/* VARIABLES PROTECTED BY proglst_lock: proglst */
|
|
|
|
mutex_lock(&proglst_lock);
|
2000-07-06 07:10:34 +04:00
|
|
|
while ((nconf = __rpc_getconf(handle)) != NULL) {
|
2000-06-03 03:11:05 +04:00
|
|
|
struct proglst *pl;
|
|
|
|
SVCXPRT *svcxprt;
|
|
|
|
int madenow;
|
|
|
|
u_int recvsz;
|
|
|
|
char *xdrbuf;
|
|
|
|
char *netid;
|
|
|
|
|
|
|
|
madenow = FALSE;
|
2000-07-06 07:10:34 +04:00
|
|
|
svcxprt = NULL;
|
2000-06-03 03:11:05 +04:00
|
|
|
for (pl = proglst; pl; pl = pl->p_nxt)
|
|
|
|
if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
|
|
|
|
svcxprt = pl->p_transp;
|
|
|
|
xdrbuf = pl->p_xdrbuf;
|
|
|
|
recvsz = pl->p_recvsz;
|
|
|
|
netid = pl->p_netid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-07-06 07:10:34 +04:00
|
|
|
if (svcxprt == NULL) {
|
2000-06-03 03:11:05 +04:00
|
|
|
struct __rpc_sockinfo si;
|
|
|
|
|
2000-07-06 07:10:34 +04:00
|
|
|
svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
|
|
|
|
if (svcxprt == NULL)
|
2000-06-03 03:11:05 +04:00
|
|
|
continue;
|
|
|
|
if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
|
|
|
|
warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
|
|
|
|
SVC_DESTROY(svcxprt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
|
|
|
|
if (recvsz == 0) {
|
|
|
|
warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
|
|
|
|
SVC_DESTROY(svcxprt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) ||
|
|
|
|
((netid = strdup(nconf->nc_netid)) == NULL)) {
|
|
|
|
warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
|
|
|
|
SVC_DESTROY(svcxprt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
madenow = TRUE;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check if this (program, version, netid) had already been
|
|
|
|
* registered. The check may save a few RPC calls to rpcbind
|
|
|
|
*/
|
|
|
|
for (pl = proglst; pl; pl = pl->p_nxt)
|
|
|
|
if ((pl->p_prognum == prognum) &&
|
|
|
|
(pl->p_versnum == versnum) &&
|
|
|
|
(strcmp(pl->p_netid, netid) == 0))
|
|
|
|
break;
|
|
|
|
if (pl == NULL) { /* Not yet */
|
|
|
|
(void) rpcb_unset(prognum, versnum, nconf);
|
|
|
|
} else {
|
|
|
|
/* so that svc_reg does not call rpcb_set() */
|
|
|
|
nconf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
|
|
|
|
warnx("%s couldn't register prog %u vers %u for %s",
|
|
|
|
rpc_reg_msg, (unsigned)prognum,
|
|
|
|
(unsigned)versnum, netid);
|
|
|
|
if (madenow) {
|
|
|
|
SVC_DESTROY(svcxprt);
|
|
|
|
free(xdrbuf);
|
|
|
|
free(netid);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-07-06 07:10:34 +04:00
|
|
|
pl = malloc(sizeof (struct proglst));
|
|
|
|
if (pl == NULL) {
|
2000-06-03 03:11:05 +04:00
|
|
|
warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
|
|
|
|
if (madenow) {
|
|
|
|
SVC_DESTROY(svcxprt);
|
|
|
|
free(xdrbuf);
|
|
|
|
free(netid);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pl->p_progname = progname;
|
|
|
|
pl->p_prognum = prognum;
|
|
|
|
pl->p_versnum = versnum;
|
|
|
|
pl->p_procnum = procnum;
|
|
|
|
pl->p_inproc = inproc;
|
|
|
|
pl->p_outproc = outproc;
|
|
|
|
pl->p_transp = svcxprt;
|
|
|
|
pl->p_xdrbuf = xdrbuf;
|
|
|
|
pl->p_recvsz = recvsz;
|
|
|
|
pl->p_netid = netid;
|
|
|
|
pl->p_nxt = proglst;
|
|
|
|
proglst = pl;
|
|
|
|
done = TRUE;
|
|
|
|
}
|
|
|
|
__rpc_endconf(handle);
|
|
|
|
mutex_unlock(&proglst_lock);
|
|
|
|
|
|
|
|
if (done == FALSE) {
|
|
|
|
warnx("%s cant find suitable transport for %s",
|
|
|
|
rpc_reg_msg, nettype);
|
1993-10-07 10:29:33 +03:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
/*
|
|
|
|
* The universal handler for the services registered using registerrpc.
|
|
|
|
* It handles both the connectionless and the connection oriented cases.
|
|
|
|
*/
|
|
|
|
|
1993-10-07 10:29:33 +03:00
|
|
|
static void
|
|
|
|
universal(rqstp, transp)
|
|
|
|
struct svc_req *rqstp;
|
|
|
|
SVCXPRT *transp;
|
|
|
|
{
|
2000-06-03 03:11:05 +04:00
|
|
|
rpcprog_t prog;
|
|
|
|
rpcvers_t vers;
|
|
|
|
rpcproc_t proc;
|
1993-10-07 10:29:33 +03:00
|
|
|
char *outdata;
|
2000-06-03 03:11:05 +04:00
|
|
|
char *xdrbuf;
|
|
|
|
struct proglst *pl;
|
|
|
|
#ifdef __REENT
|
|
|
|
extern mutex_t proglst_lock;
|
|
|
|
#endif
|
1999-09-16 15:44:54 +04:00
|
|
|
|
2001-01-04 17:42:18 +03:00
|
|
|
_DIAGASSERT(rqstp != NULL);
|
|
|
|
_DIAGASSERT(transp != NULL);
|
|
|
|
|
2000-06-03 03:11:05 +04:00
|
|
|
/*
|
1993-10-07 10:29:33 +03:00
|
|
|
* enforce "procnum 0 is echo" convention
|
|
|
|
*/
|
|
|
|
if (rqstp->rq_proc == NULLPROC) {
|
2000-07-06 07:10:34 +04:00
|
|
|
if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
|
|
|
|
FALSE) {
|
2000-06-03 03:11:05 +04:00
|
|
|
warnx("svc_sendreply failed");
|
|
|
|
}
|
1993-10-07 10:29:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
prog = rqstp->rq_prog;
|
2000-06-03 03:11:05 +04:00
|
|
|
vers = rqstp->rq_vers;
|
1993-10-07 10:29:33 +03:00
|
|
|
proc = rqstp->rq_proc;
|
2000-06-03 03:11:05 +04:00
|
|
|
mutex_lock(&proglst_lock);
|
|
|
|
for (pl = proglst; pl; pl = pl->p_nxt)
|
|
|
|
if (pl->p_prognum == prog && pl->p_procnum == proc &&
|
|
|
|
pl->p_versnum == vers &&
|
|
|
|
(strcmp(pl->p_netid, transp->xp_netid) == 0)) {
|
1993-10-07 10:29:33 +03:00
|
|
|
/* decode arguments into a CLEAN buffer */
|
2000-06-03 03:11:05 +04:00
|
|
|
xdrbuf = pl->p_xdrbuf;
|
|
|
|
/* Zero the arguments: reqd ! */
|
|
|
|
(void) memset(xdrbuf, 0, sizeof (pl->p_recvsz));
|
|
|
|
/*
|
|
|
|
* Assuming that sizeof (xdrbuf) would be enough
|
|
|
|
* for the arguments; if not then the program
|
|
|
|
* may bomb. BEWARE!
|
|
|
|
*/
|
|
|
|
if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
|
1993-10-07 10:29:33 +03:00
|
|
|
svcerr_decode(transp);
|
2000-06-03 03:11:05 +04:00
|
|
|
mutex_unlock(&proglst_lock);
|
1993-10-07 10:29:33 +03:00
|
|
|
return;
|
|
|
|
}
|
2000-06-03 03:11:05 +04:00
|
|
|
outdata = (*(pl->p_progname))(xdrbuf);
|
1994-12-04 04:12:39 +03:00
|
|
|
if (outdata == NULL &&
|
2000-06-03 03:11:05 +04:00
|
|
|
pl->p_outproc != (xdrproc_t) xdr_void){
|
1993-10-07 10:29:33 +03:00
|
|
|
/* there was an error */
|
2000-06-03 03:11:05 +04:00
|
|
|
mutex_unlock(&proglst_lock);
|
1993-10-07 10:29:33 +03:00
|
|
|
return;
|
2000-06-03 03:11:05 +04:00
|
|
|
}
|
|
|
|
if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
|
|
|
|
warnx(
|
|
|
|
"rpc: rpc_reg trouble replying to prog %u vers %u",
|
|
|
|
(unsigned)prog, (unsigned)vers);
|
|
|
|
mutex_unlock(&proglst_lock);
|
|
|
|
return;
|
|
|
|
}
|
1993-10-07 10:29:33 +03:00
|
|
|
/* free the decoded arguments */
|
2000-06-03 03:11:05 +04:00
|
|
|
(void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
|
|
|
|
mutex_unlock(&proglst_lock);
|
1993-10-07 10:29:33 +03:00
|
|
|
return;
|
|
|
|
}
|
2000-06-03 03:11:05 +04:00
|
|
|
mutex_unlock(&proglst_lock);
|
|
|
|
/* This should never happen */
|
|
|
|
warnx("rpc: rpc_reg: never registered prog %u vers %u",
|
|
|
|
(unsigned)prog, (unsigned)vers);
|
|
|
|
return;
|
1993-10-07 10:29:33 +03:00
|
|
|
}
|