xref: /illumos-gate/usr/src/lib/libnsl/rpc/clnt_simple.c (revision a99cb9618990662acbd3bab1b4a5b05a6ca62556)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 /*
36  * Simplified front end to client rpc.
37  */
38 
39 #include "mt.h"
40 #include "rpc_mt.h"
41 #include <stdio.h>
42 #include <errno.h>
43 #include <rpc/rpc.h>
44 #include <string.h>
45 #include <sys/param.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 
49 #ifndef MAXHOSTNAMELEN
50 #define	MAXHOSTNAMELEN 64
51 #endif
52 
53 #ifndef NETIDLEN
54 #define	NETIDLEN 32
55 #endif
56 
57 struct rpc_call_private {
58 	int	valid;			/* Is this entry valid ? */
59 	CLIENT	*client;		/* Client handle */
60 	pid_t	pid;			/* process-id at moment of creation */
61 	rpcprog_t	prognum;	/* Program */
62 	rpcvers_t	versnum;	/* version */
63 	char	host[MAXHOSTNAMELEN];	/* Servers host */
64 	char	nettype[NETIDLEN];	/* Network type */
65 };
66 
67 static void
68 rpc_call_destroy(void *vp)
69 {
70 	struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
71 
72 	if (rcp) {
73 		if (rcp->client)
74 			CLNT_DESTROY(rcp->client);
75 		free(rcp);
76 	}
77 }
78 
79 /*
80  * This is the simplified interface to the client rpc layer.
81  * The client handle is not destroyed here and is reused for
82  * the future calls to same prog, vers, host and nettype combination.
83  *
84  * The total time available is 25 seconds.
85  */
86 enum clnt_stat
87 rpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum,
88 	const rpcproc_t procnum, const xdrproc_t inproc, const char *in,
89 	const xdrproc_t outproc, char  *out, const char *netclass)
90 {
91 	struct rpc_call_private *rcp;
92 	enum clnt_stat clnt_stat;
93 	struct timeval timeout, tottimeout;
94 	static pthread_key_t rpc_call_key = PTHREAD_ONCE_KEY_NP;
95 	char nettype_array[NETIDLEN];
96 	char *nettype = &nettype_array[0];
97 
98 	if (netclass == NULL)
99 		nettype = NULL;
100 	else {
101 		size_t len = strlen(netclass);
102 		if (len >= sizeof (nettype_array)) {
103 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
104 			return (rpc_createerr.cf_stat);
105 		}
106 		(void) strcpy(nettype, netclass);
107 	}
108 
109 	rcp = thr_get_storage(&rpc_call_key, sizeof (*rcp), rpc_call_destroy);
110 	if (rcp == NULL) {
111 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
112 		rpc_createerr.cf_error.re_errno = errno;
113 		return (rpc_createerr.cf_stat);
114 	}
115 
116 	if ((nettype == NULL) || (nettype[0] == '\0'))
117 		nettype = "netpath";
118 	if (!(rcp->valid &&
119 	    rcp->pid == getpid() &&
120 	    rcp->prognum == prognum &&
121 	    rcp->versnum == versnum &&
122 	    strcmp(rcp->host, host) == 0 &&
123 	    strcmp(rcp->nettype, nettype) == 0)) {
124 		int fd;
125 
126 		rcp->valid = 0;
127 		if (rcp->client)
128 			CLNT_DESTROY(rcp->client);
129 		/*
130 		 * Using the first successful transport for that type
131 		 */
132 		rcp->client = clnt_create(host, prognum, versnum, nettype);
133 		rcp->pid = getpid();
134 		if (rcp->client == NULL)
135 			return (rpc_createerr.cf_stat);
136 		/*
137 		 * Set time outs for connectionless case.  Do it
138 		 * unconditionally.  Faster than doing a t_getinfo()
139 		 * and then doing the right thing.
140 		 */
141 		timeout.tv_usec = 0;
142 		timeout.tv_sec = 5;
143 		(void) CLNT_CONTROL(rcp->client,
144 				CLSET_RETRY_TIMEOUT, (char *)&timeout);
145 		if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)&fd))
146 			(void) fcntl(fd, F_SETFD, 1);	/* close on exec */
147 		rcp->prognum = prognum;
148 		rcp->versnum = versnum;
149 		if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
150 		    (strlen(nettype) < (size_t)NETIDLEN)) {
151 			(void) strcpy(rcp->host, host);
152 			(void) strcpy(rcp->nettype, nettype);
153 			rcp->valid = 1;
154 		} else {
155 			rcp->valid = 0;
156 		}
157 	} /* else reuse old client */
158 	tottimeout.tv_sec = 25;
159 	tottimeout.tv_usec = 0;
160 	clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *)in,
161 				outproc, out, tottimeout);
162 	/*
163 	 * if call failed, empty cache
164 	 */
165 	if (clnt_stat != RPC_SUCCESS)
166 		rcp->valid = 0;
167 	return (clnt_stat);
168 }
169