xref: /freebsd/lib/libc/rpc/clnt_simple.c (revision 6f88d2a870a280d1ee52ed56c970ea70dbe92dbf)
18360efbdSAlfred Perlstein /*	$NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $	*/
28360efbdSAlfred Perlstein 
399064799SGarrett Wollman /*
499064799SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
599064799SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
699064799SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
799064799SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
899064799SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
999064799SGarrett Wollman  * program developed by the user.
1099064799SGarrett Wollman  *
1199064799SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1299064799SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1399064799SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1499064799SGarrett Wollman  *
1599064799SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
1699064799SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
1799064799SGarrett Wollman  * modification or enhancement.
1899064799SGarrett Wollman  *
1999064799SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2099064799SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2199064799SGarrett Wollman  * OR ANY PART THEREOF.
2299064799SGarrett Wollman  *
2399064799SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2499064799SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
2599064799SGarrett Wollman  * Sun has been advised of the possibility of such damages.
2699064799SGarrett Wollman  *
2799064799SGarrett Wollman  * Sun Microsystems, Inc.
2899064799SGarrett Wollman  * 2550 Garcia Avenue
2999064799SGarrett Wollman  * Mountain View, California  94043
3099064799SGarrett Wollman  */
318360efbdSAlfred Perlstein /*
328360efbdSAlfred Perlstein  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
338360efbdSAlfred Perlstein  */
3499064799SGarrett Wollman 
3599064799SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
36a986ef57SDavid E. O'Brien static char *sccsid2 = "from: @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
37d3d20c82SDavid E. O'Brien static char *sccsid = "from: @(#)clnt_simple.c	2.2 88/08/01 4.0 RPCSRC";
3899064799SGarrett Wollman #endif
39d3d20c82SDavid E. O'Brien #include <sys/cdefs.h>
40d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$");
4199064799SGarrett Wollman 
4299064799SGarrett Wollman /*
4399064799SGarrett Wollman  * clnt_simple.c
448360efbdSAlfred Perlstein  * Simplified front end to client rpc.
4599064799SGarrett Wollman  *
4699064799SGarrett Wollman  */
4799064799SGarrett Wollman 
48d201fe46SDaniel Eischen #include "namespace.h"
499f5afc13SIan Dowse #include "reentrant.h"
500ab74c6fSPeter Wemm #include <sys/param.h>
5199064799SGarrett Wollman #include <stdio.h>
528360efbdSAlfred Perlstein #include <errno.h>
5399064799SGarrett Wollman #include <rpc/rpc.h>
548360efbdSAlfred Perlstein #include <string.h>
558360efbdSAlfred Perlstein #include <stdlib.h>
568360efbdSAlfred Perlstein #include <fcntl.h>
578360efbdSAlfred Perlstein #include <unistd.h>
58d201fe46SDaniel Eischen #include "un-namespace.h"
59235baf26SDaniel Eischen #include "mt_misc.h"
6099064799SGarrett Wollman 
618360efbdSAlfred Perlstein #ifndef MAXHOSTNAMELEN
628360efbdSAlfred Perlstein #define	MAXHOSTNAMELEN 64
638360efbdSAlfred Perlstein #endif
6499064799SGarrett Wollman 
658360efbdSAlfred Perlstein #ifndef NETIDLEN
668360efbdSAlfred Perlstein #define	NETIDLEN 32
678360efbdSAlfred Perlstein #endif
688360efbdSAlfred Perlstein 
698360efbdSAlfred Perlstein struct rpc_call_private {
708360efbdSAlfred Perlstein 	int	valid;			/* Is this entry valid ? */
718360efbdSAlfred Perlstein 	CLIENT	*client;		/* Client handle */
728360efbdSAlfred Perlstein 	pid_t	pid;			/* process-id at moment of creation */
738360efbdSAlfred Perlstein 	rpcprog_t prognum;		/* Program */
748360efbdSAlfred Perlstein 	rpcvers_t versnum;		/* Version */
758360efbdSAlfred Perlstein 	char	host[MAXHOSTNAMELEN];	/* Servers host */
768360efbdSAlfred Perlstein 	char	nettype[NETIDLEN];	/* Network type */
778360efbdSAlfred Perlstein };
788360efbdSAlfred Perlstein static struct rpc_call_private *rpc_call_private_main;
796f88d2a8SJohn Baldwin static thread_key_t rpc_call_key;
806f88d2a8SJohn Baldwin static once_t rpc_call_once = ONCE_INITIALIZER;
816f88d2a8SJohn Baldwin static int rpc_call_key_error;
828360efbdSAlfred Perlstein 
836f88d2a8SJohn Baldwin static void rpc_call_key_init(void);
84c05ac53bSDavid E. O'Brien static void rpc_call_destroy(void *);
858360efbdSAlfred Perlstein 
868360efbdSAlfred Perlstein static void
878360efbdSAlfred Perlstein rpc_call_destroy(void *vp)
8899064799SGarrett Wollman {
898360efbdSAlfred Perlstein 	struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
9099064799SGarrett Wollman 
918360efbdSAlfred Perlstein 	if (rcp) {
928360efbdSAlfred Perlstein 		if (rcp->client)
938360efbdSAlfred Perlstein 			CLNT_DESTROY(rcp->client);
948360efbdSAlfred Perlstein 		free(rcp);
9599064799SGarrett Wollman 	}
9699064799SGarrett Wollman }
978360efbdSAlfred Perlstein 
986f88d2a8SJohn Baldwin static void
996f88d2a8SJohn Baldwin rpc_call_key_init(void)
1006f88d2a8SJohn Baldwin {
1016f88d2a8SJohn Baldwin 
1026f88d2a8SJohn Baldwin 	rpc_call_key_error = thr_keycreate(&rpc_call_key, rpc_call_destroy);
1036f88d2a8SJohn Baldwin }
1046f88d2a8SJohn Baldwin 
1058360efbdSAlfred Perlstein /*
1068360efbdSAlfred Perlstein  * This is the simplified interface to the client rpc layer.
1078360efbdSAlfred Perlstein  * The client handle is not destroyed here and is reused for
1088360efbdSAlfred Perlstein  * the future calls to same prog, vers, host and nettype combination.
1098360efbdSAlfred Perlstein  *
1108360efbdSAlfred Perlstein  * The total time available is 25 seconds.
1118360efbdSAlfred Perlstein  */
1128360efbdSAlfred Perlstein enum clnt_stat
1138360efbdSAlfred Perlstein rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
1148360efbdSAlfred Perlstein 	const char *host;			/* host name */
1158360efbdSAlfred Perlstein 	rpcprog_t prognum;			/* program number */
1168360efbdSAlfred Perlstein 	rpcvers_t versnum;			/* version number */
1178360efbdSAlfred Perlstein 	rpcproc_t procnum;			/* procedure number */
1188360efbdSAlfred Perlstein 	xdrproc_t inproc, outproc;	/* in/out XDR procedures */
1198360efbdSAlfred Perlstein 	const char *in;
1208360efbdSAlfred Perlstein 	char  *out;			/* recv/send data */
1218360efbdSAlfred Perlstein 	const char *nettype;			/* nettype */
1228360efbdSAlfred Perlstein {
1238360efbdSAlfred Perlstein 	struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
1248360efbdSAlfred Perlstein 	enum clnt_stat clnt_stat;
1258360efbdSAlfred Perlstein 	struct timeval timeout, tottimeout;
1268360efbdSAlfred Perlstein 	int main_thread = 1;
1278360efbdSAlfred Perlstein 
1288360efbdSAlfred Perlstein 	if ((main_thread = thr_main())) {
1298360efbdSAlfred Perlstein 		rcp = rpc_call_private_main;
13099064799SGarrett Wollman 	} else {
1316f88d2a8SJohn Baldwin 		if (thr_once(&rpc_call_once, rpc_call_key_init) != 0 ||
1326f88d2a8SJohn Baldwin 		    rpc_call_key_error != 0) {
1336f88d2a8SJohn Baldwin 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1346f88d2a8SJohn Baldwin 			rpc_createerr.cf_error.re_errno = rpc_call_key_error;
1356f88d2a8SJohn Baldwin 			return (rpc_createerr.cf_stat);
13699064799SGarrett Wollman 		}
1378360efbdSAlfred Perlstein 		rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
1388360efbdSAlfred Perlstein 	}
1398360efbdSAlfred Perlstein 	if (rcp == NULL) {
1408360efbdSAlfred Perlstein 		rcp = malloc(sizeof (*rcp));
1418360efbdSAlfred Perlstein 		if (rcp == NULL) {
1428360efbdSAlfred Perlstein 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1438360efbdSAlfred Perlstein 			rpc_createerr.cf_error.re_errno = errno;
1448360efbdSAlfred Perlstein 			return (rpc_createerr.cf_stat);
1458360efbdSAlfred Perlstein 		}
1468360efbdSAlfred Perlstein 		if (main_thread)
1478360efbdSAlfred Perlstein 			rpc_call_private_main = rcp;
1488360efbdSAlfred Perlstein 		else
1498360efbdSAlfred Perlstein 			thr_setspecific(rpc_call_key, (void *) rcp);
1508360efbdSAlfred Perlstein 		rcp->valid = 0;
1518360efbdSAlfred Perlstein 		rcp->client = NULL;
1528360efbdSAlfred Perlstein 	}
1532ad99b72SMartin Blapp 	if ((nettype == NULL) || (nettype[0] == 0))
1548360efbdSAlfred Perlstein 		nettype = "netpath";
1558360efbdSAlfred Perlstein 	if (!(rcp->valid && rcp->pid == getpid() &&
1568360efbdSAlfred Perlstein 		(rcp->prognum == prognum) &&
1578360efbdSAlfred Perlstein 		(rcp->versnum == versnum) &&
1588360efbdSAlfred Perlstein 		(!strcmp(rcp->host, host)) &&
1598360efbdSAlfred Perlstein 		(!strcmp(rcp->nettype, nettype)))) {
1608360efbdSAlfred Perlstein 		int fd;
1618360efbdSAlfred Perlstein 
1628360efbdSAlfred Perlstein 		rcp->valid = 0;
1638360efbdSAlfred Perlstein 		if (rcp->client)
1648360efbdSAlfred Perlstein 			CLNT_DESTROY(rcp->client);
1658360efbdSAlfred Perlstein 		/*
1668360efbdSAlfred Perlstein 		 * Using the first successful transport for that type
1678360efbdSAlfred Perlstein 		 */
1688360efbdSAlfred Perlstein 		rcp->client = clnt_create(host, prognum, versnum, nettype);
1698360efbdSAlfred Perlstein 		rcp->pid = getpid();
1708360efbdSAlfred Perlstein 		if (rcp->client == NULL) {
1718360efbdSAlfred Perlstein 			return (rpc_createerr.cf_stat);
1728360efbdSAlfred Perlstein 		}
1738360efbdSAlfred Perlstein 		/*
1748360efbdSAlfred Perlstein 		 * Set time outs for connectionless case.  Do it
1758360efbdSAlfred Perlstein 		 * unconditionally.  Faster than doing a t_getinfo()
1768360efbdSAlfred Perlstein 		 * and then doing the right thing.
1778360efbdSAlfred Perlstein 		 */
17899064799SGarrett Wollman 		timeout.tv_usec = 0;
17999064799SGarrett Wollman 		timeout.tv_sec = 5;
1808360efbdSAlfred Perlstein 		(void) CLNT_CONTROL(rcp->client,
1818360efbdSAlfred Perlstein 				CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
1828360efbdSAlfred Perlstein 		if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
1838360efbdSAlfred Perlstein 			_fcntl(fd, F_SETFD, 1);	/* make it "close on exec" */
1848360efbdSAlfred Perlstein 		rcp->prognum = prognum;
1858360efbdSAlfred Perlstein 		rcp->versnum = versnum;
1868360efbdSAlfred Perlstein 		if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
1878360efbdSAlfred Perlstein 		    (strlen(nettype) < (size_t)NETIDLEN)) {
1888360efbdSAlfred Perlstein 			(void) strcpy(rcp->host, host);
1898360efbdSAlfred Perlstein 			(void) strcpy(rcp->nettype, nettype);
1908360efbdSAlfred Perlstein 			rcp->valid = 1;
1918360efbdSAlfred Perlstein 		} else {
1928360efbdSAlfred Perlstein 			rcp->valid = 0;
19399064799SGarrett Wollman 		}
1948360efbdSAlfred Perlstein 	} /* else reuse old client */
19599064799SGarrett Wollman 	tottimeout.tv_sec = 25;
19699064799SGarrett Wollman 	tottimeout.tv_usec = 0;
1978360efbdSAlfred Perlstein 	/*LINTED const castaway*/
1988360efbdSAlfred Perlstein 	clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
19999064799SGarrett Wollman 	    outproc, out, tottimeout);
20099064799SGarrett Wollman 	/*
20199064799SGarrett Wollman 	 * if call failed, empty cache
20299064799SGarrett Wollman 	 */
20399064799SGarrett Wollman 	if (clnt_stat != RPC_SUCCESS)
2048360efbdSAlfred Perlstein 		rcp->valid = 0;
2058360efbdSAlfred Perlstein 	return (clnt_stat);
20699064799SGarrett Wollman }
207