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