+static int handle_request_quit (HANDLER_PROTO) /* {{{ */
+{
+ return -1;
+} /* }}} static int handle_request_quit */
+
+static command_t list_of_commands[] = { /* {{{ */
+ {
+ "UPDATE",
+ handle_request_update,
+ CMD_CONTEXT_ANY,
+ "UPDATE <filename> <values> [<values> ...]\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"
+ },
+ {
+ "WROTE",
+ handle_request_wrote,
+ CMD_CONTEXT_JOURNAL,
+ NULL,
+ NULL
+ },
+ {
+ "FLUSH",
+ handle_request_flush,
+ CMD_CONTEXT_CLIENT | CMD_CONTEXT_BATCH,
+ "FLUSH <filename>\n"
+ ,
+ "Adds the given filename to the head of the update queue and returns\n"
+ "after it has been dequeued.\n"
+ },
+ {
+ "FLUSHALL",
+ handle_request_flushall,
+ CMD_CONTEXT_CLIENT,
+ "FLUSHALL\n"
+ ,
+ "Triggers writing of all pending updates. Returns immediately.\n"
+ },
+ {
+ "PENDING",
+ handle_request_pending,
+ CMD_CONTEXT_CLIENT,
+ "PENDING <filename>\n"
+ ,
+ "Shows any 'pending' updates for a file, in order.\n"
+ "The updates shown have not yet been written to the underlying RRD file.\n"
+ },
+ {
+ "FORGET",
+ handle_request_forget,
+ CMD_CONTEXT_ANY,
+ "FORGET <filename>\n"
+ ,
+ "Removes the file completely from the cache.\n"
+ "Any pending updates for the file will be lost.\n"
+ },
+ {
+ "QUEUE",
+ handle_request_queue,
+ CMD_CONTEXT_CLIENT,
+ "QUEUE\n"
+ ,
+ "Shows all files in the output queue.\n"
+ "The output is zero or more lines in the following format:\n"
+ "(where <num_vals> is the number of values to be written)\n"
+ "\n"
+ "<num_vals> <filename>\n"
+ },
+ {
+ "STATS",
+ handle_request_stats,
+ CMD_CONTEXT_CLIENT,
+ "STATS\n"
+ ,
+ "Returns some performance counters, see the rrdcached(1) manpage for\n"
+ "a description of the values.\n"
+ },
+ {
+ "HELP",
+ handle_request_help,
+ CMD_CONTEXT_CLIENT,
+ "HELP [<command>]\n",
+ NULL, /* special! */
+ },
+ {
+ "BATCH",
+ batch_start,
+ CMD_CONTEXT_CLIENT,
+ "BATCH\n"
+ ,
+ "The 'BATCH' command permits the client to initiate a bulk load\n"
+ " of commands to rrdcached.\n"
+ "\n"
+ "Usage:\n"
+ "\n"
+ " client: BATCH\n"
+ " server: 0 Go ahead. End with dot '.' on its own line.\n"
+ " client: command #1\n"
+ " client: command #2\n"
+ " client: ... and so on\n"
+ " client: .\n"
+ " server: 2 errors\n"
+ " server: 7 message for command #7\n"
+ " server: 9 message for command #9\n"
+ "\n"
+ "For more information, consult the rrdcached(1) documentation.\n"
+ },
+ {
+ ".", /* BATCH terminator */
+ batch_done,
+ CMD_CONTEXT_BATCH,
+ NULL,
+ NULL
+ },
+ {
+ "FETCH",
+ handle_request_fetch,
+ CMD_CONTEXT_CLIENT,
+ "FETCH <file> <CF> [<start> [<end>]]\n"
+ ,
+ "The 'FETCH' can be used by the client to retrieve values from an RRD file.\n"
+ },
+ {
+ "INFO",
+ handle_request_info,
+ CMD_CONTEXT_CLIENT,
+ "INFO <filename>\n",
+ "The INFO command retrieves information about a specified RRD file.\n"
+ "This is returned in standard rrdinfo format, a sequence of lines\n"
+ "with the format <keyname> = <value>\n"
+ "Note that this is the data as of the last update of the RRD file itself,\n"
+ "not the last time data was received via rrdcached, so there may be pending\n"
+ "updates in the queue. If this bothers you, then first run a FLUSH.\n"
+ },
+ {
+ "FIRST",
+ handle_request_first,
+ CMD_CONTEXT_CLIENT,
+ "FIRST <filename> <rra index>\n",
+ "The FIRST command retrieves the first data time for a specified RRA in\n"
+ "an RRD file.\n"
+ },
+ {
+ "LAST",
+ handle_request_last,
+ CMD_CONTEXT_CLIENT,
+ "LAST <filename>\n",
+ "The LAST command retrieves the last update time for a specified RRD file.\n"
+ "Note that this is the time of the last update of the RRD file itself, not\n"
+ "the last time data was received via rrdcached, so there may be pending\n"
+ "updates in the queue. If this bothers you, then first run a FLUSH.\n"
+ },
+ {
+ "CREATE",
+ handle_request_create,
+ CMD_CONTEXT_CLIENT | CMD_CONTEXT_BATCH,
+ "CREATE <filename> [-b start] [-s step] [-O] <DS definitions> <RRA definitions>\n",
+ "The CREATE command will create an RRD file, overwriting any existing file\n"
+ "unless the -O option is given or rrdcached was started with the -O option.\n"
+ "The start parameter needs to be in seconds since 1/1/70 (AT-style syntax is\n"
+ "not acceptable) and the step is in seconds (default is 300).\n"
+ "The DS and RRA definitions are as for the 'rrdtool create' command.\n"
+ },
+ {
+ "QUIT",
+ handle_request_quit,
+ CMD_CONTEXT_CLIENT | CMD_CONTEXT_BATCH,
+ "QUIT\n"
+ ,
+ "Disconnect from rrdcached.\n"
+ }
+}; /* }}} command_t list_of_commands[] */
+static size_t list_of_commands_len = sizeof (list_of_commands)
+ / sizeof (list_of_commands[0]);
+
+static command_t *find_command(char *cmd)
+{
+ size_t i;
+
+ for (i = 0; i < list_of_commands_len; i++)
+ if (strcasecmp(cmd, list_of_commands[i].cmd) == 0)
+ return (&list_of_commands[i]);
+ return NULL;
+}
+
+/* We currently use the index in the `list_of_commands' array as a bit position
+ * in `listen_socket_t.permissions'. This member schould NEVER be accessed from
+ * outside these functions so that switching to a more elegant storage method
+ * is easily possible. */
+static ssize_t find_command_index (const char *cmd) /* {{{ */
+{
+ size_t i;
+
+ for (i = 0; i < list_of_commands_len; i++)
+ if (strcasecmp(cmd, list_of_commands[i].cmd) == 0)
+ return ((ssize_t) i);
+ return (-1);
+} /* }}} ssize_t find_command_index */
+
+static int socket_permission_check (listen_socket_t *sock, /* {{{ */
+ const char *cmd)
+{
+ ssize_t i;
+
+ if (JOURNAL_REPLAY(sock))
+ return (1);
+
+ if (cmd == NULL)
+ return (-1);
+
+ if ((strcasecmp ("QUIT", cmd) == 0)
+ || (strcasecmp ("HELP", cmd) == 0))
+ return (1);
+ else if (strcmp (".", cmd) == 0)
+ cmd = "BATCH";
+
+ i = find_command_index (cmd);
+ if (i < 0)
+ return (-1);
+ assert (i < 32);
+
+ if ((sock->permissions & (1 << i)) != 0)
+ return (1);
+ return (0);
+} /* }}} int socket_permission_check */
+
+static int socket_permission_add (listen_socket_t *sock, /* {{{ */
+ const char *cmd)
+{
+ ssize_t i;
+
+ i = find_command_index (cmd);
+ if (i < 0)
+ return (-1);
+ assert (i < 32);
+
+ sock->permissions |= (1 << i);
+ return (0);
+} /* }}} int socket_permission_add */
+
+static void socket_permission_clear (listen_socket_t *sock) /* {{{ */
+{
+ sock->permissions = 0;
+} /* }}} socket_permission_clear */
+
+static void socket_permission_copy (listen_socket_t *dest, /* {{{ */
+ listen_socket_t *src)
+{
+ dest->permissions = src->permissions;
+} /* }}} socket_permission_copy */
+
+static void socket_permission_set_all (listen_socket_t *sock) /* {{{ */
+{
+ size_t i;
+
+ sock->permissions = 0;
+ for (i = 0; i < list_of_commands_len; i++)
+ sock->permissions |= (1 << i);
+} /* }}} void socket_permission_set_all */
+
+/* check whether commands are received in the expected context */
+static int command_check_context(listen_socket_t *sock, command_t *cmd)
+{
+ if (JOURNAL_REPLAY(sock))
+ return (cmd->context & CMD_CONTEXT_JOURNAL);
+ else if (sock->batch_start)
+ return (cmd->context & CMD_CONTEXT_BATCH);
+ else
+ return (cmd->context & CMD_CONTEXT_CLIENT);
+
+ /* NOTREACHED */
+ assert(1==0);
+}
+
+static int handle_request_help (HANDLER_PROTO) /* {{{ */
+{
+ int status;
+ char *cmd_str;
+ char *resp_txt;
+ command_t *help = NULL;
+
+ status = buffer_get_field (&buffer, &buffer_size, &cmd_str);
+ if (status == 0)
+ help = find_command(cmd_str);
+
+ if (help && (help->syntax || help->help))
+ {
+ char tmp[RRD_CMD_MAX];
+
+ snprintf(tmp, sizeof(tmp)-1, "Help for %s\n", help->cmd);
+ resp_txt = tmp;
+
+ if (help->syntax)
+ add_response_info(sock, "Usage: %s\n", help->syntax);
+
+ if (help->help)
+ add_response_info(sock, "%s\n", help->help);
+ }
+ else
+ {
+ size_t i;
+
+ resp_txt = "Command overview\n";
+
+ for (i = 0; i < list_of_commands_len; i++)
+ {
+ if (list_of_commands[i].syntax == NULL)
+ continue;
+ add_response_info (sock, "%s", list_of_commands[i].syntax);
+ }
+ }
+
+ return send_response(sock, RESP_OK, resp_txt);
+} /* }}} int handle_request_help */
+
+static int handle_request (DISPATCH_PROTO) /* {{{ */
+{
+ char *buffer_ptr = buffer;
+ char *cmd_str = NULL;
+ command_t *cmd = NULL;
+ int status;
+
+ assert (buffer[buffer_size - 1] == '\0');
+
+ status = buffer_get_field (&buffer_ptr, &buffer_size, &cmd_str);
+ if (status != 0)
+ {
+ RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
+ return (-1);
+ }
+
+ if (sock != NULL && sock->batch_start)
+ sock->batch_cmd++;
+
+ cmd = find_command(cmd_str);
+ if (!cmd)
+ return send_response(sock, RESP_ERR, "Unknown command: %s\n", cmd_str);
+
+ if (!socket_permission_check (sock, cmd->cmd))
+ return send_response(sock, RESP_ERR, "Permission denied.\n");
+
+ if (!command_check_context(sock, cmd))
+ return send_response(sock, RESP_ERR, "Can't use '%s' here.\n", cmd_str);
+
+ return cmd->handler(cmd, sock, now, buffer_ptr, buffer_size);
+} /* }}} int handle_request */
+
+static void journal_set_free (journal_set *js) /* {{{ */
+{
+ if (js == NULL)
+ return;
+
+ rrd_free_ptrs((void ***) &js->files, &js->files_num);
+
+ free(js);
+} /* }}} journal_set_free */
+
+static void journal_set_remove (journal_set *js) /* {{{ */
+{
+ if (js == NULL)
+ return;
+
+ for (uint i=0; i < js->files_num; i++)
+ {
+ RRDD_LOG(LOG_DEBUG, "removing old journal %s", js->files[i]);
+ unlink(js->files[i]);
+ }
+} /* }}} journal_set_remove */
+
+/* close current journal file handle.
+ * MUST hold journal_lock before calling */
+static void journal_close(void) /* {{{ */
+{
+ if (journal_fh != NULL)
+ {
+ if (fclose(journal_fh) != 0)
+ RRDD_LOG(LOG_ERR, "cannot close journal: %s", rrd_strerror(errno));
+ }
+
+ journal_fh = NULL;
+ journal_size = 0;
+} /* }}} journal_close */
+
+/* MUST hold journal_lock before calling */
+static void journal_new_file(void) /* {{{ */