Removed thread.*, using pthreads directly. More renaming of guac to guacd.
[guacd.git] / src / daemon.c
index 4fec767..56a458a 100644 (file)
@@ -52,6 +52,7 @@
 #include <guacamole/error.h>
 
 #include "client.h"
+#include "log.h"
 
 typedef struct client_thread_data {
 
@@ -64,22 +65,22 @@ void* start_client_thread(void* data) {
 
     guac_client* client;
     guac_client_plugin* plugin;
-    client_thread_data* thread_data = (client_thread_data*) data;
-    guac_socket* socket;
     guac_instruction* select;
     guac_instruction* connect;
 
+    /* Get thread data */
+    client_thread_data* thread_data = (client_thread_data*) data;
+
     /* Open guac_socket */
-    socket = guac_socket_open(thread_data->fd);
+    guac_socket* socket = guac_socket_open(thread_data->fd);
 
     /* 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 */
-        syslog(LOG_ERR, "Error reading \"select\": %s",
-                guac_status_string(guac_error));
+        guacd_log_guac_error("Error reading \"select\"");
 
         /* Free resources */
         guac_socket_close(socket);
@@ -109,8 +110,7 @@ void* start_client_thread(void* data) {
     if (plugin == NULL) {
 
         /* Log error */
-        syslog(LOG_ERR, "Error loading client plugin: %s",
-                guac_status_string(guac_error));
+        guacd_log_guac_error("Error loading client plugin");
 
         /* Free resources */
         guac_socket_close(socket);
@@ -123,11 +123,10 @@ void* start_client_thread(void* data) {
             || guac_socket_flush(socket)) {
 
         /* Log error */
-        syslog(LOG_ERR, "Error sending \"args\": %s",
-                guac_status_string(guac_error));
+        guacd_log_guac_error("Error sending \"args\"");
 
         if (guac_client_plugin_close(plugin))
-            syslog(LOG_ERR, "Error closing client plugin");
+            guacd_log_guac_error("Error closing client plugin");
 
         guac_socket_close(socket);
         free(data);
@@ -136,15 +135,14 @@ 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 */
-        syslog(LOG_ERR, "Error reading \"connect\": %s",
-                guac_status_string(guac_error));
+        guacd_log_guac_error("Error reading \"connect\"");
 
         if (guac_client_plugin_close(plugin))
-            syslog(LOG_ERR, "Error closing client plugin");
+            guacd_log_guac_error("Error closing client plugin");
 
         guac_socket_close(socket);
         free(data);
@@ -154,27 +152,32 @@ void* start_client_thread(void* data) {
     /* Load and init client */
     client = guac_client_plugin_get_client(plugin, socket,
             connect->argc, connect->argv); 
-    guac_instruction_free(select);
+    guac_instruction_free(connect);
 
     if (client == NULL) {
 
-        syslog(LOG_ERR, "Error instantiating client: %s",
-                guac_status_string(guac_error));
+        guacd_log_guac_error("Error instantiating client");
 
         if (guac_client_plugin_close(plugin))
-            syslog(LOG_ERR, "Error closing client plugin");
+            guacd_log_guac_error("Error closing client plugin");
 
         guac_socket_close(socket);
         free(data);
         return NULL;
     }
 
+    /* Set up logging in client */
+    client->log_info_handler  = guacd_log_info;
+    client->log_error_handler = guacd_log_error;
+
     /* Start client threads */
     syslog(LOG_INFO, "Starting client");
-    guac_start_client(client);
+    if (guacd_client_start(client))
+        syslog(LOG_ERR, "Client finished abnormally");
+    else
+        syslog(LOG_INFO, "Client finished normally");
 
     /* Clean up */
-    syslog(LOG_INFO, "Client finished");
     guac_client_free(client);
     if (guac_client_plugin_close(plugin))
         syslog(LOG_ERR, "Error closing client plugin");
@@ -252,7 +255,6 @@ int main(int argc, char* argv[]) {
     } 
 
     /* Fork into background */
-#ifdef HAVE_FORK
     daemon_pid = fork();
 
     /* If error, fail */
@@ -283,14 +285,12 @@ 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, "Started, listening on port %i", listen_port);
+    syslog(LOG_INFO, "Listening on port %i", listen_port);
 
     /* Ignore SIGPIPE */
     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
@@ -305,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 */
@@ -330,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
@@ -364,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 */