clnt_simple.c revision 1.32 1 1.32 tron /* $NetBSD: clnt_simple.c,v 1.32 2013/03/11 20:19:29 tron Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.32 tron * Copyright (c) 2010, Oracle America, Inc.
5 1.32 tron *
6 1.32 tron * Redistribution and use in source and binary forms, with or without
7 1.32 tron * modification, are permitted provided that the following conditions are
8 1.32 tron * met:
9 1.32 tron *
10 1.32 tron * * Redistributions of source code must retain the above copyright
11 1.32 tron * notice, this list of conditions and the following disclaimer.
12 1.32 tron * * Redistributions in binary form must reproduce the above
13 1.32 tron * copyright notice, this list of conditions and the following
14 1.32 tron * disclaimer in the documentation and/or other materials
15 1.32 tron * provided with the distribution.
16 1.32 tron * * Neither the name of the "Oracle America, Inc." nor the names of its
17 1.32 tron * contributors may be used to endorse or promote products derived
18 1.32 tron * from this software without specific prior written permission.
19 1.32 tron *
20 1.32 tron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.32 tron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.32 tron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.32 tron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.32 tron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 1.32 tron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.32 tron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.32 tron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.32 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.32 tron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.32 tron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.32 tron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.20 fvdl /*
34 1.20 fvdl * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 1.20 fvdl */
36 1.20 fvdl
37 1.20 fvdl /* #ident "@(#)clnt_simple.c 1.17 94/04/24 SMI" */
38 1.1 cgd
39 1.24 itojun #include <sys/cdefs.h>
40 1.24 itojun #if defined(LIBC_SCCS) && !defined(lint)
41 1.8 christos #if 0
42 1.20 fvdl static char sccsid[] = "@(#)clnt_simple.c 1.49 89/01/31 Copyr 1984 Sun Micro";
43 1.24 itojun #else
44 1.32 tron __RCSID("$NetBSD: clnt_simple.c,v 1.32 2013/03/11 20:19:29 tron Exp $");
45 1.8 christos #endif
46 1.1 cgd #endif
47 1.1 cgd
48 1.20 fvdl /*
49 1.1 cgd * clnt_simple.c
50 1.20 fvdl * Simplified front end to client rpc.
51 1.1 cgd *
52 1.1 cgd */
53 1.1 cgd
54 1.9 jtc #include "namespace.h"
55 1.20 fvdl #include "reentrant.h"
56 1.20 fvdl #include <sys/param.h>
57 1.1 cgd #include <stdio.h>
58 1.22 lukem #include <assert.h>
59 1.20 fvdl #include <errno.h>
60 1.20 fvdl #include <rpc/rpc.h>
61 1.20 fvdl #include <string.h>
62 1.1 cgd #include <stdlib.h>
63 1.20 fvdl #include <fcntl.h>
64 1.8 christos #include <unistd.h>
65 1.12 lukem
66 1.20 fvdl #ifdef __weak_alias
67 1.20 fvdl __weak_alias(rpc_call,_rpc_call)
68 1.20 fvdl #endif
69 1.20 fvdl
70 1.20 fvdl #ifndef MAXHOSTNAMELEN
71 1.20 fvdl #define MAXHOSTNAMELEN 64
72 1.20 fvdl #endif
73 1.20 fvdl
74 1.20 fvdl #ifndef NETIDLEN
75 1.20 fvdl #define NETIDLEN 32
76 1.20 fvdl #endif
77 1.20 fvdl
78 1.20 fvdl struct rpc_call_private {
79 1.20 fvdl int valid; /* Is this entry valid ? */
80 1.20 fvdl CLIENT *client; /* Client handle */
81 1.20 fvdl pid_t pid; /* process-id at moment of creation */
82 1.20 fvdl rpcprog_t prognum; /* Program */
83 1.20 fvdl rpcvers_t versnum; /* Version */
84 1.20 fvdl char host[MAXHOSTNAMELEN]; /* Servers host */
85 1.20 fvdl char nettype[NETIDLEN]; /* Network type */
86 1.20 fvdl };
87 1.20 fvdl static struct rpc_call_private *rpc_call_private_main;
88 1.20 fvdl
89 1.23 thorpej #ifdef _REENTRANT
90 1.31 matt static void rpc_call_destroy(void *);
91 1.20 fvdl
92 1.20 fvdl static void
93 1.20 fvdl rpc_call_destroy(void *vp)
94 1.20 fvdl {
95 1.21 christos struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
96 1.9 jtc
97 1.20 fvdl if (rcp) {
98 1.20 fvdl if (rcp->client)
99 1.20 fvdl CLNT_DESTROY(rcp->client);
100 1.20 fvdl free(rcp);
101 1.20 fvdl }
102 1.20 fvdl }
103 1.23 thorpej static thread_key_t rpc_call_key;
104 1.23 thorpej static once_t rpc_call_once = ONCE_INITIALIZER;
105 1.23 thorpej
106 1.23 thorpej static void
107 1.23 thorpej rpc_call_setup(void)
108 1.23 thorpej {
109 1.23 thorpej
110 1.23 thorpej thr_keycreate(&rpc_call_key, rpc_call_destroy);
111 1.23 thorpej }
112 1.9 jtc #endif
113 1.1 cgd
114 1.23 thorpej
115 1.20 fvdl /*
116 1.20 fvdl * This is the simplified interface to the client rpc layer.
117 1.20 fvdl * The client handle is not destroyed here and is reused for
118 1.20 fvdl * the future calls to same prog, vers, host and nettype combination.
119 1.20 fvdl *
120 1.20 fvdl * The total time available is 25 seconds.
121 1.20 fvdl */
122 1.20 fvdl enum clnt_stat
123 1.31 matt rpc_call(
124 1.31 matt const char * host, /* host name */
125 1.31 matt rpcprog_t prognum, /* program number */
126 1.31 matt rpcvers_t versnum, /* version number */
127 1.31 matt rpcproc_t procnum, /* procedure number */
128 1.31 matt xdrproc_t inproc, /* in XDR procedures */
129 1.31 matt const char * in, /* recv data */
130 1.31 matt xdrproc_t outproc, /* out XDR procedures */
131 1.31 matt char * out, /* send data */
132 1.31 matt const char * nettype) /* nettype */
133 1.1 cgd {
134 1.20 fvdl struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
135 1.1 cgd enum clnt_stat clnt_stat;
136 1.1 cgd struct timeval timeout, tottimeout;
137 1.22 lukem
138 1.22 lukem _DIAGASSERT(host != NULL);
139 1.22 lukem /* XXX: in may be NULL ??? */
140 1.22 lukem /* XXX: out may be NULL ??? */
141 1.22 lukem /* XXX: nettype may be NULL ??? */
142 1.17 lukem
143 1.23 thorpej #ifdef _REENTRANT
144 1.23 thorpej if (__isthreaded == 0) {
145 1.20 fvdl rcp = rpc_call_private_main;
146 1.20 fvdl } else {
147 1.23 thorpej thr_once(&rpc_call_once, rpc_call_setup);
148 1.23 thorpej rcp = thr_getspecific(rpc_call_key);
149 1.1 cgd }
150 1.20 fvdl #else
151 1.20 fvdl rcp = rpc_call_private_main;
152 1.20 fvdl #endif
153 1.21 christos if (rcp == NULL) {
154 1.21 christos rcp = malloc(sizeof (*rcp));
155 1.21 christos if (rcp == NULL) {
156 1.20 fvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
157 1.20 fvdl rpc_createerr.cf_error.re_errno = errno;
158 1.20 fvdl return (rpc_createerr.cf_stat);
159 1.20 fvdl }
160 1.23 thorpej if (__isthreaded == 0)
161 1.20 fvdl rpc_call_private_main = rcp;
162 1.20 fvdl else
163 1.20 fvdl thr_setspecific(rpc_call_key, (void *) rcp);
164 1.20 fvdl rcp->valid = 0;
165 1.20 fvdl rcp->client = NULL;
166 1.1 cgd }
167 1.25 fvdl if ((nettype == NULL) || (nettype[0] == 0))
168 1.20 fvdl nettype = "netpath";
169 1.20 fvdl if (!(rcp->valid && rcp->pid == getpid() &&
170 1.20 fvdl (rcp->prognum == prognum) &&
171 1.20 fvdl (rcp->versnum == versnum) &&
172 1.20 fvdl (!strcmp(rcp->host, host)) &&
173 1.20 fvdl (!strcmp(rcp->nettype, nettype)))) {
174 1.20 fvdl int fd;
175 1.20 fvdl
176 1.20 fvdl rcp->valid = 0;
177 1.20 fvdl if (rcp->client)
178 1.20 fvdl CLNT_DESTROY(rcp->client);
179 1.20 fvdl /*
180 1.20 fvdl * Using the first successful transport for that type
181 1.20 fvdl */
182 1.20 fvdl rcp->client = clnt_create(host, prognum, versnum, nettype);
183 1.20 fvdl rcp->pid = getpid();
184 1.21 christos if (rcp->client == NULL) {
185 1.20 fvdl return (rpc_createerr.cf_stat);
186 1.1 cgd }
187 1.20 fvdl /*
188 1.20 fvdl * Set time outs for connectionless case. Do it
189 1.20 fvdl * unconditionally. Faster than doing a t_getinfo()
190 1.20 fvdl * and then doing the right thing.
191 1.20 fvdl */
192 1.1 cgd timeout.tv_usec = 0;
193 1.1 cgd timeout.tv_sec = 5;
194 1.20 fvdl (void) CLNT_CONTROL(rcp->client,
195 1.21 christos CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
196 1.21 christos if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
197 1.29 christos (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
198 1.20 fvdl rcp->prognum = prognum;
199 1.20 fvdl rcp->versnum = versnum;
200 1.20 fvdl if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
201 1.20 fvdl (strlen(nettype) < (size_t)NETIDLEN)) {
202 1.20 fvdl (void) strcpy(rcp->host, host);
203 1.20 fvdl (void) strcpy(rcp->nettype, nettype);
204 1.20 fvdl rcp->valid = 1;
205 1.20 fvdl } else {
206 1.20 fvdl rcp->valid = 0;
207 1.20 fvdl }
208 1.20 fvdl } /* else reuse old client */
209 1.1 cgd tottimeout.tv_sec = 25;
210 1.1 cgd tottimeout.tv_usec = 0;
211 1.27 yamt clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, in,
212 1.21 christos outproc, out, tottimeout);
213 1.20 fvdl /*
214 1.1 cgd * if call failed, empty cache
215 1.1 cgd */
216 1.1 cgd if (clnt_stat != RPC_SUCCESS)
217 1.20 fvdl rcp->valid = 0;
218 1.20 fvdl return (clnt_stat);
219 1.1 cgd }
220