+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;
+
+ if (buffer_size <= 0)
+ return (-1);
+
+ /* 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 flush_file (const char *filename) /* {{{ */
+{
+ cache_item_t *ci;
+
+ pthread_mutex_lock (&cache_lock);
+
+ ci = (cache_item_t *) g_tree_lookup (cache_tree, filename);
+ if (ci == NULL)
+ {
+ pthread_mutex_unlock (&cache_lock);
+ return (ENOENT);
+ }
+
+ /* Enqueue at head */
+ enqueue_cache_item (ci, HEAD);
+ pthread_cond_signal (&cache_cond);
+
+ while ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
+ {
+ ci = NULL;
+
+ pthread_cond_wait (&flush_cond, &cache_lock);
+
+ ci = g_tree_lookup (cache_tree, filename);
+ if (ci == NULL)
+ {
+ RRDD_LOG (LOG_ERR, "flush_file: Tree node went away "
+ "while waiting for flush.");
+ pthread_mutex_unlock (&cache_lock);
+ return (-1);
+ }
+ }
+
+ pthread_mutex_unlock (&cache_lock);
+ return (0);
+} /* }}} int flush_file */
+
+static int handle_request_help (int fd, /* {{{ */
+ char *buffer, size_t buffer_size)
+{
+ int status;
+ char **help_text;
+ size_t help_text_len;
+ char *command;
+ size_t i;
+
+ char *help_help[] =
+ {
+ "4 Command overview\n",
+ "FLUSH <filename>\n",
+ "HELP [<command>]\n",
+ "UPDATE <filename> <values> [<values> ...]\n",
+ "STATS\n"
+ };
+ size_t help_help_len = sizeof (help_help) / sizeof (help_help[0]);
+
+ char *help_flush[] =
+ {
+ "4 Help for FLUSH\n",
+ "Usage: FLUSH <filename>\n",
+ "\n",
+ "Adds the given filename to the head of the update queue and returns\n",
+ "after is has been dequeued.\n"
+ };
+ size_t help_flush_len = sizeof (help_flush) / sizeof (help_flush[0]);
+
+ char *help_update[] =
+ {
+ "9 Help for UPDATE\n",
+ "Usage: UPDATE <filename> <values> [<values> ...]\n"
+ "\n",
+ "Adds the given file to the internal cache if it is not yet known and\n",
+ "appends the given value(s) to the entry. See the rrdcached(1) manpage\n",
+ "for details.\n",
+ "\n",
+ "Each <values> has the following form:\n",
+ " <values> = <time>:<value>[:<value>[...]]\n",
+ "See the rrdupdate(1) manpage for details.\n"
+ };
+ size_t help_update_len = sizeof (help_update) / sizeof (help_update[0]);
+
+ char *help_stats[] =
+ {
+ "4 Help for STATS\n",
+ "Usage: STATS\n",
+ "\n",
+ "Returns some performance counters, see the rrdcached(1) manpage for\n",
+ "a description of the values.\n"
+ };
+ size_t help_stats_len = sizeof (help_stats) / sizeof (help_stats[0]);
+
+ status = buffer_get_field (&buffer, &buffer_size, &command);
+ if (status != 0)
+ {
+ help_text = help_help;
+ help_text_len = help_help_len;
+ }
+ else
+ {
+ if (strcasecmp (command, "update") == 0)
+ {
+ help_text = help_update;
+ help_text_len = help_update_len;
+ }
+ else if (strcasecmp (command, "flush") == 0)
+ {
+ help_text = help_flush;
+ help_text_len = help_flush_len;
+ }
+ else if (strcasecmp (command, "stats") == 0)
+ {
+ help_text = help_stats;
+ help_text_len = help_stats_len;
+ }
+ else
+ {
+ help_text = help_help;
+ help_text_len = help_help_len;
+ }
+ }
+
+ for (i = 0; i < help_text_len; i++)
+ {
+ status = swrite (fd, help_text[i], strlen (help_text[i]));
+ if (status < 0)
+ {
+ status = errno;
+ RRDD_LOG (LOG_ERR, "handle_request_help: swrite returned an error.");
+ return (status);
+ }
+ }
+
+ return (0);
+} /* }}} int handle_request_help */
+
+static int handle_request_stats (int fd, /* {{{ */
+ char *buffer __attribute__((unused)),
+ size_t buffer_size __attribute__((unused)))
+{
+ int status;
+ char outbuf[4096];
+
+ uint64_t copy_queue_length;
+ uint64_t copy_updates_written;
+ uint64_t copy_data_sets_written;
+
+ uint64_t tree_nodes_number;
+ uint64_t tree_depth;
+
+ pthread_mutex_lock (&stats_lock);
+ copy_queue_length = stats_queue_length;
+ copy_updates_written = stats_updates_written;
+ copy_data_sets_written = stats_data_sets_written;
+ pthread_mutex_unlock (&stats_lock);
+
+ pthread_mutex_lock (&cache_lock);
+ tree_nodes_number = (uint64_t) g_tree_nnodes (cache_tree);
+ tree_depth = (uint64_t) g_tree_height (cache_tree);
+ pthread_mutex_unlock (&cache_lock);
+
+#define RRDD_STATS_SEND \
+ outbuf[sizeof (outbuf) - 1] = 0; \
+ status = swrite (fd, outbuf, strlen (outbuf)); \
+ if (status < 0) \
+ { \
+ status = errno; \
+ RRDD_LOG (LOG_INFO, "handle_request_stats: swrite returned an error."); \
+ return (status); \
+ }
+
+ strncpy (outbuf, "5 Statistics follow\n", sizeof (outbuf));
+ RRDD_STATS_SEND;
+
+ snprintf (outbuf, sizeof (outbuf),
+ "QueueLength: %"PRIu64"\n", copy_queue_length);
+ RRDD_STATS_SEND;
+
+ snprintf (outbuf, sizeof (outbuf),
+ "UpdatesWritten: %"PRIu64"\n", copy_updates_written);
+ RRDD_STATS_SEND;
+
+ snprintf (outbuf, sizeof (outbuf),
+ "DataSetsWritten: %"PRIu64"\n", copy_data_sets_written);
+ RRDD_STATS_SEND;
+
+ snprintf (outbuf, sizeof (outbuf),
+ "TreeNodesNumber: %"PRIu64"\n", tree_nodes_number);
+ RRDD_STATS_SEND;
+
+ snprintf (outbuf, sizeof (outbuf),
+ "TreeDepth: %"PRIu64"\n", tree_depth);
+ RRDD_STATS_SEND;
+
+ return (0);
+#undef RRDD_STATS_SEND
+} /* }}} int handle_request_stats */
+
+static int handle_request_flush (int fd, /* {{{ */
+ char *buffer, size_t buffer_size)
+{
+ char *file;
+ int status;
+ char result[4096];
+
+ status = buffer_get_field (&buffer, &buffer_size, &file);
+ if (status != 0)
+ {
+ strncpy (result, "-1 Usage: flush <filename>\n", sizeof (result));
+ }
+ else
+ {
+ status = flush_file (file);
+ if (status == 0)
+ snprintf (result, sizeof (result), "0 Successfully flushed %s.\n", file);
+ else if (status == ENOENT)
+ snprintf (result, sizeof (result), "-1 No such file: %s.\n", file);
+ else if (status < 0)
+ strncpy (result, "-1 Internal error.\n", sizeof (result));
+ else
+ snprintf (result, sizeof (result), "-1 Failed with status %i.\n", status);
+ }
+ result[sizeof (result) - 1] = 0;
+
+ status = swrite (fd, result, strlen (result));
+ if (status < 0)
+ {
+ status = errno;
+ RRDD_LOG (LOG_INFO, "handle_request_flush: swrite returned an error.");
+ return (status);
+ }
+
+ return (0);
+} /* }}} int handle_request_flush */
+