1 /* $NetBSD: clnt_simple.c,v 1.32 2013/03/11 20:19:29 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, 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
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
37 /* #ident "@(#)clnt_simple.c 1.17 94/04/24 SMI" */
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid
[] = "@(#)clnt_simple.c 1.49 89/01/31 Copyr 1984 Sun Micro";
44 __RCSID("$NetBSD: clnt_simple.c,v 1.32 2013/03/11 20:19:29 tron Exp $");
50 * Simplified front end to client rpc.
54 #include "namespace.h"
55 #include "reentrant.h"
56 #include <sys/param.h>
67 __weak_alias(rpc_call
,_rpc_call
)
70 #ifndef MAXHOSTNAMELEN
71 #define MAXHOSTNAMELEN 64
78 struct rpc_call_private
{
79 int valid
; /* Is this entry valid ? */
80 CLIENT
*client
; /* Client handle */
81 pid_t pid
; /* process-id at moment of creation */
82 rpcprog_t prognum
; /* Program */
83 rpcvers_t versnum
; /* Version */
84 char host
[MAXHOSTNAMELEN
]; /* Servers host */
85 char nettype
[NETIDLEN
]; /* Network type */
87 static struct rpc_call_private
*rpc_call_private_main
;
90 static void rpc_call_destroy(void *);
93 rpc_call_destroy(void *vp
)
95 struct rpc_call_private
*rcp
= (struct rpc_call_private
*)vp
;
99 CLNT_DESTROY(rcp
->client
);
103 static thread_key_t rpc_call_key
;
104 static once_t rpc_call_once
= ONCE_INITIALIZER
;
110 thr_keycreate(&rpc_call_key
, rpc_call_destroy
);
116 * This is the simplified interface to the client rpc layer.
117 * The client handle is not destroyed here and is reused for
118 * the future calls to same prog, vers, host and nettype combination.
120 * The total time available is 25 seconds.
124 const char * host
, /* host name */
125 rpcprog_t prognum
, /* program number */
126 rpcvers_t versnum
, /* version number */
127 rpcproc_t procnum
, /* procedure number */
128 xdrproc_t inproc
, /* in XDR procedures */
129 const char * in
, /* recv data */
130 xdrproc_t outproc
, /* out XDR procedures */
131 char * out
, /* send data */
132 const char * nettype
) /* nettype */
134 struct rpc_call_private
*rcp
= (struct rpc_call_private
*) 0;
135 enum clnt_stat clnt_stat
;
136 struct timeval timeout
, tottimeout
;
138 _DIAGASSERT(host
!= NULL
);
139 /* XXX: in may be NULL ??? */
140 /* XXX: out may be NULL ??? */
141 /* XXX: nettype may be NULL ??? */
144 if (__isthreaded
== 0) {
145 rcp
= rpc_call_private_main
;
147 thr_once(&rpc_call_once
, rpc_call_setup
);
148 rcp
= thr_getspecific(rpc_call_key
);
151 rcp
= rpc_call_private_main
;
154 rcp
= malloc(sizeof (*rcp
));
156 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
157 rpc_createerr
.cf_error
.re_errno
= errno
;
158 return (rpc_createerr
.cf_stat
);
160 if (__isthreaded
== 0)
161 rpc_call_private_main
= rcp
;
163 thr_setspecific(rpc_call_key
, (void *) rcp
);
167 if ((nettype
== NULL
) || (nettype
[0] == 0))
169 if (!(rcp
->valid
&& rcp
->pid
== getpid() &&
170 (rcp
->prognum
== prognum
) &&
171 (rcp
->versnum
== versnum
) &&
172 (!strcmp(rcp
->host
, host
)) &&
173 (!strcmp(rcp
->nettype
, nettype
)))) {
178 CLNT_DESTROY(rcp
->client
);
180 * Using the first successful transport for that type
182 rcp
->client
= clnt_create(host
, prognum
, versnum
, nettype
);
184 if (rcp
->client
== NULL
) {
185 return (rpc_createerr
.cf_stat
);
188 * Set time outs for connectionless case. Do it
189 * unconditionally. Faster than doing a t_getinfo()
190 * and then doing the right thing.
194 (void) CLNT_CONTROL(rcp
->client
,
195 CLSET_RETRY_TIMEOUT
, (char *)(void *)&timeout
);
196 if (CLNT_CONTROL(rcp
->client
, CLGET_FD
, (char *)(void *)&fd
))
197 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
198 rcp
->prognum
= prognum
;
199 rcp
->versnum
= versnum
;
200 if ((strlen(host
) < (size_t)MAXHOSTNAMELEN
) &&
201 (strlen(nettype
) < (size_t)NETIDLEN
)) {
202 (void) strcpy(rcp
->host
, host
);
203 (void) strcpy(rcp
->nettype
, nettype
);
208 } /* else reuse old client */
209 tottimeout
.tv_sec
= 25;
210 tottimeout
.tv_usec
= 0;
211 clnt_stat
= CLNT_CALL(rcp
->client
, procnum
, inproc
, in
,
212 outproc
, out
, tottimeout
);
214 * if call failed, empty cache
216 if (clnt_stat
!= RPC_SUCCESS
)