Add _XOPEN_SOURCE=600 to all .c files with standards compliance defines.
[collectd.git] / src / target_set.c
1 /**
2  * collectd - src/target_set.c
3  * Copyright (C) 2008  Florian Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian Forster <octo at verplant.org>
20  **/
21
22 /*
23  * First tell the compiler to stick to the C99 and POSIX standards as close as
24  * possible.
25  */
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
28 #endif
29
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
32 #endif
33
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
36 #endif
37 #define _POSIX_C_SOURCE 200112L
38
39 #ifdef _XOPEN_SOURCE
40 # undef _XOPEN_SOURCE
41 #endif
42 #define _XOPEN_SOURCE 600
43
44 #ifndef _REENTRANT
45 # define _REENTRANT
46 #endif
47
48 #ifndef _THREAD_SAFE
49 # define _THREAD_SAFE
50 #endif
51
52 #ifdef _GNU_SOURCE
53 # undef _GNU_SOURCE
54 #endif
55 /* }}} */
56
57 #include "collectd.h"
58 #include "common.h"
59 #include "filter_chain.h"
60
61 struct ts_data_s
62 {
63   char *host;
64   char *plugin;
65   char *plugin_instance;
66   /* char *type; */
67   char *type_instance;
68 };
69 typedef struct ts_data_s ts_data_t;
70
71 static char *ts_strdup (const char *orig) /* {{{ */
72 {
73   size_t sz;
74   char *dest;
75
76   if (orig == NULL)
77     return (NULL);
78
79   sz = strlen (orig) + 1;
80   dest = (char *) malloc (sz);
81   if (dest == NULL)
82     return (NULL);
83
84   memcpy (dest, orig, sz);
85
86   return (dest);
87 } /* }}} char *ts_strdup */
88
89 static int ts_config_add_string (char **dest, /* {{{ */
90     const oconfig_item_t *ci, int may_be_empty)
91 {
92   char *temp;
93
94   if (dest == NULL)
95     return (-EINVAL);
96
97   if ((ci->values_num != 1)
98       || (ci->values[0].type != OCONFIG_TYPE_STRING))
99   {
100     ERROR ("Target `set': The `%s' option requires exactly one string "
101         "argument.", ci->key);
102     return (-1);
103   }
104
105   if ((!may_be_empty) && (ci->values[0].value.string[0] == 0))
106   {
107     ERROR ("Target `set': The `%s' option does not accept empty strings.",
108         ci->key);
109     return (-1);
110   }
111
112   temp = ts_strdup (ci->values[0].value.string);
113   if (temp == NULL)
114   {
115     ERROR ("ts_config_add_string: ts_strdup failed.");
116     return (-1);
117   }
118
119   free (*dest);
120   *dest = temp;
121
122   return (0);
123 } /* }}} int ts_config_add_string */
124
125 static int ts_destroy (void **user_data) /* {{{ */
126 {
127   ts_data_t *data;
128
129   if (user_data == NULL)
130     return (-EINVAL);
131
132   data = *user_data;
133   if (data == NULL)
134     return (0);
135
136   free (data->host);
137   free (data->plugin);
138   free (data->plugin_instance);
139   /* free (data->type); */
140   free (data->type_instance);
141   free (data);
142
143   return (0);
144 } /* }}} int ts_destroy */
145
146 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
147 {
148   ts_data_t *data;
149   int status;
150   int i;
151
152   data = (ts_data_t *) malloc (sizeof (*data));
153   if (data == NULL)
154   {
155     ERROR ("ts_create: malloc failed.");
156     return (-ENOMEM);
157   }
158   memset (data, 0, sizeof (*data));
159
160   data->host = NULL;
161   data->plugin = NULL;
162   data->plugin_instance = NULL;
163   /* data->type = NULL; */
164   data->type_instance = NULL;
165
166   status = 0;
167   for (i = 0; i < ci->children_num; i++)
168   {
169     oconfig_item_t *child = ci->children + i;
170
171     if ((strcasecmp ("Host", child->key) == 0)
172         || (strcasecmp ("Hostname", child->key) == 0))
173       status = ts_config_add_string (&data->host, child,
174           /* may be empty = */ 0);
175     else if (strcasecmp ("Plugin", child->key) == 0)
176       status = ts_config_add_string (&data->plugin, child,
177           /* may be empty = */ 0);
178     else if (strcasecmp ("PluginInstance", child->key) == 0)
179       status = ts_config_add_string (&data->plugin_instance, child,
180           /* may be empty = */ 1);
181 #if 0
182     else if (strcasecmp ("Type", child->key) == 0)
183       status = ts_config_add_string (&data->type, child,
184           /* may be empty = */ 0);
185 #endif
186     else if (strcasecmp ("TypeInstance", child->key) == 0)
187       status = ts_config_add_string (&data->type_instance, child,
188           /* may be empty = */ 1);
189     else
190     {
191       ERROR ("Target `set': The `%s' configuration option is not understood "
192           "and will be ignored.", child->key);
193       status = 0;
194     }
195
196     if (status != 0)
197       break;
198   }
199
200   /* Additional sanity-checking */
201   while (status == 0)
202   {
203     if ((data->host == NULL)
204         && (data->plugin == NULL)
205         && (data->plugin_instance == NULL)
206         /* && (data->type == NULL) */
207         && (data->type_instance == NULL))
208     {
209       ERROR ("Target `set': You need to set at lease one of `Host', "
210           "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
211       status = -1;
212     }
213
214     break;
215   }
216
217   if (status != 0)
218   {
219     ts_destroy ((void *) &data);
220     return (status);
221   }
222
223   *user_data = data;
224   return (0);
225 } /* }}} int ts_create */
226
227 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
228     notification_meta_t __attribute__((unused)) **meta, void **user_data)
229 {
230   ts_data_t *data;
231
232   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
233     return (-EINVAL);
234
235   data = *user_data;
236   if (data == NULL)
237   {
238     ERROR ("Target `set': Invoke: `data' is NULL.");
239     return (-EINVAL);
240   }
241
242 #define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
243   SET_FIELD (host);
244   SET_FIELD (plugin);
245   SET_FIELD (plugin_instance);
246   /* SET_FIELD (type); */
247   SET_FIELD (type_instance);
248
249   return (FC_TARGET_CONTINUE);
250 } /* }}} int ts_invoke */
251
252 void module_register (void)
253 {
254         target_proc_t tproc;
255
256         memset (&tproc, 0, sizeof (tproc));
257         tproc.create  = ts_create;
258         tproc.destroy = ts_destroy;
259         tproc.invoke  = ts_invoke;
260         fc_register_target ("set", tproc);
261 } /* module_register */
262
263 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
264