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