src/graph_config.c: Use the correct config file …
[collection4.git] / src / graph_config.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <errno.h>
8
9 #include "graph_config.h"
10 #include "graph_list.h"
11 #include "oconfig.h"
12 #include "common.h"
13
14 #ifndef CONFIGFILE
15 # define CONFIGFILE "/etc/collection.conf"
16 #endif
17
18 time_t last_read_mtime = 0;
19
20 static int dispatch_config (const oconfig_item_t *ci) /* {{{ */
21 {
22   int i;
23
24   for (i = 0; i < ci->children_num; i++)
25   {
26     oconfig_item_t *child;
27
28     child = ci->children + i;
29     if (strcasecmp ("Graph", child->key) == 0)
30       graph_config_add (child);
31     else
32     {
33       DEBUG ("Unknown config option: %s", child->key);
34     }
35   }
36
37   return (0);
38 } /* }}} int dispatch_config */
39
40 static int internal_read_config (void) /* {{{ */
41 {
42   oconfig_item_t *ci;
43
44   ci = oconfig_parse_file (CONFIGFILE);
45   if (ci == NULL)
46     return (-1);
47
48   dispatch_config (ci);
49
50   oconfig_free (ci);
51
52   gl_config_submit ();
53
54   return (0);
55 } /* }}} int internal_read_config */
56
57 static time_t get_config_mtime (void) /* {{{ */
58 {
59   struct stat statbuf;
60   int status;
61
62   memset (&statbuf, 0, sizeof (statbuf));
63   status = stat (CONFIGFILE, &statbuf);
64   if (status != 0)
65     return (0);
66
67   return (statbuf.st_mtime);
68 } /* }}} time_t get_config_mtime */
69
70 int graph_read_config (void) /* {{{ */
71 {
72   time_t mtime;
73
74   mtime = get_config_mtime ();
75
76   if (mtime <= last_read_mtime)
77     return (0);
78
79   internal_read_config ();
80
81   last_read_mtime = mtime;
82
83   return (0);
84 } /* }}} int graph_read_config */
85
86 int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */
87     char **ret_str)
88 {
89   char *tmp;
90
91   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
92     return (EINVAL);
93
94   tmp = strdup (ci->values[0].value.string);
95   if (tmp == NULL)
96     return (ENOMEM);
97
98   free (*ret_str);
99   *ret_str = tmp;
100
101   return (0);
102 } /* }}} int graph_config_get_string */
103
104 int graph_config_get_bool (const oconfig_item_t *ci, /* {{{ */
105     _Bool *ret_bool)
106 {
107   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
108     return (EINVAL);
109
110   if (ci->values[0].value.boolean)
111     *ret_bool = 1;
112   else
113     *ret_bool = 0;
114
115   return (0);
116 } /* }}} int graph_config_get_bool */
117
118 /* vim: set sw=2 sts=2 et fdm=marker : */