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