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
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#if defined(DARWIN)
# include <termios.h>
# include <sys/ttycom.h>
# include <sys/ioctl.h>
#elif defined(HP11) || defined(HP1164)
# include <termios.h>
#elif defined(INTERIX)
# include <termios.h>
# include <sys/ioctl.h>
#else
# include <termio.h>
#endif
#include "sgermon.h"
#include "sge_io.h"
#include "sge_utility.h"
#include "sge_pty.h"
#include "sge_ijs_comm.h"
#include "sge_ijs_threads.h"
#include "sge_client_ijs.h"
/* global variables */
char *g_hostname = NULL;
COMMUNICATION_HANDLE *g_comm_handle = NULL;
/* module variables */
static int g_exit_status = 0;
static int g_nostdin = 0;
static int g_noshell = 0;
/*
* static volatile sig_atomic_t received_window_change_signal = 1;
* Flag to indicate that we have received a window change signal which has
* not yet been processed. This will cause a message indicating the new
* window size to be sent to the server a little later. This is volatile
* because this is updated in a signal handler.
*/
static volatile sig_atomic_t received_window_change_signal = 1;
static volatile sig_atomic_t received_broken_pipe_signal = 0;
static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
volatile sig_atomic_t received_signal = 0;
/****** window_change_handler **************************************************
* NAME
* window_change_handler() -- handler for the window changed signal
*
* SYNOPSIS
* static void window_change_handler(int sig)
*
* FUNCTION
* Signal handler for the window change signal (SIGWINCH). This just sets a
* flag indicating that the window has changed.
*
* INPUTS
* int sig - number of the received signal
*
* RESULT
* void - no result
*
* NOTES
* MT-NOTE: window_change_handler() is not MT safe, because it uses
* received_window_change_signal
*
* SEE ALSO
*******************************************************************************/
static void window_change_handler(int sig)
{
/* Do not use DPRINTF in a signal handler! */
received_window_change_signal = 1;
signal(SIGWINCH, window_change_handler);
}
/****** broken_pipe_handler ****************************************************
* NAME
* broken_pipe_handler() -- handler for the broken pipe signal
*
* SYNOPSIS
* static void broken_pipe_handler(int sig)
*
* FUNCTION
* Handler for the SIGPIPE signal.
*
* INPUTS
* int sig - number of the received signal
*
* RESULT
* void - no result
*
* NOTES
*
* SEE ALSO
*******************************************************************************/
static void broken_pipe_handler(int sig)
{
received_broken_pipe_signal = 1;
signal(SIGPIPE, broken_pipe_handler);
}
/****** signal_handler() *******************************************************
* NAME
* signal_handler() -- handler for quit signals
*
* SYNOPSIS
* static void signal_handler(int sig)
*
* FUNCTION
* Handler for all signals that quit the program. These signals are trapped
* in order to restore the terminal modes.
*
* INPUTS
* int sig - number of the received signal
*
* RESULT
* void - no result
*
* NOTES
*
* SEE ALSO
*******************************************************************************/
void signal_handler(int sig)
{
received_signal = sig;
quit_pending = 1;
}
/****** set_signal_handlers() **************************************************
* NAME
* set_signal_handlers() -- set all signal handlers
*
* SYNOPSIS
* static void set_signal_handlers(void)
*
* FUNCTION
* Sets all signal handlers. Doesn't overwrite SIG_IGN and therefore
* matches the behaviour of rsh.
*
* RESULT
* void - no result
*
* NOTES
*
* SEE ALSO
*******************************************************************************/
void set_signal_handlers(void)
{
struct sigaction old_handler, new_handler;
/* Is SIGHUP necessary?
* Yes: termio(7I) says:
* "When a modem disconnect is detected, a SIGHUP signal is sent
* to the terminal's controlling process.
* Unless other arrangements have been made, these signals
* cause the process to terminate. If SIGHUP is ignored or
* caught, any subsequent read returns with an end-of-file
* indication until the terminal is closed."
*/
sigaction(SIGHUP, NULL, &old_handler);
if (old_handler.sa_handler != SIG_IGN) {
new_handler.sa_handler = signal_handler;
sigaddset(&new_handler.sa_mask, SIGHUP);
new_handler.sa_flags = SA_RESTART;
sigaction(SIGHUP, &new_handler, NULL);
}
sigaction(SIGINT, NULL, &old_handler);
if (old_handler.sa_handler != SIG_IGN) {
new_handler.sa_handler = signal_handler;
sigaddset(&new_handler.sa_mask, SIGINT);
new_handler.sa_flags = SA_RESTART;
sigaction(SIGINT, &new_handler, NULL);
}
sigaction(SIGQUIT, NULL, &old_handler);
if (old_handler.sa_handler != SIG_IGN) {
new_handler.sa_handler = signal_handler;
sigaddset(&new_handler.sa_mask, SIGQUIT);
new_handler.sa_flags = SA_RESTART;
sigaction(SIGQUIT, &new_handler, NULL);
}
sigaction(SIGTERM, NULL, &old_handler);
if (old_handler.sa_handler != SIG_IGN) {
new_handler.sa_handler = signal_handler;
sigaddset(&new_handler.sa_mask, SIGTERM);
new_handler.sa_flags = SA_RESTART;
sigaction(SIGTERM, &new_handler, NULL);
}
new_handler.sa_handler = window_change_handler;
sigaddset(&new_handler.sa_mask, SIGWINCH);
new_handler.sa_flags = SA_RESTART;
sigaction(SIGWINCH, &new_handler, NULL);
new_handler.sa_handler = broken_pipe_handler;
sigaddset(&new_handler.sa_mask, SIGPIPE);
new_handler.sa_flags = SA_RESTART;
sigaction(SIGPIPE, &new_handler, NULL);
}
/****** client_check_window_change() *******************************************
* NAME
* client_check_window_change() -- check if window size was change and
* submit changes to pty
*
* SYNOPSIS
* static void client_check_window_change(COMMUNICATION_HANDLE *handle)
*
* FUNCTION
* Checks if the window size of the terminal window was changed.
* If the size was changed, submits the new window size to the
* pty.
* The actual change is detected by a signal (on Unix), this function
* just checks the according flag.
*
* INPUTS
* COMMUNICATION_HANDLE *handle - pointer to the commlib handle
*
* RESULT
* void - no result
*
* NOTES
* MT-NOTE: client_check_window_change() is MT-safe (see comment in code)
*
* SEE ALSO
* window_change_handler()
*******************************************************************************/
static void client_check_window_change(COMMUNICATION_HANDLE *handle)
{
struct winsize ws;
char buf[200];
dstring err_msg = DSTRING_INIT;
DENTER(TOP_LAYER, "client_check_window_change");
if (received_window_change_signal) {
/*
* here we can have a race condition between the two working threads,
* but it doesn't matter - in the worst case, the new window size gets
* submitted two times.
*/
received_window_change_signal = 0;
if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) >= 0) {
DPRINTF(("sendig WINDOW_SIZE_CTRL_MSG with new window size: "
"%d, %d, %d, %d to shepherd\n",
ws.ws_row, ws.ws_col, ws.ws_xpixel, ws.ws_ypixel));
sprintf(buf, "WS %d %d %d %d",
ws.ws_row, ws.ws_col, ws.ws_xpixel, ws.ws_ypixel);
comm_write_message(handle, g_hostname, COMM_CLIENT, 1,
(unsigned char*)buf, strlen(buf),
WINDOW_SIZE_CTRL_MSG, &err_msg);
} else {
DPRINTF(("client_check_windows_change: ioctl() failed! "
"sending dummy WINDOW_SIZE_CTRL_MSG to fullfill protocol.\n"));
sprintf(buf, "WS 60 80 480 640");
comm_write_message(handle, g_hostname, COMM_CLIENT, 1,
(unsigned char*)buf, strlen(buf), WINDOW_SIZE_CTRL_MSG, &err_msg);
}
}
sge_dstring_free(&err_msg);
DEXIT;
}
/****** tty_to_commlib() *******************************************************
* NAME
* tty_to_commlib() -- tty_to_commlib thread entry point and main loop
*
* SYNOPSIS
* void* tty_to_commlib(void *t_conf)
*
* FUNCTION
* Entry point and main loop of the tty_to_commlib thread.
* Reads data from the tty and writes it to the commlib.
*
* INPUTS
* void *t_conf - pointer to cl_thread_settings_t struct of the thread
*
* RESULT
* void* - always NULL
*
* NOTES
* MT-NOTE: tty_to_commlib is MT-safe ?
*
* SEE ALSO
*******************************************************************************/
void* tty_to_commlib(void *t_conf)
{
char *pbuf;
fd_set read_fds;
struct timeval timeout;
dstring err_msg = DSTRING_INIT;
int ret, nread, do_exit = 0;
DENTER(TOP_LAYER, "tty_to_commlib");
thread_func_startup(t_conf);
thread_setcancelstate(1);
/*
* allocate working buffer
*/
pbuf = (char*)malloc(BUFSIZE);
if (pbuf == NULL) {
DPRINTF(("tty_to_commlib can't allocate working buffer: %s (%d)\n",
strerror(errno), errno));
do_exit = 1;
}
while (do_exit == 0) {
FD_ZERO(&read_fds);
if (g_nostdin == 0) {
/* wait for input on tty */
FD_SET(STDIN_FILENO, &read_fds);
}
timeout.tv_sec = 1;
timeout.tv_usec = 0;
DPRINTF(("tty_to_commlib: Waiting in select() for data\n"));
ret = select(STDIN_FILENO+1, &read_fds, NULL, NULL, &timeout);
thread_testcancel(t_conf);
client_check_window_change(g_comm_handle);
if (received_signal == SIGHUP ||
received_signal == SIGINT ||
received_signal == SIGQUIT ||
received_signal == SIGTERM) {
/* If we receive one of these signals, we must terminate */
do_exit = 1;
continue;
}
if (ret > 0) {
if (g_nostdin == 1) {
/* We should never get here if STDIN is closed */
DPRINTF(("tty_to_commlib: STDIN ready to read while it should be closed!!!\n"));
}
DPRINTF(("tty_to_commlib: trying to read() from stdin\n"));
nread = read(STDIN_FILENO, pbuf, BUFSIZE-1);
DPRINTF(("tty_to_commlib: nread = %d\n", nread));
if (nread < 0 && (errno == EINTR || errno == EAGAIN)) {
DPRINTF(("tty_to_commlib: EINTR or EAGAIN\n"));
/* do nothing */
} else if (nread <= 0) {
do_exit = 1;
} else {
DPRINTF(("tty_to_commlib: writing to commlib: %d bytes\n", nread));
if (comm_write_message(g_comm_handle, g_hostname,
COMM_CLIENT, 1, (unsigned char*)pbuf,
(unsigned long)nread, STDIN_DATA_MSG, &err_msg) != nread) {
DPRINTF(("tty_to_commlib: couldn't write all data\n"));
} else {
DPRINTF(("tty_to_commlib: data successfully written\n"));
}
comm_flush_write_messages(g_comm_handle, &err_msg);
}
} else {
/*
* We got either a select timeout or a select error. In both cases,
* it's a good chance to check if our client is still alive.
*/
DPRINTF(("tty_to_commlib: Checking if client is still alive\n"));
if (comm_get_connection_count(g_comm_handle, &err_msg) == 0) {
DPRINTF(("tty_to_commlib: Client is not alive! -> exiting.\n"));
do_exit = 1;
} else {
DPRINTF(("tty_to_commlib: Client is still alive\n"));
}
}
} /* while (do_exit == 0) */
/* clean up */
FREE(pbuf);
thread_func_cleanup(t_conf);
sge_dstring_free(&err_msg);
DPRINTF(("tty_to_commlib: exiting tty_to_commlib thread!\n"));
DEXIT;
return NULL;
}
/****** commlib_to_tty() *******************************************************
* NAME
* commlib_to_tty() -- commlib_to_tty thread entry point and main loop
*
* SYNOPSIS
* void* commlib_to_tty(void *t_conf)
*
* FUNCTION
* Entry point and main loop of the commlib_to_tty thread.
* Reads data from the commlib and writes it to the tty.
*
* INPUTS
* void *t_conf - pointer to cl_thread_settings_t struct of the thread
*
* RESULT
* void* - always NULL
*
* NOTES
* MT-NOTE: commlib_to_tty is MT-safe ?
*
* SEE ALSO
*******************************************************************************/
void* commlib_to_tty(void *t_conf)
{
recv_message_t recv_mess;
dstring err_msg = DSTRING_INIT;
int ret = 0, do_exit = 0;
DENTER(TOP_LAYER, "commlib_to_tty");
thread_func_startup(t_conf);
thread_setcancelstate(1);
while (do_exit == 0) {
/*
* wait blocking for a message from commlib
*/
recv_mess.cl_message = NULL;
recv_mess.data = NULL;
DPRINTF(("commlib_to_tty: recv_message()\n"));
ret = comm_recv_message(g_comm_handle, CL_TRUE, &recv_mess, &err_msg);
if (ret != COMM_RETVAL_OK) {
/* check if we are still connected to anybody. */
/* if not - exit. */
DPRINTF(("commlib_to_tty: error receiving message: %s\n",
sge_dstring_get_string(&err_msg)));
if (comm_get_connection_count(g_comm_handle, &err_msg) == 0) {
DPRINTF(("commlib_to_tty: no endpoint found\n"));
do_exit = 1;
continue;
}
}
DPRINTF(("commlib_to_tty: received a message\n"));
thread_testcancel(t_conf);
client_check_window_change(g_comm_handle);
if (received_signal == SIGHUP ||
received_signal == SIGINT ||
received_signal == SIGQUIT ||
received_signal == SIGTERM) {
/* If we receive one of these signals, we must terminate */
DPRINTF(("commlib_to_tty: shutting down because of signal %d\n",
received_signal));
do_exit = 1;
continue;
}
DPRINTF(("'parsing' message\n"));
/*
* 'parse' message
* A 1 byte prefix tells us what kind of message it is.
* See sge_ijs_comm.h for message types.
*/
if (recv_mess.cl_message != NULL) {
char buf[100];
switch (recv_mess.type) {
case STDOUT_DATA_MSG:
/* copy recv_mess.data to buf to append '\0' */
memcpy(buf, recv_mess.data, MIN(99, recv_mess.cl_message->message_length - 1));
buf[MIN(99, recv_mess.cl_message->message_length - 1)] = 0;
DPRINTF(("commlib_to_tty: received stdout message, writing to tty.\n"));
DPRINTF(("commlib_to_tty: message is: %s\n", buf));
/* TODO: If it's not possible to write all data to the tty, retry blocking
* until all data was written. The commlib must block then, too.
*/
if (sge_writenbytes(STDOUT_FILENO, recv_mess.data,
(int)(recv_mess.cl_message->message_length-1))
!= (int)(recv_mess.cl_message->message_length-1)) {
DPRINTF(("commlib_to_tty: sge_writenbytes() error\n"));
}
break;
case STDERR_DATA_MSG:
DPRINTF(("commlib_to_tty: received stderr message, writing to tty.\n"));
/* TODO: If it's not possible to write all data to the tty, retry blocking
* until all data was written. The commlib must block then, too.
*/
if (sge_writenbytes(STDERR_FILENO, recv_mess.data,
(int)(recv_mess.cl_message->message_length-1))
!= (int)(recv_mess.cl_message->message_length-1)) {
DPRINTF(("commlib_to_tty: sge_writenbytes() error\n"));
}
break;
case WINDOW_SIZE_CTRL_MSG:
/* control message */
/* we don't expect a control message */
DPRINTF(("commlib_to_tty: received window size message! "
"This was unexpected!\n"));
break;
case REGISTER_CTRL_MSG:
/* control message */
/* a client registered with us. With the next loop, the
* cl_commlib_trigger function will send the WINDOW_SIZE_CTRL_MSG
* (and perhaps some data messages), which is already in the
* send_messages list of the connection, to the client.
*/
DPRINTF(("commlib_to_tty: received register message!\n"));
/* Send the settings in response */
sprintf(buf, "noshell = %d", g_noshell);
ret = (int)comm_write_message(g_comm_handle, g_hostname, COMM_CLIENT, 1,
(unsigned char*)buf, strlen(buf)+1, SETTINGS_CTRL_MSG, &err_msg);
DPRINTF(("commlib_to_tty: sent SETTINGS_CTRL_MSG, ret = %d\n", ret));
break;
case UNREGISTER_CTRL_MSG:
/* control message */
/* the client wants to quit, as this is the last message the client
* sends, we can be sure to have received all messages from the
* client. We answer with a UNREGISTER_RESPONSE_CTRL_MSG so
* the client knows that it can quit now. We can quit, also.
*/
DPRINTF(("commlib_to_tty: received unregister message!\n"));
DPRINTF(("commlib_to_tty: writing UNREGISTER_RESPONSE_CTRL_MSG\n"));
/* copy recv_mess.data to buf to append '\0' */
memcpy(buf, recv_mess.data, MIN(99, recv_mess.cl_message->message_length - 1));
buf[MIN(99, recv_mess.cl_message->message_length - 1)] = 0;
sscanf(buf, "%d", &g_exit_status);
comm_write_message(g_comm_handle, g_hostname, COMM_CLIENT, 1,
(unsigned char*)" ", 1, UNREGISTER_RESPONSE_CTRL_MSG, &err_msg);
DPRINTF(("commlib_to_tty: received exit_status from shepherd: %d\n",
g_exit_status));
do_exit = 1;
break;
}
}
comm_free_message(&recv_mess, &err_msg);
}
thread_func_cleanup(t_conf);
DPRINTF(("commlib_to_tty: exiting commlib_to_tty thread!\n"));
sge_dstring_free(&err_msg);
DEXIT;
return NULL;
}
/****** do_server_loop() *******************************************************
* NAME
* do_server_loop() -- The servers main loop
*
* SYNOPSIS
* int do_server_loop(u_long32 job_id, int nostdin, int noshell,
* int is_rsh, int is_qlogin, int force_pty,
* int *p_exit_status)
*
* FUNCTION
* The main loop of the commlib server, handling the data transfer from
* and to the client.
*
* INPUTS
* u_long32 job_id - SGE job id of this job
* int nostdin - The "-nostdin" switch
* int noshell - The "-noshell" switch
* int is_rsh - Is it a qrsh with commandline?
* int is_qlogin - Is it a qlogin or qrsh without commandline?
* int force_pty - The user forced use of pty by the "-pty yes" switch
* int *p_exit_status -
*
* RESULT
* int - 0: Ok.
* 1: An error occured.
*
* NOTES
* MT-NOTE: do_server_loop is not MT-safe
*
* SEE ALSO
* do_client_loop()
*******************************************************************************/
int do_server_loop(u_long32 job_id, int nostdin, int noshell,
int is_rsh, int is_qlogin, int force_pty,
int *p_exit_status)
{
bool terminal_is_in_raw_mode = false;
int ret = 0;
dstring err_msg = DSTRING_INIT;
THREAD_HANDLE *pthread_tty_to_commlib = NULL;
THREAD_HANDLE *pthread_commlib_to_tty = NULL;
THREAD_LIB_HANDLE *thread_lib_handle = NULL;
cl_raw_list_t *cl_com_log_list = NULL;
DENTER(TOP_LAYER, "do_server_loop");
if (p_exit_status == NULL) {
return 1;
}
cl_com_log_list = cl_com_get_log_list();
g_nostdin = nostdin;
g_noshell = noshell;
/*
* qrsh without command and qlogin both have is_rsh == 0 and is_qlogin == 1
* qrsh with command and qsh don't need to set terminal mode.
* If the user requested a pty we also have to set terminal mode.
*/
if ((force_pty == 2 && is_rsh == 0 && is_qlogin == 1) || force_pty == 1) {
/*
* Set this terminal to raw mode, just output everything, don't interpreti
* it. Let the pty on the client side interpret the characters.
*/
ret = terminal_enter_raw_mode();
if (ret != 0) {
fprintf(stderr, "Can't set terminal mode. Error reason: %s (%d)\n",
strerror(ret), ret);
return 1;
} else {
terminal_is_in_raw_mode = true;
}
}
/*
* Setup thread list and create two worker threads
*/
DPRINTF(("creating worker thread\n"));
thread_init_lib(&thread_lib_handle);
ret = create_thread(thread_lib_handle, &pthread_tty_to_commlib, cl_com_log_list,
"tty_to_commlib thread", 1, tty_to_commlib);
if (ret != CL_RETVAL_OK) {
DPRINTF(("can't create tty_to_commlib thread, return value = %d\n",
ret));
goto cleanup;
}
ret = create_thread(thread_lib_handle, &pthread_commlib_to_tty, cl_com_log_list,
"commlib_to_tty thread", 1, commlib_to_tty);
if (ret != CL_RETVAL_OK) {
DPRINTF(("can't create commlib_to_tty thread, return value = %d\n",
ret));
goto cleanup;
}
/*
* From here on, the two worker threads are doing all the work.
* This main thread is just waiting until the client closes the
* connection to us, which causes the commlib_to_tty thread to
* exit. Then it closes the tty_to_commlib thread, too, and
* cleans up everything.
*/
DPRINTF(("waiting for end of commlib_to_tty thread\n"));
thread_join(pthread_commlib_to_tty);
DPRINTF(("shutting down tty_to_commlib thread\n"));
thread_shutdown(pthread_tty_to_commlib);
/*
* TODO: Why doesn't this work on some hosts (e.g. oin)
* Is it necessary at all?
DPRINTF(("wait until there is no client connected any more\n"));
comm_wait_for_no_connection(g_comm_handle, COMM_CLIENT, 10, &err_msg);
*/
DPRINTF(("shut down the connection from our side\n"));
comm_shutdown_connection(g_comm_handle, COMM_CLIENT, &err_msg);
g_comm_handle = NULL;
/*
* Close stdin to awake the tty_to_commlib-thread from the select() call.
* thread_shutdown() doesn't work on all architectures.
*/
close(STDIN_FILENO);
DPRINTF(("waiting for end of tty_to_commlib thread\n"));
thread_join(pthread_tty_to_commlib);
cleanup:
/*
* Set our terminal back to 'unraw' mode. Should be done automatically
* by OS on process end, but we want to be sure.
*/
sge_dstring_free(&err_msg);
if (terminal_is_in_raw_mode == true) {
ret = terminal_leave_raw_mode();
DPRINTF(("terminal_leave_raw_mode() returned %d (%s)\n", ret, strerror(ret)));
terminal_is_in_raw_mode = false;
}
*p_exit_status = g_exit_status;
thread_cleanup_lib(&thread_lib_handle);
DRETURN(0);
}
|