1 /* $NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char *sccsid2 = "from: @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
37 static char *sccsid = "from: @(#)clnt_simple.c 2.2 88/08/01 4.0 RPCSRC";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
44 * Simplified front end to client rpc.
48 #include "namespace.h"
49 #include "reentrant.h"
50 #include <sys/param.h>
58 #include "un-namespace.h"
61 #ifndef MAXHOSTNAMELEN
62 #define MAXHOSTNAMELEN 64
69 struct rpc_call_private {
70 int valid; /* Is this entry valid ? */
71 CLIENT *client; /* Client handle */
72 pid_t pid; /* process-id at moment of creation */
73 rpcprog_t prognum; /* Program */
74 rpcvers_t versnum; /* Version */
75 char host[MAXHOSTNAMELEN]; /* Servers host */
76 char nettype[NETIDLEN]; /* Network type */
78 static struct rpc_call_private *rpc_call_private_main;
79 static thread_key_t rpc_call_key;
80 static once_t rpc_call_once = ONCE_INITIALIZER;
81 static int rpc_call_key_error;
83 static void rpc_call_key_init(void);
84 static void rpc_call_destroy(void *);
87 rpc_call_destroy(void *vp)
89 struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
93 CLNT_DESTROY(rcp->client);
99 rpc_call_key_init(void)
102 rpc_call_key_error = thr_keycreate(&rpc_call_key, rpc_call_destroy);
106 * This is the simplified interface to the client rpc layer.
107 * The client handle is not destroyed here and is reused for
108 * the future calls to same prog, vers, host and nettype combination.
110 * The total time available is 25 seconds.
113 rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
114 const char *host; /* host name */
115 rpcprog_t prognum; /* program number */
116 rpcvers_t versnum; /* version number */
117 rpcproc_t procnum; /* procedure number */
118 xdrproc_t inproc, outproc; /* in/out XDR procedures */
120 char *out; /* recv/send data */
121 const char *nettype; /* nettype */
123 struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
124 enum clnt_stat clnt_stat;
125 struct timeval timeout, tottimeout;
128 if ((main_thread = thr_main())) {
129 rcp = rpc_call_private_main;
131 if (thr_once(&rpc_call_once, rpc_call_key_init) != 0 ||
132 rpc_call_key_error != 0) {
133 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
134 rpc_createerr.cf_error.re_errno = rpc_call_key_error;
135 return (rpc_createerr.cf_stat);
137 rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
140 rcp = malloc(sizeof (*rcp));
142 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
143 rpc_createerr.cf_error.re_errno = errno;
144 return (rpc_createerr.cf_stat);
147 rpc_call_private_main = rcp;
149 thr_setspecific(rpc_call_key, (void *) rcp);
153 if ((nettype == NULL) || (nettype[0] == 0))
155 if (!(rcp->valid && rcp->pid == getpid() &&
156 (rcp->prognum == prognum) &&
157 (rcp->versnum == versnum) &&
158 (!strcmp(rcp->host, host)) &&
159 (!strcmp(rcp->nettype, nettype)))) {
164 CLNT_DESTROY(rcp->client);
166 * Using the first successful transport for that type
168 rcp->client = clnt_create(host, prognum, versnum, nettype);
170 if (rcp->client == NULL) {
171 return (rpc_createerr.cf_stat);
174 * Set time outs for connectionless case. Do it
175 * unconditionally. Faster than doing a t_getinfo()
176 * and then doing the right thing.
180 (void) CLNT_CONTROL(rcp->client,
181 CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
182 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
183 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
184 rcp->prognum = prognum;
185 rcp->versnum = versnum;
186 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
187 (strlen(nettype) < (size_t)NETIDLEN)) {
188 (void) strcpy(rcp->host, host);
189 (void) strcpy(rcp->nettype, nettype);
194 } /* else reuse old client */
195 tottimeout.tv_sec = 25;
196 tottimeout.tv_usec = 0;
197 /*LINTED const castaway*/
198 clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
199 outproc, out, tottimeout);
201 * if call failed, empty cache
203 if (clnt_stat != RPC_SUCCESS)