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