Home | History | Annotate | Line # | Download | only in rpc
svc_simple.c revision 1.27
      1  1.27     lukem /*	$NetBSD: svc_simple.c,v 1.27 2005/06/01 05:54:07 lukem Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5   1.1       cgd  * unrestricted use provided that this legend is included on all tape
      6   1.1       cgd  * media and as a part of the software program in whole or part.  Users
      7   1.1       cgd  * may copy or modify Sun RPC without charge, but are not authorized
      8   1.1       cgd  * to license or distribute it to anyone else except as part of a product or
      9   1.1       cgd  * program developed by the user.
     10   1.1       cgd  *
     11   1.1       cgd  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12   1.1       cgd  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13   1.1       cgd  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14   1.1       cgd  *
     15   1.1       cgd  * Sun RPC is provided with no support and without any obligation on the
     16   1.1       cgd  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17   1.1       cgd  * modification or enhancement.
     18   1.1       cgd  *
     19   1.1       cgd  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20   1.1       cgd  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21   1.1       cgd  * OR ANY PART THEREOF.
     22   1.1       cgd  *
     23   1.1       cgd  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24   1.1       cgd  * or profits or other special, indirect and consequential damages, even if
     25   1.1       cgd  * Sun has been advised of the possibility of such damages.
     26   1.1       cgd  *
     27   1.1       cgd  * Sun Microsystems, Inc.
     28   1.1       cgd  * 2550 Garcia Avenue
     29   1.1       cgd  * Mountain View, California  94043
     30   1.1       cgd  */
     31  1.19      fvdl /*
     32  1.19      fvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     33  1.19      fvdl  */
     34   1.1       cgd 
     35  1.19      fvdl /* #pragma ident	"@(#)svc_simple.c	1.18	94/04/24 SMI" */
     36   1.1       cgd 
     37  1.19      fvdl /*
     38   1.1       cgd  * svc_simple.c
     39   1.1       cgd  * Simplified front end to rpc.
     40  1.19      fvdl  */
     41  1.19      fvdl 
     42  1.19      fvdl /*
     43  1.19      fvdl  * This interface creates a virtual listener for all the services
     44  1.19      fvdl  * started thru rpc_reg(). It listens on the same endpoint for
     45  1.19      fvdl  * all the services and then executes the corresponding service
     46  1.19      fvdl  * for the given prognum and procnum.
     47   1.1       cgd  */
     48  1.26    itojun 
     49  1.26    itojun #include <sys/cdefs.h>
     50  1.26    itojun #if defined(LIBC_SCCS) && !defined(lint)
     51  1.27     lukem __RCSID("$NetBSD: svc_simple.c,v 1.27 2005/06/01 05:54:07 lukem Exp $");
     52  1.26    itojun #endif
     53   1.1       cgd 
     54   1.7       jtc #include "namespace.h"
     55  1.19      fvdl #include "reentrant.h"
     56  1.10     lukem #include <sys/types.h>
     57  1.19      fvdl #include <rpc/rpc.h>
     58  1.20  christos #include <rpc/nettype.h>
     59  1.21     lukem #include <assert.h>
     60  1.21     lukem #include <err.h>
     61   1.1       cgd #include <stdio.h>
     62   1.2       cgd #include <stdlib.h>
     63   1.4       jtc #include <string.h>
     64  1.10     lukem 
     65  1.22      fvdl #include "rpc_internal.h"
     66   1.7       jtc 
     67   1.7       jtc #ifdef __weak_alias
     68  1.19      fvdl __weak_alias(rpc_reg,_rpc_reg)
     69   1.7       jtc #endif
     70   1.1       cgd 
     71  1.19      fvdl static void universal __P((struct svc_req *, SVCXPRT *));
     72  1.19      fvdl 
     73   1.1       cgd static struct proglst {
     74  1.19      fvdl 	char *(*p_progname) __P((char *));
     75  1.19      fvdl 	rpcprog_t p_prognum;
     76  1.19      fvdl 	rpcvers_t p_versnum;
     77  1.19      fvdl 	rpcproc_t p_procnum;
     78  1.19      fvdl 	SVCXPRT *p_transp;
     79  1.19      fvdl 	char *p_netid;
     80  1.19      fvdl 	char *p_xdrbuf;
     81  1.19      fvdl 	int p_recvsz;
     82   1.1       cgd 	xdrproc_t p_inproc, p_outproc;
     83   1.1       cgd 	struct proglst *p_nxt;
     84   1.1       cgd } *proglst;
     85   1.6  christos 
     86  1.19      fvdl static const char rpc_reg_err[] = "%s: %s";
     87  1.19      fvdl static const char rpc_reg_msg[] = "rpc_reg: ";
     88  1.19      fvdl static const char __reg_err1[] = "can't find appropriate transport";
     89  1.19      fvdl static const char __reg_err2[] = "can't get protocol info";
     90  1.19      fvdl static const char __reg_err3[] = "unsupported transport size";
     91  1.19      fvdl static const char __no_mem_str[] = "out of memory";
     92   1.1       cgd 
     93  1.19      fvdl /*
     94  1.19      fvdl  * For simplified, easy to use kind of rpc interfaces.
     95  1.19      fvdl  * nettype indicates the type of transport on which the service will be
     96  1.19      fvdl  * listening. Used for conservation of the system resource. Only one
     97  1.19      fvdl  * handle is created for all the services (actually one of each netid)
     98  1.19      fvdl  * and same xdrbuf is used for same netid. The size of the arguments
     99  1.19      fvdl  * is also limited by the recvsize for that transport, even if it is
    100  1.19      fvdl  * a COTS transport. This may be wrong, but for cases like these, they
    101  1.19      fvdl  * should not use the simplified interfaces like this.
    102  1.19      fvdl  */
    103   1.6  christos 
    104   1.5       jtc int
    105  1.19      fvdl rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
    106  1.19      fvdl 	rpcprog_t prognum;			/* program number */
    107  1.19      fvdl 	rpcvers_t versnum;			/* version number */
    108  1.19      fvdl 	rpcproc_t procnum;			/* procedure number */
    109  1.19      fvdl 	char *(*progname) __P((char *)); /* Server routine */
    110  1.19      fvdl 	xdrproc_t inproc, outproc;	/* in/out XDR procedures */
    111  1.19      fvdl 	char *nettype;			/* nettype */
    112   1.1       cgd {
    113  1.19      fvdl 	struct netconfig *nconf;
    114  1.19      fvdl 	int done = FALSE;
    115  1.19      fvdl 	void *handle;
    116  1.23   thorpej #ifdef _REENTRANT
    117  1.19      fvdl 	extern mutex_t proglst_lock;
    118  1.19      fvdl #endif
    119  1.19      fvdl 
    120   1.1       cgd 	if (procnum == NULLPROC) {
    121  1.19      fvdl 		warnx("%s can't reassign procedure number %u", rpc_reg_msg,
    122  1.19      fvdl 			NULLPROC);
    123  1.19      fvdl 		return (-1);
    124  1.19      fvdl 	}
    125  1.19      fvdl 
    126  1.19      fvdl 	if (nettype == NULL)
    127  1.19      fvdl 		nettype = "netpath";		/* The default behavior */
    128  1.19      fvdl 	if ((handle = __rpc_setconf(nettype)) == NULL) {
    129  1.19      fvdl 		warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
    130   1.1       cgd 		return (-1);
    131   1.1       cgd 	}
    132  1.19      fvdl /* VARIABLES PROTECTED BY proglst_lock: proglst */
    133  1.19      fvdl 	mutex_lock(&proglst_lock);
    134  1.20  christos 	while ((nconf = __rpc_getconf(handle)) != NULL) {
    135  1.19      fvdl 		struct proglst *pl;
    136  1.19      fvdl 		SVCXPRT *svcxprt;
    137  1.19      fvdl 		int madenow;
    138  1.19      fvdl 		u_int recvsz;
    139  1.19      fvdl 		char *xdrbuf;
    140  1.19      fvdl 		char *netid;
    141  1.19      fvdl 
    142  1.19      fvdl 		madenow = FALSE;
    143  1.20  christos 		svcxprt = NULL;
    144  1.27     lukem 		recvsz = 0;
    145  1.27     lukem 		xdrbuf = NULL;
    146  1.27     lukem 		netid = NULL;
    147  1.19      fvdl 		for (pl = proglst; pl; pl = pl->p_nxt)
    148  1.19      fvdl 			if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
    149  1.19      fvdl 				svcxprt = pl->p_transp;
    150  1.19      fvdl 				xdrbuf = pl->p_xdrbuf;
    151  1.19      fvdl 				recvsz = pl->p_recvsz;
    152  1.19      fvdl 				netid = pl->p_netid;
    153  1.19      fvdl 				break;
    154  1.19      fvdl 			}
    155  1.19      fvdl 
    156  1.20  christos 		if (svcxprt == NULL) {
    157  1.19      fvdl 			struct __rpc_sockinfo si;
    158  1.19      fvdl 
    159  1.20  christos 			svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
    160  1.20  christos 			if (svcxprt == NULL)
    161  1.19      fvdl 				continue;
    162  1.19      fvdl 			if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
    163  1.19      fvdl 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
    164  1.19      fvdl 				SVC_DESTROY(svcxprt);
    165  1.19      fvdl 				continue;
    166  1.19      fvdl 			}
    167  1.19      fvdl 			recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
    168  1.19      fvdl 			if (recvsz == 0) {
    169  1.19      fvdl 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
    170  1.19      fvdl 				SVC_DESTROY(svcxprt);
    171  1.19      fvdl 				continue;
    172  1.19      fvdl 			}
    173  1.19      fvdl 			if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) ||
    174  1.19      fvdl 				((netid = strdup(nconf->nc_netid)) == NULL)) {
    175  1.19      fvdl 				warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
    176  1.19      fvdl 				SVC_DESTROY(svcxprt);
    177  1.19      fvdl 				break;
    178  1.19      fvdl 			}
    179  1.19      fvdl 			madenow = TRUE;
    180  1.19      fvdl 		}
    181  1.19      fvdl 		/*
    182  1.19      fvdl 		 * Check if this (program, version, netid) had already been
    183  1.19      fvdl 		 * registered.  The check may save a few RPC calls to rpcbind
    184  1.19      fvdl 		 */
    185  1.19      fvdl 		for (pl = proglst; pl; pl = pl->p_nxt)
    186  1.19      fvdl 			if ((pl->p_prognum == prognum) &&
    187  1.19      fvdl 				(pl->p_versnum == versnum) &&
    188  1.19      fvdl 				(strcmp(pl->p_netid, netid) == 0))
    189  1.19      fvdl 				break;
    190  1.19      fvdl 		if (pl == NULL) { /* Not yet */
    191  1.19      fvdl 			(void) rpcb_unset(prognum, versnum, nconf);
    192  1.19      fvdl 		} else {
    193  1.19      fvdl 			/* so that svc_reg does not call rpcb_set() */
    194  1.19      fvdl 			nconf = NULL;
    195  1.19      fvdl 		}
    196  1.19      fvdl 
    197  1.19      fvdl 		if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
    198  1.19      fvdl 			warnx("%s couldn't register prog %u vers %u for %s",
    199  1.19      fvdl 				rpc_reg_msg, (unsigned)prognum,
    200  1.19      fvdl 				(unsigned)versnum, netid);
    201  1.19      fvdl 			if (madenow) {
    202  1.19      fvdl 				SVC_DESTROY(svcxprt);
    203  1.19      fvdl 				free(xdrbuf);
    204  1.19      fvdl 				free(netid);
    205  1.19      fvdl 			}
    206  1.19      fvdl 			continue;
    207  1.19      fvdl 		}
    208  1.19      fvdl 
    209  1.20  christos 		pl = malloc(sizeof (struct proglst));
    210  1.20  christos 		if (pl == NULL) {
    211  1.19      fvdl 			warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
    212  1.19      fvdl 			if (madenow) {
    213  1.19      fvdl 				SVC_DESTROY(svcxprt);
    214  1.19      fvdl 				free(xdrbuf);
    215  1.19      fvdl 				free(netid);
    216  1.19      fvdl 			}
    217  1.19      fvdl 			break;
    218   1.1       cgd 		}
    219  1.19      fvdl 		pl->p_progname = progname;
    220  1.19      fvdl 		pl->p_prognum = prognum;
    221  1.19      fvdl 		pl->p_versnum = versnum;
    222  1.19      fvdl 		pl->p_procnum = procnum;
    223  1.19      fvdl 		pl->p_inproc = inproc;
    224  1.19      fvdl 		pl->p_outproc = outproc;
    225  1.19      fvdl 		pl->p_transp = svcxprt;
    226  1.19      fvdl 		pl->p_xdrbuf = xdrbuf;
    227  1.19      fvdl 		pl->p_recvsz = recvsz;
    228  1.19      fvdl 		pl->p_netid = netid;
    229  1.19      fvdl 		pl->p_nxt = proglst;
    230  1.19      fvdl 		proglst = pl;
    231  1.19      fvdl 		done = TRUE;
    232   1.1       cgd 	}
    233  1.19      fvdl 	__rpc_endconf(handle);
    234  1.19      fvdl 	mutex_unlock(&proglst_lock);
    235  1.19      fvdl 
    236  1.19      fvdl 	if (done == FALSE) {
    237  1.19      fvdl 		warnx("%s cant find suitable transport for %s",
    238  1.19      fvdl 			rpc_reg_msg, nettype);
    239   1.1       cgd 		return (-1);
    240   1.1       cgd 	}
    241   1.1       cgd 	return (0);
    242   1.1       cgd }
    243   1.1       cgd 
    244  1.19      fvdl /*
    245  1.19      fvdl  * The universal handler for the services registered using registerrpc.
    246  1.19      fvdl  * It handles both the connectionless and the connection oriented cases.
    247  1.19      fvdl  */
    248  1.19      fvdl 
    249   1.1       cgd static void
    250   1.1       cgd universal(rqstp, transp)
    251   1.1       cgd 	struct svc_req *rqstp;
    252   1.1       cgd 	SVCXPRT *transp;
    253   1.1       cgd {
    254  1.19      fvdl 	rpcprog_t prog;
    255  1.19      fvdl 	rpcvers_t vers;
    256  1.19      fvdl 	rpcproc_t proc;
    257   1.1       cgd 	char *outdata;
    258  1.19      fvdl 	char *xdrbuf;
    259  1.19      fvdl 	struct proglst *pl;
    260  1.23   thorpej #ifdef _REENTRANT
    261  1.19      fvdl 	extern mutex_t proglst_lock;
    262  1.19      fvdl #endif
    263  1.21     lukem 
    264  1.21     lukem 	_DIAGASSERT(rqstp != NULL);
    265  1.21     lukem 	_DIAGASSERT(transp != NULL);
    266   1.1       cgd 
    267  1.19      fvdl 	/*
    268   1.1       cgd 	 * enforce "procnum 0 is echo" convention
    269   1.1       cgd 	 */
    270   1.1       cgd 	if (rqstp->rq_proc == NULLPROC) {
    271  1.20  christos 		if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
    272  1.20  christos 		    FALSE) {
    273  1.19      fvdl 			warnx("svc_sendreply failed");
    274  1.19      fvdl 		}
    275   1.1       cgd 		return;
    276   1.1       cgd 	}
    277   1.1       cgd 	prog = rqstp->rq_prog;
    278  1.19      fvdl 	vers = rqstp->rq_vers;
    279   1.1       cgd 	proc = rqstp->rq_proc;
    280  1.19      fvdl 	mutex_lock(&proglst_lock);
    281  1.19      fvdl 	for (pl = proglst; pl; pl = pl->p_nxt)
    282  1.19      fvdl 		if (pl->p_prognum == prog && pl->p_procnum == proc &&
    283  1.19      fvdl 			pl->p_versnum == vers &&
    284  1.19      fvdl 			(strcmp(pl->p_netid, transp->xp_netid) == 0)) {
    285   1.1       cgd 			/* decode arguments into a CLEAN buffer */
    286  1.19      fvdl 			xdrbuf = pl->p_xdrbuf;
    287  1.19      fvdl 			/* Zero the arguments: reqd ! */
    288  1.25  christos 			(void) memset(xdrbuf, 0, (size_t)pl->p_recvsz);
    289  1.19      fvdl 			/*
    290  1.19      fvdl 			 * Assuming that sizeof (xdrbuf) would be enough
    291  1.19      fvdl 			 * for the arguments; if not then the program
    292  1.19      fvdl 			 * may bomb. BEWARE!
    293  1.19      fvdl 			 */
    294  1.19      fvdl 			if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
    295   1.1       cgd 				svcerr_decode(transp);
    296  1.19      fvdl 				mutex_unlock(&proglst_lock);
    297   1.1       cgd 				return;
    298   1.1       cgd 			}
    299  1.19      fvdl 			outdata = (*(pl->p_progname))(xdrbuf);
    300   1.2       cgd 			if (outdata == NULL &&
    301  1.19      fvdl 				pl->p_outproc != (xdrproc_t) xdr_void){
    302   1.1       cgd 				/* there was an error */
    303  1.19      fvdl 				mutex_unlock(&proglst_lock);
    304   1.1       cgd 				return;
    305  1.19      fvdl 			}
    306  1.19      fvdl 			if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
    307  1.19      fvdl 				warnx(
    308  1.19      fvdl 			"rpc: rpc_reg trouble replying to prog %u vers %u",
    309  1.19      fvdl 				(unsigned)prog, (unsigned)vers);
    310  1.19      fvdl 				mutex_unlock(&proglst_lock);
    311  1.19      fvdl 				return;
    312  1.19      fvdl 			}
    313   1.1       cgd 			/* free the decoded arguments */
    314  1.19      fvdl 			(void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
    315  1.19      fvdl 			mutex_unlock(&proglst_lock);
    316   1.1       cgd 			return;
    317   1.1       cgd 		}
    318  1.19      fvdl 	mutex_unlock(&proglst_lock);
    319  1.19      fvdl 	/* This should never happen */
    320  1.19      fvdl 	warnx("rpc: rpc_reg: never registered prog %u vers %u",
    321  1.19      fvdl 		(unsigned)prog, (unsigned)vers);
    322  1.19      fvdl 	return;
    323   1.1       cgd }
    324