X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_vl_lookup.c;h=a0237841eb43a7aebea172bed7246cd71f0e0bea;hb=db35efb33e81d0a013e09a8a6ffa362ad5962f7c;hp=722c45230fce5669f34d999a644a0a186a9273c5;hpb=a320c4a23fee0e900a7cd2310c771b8dbd2a2d82;p=collectd.git diff --git a/src/utils_vl_lookup.c b/src/utils_vl_lookup.c index 722c4523..a0237841 100644 --- a/src/utils_vl_lookup.c +++ b/src/utils_vl_lookup.c @@ -1,6 +1,6 @@ /** * collectd - src/utils_vl_lookup.c - * Copyright (C) 2012 Florian Forster + * Copyright (C) 2012 Florian Forster * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -26,12 +26,17 @@ #include "collectd.h" +#include #include #include "common.h" #include "utils_vl_lookup.h" #include "utils_avltree.h" +#if HAVE_LIBKSTAT +kstat_ctl_t *kc; +#endif /* HAVE_LIBKSTAT */ + #if BUILD_TEST # define sstrncpy strncpy # define plugin_log(s, ...) do { \ @@ -86,6 +91,7 @@ struct user_obj_s struct user_class_s { + pthread_mutex_t lock; void *user_class; identifier_match_t match; user_obj_t *user_obj_list; /* list of user_obj */ @@ -151,7 +157,7 @@ static int lu_copy_ident_to_match_part (part_match_t *match_part, /* {{{ */ assert (sizeof (match_part->str) > len); /* strip trailing slash */ match_part->str[len - 2] = 0; - + status = regcomp (&match_part->regex, match_part->str, /* flags = */ REG_EXTENDED); if (status != 0) @@ -163,7 +169,7 @@ static int lu_copy_ident_to_match_part (part_match_t *match_part, /* {{{ */ return (EINVAL); } match_part->is_regex = 1; - + return (0); } /* }}} int lu_copy_ident_to_match_part */ @@ -191,19 +197,19 @@ static int lu_copy_ident_to_match (identifier_match_t *match, /* {{{ */ return (0); } /* }}} int lu_copy_ident_to_match */ +/* user_class->lock must be held when calling this function */ static void *lu_create_user_obj (lookup_t *obj, /* {{{ */ data_set_t const *ds, value_list_t const *vl, user_class_t *user_class) { user_obj_t *user_obj; - user_obj = malloc (sizeof (*user_obj)); + user_obj = calloc (1, sizeof (*user_obj)); if (user_obj == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); return (NULL); } - memset (user_obj, 0, sizeof (*user_obj)); user_obj->next = NULL; user_obj->user_obj = obj->cb_user_class (ds, vl, user_class->user_class); @@ -245,6 +251,7 @@ static void *lu_create_user_obj (lookup_t *obj, /* {{{ */ return (user_obj); } /* }}} void *lu_create_user_obj */ +/* user_class->lock must be held when calling this function */ static user_obj_t *lu_find_user_obj (user_class_t *user_class, /* {{{ */ value_list_t const *vl) { @@ -294,14 +301,18 @@ static int lu_handle_user_class (lookup_t *obj, /* {{{ */ || !lu_part_matches (&user_class->match.host, vl->host)) return (1); + pthread_mutex_lock (&user_class->lock); user_obj = lu_find_user_obj (user_class, vl); if (user_obj == NULL) { /* call lookup_class_callback_t() and insert into the list of user objects. */ user_obj = lu_create_user_obj (obj, ds, vl, user_class); - if (user_obj == NULL) + if (user_obj == NULL) { + pthread_mutex_unlock (&user_class->lock); return (-1); + } } + pthread_mutex_unlock (&user_class->lock); status = obj->cb_user_obj (ds, vl, user_class->user_class, user_obj->user_obj); @@ -325,7 +336,7 @@ static int lu_handle_user_class_list (lookup_t *obj, /* {{{ */ { user_class_list_t *ptr; int retval = 0; - + for (ptr = user_class_list; ptr != NULL; ptr = ptr->next) { int status; @@ -361,16 +372,15 @@ static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */ return (NULL); } - by_type = malloc (sizeof (*by_type)); + by_type = calloc (1, sizeof (*by_type)); if (by_type == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); sfree (type_copy); return (NULL); } - memset (by_type, 0, sizeof (*by_type)); by_type->wildcard_plugin_list = NULL; - + by_type->by_plugin_tree = c_avl_create ((void *) strcmp); if (by_type->by_plugin_tree == NULL) { @@ -391,7 +401,7 @@ static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */ sfree (type_copy); return (NULL); } - + return (by_type); } /* }}} by_type_entry_t *lu_search_by_type */ @@ -402,7 +412,7 @@ static int lu_add_by_plugin (by_type_entry_t *by_type, /* {{{ */ identifier_match_t const *match = &user_class_list->entry.match; /* Lookup user_class_list from the per-plugin structure. If this is the first - * user_class to be added, the blocks return immediately. Otherwise they will + * user_class to be added, the block returns immediately. Otherwise they will * set "ptr" to non-NULL. */ if (match->plugin.is_regex) { @@ -487,6 +497,7 @@ static void lu_destroy_user_class_list (lookup_t *obj, /* {{{ */ lu_destroy_user_obj (obj, user_class_list->entry.user_obj_list); user_class_list->entry.user_obj_list = NULL; + pthread_mutex_destroy (&user_class_list->entry.lock); sfree (user_class_list); user_class_list = next; @@ -496,7 +507,7 @@ static void lu_destroy_user_class_list (lookup_t *obj, /* {{{ */ static void lu_destroy_by_type (lookup_t *obj, /* {{{ */ by_type_entry_t *by_type) { - + while (42) { char *plugin = NULL; @@ -531,13 +542,12 @@ lookup_t *lookup_create (lookup_class_callback_t cb_user_class, /* {{{ */ lookup_free_class_callback_t cb_free_class, lookup_free_obj_callback_t cb_free_obj) { - lookup_t *obj = malloc (sizeof (*obj)); + lookup_t *obj = calloc (1, sizeof (*obj)); if (obj == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); return (NULL); } - memset (obj, 0, sizeof (*obj)); obj->by_type_tree = c_avl_create ((void *) strcmp); if (obj->by_type_tree == NULL) @@ -592,13 +602,13 @@ int lookup_add (lookup_t *obj, /* {{{ */ if (by_type == NULL) return (-1); - user_class_obj = malloc (sizeof (*user_class_obj)); + user_class_obj = calloc (1, sizeof (*user_class_obj)); if (user_class_obj == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); return (ENOMEM); } - memset (user_class_obj, 0, sizeof (*user_class_obj)); + pthread_mutex_init (&user_class_obj->entry.lock, /* attr = */ NULL); user_class_obj->entry.user_class = user_class; lu_copy_ident_to_match (&user_class_obj->entry.match, ident, group_by); user_class_obj->entry.user_obj_list = NULL; @@ -641,6 +651,6 @@ int lookup_search (lookup_t *obj, /* {{{ */ return (status); retval += status; } - + return (retval); } /* }}} lookup_search */