src/rrd_{client,daemon}.c: Move to a ASCII only protocol.
[rrdtool.git] / src / rrd_daemon.c
index 3af33dc..df31d51 100644 (file)
 # define __attribute__(x) /**/
 #endif
 
-#if 0
-/* FIXME: I don't want to declare strdup myself! */
-extern char *strdup (const char *s);
-#endif
-
 /*
  * Types
  */
@@ -167,7 +162,7 @@ static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
  */
 static int enqueue_cache_item (cache_item_t *ci) /* {{{ */
 {
-  RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
+  RRDD_LOG (LOG_DEBUG, "enqueue_cache_item: Adding %s to the update queue.",
       ci->file);
 
   if (ci == NULL)
@@ -178,12 +173,19 @@ static int enqueue_cache_item (cache_item_t *ci) /* {{{ */
 
   assert (ci->next == NULL);
 
+  if (ci->values_num == 0)
+    return (0);
+
   if (cache_queue_tail == NULL)
     cache_queue_head = ci;
   else
     cache_queue_tail->next = ci;
   cache_queue_tail = ci;
 
+  ci->flags |= CI_FLAGS_IN_QUEUE;
+
+  ci->flags |= CI_FLAGS_IN_QUEUE;
+
   return (0);
 } /* }}} int enqueue_cache_item */
 
@@ -204,7 +206,8 @@ static gboolean tree_callback_flush (gpointer key /* {{{ */
   now = *((time_t *) data);
 
   if (((now - ci->last_flush_time) >= config_write_interval)
-      && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
+      && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
+      && (ci->values_num > 0))
     enqueue_cache_item (ci);
 
   return (TRUE);
@@ -252,21 +255,12 @@ static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
      * something comes in or it's time to do the cache flush. */
     if (cache_queue_head == NULL)
     {
-      struct timespec timeout;
-
-      timeout.tv_sec = next_flush.tv_sec - now.tv_sec;
-      if (next_flush.tv_nsec < (1000 * now.tv_usec))
+      status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
+      if ((status != 0) && (status != ETIMEDOUT))
       {
-        timeout.tv_sec--;
-        timeout.tv_nsec = 1000000000 + next_flush.tv_nsec
-          - (1000 * now.tv_usec);
+        RRDD_LOG (LOG_ERR, "queue_thread_main: "
+            "pthread_cond_timedwait returned %i.", status);
       }
-      else
-      {
-        timeout.tv_nsec = next_flush.tv_nsec - (1000 * now.tv_usec);
-      }
-
-      pthread_cond_timedwait (&cache_cond, &cache_lock, &timeout);
     }
 
     /* Check if a value has arrived. This may be NULL if we timed out or there
@@ -324,12 +318,70 @@ static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
   return (NULL);
 } /* }}} void *queue_thread_main */
 
+static int buffer_get_field (char **buffer_ret, /* {{{ */
+    size_t *buffer_size_ret, char **field_ret)
+{
+  char *buffer;
+  size_t buffer_pos;
+  size_t buffer_size;
+  char *field;
+  size_t field_size;
+  int status;
+
+  buffer = *buffer_ret;
+  buffer_pos = 0;
+  buffer_size = *buffer_size_ret;
+  field = *buffer_ret;
+  field_size = 0;
+
+  /* This is ensured by `handle_request'. */
+  assert (buffer[buffer_size - 1] == ' ');
+
+  status = -1;
+  while (buffer_pos < buffer_size)
+  {
+    /* Check for end-of-field or end-of-buffer */
+    if (buffer[buffer_pos] == ' ')
+    {
+      field[field_size] = 0;
+      field_size++;
+      buffer_pos++;
+      status = 0;
+      break;
+    }
+    /* Handle escaped characters. */
+    else if (buffer[buffer_pos] == '\\')
+    {
+      if (buffer_pos >= (buffer_size - 1))
+        break;
+      buffer_pos++;
+      field[field_size] = buffer[buffer_pos];
+      field_size++;
+      buffer_pos++;
+    }
+    /* Normal operation */ 
+    else
+    {
+      field[field_size] = buffer[buffer_pos];
+      field_size++;
+      buffer_pos++;
+    }
+  } /* while (buffer_pos < buffer_size) */
+
+  if (status != 0)
+    return (status);
+
+  *buffer_ret = buffer + buffer_pos;
+  *buffer_size_ret = buffer_size - buffer_pos;
+  *field_ret = field;
+
+  return (0);
+} /* }}} int buffer_get_field */
+
 static int handle_request_update (int fd, /* {{{ */
-    char *buffer, int buffer_size)
+    char *buffer, size_t buffer_size)
 {
   char *file;
-  char *value;
-  char *buffer_ptr;
   int values_num = 0;
   int status;
 
@@ -340,18 +392,17 @@ static int handle_request_update (int fd, /* {{{ */
 
   now = time (NULL);
 
-  RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
-      fd, (void *) buffer, buffer_size);
-
-  buffer_ptr = buffer;
-
-  file = buffer_ptr;
-  buffer_ptr += strlen (file) + 1;
+  status = buffer_get_field (&buffer, &buffer_size, &file);
+  if (status != 0)
+  {
+    RRDD_LOG (LOG_INFO, "handle_request_update: Cannot get file name.");
+    return (-1);
+  }
 
   pthread_mutex_lock (&cache_lock);
 
   ci = g_tree_lookup (cache_tree, file);
-  if (ci == NULL)
+  if (ci == NULL) /* {{{ */
   {
     ci = (cache_item_t *) malloc (sizeof (cache_item_t));
     if (ci == NULL)
@@ -380,15 +431,20 @@ static int handle_request_update (int fd, /* {{{ */
 
     RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
         ci->file);
-  }
+  } /* }}} */
   assert (ci != NULL);
 
