xref: /freebsd/lib/libc/rpc/clnt_simple.c (revision b740c88bfb6453416926271c089262e7164dace3)
1 /*	$NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009, Sun Microsystems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * - Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  * - Neither the name of Sun Microsystems, Inc. nor the names of its
15  *   contributors may be used to endorse or promote products derived
16  *   from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*
31  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid2 = "from: @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "from: @(#)clnt_simple.c	2.2 88/08/01 4.0 RPCSRC";
37 #endif
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * clnt_simple.c
43  * Simplified front end to client rpc.
44  *
45  */
46 
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include <sys/param.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <rpc/rpc.h>
53 #include <string.h>
54 #include <stdlib.h>
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include "un-namespace.h"
58 #include "mt_misc.h"
59 
60 #ifndef MAXHOSTNAMELEN
61 #define	MAXHOSTNAMELEN 64
62 #endif
63 
64 #ifndef NETIDLEN
65 #define	NETIDLEN 32
66 #endif
67 
68 struct rpc_call_private {
69 	int	valid;			/* Is this entry valid ? */
70 	CLIENT	*client;		/* Client handle */
71 	pid_t	pid;			/* process-id at moment of creation */
72 	rpcprog_t prognum;		/* Program */
73 	rpcvers_t versnum;		/* Version */
74 	char	host[MAXHOSTNAMELEN];	/* Servers host */
75 	char	nettype[NETIDLEN];	/* Network type */
76 };
77 static struct rpc_call_private *rpc_call_private_main;
78 static thread_key_t rpc_call_key;
79 static once_t rpc_call_once = ONCE_INITIALIZER;
80 static int rpc_call_key_error;
81 
82 static void rpc_call_key_init(void);
83 static void rpc_call_destroy(void *);
84 
85 static void
86 rpc_call_destroy(void *vp)
87 {
88 	struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
89 
90 	if (rcp) {
91 		if (rcp->client)
92 			CLNT_DESTROY(rcp->client);
93 		free(rcp);
94 	}
95 }
96 
97 static void
98 rpc_call_key_init(void)
99 {
100 
101 	rpc_call_key_error = thr_keycreate(&rpc_call_key, rpc_call_destroy);
102 }
103 
104 /*
105  * This is the simplified interface to the client rpc layer.
106  * The client handle is not destroyed here and is reused for
107  * the future calls to same prog, vers, host and nettype combination.
108  *
109  * The total time available is 25 seconds.
110  */
111 enum clnt_stat
112 rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
113 	const char *host;			/* host name */
114 	rpcprog_t prognum;			/* program number */
115 	rpcvers_t versnum;			/* version number */
116 	rpcproc_t procnum;			/* procedure number */
117 	xdrproc_t inproc, outproc;	/* in/out XDR procedures */
118 	const char *in;
119 	char  *out;			/* recv/send data */
120 	const char *nettype;			/* nettype */
121 {
122 	struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
123 	enum clnt_stat clnt_stat;
124 	struct timeval timeout, tottimeout;
125 	int main_thread = 1;
126 
127 	if ((main_thread = thr_main())) {
128 		rcp = rpc_call_private_main;
129 	} else {
130 		if (thr_once(&rpc_call_once, rpc_call_key_init) != 0 ||
131 		    rpc_call_key_error != 0) {
132 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
133 			rpc_createerr.cf_error.re_errno = rpc_call_key_error;
134 			return (rpc_createerr.cf_stat);
135 		}
136 		rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
137 	}
138 	if (rcp == NULL) {
139 		rcp = malloc(sizeof (*rcp));
140 		if (rcp == NULL) {
141 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
142 			rpc_createerr.cf_error.re_errno = errno;
143 			return (rpc_createerr.cf_stat);
144 		}
145 		if (main_thread)
146 			rpc_call_private_main = rcp;
147 		else
148 			thr_setspecific(rpc_call_key, (void *) rcp);
149 		rcp->valid = 0;
150 		rcp->client = NULL;
151 	}
152 	if ((nettype == NULL) || (nettype[0] == 0))
153 		nettype = "netpath";
154 	if (!(rcp->valid && rcp->pid == getpid() &&
155 		(rcp->prognum == prognum) &&
156 		(rcp->versnum == versnum) &&
157 		(!strcmp(rcp->host, host)) &&
158 		(!strcmp(rcp->nettype, nettype)))) {
159 		int fd;
160 
161 		rcp->valid = 0;
162 		if (rcp->client)
163 			CLNT_DESTROY(rcp->client);
164 		/*
165 		 * Using the first successful transport for that type
166 		 */
167 		rcp->client = clnt_create(host, prognum, versnum, nettype);
168 		rcp->pid = getpid();
169 		if (rcp->client == NULL) {
170 			return (rpc_createerr.cf_stat);
171 		}
172 		/*
173 		 * Set time outs for connectionless case.  Do it
174 		 * unconditionally.  Faster than doing a t_getinfo()
175 		 * and then doing the right thing.
176 		 */
177 		timeout.tv_usec = 0;
178 		timeout.tv_sec = 5;
179 		(void) CLNT_CONTROL(rcp->client,
180 				CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
181 		if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
182 			_fcntl(fd, F_SETFD, 1);	/* make it "close on exec" */
183 		rcp->prognum = prognum;
184 		rcp->versnum = versnum;
185 		if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
186 		    (strlen(nettype) < (size_t)NETIDLEN)) {
187 			(void) strcpy(rcp->host, host);
188 			(void) strcpy(rcp->nettype, nettype);
189 			rcp->valid = 1;
190 		} else {
191 			rcp->valid = 0;
192 		}
193 	} /* else reuse old client */
194 	tottimeout.tv_sec = 25;
195 	tottimeout.tv_usec = 0;
196 	/*LINTED const castaway*/
197 	clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
198 	    outproc, out, tottimeout);
199 	/*
200 	 * if call failed, empty cache
201 	 */
202 	if (clnt_stat != RPC_SUCCESS)
203 		rcp->valid = 0;
204 	return (clnt_stat);
205 }
206