diff --git a/src/inc_internal/maat_ex_data.h b/src/inc_internal/maat_ex_data.h index 9959838..a3dbca2 100644 --- a/src/inc_internal/maat_ex_data.h +++ b/src/inc_internal/maat_ex_data.h @@ -43,7 +43,7 @@ struct ex_data_runtime; /* ex_data_runtime API */ struct ex_data_runtime * ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn, - struct maat_garbage_bin *garbage_bin, struct log_handle *logger); + struct log_handle *logger); void ex_data_runtime_free(struct ex_data_runtime *ex_data_rt); void ex_data_runtime_commit(struct ex_data_runtime *ex_data_rt); diff --git a/src/maat_api.c b/src/maat_api.c index 181d989..8f1062a 100644 --- a/src/maat_api.c +++ b/src/maat_api.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include "maat_utils.h" @@ -438,7 +439,9 @@ void maat_free(struct maat *maat_instance) } void *ret = NULL; - + while (0 == maat_instance->is_running) { + usleep(500 * 1000); + } maat_instance->is_running = 0; pthread_join(maat_instance->cfg_mon_thread, &ret); } diff --git a/src/maat_bool_plugin.c b/src/maat_bool_plugin.c index 39a858c..cdf76f7 100644 --- a/src/maat_bool_plugin.c +++ b/src/maat_bool_plugin.c @@ -189,7 +189,7 @@ void *bool_plugin_runtime_new(void *bool_plugin_schema, int max_thread_num, bool_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_container_free, - garbage_bin, logger); + logger); bool_plugin_rt->ref_garbage_bin = garbage_bin; bool_plugin_rt->logger = logger; diff --git a/src/maat_compile.c b/src/maat_compile.c index cdafa48..9c73553 100644 --- a/src/maat_compile.c +++ b/src/maat_compile.c @@ -858,7 +858,7 @@ int compare_literal_id(const void *pa, const void *pb) struct maat_literal_id *la = (struct maat_literal_id *)pa; struct maat_literal_id *lb = (struct maat_literal_id *)pb; - int ret = la->vtable_id - lb->vtable_id; + long long ret = la->vtable_id - lb->vtable_id; if (0 == ret) { ret = la->group_id - lb->group_id; } @@ -927,7 +927,7 @@ maat_clause_hash_fetch_clause(struct compile_runtime *compile_rt, HASH_FIND(hh, compile_rt->clause_by_literals_hash, literal_ids, n_literal_id * sizeof(struct maat_literal_id), clause); - if (!clause) { + if (NULL == clause) { clause = ALLOC(struct maat_clause, 1); clause->clause_id = maat_runtime_get_sequence(compile_rt->ref_maat_rt, "clause_id"); clause->n_literal_id = n_literal_id; diff --git a/src/maat_ex_data.c b/src/maat_ex_data.c index 2990c8d..2d60295 100644 --- a/src/maat_ex_data.c +++ b/src/maat_ex_data.c @@ -25,7 +25,6 @@ struct ex_data_runtime { size_t cache_size; struct rcu_hash_table *htable; - struct maat_garbage_bin *ref_garbage_bin; int table_id; struct log_handle *logger; @@ -40,10 +39,9 @@ UT_icd ut_cache_row_icd = {sizeof(char*), NULL, NULL, cache_row_free}; struct ex_data_runtime * ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn, - struct maat_garbage_bin *garbage_bin, struct log_handle *logger) + struct log_handle *logger) { - if (NULL == data_free_fn || NULL == garbage_bin || - NULL == logger) { + if (NULL == data_free_fn || NULL == logger) { return NULL; } @@ -52,7 +50,6 @@ ex_data_runtime_new(int table_id, rcu_hash_data_free_fn *data_free_fn, utarray_new(ex_data_rt->cache_rows, &ut_cache_row_icd); ex_data_rt->htable = rcu_hash_new(data_free_fn); ex_data_rt->table_id = table_id; - ex_data_rt->ref_garbage_bin = garbage_bin; ex_data_rt->logger = logger; return ex_data_rt; @@ -203,25 +200,26 @@ struct ex_container *ex_container_new(void *ex_data, void *custom_data) void ex_container_free(void *schema, void *data) { /* schema is NULL if not call ex_data_runtime_set_ex_container_schema */ - if (NULL == schema || NULL == data) { + if (NULL == data) { return; } struct ex_container *ex_container = (struct ex_container *)data; - struct ex_container_schema *container_schema = (struct ex_container_schema *)schema; - long argl = container_schema->ex_schema->argl; - void *argp = container_schema->ex_schema->argp; - - if (ex_container->ex_data != NULL - && container_schema->ex_schema->free_func != NULL) { - container_schema->ex_schema->free_func(container_schema->table_id, - &(ex_container->ex_data), argl, argp); - } - if (ex_container->custom_data != NULL) { FREE(ex_container->custom_data); } + struct ex_container_schema *container_schema = (struct ex_container_schema *)schema; + if (container_schema != NULL && container_schema->ex_schema != NULL) { + long argl = container_schema->ex_schema->argl; + void *argp = container_schema->ex_schema->argp; + + if (ex_container->ex_data != NULL && container_schema->ex_schema->free_func != NULL) { + container_schema->ex_schema->free_func(container_schema->table_id, + &(ex_container->ex_data), argl, argp); + } + } + FREE(ex_container); } diff --git a/src/maat_fqdn_plugin.c b/src/maat_fqdn_plugin.c index 668143c..067feb9 100644 --- a/src/maat_fqdn_plugin.c +++ b/src/maat_fqdn_plugin.c @@ -173,7 +173,7 @@ void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, int max_thread_num, struct fqdn_plugin_runtime *fqdn_plugin_rt = ALLOC(struct fqdn_plugin_runtime, 1); fqdn_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_container_free, - garbage_bin, logger); + logger); fqdn_plugin_rt->ref_garbage_bin = garbage_bin; fqdn_plugin_rt->logger = logger; diff --git a/src/maat_ip.c b/src/maat_ip.c index 6f0f476..6a2fe85 100644 --- a/src/maat_ip.c +++ b/src/maat_ip.c @@ -403,6 +403,11 @@ void ip_runtime_free(void *ip_runtime) ip_rt->ip_matcher = NULL; } + if (ip_rt->intval_matcher != NULL) { + interval_matcher_free(ip_rt->intval_matcher); + ip_rt->intval_matcher = NULL; + } + if (ip_rt->htable != NULL) { rcu_hash_free(ip_rt->htable); ip_rt->htable = NULL; @@ -614,6 +619,10 @@ int ip_runtime_commit(void *ip_runtime, const char *table_name) FREE(rules); } + if (intval_rules != NULL) { + FREE(intval_rules); + } + if (ex_data_array != NULL) { FREE(ex_data_array); } diff --git a/src/maat_ip_plugin.c b/src/maat_ip_plugin.c index 3ea7e5f..9c1f14c 100644 --- a/src/maat_ip_plugin.c +++ b/src/maat_ip_plugin.c @@ -358,7 +358,7 @@ void *ip_plugin_runtime_new(void *ip_plugin_schema, int max_thread_num, struct ip_plugin_runtime *ip_plugin_rt = ALLOC(struct ip_plugin_runtime, 1); ip_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_container_free, - garbage_bin, logger); + logger); ip_plugin_rt->ref_garbage_bin = garbage_bin; ip_plugin_rt->logger = logger; diff --git a/src/maat_plugin.c b/src/maat_plugin.c index b7c35f8..80c1f3c 100644 --- a/src/maat_plugin.c +++ b/src/maat_plugin.c @@ -267,7 +267,7 @@ void *plugin_runtime_new(void *plugin_schema, int max_thread_num, struct plugin_runtime *plugin_rt = ALLOC(struct plugin_runtime, 1); plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_container_free, - garbage_bin, logger); + logger); plugin_rt->ref_garbage_bin = garbage_bin; plugin_rt->logger = logger; diff --git a/test/maat_ex_data_gtest.cpp b/test/maat_ex_data_gtest.cpp index edb7cb7..c3cb702 100644 --- a/test/maat_ex_data_gtest.cpp +++ b/test/maat_ex_data_gtest.cpp @@ -29,7 +29,7 @@ void ex_data_new_cb(const char *table_name, int table_id, const char *key, const int valid = 0, tag = 0; int ret = sscanf(table_line, "%d\t%s\t%s%d\t%d", &(u->id), u->ip_addr, u->name, &valid, &tag); EXPECT_EQ(ret, 5); - u->ref_cnt = 1; + u->ref_cnt = 0; *ad = u; (*counter)++; } @@ -38,9 +38,12 @@ void ex_data_free_cb(int table_id, void **ad, long argl, void *argp) { struct user_info *u = (struct user_info *)(*ad); if ((__sync_sub_and_fetch(&u->ref_cnt, 1) == 0)) { + printf("yes, free(u)\n"); free(u); *ad = NULL; - } + } else { + printf("u->ref_cnt:%d\n", u->ref_cnt); + } } void ex_data_dup_cb(int table_id, void **to, void **from, long argl, void *argp) @@ -56,8 +59,7 @@ TEST(EXDataRuntime, Update) { ASSERT_GT(table_id, 0); int ex_data_counter = 0; - struct maat_garbage_bin *garbage_bin = maat_garbage_bin_new(10); - struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, ex_container_free, garbage_bin, g_logger); + struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, ex_container_free, g_logger); struct ex_data_schema *ex_schema = ex_data_schema_new(ex_data_new_cb, ex_data_free_cb, ex_data_dup_cb, 0, &ex_data_counter); @@ -87,15 +89,17 @@ TEST(EXDataRuntime, Update) { struct user_info *info = (struct user_info *)res_data1; EXPECT_EQ(0, strcmp(info->name, "mahuateng")); EXPECT_EQ(info->id, 1); + ex_data_free_cb(table_id, (void **)&res_data1, 0, NULL); + ex_container = NULL; void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.2", 11); EXPECT_TRUE(res_data2 != NULL); info = (struct user_info *)res_data2; EXPECT_EQ(0, strcmp(info->name, "liyanhong")); EXPECT_EQ(info->id, 2); + ex_data_free_cb(table_id, (void **)&res_data2, 0, NULL); - maat_garbage_bin_free(garbage_bin); ex_data_runtime_free(ex_data_rt); ex_data_schema_free(ex_schema); } diff --git a/test/maat_framework_gtest.cpp b/test/maat_framework_gtest.cpp index 073e0c4..d6897d3 100644 --- a/test/maat_framework_gtest.cpp +++ b/test/maat_framework_gtest.cpp @@ -2186,7 +2186,7 @@ void fqdn_plugin_ex_new_cb(const char *table_name, int table_id, const char *key EXPECT_EQ(ret, 0); sscanf(table_line + column_offset, "catid=%d", &ud->catid); - ud->ref_cnt = 1; + ud->ref_cnt = 0; *ad = ud; (*counter)++; } @@ -2268,7 +2268,7 @@ void bool_plugin_ex_new_cb(const char *table_name, int table_id, const char *key ud->name = (char *)malloc(column_len+1); memcpy(ud->name, table_line+column_offset, column_len); - ud->ref_cnt = 1; + ud->ref_cnt = 0; *ad = ud; (*counter)++; } @@ -3946,7 +3946,7 @@ void plugin_ex_new_cb(const char *table_name, int table_id, const char *key, int valid = 0, tag = 0; int ret = sscanf(table_line, "%d\t%s\t%s%d\t%d", &(u->id), u->ip_addr, u->name, &valid, &tag); EXPECT_EQ(ret, 5); - u->ref_cnt = 1; + u->ref_cnt = 0; *ad = u; (*counter)++; }