import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / nsl / clnt_simple.c
blob167429e0b428ae4f268111e8191cf98dcf2f1f6f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
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.
37 * Simplified front end to client rpc.
40 #include "mt.h"
41 #include "rpc_mt.h"
42 #include <stdio.h>
43 #include <errno.h>
44 #include <rpc/rpc.h>
45 #include <string.h>
46 #include <sys/param.h>
47 #include <stdlib.h>
48 #include <unistd.h>
50 #ifndef MAXHOSTNAMELEN
51 #define MAXHOSTNAMELEN 64
52 #endif
54 #ifndef NETIDLEN
55 #define NETIDLEN 32
56 #endif
58 struct rpc_call_private {
59 int valid; /* Is this entry valid ? */
60 CLIENT *client; /* Client handle */
61 pid_t pid; /* process-id at moment of creation */
62 rpcprog_t prognum; /* Program */
63 rpcvers_t versnum; /* version */
64 char host[MAXHOSTNAMELEN]; /* Servers host */
65 char nettype[NETIDLEN]; /* Network type */
68 static void
69 rpc_call_destroy(void *vp)
71 struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
73 if (rcp) {
74 if (rcp->client)
75 CLNT_DESTROY(rcp->client);
76 free(rcp);
81 * This is the simplified interface to the client rpc layer.
82 * The client handle is not destroyed here and is reused for
83 * the future calls to same prog, vers, host and nettype combination.
85 * The total time available is 25 seconds.
87 enum clnt_stat
88 rpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum,
89 const rpcproc_t procnum, const xdrproc_t inproc, const char *in,
90 const xdrproc_t outproc, char *out, const char *netclass)
92 struct rpc_call_private *rcp;
93 enum clnt_stat clnt_stat;
94 struct timeval timeout, tottimeout;
95 static pthread_key_t rpc_call_key = PTHREAD_ONCE_KEY_NP;
96 char nettype_array[NETIDLEN];
97 char *nettype = &nettype_array[0];
99 if (netclass == NULL)
100 nettype = NULL;
101 else {
102 size_t len = strlen(netclass);
103 if (len >= sizeof (nettype_array)) {
104 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
105 return (rpc_createerr.cf_stat);
107 (void) strcpy(nettype, netclass);
110 rcp = thr_get_storage(&rpc_call_key, sizeof (*rcp), rpc_call_destroy);
111 if (rcp == NULL) {
112 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
113 rpc_createerr.cf_error.re_errno = errno;
114 return (rpc_createerr.cf_stat);
117 if ((nettype == NULL) || (nettype[0] == '\0'))
118 nettype = "netpath";
119 if (!(rcp->valid &&
120 rcp->pid == getpid() &&
121 rcp->prognum == prognum &&
122 rcp->versnum == versnum &&
123 strcmp(rcp->host, host) == 0 &&
124 strcmp(rcp->nettype, nettype) == 0)) {
125 int fd;
127 rcp->valid = 0;
128 if (rcp->client)
129 CLNT_DESTROY(rcp->client);
131 * Using the first successful transport for that type
133 rcp->client = clnt_create(host, prognum, versnum, nettype);
134 rcp->pid = getpid();
135 if (rcp->client == NULL)
136 return (rpc_createerr.cf_stat);
138 * Set time outs for connectionless case. Do it
139 * unconditionally. Faster than doing a t_getinfo()
140 * and then doing the right thing.
142 timeout.tv_usec = 0;
143 timeout.tv_sec = 5;
144 (void) CLNT_CONTROL(rcp->client,
145 CLSET_RETRY_TIMEOUT, (char *)&timeout);
146 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)&fd))
147 (void) fcntl(fd, F_SETFD, 1); /* close on exec */
148 rcp->prognum = prognum;
149 rcp->versnum = versnum;
150 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
151 (strlen(nettype) < (size_t)NETIDLEN)) {
152 (void) strcpy(rcp->host, host);
153 (void) strcpy(rcp->nettype, nettype);
154 rcp->valid = 1;
155 } else {
156 rcp->valid = 0;
158 } /* else reuse old client */
159 tottimeout.tv_sec = 25;
160 tottimeout.tv_usec = 0;
161 clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *)in,
162 outproc, out, tottimeout);
164 * if call failed, empty cache
166 if (clnt_stat != RPC_SUCCESS)
167 rcp->valid = 0;
168 return (clnt_stat);