Expand PMF_FN_* macros.
[netbsd-mini2440.git] / lib / libc / rpc / clnt_simple.c
blob04c1492ed4abfb019302d78f6a45e93f61bf7e47
1 /* $NetBSD: clnt_simple.c,v 1.28 2006/11/03 20:18:49 christos Exp $ */
3 /*
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.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 /* #ident "@(#)clnt_simple.c 1.17 94/04/24 SMI" */
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)clnt_simple.c 1.49 89/01/31 Copyr 1984 Sun Micro";
41 #else
42 __RCSID("$NetBSD: clnt_simple.c,v 1.28 2006/11/03 20:18:49 christos Exp $");
43 #endif
44 #endif
47 * clnt_simple.c
48 * Simplified front end to client rpc.
52 #include "namespace.h"
53 #include "reentrant.h"
54 #include <sys/param.h>
55 #include <stdio.h>
56 #include <assert.h>
57 #include <errno.h>
58 #include <rpc/rpc.h>
59 #include <string.h>
60 #include <stdlib.h>
61 #include <fcntl.h>
62 #include <unistd.h>
64 #ifdef __weak_alias
65 __weak_alias(rpc_call,_rpc_call)
66 #endif
68 #ifndef MAXHOSTNAMELEN
69 #define MAXHOSTNAMELEN 64
70 #endif
72 #ifndef NETIDLEN
73 #define NETIDLEN 32
74 #endif
76 struct rpc_call_private {
77 int valid; /* Is this entry valid ? */
78 CLIENT *client; /* Client handle */
79 pid_t pid; /* process-id at moment of creation */
80 rpcprog_t prognum; /* Program */
81 rpcvers_t versnum; /* Version */
82 char host[MAXHOSTNAMELEN]; /* Servers host */
83 char nettype[NETIDLEN]; /* Network type */
85 static struct rpc_call_private *rpc_call_private_main;
87 #ifdef _REENTRANT
88 static void rpc_call_destroy __P((void *));
90 static void
91 rpc_call_destroy(void *vp)
93 struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
95 if (rcp) {
96 if (rcp->client)
97 CLNT_DESTROY(rcp->client);
98 free(rcp);
101 static thread_key_t rpc_call_key;
102 static once_t rpc_call_once = ONCE_INITIALIZER;
104 static void
105 rpc_call_setup(void)
108 thr_keycreate(&rpc_call_key, rpc_call_destroy);
110 #endif
114 * This is the simplified interface to the client rpc layer.
115 * The client handle is not destroyed here and is reused for
116 * the future calls to same prog, vers, host and nettype combination.
118 * The total time available is 25 seconds.
120 enum clnt_stat
121 rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
122 const char *host; /* host name */
123 rpcprog_t prognum; /* program number */
124 rpcvers_t versnum; /* version number */
125 rpcproc_t procnum; /* procedure number */
126 xdrproc_t inproc, outproc; /* in/out XDR procedures */
127 const char *in;
128 char *out; /* recv/send data */
129 const char *nettype; /* nettype */
131 struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
132 enum clnt_stat clnt_stat;
133 struct timeval timeout, tottimeout;
134 extern int __isthreaded;
136 _DIAGASSERT(host != NULL);
137 /* XXX: in may be NULL ??? */
138 /* XXX: out may be NULL ??? */
139 /* XXX: nettype may be NULL ??? */
141 #ifdef _REENTRANT
142 if (__isthreaded == 0) {
143 rcp = rpc_call_private_main;
144 } else {
145 thr_once(&rpc_call_once, rpc_call_setup);
146 rcp = thr_getspecific(rpc_call_key);
148 #else
149 rcp = rpc_call_private_main;
150 #endif
151 if (rcp == NULL) {
152 rcp = malloc(sizeof (*rcp));
153 if (rcp == NULL) {
154 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
155 rpc_createerr.cf_error.re_errno = errno;
156 return (rpc_createerr.cf_stat);
158 if (__isthreaded == 0)
159 rpc_call_private_main = rcp;
160 else
161 thr_setspecific(rpc_call_key, (void *) rcp);
162 rcp->valid = 0;
163 rcp->client = NULL;
165 if ((nettype == NULL) || (nettype[0] == 0))
166 nettype = "netpath";
167 if (!(rcp->valid && rcp->pid == getpid() &&
168 (rcp->prognum == prognum) &&
169 (rcp->versnum == versnum) &&
170 (!strcmp(rcp->host, host)) &&
171 (!strcmp(rcp->nettype, nettype)))) {
172 int fd;
174 rcp->valid = 0;
175 if (rcp->client)
176 CLNT_DESTROY(rcp->client);
178 * Using the first successful transport for that type
180 rcp->client = clnt_create(host, prognum, versnum, nettype);
181 rcp->pid = getpid();
182 if (rcp->client == NULL) {
183 return (rpc_createerr.cf_stat);
186 * Set time outs for connectionless case. Do it
187 * unconditionally. Faster than doing a t_getinfo()
188 * and then doing the right thing.
190 timeout.tv_usec = 0;
191 timeout.tv_sec = 5;
192 (void) CLNT_CONTROL(rcp->client,
193 CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
194 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
195 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
196 rcp->prognum = prognum;
197 rcp->versnum = versnum;
198 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
199 (strlen(nettype) < (size_t)NETIDLEN)) {
200 (void) strcpy(rcp->host, host);
201 (void) strcpy(rcp->nettype, nettype);
202 rcp->valid = 1;
203 } else {
204 rcp->valid = 0;
206 } /* else reuse old client */
207 tottimeout.tv_sec = 25;
208 tottimeout.tv_usec = 0;
209 clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, in,
210 outproc, out, tottimeout);
212 * if call failed, empty cache
214 if (clnt_stat != RPC_SUCCESS)
215 rcp->valid = 0;
216 return (clnt_stat);