2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro
29 * @(#)clnt_simple.c 2.2 88/08/01 4.0 RPCSRC
30 * $NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $
31 * $FreeBSD: src/lib/libc/rpc/clnt_simple.c,v 1.20 2006/02/27 22:10:59 deischen Exp $
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
39 * Simplified front end to client rpc.
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/param.h>
53 #include "un-namespace.h"
56 #ifndef MAXHOSTNAMELEN
57 #define MAXHOSTNAMELEN 64
64 struct rpc_call_private {
65 int valid; /* Is this entry valid ? */
66 CLIENT *client; /* Client handle */
67 pid_t pid; /* process-id at moment of creation */
68 rpcprog_t prognum; /* Program */
69 rpcvers_t versnum; /* Version */
70 char host[MAXHOSTNAMELEN]; /* Servers host */
71 char nettype[NETIDLEN]; /* Network type */
73 static struct rpc_call_private *rpc_call_private_main;
75 static void rpc_call_destroy(void *);
78 rpc_call_destroy(void *vp)
80 struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
84 CLNT_DESTROY(rcp->client);
90 * This is the simplified interface to the client rpc layer.
91 * The client handle is not destroyed here and is reused for
92 * the future calls to same prog, vers, host and nettype combination.
94 * The total time available is 25 seconds.
97 * prognum - program number
98 * versnum - version number
99 * procnum - procedure number
100 * inproc, outproc - in/out XDR procedures
101 * in, out - recv/send data
105 rpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum,
106 const rpcproc_t procnum, const xdrproc_t inproc, const char *in,
107 const xdrproc_t outproc, char *out, const char *nettype)
109 struct rpc_call_private *rcp = NULL;
110 enum clnt_stat clnt_stat;
111 struct timeval timeout, tottimeout;
112 static thread_key_t rpc_call_key;
115 if ((main_thread = thr_main())) {
116 rcp = rpc_call_private_main;
118 if (rpc_call_key == 0) {
119 mutex_lock(&tsd_lock);
120 if (rpc_call_key == 0)
121 thr_keycreate(&rpc_call_key, rpc_call_destroy);
122 mutex_unlock(&tsd_lock);
124 rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
127 rcp = malloc(sizeof (*rcp));
129 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
130 rpc_createerr.cf_error.re_errno = errno;
131 return (rpc_createerr.cf_stat);
134 rpc_call_private_main = rcp;
136 thr_setspecific(rpc_call_key, (void *) rcp);
140 if ((nettype == NULL) || (nettype[0] == 0))
142 if (!(rcp->valid && rcp->pid == getpid() &&
143 (rcp->prognum == prognum) &&
144 (rcp->versnum == versnum) &&
145 (!strcmp(rcp->host, host)) &&
146 (!strcmp(rcp->nettype, nettype)))) {
151 CLNT_DESTROY(rcp->client);
153 * Using the first successful transport for that type
155 rcp->client = clnt_create(host, prognum, versnum, nettype);
157 if (rcp->client == NULL) {
158 return (rpc_createerr.cf_stat);
161 * Set time outs for connectionless case. Do it
162 * unconditionally. Faster than doing a t_getinfo()
163 * and then doing the right thing.
167 CLNT_CONTROL(rcp->client,
168 CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
169 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
170 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
171 rcp->prognum = prognum;
172 rcp->versnum = versnum;
173 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
174 (strlen(nettype) < (size_t)NETIDLEN)) {
175 strcpy(rcp->host, host);
176 strcpy(rcp->nettype, nettype);
181 } /* else reuse old client */
182 tottimeout.tv_sec = 25;
183 tottimeout.tv_usec = 0;
184 /*LINTED const castaway*/
185 clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
186 outproc, out, tottimeout);
188 * if call failed, empty cache
190 if (clnt_stat != RPC_SUCCESS)