xref: /minix3/lib/libc/rpc/clnt_simple.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: clnt_simple.c,v 1.33 2015/01/20 18:31:25 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
484d9c625SLionel Sambuc  * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras  *
684d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
784d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions are
884d9c625SLionel Sambuc  * met:
92fe8fb19SBen Gras  *
1084d9c625SLionel Sambuc  *     * Redistributions of source code must retain the above copyright
1184d9c625SLionel Sambuc  *       notice, this list of conditions and the following disclaimer.
1284d9c625SLionel Sambuc  *     * Redistributions in binary form must reproduce the above
1384d9c625SLionel Sambuc  *       copyright notice, this list of conditions and the following
1484d9c625SLionel Sambuc  *       disclaimer in the documentation and/or other materials
1584d9c625SLionel Sambuc  *       provided with the distribution.
1684d9c625SLionel Sambuc  *     * Neither the name of the "Oracle America, Inc." nor the names of its
1784d9c625SLionel Sambuc  *       contributors may be used to endorse or promote products derived
1884d9c625SLionel Sambuc  *       from this software without specific prior written permission.
192fe8fb19SBen Gras  *
2084d9c625SLionel Sambuc  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2184d9c625SLionel Sambuc  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2284d9c625SLionel Sambuc  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2384d9c625SLionel Sambuc  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2484d9c625SLionel Sambuc  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2584d9c625SLionel Sambuc  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2684d9c625SLionel Sambuc  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2784d9c625SLionel Sambuc  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2884d9c625SLionel Sambuc  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2984d9c625SLionel Sambuc  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3084d9c625SLionel Sambuc  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3184d9c625SLionel Sambuc  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras  */
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
352fe8fb19SBen Gras  */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras /* #ident	"@(#)clnt_simple.c	1.17	94/04/24 SMI" */
382fe8fb19SBen Gras 
392fe8fb19SBen Gras #include <sys/cdefs.h>
402fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
412fe8fb19SBen Gras #if 0
422fe8fb19SBen Gras static char sccsid[] = "@(#)clnt_simple.c 1.49 89/01/31 Copyr 1984 Sun Micro";
432fe8fb19SBen Gras #else
44*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: clnt_simple.c,v 1.33 2015/01/20 18:31:25 christos Exp $");
452fe8fb19SBen Gras #endif
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras 
482fe8fb19SBen Gras /*
492fe8fb19SBen Gras  * clnt_simple.c
502fe8fb19SBen Gras  * Simplified front end to client rpc.
512fe8fb19SBen Gras  *
522fe8fb19SBen Gras  */
532fe8fb19SBen Gras 
542fe8fb19SBen Gras #include "namespace.h"
552fe8fb19SBen Gras #include "reentrant.h"
562fe8fb19SBen Gras #include <sys/param.h>
572fe8fb19SBen Gras #include <stdio.h>
582fe8fb19SBen Gras #include <assert.h>
592fe8fb19SBen Gras #include <errno.h>
602fe8fb19SBen Gras #include <rpc/rpc.h>
612fe8fb19SBen Gras #include <string.h>
622fe8fb19SBen Gras #include <stdlib.h>
632fe8fb19SBen Gras #include <fcntl.h>
642fe8fb19SBen Gras #include <unistd.h>
652fe8fb19SBen Gras 
662fe8fb19SBen Gras #ifdef __weak_alias
672fe8fb19SBen Gras __weak_alias(rpc_call,_rpc_call)
682fe8fb19SBen Gras #endif
692fe8fb19SBen Gras 
702fe8fb19SBen Gras #ifndef MAXHOSTNAMELEN
712fe8fb19SBen Gras #define	MAXHOSTNAMELEN 64
722fe8fb19SBen Gras #endif
732fe8fb19SBen Gras 
742fe8fb19SBen Gras #ifndef NETIDLEN
752fe8fb19SBen Gras #define	NETIDLEN 32
762fe8fb19SBen Gras #endif
772fe8fb19SBen Gras 
782fe8fb19SBen Gras struct rpc_call_private {
792fe8fb19SBen Gras 	int	valid;			/* Is this entry valid ? */
802fe8fb19SBen Gras 	CLIENT	*client;		/* Client handle */
812fe8fb19SBen Gras 	pid_t	pid;			/* process-id at moment of creation */
822fe8fb19SBen Gras 	rpcprog_t prognum;		/* Program */
832fe8fb19SBen Gras 	rpcvers_t versnum;		/* Version */
842fe8fb19SBen Gras 	char	host[MAXHOSTNAMELEN];	/* Servers host */
852fe8fb19SBen Gras 	char	nettype[NETIDLEN];	/* Network type */
862fe8fb19SBen Gras };
872fe8fb19SBen Gras static struct rpc_call_private *rpc_call_private_main;
882fe8fb19SBen Gras 
892fe8fb19SBen Gras #ifdef _REENTRANT
90f14fb602SLionel Sambuc static void rpc_call_destroy(void *);
912fe8fb19SBen Gras 
922fe8fb19SBen Gras static void
rpc_call_destroy(void * vp)932fe8fb19SBen Gras rpc_call_destroy(void *vp)
942fe8fb19SBen Gras {
952fe8fb19SBen Gras 	struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
962fe8fb19SBen Gras 
972fe8fb19SBen Gras 	if (rcp) {
982fe8fb19SBen Gras 		if (rcp->client)
992fe8fb19SBen Gras 			CLNT_DESTROY(rcp->client);
1002fe8fb19SBen Gras 		free(rcp);
1012fe8fb19SBen Gras 	}
1022fe8fb19SBen Gras }
1032fe8fb19SBen Gras static thread_key_t rpc_call_key;
1042fe8fb19SBen Gras static once_t rpc_call_once = ONCE_INITIALIZER;
1052fe8fb19SBen Gras 
1062fe8fb19SBen Gras static void
rpc_call_setup(void)1072fe8fb19SBen Gras rpc_call_setup(void)
1082fe8fb19SBen Gras {
1092fe8fb19SBen Gras 
1102fe8fb19SBen Gras 	thr_keycreate(&rpc_call_key, rpc_call_destroy);
1112fe8fb19SBen Gras }
1122fe8fb19SBen Gras #endif
1132fe8fb19SBen Gras 
1142fe8fb19SBen Gras 
1152fe8fb19SBen Gras /*
1162fe8fb19SBen Gras  * This is the simplified interface to the client rpc layer.
1172fe8fb19SBen Gras  * The client handle is not destroyed here and is reused for
1182fe8fb19SBen Gras  * the future calls to same prog, vers, host and nettype combination.
1192fe8fb19SBen Gras  *
1202fe8fb19SBen Gras  * The total time available is 25 seconds.
1212fe8fb19SBen Gras  */
1222fe8fb19SBen Gras enum clnt_stat
rpc_call(const char * host,rpcprog_t prognum,rpcvers_t versnum,rpcproc_t procnum,xdrproc_t inproc,const char * in,xdrproc_t outproc,char * out,const char * nettype)123f14fb602SLionel Sambuc rpc_call(
124f14fb602SLionel Sambuc 	const char *	host,		/* host name */
125f14fb602SLionel Sambuc 	rpcprog_t	prognum,	/* program number */
126f14fb602SLionel Sambuc 	rpcvers_t	versnum,	/* version number */
127f14fb602SLionel Sambuc 	rpcproc_t	procnum,	/* procedure number */
128f14fb602SLionel Sambuc 	xdrproc_t	inproc,		/* in XDR procedures */
129f14fb602SLionel Sambuc 	const char *	in,		/* recv data */
130f14fb602SLionel Sambuc 	xdrproc_t	outproc,	/* out XDR procedures */
131f14fb602SLionel Sambuc 	char *		out,		/* send data */
132f14fb602SLionel Sambuc 	const char *	nettype)	/* nettype */
1332fe8fb19SBen Gras {
1342fe8fb19SBen Gras 	struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
1352fe8fb19SBen Gras 	enum clnt_stat clnt_stat;
1362fe8fb19SBen Gras 	struct timeval timeout, tottimeout;
1372fe8fb19SBen Gras 
1382fe8fb19SBen Gras 	_DIAGASSERT(host != NULL);
1392fe8fb19SBen Gras 	/* XXX: in may be NULL ??? */
1402fe8fb19SBen Gras 	/* XXX: out may be NULL ??? */
1412fe8fb19SBen Gras 	/* XXX: nettype may be NULL ??? */
1422fe8fb19SBen Gras 
1432fe8fb19SBen Gras #ifdef _REENTRANT
144*0a6a1f1dSLionel Sambuc 	if (__isthreaded) {
1452fe8fb19SBen Gras 		thr_once(&rpc_call_once, rpc_call_setup);
1462fe8fb19SBen Gras 		rcp = thr_getspecific(rpc_call_key);
147*0a6a1f1dSLionel Sambuc 	} else
1482fe8fb19SBen Gras #endif
149*0a6a1f1dSLionel Sambuc 		rcp = rpc_call_private_main;
1502fe8fb19SBen Gras 	if (rcp == NULL) {
1512fe8fb19SBen Gras 		rcp = malloc(sizeof (*rcp));
1522fe8fb19SBen Gras 		if (rcp == NULL) {
1532fe8fb19SBen Gras 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1542fe8fb19SBen Gras 			rpc_createerr.cf_error.re_errno = errno;
1552fe8fb19SBen Gras 			return (rpc_createerr.cf_stat);
1562fe8fb19SBen Gras 		}
157*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
158*0a6a1f1dSLionel Sambuc 		if (__isthreaded)
1592fe8fb19SBen Gras 			thr_setspecific(rpc_call_key, (void *) rcp);
160*0a6a1f1dSLionel Sambuc 		else
161*0a6a1f1dSLionel Sambuc #endif
162*0a6a1f1dSLionel Sambuc 			rpc_call_private_main = rcp;
1632fe8fb19SBen Gras 		rcp->valid = 0;
1642fe8fb19SBen Gras 		rcp->client = NULL;
1652fe8fb19SBen Gras 	}
1662fe8fb19SBen Gras 	if ((nettype == NULL) || (nettype[0] == 0))
1672fe8fb19SBen Gras 		nettype = "netpath";
1682fe8fb19SBen Gras 	if (!(rcp->valid && rcp->pid == getpid() &&
1692fe8fb19SBen Gras 		(rcp->prognum == prognum) &&
1702fe8fb19SBen Gras 		(rcp->versnum == versnum) &&
1712fe8fb19SBen Gras 		(!strcmp(rcp->host, host)) &&
1722fe8fb19SBen Gras 		(!strcmp(rcp->nettype, nettype)))) {
1732fe8fb19SBen Gras 		int fd;
1742fe8fb19SBen Gras 
1752fe8fb19SBen Gras 		rcp->valid = 0;
1762fe8fb19SBen Gras 		if (rcp->client)
1772fe8fb19SBen Gras 			CLNT_DESTROY(rcp->client);
1782fe8fb19SBen Gras 		/*
1792fe8fb19SBen Gras 		 * Using the first successful transport for that type
1802fe8fb19SBen Gras 		 */
1812fe8fb19SBen Gras 		rcp->client = clnt_create(host, prognum, versnum, nettype);
1822fe8fb19SBen Gras 		rcp->pid = getpid();
1832fe8fb19SBen Gras 		if (rcp->client == NULL) {
1842fe8fb19SBen Gras 			return (rpc_createerr.cf_stat);
1852fe8fb19SBen Gras 		}
1862fe8fb19SBen Gras 		/*
1872fe8fb19SBen Gras 		 * Set time outs for connectionless case.  Do it
1882fe8fb19SBen Gras 		 * unconditionally.  Faster than doing a t_getinfo()
1892fe8fb19SBen Gras 		 * and then doing the right thing.
1902fe8fb19SBen Gras 		 */
1912fe8fb19SBen Gras 		timeout.tv_usec = 0;
1922fe8fb19SBen Gras 		timeout.tv_sec = 5;
1932fe8fb19SBen Gras 		(void) CLNT_CONTROL(rcp->client,
1942fe8fb19SBen Gras 				CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
1952fe8fb19SBen Gras 		if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
1962fe8fb19SBen Gras 			(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
1972fe8fb19SBen Gras 		rcp->prognum = prognum;
1982fe8fb19SBen Gras 		rcp->versnum = versnum;
1992fe8fb19SBen Gras 		if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
2002fe8fb19SBen Gras 		    (strlen(nettype) < (size_t)NETIDLEN)) {
2012fe8fb19SBen Gras 			(void) strcpy(rcp->host, host);
2022fe8fb19SBen Gras 			(void) strcpy(rcp->nettype, nettype);
2032fe8fb19SBen Gras 			rcp->valid = 1;
2042fe8fb19SBen Gras 		} else {
2052fe8fb19SBen Gras 			rcp->valid = 0;
2062fe8fb19SBen Gras 		}
2072fe8fb19SBen Gras 	} /* else reuse old client */
2082fe8fb19SBen Gras 	tottimeout.tv_sec = 25;
2092fe8fb19SBen Gras 	tottimeout.tv_usec = 0;
2102fe8fb19SBen Gras 	clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, in,
2112fe8fb19SBen Gras 	    outproc, out, tottimeout);
2122fe8fb19SBen Gras 	/*
2132fe8fb19SBen Gras 	 * if call failed, empty cache
2142fe8fb19SBen Gras 	 */
2152fe8fb19SBen Gras 	if (clnt_stat != RPC_SUCCESS)
2162fe8fb19SBen Gras 		rcp->valid = 0;
2172fe8fb19SBen Gras 	return (clnt_stat);
2182fe8fb19SBen Gras }
219