tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / rpc / clnt_simple.c
blobcd3bebf8735588fe91c2c0c0a98c29c451516540
1 /* $NetBSD: clnt_simple.c,v 1.32 2013/03/11 20:19:29 tron Exp $ */
3 /*
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
8 * met:
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)
41 #if 0
42 static char sccsid[] = "@(#)clnt_simple.c 1.49 89/01/31 Copyr 1984 Sun Micro";
43 #else
44 __RCSID("$NetBSD: clnt_simple.c,v 1.32 2013/03/11 20:19:29 tron Exp $");
45 #endif
46 #endif
49 * clnt_simple.c
50 * Simplified front end to client rpc.
54 #include "namespace.h"
55 #include "reentrant.h"
56 #include <sys/param.h>
57 #include <stdio.h>
58 #include <assert.h>
59 #include <errno.h>
60 #include <rpc/rpc.h>
61 #include <string.h>
62 #include <stdlib.h>
63 #include <fcntl.h>
64 #include <unistd.h>
66 #ifdef __weak_alias
67 __weak_alias(rpc_call,_rpc_call)
68 #endif
70 #ifndef MAXHOSTNAMELEN
71 #define MAXHOSTNAMELEN 64
72 #endif
74 #ifndef NETIDLEN
75 #define NETIDLEN 32
76 #endif
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;
89 #ifdef _REENTRANT
90 static void rpc_call_destroy(void *);
92 static void
93 rpc_call_destroy(void *vp)
95 struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
97 if (rcp) {
98 if (rcp->client)
99 CLNT_DESTROY(rcp->client);
100 free(rcp);
103 static thread_key_t rpc_call_key;
104 static once_t rpc_call_once = ONCE_INITIALIZER;
106 static void
107 rpc_call_setup(void)
110 thr_keycreate(&rpc_call_key, rpc_call_destroy);
112 #endif
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.
122 enum clnt_stat
123 rpc_call(
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 ??? */
143 #ifdef _REENTRANT
144 if (__isthreaded == 0) {
145 rcp = rpc_call_private_main;
146 } else {
147 thr_once(&rpc_call_once, rpc_call_setup);
148 rcp = thr_getspecific(rpc_call_key);
150 #else
151 rcp = rpc_call_private_main;
152 #endif
153 if (rcp == NULL) {
154 rcp = malloc(sizeof (*rcp));
155 if (rcp == NULL) {
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;
162 else
163 thr_setspecific(rpc_call_key, (void *) rcp);
164 rcp->valid = 0;
165 rcp->client = NULL;
167 if ((nettype == NULL) || (nettype[0] == 0))
168 nettype = "netpath";
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)))) {
174 int fd;
176 rcp->valid = 0;
177 if (rcp->client)
178 CLNT_DESTROY(rcp->client);
180 * Using the first successful transport for that type
182 rcp->client = clnt_create(host, prognum, versnum, nettype);
183 rcp->pid = getpid();
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.
192 timeout.tv_usec = 0;
193 timeout.tv_sec = 5;
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);
204 rcp->valid = 1;
205 } else {
206 rcp->valid = 0;
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)
217 rcp->valid = 0;
218 return (clnt_stat);