xref: /freebsd/lib/libc/rpc/clnt_simple.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*	$NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *sccsid2 = "from: @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid = "from: @(#)clnt_simple.c	2.2 88/08/01 4.0 RPCSRC";
39 #endif
40 /*
41  * clnt_simple.c
42  * Simplified front end to client rpc.
43  *
44  */
45 
46 #include "namespace.h"
47 #include "reentrant.h"
48 #include <sys/param.h>
49 #include <stdio.h>
50 #include <errno.h>
51 #include <rpc/rpc.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include <fcntl.h>
55 #include <unistd.h>
56 #include "un-namespace.h"
57 #include "mt_misc.h"
58 
59 #ifndef MAXHOSTNAMELEN
60 #define	MAXHOSTNAMELEN 64
61 #endif
62 
63 #ifndef NETIDLEN
64 #define	NETIDLEN 32
65 #endif
66 
67 struct rpc_call_private {
68 	int	valid;			/* Is this entry valid ? */
69 	CLIENT	*client;		/* Client handle */
70 	pid_t	pid;			/* process-id at moment of creation */
71 	rpcprog_t prognum;		/* Program */
72 	rpcvers_t versnum;		/* Version */
73 	char	host[MAXHOSTNAMELEN];	/* Servers host */
74 	char	nettype[NETIDLEN];	/* Network type */
75 };
76 static struct rpc_call_private *rpc_call_private_main;
77 static thread_key_t rpc_call_key;
78 static once_t rpc_call_once = ONCE_INITIALIZER;
79 static int rpc_call_key_error;
80 
81 static void rpc_call_key_init(void);
82 static void rpc_call_destroy(void *);
83 
84 static void
85 rpc_call_destroy(void *vp)
86 {
87 	struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
88 
89 	if (rcp) {
90 		if (rcp->client)
91 			CLNT_DESTROY(rcp->client);
92 		free(rcp);
93 	}
94 }
95 
96 static void
97 rpc_call_key_init(void)
98 {
99 
100 	rpc_call_key_error = thr_keycreate(&rpc_call_key, rpc_call_destroy);
101 }
102 
103 /*
104  * This is the simplified interface to the client rpc layer.
105  * The client handle is not destroyed here and is reused for
106  * the future calls to same prog, vers, host and nettype combination.
107  *
108  * The total time available is 25 seconds.
109  *
110  * host    - host name
111  * prognum - program number
112  * versnum - version number
113  * procnum - procedure number
114  * inproc, outproc -  in/out XDR procedures
115  * in, out - recv/send data
116  * nettype - nettype
117  */
118 enum clnt_stat
119 rpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum,
120     const rpcproc_t procnum, const xdrproc_t inproc, const char *in,
121     const xdrproc_t outproc, char *out, const char *nettype)
122 {
123 	struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
124 	enum clnt_stat clnt_stat;
125 	struct timeval timeout, tottimeout;
126 	int main_thread = 1;
127 
128 	if ((main_thread = thr_main())) {
129 		rcp = rpc_call_private_main;
130 	} else {
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);
136 		}
137 		rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
138 	}
139 	if (rcp == NULL) {
140 		rcp = malloc(sizeof (*rcp));
141 		if (rcp == NULL) {
142 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
143 			rpc_createerr.cf_error.re_errno = errno;
144 			return (rpc_createerr.cf_stat);
145 		}
146 		if (main_thread)
147 			rpc_call_private_main = rcp;
148 		else
149 			thr_setspecific(rpc_call_key, (void *) rcp);
150 		rcp->valid = 0;
151 		rcp->client = NULL;
152 	}
153 	if ((nettype == NULL) || (nettype[0] == 0))
154 		nettype = "netpath";
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)))) {
160 		int fd;
161 
162 		rcp->valid = 0;
163 		if (rcp->client)
164 			CLNT_DESTROY(rcp->client);
165 		/*
166 		 * Using the first successful transport for that type
167 		 */
168 		rcp->client = clnt_create(host, prognum, versnum, nettype);
169 		rcp->pid = getpid();
170 		if (rcp->client == NULL) {
171 			return (rpc_createerr.cf_stat);
172 		}
173 		/*
174 		 * Set time outs for connectionless case.  Do it
175 		 * unconditionally.  Faster than doing a t_getinfo()
176 		 * and then doing the right thing.
177 		 */
178 		timeout.tv_usec = 0;
179 		timeout.tv_sec = 5;
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);
190 			rcp->valid = 1;
191 		} else {
192 			rcp->valid = 0;
193 		}
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);
200 	/*
201 	 * if call failed, empty cache
202 	 */
203 	if (clnt_stat != RPC_SUCCESS)
204 		rcp->valid = 0;
205 	return (clnt_stat);
206 }
207