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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
|
#include "globus_i_gfork.h"
#include "version.h"
#define GFORK_CHILD_READ_ENV "GFORK_CHILD_READ_ENV"
#define GFORK_CHILD_WRITE_ENV "GFORK_CHILD_WRITE_ENV"
globus_xio_stack_t gfork_i_file_stack;
globus_xio_attr_t gfork_i_file_attr;
globus_xio_driver_t gfork_i_file_driver;
static globus_bool_t gfork_l_globals_set = GLOBUS_FALSE;
GlobusDebugDefine(GLOBUS_GFORK);
static
globus_result_t
gfork_l_get_env_fd(
char * env,
int * out_fd);
static
void
gfork_l_child_read_header_cb(
globus_xio_handle_t handle,
globus_result_t result,
globus_byte_t * buffer,
globus_size_t len,
globus_size_t nbytes,
globus_xio_data_descriptor_t data_desc,
void * user_arg);
static
void
gfork_l_child_read_close_cb(
globus_xio_handle_t xio_handle,
globus_result_t result,
void * user_arg)
{
gfork_i_lib_handle_t * handle;
globus_result_t user_res;
handle = (gfork_i_lib_handle_t *) user_arg;
user_res = globus_error_put(handle->error_obj);
handle->error_cb(handle, handle->user_arg, user_res);
globus_mutex_destroy(&handle->mutex);
globus_free(handle);
}
static
void
gfork_l_child_write_close_cb(
globus_xio_handle_t xio_handle,
globus_result_t result,
void * user_arg)
{
gfork_i_lib_handle_t * handle;
handle = (gfork_i_lib_handle_t *) user_arg;
result = globus_xio_register_close(
handle->read_xio, NULL,
gfork_l_child_read_close_cb, handle);
if(result != GLOBUS_SUCCESS)
{
gfork_l_child_read_close_cb(handle->read_xio, GLOBUS_SUCCESS, handle);
}
}
static
void
gfork_l_child_error(
gfork_i_lib_handle_t * handle,
globus_result_t in_result)
{
globus_result_t result;
switch(handle->state)
{
/* we are already doing it */
case GFORK_STATE_CLOSING:
break;
case GFORK_STATE_OPEN:
handle->error_obj = globus_error_get(in_result);
result = globus_xio_register_close(
handle->write_xio, NULL,
gfork_l_child_write_close_cb, handle);
if(result != GLOBUS_SUCCESS)
{
/* wtf ? */
}
handle->state = GFORK_STATE_CLOSING;
break;
default:
globus_assert(0 && "Invalid state");
}
}
static
void
gfork_l_child_read_body_cb(
globus_xio_handle_t xio_handle,
globus_result_t result,
globus_byte_t * buffer,
globus_size_t len,
globus_size_t nbytes,
globus_xio_data_descriptor_t data_desc,
void * user_arg)
{
gfork_i_lib_handle_t * handle;
handle = (gfork_i_lib_handle_t *) user_arg;
if(result != GLOBUS_SUCCESS)
{
goto error_incoming;
}
globus_assert(nbytes == handle->header.size);
handle->incoming_cb(
handle,
handle->user_arg,
handle->header.from_pid,
buffer,
nbytes);
result = globus_xio_register_read(
handle->read_xio,
(globus_byte_t *)&handle->header,
sizeof(gfork_i_msg_header_t),
sizeof(gfork_i_msg_header_t),
NULL,
gfork_l_child_read_header_cb,
handle);
if(result != GLOBUS_SUCCESS)
{
goto error_post;
}
return;
error_post:
error_incoming:
free(buffer);
globus_mutex_lock(&handle->mutex);
{
gfork_l_child_error(handle, result);
}
globus_mutex_unlock(&handle->mutex);
}
static
void
gfork_l_child_read_header_cb(
globus_xio_handle_t xio_handle,
globus_result_t result,
globus_byte_t * buffer,
globus_size_t len,
globus_size_t nbytes,
globus_xio_data_descriptor_t data_desc,
void * user_arg)
{
gfork_i_lib_handle_t * handle;
globus_bool_t call_close = GLOBUS_FALSE;
globus_bool_t call_open = GLOBUS_FALSE;
handle = (gfork_i_lib_handle_t *) user_arg;
globus_mutex_lock(&handle->mutex);
{
if(result != GLOBUS_SUCCESS)
{
goto error_incoming;
}
switch(handle->header.type)
{
case GLOBUS_GFORK_MSG_DATA:
if(handle->header.size <= 0)
{
/* assume a bad message, report header */
result = globus_xio_register_read(
handle->read_xio,
(globus_byte_t *)&handle->header,
sizeof(gfork_i_msg_header_t),
sizeof(gfork_i_msg_header_t),
NULL,
gfork_l_child_read_header_cb,
handle);
if(result != GLOBUS_SUCCESS)
{
goto error_post;
}
}
else
{
handle->data = globus_malloc(handle->header.size);
result = globus_xio_register_read(
handle->read_xio,
handle->data,
handle->header.size,
handle->header.size,
NULL,
gfork_l_child_read_body_cb,
handle);
if(result != GLOBUS_SUCCESS)
{
goto error_post;
}
}
break;
/* any of these we consider garbage */
case GLOBUS_GFORK_MSG_OPEN:
call_open = handle->master;
/* assume a bad message, report header */
result = globus_xio_register_read(
handle->read_xio,
(globus_byte_t *)&handle->header,
sizeof(gfork_i_msg_header_t),
sizeof(gfork_i_msg_header_t),
NULL,
gfork_l_child_read_header_cb,
handle);
if(result != GLOBUS_SUCCESS)
{
goto error_post;
}
break;
case GLOBUS_GFORK_MSG_CLOSE:
call_close = handle->master;
/* assume a bad message, report header */
result = globus_xio_register_read(
handle->read_xio,
(globus_byte_t *)&handle->header,
sizeof(gfork_i_msg_header_t),
sizeof(gfork_i_msg_header_t),
NULL,
gfork_l_child_read_header_cb,
handle);
if(result != GLOBUS_SUCCESS)
{
goto error_post;
}
break;
}
}
globus_mutex_unlock(&handle->mutex);
/* shold only happen on maste rprocess */
if(call_open && handle->open_cb)
{
handle->open_cb(handle, handle->user_arg, handle->header.from_pid);
}
else if(call_close && handle->close_cb)
{
handle->close_cb(handle, handle->user_arg, handle->header.from_pid);
}
return;
error_post:
error_incoming:
gfork_l_child_error(handle, result);
globus_mutex_unlock(&handle->mutex);
}
static
globus_result_t
globus_l_gfork_child_start(
gfork_child_handle_t * out_handle,
const char * in_env_suffix,
globus_gfork_open_func_t open_cb,
globus_gfork_closed_func_t close_cb,
globus_gfork_incoming_cb_t incoming_cb,
globus_gfork_error_func_t error_cb,
void * user_arg,
globus_bool_t master)
{
globus_result_t result;
gfork_i_lib_handle_t * handle;
char * env;
char * env_suffix;
int read_fd;
int write_fd;
handle = (gfork_i_lib_handle_t *)
globus_calloc(1, sizeof(gfork_i_lib_handle_t));
handle->state = GFORK_STATE_OPEN;
handle->open_cb = open_cb;
handle->close_cb = close_cb;
handle->error_cb = error_cb;
handle->incoming_cb = incoming_cb;
handle->user_arg = user_arg;
handle->master = master;
globus_mutex_init(&handle->mutex, NULL);
globus_fifo_init(&handle->write_q);
if(in_env_suffix == NULL)
{
env_suffix = "";
}
else
{
env_suffix = (char *) in_env_suffix;
}
env = globus_common_create_string("%s%s", GFORK_CHILD_READ_ENV, env_suffix);
result = gfork_l_get_env_fd(env, &read_fd);
globus_free(env);
if(result != GLOBUS_SUCCESS)
{
goto error_read_env;
}
env = globus_common_create_string("%s%s",GFORK_CHILD_WRITE_ENV,env_suffix);
result = gfork_l_get_env_fd(env, &write_fd);
globus_free(env);
if(result != GLOBUS_SUCCESS)
{
goto error_write_env;
}
result = gfork_i_make_xio_handle(&handle->read_xio, read_fd);
if(result != GLOBUS_SUCCESS)
{
goto error_read_convert;
}
result = gfork_i_make_xio_handle(&handle->write_xio, write_fd);
if(result != GLOBUS_SUCCESS)
{
goto error_write_convert;
}
globus_mutex_lock(&handle->mutex);
{
result = globus_xio_register_read(
handle->read_xio,
(globus_byte_t *)&handle->header,
sizeof(gfork_i_msg_header_t),
sizeof(gfork_i_msg_header_t),
NULL,
gfork_l_child_read_header_cb,
handle);
if(result != GLOBUS_SUCCESS)
{
goto error_post;
}
}
globus_mutex_unlock(&handle->mutex);
*out_handle = handle;
return GLOBUS_SUCCESS;
error_post:
gfork_l_child_error(handle, result);
globus_mutex_unlock(&handle->mutex);
error_write_convert:
globus_xio_close(handle->read_xio, NULL);
error_read_convert:
error_write_env:
error_read_env:
globus_fifo_destroy(&handle->write_q);
globus_mutex_destroy(&handle->mutex);
globus_free(handle);
return result;
}
globus_result_t
globus_gfork_child_worker_start(
gfork_child_handle_t * out_handle,
const char * in_env_suffix,
globus_gfork_closed_func_t close_cb,
globus_gfork_incoming_cb_t incoming_cb,
globus_gfork_error_func_t error_cb,
void * user_arg)
{
return globus_l_gfork_child_start(
out_handle,
in_env_suffix,
NULL,
close_cb,
incoming_cb,
error_cb,
user_arg,
GLOBUS_FALSE);
}
globus_result_t
globus_gfork_child_master_start(
gfork_child_handle_t * out_handle,
const char * in_env_suffix,
globus_gfork_open_func_t open_cb,
globus_gfork_closed_func_t close_cb,
globus_gfork_incoming_cb_t incoming_cb,
globus_gfork_error_func_t error_cb,
void * user_arg)
{
return globus_l_gfork_child_start(
out_handle,
in_env_suffix,
open_cb,
close_cb,
incoming_cb,
error_cb,
user_arg,
GLOBUS_TRUE);
}
globus_result_t
globus_gfork_child_stop(
gfork_child_handle_t in_handle)
{
gfork_i_lib_handle_t * handle;
handle = (gfork_i_lib_handle_t *) in_handle;
globus_mutex_lock(&handle->mutex);
{
gfork_l_child_error(handle, GLOBUS_SUCCESS);
}
globus_mutex_unlock(&handle->mutex);
return GLOBUS_SUCCESS;
}
static
globus_result_t
gfork_l_get_env_fd(
char * env,
int * out_fd)
{
globus_result_t res;
char * tmp_str;
int sc;
int fd;
GForkFuncName(gfork_l_get_env_fd);
tmp_str = getenv(env);
if(tmp_str == NULL)
{
res = GForkErrorStr("Env not set");
goto error_env;
}
sc = sscanf(tmp_str, "%d", &fd);
if(sc != 1)
{
res = GForkErrorStr("Env not and integer");
goto error_scan;
}
*out_fd = fd;
return GLOBUS_SUCCESS;
error_scan:
error_env:
return res;
}
globus_result_t
gfork_i_make_xio_handle(
globus_xio_handle_t * xio_handle,
int fd)
{
globus_result_t res;
globus_xio_attr_t attr;
globus_xio_handle_t handle;
res = globus_xio_attr_init(&attr);
if(res != GLOBUS_SUCCESS)
{
goto error_copy;
}
res = globus_xio_attr_cntl(attr, gfork_i_file_driver,
GLOBUS_XIO_FILE_SET_HANDLE, fd);
if(res != GLOBUS_SUCCESS)
{
goto error_attr;
}
res = globus_xio_handle_create(&handle, gfork_i_file_stack);
if(res != GLOBUS_SUCCESS)
{
goto error_create;
}
/* the way the stack is set up xio should not poll. */
res = globus_xio_open(
handle,
NULL,
attr);
if(res != GLOBUS_SUCCESS)
{
goto error_open;
}
globus_xio_attr_destroy(attr);
*xio_handle = handle;
return GLOBUS_SUCCESS;
error_open:
error_create:
error_attr:
globus_xio_attr_destroy(attr);
error_copy:
return res;
}
static
void
gfork_l_client_writev_cb(
globus_xio_handle_t xio_handle,
globus_result_t result,
globus_xio_iovec_t * iovec,
int count,
globus_size_t nbytes,
globus_xio_data_descriptor_t data_desc,
void * user_arg)
{
gfork_i_msg_t * msg;
gfork_i_lib_handle_t * handle;
msg = (gfork_i_msg_t *) user_arg;
handle = msg->lib_handle;
/* lazy reuse of XIO callback. perhaps we should define our own */
if(msg->client_cb)
{
msg->client_cb(NULL, result, &msg->iov[1],
count - 1, nbytes, data_desc, msg->user_arg);
}
globus_free(msg->iov);
globus_free(msg);
globus_mutex_lock(&handle->mutex);
{
handle->writing = GLOBUS_FALSE;
if(result != GLOBUS_SUCCESS)
{
goto error;
}
if(!globus_fifo_empty(&handle->write_q))
{
msg = (gfork_i_msg_t *) globus_fifo_dequeue(&handle->write_q);
result = globus_xio_register_writev(
handle->write_xio,
msg->iov,
msg->iovc,
msg->nbytes,
NULL,
gfork_l_client_writev_cb,
msg);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
handle->writing = GLOBUS_TRUE;
}
}
globus_mutex_unlock(&handle->mutex);
return;
error:
assert(0);
globus_mutex_unlock(&handle->mutex);
}
globus_result_t
globus_l_gfork_send(
gfork_i_lib_handle_t * handle,
uid_t pid,
globus_xio_iovec_t * iov,
int iovc,
globus_xio_iovec_callback_t cb,
void * user_arg)
{
int i;
globus_size_t nbytes;
gfork_i_msg_t * msg;
globus_result_t result = GLOBUS_SUCCESS;
GForkFuncName(globus_l_gfork_send);
if(handle->state != GFORK_STATE_OPEN)
{
return GForkErrorStr("Invalid state. Is the handle already closed?");
}
msg = (gfork_i_msg_t *) globus_calloc(1, sizeof(gfork_i_msg_t));
msg->header.from_pid = getpid();
msg->header.to_pid = pid;
msg->header.type = GLOBUS_GFORK_MSG_DATA;
msg->lib_handle = handle;
msg->user_arg = user_arg;
msg->iov = (globus_xio_iovec_t *) globus_calloc(
iovc + 1, sizeof(globus_xio_iovec_t));
msg->iov[0].iov_base = &msg->header;
msg->iov[0].iov_len = sizeof(gfork_i_msg_header_t);
nbytes = 0;
for(i = 0; i < iovc; i++)
{
msg->iov[i+1].iov_base = iov[i].iov_base;
msg->iov[i+1].iov_len = iov[i].iov_len;
nbytes += iov[i].iov_len;
}
msg->client_cb = cb;
msg->header.size = nbytes;
nbytes += msg->iov[0].iov_len;
msg->nbytes = nbytes;
msg->iovc = iovc+1;
if(!handle->writing)
{
handle->writing = GLOBUS_TRUE;
result = globus_xio_register_writev(
handle->write_xio,
msg->iov,
msg->iovc,
msg->nbytes,
NULL,
gfork_l_client_writev_cb,
msg);
}
else
{
globus_fifo_enqueue(&handle->write_q, msg);
}
return result;
}
globus_result_t
globus_gfork_broadcast(
gfork_child_handle_t in_handle,
globus_xio_iovec_t * iov,
int iovc,
globus_xio_iovec_callback_t cb,
void * user_arg)
{
globus_result_t result;
gfork_i_lib_handle_t * handle;
handle = (gfork_i_lib_handle_t *) in_handle;
globus_mutex_lock(&handle->mutex);
{
result = globus_l_gfork_send(handle, -1, iov, iovc, cb, user_arg);
}
globus_mutex_unlock(&handle->mutex);
return result;
}
globus_result_t
globus_gfork_send(
gfork_child_handle_t in_handle,
uid_t pid,
globus_xio_iovec_t * iov,
int iovc,
globus_xio_iovec_callback_t cb,
void * user_arg)
{
globus_result_t result;
gfork_i_lib_handle_t * handle;
handle = (gfork_i_lib_handle_t *) in_handle;
globus_mutex_lock(&handle->mutex);
{
result = globus_l_gfork_send(handle, pid, iov, iovc, cb, user_arg);
}
globus_mutex_unlock(&handle->mutex);
return result;
}
static
int
gfork_l_activate()
{
int rc;
globus_result_t res;
rc = globus_module_activate(GLOBUS_XIO_MODULE);
if(rc != 0)
{
goto error_activate;
}
if(!gfork_l_globals_set)
{
GlobusDebugInit(GLOBUS_GFORK,
ERROR WARNING TRACE INTERNAL_TRACE INFO STATE INFO_VERBOSE);
gfork_i_state_init();
res = globus_xio_stack_init(&gfork_i_file_stack, NULL);
if(res != GLOBUS_SUCCESS)
{
goto error_file_stack;
}
res = globus_xio_driver_load("file", &gfork_i_file_driver);
if(res != GLOBUS_SUCCESS)
{
goto error_file_driver;
}
res = globus_xio_stack_push_driver(
gfork_i_file_stack, gfork_i_file_driver);
if(res != GLOBUS_SUCCESS)
{
goto error_file_push;
}
globus_xio_attr_init(&gfork_i_file_attr);
}
gfork_l_globals_set = GLOBUS_TRUE;
return 0;
error_file_push:
globus_xio_driver_unload(gfork_i_file_driver);
error_file_driver:
globus_xio_stack_destroy(gfork_i_file_stack);
error_file_stack:
globus_module_deactivate(GLOBUS_XIO_MODULE);
error_activate:
return 1;
}
static int
gfork_l_parent_activate()
{
int rc;
rc = gfork_l_activate();
if(rc != 0)
{
goto error_activate;
}
return 0;
error_activate:
return 1;
}
static int
gfork_l_child_activate()
{
int rc;
rc = gfork_l_activate();
if(rc != 0)
{
goto error_activate;
}
return 0;
error_activate:
return 1;
}
static int
gfork_l_deactivate()
{
gfork_l_globals_set = GLOBUS_FALSE;
globus_module_deactivate(GLOBUS_XIO_MODULE);
return 0;
}
globus_module_descriptor_t globus_i_gfork_parent_module =
{
"globus_gfork",
gfork_l_parent_activate,
gfork_l_deactivate,
NULL,
NULL,
&local_version
};
globus_module_descriptor_t globus_i_gfork_child_module =
{
"globus_gfork",
gfork_l_child_activate,
gfork_l_deactivate,
NULL,
NULL,
&local_version
};
|