Home | History | Annotate | Line # | Download | only in rpc
clnt_simple.c revision 1.30.6.1
      1  1.30.6.1      yamt /*	$NetBSD: clnt_simple.c,v 1.30.6.1 2012/04/17 00:05:22 yamt 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.20      fvdl /*
     32      1.20      fvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     33      1.20      fvdl  */
     34      1.20      fvdl 
     35      1.20      fvdl /* #ident	"@(#)clnt_simple.c	1.17	94/04/24 SMI" */
     36       1.1       cgd 
     37      1.24    itojun #include <sys/cdefs.h>
     38      1.24    itojun #if defined(LIBC_SCCS) && !defined(lint)
     39       1.8  christos #if 0
     40      1.20      fvdl static char sccsid[] = "@(#)clnt_simple.c 1.49 89/01/31 Copyr 1984 Sun Micro";
     41      1.24    itojun #else
     42  1.30.6.1      yamt __RCSID("$NetBSD: clnt_simple.c,v 1.30.6.1 2012/04/17 00:05:22 yamt Exp $");
     43       1.8  christos #endif
     44       1.1       cgd #endif
     45       1.1       cgd 
     46      1.20      fvdl /*
     47       1.1       cgd  * clnt_simple.c
     48      1.20      fvdl  * Simplified front end to client rpc.
     49       1.1       cgd  *
     50       1.1       cgd  */
     51       1.1       cgd 
     52       1.9       jtc #include "namespace.h"
     53      1.20      fvdl #include "reentrant.h"
     54      1.20      fvdl #include <sys/param.h>
     55       1.1       cgd #include <stdio.h>
     56      1.22     lukem #include <assert.h>
     57      1.20      fvdl #include <errno.h>
     58      1.20      fvdl #include <rpc/rpc.h>
     59      1.20      fvdl #include <string.h>
     60       1.1       cgd #include <stdlib.h>
     61      1.20      fvdl #include <fcntl.h>
     62       1.8  christos #include <unistd.h>
     63      1.12     lukem 
     64      1.20      fvdl #ifdef __weak_alias
     65      1.20      fvdl __weak_alias(rpc_call,_rpc_call)
     66      1.20      fvdl #endif
     67      1.20      fvdl 
     68      1.20      fvdl #ifndef MAXHOSTNAMELEN
     69      1.20      fvdl #define	MAXHOSTNAMELEN 64
     70      1.20      fvdl #endif
     71      1.20      fvdl 
     72      1.20      fvdl #ifndef NETIDLEN
     73      1.20      fvdl #define	NETIDLEN 32
     74      1.20      fvdl #endif
     75      1.20      fvdl 
     76      1.20      fvdl struct rpc_call_private {
     77      1.20      fvdl 	int	valid;			/* Is this entry valid ? */
     78      1.20      fvdl 	CLIENT	*client;		/* Client handle */
     79      1.20      fvdl 	pid_t	pid;			/* process-id at moment of creation */
     80      1.20      fvdl 	rpcprog_t prognum;		/* Program */
     81      1.20      fvdl 	rpcvers_t versnum;		/* Version */
     82      1.20      fvdl 	char	host[MAXHOSTNAMELEN];	/* Servers host */
     83      1.20      fvdl 	char	nettype[NETIDLEN];	/* Network type */
     84      1.20      fvdl };
     85      1.20      fvdl static struct rpc_call_private *rpc_call_private_main;
     86      1.20      fvdl 
     87      1.23   thorpej #ifdef _REENTRANT
     88  1.30.6.1      yamt static void rpc_call_destroy(void *);
     89      1.20      fvdl 
     90      1.20      fvdl static void
     91      1.20      fvdl rpc_call_destroy(void *vp)
     92      1.20      fvdl {
     93      1.21  christos 	struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
     94       1.9       jtc 
     95      1.20      fvdl 	if (rcp) {
     96      1.20      fvdl 		if (rcp->client)
     97      1.20      fvdl 			CLNT_DESTROY(rcp->client);
     98      1.20      fvdl 		free(rcp);
     99      1.20      fvdl 	}
    100      1.20      fvdl }
    101      1.23   thorpej static thread_key_t rpc_call_key;
    102      1.23   thorpej static once_t rpc_call_once = ONCE_INITIALIZER;
    103      1.23   thorpej 
    104      1.23   thorpej static void
    105      1.23   thorpej rpc_call_setup(void)
    106      1.23   thorpej {
    107      1.23   thorpej 
    108      1.23   thorpej 	thr_keycreate(&rpc_call_key, rpc_call_destroy);
    109      1.23   thorpej }
    110       1.9       jtc #endif
    111       1.1       cgd 
    112      1.23   thorpej 
    113      1.20      fvdl /*
    114      1.20      fvdl  * This is the simplified interface to the client rpc layer.
    115      1.20      fvdl  * The client handle is not destroyed here and is reused for
    116      1.20      fvdl  * the future calls to same prog, vers, host and nettype combination.
    117      1.20      fvdl  *
    118      1.20      fvdl  * The total time available is 25 seconds.
    119      1.20      fvdl  */
    120      1.20      fvdl enum clnt_stat
    121  1.30.6.1      yamt rpc_call(
    122  1.30.6.1      yamt 	const char *	host,		/* host name */
    123  1.30.6.1      yamt 	rpcprog_t	prognum,	/* program number */
    124  1.30.6.1      yamt 	rpcvers_t	versnum,	/* version number */
    125  1.30.6.1      yamt 	rpcproc_t	procnum,	/* procedure number */
    126  1.30.6.1      yamt 	xdrproc_t	inproc,		/* in XDR procedures */
    127  1.30.6.1      yamt 	const char *	in,		/* recv data */
    128  1.30.6.1      yamt 	xdrproc_t	outproc,	/* out XDR procedures */
    129  1.30.6.1      yamt 	char *		out,		/* send data */
    130  1.30.6.1      yamt 	const char *	nettype)	/* nettype */
    131       1.1       cgd {
    132      1.20      fvdl 	struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
    133       1.1       cgd 	enum clnt_stat clnt_stat;
    134       1.1       cgd 	struct timeval timeout, tottimeout;
    135      1.22     lukem 
    136      1.22     lukem 	_DIAGASSERT(host != NULL);
    137      1.22     lukem 	/* XXX: in may be NULL ??? */
    138      1.22     lukem 	/* XXX: out may be NULL ??? */
    139      1.22     lukem 	/* XXX: nettype may be NULL ??? */
    140      1.17     lukem 
    141      1.23   thorpej #ifdef _REENTRANT
    142      1.23   thorpej 	if (__isthreaded == 0) {
    143      1.20      fvdl 		rcp = rpc_call_private_main;
    144      1.20      fvdl 	} else {
    145      1.23   thorpej 		thr_once(&rpc_call_once, rpc_call_setup);
    146      1.23   thorpej 		rcp = thr_getspecific(rpc_call_key);
    147       1.1       cgd 	}
    148      1.20      fvdl #else
    149      1.20      fvdl 	rcp = rpc_call_private_main;
    150      1.20      fvdl #endif
    151      1.21  christos 	if (rcp == NULL) {
    152      1.21  christos 		rcp = malloc(sizeof (*rcp));
    153      1.21  christos 		if (rcp == NULL) {
    154      1.20      fvdl 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    155      1.20      fvdl 			rpc_createerr.cf_error.re_errno = errno;
    156      1.20      fvdl 			return (rpc_createerr.cf_stat);
    157      1.20      fvdl 		}
    158      1.23   thorpej 		if (__isthreaded == 0)
    159      1.20      fvdl 			rpc_call_private_main = rcp;
    160      1.20      fvdl 		else
    161      1.20      fvdl 			thr_setspecific(rpc_call_key, (void *) rcp);
    162      1.20      fvdl 		rcp->valid = 0;
    163      1.20      fvdl 		rcp->client = NULL;
    164       1.1       cgd 	}
    165      1.25      fvdl 	if ((nettype == NULL) || (nettype[0] == 0))
    166      1.20      fvdl 		nettype = "netpath";
    167      1.20      fvdl 	if (!(rcp->valid && rcp->pid == getpid() &&
    168      1.20      fvdl 		(rcp->prognum == prognum) &&
    169      1.20      fvdl 		(rcp->versnum == versnum) &&
    170      1.20      fvdl 		(!strcmp(rcp->host, host)) &&
    171      1.20      fvdl 		(!strcmp(rcp->nettype, nettype)))) {
    172      1.20      fvdl 		int fd;
    173      1.20      fvdl 
    174      1.20      fvdl 		rcp->valid = 0;
    175      1.20      fvdl 		if (rcp->client)
    176      1.20      fvdl 			CLNT_DESTROY(rcp->client);
    177      1.20      fvdl 		/*
    178      1.20      fvdl 		 * Using the first successful transport for that type
    179      1.20      fvdl 		 */
    180      1.20      fvdl 		rcp->client = clnt_create(host, prognum, versnum, nettype);
    181      1.20      fvdl 		rcp->pid = getpid();
    182      1.21  christos 		if (rcp->client == NULL) {
    183      1.20      fvdl 			return (rpc_createerr.cf_stat);
    184       1.1       cgd 		}
    185      1.20      fvdl 		/*
    186      1.20      fvdl 		 * Set time outs for connectionless case.  Do it
    187      1.20      fvdl 		 * unconditionally.  Faster than doing a t_getinfo()
    188      1.20      fvdl 		 * and then doing the right thing.
    189      1.20      fvdl 		 */
    190       1.1       cgd 		timeout.tv_usec = 0;
    191       1.1       cgd 		timeout.tv_sec = 5;
    192      1.20      fvdl 		(void) CLNT_CONTROL(rcp->client,
    193      1.21  christos 				CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
    194      1.21  christos 		if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
    195      1.29  christos 			(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
    196      1.20      fvdl 		rcp->prognum = prognum;
    197      1.20      fvdl 		rcp->versnum = versnum;
    198      1.20      fvdl 		if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
    199      1.20      fvdl 		    (strlen(nettype) < (size_t)NETIDLEN)) {
    200      1.20      fvdl 			(void) strcpy(rcp->host, host);
    201      1.20      fvdl 			(void) strcpy(rcp->nettype, nettype);
    202      1.20      fvdl 			rcp->valid = 1;
    203      1.20      fvdl 		} else {
    204      1.20      fvdl 			rcp->valid = 0;
    205      1.20      fvdl 		}
    206      1.20      fvdl 	} /* else reuse old client */
    207       1.1       cgd 	tottimeout.tv_sec = 25;
    208       1.1       cgd 	tottimeout.tv_usec = 0;
    209      1.27      yamt 	clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, in,
    210      1.21  christos 	    outproc, out, tottimeout);
    211      1.20      fvdl 	/*
    212       1.1       cgd 	 * if call failed, empty cache
    213       1.1       cgd 	 */
    214       1.1       cgd 	if (clnt_stat != RPC_SUCCESS)
    215      1.20      fvdl 		rcp->valid = 0;
    216      1.20      fvdl 	return (clnt_stat);
    217       1.1       cgd }
    218