-  while (*buffer_ptr != 0)
+  while (buffer_size > 0)
   {
     char **temp;
+    char *value;
 
-    value = buffer_ptr;
-    buffer_ptr += strlen (value) + 1;
+    status = buffer_get_field (&buffer, &buffer_size, &value);
+    if (status != 0)
+    {
+      RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
+      break;
+    }
 
     temp = (char **) realloc (ci->values,
         sizeof (char *) * (ci->values_num + 1));
@@ -411,7 +467,8 @@ static int handle_request_update (int fd, /* {{{ */
   }
 
   if (((now - ci->last_flush_time) >= config_write_interval)
-      && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
+      && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
+      && (ci->values_num > 0))
   {
     enqueue_cache_item (ci);
     pthread_cond_signal (&cache_cond);
@@ -422,7 +479,7 @@ static int handle_request_update (int fd, /* {{{ */
   snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
   answer[sizeof (answer) - 1] = 0;
 
-  status = write (fd, answer, sizeof (answer));
+  status = write (fd, answer, strlen (answer));
   if (status < 0)
   {
     status = errno;
@@ -436,31 +493,41 @@ static int handle_request_update (int fd, /* {{{ */
 static int handle_request (int fd) /* {{{ */
 {
   char buffer[4096];
-  int buffer_size;
-
-  RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
+  size_t buffer_size;
+  char *buffer_ptr;
+  char *command;
+  int status;
 
-  buffer_size = read (fd, buffer, sizeof (buffer));
-  if (buffer_size < 1)
+  status = read (fd, buffer, sizeof (buffer));
+  if (status < 1)
   {
     RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
     return (-1);
   }
+  buffer_size = status;
   assert (((size_t) buffer_size) <= sizeof (buffer));
 
-  if ((buffer[buffer_size - 2] != 0)
-      || (buffer[buffer_size - 1] != 0))
+  if (buffer[buffer_size - 1] != '\n')
   {
     RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
     return (-1);
   }
+  /* Place the normal field separator at the end to simplify
+   * `buffer_get_field's work. */
+  buffer[buffer_size - 1] = ' ';
+
+  buffer_ptr = buffer;
+  command = NULL;
+  status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
+  if (status != 0)
+  {
+    RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
+    return (-1);
+  }
 
-  /* fields in the buffer a separated by null bytes. */
-  if (strcmp (buffer, "update") == 0)
+  if (strcmp (command, "update") == 0)
   {
-    int offset = strlen ("update") + 1;
-    return (handle_request_update (fd, buffer + offset,
-          buffer_size - offset));
+    return (handle_request_update (fd, buffer_ptr, buffer_size));
   }
   else
   {
@@ -478,8 +545,6 @@ static void *connection_thread_main (void *args /* {{{ */
   
   fd = *((int *) args);
 
-  RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
-      "connetion_threads[]..");
   pthread_mutex_lock (&connetion_threads_lock);
   {
     pthread_t *temp;
@@ -498,7 +563,6 @@ static void *connection_thread_main (void *args /* {{{ */
     }
   }
   pthread_mutex_unlock (&connetion_threads_lock);
-  RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
 
   while (do_shutdown == 0)
   {
@@ -523,8 +587,6 @@ static void *connection_thread_main (void *args /* {{{ */
 
     if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
     {
-      RRDD_LOG (LOG_DEBUG, "connection_thread_main: "
-          "poll(2) returned POLLHUP.");
       close (fd);
       break;
     }
@@ -808,9 +870,6 @@ static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
         continue;
       }
 
-      RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
-          *client_sd);
-
       status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
           /* args = */ (void *) client_sd);
       if (status != 0)
@@ -820,10 +879,6 @@ static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
         free (client_sd);
         continue;
       }
-
-      RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
-          "tid = %lu",
-          *((unsigned long *) &tid));
     } /* for (pollfds_num) */
   } /* while (do_shutdown == 0) */