src/utils_ignorelist.[ch]: Renamed some functions. Internal changes.
[collectd.git] / src / utils_ignorelist.c
1 /**
2  * collectd - src/config_list.c
3  * Copyright (C) 2006 Lubos Stanek <lubek at users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/
6  * or modify it under the terms of the GNU General Public Li-
7  * cence as published by the Free Software Foundation; either
8  * version 2 of the Licence, or any later version.
9  *
10  * This program is distributed in the hope that it will be use-
11  * ful, but WITHOUT ANY WARRANTY; without even the implied war-
12  * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public Licence for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * Licence along with this program; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
18  * USA.
19  *
20  * Authors:
21  *   Lubos Stanek <lubek at users.sourceforge.net>
22  **/
23 /**
24  * ignorelist handles plugin's list of configured collectable
25  * entries with global ignore action
26  **/
27 /**
28  * Usage:
29  * 
30  * Define plugin's global pointer variable of type ignorelist_t:
31  *   ignorelist_t *myconfig_ignore;
32  * If you know the state of the global ignore (IgnoreSelected),
33  * allocate the variable with:
34  *   myconfig_ignore = ignorelist_create (YourKnownIgnore);
35  * If you do not know the state of the global ignore,
36  * initialize the global variable and set the ignore flag later:
37  *   myconfig_ignore = ignorelist_init ();
38  * Append single entries in your cf_register'ed callback function:
39  *   ignorelist_add (myconfig_ignore, newentry);
40  * When you hit the IgnoreSelected config option,
41  * offer it to the list:
42  *   ignorelist_ignore (myconfig_ignore, instantly_got_value_of_ignore);
43  * That is all for the ignorelist initialization.
44  * Later during read and write (plugin's registered functions) get
45  * the information whether this entry would be collected or not:
46  *   if (ignorelist_match (myconfig_ignore, thisentry))
47  *     return;
48  **/
49
50 #include "common.h"
51 #include "utils_debug.h"
52 #include "utils_ignorelist.h"
53
54 /*
55  * private prototypes
56  */
57 struct ignorelist_item_s
58 {
59 #if HAVE_REGEX_H
60         regex_t *rmatch;        /* regular expression entry identification */
61 #endif
62         char *smatch;           /* string entry identification */
63         struct ignorelist_item_s *next;
64 };
65 typedef struct ignorelist_item_s ignorelist_item_t;
66
67 struct ignorelist_s
68 {
69         int ignore;             /* ignore entries */
70         int num;                /* number of entries */
71         ignorelist_item_t *head;        /* pointer to the first entry */
72 };
73
74 /* *** *** *** ********************************************* *** *** *** */
75 /* *** *** *** *** *** ***   private functions   *** *** *** *** *** *** */
76 /* *** *** *** ********************************************* *** *** *** */
77
78 static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
79 {
80         assert ((il != NULL) && (item != NULL));
81
82         item->next = il->head;
83         il->head = item;
84
85         il->num++;
86 }
87
88 #if HAVE_REGEX_H
89 static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
90 {
91         int rcompile;
92         regex_t *regtemp;
93         int errsize;
94         char *regerr = NULL;
95         ignorelist_item_t *new;
96
97         /* create buffer */
98         if ((regtemp = malloc(sizeof(regex_t))) == NULL)
99         {
100                 syslog (LOG_ERR, "cannot allocate new config entry");
101                 return (1);
102         }
103         memset (regtemp, '\0', sizeof(regex_t));
104
105         /* compile regex */
106         if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
107         {
108                 /* prepare message buffer */
109                 errsize = regerror(rcompile, regtemp, NULL, 0);
110                 if (errsize)
111                         regerr = smalloc(errsize);
112                 /* get error message */
113                 if (regerror (rcompile, regtemp, regerr, errsize))
114                 {
115                         fprintf (stderr, "Cannot compile regex %s: %i/%s",
116                                         entry, rcompile, regerr);
117                         syslog (LOG_ERR, "Cannot compile regex %s: %i/%s",
118                                         entry, rcompile, regerr);
119                 }
120                 else
121                 {
122                         fprintf (stderr, "Cannot compile regex %s: %i",
123                                         entry, rcompile);
124                         syslog (LOG_ERR, "Cannot compile regex %s: %i",
125                                         entry, rcompile);
126                 }
127
128                 if (errsize)
129                         sfree (regerr);
130                 regfree (regtemp);
131                 return (1);
132         }
133         DBG("regex compiled: %s - %i", entry, rcompile);
134
135         /* create new entry */
136         if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
137         {
138                 syslog (LOG_ERR, "cannot allocate new config entry");
139                 regfree (regtemp);
140                 return (1);
141         }
142         memset (new, '\0', sizeof(ignorelist_item_t));
143         new->rmatch = regtemp;
144
145         /* append new entry */
146         ignorelist_append (il, new);
147
148         return (0);
149 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
150 #endif
151
152 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
153 {
154         ignorelist_item_t *new;
155
156         /* create new entry */
157         if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
158         {
159                 syslog (LOG_ERR, "cannot allocate new entry");
160                 return (1);
161         }
162         memset (new, '\0', sizeof(ignorelist_item_t));
163         new->smatch = sstrdup(entry);
164
165         /* append new entry */
166         ignorelist_append (il, new);
167
168         return (0);
169 } /* int ignorelist_append_string(ignorelist_t *il, const char *entry) */
170
171 #if HAVE_REGEX_H
172 /*
173  * check list for entry regex match
174  * return 1 if found
175  */
176 static int ignorelist_match_regex (ignorelist_item_t *item, const char *entry)
177 {
178         assert ((item != NULL) && (item->rmatch != NULL)
179                         && (entry != NULL) && (strlen (entry) > 0));
180
181         /* match regex */
182         if (regexec (item->rmatch, entry, 0, NULL, 0) == 0)
183                 return (1);
184
185         return (0);
186 } /* int ignorelist_match_regex (ignorelist_item_t *item, const char *entry) */
187 #endif
188
189 /*
190  * check list for entry string match
191  * return 1 if found
192  */
193 static int ignorelist_match_string (ignorelist_item_t *item, const char *entry)
194 {
195         assert ((item != NULL) && (item->smatch != NULL)
196                         && (entry != NULL) && (strlen (entry) > 0));
197
198         if (strcmp (entry, item->smatch) == 0)
199                 return (1);
200
201         return (0);
202 } /* int ignorelist_match_string (ignorelist_item_t *item, const char *entry) */
203
204
205 /* *** *** *** ******************************************** *** *** *** */
206 /* *** *** *** *** *** ***   public functions   *** *** *** *** *** *** */
207 /* *** *** *** ******************************************** *** *** *** */
208
209 /*
210  * create the ignorelist_t with known ignore state
211  * return pointer to ignorelist_t
212  */
213 ignorelist_t *ignorelist_create (int invert)
214 {
215         ignorelist_t *il;
216
217         /* smalloc exits if it failes */
218         il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
219         DBG("Ignorelist created 0x%p, default is %s",
220                         (void *) il,
221                         invert ? "collect" : "ignore");
222
223         memset (il, '\0', sizeof (ignorelist_t));
224
225         /*
226          * ->ignore == 0  =>  collect
227          * ->ignore == 1  =>  ignore
228          */
229         il->ignore = invert ? 0 : 1;
230
231         return (il);
232 } /* ignorelist_t *ignorelist_create (int ignore) */
233
234 /*
235  * free memory used by ignorelist_t
236  */
237 void ignorelist_free (ignorelist_t *il)
238 {
239         ignorelist_item_t *this;
240         ignorelist_item_t *next;
241
242         DBG ("(il = 0x%p)", (void *) il);
243
244         if (il == NULL)
245                 return;
246
247         for (this = il->head; this != NULL; this = next)
248         {
249                 DBG ("free - item = 0x%p, numlist %i", (void *) this, il->num);
250                 next = this->next;
251                 il->num--;
252 #if HAVE_REGEX_H
253                 if (this->rmatch != NULL)
254                 {
255                         regfree (this->rmatch);
256                         this->rmatch = NULL;
257                 }
258 #endif
259                 if (this->smatch != NULL)
260                 {
261                         sfree (this->smatch);
262                         this->smatch = NULL;
263                 }
264                 sfree (this);
265         }
266 #if COLLECTD_DEBUG
267         if (il->num != 0)
268                 DBG ("after free numlist: %i", il->num);
269 #endif
270         il->num = 0;
271         sfree (il);
272         il = NULL;
273 } /* void ignorelist_destroy (ignorelist_t *il) */
274
275 /*
276  * set ignore state of the ignorelist_t
277  */
278 void ignorelist_set_invert (ignorelist_t *il, int invert)
279 {
280         if (il == NULL)
281         {
282                 DBG("ignore call with ignorelist_t == NULL");
283                 return;
284         }
285
286         il->ignore = invert ? 0 : 1;
287 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
288
289 /*
290  * get number of entries in the ignorelist_t
291  * return int number
292  */
293 int ignorelist_num (ignorelist_t *il)
294 {
295         if (il == NULL)
296         {
297                 DBG("get num called with ignorelist_t == NULL");
298                 return (0);
299         }
300
301         return (il->num);
302 } /* int ignorelist_num (ignorelist_t *il) */
303
304 /*
305  * append entry into ignorelist_t
306  * return 1 for success
307  */
308 int ignorelist_add (ignorelist_t *il, const char *entry)
309 {
310         int ret;
311         size_t entry_len;
312         char *entry_copy;
313
314         if (il == NULL)
315         {
316                 DBG ("add called with ignorelist_t == NULL");
317                 return (1);
318         }
319
320         entry_len = strlen (entry);
321
322         /* append nothing */
323         if (entry_len == 0)
324         {
325                 DBG("not appending: empty entry");
326                 return (1);
327         }
328
329 #if HAVE_REGEX_H
330         /* regex string is enclosed in "/.../" */
331         if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
332         {
333                 /* We need to copy `entry' since it's const */
334                 entry_copy = smalloc (entry_len);
335                 memset (entry_copy, '\0', entry_len);
336                 strncpy (entry_copy, entry + 1, entry_len - 2);
337
338                 DBG("I'm about to add regex entry: %s", entry_copy);
339                 ret = ignorelist_append_regex(il, entry_copy);
340                 sfree (entry_copy);
341         }
342         else
343 #endif
344         {
345                 DBG("to add entry: %s", entry);
346                 ret = ignorelist_append_string(il, entry);
347         }
348
349         return (ret);
350 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
351
352 /*
353  * check list for entry
354  * return 1 for ignored entry
355  */
356 int ignorelist_match (ignorelist_t *il, const char *entry)
357 {
358         ignorelist_item_t *traverse;
359
360         /* if no entries, collect all */
361         if (ignorelist_num(il) == 0)
362                 return (0);
363
364         /* traverse list and check entries */
365         for (traverse = il->head; traverse != NULL; traverse = traverse->next)
366         {
367 #if HAVE_REGEX_H
368                 if (traverse->rmatch != NULL)
369                 {
370                         if (ignorelist_match_regex (traverse, entry))
371                                 return (il->ignore);
372                 }
373                 else
374 #endif
375                 {
376                         if (ignorelist_match_string (traverse, entry))
377                                 return (il->ignore);
378                 }
379         } /* for traverse */
380
381         return (1 - il->ignore);
382 } /* int ignorelist_match (ignorelist_t *il, const char *entry) */
383