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 #pragma ident "%Z%%M% %I% %E% SMI"
36
37 /*
38 * Simplified front end to client rpc.
39 */
40
41 #include "mt.h"
42 #include "rpc_mt.h"
43 #include <stdio.h>
44 #include <errno.h>
45 #include <rpc/rpc.h>
46 #include <string.h>
47 #include <sys/param.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50
51 #ifndef MAXHOSTNAMELEN
52 #define MAXHOSTNAMELEN 64
53 #endif
54
55 #ifndef NETIDLEN
56 #define NETIDLEN 32
57 #endif
58
59 struct rpc_call_private {
60 int valid; /* Is this entry valid ? */
61 CLIENT *client; /* Client handle */
62 pid_t pid; /* process-id at moment of creation */
63 rpcprog_t prognum; /* Program */
64 rpcvers_t versnum; /* version */
65 char host[MAXHOSTNAMELEN]; /* Servers host */
66 char nettype[NETIDLEN]; /* Network type */
67 };
68
69 static void
rpc_call_destroy(void * vp)70 rpc_call_destroy(void *vp)
71 {
72 struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
73
74 if (rcp) {
75 if (rcp->client)
76 CLNT_DESTROY(rcp->client);
77 free(rcp);
78 }
79 }
80
81 /*
82 * This is the simplified interface to the client rpc layer.
83 * The client handle is not destroyed here and is reused for
84 * the future calls to same prog, vers, host and nettype combination.
85 *
86 * The total time available is 25 seconds.
87 */
88 enum clnt_stat
rpc_call(const char * host,const rpcprog_t prognum,const rpcvers_t versnum,const rpcproc_t procnum,const xdrproc_t inproc,const char * in,const xdrproc_t outproc,char * out,const char * netclass)89 rpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum,
90 const rpcproc_t procnum, const xdrproc_t inproc, const char *in,
91 const xdrproc_t outproc, char *out, const char *netclass)
92 {
93 struct rpc_call_private *rcp;
94 enum clnt_stat clnt_stat;
95 struct timeval timeout, tottimeout;
96 static pthread_key_t rpc_call_key = PTHREAD_ONCE_KEY_NP;
97 char nettype_array[NETIDLEN];
98 char *nettype = &nettype_array[0];
99
100 if (netclass == NULL)
101 nettype = NULL;
102 else {
103 size_t len = strlen(netclass);
104 if (len >= sizeof (nettype_array)) {
105 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
106 return (rpc_createerr.cf_stat);
107 }
108 (void) strcpy(nettype, netclass);
109 }
110
111 rcp = thr_get_storage(&rpc_call_key, sizeof (*rcp), rpc_call_destroy);
112 if (rcp == NULL) {
113 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
114 rpc_createerr.cf_error.re_errno = errno;
115 return (rpc_createerr.cf_stat);
116 }
117
118 if ((nettype == NULL) || (nettype[0] == NULL))
119 nettype = "netpath";
120 if (!(rcp->valid &&
121 rcp->pid == getpid() &&
122 rcp->prognum == prognum &&
123 rcp->versnum == versnum &&
124 strcmp(rcp->host, host) == 0 &&
125 strcmp(rcp->nettype, nettype) == 0)) {
126 int fd;
127
128 rcp->valid = 0;
129 if (rcp->client)
130 CLNT_DESTROY(rcp->client);
131 /*
132 * Using the first successful transport for that type
133 */
134 rcp->client = clnt_create(host, prognum, versnum, nettype);
135 rcp->pid = getpid();
136 if (rcp->client == NULL)
137 return (rpc_createerr.cf_stat);
138 /*
139 * Set time outs for connectionless case. Do it
140 * unconditionally. Faster than doing a t_getinfo()
141 * and then doing the right thing.
142 */
143 timeout.tv_usec = 0;
144 timeout.tv_sec = 5;
145 (void) CLNT_CONTROL(rcp->client,
146 CLSET_RETRY_TIMEOUT, (char *)&timeout);
147 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)&fd))
148 (void) fcntl(fd, F_SETFD, 1); /* close on exec */
149 rcp->prognum = prognum;
150 rcp->versnum = versnum;
151 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
152 (strlen(nettype) < (size_t)NETIDLEN)) {
153 (void) strcpy(rcp->host, host);
154 (void) strcpy(rcp->nettype, nettype);
155 rcp->valid = 1;
156 } else {
157 rcp->valid = 0;
158 }
159 } /* else reuse old client */
160 tottimeout.tv_sec = 25;
161 tottimeout.tv_usec = 0;
162 clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *)in,
163 outproc, out, tottimeout);
164 /*
165 * if call failed, empty cache
166 */
167 if (clnt_stat != RPC_SUCCESS)
168 rcp->valid = 0;
169 return (clnt_stat);
170 }
171