xref: /titanic_44/usr/src/lib/libnsl/rpc/clnt_simple.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  *
22  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
26 /* All Rights Reserved */
27 /*
28  * Portions of this source code were derived from Berkeley
29  * 4.3 BSD under license from the Regents of the University of
30  * California.
31  */
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 /*
36  * clnt_simple.c
37  * Simplified front end to client rpc.
38  *
39  */
40 
41 #include "mt.h"
42 #include "rpc_mt.h"
43 #include <stdio.h>
44 #include <errno.h>
45 #include <rpc/rpc.h>
46 #include <rpc/trace.h>
47 #include <string.h>
48 #include <sys/param.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 
52 #ifndef MAXHOSTNAMELEN
53 #define	MAXHOSTNAMELEN 64
54 #endif
55 
56 #ifndef NETIDLEN
57 #define	NETIDLEN 32
58 #endif
59 
60 struct rpc_call_private {
61 	int	valid;			/* Is this entry valid ? */
62 	CLIENT	*client;		/* Client handle */
63 	pid_t	pid;			/* process-id at moment of creation */
64 	rpcprog_t	prognum;	/* Program */
65 	rpcvers_t	versnum;	/* version */
66 	char	host[MAXHOSTNAMELEN];	/* Servers host */
67 	char	nettype[NETIDLEN];	/* Network type */
68 };
69 
70 static void
71 rpc_call_destroy(void *vp)
72 {
73 	struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
74 
75 	if (rcp) {
76 		if (rcp->client)
77 			CLNT_DESTROY(rcp->client);
78 		free(rcp);
79 	}
80 }
81 
82 /*
83  * This is the simplified interface to the client rpc layer.
84  * The client handle is not destroyed here and is reused for
85  * the future calls to same prog, vers, host and nettype combination.
86  *
87  * The total time available is 25 seconds.
88  */
89 enum clnt_stat
90 rpc_call(const char *host, rpcprog_t prognum, rpcvers_t versnum,
91 	rpcproc_t procnum, xdrproc_t inproc, const char *in,
92 	xdrproc_t outproc, char  *out, const char *netclass)
93 {
94 	struct rpc_call_private *rcp;
95 	enum clnt_stat clnt_stat;
96 	struct timeval timeout, tottimeout;
97 	static pthread_key_t rpc_call_key;
98 	int main_thread;
99 	char nettype_array[NETIDLEN];
100 	char *nettype = &nettype_array[0];
101 
102 	trace4(TR_rpc_call, 0, prognum, versnum, procnum);
103 
104 	if (netclass == NULL)
105 		nettype = NULL;
106 	else {
107 		size_t len = strlen(netclass);
108 		if (len >= sizeof (nettype_array)) {
109 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
110 			trace4(TR_rpc_call, 1, prognum, versnum, procnum);
111 			return (rpc_createerr.cf_stat);
112 		}
113 		strcpy(nettype, netclass);
114 	}
115 
116 	rcp = thr_get_storage(&rpc_call_key, sizeof (*rcp), rpc_call_destroy);
117 	if (rcp == NULL) {
118 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
119 		rpc_createerr.cf_error.re_errno = errno;
120 		trace4(TR_rpc_call, 1, prognum, versnum, procnum);
121 		return (rpc_createerr.cf_stat);
122 	}
123 
124 	if ((nettype == NULL) || (nettype[0] == NULL))
125 		nettype = "netpath";
126 	if (!(rcp->valid &&
127 	    rcp->pid == getpid() &&
128 	    rcp->prognum == prognum &&
129 	    rcp->versnum == versnum &&
130 	    strcmp(rcp->host, host) == 0 &&
131 	    strcmp(rcp->nettype, nettype) == 0)) {
132 		int fd;
133 
134 		rcp->valid = 0;
135 		if (rcp->client)
136 			CLNT_DESTROY(rcp->client);
137 		/*
138 		 * Using the first successful transport for that type
139 		 */
140 		rcp->client = clnt_create(host, prognum, versnum, nettype);
141 		rcp->pid = getpid();
142 		if (rcp->client == (CLIENT *)NULL) {
143 			trace4(TR_rpc_call, 1, prognum, versnum, procnum);
144 			return (rpc_createerr.cf_stat);
145 		}
146 		/*
147 		 * Set time outs for connectionless case.  Do it
148 		 * unconditionally.  Faster than doing a t_getinfo()
149 		 * and then doing the right thing.
150 		 */
151 		timeout.tv_usec = 0;
152 		timeout.tv_sec = 5;
153 		(void) CLNT_CONTROL(rcp->client,
154 				CLSET_RETRY_TIMEOUT, (char *)&timeout);
155 		if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)&fd))
156 			_fcntl(fd, F_SETFD, 1);	/* make it "close on exec" */
157 		rcp->prognum = prognum;
158 		rcp->versnum = versnum;
159 		if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
160 		    (strlen(nettype) < (size_t)NETIDLEN)) {
161 			(void) strcpy(rcp->host, host);
162 			(void) strcpy(rcp->nettype, nettype);
163 			rcp->valid = 1;
164 		} else {
165 			rcp->valid = 0;
166 		}
167 	} /* else reuse old client */
168 	tottimeout.tv_sec = 25;
169 	tottimeout.tv_usec = 0;
170 	clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *)in,
171 				outproc, out, tottimeout);
172 	/*
173 	 * if call failed, empty cache
174 	 */
175 	if (clnt_stat != RPC_SUCCESS)
176 		rcp->valid = 0;
177 	trace4(TR_rpc_call, 1, prognum, versnum, procnum);
178 	return (clnt_stat);
179 }
180