xref: /dragonfly/lib/libc/rpc/clnt_simple.c (revision 65ebff40bad065cba7ef5daf1c3c241e91d349af)
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  * @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro
29  * @(#)clnt_simple.c          2.2 88/08/01 4.0 RPCSRC
30  * $NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $
31  * $FreeBSD: src/lib/libc/rpc/clnt_simple.c,v 1.20 2006/02/27 22:10:59 deischen Exp $
32  */
33 /*
34  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35  */
36 
37 /*
38  * clnt_simple.c
39  * Simplified front end to client rpc.
40  *
41  */
42 
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/param.h>
46 #include <stdio.h>
47 #include <errno.h>
48 #include <rpc/rpc.h>
49 #include <string.h>
50 #include <stdlib.h>
51 #include <fcntl.h>
52 #include <unistd.h>
53 #include "un-namespace.h"
54 #include "mt_misc.h"
55 
56 #ifndef MAXHOSTNAMELEN
57 #define   MAXHOSTNAMELEN 64
58 #endif
59 
60 #ifndef NETIDLEN
61 #define   NETIDLEN 32
62 #endif
63 
64 struct rpc_call_private {
65           int       valid;                        /* Is this entry valid ? */
66           CLIENT    *client;            /* Client handle */
67           pid_t     pid;                          /* process-id at moment of creation */
68           rpcprog_t prognum;            /* Program */
69           rpcvers_t versnum;            /* Version */
70           char      host[MAXHOSTNAMELEN];         /* Servers host */
71           char      nettype[NETIDLEN];  /* Network type */
72 };
73 static struct rpc_call_private *rpc_call_private_main;
74 
75 static void rpc_call_destroy(void *);
76 
77 static void
rpc_call_destroy(void * vp)78 rpc_call_destroy(void *vp)
79 {
80           struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
81 
82           if (rcp) {
83                     if (rcp->client)
84                               CLNT_DESTROY(rcp->client);
85                     free(rcp);
86           }
87 }
88 
89 /*
90  * This is the simplified interface to the client rpc layer.
91  * The client handle is not destroyed here and is reused for
92  * the future calls to same prog, vers, host and nettype combination.
93  *
94  * The total time available is 25 seconds.
95  *
96  * host    - host name
97  * prognum - program number
98  * versnum - version number
99  * procnum - procedure number
100  * inproc, outproc - in/out XDR procedures
101  * in, out - recv/send data
102  * nettype - nettype
103  */
104 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 * nettype)105 rpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum,
106     const rpcproc_t procnum, const xdrproc_t inproc, const char *in,
107     const xdrproc_t outproc, char *out, const char *nettype)
108 {
109           struct rpc_call_private *rcp = NULL;
110           enum clnt_stat clnt_stat;
111           struct timeval timeout, tottimeout;
112           static thread_key_t rpc_call_key;
113           int main_thread = 1;
114 
115           if ((main_thread = thr_main())) {
116                     rcp = rpc_call_private_main;
117           } else {
118                     if (rpc_call_key == 0) {
119                               mutex_lock(&tsd_lock);
120                               if (rpc_call_key == 0)
121                                         thr_keycreate(&rpc_call_key, rpc_call_destroy);
122                               mutex_unlock(&tsd_lock);
123                     }
124                     rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
125           }
126           if (rcp == NULL) {
127                     rcp = malloc(sizeof (*rcp));
128                     if (rcp == NULL) {
129                               rpc_createerr.cf_stat = RPC_SYSTEMERROR;
130                               rpc_createerr.cf_error.re_errno = errno;
131                               return (rpc_createerr.cf_stat);
132                     }
133                     if (main_thread)
134                               rpc_call_private_main = rcp;
135                     else
136                               thr_setspecific(rpc_call_key, (void *) rcp);
137                     rcp->valid = 0;
138                     rcp->client = NULL;
139           }
140           if ((nettype == NULL) || (nettype[0] == 0))
141                     nettype = "netpath";
142           if (!(rcp->valid && rcp->pid == getpid() &&
143                     (rcp->prognum == prognum) &&
144                     (rcp->versnum == versnum) &&
145                     (!strcmp(rcp->host, host)) &&
146                     (!strcmp(rcp->nettype, nettype)))) {
147                     int fd;
148 
149                     rcp->valid = 0;
150                     if (rcp->client)
151                               CLNT_DESTROY(rcp->client);
152                     /*
153                      * Using the first successful transport for that type
154                      */
155                     rcp->client = clnt_create(host, prognum, versnum, nettype);
156                     rcp->pid = getpid();
157                     if (rcp->client == NULL) {
158                               return (rpc_createerr.cf_stat);
159                     }
160                     /*
161                      * Set time outs for connectionless case.  Do it
162                      * unconditionally.  Faster than doing a t_getinfo()
163                      * and then doing the right thing.
164                      */
165                     timeout.tv_usec = 0;
166                     timeout.tv_sec = 5;
167                     CLNT_CONTROL(rcp->client,
168                                         CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
169                     if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
170                               _fcntl(fd, F_SETFD, 1);       /* make it "close on exec" */
171                     rcp->prognum = prognum;
172                     rcp->versnum = versnum;
173                     if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
174                         (strlen(nettype) < (size_t)NETIDLEN)) {
175                               strcpy(rcp->host, host);
176                               strcpy(rcp->nettype, nettype);
177                               rcp->valid = 1;
178                     } else {
179                               rcp->valid = 0;
180                     }
181           } /* else reuse old client */
182           tottimeout.tv_sec = 25;
183           tottimeout.tv_usec = 0;
184           /*LINTED const castaway*/
185           clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
186               outproc, out, tottimeout);
187           /*
188            * if call failed, empty cache
189            */
190           if (clnt_stat != RPC_SUCCESS)
191                     rcp->valid = 0;
192           return (clnt_stat);
193 }
194