Add _XOPEN_SOURCE=600 to all .c files with standards compliance defines.
[collectd.git] / src / target_replace.c
1 /**
2  * collectd - src/target_replace.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 #include "utils_subst.h"
61
62 #include <regex.h>
63
64 struct tr_action_s;
65 typedef struct tr_action_s tr_action_t;
66 struct tr_action_s
67 {
68   regex_t re;
69   char *replacement;
70   int may_be_empty;
71
72   tr_action_t *next;
73 };
74
75 struct tr_data_s
76 {
77   tr_action_t *host;
78   tr_action_t *plugin;
79   tr_action_t *plugin_instance;
80   /* tr_action_t *type; */
81   tr_action_t *type_instance;
82 };
83 typedef struct tr_data_s tr_data_t;
84
85 static char *tr_strdup (const char *orig) /* {{{ */
86 {
87   size_t sz;
88   char *dest;
89
90   if (orig == NULL)
91     return (NULL);
92
93   sz = strlen (orig) + 1;
94   dest = (char *) malloc (sz);
95   if (dest == NULL)
96     return (NULL);
97
98   memcpy (dest, orig, sz);
99
100   return (dest);
101 } /* }}} char *tr_strdup */
102
103 static void tr_action_destroy (tr_action_t *act) /* {{{ */
104 {
105   if (act == NULL)
106     return;
107
108   regfree (&act->re);
109   sfree (act->replacement);
110
111   if (act->next != NULL)
112     tr_action_destroy (act->next);
113
114   sfree (act);
115 } /* }}} void tr_action_destroy */
116
117 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
118     const oconfig_item_t *ci, int may_be_empty)
119 {
120   tr_action_t *act;
121   int status;
122
123   if (dest == NULL)
124     return (-EINVAL);
125
126   if ((ci->values_num != 2)
127       || (ci->values[0].type != OCONFIG_TYPE_STRING)
128       || (ci->values[1].type != OCONFIG_TYPE_STRING))
129   {
130     ERROR ("Target `replace': The `%s' option requires exactly two string "
131         "arguments.", ci->key);
132     return (-1);
133   }
134
135   act = (tr_action_t *) malloc (sizeof (*act));
136   if (act == NULL)
137   {
138     ERROR ("tr_config_add_action: malloc failed.");
139     return (-ENOMEM);
140   }
141   memset (act, 0, sizeof (*act));
142
143   act->replacement = NULL;
144   act->may_be_empty = may_be_empty;
145
146   status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
147   if (status != 0)
148   {
149     char errbuf[1024] = "";
150
151     /* regerror assures null termination. */
152     regerror (status, &act->re, errbuf, sizeof (errbuf));
153     ERROR ("Target `replace': Compiling the regular expression `%s' "
154         "failed: %s.",
155         ci->values[0].value.string, errbuf);
156     sfree (act);
157     return (-EINVAL);
158   }
159
160   act->replacement = tr_strdup (ci->values[1].value.string);
161   if (act->replacement == NULL)
162   {
163     ERROR ("tr_config_add_action: tr_strdup failed.");
164     regfree (&act->re);
165     sfree (act);
166     return (-ENOMEM);
167   }
168
169   /* Insert action at end of list. */
170   if (*dest == NULL)
171     *dest = act;
172   else
173   {
174     tr_action_t *prev;
175
176     prev = *dest;
177     while (prev->next != NULL)
178       prev = prev->next;
179
180     prev->next = act;
181   }
182
183   return (0);
184 } /* }}} int tr_config_add_action */
185
186 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
187     char *buffer_in, size_t buffer_in_size, int may_be_empty)
188 {
189   tr_action_t *act;
190   int status;
191   char buffer[DATA_MAX_NAME_LEN];
192   regmatch_t matches[8];
193
194   if (act_head == NULL)
195     return (-EINVAL);
196
197   sstrncpy (buffer, buffer_in, sizeof (buffer));
198   memset (matches, 0, sizeof (matches));
199
200   DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
201
202   for (act = act_head; act != NULL; act = act->next)
203   {
204     char temp[DATA_MAX_NAME_LEN];
205     char *subst_status;
206
207     status = regexec (&act->re, buffer,
208         STATIC_ARRAY_SIZE (matches), matches,
209         /* flags = */ 0);
210     if (status == REG_NOMATCH)
211       continue;
212     else if (status != 0)
213     {
214       char errbuf[1024] = "";
215
216       regerror (status, &act->re, errbuf, sizeof (errbuf));
217       ERROR ("Target `replace': Executing a regular expression failed: %s.",
218           errbuf);
219       continue;
220     }
221
222     subst_status = subst (temp, sizeof (temp), buffer,
223         matches[0].rm_so, matches[0].rm_eo, act->replacement);
224     if (subst_status == NULL)
225     {
226       ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
227           "replacement = %s) failed.",
228           buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
229           act->replacement);
230       continue;
231     }
232     sstrncpy (buffer, temp, sizeof (buffer));
233
234     DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
235   } /* for (act = act_head; act != NULL; act = act->next) */
236
237   if ((may_be_empty == 0) && (buffer[0] == 0))
238   {
239     WARNING ("Target `replace': Replacement resulted in an empty string, "
240         "which is not allowed for this buffer (`host' or `plugin').");
241     return (0);
242   }
243
244   DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
245   sstrncpy (buffer_in, buffer, buffer_in_size);
246
247   return (0);
248 } /* }}} int tr_action_invoke */
249
250 static int tr_destroy (void **user_data) /* {{{ */
251 {
252   tr_data_t *data;
253
254   if (user_data == NULL)
255     return (-EINVAL);
256
257   data = *user_data;
258   if (data == NULL)
259     return (0);
260
261   tr_action_destroy (data->host);
262   tr_action_destroy (data->plugin);
263   tr_action_destroy (data->plugin_instance);
264   /* tr_action_destroy (data->type); */
265   tr_action_destroy (data->type_instance);
266   sfree (data);
267
268   return (0);
269 } /* }}} int tr_destroy */
270
271 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
272 {
273   tr_data_t *data;
274   int status;
275   int i;
276
277   data = (tr_data_t *) malloc (sizeof (*data));
278   if (data == NULL)
279   {
280     ERROR ("tr_create: malloc failed.");
281     return (-ENOMEM);
282   }
283   memset (data, 0, sizeof (*data));
284
285   data->host = NULL;
286   data->plugin = NULL;
287   data->plugin_instance = NULL;
288   /* data->type = NULL; */
289   data->type_instance = NULL;
290
291   status = 0;
292   for (i = 0; i < ci->children_num; i++)
293   {
294     oconfig_item_t *child = ci->children + i;
295
296     if ((strcasecmp ("Host", child->key) == 0)
297         || (strcasecmp ("Hostname", child->key) == 0))
298       status = tr_config_add_action (&data->host, child,
299           /* may be empty = */ 0);
300     else if (strcasecmp ("Plugin", child->key) == 0)
301       status = tr_config_add_action (&data->plugin, child,
302           /* may be empty = */ 0);
303     else if (strcasecmp ("PluginInstance", child->key) == 0)
304       status = tr_config_add_action (&data->plugin_instance, child,
305           /* may be empty = */ 1);
306 #if 0
307     else if (strcasecmp ("Type", child->key) == 0)
308       status = tr_config_add_action (&data->type, child,
309           /* may be empty = */ 0);
310 #endif
311     else if (strcasecmp ("TypeInstance", child->key) == 0)
312       status = tr_config_add_action (&data->type_instance, child,
313           /* may be empty = */ 1);
314     else
315     {
316       ERROR ("Target `replace': The `%s' configuration option is not understood "
317           "and will be ignored.", child->key);
318       status = 0;
319     }
320
321     if (status != 0)
322       break;
323   }
324
325   /* Additional sanity-checking */
326   while (status == 0)
327   {
328     if ((data->host == NULL)
329         && (data->plugin == NULL)
330         && (data->plugin_instance == NULL)
331         /* && (data->type == NULL) */
332         && (data->type_instance == NULL))
333     {
334       ERROR ("Target `replace': You need to set at lease one of `Host', "
335           "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
336       status = -1;
337     }
338
339     break;
340   }
341
342   if (status != 0)
343   {
344     tr_destroy ((void *) &data);
345     return (status);
346   }
347
348   *user_data = data;
349   return (0);
350 } /* }}} int tr_create */
351
352 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
353     notification_meta_t __attribute__((unused)) **meta, void **user_data)
354 {
355   tr_data_t *data;
356
357   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
358     return (-EINVAL);
359
360   data = *user_data;
361   if (data == NULL)
362   {
363     ERROR ("Target `replace': Invoke: `data' is NULL.");
364     return (-EINVAL);
365   }
366
367 #define HANDLE_FIELD(f,e) \
368   if (data->f != NULL) \
369     tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
370   HANDLE_FIELD (host, 0);
371   HANDLE_FIELD (plugin, 0);
372   HANDLE_FIELD (plugin_instance, 1);
373   /* HANDLE_FIELD (type); */
374   HANDLE_FIELD (type_instance, 1);
375
376   return (FC_TARGET_CONTINUE);
377 } /* }}} int tr_invoke */
378
379 void module_register (void)
380 {
381         target_proc_t tproc;
382
383         memset (&tproc, 0, sizeof (tproc));
384         tproc.create  = tr_create;
385         tproc.destroy = tr_destroy;
386         tproc.invoke  = tr_invoke;
387         fc_register_target ("replace", tproc);
388 } /* module_register */
389
390 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
391