3 * Simplified front end to rpc.
5 * Copyright (c) 2010, Oracle America, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 * * Neither the name of the "Oracle America, Inc." nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/socket.h>
43 #include <shlib-compat.h>
45 struct callrpc_private_s
49 u_long oldprognum
, oldversnum
, valid
;
52 #define callrpc_private RPC_THREAD_VARIABLE(callrpc_private_s)
55 callrpc (const char *host
, u_long prognum
, u_long versnum
, u_long procnum
,
56 xdrproc_t inproc
, const char *in
, xdrproc_t outproc
, char *out
)
58 struct callrpc_private_s
*crp
= callrpc_private
;
59 struct sockaddr_in server_addr
;
60 enum clnt_stat clnt_stat
;
61 struct timeval timeout
, tottimeout
;
65 crp
= (struct callrpc_private_s
*) calloc (1, sizeof (*crp
));
68 callrpc_private
= crp
;
70 if (crp
->oldhost
== NULL
)
72 crp
->oldhost
= malloc (256);
74 crp
->socket
= RPC_ANYSOCK
;
76 if (crp
->valid
&& crp
->oldprognum
== prognum
&& crp
->oldversnum
== versnum
77 && strcmp (crp
->oldhost
, host
) == 0)
79 /* reuse old client */
84 if (crp
->socket
!= RPC_ANYSOCK
)
86 (void) __close (crp
->socket
);
87 crp
->socket
= RPC_ANYSOCK
;
91 clnt_destroy (crp
->client
);
95 if (__libc_rpc_gethostbyname (host
, &server_addr
) != 0)
96 return (int) get_rpc_createerr().cf_stat
;
100 if ((crp
->client
= clntudp_create (&server_addr
, (u_long
) prognum
,
101 (u_long
) versnum
, timeout
, &crp
->socket
)) == NULL
)
102 return (int) get_rpc_createerr().cf_stat
;
104 crp
->oldprognum
= prognum
;
105 crp
->oldversnum
= versnum
;
106 (void) strncpy (crp
->oldhost
, host
, 255);
107 crp
->oldhost
[255] = '\0';
109 tottimeout
.tv_sec
= 25;
110 tottimeout
.tv_usec
= 0;
111 clnt_stat
= clnt_call (crp
->client
, procnum
, inproc
, (char *) in
,
112 outproc
, out
, tottimeout
);
114 * if call failed, empty cache
116 if (clnt_stat
!= RPC_SUCCESS
)
118 return (int) clnt_stat
;
120 libc_hidden_nolink_sunrpc (callrpc
, GLIBC_2_0
)
123 __rpc_thread_clnt_cleanup (void)
125 struct callrpc_private_s
*rcp
= RPC_THREAD_VARIABLE(callrpc_private_s
);
129 CLNT_DESTROY (rcp
->client
);