xref: /onnv-gate/usr/src/lib/libnsl/rpc/clnt_simple.c (revision 3864:2ae506652d11)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3864Sraf  * Common Development and Distribution License (the "License").
6*3864Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
20132Srobinson  */
21132Srobinson 
22132Srobinson /*
23*3864Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
261219Sraf 
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * Simplified front end to client rpc.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "mt.h"
420Sstevel@tonic-gate #include "rpc_mt.h"
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <rpc/rpc.h>
460Sstevel@tonic-gate #include <string.h>
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate #include <stdlib.h>
490Sstevel@tonic-gate #include <unistd.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #ifndef MAXHOSTNAMELEN
520Sstevel@tonic-gate #define	MAXHOSTNAMELEN 64
530Sstevel@tonic-gate #endif
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #ifndef NETIDLEN
560Sstevel@tonic-gate #define	NETIDLEN 32
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate 
590Sstevel@tonic-gate struct rpc_call_private {
600Sstevel@tonic-gate 	int	valid;			/* Is this entry valid ? */
610Sstevel@tonic-gate 	CLIENT	*client;		/* Client handle */
620Sstevel@tonic-gate 	pid_t	pid;			/* process-id at moment of creation */
630Sstevel@tonic-gate 	rpcprog_t	prognum;	/* Program */
640Sstevel@tonic-gate 	rpcvers_t	versnum;	/* version */
650Sstevel@tonic-gate 	char	host[MAXHOSTNAMELEN];	/* Servers host */
660Sstevel@tonic-gate 	char	nettype[NETIDLEN];	/* Network type */
670Sstevel@tonic-gate };
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static void
rpc_call_destroy(void * vp)700Sstevel@tonic-gate rpc_call_destroy(void *vp)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (rcp) {
750Sstevel@tonic-gate 		if (rcp->client)
760Sstevel@tonic-gate 			CLNT_DESTROY(rcp->client);
770Sstevel@tonic-gate 		free(rcp);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * This is the simplified interface to the client rpc layer.
830Sstevel@tonic-gate  * The client handle is not destroyed here and is reused for
840Sstevel@tonic-gate  * the future calls to same prog, vers, host and nettype combination.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  * The total time available is 25 seconds.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate enum clnt_stat
rpc_call(const char * host,const rpcprog_t prognum,const rpcvers_t versnum,const rpcproc_t procnum,const xdrproc_t inproc,const char * in,const xdrproc_t outproc,char * out,const char * netclass)89132Srobinson rpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum,
90132Srobinson 	const rpcproc_t procnum, const xdrproc_t inproc, const char *in,
91132Srobinson 	const xdrproc_t outproc, char  *out, const char *netclass)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	struct rpc_call_private *rcp;
940Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
950Sstevel@tonic-gate 	struct timeval timeout, tottimeout;
96*3864Sraf 	static pthread_key_t rpc_call_key = PTHREAD_ONCE_KEY_NP;
970Sstevel@tonic-gate 	char nettype_array[NETIDLEN];
980Sstevel@tonic-gate 	char *nettype = &nettype_array[0];
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (netclass == NULL)
1010Sstevel@tonic-gate 		nettype = NULL;
1020Sstevel@tonic-gate 	else {
1030Sstevel@tonic-gate 		size_t len = strlen(netclass);
1040Sstevel@tonic-gate 		if (len >= sizeof (nettype_array)) {
1050Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1060Sstevel@tonic-gate 			return (rpc_createerr.cf_stat);
1070Sstevel@tonic-gate 		}
108132Srobinson 		(void) strcpy(nettype, netclass);
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	rcp = thr_get_storage(&rpc_call_key, sizeof (*rcp), rpc_call_destroy);
1120Sstevel@tonic-gate 	if (rcp == NULL) {
1130Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1140Sstevel@tonic-gate 		rpc_createerr.cf_error.re_errno = errno;
1150Sstevel@tonic-gate 		return (rpc_createerr.cf_stat);
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if ((nettype == NULL) || (nettype[0] == NULL))
1190Sstevel@tonic-gate 		nettype = "netpath";
1200Sstevel@tonic-gate 	if (!(rcp->valid &&
1210Sstevel@tonic-gate 	    rcp->pid == getpid() &&
1220Sstevel@tonic-gate 	    rcp->prognum == prognum &&
1230Sstevel@tonic-gate 	    rcp->versnum == versnum &&
1240Sstevel@tonic-gate 	    strcmp(rcp->host, host) == 0 &&
1250Sstevel@tonic-gate 	    strcmp(rcp->nettype, nettype) == 0)) {
1260Sstevel@tonic-gate 		int fd;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 		rcp->valid = 0;
1290Sstevel@tonic-gate 		if (rcp->client)
1300Sstevel@tonic-gate 			CLNT_DESTROY(rcp->client);
1310Sstevel@tonic-gate 		/*
1320Sstevel@tonic-gate 		 * Using the first successful transport for that type
1330Sstevel@tonic-gate 		 */
1340Sstevel@tonic-gate 		rcp->client = clnt_create(host, prognum, versnum, nettype);
1350Sstevel@tonic-gate 		rcp->pid = getpid();
136132Srobinson 		if (rcp->client == NULL)
1370Sstevel@tonic-gate 			return (rpc_createerr.cf_stat);
1380Sstevel@tonic-gate 		/*
1390Sstevel@tonic-gate 		 * Set time outs for connectionless case.  Do it
1400Sstevel@tonic-gate 		 * unconditionally.  Faster than doing a t_getinfo()
1410Sstevel@tonic-gate 		 * and then doing the right thing.
1420Sstevel@tonic-gate 		 */
1430Sstevel@tonic-gate 		timeout.tv_usec = 0;
1440Sstevel@tonic-gate 		timeout.tv_sec = 5;
1450Sstevel@tonic-gate 		(void) CLNT_CONTROL(rcp->client,
1460Sstevel@tonic-gate 				CLSET_RETRY_TIMEOUT, (char *)&timeout);
1470Sstevel@tonic-gate 		if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)&fd))
1481219Sraf 			(void) fcntl(fd, F_SETFD, 1);	/* close on exec */
1490Sstevel@tonic-gate 		rcp->prognum = prognum;
1500Sstevel@tonic-gate 		rcp->versnum = versnum;
1510Sstevel@tonic-gate 		if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
1520Sstevel@tonic-gate 		    (strlen(nettype) < (size_t)NETIDLEN)) {
1530Sstevel@tonic-gate 			(void) strcpy(rcp->host, host);
1540Sstevel@tonic-gate 			(void) strcpy(rcp->nettype, nettype);
1550Sstevel@tonic-gate 			rcp->valid = 1;
1560Sstevel@tonic-gate 		} else {
1570Sstevel@tonic-gate 			rcp->valid = 0;
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 	} /* else reuse old client */
1600Sstevel@tonic-gate 	tottimeout.tv_sec = 25;
1610Sstevel@tonic-gate 	tottimeout.tv_usec = 0;
1620Sstevel@tonic-gate 	clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *)in,
1630Sstevel@tonic-gate 				outproc, out, tottimeout);
1640Sstevel@tonic-gate 	/*
1650Sstevel@tonic-gate 	 * if call failed, empty cache
1660Sstevel@tonic-gate 	 */
1670Sstevel@tonic-gate 	if (clnt_stat != RPC_SUCCESS)
1680Sstevel@tonic-gate 		rcp->valid = 0;
1690Sstevel@tonic-gate 	return (clnt_stat);
1700Sstevel@tonic-gate }
171