mgcp_ratectr: do not set talloc destructor on library allocated item

The rate counter and stats item groups, which are allocated in
mgcp_ratectr.c are freed using a talloc destructor that is set in the
context of the item we just allocated using the stats / rate counter API
functions. When we do that, we risk overwriting an already existing
talloc destructor. Lets instead implement own free functions and set
those as talloc_destructor from above (trunk and MGCP config)

Change-Id: Ifc5091e9f95cc721e58d1eb2e55b97102c497706
Related: OS#5201
This commit is contained in:
Philipp Maier 2021-07-29 17:38:34 +02:00
parent 39889e4389
commit 38533ba9b3
4 changed files with 80 additions and 21 deletions

View File

@ -93,7 +93,9 @@ struct mgcp_config;
struct mgcp_trunk;
int mgcp_ratectr_global_alloc(struct mgcp_config *cfg);
void mgcp_ratectr_global_free(struct mgcp_config *cfg);
int mgcp_ratectr_trunk_alloc(struct mgcp_trunk *trunk);
void mgcp_ratectr_trunk_free(struct mgcp_trunk *trunk);
/* Trunk-global common stat items */
enum {
@ -107,4 +109,4 @@ struct mgcp_stat_trunk {
};
int mgcp_stat_trunk_alloc(struct mgcp_trunk *trunk);
void mgcp_stat_trunk_free(struct mgcp_trunk *trunk);

View File

@ -1583,6 +1583,13 @@ void mgcp_trunk_set_keepalive(struct mgcp_trunk *trunk, int interval)
trunk->keepalive_interval, 0);
}
/* Free config, this function is automatically called by talloc_free when the configuration is freed. */
static int config_free_talloc_destructor(struct mgcp_config *cfg)
{
mgcp_ratectr_global_free(cfg);
return 0;
}
/*! allocate configuration with default values.
* (called once at startup by main function) */
struct mgcp_config *mgcp_config_alloc(void)
@ -1621,6 +1628,7 @@ struct mgcp_config *mgcp_config_alloc(void)
}
mgcp_ratectr_global_alloc(cfg);
talloc_set_destructor(cfg, config_free_talloc_destructor);
return cfg;
}

View File

