suricata
util-debug-filters.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22  *
23  * Debug filter utility functions
24  */
25 
26 #include "suricata-common.h"
27 #include "util-debug-filters.h"
28 #include "threads.h"
29 #include "util-debug.h"
30 
31 /* both of these are defined in util-debug.c */
32 extern int sc_log_module_initialized;
33 extern int sc_log_module_cleaned;
34 
35 /* used to indicate if any FG filters are registered */
37 
38 /* used to indicate if any FD filters are registered */
40 
41 /**
42  * \brief Holds the fine-grained filters
43  */
45 
46 /**
47  * \brief Mutex for accessing the fine-grained filters sc_log_fg_filters
48  */
49 static SCMutex sc_log_fg_filters_m[SC_LOG_FILTER_MAX] = { SCMUTEX_INITIALIZER,
51 
52 /**
53  * \brief Holds the function-dependent filters
54  */
55 static SCLogFDFilter *sc_log_fd_filters = NULL;
56 
57 /**
58  * \brief Mutex for accessing the function-dependent filters sc_log_fd_filters
59  */
60 static SCMutex sc_log_fd_filters_m = SCMUTEX_INITIALIZER;
61 
62 /**
63  * \brief Holds the thread_list required by function-dependent filters
64  */
65 static SCLogFDFilterThreadList *sc_log_fd_filters_tl = NULL;
66 
67 /**
68  * \brief Mutex for accessing the FD thread_list sc_log_fd_filters_tl
69  */
70 static SCMutex sc_log_fd_filters_tl_m = SCMUTEX_INITIALIZER;
71 
72 /**
73  * \brief Helper function used internally to add a FG filter
74  *
75  * \param file File_name of the filter
76  * \param function Function_name of the filter
77  * \param line Line number of the filter
78  * \param listtype The filter listtype. Can be either a blacklist or whitelist
79  * filter listtype(SC_LOG_FILTER_BL or SC_LOG_FILTER_WL)
80  *
81  * \retval 0 on successfully adding the filter;
82  * \retval -1 on failure
83  */
84 static int SCLogAddFGFilter(const char *file, const char *function,
85  int line, int listtype)
86 {
87  SCLogFGFilterFile *fgf_file = NULL;
88  SCLogFGFilterFile *prev_fgf_file = NULL;
89 
90  SCLogFGFilterFunc *fgf_func = NULL;
91  SCLogFGFilterFunc *prev_fgf_func = NULL;
92 
93  SCLogFGFilterLine *fgf_line = NULL;
94  SCLogFGFilterLine *prev_fgf_line = NULL;
95 
96  int found = 0;
97 
98  if (sc_log_module_initialized != 1) {
99  printf("Logging module not initialized. Call SCLogInitLogModule() "
100  "first before using the debug API\n");
101  return -1 ;
102  }
103 
104  if (file == NULL && function == NULL && line < 0) {
105  printf("Error: Invalid arguments supplied to SCLogAddFGFilter\n");
106  return -1;
107  }
108 
109  SCMutex *m = &sc_log_fg_filters_m[listtype];
110 
111  SCMutexLock(m);
112 
113  fgf_file = sc_log_fg_filters[listtype];
114 
115  prev_fgf_file = fgf_file;
116  while (fgf_file != NULL) {
117  prev_fgf_file = fgf_file;
118  if (file == NULL && fgf_file->file == NULL)
119  found = 1;
120  else if (file != NULL && fgf_file->file != NULL)
121  found = (strcmp(file, fgf_file->file) == 0);
122  else
123  found = 0;
124 
125  if (found == 1)
126  break;
127 
128  fgf_file = fgf_file->next;
129  }
130 
131  if (found == 0) {
132  SCLogAddToFGFFileList(prev_fgf_file, file, function, line, listtype);
133  goto done;
134  }
135 
136  found = 0;
137  fgf_func = fgf_file->func;
138  prev_fgf_func = fgf_func;
139  while (fgf_func != NULL) {
140  prev_fgf_func = fgf_func;
141  if (function == NULL && fgf_func->func == NULL)
142  found = 1;
143  else if (function != NULL && fgf_func->func != NULL)
144  found = (strcmp(function, fgf_func->func) == 0);
145  else
146  found = 0;
147 
148  if (found == 1)
149  break;
150 
151  fgf_func = fgf_func->next;
152  }
153 
154  if (found == 0) {
155  SCLogAddToFGFFuncList(fgf_file, prev_fgf_func, function, line);
156  goto done;
157  }
158 
159  found = 0;
160  fgf_line = fgf_func->line;
161  prev_fgf_line = fgf_line;
162  while(fgf_line != NULL) {
163  prev_fgf_line = fgf_line;
164  if (line == fgf_line->line) {
165  found = 1;
166  break;
167  }
168 
169  fgf_line = fgf_line->next;
170  }
171 
172  if (found == 0) {
173  SCLogAddToFGFLineList(fgf_func, prev_fgf_line, line);
174  goto done;
175  }
176 
177  done:
178  SCMutexUnlock(&sc_log_fg_filters_m[listtype]);
180 
181  return 0;
182 }
183 
184 /**
185  * \brief Internal function used to check for matches against registered FG
186  * filters. Checks if there is a match for the incoming log_message with
187  * any of the FG filters. Based on whether the filter type is whitelist
188  * or blacklist, the function allows the message to be logged or not.
189  *
190  * \param file File_name from where the log_message originated
191  * \param function Function_name from where the log_message originated
192  * \param line Line number from where the log_message originated
193  * \param listtype The filter listtype. Can be either a blacklist or whitelist
194  * filter listtype(SC_LOG_FILTER_BL or SC_LOG_FILTER_WL)
195  *
196  * \retval 1 if there is a match
197  * \retval 0 on no match
198  * \retval -1 on failure
199  */
200 static int SCLogMatchFGFilter(const char *file, const char *function, int line,
201  int listtype)
202 {
203  SCLogFGFilterFile *fgf_file = NULL;
204  SCLogFGFilterFunc *fgf_func = NULL;
205  SCLogFGFilterLine *fgf_line = NULL;
206  int match = 1;
207 
208  if (sc_log_module_initialized != 1) {
209  printf("Logging module not initialized. Call SCLogInitLogModule() "
210  "first before using the debug API\n");
211  return -1;
212  }
213 
214  SCMutexLock(&sc_log_fg_filters_m[listtype]);
215 
216  fgf_file = sc_log_fg_filters[listtype];
217 
218  if (fgf_file == NULL) {
219  SCMutexUnlock(&sc_log_fg_filters_m[listtype]);
220  return 1;
221  }
222 
223  while(fgf_file != NULL) {
224  match = 1;
225 
226  match &= (fgf_file->file != NULL)? !strcmp(file, fgf_file->file): 1;
227 
228  if (match == 0) {
229  fgf_file = fgf_file->next;
230  continue;
231  }
232 
233  fgf_func = fgf_file->func;
234  while (fgf_func != NULL) {
235  match = 1;
236 
237  match &= (fgf_func->func != NULL)? !strcmp(function, fgf_func->func): 1;
238 
239  if (match == 0) {
240  fgf_func = fgf_func->next;
241  continue;
242  }
243 
244  fgf_line = fgf_func->line;
245  while (fgf_line != NULL) {
246  match = 1;
247 
248  match &= (fgf_line->line != -1)? (line == fgf_line->line): 1;
249 
250  if (match == 1)
251  break;
252 
253  fgf_line = fgf_line->next;
254  }
255 
256  if (match == 1)
257  break;
258 
259  fgf_func = fgf_func->next;
260  }
261 
262  if (match == 1) {
263  SCMutexUnlock(&sc_log_fg_filters_m[listtype]);
264  if (listtype == SC_LOG_FILTER_WL)
265  return 1;
266  else
267  return 0;
268  }
269 
270  fgf_file = fgf_file->next;
271  }
272 
273  SCMutexUnlock(&sc_log_fg_filters_m[listtype]);
274 
275  if (listtype == SC_LOG_FILTER_WL)
276  return 0;
277  else
278  return 1;
279 }
280 
281 /**
282  * \brief Checks if there is a match for the incoming log_message with any
283  * of the FG filters. If there is a match, it allows the message
284  * to be logged, else it rejects that message.
285  *
286  * \param file File_name from where the log_message originated
287  * \param function Function_name from where the log_message originated
288  * \param line Line number from where the log_message originated
289  *
290  * \retval 1 if there is a match
291  * \retval 0 on no match
292  * \retval -1 on failure
293  */
294 int SCLogMatchFGFilterWL(const char *file, const char *function, int line)
295 {
296  return SCLogMatchFGFilter(file, function, line, SC_LOG_FILTER_WL);
297 }
298 
299 /**
300  * \brief Checks if there is a match for the incoming log_message with any
301  * of the FG filters. If there is a match it rejects the logging
302  * for that messages, else it allows that message to be logged
303  *
304  * \param file File_name from where the log_message originated
305  * \param function Function_name from where the log_message originated
306  * \param line Line number from where the log_message originated
307  *
308  * \retval 1 if there is a match
309  * \retval 0 on no match
310  * \retval -1 on failure
311  */
312 int SCLogMatchFGFilterBL(const char *file, const char *function, int line)
313 {
314  return SCLogMatchFGFilter(file, function, line, SC_LOG_FILTER_BL);
315 }
316 
317 /**
318  * \brief Adds a Blacklist(BL) fine-grained(FG) filter. A FG filter BL filter
319  * allows messages that don't match this filter, to be logged, while the
320  * filter is defined using a file_name, function_name and line_number
321  *
322  * If a particular parameter in the fg-filter(file, function and line),
323  * shouldn't be considered while logging the message, one can supply
324  * NULL for the file_name or function_name and a negative line_no.
325  *
326  * \param file File_name of the filter
327  * \param function Function_name of the filter
328  * \param line Line number of the filter
329  *
330  * \retval 0 on successfully adding the filter
331  * \retval -1 on failure
332  */
333 int SCLogAddFGFilterBL(const char *file, const char *function, int line)
334 {
335  return SCLogAddFGFilter(file, function, line, SC_LOG_FILTER_BL);
336 }
337 
339 {
340  SCLogFGFilterFile *fgf_file = NULL;
341  SCLogFGFilterFunc *fgf_func = NULL;
342  SCLogFGFilterLine *fgf_line = NULL;
343 
344  void *temp = NULL;
345 
346  int i = 0;
347 
348  for (i = 0; i < SC_LOG_FILTER_MAX; i++) {
349  SCMutexLock(&sc_log_fg_filters_m[i]);
350 
351  fgf_file = sc_log_fg_filters[i];
352  while (fgf_file != NULL) {
353 
354  fgf_func = fgf_file->func;
355  while (fgf_func != NULL) {
356 
357  fgf_line = fgf_func->line;
358  while(fgf_line != NULL) {
359  temp = fgf_line;
360  fgf_line = fgf_line->next;
361  SCFree(temp);
362  }
363 
364  if (fgf_func->func != NULL)
365  SCFree(fgf_func->func);
366  temp = fgf_func;
367  fgf_func = fgf_func->next;
368  SCFree(temp);
369  }
370 
371  if (fgf_file->file != NULL)
372  SCFree(fgf_file->file);
373  temp = fgf_file;
374  fgf_file = fgf_file->next;
375  SCFree(temp);
376  }
377 
378  SCMutexUnlock(&sc_log_fg_filters_m[i]);
379  sc_log_fg_filters[i] = NULL;
380  }
381 
382  return;
383 }
384 
385 /**
386  * \brief Prints the FG filters(both WL and BL). Used for debugging purposes.
387  *
388  * \retval count The no of FG filters
389  */
391 {
392  SCLogFGFilterFile *fgf_file = NULL;
393  SCLogFGFilterFunc *fgf_func = NULL;
394  SCLogFGFilterLine *fgf_line = NULL;
395 
396  int count = 0;
397  int i = 0;
398 
399  if (sc_log_module_initialized != 1) {
400  printf("Logging module not initialized. Call SCLogInitLogModule() "
401  "first before using the debug API\n");
402  return 0;
403  }
404 
405 #ifdef DEBUG
406  printf("Fine grained filters:\n");
407 #endif
408 
409  for (i = 0; i < SC_LOG_FILTER_MAX; i++) {
410  SCMutexLock(&sc_log_fg_filters_m[i]);
411 
412  fgf_file = sc_log_fg_filters[i];
413  while (fgf_file != NULL) {
414 
415  fgf_func = fgf_file->func;
416  while (fgf_func != NULL) {
417 
418  fgf_line = fgf_func->line;
419  while(fgf_line != NULL) {
420 #ifdef DEBUG
421  printf("%s - ", fgf_file->file);
422  printf("%s - ", fgf_func->func);
423  printf("%d\n", fgf_line->line);
424 #endif
425 
426  count++;
427 
428  fgf_line = fgf_line->next;
429  }
430 
431  fgf_func = fgf_func->next;
432  }
433 
434  fgf_file = fgf_file->next;
435  }
436  SCMutexUnlock(&sc_log_fg_filters_m[i]);
437  }
438 
439  return count;
440 }
441 
442 
443 
444 /* --------------------------------------------------|--------------------------
445  * -------------------------- Code for the FD Filter |--------------------------
446  * --------------------------------------------------V--------------------------
447  */
448 
449 /**
450  * \brief Checks if there is a match for the incoming log_message with any
451  * of the FD filters
452  *
453  * \param function Function_name from where the log_message originated
454  *
455  * \retval 1 if there is a match
456  * \retval 0 on no match;
457  */
458 int SCLogMatchFDFilter(const char *function)
459 {
460 #ifndef DEBUG
461  return 1;
462 #else
463  SCLogFDFilterThreadList *thread_list = NULL;
464 
465  pthread_t self = pthread_self();
466 
467  if (sc_log_module_initialized != 1) {
468  printf("Logging module not initialized. Call SCLogInitLogModule() "
469  "first before using the debug API\n");
470  return 0;
471  }
472 
473  SCMutexLock(&sc_log_fd_filters_tl_m);
474 
475  if (sc_log_fd_filters_tl == NULL) {
476  SCMutexUnlock(&sc_log_fd_filters_tl_m);
477  if (sc_log_fd_filters != NULL)
478  return 0;
479  return 1;
480  }
481 
482  thread_list = sc_log_fd_filters_tl;
483  while (thread_list != NULL) {
484  if (pthread_equal(self, thread_list->t)) {
485  if (thread_list->entered > 0) {
486  SCMutexUnlock(&sc_log_fd_filters_tl_m);
487  return 1;
488  }
489  SCMutexUnlock(&sc_log_fd_filters_tl_m);
490  return 0;
491  }
492 
493  thread_list = thread_list->next;
494  }
495 
496  SCMutexUnlock(&sc_log_fd_filters_tl_m);
497 
498  return 0;
499 #endif
500 }
501 
502 /**
503  * \brief Updates a FD filter, based on whether the function that calls this
504  * function, is registered as a FD filter or not. This is called by
505  * a function only on its entry
506  *
507  * \param function Function_name from where the log_message originated
508  *
509  * \retval 1 Since it is a hack to get things working inside the macros
510  */
511 int SCLogCheckFDFilterEntry(const char *function)
512 {
513  SCLogFDFilter *curr = NULL;
514 
515  SCLogFDFilterThreadList *thread_list = NULL;
516  SCLogFDFilterThreadList *thread_list_temp = NULL;
517 
518  //pid_t self = syscall(SYS_gettid);
519  pthread_t self = pthread_self();
520 
521  if (sc_log_module_initialized != 1) {
522  printf("Logging module not initialized. Call SCLogInitLogModule() "
523  "first before using the debug API\n");
524  return 0;
525  }
526 
527  SCMutexLock(&sc_log_fd_filters_m);
528 
529  curr = sc_log_fd_filters;
530 
531  while (curr != NULL) {
532  if (strcmp(function, curr->func) == 0)
533  break;
534 
535  curr = curr->next;
536  }
537 
538  if (curr == NULL) {
539  SCMutexUnlock(&sc_log_fd_filters_m);
540  return 1;
541  }
542 
543  SCMutexUnlock(&sc_log_fd_filters_m);
544 
545  SCMutexLock(&sc_log_fd_filters_tl_m);
546 
547  thread_list = sc_log_fd_filters_tl;
548  while (thread_list != NULL) {
549  if (pthread_equal(self, thread_list->t))
550  break;
551 
552  thread_list = thread_list->next;
553  }
554 
555  if (thread_list != NULL) {
556  thread_list->entered++;
557  SCMutexUnlock(&sc_log_fd_filters_tl_m);
558  return 1;
559  }
560 
561  if ((thread_list_temp = SCCalloc(1, sizeof(SCLogFDFilterThreadList))) == NULL) {
562  SCMutexUnlock(&sc_log_fd_filters_tl_m);
563  return 0;
564  }
565 
566  thread_list_temp->t = self;
567  thread_list_temp->entered++;
568 
569  sc_log_fd_filters_tl = thread_list_temp;
570 
571  SCMutexUnlock(&sc_log_fd_filters_tl_m);
572 
573  return 1;
574 }
575 
576 /**
577  * \brief Updates a FD filter, based on whether the function that calls this
578  * function, is registered as a FD filter or not. This is called by
579  * a function only before its exit.
580  *
581  * \param function Function_name from where the log_message originated
582  *
583  */
584 void SCLogCheckFDFilterExit(const char *function)
585 {
586  SCLogFDFilter *curr = NULL;
587 
588  SCLogFDFilterThreadList *thread_list = NULL;
589 
590  //pid_t self = syscall(SYS_gettid);
591  pthread_t self = pthread_self();
592 
593  if (sc_log_module_initialized != 1) {
594  printf("Logging module not initialized. Call SCLogInitLogModule() "
595  "first before using the debug API\n");
596  return;
597  }
598 
599  SCMutexLock(&sc_log_fd_filters_m);
600 
601  curr = sc_log_fd_filters;
602 
603  while (curr != NULL) {
604  if (strcmp(function, curr->func) == 0)
605  break;
606 
607  curr = curr->next;
608  }
609 
610  if (curr == NULL) {
611  SCMutexUnlock(&sc_log_fd_filters_m);
612  return;
613  }
614 
615  SCMutexUnlock(&sc_log_fd_filters_m);
616 
617  SCMutexLock(&sc_log_fd_filters_tl_m);
618 
619  thread_list = sc_log_fd_filters_tl;
620  while (thread_list != NULL) {
621  if (pthread_equal(self, thread_list->t))
622  break;
623 
624  thread_list = thread_list->next;
625  }
626 
627  SCMutexUnlock(&sc_log_fd_filters_tl_m);
628 
629  if (thread_list != NULL)
630  thread_list->entered--;
631 
632  return;
633 }
634 
635 /**
636  * \brief Adds a Function-Dependent(FD) filter
637  *
638  * \param Name of the function for which a FD filter has to be registered
639  *
640  * \retval 0 on success
641  * \retval -1 on failure
642  */
643 int SCLogAddFDFilter(const char *function)
644 {
645  SCLogFDFilter *curr = NULL;
646  SCLogFDFilter *prev = NULL;
647  SCLogFDFilter *temp = NULL;
648 
649  if (sc_log_module_initialized != 1) {
650  printf("Logging module not initialized. Call SCLogInitLogModule() "
651  "first before using the debug API\n");
652  return -1;
653  }
654 
655  if (function == NULL) {
656  printf("Invalid argument supplied to SCLogAddFDFilter\n");
657  return -1;
658  }
659 
660  SCMutexLock(&sc_log_fd_filters_m);
661 
662  curr = sc_log_fd_filters;
663  while (curr != NULL) {
664  prev = curr;
665 
666  if (strcmp(function, curr->func) == 0) {
667 
668  SCMutexUnlock(&sc_log_fd_filters_m);
669  return 0;
670  }
671 
672  curr = curr->next;
673  }
674 
675  if ((temp = SCCalloc(1, sizeof(SCLogFDFilter))) == NULL) {
676  printf("Error Allocating memory (SCCalloc)\n");
677  exit(EXIT_FAILURE);
678  }
679 
680  if ( (temp->func = SCStrdup(function)) == NULL) {
681  printf("Error Allocating memory (SCStrdup)\n");
682  exit(EXIT_FAILURE);
683  }
684 
685  if (sc_log_fd_filters == NULL)
686  sc_log_fd_filters = temp;
687  /* clang thinks prev can be NULL, but it can't be unless
688  * sc_log_fd_filters is also NULL which is handled here.
689  * Doing this "fix" to shut clang up. */
690  else if (prev != NULL)
691  prev->next = temp;
692 
693  SCMutexUnlock(&sc_log_fd_filters_m);
695 
696  return 0;
697 }
698 
699 /**
700  * \brief Releases all the FD filters added to the logging module
701  */
703 {
704  SCLogFDFilter *fdf = NULL;
705  SCLogFDFilter *temp = NULL;
706 
707  SCMutexLock(&sc_log_fd_filters_m);
708 
709  fdf = sc_log_fd_filters;
710  while (fdf != NULL) {
711  temp = fdf;
712  fdf = fdf->next;
713  SCLogReleaseFDFilter(temp);
714  }
715 
716  sc_log_fd_filters = NULL;
717 
718  SCMutexUnlock( &sc_log_fd_filters_m );
719 
720  return;
721 }
722 
723 /**
724  * \brief Removes a Function-Dependent(FD) filter
725  *
726  * \param Name of the function for which a FD filter has to be unregistered
727  *
728  * \retval 0 on success(the filter was removed or the filter was not present)
729  * \retval -1 on failure/error
730  */
731 int SCLogRemoveFDFilter(const char *function)
732 {
733  SCLogFDFilter *curr = NULL;
734  SCLogFDFilter *prev = NULL;
735 
736  if (sc_log_module_initialized != 1) {
737  printf("Logging module not initialized. Call SCLogInitLogModule() "
738  "first before using the debug API\n");
739  return -1 ;
740  }
741 
742  if (function == NULL) {
743  printf("Invalid argument(s) supplied to SCLogRemoveFDFilter\n");
744  return -1;
745  }
746 
747  SCMutexLock(&sc_log_fd_filters_m);
748 
749  if (sc_log_fd_filters == NULL) {
750  SCMutexUnlock(&sc_log_fd_filters_m);
751  return 0;
752  }
753 
754  curr = sc_log_fd_filters;
755  prev = curr;
756  while (curr != NULL) {
757  if (strcmp(function, curr->func) == 0)
758  break;
759 
760  prev = curr;
761  curr = curr->next;
762  }
763 
764  if (curr == NULL) {
765 
766  SCMutexUnlock(&sc_log_fd_filters_m);
767 
768  return 0;
769  }
770 
771  if (sc_log_fd_filters == curr)
772  sc_log_fd_filters = curr->next;
773  else
774  prev->next = curr->next;
775 
776  SCLogReleaseFDFilter(curr);
777 
778  SCMutexUnlock(&sc_log_fd_filters_m);
779 
780  if (sc_log_fd_filters == NULL)
782 
783  return 0;
784 }
785 
786 /**
787  * \brief Prints the FG filters(both WL and BL). Used for debugging purposes.
788  *
789  * \retval count The no of FG filters
790  */
792 {
793  SCLogFDFilter *fdf = NULL;
794  int count = 0;
795 
796  if (sc_log_module_initialized != 1) {
797  printf("Logging module not initialized. Call SCLogInitLogModule() "
798  "first before using the debug API\n");
799  return 0;
800  }
801 
802 #ifdef DEBUG
803  printf("FD filters:\n");
804 #endif
805 
806  SCMutexLock(&sc_log_fd_filters_m);
807 
808  fdf = sc_log_fd_filters;
809  while (fdf != NULL) {
810 #ifdef DEBUG
811  printf("%s \n", fdf->func);
812 #endif
813  fdf = fdf->next;
814  count++;
815  }
816 
817  SCMutexUnlock(&sc_log_fd_filters_m);
818 
819  return count;
820 }
821 
822 /**
823  * \brief Helper function used internally to add a FG filter. This function is
824  * called when the file component of the incoming filter has no entry
825  * in the filter list.
826  *
827  * \param fgf_file The file component(basically the position in the list) from
828  * the filter list, after which the new filter has to be added
829  * \param file File_name of the filter
830  * \param function Function_name of the filter
831  * \param line Line number of the filter
832  * \param listtype The filter listtype. Can be either a blacklist or whitelist
833  * filter listtype(SC_LOG_FILTER_BL or SC_LOG_FILTER_WL)
834  */
836  const char *file,
837  const char *function, int line,
838  int listtype)
839 {
840  SCLogFGFilterFile *fgf_file_temp = NULL;
841  SCLogFGFilterFunc *fgf_func_temp = NULL;
842  SCLogFGFilterLine *fgf_line_temp = NULL;
843 
844  if ((fgf_file_temp = SCCalloc(1, sizeof(SCLogFGFilterFile))) == NULL) {
845  FatalError("Fatal error encountered in SCLogAddToFGFFileList. Exiting...");
846  }
847 
848  if ( file != NULL && (fgf_file_temp->file = SCStrdup(file)) == NULL) {
849  printf("Error Allocating memory\n");
850  exit(EXIT_FAILURE);
851  }
852 
853  if ((fgf_func_temp = SCCalloc(1, sizeof(SCLogFGFilterFunc))) == NULL) {
854  FatalError("Fatal error encountered in SCLogAddToFGFFileList. Exiting...");
855  }
856 
857  if ( function != NULL && (fgf_func_temp->func = SCStrdup(function)) == NULL) {
858  printf("Error Allocating memory\n");
859  exit(EXIT_FAILURE);
860  }
861 
862  if ((fgf_line_temp = SCCalloc(1, sizeof(SCLogFGFilterLine))) == NULL) {
863  FatalError("Fatal error encountered in SCLogAddToFGFFileList. Exiting...");
864  }
865 
866  fgf_line_temp->line = line;
867 
868  /* add to the lists */
869  fgf_func_temp->line = fgf_line_temp;
870 
871  fgf_file_temp->func = fgf_func_temp;
872 
873  if (fgf_file == NULL)
874  sc_log_fg_filters[listtype] = fgf_file_temp;
875  else
876  fgf_file->next = fgf_file_temp;
877 
878  return;
879 }
880 
881 /**
882  * \brief Helper function used internally to add a FG filter. This function is
883  * called when the file component of the incoming filter has an entry
884  * in the filter list, but the function component doesn't have an entry
885  * for the corresponding file component
886  *
887  * \param fgf_file The file component from the filter list to which the new
888  * filter has to be added
889  * \param fgf_func The function component(basically the position in the list),
890  * from the filter list, after which the new filter has to be
891  * added
892  * \param function Function_name of the filter
893  * \param line Line number of the filter
894  */
896  SCLogFGFilterFunc *fgf_func,
897  const char *function, int line)
898 {
899  SCLogFGFilterFunc *fgf_func_temp = NULL;
900  SCLogFGFilterLine *fgf_line_temp = NULL;
901 
902  if ((fgf_func_temp = SCCalloc(1, sizeof(SCLogFGFilterFunc))) == NULL) {
903  FatalError("Fatal error encountered in SCLogAddToFGFFuncList. Exiting...");
904  }
905 
906  if ( function != NULL && (fgf_func_temp->func = SCStrdup(function)) == NULL) {
907  printf("Error Allocating memory\n");
908  exit(EXIT_FAILURE);
909  }
910 
911  if ((fgf_line_temp = SCCalloc(1, sizeof(SCLogFGFilterLine))) == NULL) {
912  FatalError("Fatal error encountered in SCLogAddToFGFFuncList. Exiting...");
913  }
914 
915  fgf_line_temp->line = line;
916 
917  /* add to the lists */
918  fgf_func_temp->line = fgf_line_temp;
919 
920  if (fgf_func == NULL)
921  fgf_file->func = fgf_func_temp;
922  else
923  fgf_func->next = fgf_func_temp;
924 
925  return;
926 }
927 
928 /**
929  * \brief Helper function used internally to add a FG filter. This function is
930  * called when the file and function components of the incoming filter
931  * have an entry in the filter list, but the line component doesn't have
932  * an entry for the corresponding function component
933  *
934  * \param fgf_func The function component from the filter list to which the new
935  * filter has to be added
936  * \param fgf_line The function component(basically the position in the list),
937  * from the filter list, after which the new filter has to be
938  * added
939  * \param line Line number of the filter
940  */
942  SCLogFGFilterLine *fgf_line,
943  int line)
944 {
945  SCLogFGFilterLine *fgf_line_temp = NULL;
946 
947  if ((fgf_line_temp = SCCalloc(1, sizeof(SCLogFGFilterLine))) == NULL) {
948  FatalError("Fatal error encountered in SCLogAddToFGFLineList. Exiting...");
949  }
950 
951  fgf_line_temp->line = line;
952 
953  /* add to the lists */
954  if (fgf_line == NULL)
955  fgf_func->line = fgf_line_temp;
956  else
957  fgf_line->next = fgf_line_temp;
958 
959  return;
960 }
961 
962 /**
963  * \brief Releases the memory alloted to a FD filter
964  *
965  * \param Pointer to the FD filter that has to be freed
966  */
968 {
969  if (fdf != NULL) {
970  if (fdf->func != NULL)
971  SCFree(fdf->func);
972  SCFree(fdf);
973  }
974 
975  return;
976 }
977 
SCLogMatchFGFilterBL
int SCLogMatchFGFilterBL(const char *file, const char *function, int line)
Checks if there is a match for the incoming log_message with any of the FG filters....
Definition: util-debug-filters.c:312
SCLogFDFilterThreadList_
Structure used to hold the thread_list used by FD filters.
Definition: util-debug-filters.h:72
SCLogCheckFDFilterEntry
int SCLogCheckFDFilterEntry(const char *function)
Updates a FD filter, based on whether the function that calls this function, is registered as a FD fi...
Definition: util-debug-filters.c:511
SCLogAddToFGFFuncList
void SCLogAddToFGFFuncList(SCLogFGFilterFile *fgf_file, SCLogFGFilterFunc *fgf_func, const char *function, int line)
Helper function used internally to add a FG filter. This function is called when the file component o...
Definition: util-debug-filters.c:895
SCLogReleaseFDFilter
void SCLogReleaseFDFilter(SCLogFDFilter *fdf)
Releases the memory alloted to a FD filter.
Definition: util-debug-filters.c:967
sc_log_fg_filters_present
int sc_log_fg_filters_present
Definition: util-debug-filters.c:36
SC_LOG_FILTER_BL
@ SC_LOG_FILTER_BL
Definition: util-debug-filters.h:34
threads.h
SCLogFDFilterThreadList_::t
pthread_t t
Definition: util-debug-filters.h:74
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
SCLogFGFilterLine_::line
int line
Definition: util-debug-filters.h:43
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:121
SCLogFGFilterLine_::next
struct SCLogFGFilterLine_ * next
Definition: util-debug-filters.h:45
SCLogAddToFGFLineList
void SCLogAddToFGFLineList(SCLogFGFilterFunc *fgf_func, SCLogFGFilterLine *fgf_line, int line)
Helper function used internally to add a FG filter. This function is called when the file and functio...
Definition: util-debug-filters.c:941
m
SCMutex m
Definition: flow-hash.h:6
SCLogMatchFDFilter
int SCLogMatchFDFilter(const char *function)
Checks if there is a match for the incoming log_message with any of the FD filters.
Definition: util-debug-filters.c:458
SCLogFGFilterFile_::next
struct SCLogFGFilterFile_ * next
Definition: util-debug-filters.h:66
SCLogReleaseFGFilters
void SCLogReleaseFGFilters(void)
Definition: util-debug-filters.c:338
util-debug.h
SCLogReleaseFDFilters
void SCLogReleaseFDFilters(void)
Releases all the FD filters added to the logging module.
Definition: util-debug-filters.c:702
SC_LOG_FILTER_MAX
@ SC_LOG_FILTER_MAX
Definition: util-debug-filters.h:36
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
SCLogFGFilterFunc_::line
SCLogFGFilterLine * line
Definition: util-debug-filters.h:53
SCLogCheckFDFilterExit
void SCLogCheckFDFilterExit(const char *function)
Updates a FD filter, based on whether the function that calls this function, is registered as a FD fi...
Definition: util-debug-filters.c:584
SCLogFDFilter_::next
struct SCLogFDFilter_ * next
Definition: util-debug-filters.h:86
SCLogFDFilterThreadList_::next
struct SCLogFDFilterThreadList_ * next
Definition: util-debug-filters.h:77
SCLogMatchFGFilterWL
int SCLogMatchFGFilterWL(const char *file, const char *function, int line)
Checks if there is a match for the incoming log_message with any of the FG filters....
Definition: util-debug-filters.c:294
SCLogFGFilterLine_
Structure used to hold the line_no details of a FG filter.
Definition: util-debug-filters.h:42
SCLogAddToFGFFileList
void SCLogAddToFGFFileList(SCLogFGFilterFile *fgf_file, const char *file, const char *function, int line, int listtype)
Helper function used internally to add a FG filter. This function is called when the file component o...
Definition: util-debug-filters.c:835
SCLogFDFilter_
Structure that holds the FD filters.
Definition: util-debug-filters.h:83
SCLogPrintFDFilters
int SCLogPrintFDFilters(void)
Prints the FG filters(both WL and BL). Used for debugging purposes.
Definition: util-debug-filters.c:791
suricata-common.h
sc_log_fd_filters_present
int sc_log_fd_filters_present
Definition: util-debug-filters.c:39
SCLogFGFilterFunc_::func
char * func
Definition: util-debug-filters.h:52
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:502
SC_LOG_FILTER_WL
@ SC_LOG_FILTER_WL
Definition: util-debug-filters.h:35
SCLogFDFilter_::func
char * func
Definition: util-debug-filters.h:84
sc_log_module_cleaned
int sc_log_module_cleaned
Used to indicate whether the logging module has been cleaned or not.
Definition: util-debug.c:111
SCLogFGFilterFunc_
structure used to hold the function details of a FG filter
Definition: util-debug-filters.h:51
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SCLogRemoveFDFilter
int SCLogRemoveFDFilter(const char *function)
Removes a Function-Dependent(FD) filter.
Definition: util-debug-filters.c:731
SCLogPrintFGFilters
int SCLogPrintFGFilters(void)
Prints the FG filters(both WL and BL). Used for debugging purposes.
Definition: util-debug-filters.c:390
SCLogAddFGFilterBL
int SCLogAddFGFilterBL(const char *file, const char *function, int line)
Adds a Blacklist(BL) fine-grained(FG) filter. A FG filter BL filter allows messages that don't match ...
Definition: util-debug-filters.c:333
sc_log_module_initialized
int sc_log_module_initialized
Used to indicate whether the logging module has been init or not.
Definition: util-debug.c:106
SCLogFGFilterFile_::func
SCLogFGFilterFunc * func
Definition: util-debug-filters.h:64
util-debug-filters.h
SCLogAddFDFilter
int SCLogAddFDFilter(const char *function)
Adds a Function-Dependent(FD) filter.
Definition: util-debug-filters.c:643
SCLogFGFilterFile_
Structure used to hold FG filters. Encapsulates filename details, func details, which inturn encapsul...
Definition: util-debug-filters.h:62
SCLogFGFilterFunc_::next
struct SCLogFGFilterFunc_ * next
Definition: util-debug-filters.h:55
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
sc_log_fg_filters
SCLogFGFilterFile * sc_log_fg_filters[SC_LOG_FILTER_MAX]
Holds the fine-grained filters.
Definition: util-debug-filters.c:44
SCMutex
#define SCMutex
Definition: threads-debug.h:114
SCLogFDFilterThreadList_::entered
int entered
Definition: util-debug-filters.h:73
SCLogFGFilterFile_::file
char * file
Definition: util-debug-filters.h:63