+static void gl_dump_cb (void *ctx, /* {{{ */
+ const char *str, unsigned int len)
+{
+ int fd = *((int *) ctx);
+ const char *buffer;
+ size_t buffer_size;
+ ssize_t status;
+
+ buffer = str;
+ buffer_size = (size_t) len;
+ while (buffer_size > 0)
+ {
+ status = write (fd, buffer, buffer_size);
+ if (status < 0)
+ {
+ fprintf (stderr, "write(2) failed with status %i\n", errno);
+ return;
+ }
+
+ buffer += status;
+ buffer_size -= status;
+ }
+} /* }}} void gl_dump_cb */
+
+static int gl_update_cache (void) /* {{{ */
+{
+ int fd;
+ yajl_gen handler;
+ yajl_gen_config handler_config = { /* pretty = */ 1, /* indent = */ " " };
+ struct flock lock;
+ struct stat statbuf;
+ int status;
+ size_t i;
+
+ memset (&statbuf, 0, sizeof (statbuf));
+ status = stat (CACHE_FILE, &statbuf);
+ if (status == 0)
+ {
+ if (statbuf.st_mtime >= gl_last_update)
+ {
+ fprintf (stderr, "gl_update_cache: Not writing to cache because it's "
+ "at least as new as our internal data\n");
+ return (0);
+ }
+ }
+ else
+ {
+ status = errno;
+ fprintf (stderr, "gl_update_cache: stat(2) failed with status %i\n",
+ status);
+ /* Continue writing the file if possible. */
+ }
+
+ fd = open (CACHE_FILE, O_WRONLY | O_TRUNC | O_CREAT,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+ if (fd < 0)
+ {
+ status = errno;
+ fprintf (stderr, "gl_update_cache: open(2) failed with status %i\n",
+ status);
+ return (status);
+ }
+
+ memset (&lock, 0, sizeof (lock));
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0;
+ lock.l_len = 0; /* lock everything */
+
+ while (42)
+ {
+ status = fcntl (fd, F_SETLKW, &lock);
+ if (status == 0)
+ break;
+
+ if (errno == EINTR)
+ continue;
+
+ fprintf (stderr, "gl_update_cache: fcntl(2) failed with status %i\n", errno);
+ close (fd);
+ return (errno);
+ }
+
+ handler = yajl_gen_alloc2 (gl_dump_cb, &handler_config,
+ /* alloc funcs = */ NULL, /* ctx = */ &fd);
+ if (handler == NULL)
+ {
+ close (fd);
+ return (-1);
+ }
+
+ fprintf (stderr, "gl_update_cache: Start writing data\n");
+ fflush (stderr);
+
+ yajl_gen_array_open (handler);
+
+ for (i = 0; i < gl_active_num; i++)
+ graph_to_json (gl_active[i], handler);
+
+ for (i = 0; i < gl_dynamic_num; i++)
+ graph_to_json (gl_dynamic[i], handler);
+
+ yajl_gen_array_close (handler);
+
+ yajl_gen_free (handler);
+ close (fd);
+
+ fprintf (stderr, "gl_update_cache: Finished writing data\n");
+ fflush (stderr);
+
+ return (0);
+} /* }}} int gl_update_cache */
+
+/*
+ * JSON parsing functions
+ */
+#define CTX_MASK 0xff000000
+#define CTX_GRAPH 0x01000000
+#define CTX_GRAPH_SELECT 0x02000000
+#define CTX_INST 0x03000000
+#define CTX_INST_SELECT 0x04000000
+#define CTX_INST_FILE 0x05000000
+
+#define CTX_IDENT_MASK 0x00ff0000
+#define CTX_IDENT_HOST 0x00010000
+#define CTX_IDENT_PLUGIN 0x00020000
+#define CTX_IDENT_PLUGIN_INSTANCE 0x00030000
+#define CTX_IDENT_TYPE 0x00040000
+#define CTX_IDENT_TYPE_INSTANCE 0x00050000
+
+struct gl_json_context_s
+{
+ uint32_t state;
+
+ graph_config_t *cfg;
+ graph_instance_t *inst;
+ graph_ident_t *ident;
+
+ _Bool dynamic_graph;
+};
+typedef struct gl_json_context_s gl_json_context_t;
+
+static void set_state (gl_json_context_t *ctx, /* {{{ */
+ uint32_t new_state, uint32_t mask)
+{
+ uint32_t old_state = ctx->state;
+ ctx->state = (old_state & ~mask) | (new_state & mask);
+} /* }}} void set_state */
+
+static int gl_json_string (void *user_data, /* {{{ */
+ const unsigned char *str,
+ unsigned int str_length)
+{
+ gl_json_context_t *ctx = user_data;
+ char buffer[str_length + 1];
+
+ memcpy (buffer, str, str_length);
+ buffer[str_length] = 0;
+
+ if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
+ || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
+ || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
+ {
+ switch (ctx->state & CTX_IDENT_MASK)
+ {
+ case CTX_IDENT_HOST:
+ ident_set_host (ctx->ident, buffer);
+ break;
+ case CTX_IDENT_PLUGIN:
+ ident_set_plugin (ctx->ident, buffer);
+ break;
+ case CTX_IDENT_PLUGIN_INSTANCE:
+ ident_set_plugin_instance (ctx->ident, buffer);
+ break;
+ case CTX_IDENT_TYPE:
+ ident_set_type (ctx->ident, buffer);
+ break;
+ case CTX_IDENT_TYPE_INSTANCE:
+ ident_set_type_instance (ctx->ident, buffer);
+ break;
+ }
+ }
+
+ return (1);
+} /* }}} int gl_json_string */
+
+static int gl_json_start_map (void *user_data) /* {{{ */
+{
+ gl_json_context_t *ctx = user_data;
+
+ if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
+ || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
+ || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
+ {
+ assert (ctx->ident == NULL);
+ ctx->ident = ident_create (ANY_TOKEN,
+ ANY_TOKEN, ANY_TOKEN,
+ ANY_TOKEN, ANY_TOKEN);
+ }
+
+ return (1);
+} /* }}} int gl_json_start_map */
+
+static int gl_json_end_map (void *user_data) /* {{{ */
+{
+ gl_json_context_t *ctx = user_data;
+
+ if ((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
+ {
+ size_t i;
+
+ /* ctx->ident should now hold the valid selector */
+ assert (ctx->cfg == NULL);
+ assert (ctx->inst == NULL);
+ assert (ctx->ident != NULL);
+
+ for (i = 0; i < gl_active_num; i++)
+ {
+ if (graph_compare (gl_active[i], ctx->ident) != 0)
+ continue;
+
+ ctx->cfg = gl_active[i];
+ ctx->dynamic_graph = 0;
+ break;
+ }
+
+ if (ctx->cfg == NULL)
+ {
+ ctx->cfg = graph_create (ctx->ident);
+ ctx->dynamic_graph = 1;
+ }
+
+ ident_destroy (ctx->ident);
+ ctx->ident = NULL;
+
+ set_state (ctx, CTX_GRAPH, CTX_MASK);
+ }
+ else if ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
+ {
+ /* ctx->ident should now hold the valid selector */
+ assert (ctx->cfg != NULL);
+ assert (ctx->inst == NULL);
+ assert (ctx->ident != NULL);
+
+ ctx->inst = inst_create (ctx->cfg, ctx->ident);
+ ident_destroy (ctx->ident);
+ ctx->ident = NULL;
+
+ set_state (ctx, CTX_INST, CTX_MASK);
+ }
+ else if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
+ {
+ /* ctx->ident should now hold the valid file */
+ assert (ctx->cfg != NULL);
+ assert (ctx->inst != NULL);
+ assert (ctx->ident != NULL);
+
+ inst_add_file (ctx->inst, ctx->ident);
+ ident_destroy (ctx->ident);
+ ctx->ident = NULL;
+
+ /* Don't reset the state here, files are in an array. */
+ }
+ else if ((ctx->state & CTX_MASK) == CTX_INST)
+ {
+ /* ctx->inst should now hold a complete instance */
+ assert (ctx->cfg != NULL);
+ assert (ctx->inst != NULL);
+ assert (ctx->ident == NULL);
+
+ graph_add_inst (ctx->cfg, ctx->inst);
+ /* don't destroy / free ctx->inst */
+ ctx->inst = NULL;
+
+ /* Don't reset the state here, instances are in an array. */
+ }
+ else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
+ {
+ /* ctx->cfg should now hold a complete graph */
+ assert (ctx->cfg != NULL);
+ assert (ctx->inst == NULL);
+ assert (ctx->ident == NULL);
+
+ if (ctx->dynamic_graph)
+ gl_add_graph_internal (ctx->cfg, &gl_dynamic, &gl_dynamic_num);
+ /* else: already contained in gl_active */
+ ctx->cfg = NULL;
+
+ /* Don't reset the state here, graphs are in an array. */
+ }
+
+ return (1);
+} /* }}} int gl_json_end_map */
+
+static int gl_json_end_array (void *user_data) /* {{{ */
+{
+ gl_json_context_t *ctx = user_data;
+
+ if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
+ set_state (ctx, CTX_INST, CTX_MASK);
+ else if ((ctx->state & CTX_MASK) == CTX_INST)
+ set_state (ctx, CTX_GRAPH, CTX_MASK);
+ else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
+ {
+ /* We're done */
+ }
+
+ return (1);
+} /* }}} int gl_json_end_array */
+
+static int gl_json_key (void *user_data, /* {{{ */
+ const unsigned char *str,
+ unsigned int str_length)
+{
+ gl_json_context_t *ctx = user_data;
+ char buffer[str_length + 1];
+
+ memcpy (buffer, str, str_length);
+ buffer[str_length] = 0;
+
+ if ((ctx->state & CTX_MASK) == CTX_GRAPH)
+ {
+ if (strcasecmp ("select", buffer) == 0)
+ set_state (ctx, CTX_GRAPH_SELECT, CTX_MASK);
+ else if (strcasecmp ("instances", buffer) == 0)
+ set_state (ctx, CTX_INST, CTX_MASK);
+ }
+ else if ((ctx->state & CTX_MASK) == CTX_INST)
+ {
+ if (strcasecmp ("select", buffer) == 0)
+ set_state (ctx, CTX_INST_SELECT, CTX_MASK);
+ else if (strcasecmp ("files", buffer) == 0)
+ set_state (ctx, CTX_INST_FILE, CTX_MASK);
+ }
+ else if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
+ || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
+ || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
+ {
+ assert (ctx->ident != NULL);
+
+ if (strcasecmp ("host", buffer) == 0)
+ set_state (ctx, CTX_IDENT_HOST, CTX_IDENT_MASK);
+ else if (strcasecmp ("plugin", buffer) == 0)
+ set_state (ctx, CTX_IDENT_PLUGIN, CTX_IDENT_MASK);
+ else if (strcasecmp ("plugin_instance", buffer) == 0)
+ set_state (ctx, CTX_IDENT_PLUGIN_INSTANCE, CTX_IDENT_MASK);
+ else if (strcasecmp ("type", buffer) == 0)
+ set_state (ctx, CTX_IDENT_TYPE, CTX_IDENT_MASK);
+ else if (strcasecmp ("type_instance", buffer) == 0)
+ set_state (ctx, CTX_IDENT_TYPE_INSTANCE, CTX_IDENT_MASK);
+ }
+
+ return (1);
+} /* }}} int gl_json_key */
+
+yajl_callbacks gl_json_callbacks =
+{
+ /* null = */ NULL,
+ /* boolean = */ NULL,
+ /* integer = */ NULL,
+ /* double = */ NULL,
+ /* number = */ NULL,
+ /* string = */ gl_json_string,
+ /* start_map = */ gl_json_start_map,
+ /* map_key = */ gl_json_key,
+ /* end_map = */ gl_json_end_map,
+ /* start_array = */ NULL,
+ /* end_array = */ gl_json_end_array
+};
+
+static int gl_read_cache (_Bool block) /* {{{ */
+{
+ yajl_handle handle;
+ gl_json_context_t context;
+ yajl_parser_config handle_config = { /* comments = */ 0, /* check UTF-8 */ 0 };
+
+ int fd;
+ int cmd;
+ struct stat statbuf;
+ struct flock lock;
+ int status;
+ time_t now;
+
+ fd = open (CACHE_FILE, O_RDONLY);
+ if (fd < 0)
+ {
+ fprintf (stderr, "gl_read_cache: open(2) failed with status %i\n", errno);
+ return (errno);
+ }
+
+ if (block)
+ cmd = F_SETLKW;
+ else
+ cmd = F_SETLK;
+
+ memset (&lock, 0, sizeof (lock));
+ lock.l_type = F_RDLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0;
+ lock.l_len = 0; /* lock everything */
+
+ while (42)
+ {
+ status = fcntl (fd, cmd, &lock);
+ if (status == 0)
+ break;
+
+ if (!block && ((errno == EACCES) || (errno == EAGAIN)))
+ {
+ close (fd);
+ return (0);
+ }
+
+ if (errno == EINTR)
+ continue;
+
+ status = errno;
+ fprintf (stderr, "gl_read_cache: fcntl(2) failed with status %i\n",
+ status);
+ close (fd);
+ return (status);
+ }
+
+ fprintf (stderr, "gl_read_cache: Opening and locking "
+ "cache file successful\n");
+
+ memset (&statbuf, 0, sizeof (statbuf));
+ status = fstat (fd, &statbuf);
+ if (status != 0)
+ {
+ status = errno;
+ fprintf (stderr, "gl_read_cache: fstat(2) failed with status %i\n",
+ status);
+ close (fd);
+ return (status);
+ }
+
+ now = time (NULL);
+
+ if (block)
+ {
+ /* Read the file. No excuses. */
+ }
+ else if (statbuf.st_mtime <= gl_last_update)
+ {
+ /* Our current data is at least as new as the cache. Return. */
+ fprintf (stderr, "gl_read_cache: Not using cache because "
+ "the internal data is newer\n");
+ fflush (stderr);
+ close (fd);
+ return (0);
+ }
+ else if ((statbuf.st_mtime + UPDATE_INTERVAL) < now)
+ {
+ /* We'll scan the directory anyway, so there is no need to parse the cache
+ * here. */
+ fprintf (stderr, "gl_read_cache: Not using cache because it's too old\n");
+ fflush (stderr);
+ close (fd);
+ return (0);
+ }
+
+ memset (&context, 0, sizeof (context));
+ context.state = CTX_GRAPH;
+ context.cfg = NULL;
+ context.inst = NULL;
+ context.ident = NULL;
+
+ handle = yajl_alloc (&gl_json_callbacks,
+ &handle_config,
+ /* alloc funcs = */ NULL,
+ &context);
+
+ fprintf (stderr, "gl_read_cache: Start parsing data\n");
+ fflush (stderr);
+
+ while (42)
+ {
+ ssize_t rd_status;
+ char buffer[1024*1024];
+
+ rd_status = read (fd, buffer, sizeof (buffer));
+ if (rd_status < 0)
+ {
+ if ((errno == EINTR) || (errno == EAGAIN))
+ continue;
+
+ status = errno;
+ fprintf (stderr, "gl_read_cache: read(2) failed with status %i\n",
+ status);
+ close (fd);
+ return (status);
+ }
+ else if (rd_status == 0)
+ {
+ yajl_parse_complete (handle);
+ break;
+ }
+ else
+ {
+ yajl_parse (handle,
+ (unsigned char *) &buffer[0],
+ (unsigned int) rd_status);
+ }
+ }
+
+ yajl_free (handle);
+
+ gl_last_update = statbuf.st_mtime;
+ close (fd);
+
+ fprintf (stderr, "gl_read_cache: Finished parsing data\n");
+ fflush (stderr);
+
+ return (0);
+} /* }}} int gl_read_cache */
+