From 5a0b8b2ea7b70944bcc46e017e316a08878af6c1 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 21 May 2013 22:02:11 -0700 Subject: [PATCH] Move pipes to terminal scope. --- protocols/ssh/include/client.h | 10 ---------- protocols/ssh/include/terminal.h | 16 ++++++++++++++++ protocols/ssh/src/client.c | 14 -------------- protocols/ssh/src/guac_handlers.c | 17 ++++------------- protocols/ssh/src/ssh_client.c | 8 ++++---- protocols/ssh/src/terminal.c | 25 +++++++++++++++++++++++++ 6 files changed, 49 insertions(+), 41 deletions(-) diff --git a/protocols/ssh/include/client.h b/protocols/ssh/include/client.h index d0301204..bb51d0a6 100644 --- a/protocols/ssh/include/client.h +++ b/protocols/ssh/include/client.h @@ -79,16 +79,6 @@ typedef struct ssh_guac_client_data { */ int mouse_mask; - /** - * Pipe which will be used to provide STDOUT to the SSH client. - */ - int stdout_pipe_fd[2]; - - /** - * Pipe which will be used to provide STDIN to the SSH client. - */ - int stdin_pipe_fd[2]; - /** * The cached I-bar cursor. */ diff --git a/protocols/ssh/include/terminal.h b/protocols/ssh/include/terminal.h index 92af6eab..6dc0bc0c 100644 --- a/protocols/ssh/include/terminal.h +++ b/protocols/ssh/include/terminal.h @@ -75,6 +75,22 @@ struct guac_terminal { */ pthread_mutex_t lock; + /** + * Pipe which should be written to (and read from) to provide output to + * this terminal. Another thread should read from this pipe when writing + * data to the terminal. It would make sense for the terminal to provide + * this thread, but for simplicity, that logic is left to the guac + * message handler (to give the message handler something to block with). + */ + int stdout_pipe_fd[2]; + + /** + * Pipe which will be the source of user input. When a terminal code + * generates synthesized user input, that data will be written to + * this pipe. + */ + int stdin_pipe_fd[2]; + /** * The relative offset of the display. A positive value indicates that * many rows have been scrolled into view, zero indicates that no diff --git a/protocols/ssh/src/client.c b/protocols/ssh/src/client.c index 0bfbf088..8332049d 100644 --- a/protocols/ssh/src/client.c +++ b/protocols/ssh/src/client.c @@ -101,20 +101,6 @@ int guac_client_init(guac_client* client, int argc, char** argv) { guac_socket_flush(socket); - /* Open STDOUT pipe */ - if (pipe(client_data->stdout_pipe_fd)) { - guac_error = GUAC_STATUS_SEE_ERRNO; - guac_error_message = "Unable to open pipe for STDOUT"; - return 1; - } - - /* Open STDIN pipe */ - if (pipe(client_data->stdin_pipe_fd)) { - guac_error = GUAC_STATUS_SEE_ERRNO; - guac_error_message = "Unable to open pipe for STDIN"; - return 1; - } - /* Set basic handlers */ client->handle_messages = ssh_guac_client_handle_messages; client->clipboard_handler = ssh_guac_client_clipboard_handler; diff --git a/protocols/ssh/src/guac_handlers.c b/protocols/ssh/src/guac_handlers.c index 5a16edb0..60438cdc 100644 --- a/protocols/ssh/src/guac_handlers.c +++ b/protocols/ssh/src/guac_handlers.c @@ -61,7 +61,7 @@ int ssh_guac_client_handle_messages(guac_client* client) { char buffer[8192]; int ret_val; - int fd = client_data->stdout_pipe_fd[0]; + int fd = client_data->term->stdout_pipe_fd[0]; struct timeval timeout; fd_set fds; @@ -158,7 +158,7 @@ int ssh_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) { int length = strlen(client_data->clipboard_data); if (length) - return write(client_data->stdin_pipe_fd[1], + return write(term->stdin_pipe_fd[1], client_data->clipboard_data, length); } @@ -229,7 +229,7 @@ int ssh_guac_client_key_handler(guac_client* client, int keysym, int pressed) { guac_terminal* term = client_data->term; /* Get write end of STDIN pipe */ - int fd = client_data->stdin_pipe_fd[1]; + int fd = term->stdin_pipe_fd[1]; /* Hide mouse cursor if not already hidden */ if (client_data->current_cursor != client_data->blank_cursor) { @@ -354,18 +354,9 @@ int ssh_guac_client_free_handler(guac_client* client) { ssh_guac_client_data* guac_client_data = (ssh_guac_client_data*) client->data; - /* Close terminal output pipe */ - close(guac_client_data->stdout_pipe_fd[1]); - close(guac_client_data->stdout_pipe_fd[0]); - - /* Close user input pipe */ - close(guac_client_data->stdin_pipe_fd[1]); - close(guac_client_data->stdin_pipe_fd[0]); - - pthread_join(guac_client_data->client_thread, NULL); - /* Free terminal */ guac_terminal_free(guac_client_data->term); + pthread_join(guac_client_data->client_thread, NULL); /* Free clipboard data */ free(guac_client_data->clipboard_data); diff --git a/protocols/ssh/src/ssh_client.c b/protocols/ssh/src/ssh_client.c index eb54a34a..564ee8cf 100644 --- a/protocols/ssh/src/ssh_client.c +++ b/protocols/ssh/src/ssh_client.c @@ -81,8 +81,8 @@ static char* prompt(guac_client* client, const char* title, char* str, int size, char in_byte; /* Get STDIN and STDOUT */ - int stdin_fd = client_data->stdin_pipe_fd[0]; - int stdout_fd = client_data->stdout_pipe_fd[1]; + int stdin_fd = client_data->term->stdin_pipe_fd[0]; + int stdout_fd = client_data->term->stdout_pipe_fd[1]; /* Print title */ __write_all(stdout_fd, title, strlen(title)); @@ -137,7 +137,7 @@ void* ssh_input_thread(void* data) { char buffer[8192]; int bytes_read; - int stdin_fd = client_data->stdin_pipe_fd[0]; + int stdin_fd = client_data->term->stdin_pipe_fd[0]; /* Write all data read */ while ((bytes_read = read(stdin_fd, buffer, sizeof(buffer))) > 0) @@ -156,7 +156,7 @@ void* ssh_client_thread(void* data) { char buffer[8192]; int bytes_read = -1234; - int stdout_fd = client_data->stdout_pipe_fd[1]; + int stdout_fd = client_data->term->stdout_pipe_fd[1]; pthread_t input_thread; diff --git a/protocols/ssh/src/terminal.c b/protocols/ssh/src/terminal.c index 74e809c0..22374405 100644 --- a/protocols/ssh/src/terminal.c +++ b/protocols/ssh/src/terminal.c @@ -46,6 +46,7 @@ #include #include #include +#include #include "types.h" #include "buffer.h" @@ -95,6 +96,22 @@ guac_terminal* guac_terminal_create(guac_client* client, term->text_selected = false; + /* Open STDOUT pipe */ + if (pipe(term->stdout_pipe_fd)) { + guac_error = GUAC_STATUS_SEE_ERRNO; + guac_error_message = "Unable to open pipe for STDOUT"; + free(term); + return NULL; + } + + /* Open STDIN pipe */ + if (pipe(term->stdin_pipe_fd)) { + guac_error = GUAC_STATUS_SEE_ERRNO; + guac_error_message = "Unable to open pipe for STDIN"; + free(term); + return NULL; + } + /* Size display */ guac_terminal_display_resize(term->display, term->term_width, term->term_height); @@ -108,6 +125,14 @@ guac_terminal* guac_terminal_create(guac_client* client, void guac_terminal_free(guac_terminal* term) { + /* Close terminal output pipe */ + close(term->stdout_pipe_fd[1]); + close(term->stdout_pipe_fd[0]); + + /* Close user input pipe */ + close(term->stdin_pipe_fd[1]); + close(term->stdin_pipe_fd[0]); + /* Free display */ guac_terminal_display_free(term->display);