xref: /netbsd-src/lib/libc/rpc/clnt_simple.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: clnt_simple.c,v 1.23 2003/01/18 11:29:04 thorpej Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 /*
32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33  */
34 
35 /* #ident	"@(#)clnt_simple.c	1.17	94/04/24 SMI" */
36 
37 #if 0
38 #if !defined(lint) && defined(SCCSIDS)
39 static char sccsid[] = "@(#)clnt_simple.c 1.49 89/01/31 Copyr 1984 Sun Micro";
40 #endif
41 #endif
42 
43 /*
44  * clnt_simple.c
45  * Simplified front end to client rpc.
46  *
47  */
48 
49 #include "namespace.h"
50 #include "reentrant.h"
51 #include <sys/param.h>
52 #include <stdio.h>
53 #include <assert.h>
54 #include <errno.h>
55 #include <rpc/rpc.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <fcntl.h>
59 #include <unistd.h>
60 
61 #ifdef __weak_alias
62 __weak_alias(rpc_call,_rpc_call)
63 #endif
64 
65 #ifndef MAXHOSTNAMELEN
66 #define	MAXHOSTNAMELEN 64
67 #endif
68 
69 #ifndef NETIDLEN
70 #define	NETIDLEN 32
71 #endif
72 
73 struct rpc_call_private {
74 	int	valid;			/* Is this entry valid ? */
75 	CLIENT	*client;		/* Client handle */
76 	pid_t	pid;			/* process-id at moment of creation */
77 	rpcprog_t prognum;		/* Program */
78 	rpcvers_t versnum;		/* Version */
79 	char	host[MAXHOSTNAMELEN];	/* Servers host */
80 	char	nettype[NETIDLEN];	/* Network type */
81 };
82 static struct rpc_call_private *rpc_call_private_main;
83 
84 #ifdef _REENTRANT
85 static void rpc_call_destroy __P((void *));
86 
87 static void
88 rpc_call_destroy(void *vp)
89 {
90 	struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
91 
92 	if (rcp) {
93 		if (rcp->client)
94 			CLNT_DESTROY(rcp->client);
95 		free(rcp);
96 	}
97 }
98 static thread_key_t rpc_call_key;
99 static once_t rpc_call_once = ONCE_INITIALIZER;
100 
101 static void
102 rpc_call_setup(void)
103 {
104 
105 	thr_keycreate(&rpc_call_key, rpc_call_destroy);
106 }
107 #endif
108 
109 
110 /*
111  * This is the simplified interface to the client rpc layer.
112  * The client handle is not destroyed here and is reused for
113  * the future calls to same prog, vers, host and nettype combination.
114  *
115  * The total time available is 25 seconds.
116  */
117 enum clnt_stat
118 rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
119 	const char *host;			/* host name */
120 	rpcprog_t prognum;			/* program number */
121 	rpcvers_t versnum;			/* version number */
122 	rpcproc_t procnum;			/* procedure number */
123 	xdrproc_t inproc, outproc;	/* in/out XDR procedures */
124 	const char *in;
125 	char  *out;			/* recv/send data */
126 	const char *nettype;			/* nettype */
127 {
128 	struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
129 	enum clnt_stat clnt_stat;
130 	struct timeval timeout, tottimeout;
131 	extern int __isthreaded;
132 
133 	_DIAGASSERT(host != NULL);
134 	/* XXX: in may be NULL ??? */
135 	/* XXX: out may be NULL ??? */
136 	/* XXX: nettype may be NULL ??? */
137 
138 #ifdef _REENTRANT
139 	if (__isthreaded == 0) {
140 		rcp = rpc_call_private_main;
141 	} else {
142 		thr_once(&rpc_call_once, rpc_call_setup);
143 		rcp = thr_getspecific(rpc_call_key);
144 	}
145 #else
146 	rcp = rpc_call_private_main;
147 #endif
148 	if (rcp == NULL) {
149 		rcp = malloc(sizeof (*rcp));
150 		if (rcp == NULL) {
151 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
152 			rpc_createerr.cf_error.re_errno = errno;
153 			return (rpc_createerr.cf_stat);
154 		}
155 		if (__isthreaded == 0)
156 			rpc_call_private_main = rcp;
157 		else
158 			thr_setspecific(rpc_call_key, (void *) rcp);
159 		rcp->valid = 0;
160 		rcp->client = NULL;
161 	}
162 	if ((nettype == NULL) || (nettype[0] == NULL))
163 		nettype = "netpath";
164 	if (!(rcp->valid && rcp->pid == getpid() &&
165 		(rcp->prognum == prognum) &&
166 		(rcp->versnum == versnum) &&
167 		(!strcmp(rcp->host, host)) &&
168 		(!strcmp(rcp->nettype, nettype)))) {
169 		int fd;
170 
171 		rcp->valid = 0;
172 		if (rcp->client)
173 			CLNT_DESTROY(rcp->client);
174 		/*
175 		 * Using the first successful transport for that type
176 		 */
177 		rcp->client = clnt_create(host, prognum, versnum, nettype);
178 		rcp->pid = getpid();
179 		if (rcp->client == NULL) {
180 			return (rpc_createerr.cf_stat);
181 		}
182 		/*
183 		 * Set time outs for connectionless case.  Do it
184 		 * unconditionally.  Faster than doing a t_getinfo()
185 		 * and then doing the right thing.
186 		 */
187 		timeout.tv_usec = 0;
188 		timeout.tv_sec = 5;
189 		(void) CLNT_CONTROL(rcp->client,
190 				CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
191 		if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
192 			fcntl(fd, F_SETFD, 1);	/* make it "close on exec" */
193 		rcp->prognum = prognum;
194 		rcp->versnum = versnum;
195 		if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
196 		    (strlen(nettype) < (size_t)NETIDLEN)) {
197 			(void) strcpy(rcp->host, host);
198 			(void) strcpy(rcp->nettype, nettype);
199 			rcp->valid = 1;
200 		} else {
201 			rcp->valid = 0;
202 		}
203 	} /* else reuse old client */
204 	tottimeout.tv_sec = 25;
205 	tottimeout.tv_usec = 0;
206 	/*LINTED const castaway*/
207 	clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
208 	    outproc, out, tottimeout);
209 	/*
210 	 * if call failed, empty cache
211 	 */
212 	if (clnt_stat != RPC_SUCCESS)
213 		rcp->valid = 0;
214 	return (clnt_stat);
215 }
216