From 7051f03b0a321713e8a910c52ffaf7323d689f21 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Wed, 30 Nov 2011 12:03:27 -0800 Subject: [PATCH] Log PID, remove support for lack-of-fork, update #define naming. --- include/client.h | 15 ++++++++------- src/client.c | 10 +++++----- src/daemon.c | 37 +++++++------------------------------ 3 files changed, 20 insertions(+), 42 deletions(-) diff --git a/include/client.h b/include/client.h index 0b1f588..8f2cc83 100644 --- a/include/client.h +++ b/include/client.h @@ -45,17 +45,17 @@ * timeframe, server messages will not be handled until a sync instruction is * received from the client. */ -#define GUAC_SYNC_THRESHOLD 500 +#define GUACD_SYNC_THRESHOLD 500 /** * The time to allow between server sync messages in milliseconds. A sync - * message from the server will be sent every GUAC_SYNC_FREQUENCY milliseconds. + * message from the server will be sent every GUACD_SYNC_FREQUENCY milliseconds. * As this will induce a response from a client that is not malfunctioning, * this is used to detect when a client has died. This must be set to a * reasonable value to avoid clients being disconnected unnecessarily due * to timeout. */ -#define GUAC_SYNC_FREQUENCY 5000 +#define GUACD_SYNC_FREQUENCY 5000 /** * The amount of time to wait after handling server messages. If a client @@ -63,20 +63,21 @@ * are being handled, there will be a pause of this many milliseconds before * the next call to the message handler. */ -#define GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY 50 +#define GUACD_MESSAGE_HANDLE_FREQUENCY 50 + /** * The number of milliseconds to wait for messages in any phase before * timing out and closing the connection with an error. */ -#define GUAC_TIMEOUT 15000 +#define GUACD_TIMEOUT 15000 /** * The number of microseconds to wait for messages in any phase before * timing out and closing the conncetion with an error. This is always - * equal to GUAC_TIMEOUT * 1000. + * equal to GUACD_TIMEOUT * 1000. */ -#define GUAC_USEC_TIMEOUT (GUAC_TIMEOUT*1000) +#define GUACD_USEC_TIMEOUT (GUACD_TIMEOUT*1000) void guac_client_stop(guac_client* client); diff --git a/src/client.c b/src/client.c index abaf442..9b6b680 100644 --- a/src/client.c +++ b/src/client.c @@ -78,7 +78,7 @@ void* __guac_client_output_thread(void* data) { /* Occasionally ping client with repeat of last sync */ guac_timestamp timestamp = guac_protocol_get_timestamp(); - if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) { + if (timestamp - last_ping_timestamp > GUACD_SYNC_FREQUENCY) { /* Record time of last synnc */ last_ping_timestamp = timestamp; @@ -106,7 +106,7 @@ void* __guac_client_output_thread(void* data) { /* Only handle messages if synced within threshold */ if (client->last_sent_timestamp - client->last_received_timestamp - < GUAC_SYNC_THRESHOLD) { + < GUACD_SYNC_THRESHOLD) { int retval = client->handle_messages(client); if (retval) { @@ -137,13 +137,13 @@ void* __guac_client_output_thread(void* data) { /* Do not spin while waiting for old sync */ else - __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY); + __guacd_sleep(GUACD_MESSAGE_HANDLE_FREQUENCY); } /* If no message handler, just sleep until next sync ping */ else - __guacd_sleep(GUAC_SYNC_FREQUENCY); + __guacd_sleep(GUACD_SYNC_FREQUENCY); } /* End of output loop */ @@ -162,7 +162,7 @@ void* __guac_client_input_thread(void* data) { /* Read instruction */ guac_instruction* instruction = - guac_protocol_read_instruction(socket, GUAC_USEC_TIMEOUT); + guac_protocol_read_instruction(socket, GUACD_USEC_TIMEOUT); /* Stop on error */ if (instruction == NULL) { diff --git a/src/daemon.c b/src/daemon.c index 6312590..5855214 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -76,7 +76,7 @@ void* start_client_thread(void* data) { /* Get protocol from select instruction */ select = guac_protocol_expect_instruction( - socket, GUAC_USEC_TIMEOUT, "select"); + socket, GUACD_USEC_TIMEOUT, "select"); if (select == NULL) { /* Log error */ @@ -135,7 +135,7 @@ void* start_client_thread(void* data) { /* Get args from connect instruction */ connect = guac_protocol_expect_instruction( - socket, GUAC_USEC_TIMEOUT, "connect"); + socket, GUACD_USEC_TIMEOUT, "connect"); if (connect == NULL) { /* Log error */ @@ -255,7 +255,6 @@ int main(int argc, char* argv[]) { } /* Fork into background */ -#ifdef HAVE_FORK daemon_pid = fork(); /* If error, fail */ @@ -286,11 +285,9 @@ int main(int argc, char* argv[]) { exit(EXIT_SUCCESS); } -#else - daemon_pid = getpid(); - syslog(LOG_INFO, "fork() not defined at compile time."); - syslog(LOG_INFO, "guacd running in foreground only."); -#endif + + /* Open log */ + openlog(NULL, LOG_PID, LOG_DAEMON); /* Otherwise, this is the daemon */ syslog(LOG_INFO, "Listening on port %i", listen_port); @@ -308,11 +305,7 @@ int main(int argc, char* argv[]) { /* Daemon loop */ for (;;) { -#ifdef HAVE_FORK pid_t child_pid; -#else - guac_thread_t thread; -#endif client_thread_data* data; /* Listen for connections */ @@ -333,17 +326,8 @@ int main(int argc, char* argv[]) { data->fd = connected_socket_fd; /* - * Once connection is accepted, send child into background, whether through - * fork() or through creating a thread. If thead support is not present on - * the platform, guacd will still work, but will only be able to handle one - * connection at a time. - */ - -#ifdef HAVE_FORK - - /*** FORK ***/ - - /* + * Once connection is accepted, send child into background. + * * Note that we prefer fork() over threads for connection-handling * processes as they give each connection its own memory area, and * isolate the main daemon and other connections from errors in any @@ -367,13 +351,6 @@ int main(int argc, char* argv[]) { syslog(LOG_ERR, "Error closing daemon reference to child descriptor: %s", strerror(errno)); } -#else - - if (guac_thread_create(&thread, start_client_thread, (void*) data)) - syslog(LOG_ERR, "Could not create client thread: %s", strerror(errno)); - -#endif - } /* Close socket */ -- 1.7.10.4