| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include <pthread.h> |
| 38 |
#include <reentrant.h> |
| 39 |
#include <sys/param.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <errno.h> |
| 42 |
#include <rpc/rpc.h> |
| 43 |
#include <string.h> |
| 44 |
#include <stdlib.h> |
| 45 |
#include <fcntl.h> |
| 46 |
#include <unistd.h> |
| 47 |
|
| 48 |
#include <rpc/clnt.h> |
| 49 |
|
| 50 |
#ifndef MAXHOSTNAMELEN |
| 51 |
#define MAXHOSTNAMELEN 64 |
| 52 |
#endif |
| 53 |
|
| 54 |
#ifndef NETIDLEN |
| 55 |
#define NETIDLEN 32 |
| 56 |
#endif |
| 57 |
|
| 58 |
struct rpc_call_private { |
| 59 |
int valid; |
| 60 |
CLIENT *client; |
| 61 |
pid_t pid; |
| 62 |
rpcprog_t prognum; |
| 63 |
rpcvers_t versnum; |
| 64 |
char host[MAXHOSTNAMELEN]; |
| 65 |
char nettype[NETIDLEN]; |
| 66 |
}; |
| 67 |
|
| 68 |
static void rpc_call_destroy(void *); |
| 69 |
|
| 70 |
static void |
| 71 |
rpc_call_destroy(void *vp) |
| 72 |
{ |
| 73 |
struct rpc_call_private *rcp = (struct rpc_call_private *)vp; |
| 74 |
|
| 75 |
if (rcp) { |
| 76 |
if (rcp->client) |
| 77 |
CLNT_DESTROY(rcp->client); |
| 78 |
free(rcp); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
enum clnt_stat |
| 90 |
rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype) |
| 91 |
const char *host; |
| 92 |
rpcprog_t prognum; |
| 93 |
rpcvers_t versnum; |
| 94 |
rpcproc_t procnum; |
| 95 |
xdrproc_t inproc, outproc; |
| 96 |
const char *in; |
| 97 |
char *out; |
| 98 |
const char *nettype; |
| 99 |
{ |
| 100 |
struct rpc_call_private *rcp = (struct rpc_call_private *) 0; |
| 101 |
enum clnt_stat clnt_stat; |
| 102 |
struct timeval timeout, tottimeout; |
| 103 |
extern thread_key_t rpc_call_key; |
| 104 |
extern mutex_t tsd_lock; |
| 105 |
|
| 106 |
if (rpc_call_key == KEY_INITIALIZER) { |
| 107 |
mutex_lock(&tsd_lock); |
| 108 |
if (rpc_call_key == KEY_INITIALIZER) |
| 109 |
thr_keycreate(&rpc_call_key, rpc_call_destroy); |
| 110 |
mutex_unlock(&tsd_lock); |
| 111 |
} |
| 112 |
rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key); |
| 113 |
if (rcp == NULL) { |
| 114 |
rcp = malloc(sizeof (*rcp)); |
| 115 |
if (rcp == NULL) { |
| 116 |
rpc_createerr.cf_stat = RPC_SYSTEMERROR; |
| 117 |
rpc_createerr.cf_error.re_errno = errno; |
| 118 |
return (rpc_createerr.cf_stat); |
| 119 |
} |
| 120 |
thr_setspecific(rpc_call_key, (void *) rcp); |
| 121 |
rcp->valid = 0; |
| 122 |
rcp->client = NULL; |
| 123 |
} |
| 124 |
if ((nettype == NULL) || (nettype[0] == 0)) |
| 125 |
nettype = "netpath"; |
| 126 |
if (!(rcp->valid && rcp->pid == getpid() && |
| 127 |
(rcp->prognum == prognum) && |
| 128 |
(rcp->versnum == versnum) && |
| 129 |
(!strcmp(rcp->host, host)) && |
| 130 |
(!strcmp(rcp->nettype, nettype)))) { |
| 131 |
int fd; |
| 132 |
|
| 133 |
rcp->valid = 0; |
| 134 |
if (rcp->client) |
| 135 |
CLNT_DESTROY(rcp->client); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
rcp->client = clnt_create(host, prognum, versnum, nettype); |
| 140 |
rcp->pid = getpid(); |
| 141 |
if (rcp->client == NULL) { |
| 142 |
return (rpc_createerr.cf_stat); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
timeout.tv_usec = 0; |
| 150 |
timeout.tv_sec = 5; |
| 151 |
(void) CLNT_CONTROL(rcp->client, |
| 152 |
CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout); |
| 153 |
if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd)) |
| 154 |
fcntl(fd, F_SETFD, 1); |
| 155 |
rcp->prognum = prognum; |
| 156 |
rcp->versnum = versnum; |
| 157 |
if ((strlen(host) < (size_t)MAXHOSTNAMELEN) && |
| 158 |
(strlen(nettype) < (size_t)NETIDLEN)) { |
| 159 |
(void) strcpy(rcp->host, host); |
| 160 |
(void) strcpy(rcp->nettype, nettype); |
| 161 |
rcp->valid = 1; |
| 162 |
} else { |
| 163 |
rcp->valid = 0; |
| 164 |
} |
| 165 |
} |
| 166 |
tottimeout.tv_sec = 25; |
| 167 |
tottimeout.tv_usec = 0; |
| 168 |
|
| 169 |
|
| 170 |
clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in, |
| 171 |
outproc, out, tottimeout); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if (clnt_stat != RPC_SUCCESS) |
| 176 |
rcp->valid = 0; |
| 177 |
return (clnt_stat); |
| 178 |
} |