src/graph_list.c: A first take at a JSON parser for reading back the cache file.
[collection4.git] / src / graph_list.c
1 /**
2  * collection4 - graph_list.c
3  * Copyright (C) 2010  Florian octo Forster
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <inttypes.h>
28 #include <string.h>
29 #include <time.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34
35 #include <yajl/yajl_parse.h>
36
37 #include "graph_list.h"
38 #include "common.h"
39 #include "filesystem.h"
40 #include "graph.h"
41 #include "graph_config.h"
42 #include "graph_def.h"
43 #include "graph_ident.h"
44 #include "graph_instance.h"
45 #include "utils_cgi.h"
46 #include "utils_search.h"
47
48 #include <fcgiapp.h>
49 #include <fcgi_stdio.h>
50
51 /*
52  * Defines
53  */
54 #define UPDATE_INTERVAL 900
55 #define CACHE_FILE "/tmp/collection4.json"
56
57 /*
58  * Global variables
59  */
60 static graph_config_t **gl_active = NULL;
61 static size_t gl_active_num = 0;
62
63 static graph_config_t **gl_staging = NULL;
64 static size_t gl_staging_num = 0;
65
66 /* Graphs created on-the-fly for files which don't match any existing graph
67  * definition. */
68 static graph_config_t **gl_dynamic = NULL;
69 static size_t gl_dynamic_num = 0;
70
71 static char **host_list = NULL;
72 static size_t host_list_len = 0;
73
74 static time_t gl_last_update = 0;
75
76 /*
77  * Private functions
78  */
79 static int gl_add_graph_internal (graph_config_t *cfg, /* {{{ */
80     graph_config_t ***gl_array, size_t *gl_array_num)
81 {
82   graph_config_t **tmp;
83
84 #define ARRAY_PTR  (*gl_array)
85 #define ARRAY_SIZE (*gl_array_num)
86
87   if (cfg == NULL)
88     return (EINVAL);
89
90   tmp = realloc (ARRAY_PTR, sizeof (*ARRAY_PTR) * (ARRAY_SIZE + 1));
91   if (tmp == NULL)
92     return (ENOMEM);
93   ARRAY_PTR = tmp;
94
95   ARRAY_PTR[ARRAY_SIZE] = cfg;
96   ARRAY_SIZE++;
97
98 #undef ARRAY_SIZE
99 #undef ARRAY_PTR
100
101   return (0);
102 } /* }}} int gl_add_graph_internal */
103
104 static void gl_destroy (graph_config_t ***gl_array, /* {{{ */
105     size_t *gl_array_num)
106 {
107   size_t i;
108
109   if ((gl_array == NULL) || (gl_array_num == NULL))
110     return;
111
112 #define ARRAY_PTR  (*gl_array)
113 #define ARRAY_SIZE (*gl_array_num)
114
115   for (i = 0; i < ARRAY_SIZE; i++)
116   {
117     graph_destroy (ARRAY_PTR[i]);
118     ARRAY_PTR[i] = NULL;
119   }
120   free (ARRAY_PTR);
121   ARRAY_PTR = NULL;
122   ARRAY_SIZE = 0;
123
124 #undef ARRAY_SIZE
125 #undef ARRAY_PTR
126 } /* }}} void gl_destroy */
127
128 static int gl_register_host (const char *host) /* {{{ */
129 {
130   char **tmp;
131   size_t i;
132
133   if (host == NULL)
134     return (EINVAL);
135
136   for (i = 0; i < host_list_len; i++)
137     if (strcmp (host_list[i], host) == 0)
138       return (0);
139
140   tmp = realloc (host_list, sizeof (*host_list) * (host_list_len + 1));
141   if (tmp == NULL)
142     return (ENOMEM);
143   host_list = tmp;
144
145   host_list[host_list_len] = strdup (host);
146   if (host_list[host_list_len] == NULL)
147     return (ENOMEM);
148
149   host_list_len++;
150   return (0);
151 } /* }}} int gl_register_host */
152
153 static int gl_clear_hosts (void) /* {{{ */
154 {
155   size_t i;
156
157   for (i = 0; i < host_list_len; i++)
158     free (host_list[i]);
159   free (host_list);
160
161   host_list = NULL;
162   host_list_len = 0;
163
164   return (0);
165 } /* }}} int gl_clear_hosts */
166
167 static int gl_compare_hosts (const void *v0, const void *v1) /* {{{ */
168 {
169   return (strcmp (*(char * const *) v0, *(char * const *) v1));
170 } /* }}} int gl_compare_hosts */
171
172 static int gl_register_file (const graph_ident_t *file, /* {{{ */
173     __attribute__((unused)) void *user_data)
174 {
175   graph_config_t *cfg;
176   int num_graphs = 0;
177   size_t i;
178
179   for (i = 0; i < gl_active_num; i++)
180   {
181     graph_config_t *cfg = gl_active[i];
182     int status;
183
184     if (!graph_ident_matches (cfg, file))
185       continue;
186
187     status = graph_add_file (cfg, file);
188     if (status != 0)
189     {
190       /* report error */;
191     }
192     else
193     {
194       num_graphs++;
195     }
196   }
197
198   if (num_graphs == 0)
199   {
200     cfg = graph_create (file);
201     gl_add_graph_internal (cfg, &gl_dynamic, &gl_dynamic_num);
202     graph_add_file (cfg, file);
203   }
204
205   gl_register_host (ident_get_host (file));
206
207   return (0);
208 } /* }}} int gl_register_file */
209
210 static const char *get_part_from_param (const char *prim_key, /* {{{ */
211     const char *sec_key)
212 {
213   const char *val;
214
215   val = param (prim_key);
216   if (val != NULL)
217     return (val);
218   
219   return (param (sec_key));
220 } /* }}} const char *get_part_from_param */
221
222 static int gl_clear_instances (void) /* {{{ */
223 {
224   size_t i;
225
226   for (i = 0; i < gl_active_num; i++)
227     graph_clear_instances (gl_active[i]);
228
229   return (0);
230 } /* }}} int gl_clear_instances */
231
232 static void gl_dump_cb (void *ctx, /* {{{ */
233     const char *str, unsigned int len)
234 {
235   FILE *fh = ctx;
236
237   /* FIXME: Has everything been written? */
238   fwrite ((void *) str, /* size = */ 1, /* nmemb = */ len, fh);
239 } /* }}} void gl_dump_cb */
240
241 static int gl_dump (void) /* {{{ */
242 {
243   FILE *fh;
244   yajl_gen handler;
245   yajl_gen_config handler_config = { /* pretty = */ 1, /* indent = */ "  " };
246   size_t i;
247
248   /* FIXME: Lock the file */
249   fh = fopen (CACHE_FILE, "w");
250   if (fh == NULL)
251     return (errno);
252
253   handler = yajl_gen_alloc2 (gl_dump_cb, &handler_config,
254       /* alloc funcs = */ NULL, /* ctx = */ fh);
255   if (handler == NULL)
256   {
257     fclose (fh);
258     return (-1);
259   }
260
261   yajl_gen_array_open (handler);
262
263   for (i = 0; i < gl_active_num; i++)
264     graph_to_json (gl_active[i], handler);
265
266   for (i = 0; i < gl_dynamic_num; i++)
267     graph_to_json (gl_dynamic[i], handler);
268
269   yajl_gen_array_close (handler);
270
271   yajl_gen_free (handler);
272   fclose (fh);
273
274   return (0);
275 } /* }}} int gl_dump */
276
277 /*
278  * JSON parsing functions
279  */
280 #define CTX_MASK                  0xff000000
281 #define CTX_GRAPH                 0x01000000
282 #define CTX_GRAPH_SELECT          0x02000000
283 #define CTX_INST                  0x03000000
284 #define CTX_INST_SELECT           0x04000000
285 #define CTX_INST_FILE             0x05000000
286
287 #define CTX_IDENT_MASK            0x00ff0000
288 #define CTX_IDENT_HOST            0x00010000
289 #define CTX_IDENT_PLUGIN          0x00020000
290 #define CTX_IDENT_PLUGIN_INSTANCE 0x00030000
291 #define CTX_IDENT_TYPE            0x00040000
292 #define CTX_IDENT_TYPE_INSTANCE   0x00050000
293
294 struct gl_json_context_s
295 {
296   uint32_t          state;
297
298   graph_config_t   *cfg;
299   graph_instance_t *inst;
300   graph_ident_t    *ident;
301
302   _Bool             dynamic_graph;
303 };
304 typedef struct gl_json_context_s gl_json_context_t;
305
306 static void set_state (gl_json_context_t *ctx, /* {{{ */
307     uint32_t new_state, uint32_t mask)
308 {
309   uint32_t old_state = ctx->state;
310   ctx->state = (old_state & ~mask) | (new_state & mask);
311 } /* }}} void set_state */
312
313 static int gl_json_string (void *user_data, /* {{{ */
314     const unsigned char *str,
315     unsigned int str_length)
316 {
317   gl_json_context_t *ctx = user_data;
318   char buffer[str_length + 1];
319
320   memcpy (buffer, str, str_length);
321   buffer[str_length] = 0;
322
323   if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
324       || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
325       || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
326   {
327     switch (ctx->state & CTX_IDENT_MASK)
328     {
329       case CTX_IDENT_HOST:
330         ident_set_host (ctx->ident, buffer);
331         break;
332       case CTX_IDENT_PLUGIN:
333         ident_set_plugin (ctx->ident, buffer);
334         break;
335       case CTX_IDENT_PLUGIN_INSTANCE:
336         ident_set_plugin_instance (ctx->ident, buffer);
337         break;
338       case CTX_IDENT_TYPE:
339         ident_set_type (ctx->ident, buffer);
340         break;
341       case CTX_IDENT_TYPE_INSTANCE:
342         ident_set_type_instance (ctx->ident, buffer);
343         break;
344     }
345   }
346
347   return (1);
348 } /* }}} int gl_json_string */
349
350 static int gl_json_end_map (void *user_data) /* {{{ */
351 {
352   gl_json_context_t *ctx = user_data;
353
354   if ((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
355   {
356     size_t i;
357
358     /* ctx->ident should now hold the valid selector */
359     assert (ctx->cfg == NULL);
360     assert (ctx->inst  == NULL);
361     assert (ctx->ident != NULL);
362
363     for (i = 0; i < gl_active_num; i++)
364     {
365       if (graph_compare (gl_active[i], ctx->ident) != 0)
366         continue;
367
368       ctx->cfg = gl_active[i];
369       ctx->dynamic_graph = 0;
370       break;
371     }
372
373     if (ctx->cfg == NULL)
374     {
375       ctx->cfg = graph_create (ctx->ident);
376       ctx->dynamic_graph = 1;
377     }
378
379     ident_destroy (ctx->ident);
380     ctx->ident = NULL;
381
382     set_state (ctx, CTX_GRAPH, CTX_MASK);
383   }
384   else if ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
385   {
386     /* ctx->ident should now hold the valid selector */
387     assert (ctx->cfg   != NULL);
388     assert (ctx->inst  == NULL);
389     assert (ctx->ident != NULL);
390
391     ctx->inst = inst_create (ctx->cfg, ctx->ident);
392     ident_destroy (ctx->ident);
393     ctx->ident = NULL;
394
395     set_state (ctx, CTX_INST, CTX_MASK);
396   }
397   else if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
398   {
399     /* ctx->ident should now hold the valid file */
400     assert (ctx->cfg   != NULL);
401     assert (ctx->inst  != NULL);
402     assert (ctx->ident != NULL);
403
404     inst_add_file (ctx->inst, ctx->ident);
405     ident_destroy (ctx->ident);
406     ctx->ident = NULL;
407
408     /* Don't reset the state here, files are in an array. */
409   }
410   else if ((ctx->state & CTX_MASK) == CTX_INST)
411   {
412     /* ctx->inst should now hold a complete instance */
413     assert (ctx->cfg   != NULL);
414     assert (ctx->inst  != NULL);
415     assert (ctx->ident == NULL);
416
417     graph_add_inst (ctx->cfg, ctx->inst);
418     /* don't destroy / free ctx->inst */
419     ctx->inst = NULL;
420
421     /* Don't reset the state here, instances are in an array. */
422   }
423   else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
424   {
425     /* ctx->cfg should now hold a complete graph */
426     assert (ctx->cfg   != NULL);
427     assert (ctx->inst  == NULL);
428     assert (ctx->ident == NULL);
429
430     if (ctx->dynamic_graph)
431       gl_add_graph_internal (ctx->cfg, &gl_dynamic, &gl_dynamic_num);
432     /* else: already contained in gl_active */
433     ctx->cfg = NULL;
434
435     /* Don't reset the state here, graphs are in an array. */
436   }
437
438   return (1);
439 } /* }}} int gl_json_end_map */
440
441 static int gl_json_end_array (void *user_data) /* {{{ */
442 {
443   gl_json_context_t *ctx = user_data;
444
445   if ((ctx->state & CTX_MASK) == CTX_INST_FILE)
446     set_state (ctx, CTX_INST, CTX_MASK);
447   else if ((ctx->state & CTX_MASK) == CTX_INST)
448     set_state (ctx, CTX_GRAPH, CTX_MASK);
449   else if ((ctx->state & CTX_MASK) == CTX_GRAPH)
450   {
451     /* We're done */
452   }
453
454   return (1);
455 } /* }}} int gl_json_end_array */
456
457 static int gl_json_key (void *user_data, /* {{{ */
458     const unsigned char *str,
459     unsigned int str_length)
460 {
461   gl_json_context_t *ctx = user_data;
462   char buffer[str_length + 1];
463
464   memcpy (buffer, str, str_length);
465   buffer[str_length] = 0;
466
467   if ((ctx->state & CTX_MASK) == CTX_GRAPH)
468   {
469     if (strcasecmp ("select", buffer) == 0)
470       set_state (ctx, CTX_GRAPH_SELECT, CTX_MASK);
471     else if (strcasecmp ("instances", buffer) == 0)
472       set_state (ctx, CTX_INST, CTX_MASK);
473   }
474   else if ((ctx->state & CTX_MASK) == CTX_INST)
475   {
476     if (strcasecmp ("select", buffer) == 0)
477       set_state (ctx, CTX_INST_SELECT, CTX_MASK);
478     else if (strcasecmp ("files", buffer) == 0)
479       set_state (ctx, CTX_INST_FILE, CTX_MASK);
480   }
481   else if (((ctx->state & CTX_MASK) == CTX_GRAPH_SELECT)
482       || ((ctx->state & CTX_MASK) == CTX_INST_SELECT)
483       || ((ctx->state & CTX_MASK) == CTX_INST_FILE))
484   {
485     if (strcasecmp ("host", buffer) == 0)
486       set_state (ctx, CTX_IDENT_HOST, CTX_IDENT_MASK);
487     else if (strcasecmp ("plugin", buffer) == 0)
488       set_state (ctx, CTX_IDENT_PLUGIN, CTX_IDENT_MASK);
489     else if (strcasecmp ("plugin_instance", buffer) == 0)
490       set_state (ctx, CTX_IDENT_PLUGIN_INSTANCE, CTX_IDENT_MASK);
491     else if (strcasecmp ("type", buffer) == 0)
492       set_state (ctx, CTX_IDENT_TYPE, CTX_IDENT_MASK);
493     else if (strcasecmp ("type_instance", buffer) == 0)
494       set_state (ctx, CTX_IDENT_TYPE_INSTANCE, CTX_IDENT_MASK);
495   }
496
497   return (0);
498 } /* }}} int gl_json_key */
499
500 yajl_callbacks gl_json_callbacks =
501 {
502   /*        null = */ NULL,
503   /*     boolean = */ NULL,
504   /*     integer = */ NULL,
505   /*      double = */ NULL,
506   /*      number = */ NULL,
507   /*      string = */ gl_json_string,
508   /*   start_map = */ NULL,
509   /*     map_key = */ gl_json_key,
510   /*     end_map = */ gl_json_end_map,
511   /* start_array = */ NULL,
512   /*   end_array = */ gl_json_end_array
513 };
514
515 static int gl_read_cache (_Bool block) /* {{{ */
516 {
517   yajl_handle handle;
518   gl_json_context_t context;
519   yajl_parser_config handle_config = { /* comments = */ 0, /* check UTF-8 */ 0 };
520
521   int fd;
522   int cmd;
523   struct flock lock;
524   int status;
525
526   fd = open (CACHE_FILE, O_RDONLY);
527   if (fd < 0)
528   {
529     fprintf (stderr, "gl_read_cache: open(2) failed with status %i\n", errno);
530     return (errno);
531   }
532
533   if (block)
534     cmd = F_SETLKW;
535   else
536     cmd = F_SETLK;
537
538   memset (&lock, 0, sizeof (lock));
539   lock.l_type = F_RDLCK;
540   lock.l_whence = SEEK_SET;
541   lock.l_start = 0;
542   lock.l_len = 0; /* lock everything */
543
544   while (42)
545   {
546     status = fcntl (fd, cmd, &lock);
547     if (status == 0)
548       break;
549
550     if (!block && ((errno == EACCES) || (errno == EAGAIN)))
551     {
552       close (fd);
553       return (0);
554     }
555
556     if (errno == EINTR)
557       continue;
558
559     fprintf (stderr, "gl_read_cache: fcntl(2) failed with status %i\n",
560         errno);
561     return (errno);
562   }
563
564   memset (&context, 0, sizeof (context));
565   context.cfg = NULL;
566   context.inst = NULL;
567   context.ident = NULL;
568
569   handle = yajl_alloc (&gl_json_callbacks,
570       &handle_config,
571       /* alloc funcs = */ NULL,
572       &context);
573
574   close (fd);
575
576 } /* }}} int gl_read_cache */
577
578 /*
579  * Global functions
580  */
581 int gl_add_graph (graph_config_t *cfg) /* {{{ */
582 {
583   return (gl_add_graph_internal (cfg, &gl_staging, &gl_staging_num));
584 } /* }}} int gl_add_graph */
585
586 int gl_config_submit (void) /* {{{ */
587 {
588   graph_config_t **old;
589   size_t old_num;
590
591   old = gl_active;
592   old_num = gl_active_num;
593
594   gl_active = gl_staging;
595   gl_active_num = gl_staging_num;
596
597   gl_staging = NULL;
598   gl_staging_num = 0;
599
600   gl_destroy (&old, &old_num);
601
602   return (0);
603 } /* }}} int graph_config_submit */
604
605 int gl_graph_get_all (graph_callback_t callback, /* {{{ */
606     void *user_data)
607 {
608   size_t i;
609
610   if (callback == NULL)
611     return (EINVAL);
612
613   gl_update ();
614
615   for (i = 0; i < gl_active_num; i++)
616   {
617     int status;
618
619     status = (*callback) (gl_active[i], user_data);
620     if (status != 0)
621       return (status);
622   }
623
624   for (i = 0; i < gl_dynamic_num; i++)
625   {
626     int status;
627
628     status = (*callback) (gl_dynamic[i], user_data);
629     if (status != 0)
630       return (status);
631   }
632
633   return (0);
634 } /* }}} int gl_graph_get_all */
635
636 graph_config_t *gl_graph_get_selected (void) /* {{{ */
637 {
638   const char *host = get_part_from_param ("graph_host", "host");
639   const char *plugin = get_part_from_param ("graph_plugin", "plugin");
640   const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
641   const char *type = get_part_from_param ("graph_type", "type");
642   const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
643   graph_ident_t *ident;
644   size_t i;
645
646   if ((host == NULL)
647       || (plugin == NULL) || (plugin_instance == NULL)
648       || (type == NULL) || (type_instance == NULL))
649     return (NULL);
650
651   ident = ident_create (host, plugin, plugin_instance, type, type_instance);
652
653   gl_update ();
654
655   for (i = 0; i < gl_active_num; i++)
656   {
657     if (graph_compare (gl_active[i], ident) != 0)
658       continue;
659
660     ident_destroy (ident);
661     return (gl_active[i]);
662   }
663
664   for (i = 0; i < gl_dynamic_num; i++)
665   {
666     if (graph_compare (gl_dynamic[i], ident) != 0)
667       continue;
668
669     ident_destroy (ident);
670     return (gl_dynamic[i]);
671   }
672
673   ident_destroy (ident);
674   return (NULL);
675 } /* }}} graph_config_t *gl_graph_get_selected */
676
677 /* gl_instance_get_all, gl_graph_instance_get_all {{{ */
678 struct gl_inst_callback_data /* {{{ */
679 {
680   graph_config_t *cfg;
681   graph_inst_callback_t callback;
682   void *user_data;
683 }; /* }}} struct gl_inst_callback_data */
684
685 static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
686     void *user_data)
687 {
688   struct gl_inst_callback_data *data = user_data;
689
690   return ((*data->callback) (data->cfg, inst, data->user_data));
691 } /* }}} int gl_inst_callback_handler */
692
693 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
694     graph_inst_callback_t callback, void *user_data)
695 {
696   struct gl_inst_callback_data data =
697   {
698     cfg,
699     callback,
700     user_data
701   };
702
703   if ((cfg == NULL) || (callback == NULL))
704     return (EINVAL);
705
706   return (graph_inst_foreach (cfg, gl_inst_callback_handler, &data));
707 } /* }}} int gl_graph_instance_get_all */
708
709 int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
710     void *user_data)
711 {
712   size_t i;
713
714   gl_update ();
715
716   for (i = 0; i < gl_active_num; i++)
717   {
718     int status;
719
720     status = gl_graph_instance_get_all (gl_active[i], callback, user_data);
721     if (status != 0)
722       return (status);
723   }
724
725   for (i = 0; i < gl_dynamic_num; i++)
726   {
727     int status;
728
729     status = gl_graph_instance_get_all (gl_dynamic[i], callback, user_data);
730     if (status != 0)
731       return (status);
732   }
733
734   return (0);
735 } /* }}} int gl_instance_get_all */
736 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
737
738 int gl_search (search_info_t *si, /* {{{ */
739     graph_inst_callback_t callback, void *user_data)
740 {
741   size_t i;
742   graph_ident_t *ident;
743
744   if ((si == NULL) || (callback == NULL))
745     return (EINVAL);
746
747   if (search_has_selector (si))
748   {
749     ident = search_to_ident (si);
750     if (ident == NULL)
751     {
752       fprintf (stderr, "gl_search: search_to_ident failed\n");
753       return (-1);
754     }
755   }
756   else
757   {
758     ident = NULL;
759   }
760
761   for (i = 0; i < gl_active_num; i++)
762   {
763     int status;
764
765     if ((ident != NULL) && !graph_ident_intersect (gl_active[i], ident))
766       continue;
767
768     status = graph_search_inst (gl_active[i], si,
769         /* callback  = */ callback,
770         /* user data = */ user_data);
771     if (status != 0)
772       return (status);
773   }
774
775   for (i = 0; i < gl_dynamic_num; i++)
776   {
777     int status;
778
779     if ((ident != NULL) && !graph_ident_intersect (gl_dynamic[i], ident))
780       continue;
781
782     status = graph_search_inst (gl_dynamic[i], si,
783         /* callback  = */ callback,
784         /* user data = */ user_data);
785     if (status != 0)
786       return (status);
787   }
788
789   return (0);
790 } /* }}} int gl_search */
791
792 int gl_search_string (const char *term, graph_inst_callback_t callback, /* {{{ */
793     void *user_data)
794 {
795   size_t i;
796
797   for (i = 0; i < gl_active_num; i++)
798   {
799     int status;
800
801     status = graph_search_inst_string (gl_active[i], term,
802         /* callback  = */ callback,
803         /* user data = */ user_data);
804     if (status != 0)
805       return (status);
806   }
807
808   for (i = 0; i < gl_dynamic_num; i++)
809   {
810     int status;
811
812     status = graph_search_inst_string (gl_dynamic[i], term,
813         /* callback  = */ callback,
814         /* user data = */ user_data);
815     if (status != 0)
816       return (status);
817   }
818
819   return (0);
820 } /* }}} int gl_search_string */
821
822 int gl_search_field (graph_ident_field_t field, /* {{{ */
823     const char *field_value,
824     graph_inst_callback_t callback, void *user_data)
825 {
826   size_t i;
827
828   if ((field_value == NULL) || (callback == NULL))
829     return (EINVAL);
830
831   for (i = 0; i < gl_active_num; i++)
832   {
833     int status;
834
835     status = graph_inst_search_field (gl_active[i],
836         field, field_value,
837         /* callback  = */ callback,
838         /* user data = */ user_data);
839     if (status != 0)
840       return (status);
841   }
842
843   for (i = 0; i < gl_dynamic_num; i++)
844   {
845     int status;
846
847     status = graph_inst_search_field (gl_dynamic[i],
848         field, field_value,
849         /* callback  = */ callback,
850         /* user data = */ user_data);
851     if (status != 0)
852       return (status);
853   }
854
855   return (0);
856 } /* }}} int gl_search_field */
857
858 int gl_foreach_host (int (*callback) (const char *host, void *user_data), /* {{{ */
859     void *user_data)
860 {
861   int status;
862   size_t i;
863
864   for (i = 0; i < host_list_len; i++)
865   {
866     status = (*callback) (host_list[i], user_data);
867     if (status != 0)
868       return (status);
869   }
870
871   return (0);
872 } /* }}} int gl_foreach_host */
873
874 int gl_update (void) /* {{{ */
875 {
876   time_t now;
877   int status;
878   size_t i;
879
880   /*
881   printf ("Content-Type: text/plain\n\n");
882   */
883
884   now = time (NULL);
885
886   if ((gl_last_update + UPDATE_INTERVAL) >= now)
887     return (0);
888
889   /* Clear state */
890   gl_clear_instances ();
891   gl_clear_hosts ();
892   gl_destroy (&gl_dynamic, &gl_dynamic_num);
893
894   graph_read_config ();
895
896   status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL);
897
898   if (host_list_len > 0)
899     qsort (host_list, host_list_len, sizeof (*host_list),
900         gl_compare_hosts);
901
902   gl_last_update = now;
903
904   for (i = 0; i < gl_active_num; i++)
905     graph_sort_instances (gl_active[i]);
906
907   gl_dump ();
908
909   return (status);
910 } /* }}} int gl_update */
911
912 /* vim: set sw=2 sts=2 et fdm=marker : */