Merge pull request #104 from glyptodon/text-recording

GUAC-1452: Implement text recording of terminal sessions.
This commit is contained in:
James Muehlner 2016-01-27 13:23:26 -08:00
commit 897ee16e91
7 changed files with 595 additions and 3 deletions

View File

@ -58,6 +58,9 @@ const char* GUAC_CLIENT_ARGS[] = {
#endif
"color-scheme",
"command",
"typescript-path",
"typescript-name",
"create-typescript-path",
NULL
};
@ -129,6 +132,25 @@ enum __SSH_ARGS_IDX {
*/
IDX_COMMAND,
/**
* The full absolute path to the directory in which typescripts should be
* written.
*/
IDX_TYPESCRIPT_PATH,
/**
* The name that should be given to typescripts which are written in the
* given path. Each typescript will consist of two files: "NAME" and
* "NAME.timing".
*/
IDX_TYPESCRIPT_NAME,
/**
* Whether the specified typescript path should automatically be created
* if it does not yet exist.
*/
IDX_CREATE_TYPESCRIPT_PATH,
SSH_ARGS_COUNT
};
@ -202,6 +224,25 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
return -1;
}
/* Set up typescript, if requested */
const char* typescript_path = argv[IDX_TYPESCRIPT_PATH];
if (typescript_path[0] != 0) {
/* Default to "typescript" if no name provided */
const char* typescript_name = argv[IDX_TYPESCRIPT_NAME];
if (typescript_name[0] == 0)
typescript_name = "typescript";
/* Parse path creation flag */
int create_path =
strcmp(argv[IDX_CREATE_TYPESCRIPT_PATH], "true") == 0;
/* Create typescript */
guac_terminal_create_typescript(client_data->term, typescript_path,
typescript_name, create_path);
}
/* Ensure main socket is threadsafe */
guac_socket_require_threadsafe(socket);

View File

@ -54,6 +54,9 @@ const char* GUAC_CLIENT_ARGS[] = {
"font-name",
"font-size",
"color-scheme",
"typescript-path",
"typescript-name",
"create-typescript-path",
NULL
};
@ -109,6 +112,25 @@ enum __TELNET_ARGS_IDX {
*/
IDX_COLOR_SCHEME,
/**
* The full absolute path to the directory in which typescripts should be
* written.
*/
IDX_TYPESCRIPT_PATH,
/**
* The name that should be given to typescripts which are written in the
* given path. Each typescript will consist of two files: "NAME" and
* "NAME.timing".
*/
IDX_TYPESCRIPT_NAME,
/**
* Whether the specified typescript path should automatically be created
* if it does not yet exist.
*/
IDX_CREATE_TYPESCRIPT_PATH,
TELNET_ARGS_COUNT
};
@ -218,6 +240,25 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
return -1;
}
/* Set up typescript, if requested */
const char* typescript_path = argv[IDX_TYPESCRIPT_PATH];
if (typescript_path[0] != 0) {
/* Default to "typescript" if no name provided */
const char* typescript_name = argv[IDX_TYPESCRIPT_NAME];
if (typescript_name[0] == 0)
typescript_name = "typescript";
/* Parse path creation flag */
int create_path =
strcmp(argv[IDX_CREATE_TYPESCRIPT_PATH], "true") == 0;
/* Create typescript */
guac_terminal_create_typescript(client_data->term, typescript_path,
typescript_name, create_path);
}
/* Send initial name */
guac_protocol_send_name(socket, client_data->hostname);

View File

@ -38,7 +38,8 @@ noinst_HEADERS = \
scrollbar.h \
terminal.h \
terminal_handlers.h \
types.h
types.h \
typescript.h
libguac_terminal_la_SOURCES = \
blank.c \
@ -52,7 +53,8 @@ libguac_terminal_la_SOURCES = \
pointer.c \
scrollbar.c \
terminal.c \
terminal_handlers.c
terminal_handlers.c \
typescript.c
libguac_terminal_la_CFLAGS = \
-Werror -Wall -pedantic \

View File

