| 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 |
#include <pthread.h> |
| 36 |
#include <reentrant.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/socket.h> |
| 39 |
#include <stdint.h> |
| 40 |
#include <poll.h> |
| 41 |
|
| 42 |
#include <sys/time.h> |
| 43 |
|
| 44 |
#include <sys/ioctl.h> |
| 45 |
#include <rpc/clnt.h> |
| 46 |
#include <arpa/inet.h> |
| 47 |
#include <rpc/rpc.h> |
| 48 |
#include <rpc/xdr.h> |
| 49 |
#include <errno.h> |
| 50 |
#include <stdlib.h> |
| 51 |
#include <string.h> |
| 52 |
#include <signal.h> |
| 53 |
#include <unistd.h> |
| 54 |
#include <err.h> |
| 55 |
#include "rpc_com.h" |
| 56 |
#include "clnt_fd_locks.h" |
| 57 |
|
| 58 |
#ifdef IP_RECVERR |
| 59 |
#include <asm/types.h> |
| 60 |
#include <linux/errqueue.h> |
| 61 |
#include <sys/uio.h> |
| 62 |
#endif |
| 63 |
|
| 64 |
#ifdef HAVE_RPCSEC_GSS |
| 65 |
#include <rpc/auth_gss.h> |
| 66 |
#endif |
| 67 |
|
| 68 |
#define MAX_DEFAULT_FDS 20000 |
| 69 |
|
| 70 |
static struct clnt_ops *clnt_dg_ops(void); |
| 71 |
static bool_t time_not_ok(struct timeval *); |
| 72 |
static enum clnt_stat clnt_dg_call(CLIENT *, rpcproc_t, xdrproc_t, void *, |
| 73 |
xdrproc_t, void *, struct timeval); |
| 74 |
static void clnt_dg_geterr(CLIENT *, struct rpc_err *); |
| 75 |
static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *); |
| 76 |
static void clnt_dg_abort(CLIENT *); |
| 77 |
static bool_t clnt_dg_control(CLIENT *, u_int, void *); |
| 78 |
static void clnt_dg_destroy(CLIENT *); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static fd_locks_t *dg_fd_locks; |
| 100 |
extern mutex_t clnt_fd_lock; |
| 101 |
#define release_fd_lock(fd_lock, mask) { \ |
| 102 |
mutex_lock(&clnt_fd_lock); \ |
| 103 |
fd_lock->active = FALSE; \ |
| 104 |
fd_lock->pending--; \ |
| 105 |
thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \ |
| 106 |
cond_signal(&fd_lock->cv); \ |
| 107 |
mutex_unlock(&clnt_fd_lock); \ |
| 108 |
} |
| 109 |
|
| 110 |
static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory"; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
struct cu_data { |
| 118 |
int cu_fd; |
| 119 |
fd_lock_t *cu_fd_lock; |
| 120 |
bool_t cu_closeit; |
| 121 |
struct sockaddr_storage cu_raddr; |
| 122 |
int cu_rlen; |
| 123 |
struct timeval cu_wait; |
| 124 |
struct timeval cu_total; |
| 125 |
struct rpc_err cu_error; |
| 126 |
XDR cu_outxdrs; |
| 127 |
u_int cu_xdrpos; |
| 128 |
u_int cu_sendsz; |
| 129 |
char *cu_outbuf; |
| 130 |
u_int cu_recvsz; |
| 131 |
int cu_async; |
| 132 |
int cu_connect; |
| 133 |
int cu_connected; |
| 134 |
char cu_inbuf[1]; |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
CLIENT * |
| 152 |
clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz) |
| 153 |
int fd; |
| 154 |
const struct netbuf *svcaddr; |
| 155 |
rpcprog_t program; |
| 156 |
rpcvers_t version; |
| 157 |
u_int sendsz; |
| 158 |
u_int recvsz; |
| 159 |
{ |
| 160 |
CLIENT *cl = NULL; |
| 161 |
struct cu_data *cu = NULL; |
| 162 |
struct timeval now; |
| 163 |
struct rpc_msg call_msg; |
| 164 |
sigset_t mask; |
| 165 |
sigset_t newmask; |
| 166 |
struct __rpc_sockinfo si; |
| 167 |
int one = 1; |
| 168 |
fd_lock_t *fd_lock; |
| 169 |
|
| 170 |
sigfillset(&newmask); |
| 171 |
thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
| 172 |
mutex_lock(&clnt_fd_lock); |
| 173 |
if (dg_fd_locks == (fd_locks_t *) NULL) { |
| 174 |
dg_fd_locks = fd_locks_init(); |
| 175 |
if (dg_fd_locks == (fd_locks_t *) NULL) { |
| 176 |
mutex_unlock(&clnt_fd_lock); |
| 177 |
goto err1; |
| 178 |
} |
| 179 |
} |
| 180 |
fd_lock = fd_lock_create(fd, dg_fd_locks); |
| 181 |
if (fd_lock == (fd_lock_t *) NULL) { |
| 182 |
mutex_unlock(&clnt_fd_lock); |
| 183 |
goto err1; |
| 184 |
} |
| 185 |
|
| 186 |
mutex_unlock(&clnt_fd_lock); |
| 187 |
thr_sigsetmask(SIG_SETMASK, &(mask), NULL); |
| 188 |
|
| 189 |
if (svcaddr == NULL) { |
| 190 |
rpc_createerr.cf_stat = RPC_UNKNOWNADDR; |
| 191 |
return (NULL); |
| 192 |
} |
| 193 |
|
| 194 |
if (!__rpc_fd2sockinfo(fd, &si)) { |
| 195 |
rpc_createerr.cf_stat = RPC_TLIERROR; |
| 196 |
rpc_createerr.cf_error.re_errno = 0; |
| 197 |
return (NULL); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); |
| 203 |
recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); |
| 204 |
if ((sendsz == 0) || (recvsz == 0)) { |
| 205 |
rpc_createerr.cf_stat = RPC_TLIERROR; |
| 206 |
rpc_createerr.cf_error.re_errno = 0; |
| 207 |
return (NULL); |
| 208 |
} |
| 209 |
|
| 210 |
if ((cl = mem_alloc(sizeof (CLIENT))) == NULL) |
| 211 |
goto err1; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
sendsz = ((sendsz + 3) / 4) * 4; |
| 216 |
recvsz = ((recvsz + 3) / 4) * 4; |
| 217 |
cu = mem_alloc(sizeof (*cu) + sendsz + recvsz); |
| 218 |
if (cu == NULL) |
| 219 |
goto err1; |
| 220 |
(void) memcpy(&cu->cu_raddr, svcaddr->buf, (size_t)svcaddr->len); |
| 221 |
cu->cu_rlen = svcaddr->len; |
| 222 |
cu->cu_outbuf = &cu->cu_inbuf[recvsz]; |
| 223 |
|
| 224 |
cu->cu_wait.tv_sec = 15; |
| 225 |
cu->cu_wait.tv_usec = 0; |
| 226 |
cu->cu_total.tv_sec = -1; |
| 227 |
cu->cu_total.tv_usec = -1; |
| 228 |
cu->cu_sendsz = sendsz; |
| 229 |
cu->cu_recvsz = recvsz; |
| 230 |
cu->cu_async = FALSE; |
| 231 |
cu->cu_connect = FALSE; |
| 232 |
cu->cu_connected = FALSE; |
| 233 |
(void) gettimeofday(&now, NULL); |
| 234 |
call_msg.rm_xid = __RPC_GETXID(&now); |
| 235 |
call_msg.rm_call.cb_prog = program; |
| 236 |
call_msg.rm_call.cb_vers = version; |
| 237 |
xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE); |
| 238 |
if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) { |
| 239 |
rpc_createerr.cf_stat = RPC_CANTENCODEARGS; |
| 240 |
rpc_createerr.cf_error.re_errno = 0; |
| 241 |
goto err2; |
| 242 |
} |
| 243 |
cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs)); |
| 244 |
|
| 245 |
|
| 246 |
#if 0 |
| 247 |
(void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf); |
| 248 |
#endif |
| 249 |
#ifdef IP_RECVERR |
| 250 |
{ |
| 251 |
int on = 1; |
| 252 |
setsockopt(fd, SOL_IP, IP_RECVERR, &on, sizeof(on)); |
| 253 |
} |
| 254 |
#endif |
| 255 |
ioctl(fd, FIONBIO, (char *)(void *)&one); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
cu->cu_closeit = FALSE; |
| 262 |
cu->cu_fd = fd; |
| 263 |
cu->cu_fd_lock = fd_lock; |
| 264 |
cl->cl_ops = clnt_dg_ops(); |
| 265 |
cl->cl_private = (caddr_t)(void *)cu; |
| 266 |
cl->cl_auth = authnone_create(); |
| 267 |
cl->cl_tp = NULL; |
| 268 |
cl->cl_netid = NULL; |
| 269 |
|
| 270 |
return (cl); |
| 271 |
err1: |
| 272 |
warnx(mem_err_clnt_dg); |
| 273 |
rpc_createerr.cf_stat = RPC_SYSTEMERROR; |
| 274 |
rpc_createerr.cf_error.re_errno = errno; |
| 275 |
err2: |
| 276 |
if (cl) { |
| 277 |
mem_free(cl, sizeof (CLIENT)); |
| 278 |
if (cu) |
| 279 |
mem_free(cu, sizeof (*cu) + sendsz + recvsz); |
| 280 |
} |
| 281 |
return (NULL); |
| 282 |
} |
| 283 |
|
| 284 |
static enum clnt_stat |
| 285 |
clnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout) |
| 286 |
CLIENT *cl; |
| 287 |
rpcproc_t proc; |
| 288 |
xdrproc_t xargs; |
| 289 |
void *argsp; |
| 290 |
xdrproc_t xresults; |
| 291 |
void *resultsp; |
| 292 |
struct timeval utimeout; |
| 293 |
{ |
| 294 |
struct cu_data *cu = (struct cu_data *)cl->cl_private; |
| 295 |
XDR *xdrs; |
| 296 |
size_t outlen = 0; |
| 297 |
struct rpc_msg reply_msg; |
| 298 |
XDR reply_xdrs; |
| 299 |
bool_t ok; |
| 300 |
int nrefreshes = 2; |
| 301 |
struct timeval timeout; |
| 302 |
struct pollfd fd; |
| 303 |
int total_time, nextsend_time, tv=0; |
| 304 |
struct sockaddr *sa; |
| 305 |
sigset_t mask; |
| 306 |
sigset_t newmask; |
| 307 |
socklen_t salen; |
| 308 |
ssize_t recvlen = 0; |
| 309 |
u_int32_t xid, inval, outval; |
| 310 |
|
| 311 |
outlen = 0; |
| 312 |
sigfillset(&newmask); |
| 313 |
thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
| 314 |
mutex_lock(&clnt_fd_lock); |
| 315 |
cu->cu_fd_lock->pending++; |
| 316 |
while (cu->cu_fd_lock->active) |
| 317 |
cond_wait(&cu->cu_fd_lock->cv, &clnt_fd_lock); |
| 318 |
cu->cu_fd_lock->active = TRUE; |
| 319 |
mutex_unlock(&clnt_fd_lock); |
| 320 |
if (cu->cu_total.tv_usec == -1) { |
| 321 |
timeout = utimeout; |
| 322 |
} else { |
| 323 |
timeout = cu->cu_total; |
| 324 |
} |
| 325 |
total_time = timeout.tv_sec * 1000 + timeout.tv_usec / 1000; |
| 326 |
nextsend_time = cu->cu_wait.tv_sec * 1000 + cu->cu_wait.tv_usec / 1000; |
| 327 |
|
| 328 |
if (cu->cu_connect && !cu->cu_connected) { |
| 329 |
if (connect(cu->cu_fd, (struct sockaddr *)&cu->cu_raddr, |
| 330 |
cu->cu_rlen) < 0) { |
| 331 |
cu->cu_error.re_errno = errno; |
| 332 |
cu->cu_error.re_status = RPC_CANTSEND; |
| 333 |
goto out; |
| 334 |
} |
| 335 |
cu->cu_connected = 1; |
| 336 |
} |
| 337 |
if (cu->cu_connected) { |
| 338 |
sa = NULL; |
| 339 |
salen = 0; |
| 340 |
} else { |
| 341 |
sa = (struct sockaddr *)&cu->cu_raddr; |
| 342 |
salen = cu->cu_rlen; |
| 343 |
} |
| 344 |
|
| 345 |
#ifdef HAVE_RPCSEC_GSS |
| 346 |
if (is_authgss_client(cl)) |
| 347 |
nrefreshes = 0; |
| 348 |
#endif |
| 349 |
|
| 350 |
|
| 351 |
call_again: |
| 352 |
xdrs = &(cu->cu_outxdrs); |
| 353 |
if (cu->cu_async == TRUE && xargs == NULL) |
| 354 |
goto get_reply; |
| 355 |
xdrs->x_op = XDR_ENCODE; |
| 356 |
XDR_SETPOS(xdrs, cu->cu_xdrpos); |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
xid = ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf)); |
| 363 |
xid++; |
| 364 |
*(u_int32_t *)(void *)(cu->cu_outbuf) = htonl(xid); |
| 365 |
|
| 366 |
if ((! XDR_PUTINT32(xdrs, (int32_t *)&proc)) || |
| 367 |
(! AUTH_MARSHALL(cl->cl_auth, xdrs)) || |
| 368 |
(! AUTH_WRAP(cl->cl_auth, xdrs, xargs, argsp))) { |
| 369 |
cu->cu_error.re_status = RPC_CANTENCODEARGS; |
| 370 |
goto out; |
| 371 |
} |
| 372 |
outlen = (size_t)XDR_GETPOS(xdrs); |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { |
| 378 |
cu->cu_error.re_status = RPC_TIMEDOUT; |
| 379 |
goto out; |
| 380 |
} |
| 381 |
|
| 382 |
send_again: |
| 383 |
if (total_time <= 0) { |
| 384 |
cu->cu_error.re_status = RPC_TIMEDOUT; |
| 385 |
goto out; |
| 386 |
} |
| 387 |
nextsend_time = cu->cu_wait.tv_sec * 1000 + cu->cu_wait.tv_usec / 1000; |
| 388 |
if (sendto(cu->cu_fd, cu->cu_outbuf, outlen, 0, sa, salen) != outlen) { |
| 389 |
cu->cu_error.re_errno = errno; |
| 390 |
cu->cu_error.re_status = RPC_CANTSEND; |
| 391 |
goto out; |
| 392 |
} |
| 393 |
|
| 394 |
get_reply: |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
reply_msg.acpted_rply.ar_verf = _null_auth; |
| 402 |
reply_msg.acpted_rply.ar_results.where = NULL; |
| 403 |
reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; |
| 404 |
|
| 405 |
fd.fd = cu->cu_fd; |
| 406 |
fd.events = POLLIN; |
| 407 |
fd.revents = 0; |
| 408 |
while (total_time > 0) { |
| 409 |
tv = total_time < nextsend_time ? total_time : nextsend_time; |
| 410 |
switch (poll(&fd, 1, tv)) { |
| 411 |
case 0: |
| 412 |
total_time -= tv; |
| 413 |
goto send_again; |
| 414 |
case -1: |
| 415 |
if (errno == EINTR) |
| 416 |
continue; |
| 417 |
cu->cu_error.re_status = RPC_CANTRECV; |
| 418 |
cu->cu_error.re_errno = errno; |
| 419 |
goto out; |
| 420 |
} |
| 421 |
break; |
| 422 |
} |
| 423 |
#ifdef IP_RECVERR |
| 424 |
if (fd.revents & POLLERR) |
| 425 |
{ |
| 426 |
struct msghdr msg; |
| 427 |
struct cmsghdr *cmsg; |
| 428 |
struct sock_extended_err *e; |
| 429 |
struct sockaddr_in err_addr; |
| 430 |
struct sockaddr_in *sin = (struct sockaddr_in *)&cu->cu_raddr; |
| 431 |
struct iovec iov; |
| 432 |
char *cbuf = (char *) mem_alloc((outlen + 256)); |
| 433 |
int ret; |
| 434 |
|
| 435 |
if (cbuf == NULL) |
| 436 |
{ |
| 437 |
cu->cu_error.re_errno = errno; |
| 438 |
return (cu->cu_error.re_status = RPC_CANTRECV); |
| 439 |
} |
| 440 |
iov.iov_base = cbuf + 256; |
| 441 |
iov.iov_len = outlen; |
| 442 |
msg.msg_name = (void *) &err_addr; |
| 443 |
msg.msg_namelen = sizeof (err_addr); |
| 444 |
msg.msg_iov = &iov; |
| 445 |
msg.msg_iovlen = 1; |
| 446 |
msg.msg_flags = 0; |
| 447 |
msg.msg_control = cbuf; |
| 448 |
msg.msg_controllen = 256; |
| 449 |
ret = recvmsg (cu->cu_fd, &msg, MSG_ERRQUEUE); |
| 450 |
if (ret >= 0 |
| 451 |
&& memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0 |
| 452 |
&& (msg.msg_flags & MSG_ERRQUEUE) |
| 453 |
&& ((msg.msg_namelen == 0 |
| 454 |
&& ret >= 12) |
| 455 |
|| (msg.msg_namelen == sizeof (err_addr) |
| 456 |
&& err_addr.sin_family == AF_INET |
| 457 |
&& memcmp (&err_addr.sin_addr, &sin->sin_addr, |
| 458 |
sizeof (err_addr.sin_addr)) == 0 |
| 459 |
&& err_addr.sin_port == sin->sin_port))) |
| 460 |
for (cmsg = CMSG_FIRSTHDR (&msg); cmsg; |
| 461 |
cmsg = CMSG_NXTHDR (&msg, cmsg)) |
| 462 |
if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR) |
| 463 |
{ |
| 464 |
e = (struct sock_extended_err *) CMSG_DATA(cmsg); |
| 465 |
cu->cu_error.re_errno = e->ee_errno; |
| 466 |
mem_free(cbuf, (outlen + 256)); |
| 467 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 468 |
return (cu->cu_error.re_status = RPC_CANTRECV); |
| 469 |
} |
| 470 |
mem_free(cbuf, (outlen + 256)); |
| 471 |
} |
| 472 |
#endif |
| 473 |
|
| 474 |
|
| 475 |
do { |
| 476 |
recvlen = recvfrom(cu->cu_fd, cu->cu_inbuf, |
| 477 |
cu->cu_recvsz, 0, NULL, NULL); |
| 478 |
} while (recvlen < 0 && errno == EINTR); |
| 479 |
if (recvlen < 0 && errno != EWOULDBLOCK) { |
| 480 |
cu->cu_error.re_errno = errno; |
| 481 |
cu->cu_error.re_status = RPC_CANTRECV; |
| 482 |
goto out; |
| 483 |
} |
| 484 |
|
| 485 |
if (recvlen < sizeof(u_int32_t)) { |
| 486 |
total_time -= tv; |
| 487 |
goto send_again; |
| 488 |
} |
| 489 |
|
| 490 |
if (cu->cu_async == FALSE) { |
| 491 |
memcpy(&inval, cu->cu_inbuf, sizeof(u_int32_t)); |
| 492 |
memcpy(&outval, cu->cu_outbuf, sizeof(u_int32_t)); |
| 493 |
if (inval != outval) { |
| 494 |
total_time -= tv; |
| 495 |
goto send_again; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)recvlen, XDR_DECODE); |
| 504 |
ok = xdr_replymsg(&reply_xdrs, &reply_msg); |
| 505 |
|
| 506 |
if (ok) { |
| 507 |
if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) && |
| 508 |
(reply_msg.acpted_rply.ar_stat == SUCCESS)) |
| 509 |
cu->cu_error.re_status = RPC_SUCCESS; |
| 510 |
else |
| 511 |
_seterr_reply(&reply_msg, &(cu->cu_error)); |
| 512 |
|
| 513 |
if (cu->cu_error.re_status == RPC_SUCCESS) { |
| 514 |
if (! AUTH_VALIDATE(cl->cl_auth, |
| 515 |
&reply_msg.acpted_rply.ar_verf)) { |
| 516 |
cu->cu_error.re_status = RPC_AUTHERROR; |
| 517 |
cu->cu_error.re_why = AUTH_INVALIDRESP; |
| 518 |
} else if (! AUTH_UNWRAP(cl->cl_auth, &reply_xdrs, |
| 519 |
xresults, resultsp)) { |
| 520 |
if (cu->cu_error.re_status == RPC_SUCCESS) |
| 521 |
cu->cu_error.re_status = RPC_CANTDECODERES; |
| 522 |
} |
| 523 |
if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { |
| 524 |
xdrs->x_op = XDR_FREE; |
| 525 |
(void) xdr_opaque_auth(xdrs, |
| 526 |
&(reply_msg.acpted_rply.ar_verf)); |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
else if (cu->cu_error.re_status == RPC_AUTHERROR) |
| 534 |
|
| 535 |
if (nrefreshes > 0 && |
| 536 |
AUTH_REFRESH(cl->cl_auth, &reply_msg)) { |
| 537 |
nrefreshes--; |
| 538 |
goto call_again; |
| 539 |
} |
| 540 |
|
| 541 |
} |
| 542 |
else { |
| 543 |
cu->cu_error.re_status = RPC_CANTDECODERES; |
| 544 |
|
| 545 |
} |
| 546 |
out: |
| 547 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 548 |
return (cu->cu_error.re_status); |
| 549 |
} |
| 550 |
|
| 551 |
static void |
| 552 |
clnt_dg_geterr(cl, errp) |
| 553 |
CLIENT *cl; |
| 554 |
struct rpc_err *errp; |
| 555 |
{ |
| 556 |
struct cu_data *cu = (struct cu_data *)cl->cl_private; |
| 557 |
|
| 558 |
*errp = cu->cu_error; |
| 559 |
} |
| 560 |
|
| 561 |
static bool_t |
| 562 |
clnt_dg_freeres(cl, xdr_res, res_ptr) |
| 563 |
CLIENT *cl; |
| 564 |
xdrproc_t xdr_res; |
| 565 |
void *res_ptr; |
| 566 |
{ |
| 567 |
struct cu_data *cu = (struct cu_data *)cl->cl_private; |
| 568 |
XDR *xdrs = &(cu->cu_outxdrs); |
| 569 |
bool_t dummy; |
| 570 |
sigset_t mask; |
| 571 |
sigset_t newmask; |
| 572 |
|
| 573 |
sigfillset(&newmask); |
| 574 |
thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
| 575 |
mutex_lock(&clnt_fd_lock); |
| 576 |
cu->cu_fd_lock->pending++; |
| 577 |
while (cu->cu_fd_lock->active) |
| 578 |
cond_wait(&cu->cu_fd_lock->cv, &clnt_fd_lock); |
| 579 |
xdrs->x_op = XDR_FREE; |
| 580 |
dummy = (*xdr_res)(xdrs, res_ptr); |
| 581 |
cu->cu_fd_lock->pending--; |
| 582 |
thr_sigsetmask(SIG_SETMASK, &mask, NULL); |
| 583 |
cond_signal(&cu->cu_fd_lock->cv); |
| 584 |
mutex_unlock(&clnt_fd_lock); |
| 585 |
return (dummy); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
static void |
| 590 |
clnt_dg_abort(h) |
| 591 |
CLIENT *h; |
| 592 |
{ |
| 593 |
} |
| 594 |
|
| 595 |
static bool_t |
| 596 |
clnt_dg_control(cl, request, info) |
| 597 |
CLIENT *cl; |
| 598 |
u_int request; |
| 599 |
void *info; |
| 600 |
{ |
| 601 |
struct cu_data *cu = (struct cu_data *)cl->cl_private; |
| 602 |
struct netbuf *addr; |
| 603 |
sigset_t mask; |
| 604 |
sigset_t newmask; |
| 605 |
|
| 606 |
sigfillset(&newmask); |
| 607 |
thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
| 608 |
mutex_lock(&clnt_fd_lock); |
| 609 |
cu->cu_fd_lock->pending++; |
| 610 |
while (cu->cu_fd_lock->active) |
| 611 |
cond_wait(&cu->cu_fd_lock->cv, &clnt_fd_lock); |
| 612 |
cu->cu_fd_lock->active = TRUE; |
| 613 |
mutex_unlock(&clnt_fd_lock); |
| 614 |
switch (request) { |
| 615 |
case CLSET_FD_CLOSE: |
| 616 |
cu->cu_closeit = TRUE; |
| 617 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 618 |
return (TRUE); |
| 619 |
case CLSET_FD_NCLOSE: |
| 620 |
cu->cu_closeit = FALSE; |
| 621 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 622 |
return (TRUE); |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
if (info == NULL) { |
| 627 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 628 |
return (FALSE); |
| 629 |
} |
| 630 |
switch (request) { |
| 631 |
case CLSET_TIMEOUT: |
| 632 |
if (time_not_ok((struct timeval *)info)) { |
| 633 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 634 |
return (FALSE); |
| 635 |
} |
| 636 |
cu->cu_total = *(struct timeval *)info; |
| 637 |
break; |
| 638 |
case CLGET_TIMEOUT: |
| 639 |
*(struct timeval *)info = cu->cu_total; |
| 640 |
break; |
| 641 |
case CLGET_SERVER_ADDR: |
| 642 |
|
| 643 |
(void) memcpy(info, &cu->cu_raddr, (size_t)cu->cu_rlen); |
| 644 |
break; |
| 645 |
case CLSET_RETRY_TIMEOUT: |
| 646 |
if (time_not_ok((struct timeval *)info)) { |
| 647 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 648 |
return (FALSE); |
| 649 |
} |
| 650 |
cu->cu_wait = *(struct timeval *)info; |
| 651 |
break; |
| 652 |
case CLGET_RETRY_TIMEOUT: |
| 653 |
*(struct timeval *)info = cu->cu_wait; |
| 654 |
break; |
| 655 |
case CLGET_FD: |
| 656 |
*(int *)info = cu->cu_fd; |
| 657 |
break; |
| 658 |
case CLGET_SVC_ADDR: |
| 659 |
addr = (struct netbuf *)info; |
| 660 |
addr->buf = &cu->cu_raddr; |
| 661 |
addr->len = cu->cu_rlen; |
| 662 |
addr->maxlen = sizeof cu->cu_raddr; |
| 663 |
break; |
| 664 |
case CLSET_SVC_ADDR: |
| 665 |
addr = (struct netbuf *)info; |
| 666 |
if (addr->len < sizeof cu->cu_raddr) { |
| 667 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 668 |
return (FALSE); |
| 669 |
} |
| 670 |
(void) memcpy(&cu->cu_raddr, addr->buf, addr->len); |
| 671 |
cu->cu_rlen = addr->len; |
| 672 |
break; |
| 673 |
case CLGET_XID: |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
*(u_int32_t *)info = |
| 680 |
ntohl(*(u_int32_t *)(void *)cu->cu_outbuf); |
| 681 |
break; |
| 682 |
|
| 683 |
case CLSET_XID: |
| 684 |
|
| 685 |
*(u_int32_t *)(void *)cu->cu_outbuf = |
| 686 |
htonl(*(u_int32_t *)info - 1); |
| 687 |
|
| 688 |
break; |
| 689 |
|
| 690 |
case CLGET_VERS: |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
*(u_int32_t *)info = |
| 698 |
ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf + |
| 699 |
4 * BYTES_PER_XDR_UNIT)); |
| 700 |
break; |
| 701 |
|
| 702 |
case CLSET_VERS: |
| 703 |
*(u_int32_t *)(void *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT) |
| 704 |
= htonl(*(u_int32_t *)info); |
| 705 |
break; |
| 706 |
|
| 707 |
case CLGET_PROG: |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
*(u_int32_t *)info = |
| 715 |
ntohl(*(u_int32_t *)(void *)(cu->cu_outbuf + |
| 716 |
3 * BYTES_PER_XDR_UNIT)); |
| 717 |
break; |
| 718 |
|
| 719 |
case CLSET_PROG: |
| 720 |
*(u_int32_t *)(void *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT) |
| 721 |
= htonl(*(u_int32_t *)info); |
| 722 |
break; |
| 723 |
case CLSET_ASYNC: |
| 724 |
cu->cu_async = *(int *)info; |
| 725 |
break; |
| 726 |
case CLSET_CONNECT: |
| 727 |
cu->cu_connect = *(int *)info; |
| 728 |
break; |
| 729 |
default: |
| 730 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 731 |
return (FALSE); |
| 732 |
} |
| 733 |
release_fd_lock(cu->cu_fd_lock, mask); |
| 734 |
return (TRUE); |
| 735 |
} |
| 736 |
|
| 737 |
static void |
| 738 |
clnt_dg_destroy(cl) |
| 739 |
CLIENT *cl; |
| 740 |
{ |
| 741 |
struct cu_data *cu = (struct cu_data *)cl->cl_private; |
| 742 |
int cu_fd = cu->cu_fd; |
| 743 |
fd_lock_t *cu_fd_lock = cu->cu_fd_lock; |
| 744 |
sigset_t mask; |
| 745 |
sigset_t newmask; |
| 746 |
|
| 747 |
sigfillset(&newmask); |
| 748 |
thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
| 749 |
mutex_lock(&clnt_fd_lock); |
| 750 |
|
| 751 |
while (cu_fd_lock->pending > 0) { |
| 752 |
|
| 753 |
if (cu_fd_lock->active == FALSE) |
| 754 |
cond_signal(&cu_fd_lock->cv); |
| 755 |
|
| 756 |
cond_wait(&cu_fd_lock->cv, &clnt_fd_lock); |
| 757 |
} |
| 758 |
if (cu->cu_closeit) |
| 759 |
(void)close(cu_fd); |
| 760 |
XDR_DESTROY(&(cu->cu_outxdrs)); |
| 761 |
mem_free(cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz)); |
| 762 |
if (cl->cl_netid && cl->cl_netid[0]) |
| 763 |
mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); |
| 764 |
if (cl->cl_tp && cl->cl_tp[0]) |
| 765 |
mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); |
| 766 |
mem_free(cl, sizeof (CLIENT)); |
| 767 |
cond_signal(&cu_fd_lock->cv); |
| 768 |
fd_lock_destroy(cu_fd, cu_fd_lock, dg_fd_locks); |
| 769 |
mutex_unlock(&clnt_fd_lock); |
| 770 |
thr_sigsetmask(SIG_SETMASK, &mask, NULL); |
| 771 |
} |
| 772 |
|
| 773 |
static struct clnt_ops * |
| 774 |
clnt_dg_ops() |
| 775 |
{ |
| 776 |
static struct clnt_ops ops; |
| 777 |
extern mutex_t ops_lock; |
| 778 |
sigset_t mask; |
| 779 |
sigset_t newmask; |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
sigfillset(&newmask); |
| 784 |
thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
| 785 |
mutex_lock(&ops_lock); |
| 786 |
if (ops.cl_call == NULL) { |
| 787 |
ops.cl_call = clnt_dg_call; |
| 788 |
ops.cl_abort = clnt_dg_abort; |
| 789 |
ops.cl_geterr = clnt_dg_geterr; |
| 790 |
ops.cl_freeres = clnt_dg_freeres; |
| 791 |
ops.cl_destroy = clnt_dg_destroy; |
| 792 |
ops.cl_control = clnt_dg_control; |
| 793 |
} |
| 794 |
mutex_unlock(&ops_lock); |
| 795 |
thr_sigsetmask(SIG_SETMASK, &mask, NULL); |
| 796 |
return (&ops); |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
static bool_t |
| 803 |
time_not_ok(t) |
| 804 |
struct timeval *t; |
| 805 |
{ |
| 806 |
return (t->tv_sec < -1 || t->tv_sec > 100000000 || |
| 807 |
t->tv_usec < -1 || t->tv_usec > 1000000); |
| 808 |
} |
| 809 |
|