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