ReactOS  0.4.14-dev-323-g6fe6a88
clnt_simple.c
Go to the documentation of this file.
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 <wintirpc.h>
39 //#include <pthread.h>
40 #include <reentrant.h>
41 //#include <sys/param.h>
42 #include <stdio.h>
43 #include <errno.h>
44 #include <rpc/rpc.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <fcntl.h>
48 //#include <unistd.h>
49 
50 #include <rpc/clnt.h>
51 
52 #ifndef MAXHOSTNAMELEN
53 #define MAXHOSTNAMELEN 64
54 #endif
55 
56 #ifndef NETIDLEN
57 #define NETIDLEN 32
58 #endif
59 
61  int valid; /* Is this entry valid ? */
62  CLIENT *client; /* Client handle */
63  pid_t pid; /* process-id at moment of creation */
64  rpcprog_t prognum; /* Program */
65  rpcvers_t versnum; /* Version */
66  char host[MAXHOSTNAMELEN]; /* Servers host */
67  char nettype[NETIDLEN]; /* Network type */
68 };
69 
70 static void rpc_call_destroy(void *);
71 
72 static void
74 {
75  struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
76 
77  if (rcp) {
78  if (rcp->client)
79  CLNT_DESTROY(rcp->client);
80  free(rcp);
81  }
82 }
83 
84 /*
85  * This is the simplified interface to the client rpc layer.
86  * The client handle is not destroyed here and is reused for
87  * the future calls to same prog, vers, host and nettype combination.
88  *
89  * The total time available is 25 seconds.
90  */
91 enum clnt_stat
92 rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
93  const char *host; /* host name */
94  rpcprog_t prognum; /* program number */
95  rpcvers_t versnum; /* version number */
96  rpcproc_t procnum; /* procedure number */
97  xdrproc_t inproc, outproc; /* in/out XDR procedures */
98  const char *in;
99  char *out; /* recv/send data */
100  const char *nettype; /* nettype */
101 {
102  struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
103  enum clnt_stat clnt_stat;
104  struct timeval timeout, tottimeout;
105  extern thread_key_t rpc_call_key;
106  extern mutex_t tsd_lock;
107 
108  if (rpc_call_key == -1) {
110  if (rpc_call_key == -1)
113  }
115  if (rcp == NULL) {
116  rcp = malloc(sizeof (*rcp));
117  if (rcp == NULL) {
119  rpc_createerr.cf_error.re_errno = errno;
120  return (rpc_createerr.cf_stat);
121  }
122  thr_setspecific(rpc_call_key, (void *) rcp);
123  rcp->valid = 0;
124  rcp->client = NULL;
125  }
126  if ((nettype == NULL) || (nettype[0] == 0))
127  nettype = "netpath";
128  if (!(rcp->valid && rcp->pid == getpid() &&
129  (rcp->prognum == prognum) &&
130  (rcp->versnum == versnum) &&
131  (!strcmp(rcp->host, host)) &&
132  (!strcmp(rcp->nettype, nettype)))) {
133  int fd;
134 
135  rcp->valid = 0;
136  if (rcp->client)
137  CLNT_DESTROY(rcp->client);
138  /*
139  * Using the first successful transport for that type
140  */
142  rcp->pid = getpid();
143  if (rcp->client == NULL) {
144  return (rpc_createerr.cf_stat);
145  }
146  /*
147  * Set time outs for connectionless case. Do it
148  * unconditionally. Faster than doing a t_getinfo()
149  * and then doing the right thing.
150  */
151  timeout.tv_usec = 0;
152  timeout.tv_sec = 5;
153  (void) CLNT_CONTROL(rcp->client,
154  CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
155  if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
156  ; // XXX fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
157  rcp->prognum = prognum;
158  rcp->versnum = versnum;
159  if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
160  (strlen(nettype) < (size_t)NETIDLEN)) {
161  (void) strcpy(rcp->host, host);
162  (void) strcpy(rcp->nettype, nettype);
163  rcp->valid = 1;
164  } else {
165  rcp->valid = 0;
166  }
167  } /* else reuse old client */
168  tottimeout.tv_sec = 25;
169  tottimeout.tv_usec = 0;
170 
171  /* LINTED const castaway */
172  clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
173  outproc, out, tottimeout);
174  /*
175  * if call failed, empty cache
176  */
177  if (clnt_stat != RPC_SUCCESS)
178  rcp->valid = 0;
179  return (clnt_stat);
180 }
u_int32_t rpcproc_t
Definition: types.h:106
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
u_int32_t rpcvers_t
Definition: types.h:105
#define CLSET_RETRY_TIMEOUT
Definition: clnt.h:265
#define thr_keycreate(k, d)
Definition: reentrant.h:144
#define CLGET_FD
Definition: clnt.h:249
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define thr_setspecific(k, p)
Definition: reentrant.h:146
#define free
Definition: debug_ros.c:5
thread_key_t rpc_call_key
Definition: mt_misc.c:90
char * host
Definition: whois.c:55
DWORD pid_t
Definition: types.h:91
enum clnt_stat rpc_call(char *host, rpcprog_t prognum, rpcvers_t versnum, rpcproc_t procnum, xdrproc_t inproc, const char *in, xdrproc_t outproc, char *out, const char *nettype) const
Definition: clnt_simple.c:92
static int fd
Definition: io.c:51
CLIENT * clnt_create(const char *hostname, const rpcprog_t prog, const rpcvers_t vers, const char *nettype)
Definition: clnt_generic.c:179
Definition: dhcpd.h:245
int errno
#define mutex_lock(m)
Definition: reentrant.h:128
#define mutex_unlock(m)
Definition: reentrant.h:129
bool_t(* xdrproc_t)(XDR *,...)
Definition: xdr.h:144
#define NETIDLEN
Definition: clnt_simple.c:57
#define thread_key_t
Definition: reentrant.h:124
static void rpc_call_destroy(void *)
Definition: clnt_simple.c:73
#define CLNT_CALL(rh, proc, xargs, argsp, xres, resp, secs)
Definition: clnt.h:199
char nettype[NETIDLEN]
Definition: clnt_simple.c:67
smooth NULL
Definition: ftsmooth.c:416
CLIENT * client
Definition: clnt_simple.c:62
mutex_t tsd_lock
Definition: mt_misc.c:86
__kernel_size_t size_t
Definition: linux.h:237
_CRTIMP int __cdecl getpid(void)
enum clnt_stat cf_stat
Definition: clnt.h:496
static FILE * out
Definition: regtests2xml.c:44
u_int32_t rpcprog_t
Definition: types.h:104
#define MAXHOSTNAMELEN
Definition: clnt_simple.c:53
#define thr_getspecific(k)
Definition: reentrant.h:147
#define CLNT_DESTROY(rh)
Definition: clnt.h:275
GLuint in
Definition: glext.h:9616
rpcprog_t prognum
Definition: clnt_simple.c:64
char host[MAXHOSTNAMELEN]
Definition: clnt_simple.c:66
struct rpc_err cf_error
Definition: clnt.h:497
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define malloc
Definition: debug_ros.c:4
#define CLNT_CONTROL(cl, rq, in)
Definition: clnt.h:240
Definition: module.h:446
rpcvers_t versnum
Definition: clnt_simple.c:65
static const struct timeval tottimeout
Definition: pmap_getport.c:44
clnt_stat
Definition: clnt_stat.h:21