@ -150,12 +150,6 @@ const static struct rate_ctr_group_desc all_rtp_conn_rate_ctr_group_desc = {
.ctr_desc = all_rtp_conn_rate_ctr_desc
};
static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
{
rate_ctr_group_free(rate_ctr_group);
return 0;
}
/*! allocate global rate counters
* (called once at startup).
* \param[in] cfg mgw configuration for which the rate counters are allocated.
@ -173,12 +167,24 @@ int mgcp_ratectr_global_alloc(struct mgcp_config *cfg)
return -EINVAL;
snprintf(ctr_name, sizeof(ctr_name), "%s:general", cfg->domain);
rate_ctr_group_set_name(ratectr->mgcp_general_ctr_group, ctr_name);
talloc_set_destructor(ratectr->mgcp_general_ctr_group, free_rate_counter_group);
general_rate_ctr_index++;
}
return 0;
}
/*! free global rate counters
* (called once at process shutdown).
* \param[in] cfg mgw configuration for which the rate counters are allocated. */
void mgcp_ratectr_global_free(struct mgcp_config *cfg)
{
struct mgcp_ratectr_global *ratectr = &cfg->ratectr;
if (ratectr->mgcp_general_ctr_group) {
rate_ctr_group_free(ratectr->mgcp_general_ctr_group);
ratectr->mgcp_general_ctr_group = NULL;
}
}
/*! allocate trunk specific rate counters
* (called once on trunk initialization).
* \param[in] trunk mgw trunk for which the rate counters are allocated.
@ -200,7 +206,6 @@ int mgcp_ratectr_trunk_alloc(struct mgcp_trunk *trunk)
snprintf(ctr_name, sizeof(ctr_name), "%s-%u:crcx", mgcp_trunk_type_strs_str(trunk->trunk_type),
trunk->trunk_nr);
rate_ctr_group_set_name(ratectr->mgcp_crcx_ctr_group, ctr_name);
talloc_set_destructor(ratectr->mgcp_crcx_ctr_group, free_rate_counter_group);
crcx_rate_ctr_index++;
}
if (ratectr->mgcp_mdcx_ctr_group == NULL) {
@ -211,7 +216,6 @@ int mgcp_ratectr_trunk_alloc(struct mgcp_trunk *trunk)
snprintf(ctr_name, sizeof(ctr_name), "%s-%u:mdcx", mgcp_trunk_type_strs_str(trunk->trunk_type),
trunk->trunk_nr);
rate_ctr_group_set_name(ratectr->mgcp_mdcx_ctr_group, ctr_name);
talloc_set_destructor(ratectr->mgcp_mdcx_ctr_group, free_rate_counter_group);
mdcx_rate_ctr_index++;
}
if (ratectr->mgcp_dlcx_ctr_group == NULL) {
@ -222,7 +226,6 @@ int mgcp_ratectr_trunk_alloc(struct mgcp_trunk *trunk)
snprintf(ctr_name, sizeof(ctr_name), "%s-%u:dlcx", mgcp_trunk_type_strs_str(trunk->trunk_type),
trunk->trunk_nr);
rate_ctr_group_set_name(ratectr->mgcp_dlcx_ctr_group, ctr_name);
talloc_set_destructor(ratectr->mgcp_dlcx_ctr_group, free_rate_counter_group);
dlcx_rate_ctr_index++;
}
if (ratectr->all_rtp_conn_stats == NULL) {
@ -233,7 +236,6 @@ int mgcp_ratectr_trunk_alloc(struct mgcp_trunk *trunk)
snprintf(ctr_name, sizeof(ctr_name), "%s-%u:rtp_conn", mgcp_trunk_type_strs_str(trunk->trunk_type),
trunk->trunk_nr);
rate_ctr_group_set_name(ratectr->all_rtp_conn_stats, ctr_name);
talloc_set_destructor(ratectr->all_rtp_conn_stats, free_rate_counter_group);
all_rtp_conn_rate_ctr_index++;
}
@ -245,12 +247,42 @@ int mgcp_ratectr_trunk_alloc(struct mgcp_trunk *trunk)
snprintf(ctr_name, sizeof(ctr_name), "%s-%u:e1", mgcp_trunk_type_strs_str(trunk->trunk_type),
trunk->trunk_nr);
rate_ctr_group_set_name(ratectr->e1_stats, ctr_name);
talloc_set_destructor(ratectr->e1_stats, free_rate_counter_group);
mdcx_rate_ctr_index++;
}
return 0;
}
/*! free trunk specific rate counters
* (called once when trunk is freed).
* \param[in] trunk mgw trunk on which the rate counters are allocated. */
void mgcp_ratectr_trunk_free(struct mgcp_trunk *trunk)
{
struct mgcp_ratectr_trunk *ratectr = &trunk->ratectr;
if (ratectr->mgcp_crcx_ctr_group) {
rate_ctr_group_free(ratectr->mgcp_crcx_ctr_group);
ratectr->mgcp_crcx_ctr_group = NULL;
}
if (ratectr->mgcp_mdcx_ctr_group) {
rate_ctr_group_free(ratectr->mgcp_mdcx_ctr_group);
ratectr->mgcp_mdcx_ctr_group = NULL;
}
if (ratectr->mgcp_dlcx_ctr_group) {
rate_ctr_group_free(ratectr->mgcp_dlcx_ctr_group);
ratectr->mgcp_dlcx_ctr_group = NULL;
}
if (ratectr->all_rtp_conn_stats) {
rate_ctr_group_free(ratectr->all_rtp_conn_stats);
ratectr->all_rtp_conn_stats = NULL;
}
/* E1 specific */
if (ratectr->e1_stats) {
rate_ctr_group_free(ratectr->e1_stats);
ratectr->e1_stats = NULL;
}
}
const struct osmo_stat_item_desc trunk_stat_desc[] = {
[TRUNK_STAT_ENDPOINTS_TOTAL] = { "endpoints:total",
"Number of endpoints that exist on the trunk",
@ -268,12 +300,6 @@ const struct osmo_stat_item_group_desc trunk_statg_desc = {
.item_desc = trunk_stat_desc,
};
static int free_stat_item_group(struct osmo_stat_item_group *stat_item_group)
{
osmo_stat_item_group_free(stat_item_group);
return 0;
}
/*! allocate trunk specific stat items
* (called once on trunk initialization).
* \param[in] trunk for which the stat items are allocated.
@ -290,8 +316,20 @@ int mgcp_stat_trunk_alloc(struct mgcp_trunk *trunk)
snprintf(stat_name, sizeof(stat_name), "%s-%u:common", mgcp_trunk_type_strs_str(trunk->trunk_type),
trunk->trunk_nr);
osmo_stat_item_group_set_name(stats->common, stat_name);
talloc_set_destructor(stats->common, free_stat_item_group);
common_stat_index++;
return 0;
}
/*! free trunk specific stat items
* (called once when trunk is freed).
* \param[in] trunk on which the stat items are allocated. */
void mgcp_stat_trunk_free(struct mgcp_trunk *trunk)
{
struct mgcp_stat_trunk *stats = &trunk->stats;
if (stats->common) {
osmo_stat_item_group_free(stats->common);
stats->common = NULL;
}
}

View File

@ -35,7 +35,17 @@ const struct value_string mgcp_trunk_type_strs[] = {
{ 0, NULL }
};
/*! allocate trunk and add it (if required) to the trunk list.
/* Free trunk, this function is automatically called by talloc_free when the trunk is freed. It does not free the
* endpoints on the trunk, this must be done separately before freeing the trunk. */
static int trunk_free_talloc_destructor(struct mgcp_trunk *trunk)
{
llist_del(&trunk->entry);
mgcp_ratectr_trunk_free(trunk);
mgcp_stat_trunk_free(trunk);
return 0;
}
/*! allocate trunk and add it to the trunk list.
* (called once at startup by VTY).
* \param[in] cfg mgcp configuration.
* \param[in] ttype trunk type.
@ -66,6 +76,7 @@ struct mgcp_trunk *mgcp_trunk_alloc(struct mgcp_config *cfg, enum mgcp_trunk_typ
mgcp_ratectr_trunk_alloc(trunk);
mgcp_stat_trunk_alloc(trunk);
talloc_set_destructor(trunk, trunk_free_talloc_destructor);
return trunk;
}