@ -35,7 +35,9 @@
#include "terminal.h"
#include "terminal_handlers.h"
#include "types.h"
#include "typescript.h"
#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
@ -329,6 +331,9 @@ guac_terminal* guac_terminal_create(guac_client* client,
/* Init pipe stream (output to display by default) */
term->pipe_stream = NULL;
/* No typescript by default */
term->typescript = NULL;
/* Init terminal lock */
pthread_mutex_init(&(term->lock), NULL);
@ -375,6 +380,9 @@ void guac_terminal_free(guac_terminal* term) {
/* Close and flush any open pipe stream */
guac_terminal_pipe_stream_close(term);
/* Close and flush any active typescript */
guac_terminal_typescript_free(term->typescript);
/* Close terminal output pipe */
close(term->stdout_pipe_fd[1]);
close(term->stdout_pipe_fd[0]);
@ -665,8 +673,18 @@ void guac_terminal_commit_cursor(guac_terminal* term) {
int guac_terminal_write(guac_terminal* term, const char* c, int size) {
while (size > 0) {
term->char_handler(term, *(c++));
/* Read and advance to next character */
char current = *(c++);
size--;
/* Write character to typescript, if any */
if (term->typescript != NULL)
guac_terminal_typescript_write(term->typescript, current);
/* Handle character and its meaning */
term->char_handler(term, current);
}
return 0;
@ -1342,9 +1360,16 @@ int guac_terminal_resize(guac_terminal* terminal, int width, int height) {
}
void guac_terminal_flush(guac_terminal* terminal) {
/* Flush typescript if in use */
if (terminal->typescript != NULL)
guac_terminal_typescript_flush(terminal->typescript);
/* Flush display state */
guac_terminal_commit_cursor(terminal);
guac_terminal_display_flush(terminal->display);
guac_terminal_scrollbar_flush(terminal->scrollbar);
}
void guac_terminal_lock(guac_terminal* terminal) {
@ -1811,3 +1836,28 @@ void guac_terminal_pipe_stream_close(guac_terminal* term) {
}
int guac_terminal_create_typescript(guac_terminal* term, const char* path,
const char* name, int create_path) {
/* Create typescript */
term->typescript = guac_terminal_typescript_alloc(path, name, create_path);
/* Log failure */
if (term->typescript == NULL) {
guac_client_log(term->client, GUAC_LOG_ERROR,
"Creation of typescript failed: %s", strerror(errno));
return 1;
}
/* If typescript was successfully created, log filenames */
guac_client_log(term->client, GUAC_LOG_INFO,
"Typescript of terminal session will be saved to \"%s\". "
"Timing file is \"%s\".",
term->typescript->data_filename,
term->typescript->timing_filename);
/* Typescript creation succeeded */
return 0;
}

View File

@ -32,6 +32,7 @@
#include "guac_clipboard.h"
#include "scrollbar.h"
#include "types.h"
#include "typescript.h"
#include <pthread.h>
#include <stdbool.h>
@ -169,6 +170,12 @@ struct guac_terminal {
*/
int pipe_buffer_length;
/**
* The currently-active typescript recording all terminal output, or NULL
* if no typescript is being used for the terminal session.
*/
guac_terminal_typescript* typescript;
/**
* Graphical representation of the current scroll state.
*/
@ -729,5 +736,37 @@ void guac_terminal_pipe_stream_flush(guac_terminal* term);
*/
void guac_terminal_pipe_stream_close(guac_terminal* term);
/**
* Requests that the terminal write all output to a new pair of typescript
* files within the given path and using the given base name. Terminal output
* will be written to these new files, along with timing information. If the
* create_path flag is non-zero, the given path will be created if it does not
* yet exist. If creation of the typescript files or path fails, error messages
* will automatically be logged, and no typescript will be written. The
* typescript will automatically be closed once the terminal is freed.
*
* @param term
* The terminal whose output should be written to a typescript.
*
* @param path
* The full absolute path to a directory in which the typescript files
* should be created.
*
* @param name
* The base name to use for the typescript files created within the
* specified path.
*
* @param create_path
* Zero if the specified path MUST exist for typescript files to be
* written, or non-zero if the path should be created if it does not yet
* exist.
*
* @return
* Zero if the typescript files have been successfully created and a
* typescript will be written, non-zero otherwise.
*/
int guac_terminal_create_typescript(guac_terminal* term, const char* path,
const char* name, int create_path);
#endif

232
src/terminal/typescript.c Normal file
View File

@ -0,0 +1,232 @@
/*
* Copyright (C) 2016 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "config.h"
#include "guac_io.h"
#include "typescript.h"
#include <guacamole/timestamp.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/**
* Attempts to open a new typescript data file within the given path and having
* the given name. If such a file already exists, sequential numeric suffixes
* (.1, .2, .3, etc.) are appended until a filename is found which does not
* exist (or until the maximum number of numeric suffixes has been tried). If
* the file absolutely cannot be opened due to an error, -1 is returned and
* errno is set appropriately.
*
* @param path
* The full path to the directory in which the data file should be created.
*
* @param name
* The name of the data file which should be crated within the given path.
*
* @param basename
* A buffer in which the path, a path separator, the filename, any
* necessary suffix, and a NULL terminator will be stored. If insufficient
* space is available, -1 will be returned, and errno will be set to
* ENAMETOOLONG.
*
* @param basename_size
* The number of bytes available within the provided basename buffer.
*
* @return
* The file descriptor of the open data file if open succeeded, or -1 on
* failure.
*/
static int guac_terminal_typescript_open_data_file(const char* path,
const char* name, char* basename, int basename_size) {
int i;
/* Concatenate path and name (separated by a single slash) */
int basename_length = snprintf(basename,
basename_size - GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH,
"%s/%s", path, name);
/* Abort if maximum length reached */
if (basename_length ==
basename_size - GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH) {
errno = ENAMETOOLONG;
return -1;
}
/* Attempt to open typescript data file */
int data_fd = open(basename,
O_CREAT | O_EXCL | O_WRONLY,
S_IRUSR | S_IWUSR);
/* Continuously retry with alternate names on failure */
if (data_fd == -1) {
/* Prepare basename for additional suffix */
basename[basename_length] = '.';
char* suffix = &(basename[basename_length + 1]);
/* Continue retrying alternative suffixes if file already exists */
for (i = 1; data_fd == -1 && errno == EEXIST
&& i <= GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX; i++) {
/* Append new suffix */
sprintf(suffix, "%i", i);
/* Retry with newly-suffixed filename */
data_fd = open(basename,
O_CREAT | O_EXCL | O_WRONLY,
S_IRUSR | S_IWUSR);
}
}
return data_fd;
}
guac_terminal_typescript* guac_terminal_typescript_alloc(const char* path,
const char* name, int create_path) {
/* Create path if it does not exist, fail if impossible */
if (create_path && mkdir(path, S_IRWXU) && errno != EEXIST)
return NULL;
/* Allocate space for new typescript */
guac_terminal_typescript* typescript =
malloc(sizeof(guac_terminal_typescript));
/* Attempt to open typescript data file */
typescript->data_fd = guac_terminal_typescript_open_data_file(
path, name, typescript->data_filename,
sizeof(typescript->data_filename)
- sizeof(GUAC_TERMINAL_TYPESCRIPT_TIMING_SUFFIX));
if (typescript->data_fd == -1) {
free(typescript);
return NULL;
}
/* Append suffix to basename */
sprintf(typescript->timing_filename, "%s.%s", typescript->data_filename,
GUAC_TERMINAL_TYPESCRIPT_TIMING_SUFFIX);
/* Attempt to open typescript timing file */
typescript->timing_fd = open(typescript->timing_filename,
O_CREAT | O_EXCL | O_WRONLY,
S_IRUSR | S_IWUSR);
if (typescript->timing_fd == -1) {
free(typescript);
close(typescript->data_fd);
return NULL;
}
/* Typescript starts out flushed */
typescript->length = 0;
typescript->last_flush = guac_timestamp_current();
/* Write header */
guac_common_write(typescript->data_fd, GUAC_TERMINAL_TYPESCRIPT_HEADER,
sizeof(GUAC_TERMINAL_TYPESCRIPT_HEADER) - 1);
return typescript;
}
void guac_terminal_typescript_write(guac_terminal_typescript* typescript,
char c) {
/* Flush buffer if no space is available */
if (typescript->length == sizeof(typescript->buffer))
guac_terminal_typescript_flush(typescript);
/* Append single byte to buffer */
typescript->buffer[typescript->length++] = c;
}
void guac_terminal_typescript_flush(guac_terminal_typescript* typescript) {
/* Do nothing if nothing to flush */
if (typescript->length == 0)
return;
/* Get timestamps of previous and current flush */
guac_timestamp this_flush = guac_timestamp_current();
guac_timestamp last_flush = typescript->last_flush;
/* Calculate time since last flush */
int elapsed_time = this_flush - last_flush;
if (elapsed_time > GUAC_TERMINAL_TYPESCRIPT_MAX_DELAY)
elapsed_time = GUAC_TERMINAL_TYPESCRIPT_MAX_DELAY;
/* Produce single line of timestamp output */
char timestamp_buffer[32];
int timestamp_length = snprintf(timestamp_buffer, sizeof(timestamp_buffer),
"%0.6f %i\n", elapsed_time / 1000.0, typescript->length);
/* Calculate actual length of timestamp line */
if (timestamp_length > sizeof(timestamp_buffer))
timestamp_length = sizeof(timestamp_buffer);
/* Write timestamp to timing file */
guac_common_write(typescript->timing_fd,
timestamp_buffer, timestamp_length);
/* Empty buffer into data file */
guac_common_write(typescript->data_fd,
typescript->buffer, typescript->length);
/* Buffer is now flushed */
typescript->length = 0;
typescript->last_flush = this_flush;
}
void guac_terminal_typescript_free(guac_terminal_typescript* typescript) {
/* Do nothing if no typescript provided */
if (typescript == NULL)
return;
/* Flush any pending data */
guac_terminal_typescript_flush(typescript);
/* Write footer */
guac_common_write(typescript->data_fd, GUAC_TERMINAL_TYPESCRIPT_FOOTER,
sizeof(GUAC_TERMINAL_TYPESCRIPT_FOOTER) - 1);
/* Close file descriptors */
close(typescript->data_fd);
close(typescript->timing_fd);
/* Free allocated typescript data */
free(typescript);
}

187
src/terminal/typescript.h Normal file
View File

@ -0,0 +1,187 @@
/*
* Copyright (C) 2016 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef GUAC_TERMINAL_TYPESCRIPT_H
#define GUAC_TERMINAL_TYPESCRIPT_H
#include "config.h"
#include <guacamole/timestamp.h>
/**
* A NULL-terminated string of raw bytes which should be written at the
* beginning of any typescript.
*/
#define GUAC_TERMINAL_TYPESCRIPT_HEADER "[BEGIN TYPESCRIPT]\n"
/**
* A NULL-terminated string of raw bytes which should be written at the
* end of any typescript.
*/
#define GUAC_TERMINAL_TYPESCRIPT_FOOTER "\n[END TYPESCRIPT]\n"
/**
* The maximum amount of time to allow for a particular timing entry, in
* milliseconds. Any timing entries exceeding this value will be written as
* exactly this value instead.
*/
#define GUAC_TERMINAL_TYPESCRIPT_MAX_DELAY 86400000
/**
* The maximum numeric value allowed for the .1, .2, .3, etc. suffix appended
* to the end of the typescript filename if a typescript having the requested
* name already exists.
*/
#define GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX 255
/**
* The maximum length of the string containing a sequential numeric suffix
* between 1 and GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX inclusive, in bytes,
* including NULL terminator.
*/
#define GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH 4
/**
* The maximum overall length of the full path to the typescript file,
* including any additional suffix and NULL terminator, in bytes.
*/
#define GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH 2048
/**
* The suffix which will be appended to the typescript data file's name to
* produce the name of the timing file.
*/
#define GUAC_TERMINAL_TYPESCRIPT_TIMING_SUFFIX "timing"
/**
* An active typescript, consisting of a data file (raw terminal output) and
* timing file (related timestamps and byte counts).
*/
typedef struct guac_terminal_typescript {
/**
* Buffer of raw terminal output which has not yet been written to the
* data file.
*/
char buffer[4096];
/**
* The number of bytes currently stored in the buffer.
*/
int length;
/**
* The full path to the file which will contain the raw terminal output for
* this typescript.
*/
char data_filename[GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH];
/**
* The full path to the file which will contain the timing information for
* this typescript.
*/
char timing_filename[GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH];
/**
* The file descriptor of the file into which raw terminal output should be
* written.
*/
int data_fd;
/**
* The file descriptor of the file into which timing information
* (timestamps and byte counts) related to the raw terminal output in the
* data file should be written.
*/
int timing_fd;
/**
* The last time that this typescript was flushed. If this typescript was
* never flushed, this will be the time the typescripe was created.
*/
guac_timestamp last_flush;
} guac_terminal_typescript;
/**
* Creates a new pair of typescript files within the given path and using the
* given base name, returning an abstraction which represents those files.
* Terminal output will be written to these new files, along with timing
* information. If the create_path flag is non-zero, the given path will be
* created if it does not yet exist.
*
* @param path
* The full absolute path to a directory in which the typescript files
* should be created.
*
* @param name
* The base name to use for the typescript files created within the
* specified path.
*
* @param create_path
* Zero if the specified path MUST exist for typescript files to be
* written, or non-zero if the path should be created if it does not yet
* exist.
*
* @return
* A new guac_terminal_typescript representing the typescript files
* requested, or NULL if creation of the typescript files failed.
*/
guac_terminal_typescript* guac_terminal_typescript_alloc(const char* path,
const char* name, int create_path);
/**
* Writes a single byte of terminal data to the typescript, flushing and
* writing a new timestamp if necessary.
*
* @param typescript
* The typescript that the given byte of raw terminal data should be
* written to.
*
* @param c
* The single byte of raw terminal data to write to the typescript.
*/
void guac_terminal_typescript_write(guac_terminal_typescript* typescript,
char c);
/**
* Flushes any pending data to the typescript, writing a new timestamp to the
* timing file if any data was flushed.
*
* @param typescript
* The typescript which should be flushed.
*/
void guac_terminal_typescript_flush(guac_terminal_typescript* typescript);
/**
* Frees all resources associated with the given typescript, flushing and
* closing the data and timing files and freeing all related memory. If the
* provided typescript is NULL, this function has no effect.
*
* @param typescript
* The typescript to free.
*/
void guac_terminal_typescript_free(guac_terminal_typescript* typescript);
#endif