Imported Upstream version 0.2.5
[platform/upstream/libtirpc.git] / src / clnt_simple.c
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 /*
29  * Copyright (c) 1986-1991 by Sun Microsystems Inc. 
30  */
31
32 #include <sys/cdefs.h>
33
34 /*
35  * clnt_simple.c
36  * Simplified front end to client rpc.
37  */
38 #include <pthread.h>
39 #include <reentrant.h>
40 #include <sys/param.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #include <rpc/rpc.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <fcntl.h>
47 #include <unistd.h>
48
49 #include <rpc/clnt.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 *);
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(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
92         const char *host;                       /* host name */
93         rpcprog_t prognum;                      /* program number */
94         rpcvers_t versnum;                      /* version number */
95         rpcproc_t procnum;                      /* procedure number */
96         xdrproc_t inproc, outproc;      /* in/out XDR procedures */
97         const char *in;
98         char  *out;                     /* recv/send data */
99         const char *nettype;                    /* nettype */
100 {
101         struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
102         enum clnt_stat clnt_stat;
103         struct timeval timeout, tottimeout;
104         extern thread_key_t rpc_call_key;
105         extern mutex_t tsd_lock;
106
107         if (rpc_call_key == KEY_INITIALIZER) {
108                 mutex_lock(&tsd_lock);
109                 if (rpc_call_key == KEY_INITIALIZER)
110                         thr_keycreate(&rpc_call_key, rpc_call_destroy);
111                 mutex_unlock(&tsd_lock);
112         }
113         rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
114         if (rcp == NULL) {
115                 rcp = malloc(sizeof (*rcp));
116                 if (rcp == NULL) {
117                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
118                         rpc_createerr.cf_error.re_errno = errno;
119                         return (rpc_createerr.cf_stat);
120                 }
121                 thr_setspecific(rpc_call_key, (void *) rcp);
122                 rcp->valid = 0;
123                 rcp->client = NULL;
124         }
125         if ((nettype == NULL) || (nettype[0] == 0))
126                 nettype = "netpath";
127         if (!(rcp->valid && rcp->pid == getpid() &&
128                 (rcp->prognum == prognum) &&
129                 (rcp->versnum == versnum) &&
130                 (!strcmp(rcp->host, host)) &&
131                 (!strcmp(rcp->nettype, nettype)))) {
132                 int fd;
133
134                 rcp->valid = 0;
135                 if (rcp->client)
136                         CLNT_DESTROY(rcp->client);
137                 /*
138                  * Using the first successful transport for that type
139                  */
140                 rcp->client = clnt_create(host, prognum, versnum, nettype);
141                 rcp->pid = getpid();
142                 if (rcp->client == NULL) {
143                         return (rpc_createerr.cf_stat);
144                 }
145                 /*
146                  * Set time outs for connectionless case.  Do it
147                  * unconditionally.  Faster than doing a t_getinfo()
148                  * and then doing the right thing.
149                  */
150                 timeout.tv_usec = 0;
151                 timeout.tv_sec = 5;
152                 (void) CLNT_CONTROL(rcp->client,
153                                 CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
154                 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
155                         fcntl(fd, F_SETFD, 1);  /* make it "close on exec" */
156                 rcp->prognum = prognum;
157                 rcp->versnum = versnum;
158                 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
159                     (strlen(nettype) < (size_t)NETIDLEN)) {
160                         (void) strcpy(rcp->host, host);
161                         (void) strcpy(rcp->nettype, nettype);
162                         rcp->valid = 1;
163                 } else {
164                         rcp->valid = 0;
165                 }
166         } /* else reuse old client */
167         tottimeout.tv_sec = 25;
168         tottimeout.tv_usec = 0;
169         
170         /* LINTED const castaway */
171         clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
172             outproc, out, tottimeout);
173         /*
174          * if call failed, empty cache
175          */
176         if (clnt_stat != RPC_SUCCESS)
177                 rcp->valid = 0;
178         return (clnt_stat);
179 }