Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / lib / libc / rpc / clnt_simple.c
blob5c92d0518afc280e318ffa4c77bbf5c8d268cfdd
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
29 * @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro
30 * @(#)clnt_simple.c 2.2 88/08/01 4.0 RPCSRC
31 * $NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $
32 * $FreeBSD: src/lib/libc/rpc/clnt_simple.c,v 1.20 2006/02/27 22:10:59 deischen Exp $
33 * $DragonFly: src/lib/libc/rpc/clnt_simple.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
36 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
40 * clnt_simple.c
41 * Simplified front end to client rpc.
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <sys/param.h>
48 #include <stdio.h>
49 #include <errno.h>
50 #include <rpc/rpc.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <fcntl.h>
54 #include <unistd.h>
55 #include "un-namespace.h"
56 #include "mt_misc.h"
58 #ifndef MAXHOSTNAMELEN
59 #define MAXHOSTNAMELEN 64
60 #endif
62 #ifndef NETIDLEN
63 #define NETIDLEN 32
64 #endif
66 struct rpc_call_private {
67 int valid; /* Is this entry valid ? */
68 CLIENT *client; /* Client handle */
69 pid_t pid; /* process-id at moment of creation */
70 rpcprog_t prognum; /* Program */
71 rpcvers_t versnum; /* Version */
72 char host[MAXHOSTNAMELEN]; /* Servers host */
73 char nettype[NETIDLEN]; /* Network type */
75 static struct rpc_call_private *rpc_call_private_main;
77 static void rpc_call_destroy(void *);
79 static void
80 rpc_call_destroy(void *vp)
82 struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
84 if (rcp) {
85 if (rcp->client)
86 CLNT_DESTROY(rcp->client);
87 free(rcp);
92 * This is the simplified interface to the client rpc layer.
93 * The client handle is not destroyed here and is reused for
94 * the future calls to same prog, vers, host and nettype combination.
96 * The total time available is 25 seconds.
98 enum clnt_stat
99 rpc_call(const char *host, /* host name */
100 rpcprog_t prognum, /* program number */
101 rpcvers_t versnum, /* version number */
102 rpcproc_t procnum, /* procedure number */
103 xdrproc_t inproc,
104 const char *in,
105 xdrproc_t outproc, /* in/out XDR procedures */
106 char *out, /* recv/send data */
107 const char *nettype) /* nettype */
109 struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
110 enum clnt_stat clnt_stat;
111 struct timeval timeout, tottimeout;
112 static thread_key_t rpc_call_key;
113 int main_thread = 1;
115 if ((main_thread = thr_main())) {
116 rcp = rpc_call_private_main;
117 } else {
118 if (rpc_call_key == 0) {
119 mutex_lock(&tsd_lock);
120 if (rpc_call_key == 0)
121 thr_keycreate(&rpc_call_key, rpc_call_destroy);
122 mutex_unlock(&tsd_lock);
124 rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
126 if (rcp == NULL) {
127 rcp = malloc(sizeof (*rcp));
128 if (rcp == NULL) {
129 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
130 rpc_createerr.cf_error.re_errno = errno;
131 return (rpc_createerr.cf_stat);
133 if (main_thread)
134 rpc_call_private_main = rcp;
135 else
136 thr_setspecific(rpc_call_key, (void *) rcp);
137 rcp->valid = 0;
138 rcp->client = NULL;
140 if ((nettype == NULL) || (nettype[0] == 0))
141 nettype = "netpath";
142 if (!(rcp->valid && rcp->pid == getpid() &&
143 (rcp->prognum == prognum) &&
144 (rcp->versnum == versnum) &&
145 (!strcmp(rcp->host, host)) &&
146 (!strcmp(rcp->nettype, nettype)))) {
147 int fd;
149 rcp->valid = 0;
150 if (rcp->client)
151 CLNT_DESTROY(rcp->client);
153 * Using the first successful transport for that type
155 rcp->client = clnt_create(host, prognum, versnum, nettype);
156 rcp->pid = getpid();
157 if (rcp->client == NULL) {
158 return (rpc_createerr.cf_stat);
161 * Set time outs for connectionless case. Do it
162 * unconditionally. Faster than doing a t_getinfo()
163 * and then doing the right thing.
165 timeout.tv_usec = 0;
166 timeout.tv_sec = 5;
167 CLNT_CONTROL(rcp->client,
168 CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
169 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
170 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
171 rcp->prognum = prognum;
172 rcp->versnum = versnum;
173 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
174 (strlen(nettype) < (size_t)NETIDLEN)) {
175 strcpy(rcp->host, host);
176 strcpy(rcp->nettype, nettype);
177 rcp->valid = 1;
178 } else {
179 rcp->valid = 0;
181 } /* else reuse old client */
182 tottimeout.tv_sec = 25;
183 tottimeout.tv_usec = 0;
184 /*LINTED const castaway*/
185 clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
186 outproc, out, tottimeout);
188 * if call failed, empty cache
190 if (clnt_stat != RPC_SUCCESS)
191 rcp->valid = 0;
192 return (clnt_stat);