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