pull/292/merge
Mark Murawski 2 days ago committed by GitHub
commit f0e92e404b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

File diff suppressed because it is too large Load Diff

@ -1196,9 +1196,48 @@ struct ast_group_info;
/*! \brief Split a group string into group and category, returning a default category if none is provided. */
int ast_app_group_split_group(const char *data, char *group, int group_max, char *category, int category_max);
/*! \brief Remove channel assignments for the specified group */
int ast_app_group_remove_all_channels(const char *group, const char *category);
/*! \brief Rename a group@category while retaining all the channel memberships
* \return Number of rename operations performed
* \retval -1 on Error
* \retval 0 group@category was not found
*/
int ast_app_group_rename(const char *old_group, const char *old_category, const char *new_group, const char *new_category);
/*! \brief Set the group for a channel, splitting the provided data into group and category, if specified. */
int ast_app_group_set_channel(struct ast_channel *chan, const char *data);
/*!
* \brief Set a group variable for a group@category
*
* \param chan channel (if any) that is setting the group variable (can be NULL)
* \param group group to set the variable on (cannot be null)
* \param category category to set the variable on (cannot be null)
* \param name name of the variable to set (cannot be null)
* \param value value of the variable to set (cannot be null)
*
* \retval 0 On success
* \retval -1 On failure (group@category not found)
* \retval -2 On failure (input validation error)
*/
int ast_app_group_set_var(struct ast_channel *chan, const char *group, const char *category, const char *name, const char *value);
/*!
* \brief Get a group variable for a group@category
*
* \param group group to get the variable from
* \param category category to get the variable from
* \param name name of the variable to get
*
* \return Allocated string containing variable contents
* \retval NULL On failure (variable in group@category not found)
*
* \post caller must ast_free() the returned value
*/
char *ast_app_group_get_var(const char *group, const char *category, const char *name);
/*! \brief Get the current channel count of the specified group and category. */
int ast_app_group_get_count(const char *group, const char *category);
@ -1223,6 +1262,15 @@ struct ast_group_info *ast_app_group_list_head(void);
/*! \brief Unlock the group count list */
int ast_app_group_list_unlock(void);
/*! \brief Read Lock the group meta list */
int ast_app_group_meta_rdlock(void);
/*! \brief Get the head of the group meta list */
struct ast_group_meta *ast_app_group_meta_head(void);
/*! \brief Unlock the group meta list */
int ast_app_group_meta_unlock(void);
/*!
\brief Define an application argument
\param name The name of the argument

@ -122,6 +122,8 @@ References:
#ifndef _ASTERISK_CHANNEL_H
#define _ASTERISK_CHANNEL_H
#include <regex.h>
#include "asterisk/alertpipe.h"
#include "asterisk/abstract_jb.h"
#include "asterisk/astobj2.h"
@ -175,6 +177,9 @@ extern "C" {
#define MAX_MUSICCLASS 80 /*!< Max length of the music class setting */
#define AST_MAX_USER_FIELD 256 /*!< Max length of the channel user field */
#define MAX_GROUP_LEN 80 /*!< Max length of a channel group */
#define MAX_CATEGORY_LEN 80 /*!< Max length of a channel group category */
#include "asterisk/frame.h"
#include "asterisk/chanvars.h"
#include "asterisk/config.h"
@ -2980,6 +2985,18 @@ struct ast_group_info {
AST_LIST_ENTRY(ast_group_info) group_list;
};
/*! \brief list of groups currently in use, with a pointer to a list of channels within the groups */
struct ast_group_meta {
int num_channels; /*!< number of channels in this group */
struct varshead varshead; /*!< A linked list for group variables. See \ref AstGroupVar */
AST_LIST_ENTRY(ast_group_info) channels_list; /*!< List of channels in this group */
AST_LIST_ENTRY(ast_group_meta) group_meta_list; /*!< Next group */
char category[MAX_CATEGORY_LEN];
char group[MAX_GROUP_LEN];
};
#define ast_channel_lock(chan) ao2_lock(chan)
#define ast_channel_unlock(chan) ao2_unlock(chan)
#define ast_channel_trylock(chan) ao2_trylock(chan)
@ -3211,6 +3228,7 @@ struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name
struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context);
/*!
* \brief Find a channel by a uniqueid
*
* \param uniqueid The uniqueid to search for
@ -3220,6 +3238,34 @@ struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *cont
*/
struct ast_channel *ast_channel_get_by_uniqueid(const char *uniqueid);
/*
* \brief Find a channel by a regex string
*
* \arg regex_string the regex pattern to search for
*
* Return a channel that where the regex pattern matches the channel name
*
* \retval a channel that matches the regex pattern
* \retval NULL if no channel was found or pattern is bad
*
* \since 23
*/
struct ast_channel *ast_channel_get_by_regex(const char *regex_string);
/*!
* \brief Find a channel by a regex pattern
*
* \arg regex the compiled regex pattern to search for
*
* Return a channel that where the regex pattern matches the channel name
*
* \retval a channel that matches the regex pattern
* \retval NULL if no channel was found
*
* \since 23
*/
struct ast_channel *ast_channel_get_by_regex_compiled(regex_t *regex);
/*! @} End channel search functions. */
/*!

@ -61,6 +61,7 @@
#include "asterisk/indications.h"
#include "asterisk/linkedlists.h"
#include "asterisk/threadstorage.h"
#include "asterisk/manager.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/astobj2.h"
@ -69,6 +70,95 @@
#include "asterisk/json.h"
#include "asterisk/format_cache.h"
/*** DOCUMENTATION
<managerEvent language="en_US" name="GroupCreate">
<managerEventInstance class="EVENT_FLAG_REPORTING">
<synopsis>Raised when a group has been created. Note: GroupChannelAdd events will show which channels have been assigned this group@category</synopsis>
<syntax>
<parameter name="Category">
<para>The category portion of the group@category that has been created</para>
</parameter>
<parameter name="Group">
<para>The group portion of the group@category that has been created</para>
</parameter>
</syntax>
</managerEventInstance>
</managerEvent>
<managerEvent language="en_US" name="GroupChannelAdd">
<managerEventInstance class="EVENT_FLAG_REPORTING">
<synopsis>Raised when a channel is now a part of a group@category GROUP() assignment</synopsis>
<syntax>
<parameter name="Channel">
<para>Channel that is now part of this group@category</para>
</parameter>
<parameter name="Uniqueid">
<para>Uniqueid of the channel that is now part of this group@category</para>
</parameter>
<parameter name="Category">
<para>The category portion of the group@category of the GROUP() that the channel is now a part of</para>
</parameter>
<parameter name="Group">
<para>The group portion of the group@category of the GROUP() that the channel is now a part of</para>
</parameter>
</syntax>
</managerEventInstance>
</managerEvent>
<managerEvent language="en_US" name="GroupVarSet">
<managerEventInstance class="EVENT_FLAG_REPORTING">
<synopsis>Raised in response to when a Group Variable is set using GROUP_VAR() on a group@category GROUP()</synopsis>
<syntax>
<parameter name="Group">
<para>Name of the group that the variable was set on</para>
</parameter>
<parameter name="Category">
<para>Name of the category that the variable was set on</para>
</parameter>
<parameter name="Channel">
<para>Channel (if any) that caused the variable to be set</para>
</parameter>
<parameter name="Variable">
<para>The variable that was set</para>
</parameter>
<parameter name="Value">
<para>The value of the variable</para>
</parameter>
</syntax>
</managerEventInstance>
</managerEvent>
<managerEvent language="en_US" name="GroupChannelRemove">
<managerEventInstance class="EVENT_FLAG_REPORTING">
<synopsis>Raised when a channel is no longer part of a group@category GROUP() assignment</synopsis>
<syntax>
<parameter name="Channel">
<para>Channel that no longer is part of this group@category</para>
</parameter>
<parameter name="Uniqueid">
<para>Uniqueid of the channel that is no longer is part of this group</para>
</parameter>
<parameter name="Category">
<para>The category portion of the group@category of the GROUP() that the channel was a part of</para>
</parameter>
<parameter name="Group">
<para>The group portion of the group@category of the GROUP() that the channel was a part of</para>
</parameter>
</syntax>
</managerEventInstance>
</managerEvent>
<managerEvent language="en_US" name="GroupDestroy">
<managerEventInstance class="EVENT_FLAG_REPORTING">
<synopsis>Raised when a group has been completely removed. Note: GroupChannelRemove events will show which channels no longer are assigned to this group@category</synopsis>
<syntax>
<parameter name="Category">
<para>The category portion of the group@category that is no longer present</para>
</parameter>
<parameter name="Group">
<para>The group portion of the group@category that is no longer present</para>
</parameter>
</syntax>
</managerEventInstance>
</managerEvent>
***/
AST_THREADSTORAGE_PUBLIC(ast_str_thread_global_buf);
static pthread_t shaun_of_the_dead_thread = AST_PTHREADT_NULL;
@ -118,10 +208,36 @@ static void *shaun_of_the_dead(void *data)
return NULL;
}
struct group_data_store;
/* \brief a single group assignment entry */
struct group_list_entry {
struct ast_str *group; /*!< A group assignment is group@category, this is the group part */
struct ast_str *category; /*!< This is the category part */
struct group_data_store *group_store; /*!< The group_data_store we live on */
AST_LIST_ENTRY(group_list_entry) entries; /*!< Next group */
};
AST_LIST_HEAD_NOLOCK(group_list, group_list_entry); /*!< All GROUP assignments */
/* Datastore for GROUP() entry storage */
struct group_data_store {
void *datastore; /*!< Pointer to the datastore that was allocated in */
struct ast_channel *chan; /*!< Channel this datastore is on */
struct group_list group_list_head; /*!< All the GROUPs that this channel is in */
AST_LIST_ENTRY(group_data_store) entries; /*!< Next GROUP datastore */
};
AST_RWLIST_HEAD_STATIC(group_stores_list, group_data_store);
#define AST_MAX_FORMATS 10
static AST_RWLIST_HEAD_STATIC(groups, ast_group_info);
static AST_RWLIST_HEAD_STATIC(groups, ast_group_info); /*!< List of channels that are in groups */
static AST_RWLIST_HEAD_STATIC(groups_meta, ast_group_meta); /*!< List of groups and their metadata */
/*!
* \brief This function presents a dialtone and reads an extension into 'collect'
@ -2191,51 +2307,274 @@ int ast_app_group_split_group(const char *data, char *group, int group_max, char
return res;
}
int ast_app_group_set_channel(struct ast_channel *chan, const char *data)
static int ast_app_group_add_meta_ifneeded(const char *group, const char *category)
{
int res = 0;
char group[80] = "", category[80] = "";
struct ast_group_info *gi = NULL;
size_t len = 0;
struct ast_group_meta *gmi = NULL; /*!< Group metadatas */
if (ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category))) {
return -1;
AST_RWLIST_WRLOCK(&groups_meta);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups_meta, gmi, group_meta_list) {
if (!strcasecmp(gmi->group, group) && !strcasecmp(gmi->category, category)) {
break; /* We only have one list item per group@category */
}
}
AST_RWLIST_TRAVERSE_SAFE_END;
/* Calculate memory we will need if this is new */
len = sizeof(*gi) + strlen(group) + 1;
if (!ast_strlen_zero(category)) {
len += strlen(category) + 1;
if (!gmi) {
if (!(gmi = ast_calloc(1, sizeof(struct ast_group_meta)))) {
AST_RWLIST_UNLOCK(&groups_meta);
return -1;
}
ast_copy_string(gmi->group, group, MAX_GROUP_LEN);
if (!ast_strlen_zero(category)) {
ast_copy_string(gmi->category, category, MAX_CATEGORY_LEN);
}
AST_RWLIST_INSERT_TAIL(&groups_meta, gmi, group_meta_list);
manager_event(EVENT_FLAG_DIALPLAN, "GroupCreate",
"Category: %s\r\n"
"Group: %s\r\n",
category, group);
}
AST_RWLIST_WRLOCK(&groups);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups, gi, group_list) {
if ((gi->chan == chan) && ((ast_strlen_zero(category) && ast_strlen_zero(gi->category)) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))) {
AST_RWLIST_REMOVE_CURRENT(group_list);
ast_free(gi);
break;
gmi->num_channels++;
AST_RWLIST_UNLOCK(&groups_meta);
return 1;
}
/* NOTE: Ideally we would also be able to use direct pointers to the group meta, so we don't have to search */
static int ast_app_group_remove_meta_ifneeded(const char *group, const char *category)
{
struct ast_group_meta *gmi = NULL; /*!< Group metadatas */
struct varshead *headp;
struct ast_var_t *vardata;
if (!category) {
category = "";
}
AST_RWLIST_WRLOCK(&groups_meta);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups_meta, gmi, group_meta_list) {
if (!strcasecmp(gmi->group, group) && !strcasecmp(gmi->category, category) && (--gmi->num_channels <= 0)) {
/* Remove all group variables */
headp = &gmi->varshead;
while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries))) {
ast_var_delete(vardata);
}
AST_RWLIST_REMOVE_CURRENT(group_meta_list);
ast_free(gmi);
break; /* We only have one list item per group@category */
}
}
AST_RWLIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&groups_meta);
if (ast_strlen_zero(group)) {
/* Enable unsetting the group */
} else if ((gi = ast_calloc(1, len))) {
gi->chan = chan;
gi->group = (char *) gi + sizeof(*gi);
strcpy(gi->group, group);
if (!ast_strlen_zero(category)) {
gi->category = (char *) gi + sizeof(*gi) + strlen(group) + 1;
strcpy(gi->category, category);
if (gmi) {
manager_event(EVENT_FLAG_DIALPLAN, "GroupDestroy",
"Category: %s\r\n"
"Group: %s\r\n",
category, group);
}
return (gmi != NULL);
}
int ast_app_group_set_channel(struct ast_channel *chan, const char *data)
{
int res = 0;
char group[MAX_GROUP_LEN] = "", category[MAX_CATEGORY_LEN] = "";
struct ast_group_info *gi = NULL;
size_t len = 0;
if (ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category))) {
return -1;
}
/* Calculate memory we will need if this is new */
len = sizeof(*gi) + strlen(group) + 1;
if (!ast_strlen_zero(category)) {
len += strlen(category) + 1;
}
/* Remove previous group assignment within this category if there is one */
AST_RWLIST_WRLOCK(&groups);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups, gi, group_list) {
if ((gi->chan == chan) && ((ast_strlen_zero(category) && ast_strlen_zero(gi->category)) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))) {
/* Find our group meta data, remove the entire group metadata if we're the last channel */
ast_app_group_remove_meta_ifneeded(gi->group, gi->category);
manager_event(EVENT_FLAG_DIALPLAN, "GroupChannelRemove",
"Channel: %s\r\n"
"Category: %s\r\n"
"Group: %s\r\n"
"Uniqueid: %s\r\n",
ast_channel_name(chan),
gi->category, gi->group,
ast_channel_uniqueid(chan));
AST_RWLIST_REMOVE_CURRENT(group_list);
ast_free(gi);
break;
}
}
AST_RWLIST_TRAVERSE_SAFE_END;
if (ast_strlen_zero(group)) {
/* Enable unsetting the group */
} else if ((gi = ast_calloc(1, len))) {
gi->chan = chan;
gi->group = (char *) gi + sizeof(*gi);
strcpy(gi->group, group);
if (!ast_strlen_zero(category)) {
gi->category = (char *) gi + sizeof(*gi) + strlen(group) + 1;
strcpy(gi->category, category);
}
AST_RWLIST_INSERT_TAIL(&groups, gi, group_list);
ast_app_group_add_meta_ifneeded(group, category);
manager_event(EVENT_FLAG_DIALPLAN, "GroupChannelAdd",
"Channel: %s\r\n"
"Category: %s\r\n"
"Group: %s\r\n"
"Uniqueid: %s\r\n",
ast_channel_name(chan),
category, group,
ast_channel_uniqueid(chan));
} else {
res = -1;
}
AST_RWLIST_UNLOCK(&groups);
return res;
}
int ast_app_group_set_var(struct ast_channel *chan, const char *group, const char *category, const char *name, const char *value)
{
struct ast_group_meta *gmi = NULL;
struct varshead *headp;
struct ast_var_t *variable = NULL;
if (!group || !name) {
ast_log(LOG_WARNING, "<%s> GROUP variable assignment failed for %s@%s, group/name cannot be NULL, group variable '%s' not set\n", chan ? ast_channel_name(chan) : "none", group, category, name);
return -2;
}
if (!category) {
category = "";
}
if (!value) {
value = "";
}
/* Find our group meta data */
AST_RWLIST_WRLOCK(&groups_meta);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups_meta, gmi, group_meta_list) {
if ((strcasecmp(gmi->group, group) != 0) || (strcasecmp(gmi->category, category) != 0)) {
continue;
}
AST_RWLIST_INSERT_TAIL(&groups, gi, group_list);
} else {
res = -1;
headp = &gmi->varshead;
AST_LIST_TRAVERSE(headp, variable, entries) {
if (strcasecmp(ast_var_name(variable), name) == 0) {
/* there is already such a variable, nuke it so we can replace it */
AST_LIST_REMOVE(headp, variable, entries);
ast_var_delete(variable);
break;
}
}
variable = ast_var_assign(name, value);
AST_LIST_INSERT_HEAD(headp, variable, entries);
manager_event(EVENT_FLAG_DIALPLAN, "GroupVarSet",
"Channel: %s\r\n"
"Category: %s\r\n"
"Group: %s\r\n"
"Variable: %s\r\n"
"Value: %s\r\n"
"Uniqueid: %s\r\n",
chan ? ast_channel_name(chan) : "none",
category, group, name, value,
chan ? ast_channel_uniqueid(chan) : "none");
break; /* We only have one list item per group@category */
}
AST_RWLIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&groups_meta);
AST_RWLIST_UNLOCK(&groups);
if (!variable) {
ast_log(LOG_WARNING, "<%s> GROUP assignment %s@%s doesn't exist, group variable '%s' not set\n", chan ? ast_channel_name(chan) : "none", group, category, name);
return -1;
}
return res;
return 0;
}
char *ast_app_group_get_var(const char *group, const char *category, const char *name)
{
struct ast_group_meta *gmi = NULL;
struct varshead *headp;
const char *variable;
char *return_value;
struct ast_var_t *ast_var;
int len;
if (!group || !name) {
return NULL;
}
if (!category) {
category = "";
}
/* Find our group meta data */
AST_RWLIST_RDLOCK(&groups_meta);
AST_RWLIST_TRAVERSE(&groups_meta, gmi, group_meta_list) {
if ((strcasecmp(gmi->group, group) != 0) || (strcasecmp(gmi->category, category) != 0)) {
continue;
}
headp = &gmi->varshead;
AST_LIST_TRAVERSE(headp, ast_var, entries) {
variable = ast_var_name(ast_var);
if (!strcasecmp(variable, name)) {
/* found it */
variable = ast_var_value(ast_var); /* Already found it, we can safely re-use */
len = strlen(variable);
return_value = ast_malloc(len + 1);
if (!return_value) {
ast_log(LOG_ERROR, "Group Variable Get Failed to allocate memory for return value\n");
AST_RWLIST_UNLOCK(&groups_meta);
return NULL;
}
ast_copy_string(return_value, variable, len + 1);
AST_RWLIST_UNLOCK(&groups_meta);
return return_value;
}
}
}
AST_RWLIST_UNLOCK(&groups_meta);
return NULL;
}
int ast_app_group_get_count(const char *group, const char *category)
@ -2276,6 +2615,7 @@ int ast_app_group_match_get_count(const char *groupmatch, const char *category)
return 0;
}
/* if regex compilation fails, return zero matches */
if (!ast_strlen_zero(category) && regcomp(&regexbuf_category, category, REG_EXTENDED | REG_NOSUB)) {
ast_log(LOG_ERROR, "Regex compile failed on: %s\n", category);
regfree(&regexbuf_group);
@ -2305,8 +2645,23 @@ int ast_app_group_update(struct ast_channel *old, struct ast_channel *new)
AST_RWLIST_WRLOCK(&groups);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups, gi, group_list) {
if (gi->chan == old) {
/* keep channel groups on transfer */
gi->chan = new;
} else if (gi->chan == new) {
/* The group assignments of old replace those of new, so drop
* any that new already had. Group membership counts are tracked
* per group@category, so release this channel's reference too. */
manager_event(EVENT_FLAG_DIALPLAN, "GroupChannelRemove",
"Channel: %s\r\n"
"Category: %s\r\n"
"Group: %s\r\n"
"Uniqueid: %s\r\n",
ast_channel_name(gi->chan),
S_OR(gi->category, ""), gi->group,
ast_channel_uniqueid(gi->chan));
ast_app_group_remove_meta_ifneeded(gi->group, gi->category);
AST_RWLIST_REMOVE_CURRENT(group_list);
ast_free(gi);
}
@ -2317,16 +2672,153 @@ int ast_app_group_update(struct ast_channel *old, struct ast_channel *new)
return 0;
}
/* Remove all channels from the given group */
int ast_app_group_remove_all_channels(const char *group, const char *category)
{
struct ast_group_info *gi = NULL;
int group_meta_found = 0;
if (!group) {
ast_log(LOG_WARNING, "Group Remove: At the minimum, group must be non-NULL\n");
return 0;
}
if (!category) {
category = "";
}
/* Traverse group@category channel assignments */
AST_RWLIST_WRLOCK(&groups);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups, gi, group_list) {
if (strcasecmp(gi->group, group) || strcasecmp(gi->category, category)) {
/* Need to match group@category exactly */
continue;
}
manager_event(EVENT_FLAG_DIALPLAN, "GroupChannelRemove",
"Channel: %s\r\n"
"Category: %s\r\n"
"Group: %s\r\n"
"Uniqueid: %s\r\n",
ast_channel_name(gi->chan),
gi->category, gi->group,
ast_channel_uniqueid(gi->chan));
ast_app_group_remove_meta_ifneeded(gi->group, gi->category);
AST_RWLIST_REMOVE_CURRENT(group_list);
ast_free(gi);
group_meta_found++;
}
AST_RWLIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&groups);
return group_meta_found;
}
int ast_app_group_rename(const char *old_group, const char *old_category, const char *new_group, const char *new_category)
{
struct ast_group_info *gi = NULL;
struct ast_group_meta *gmi = NULL; /*!< Group metadatas */
int groups_found = 0;
if (!old_group) {
ast_log(LOG_WARNING, "Old Group name is required for group rename\n");
return -1;
}
if (!new_group) {
ast_log(LOG_WARNING, "New Group name is required for group rename\n");
return -1;
}
if (!old_category) {
old_category = "";
}
if (!new_category) {
new_category = "";
}
/* Traverse group@category channel assignments */
AST_RWLIST_WRLOCK(&groups);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups, gi, group_list) {
if (strcasecmp(gi->group, old_group) || strcasecmp(gi->category, old_category)) {
/* Need to match group@category exactly */
continue;
}
ast_copy_string(gi->group, new_group, MAX_GROUP_LEN);
ast_copy_string(gi->category, new_category, MAX_CATEGORY_LEN);
groups_found++;
}
AST_RWLIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&groups);
/* Group Variables */
AST_RWLIST_WRLOCK(&groups_meta);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups_meta, gmi, group_meta_list) {
if (strcasecmp(gmi->group, old_group) || strcasecmp(gmi->category, old_category)) {
/* Need to match group@category exactly */
continue;
}
strncpy(gmi->group, new_group, MAX_GROUP_LEN - 1);
strncpy(gmi->category, new_category, MAX_CATEGORY_LEN - 1);
/*
No groups_found++ here, since we already found it earlier.
If everyone plays by the rules, we'll never have a groups_meta
entry for a group@category without first already having
a group@category itself
*/
}
AST_RWLIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&groups_meta);
if (groups_found) {
manager_event(EVENT_FLAG_DIALPLAN, "GroupRename",
"OldGroup: %s\r\n"
"OldCategory: %s\r\n"
"NewGroup: %s\r\n"
"NewCategory: %s\r\n",
old_group,
old_category,
new_group,
new_category);
}
return groups_found;
}
int ast_app_group_discard(struct ast_channel *chan)
{
struct ast_group_info *gi = NULL;
/* Find and remove all groups associated to this channel */
AST_RWLIST_WRLOCK(&groups);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups, gi, group_list) {
if (gi->chan == chan) {
AST_RWLIST_REMOVE_CURRENT(group_list);
ast_free(gi);
if (gi->chan != chan) {
continue;
}
manager_event(EVENT_FLAG_DIALPLAN, "GroupChannelRemove",
"Channel: %s\r\n"
"Category: %s\r\n"
"Group: %s\r\n"
"Uniqueid: %s\r\n",
ast_channel_name(gi->chan),
gi->category, gi->group,
ast_channel_uniqueid(chan));
/* Find our group meta data, remove the entire group metadata if we're the last channel */
ast_app_group_remove_meta_ifneeded(gi->group, gi->category);
/* Remove this group assignment for this channel */
AST_RWLIST_REMOVE_CURRENT(group_list);
ast_free(gi);
}
AST_RWLIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&groups);
@ -2354,6 +2846,21 @@ int ast_app_group_list_unlock(void)
return AST_RWLIST_UNLOCK(&groups);
}
int ast_app_group_meta_rdlock(void)
{
return AST_RWLIST_RDLOCK(&groups_meta);
}
struct ast_group_meta *ast_app_group_meta_head(void)
{
return AST_RWLIST_FIRST(&groups_meta);
}
int ast_app_group_meta_unlock(void)
{
return AST_RWLIST_UNLOCK(&groups_meta);
}
unsigned int __ast_app_separate_args(char *buf, char delim, int remove_chars, char **array, int arraylen)
{
int argc;

@ -2008,8 +2008,9 @@ static char *group_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cl
return NULL;
}
if (a->argc < 3 || a->argc > 4)
if (a->argc < 3 || a->argc > 4) {
return CLI_SHOWUSAGE;
}
if (a->argc == 4) {
if (regcomp(&regexbuf, a->argv[3], REG_EXTENDED | REG_NOSUB))
@ -2040,6 +2041,64 @@ static char *group_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cl
#undef FORMAT_STRING
}
static char *group_show_variables(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
#define FORMAT_STRING "%-20s %-20s\n"
#define FORMAT_STRING_VAR " %s=%s\n"
struct ast_group_meta *gmi = NULL;
struct varshead *headp;
struct ast_var_t *variable = NULL;
int numgroups = 0;
char group[80];
char category[80];
switch (cmd) {
case CLI_INIT:
e->command = "group show variables";
e->usage =
"Usage: group show variables [group@[category]]\n"
" Lists all currently active groups and their variables.\n"
" Optional group, or group@category can be used to only show a specific group\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc < 3 || a->argc > 4) {
return CLI_SHOWUSAGE;
}
if (a->argc == 4) {
if (ast_app_group_split_group(a->argv[3], group, sizeof(group), category, sizeof(category))) {
return NULL;
}
}
ast_cli(a->fd, "Group\n Category\nVariables\n");
ast_cli(a->fd, "------------------------------\n");
/* Print group variables */
ast_app_group_meta_rdlock();
gmi = ast_app_group_meta_head();
while (gmi) {
ast_cli(a->fd, FORMAT_STRING, gmi->group, (strcmp(gmi->category, "") ? gmi->category : "(Default)"));
numgroups++;
headp = &gmi->varshead;
AST_LIST_TRAVERSE(headp, variable, entries) {
ast_cli(a->fd, FORMAT_STRING_VAR, ast_var_name(variable), ast_var_value(variable));
}
gmi = AST_LIST_NEXT(gmi, group_meta_list);
}
ast_app_group_meta_unlock();
ast_cli(a->fd, "%d active group%s\n", numgroups, ESS(numgroups));
return CLI_SUCCESS;
#undef FORMAT_STRING
}
static char *handle_cli_wait_fullybooted(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
switch (cmd) {
@ -2113,7 +2172,6 @@ static struct ast_cli_entry cli_cli[] = {
AST_CLI_DEFINE(handle_debug, "Set level of debug chattiness"),
AST_CLI_DEFINE(handle_trace, "Set level of trace chattiness"),
AST_CLI_DEFINE(handle_verbose, "Set level of verbose chattiness"),
AST_CLI_DEFINE(handle_help, "Display help list, or specific help on a command"),
AST_CLI_DEFINE(handle_logger_mute, "Toggle logging output to a console"),
@ -2143,6 +2201,7 @@ static struct ast_cli_entry cli_channels_cli[] = {
AST_CLI_DEFINE(handle_showchan, "Display information on a specific channel"),
AST_CLI_DEFINE(handle_core_set_debug_channel, "Enable/disable debugging on a channel"),
AST_CLI_DEFINE(group_show_channels, "Display active channels with group(s)"),
AST_CLI_DEFINE(group_show_variables, "Display active channels with group(s), along with group variables"),
AST_CLI_DEFINE(handle_softhangup, "Request a hangup on a given channel"),
};

@ -221,8 +221,308 @@ exit_group_test:
return res;
}
AST_TEST_DEFINE(app_group_var)
{
struct ast_channel *test_channel1 = NULL;
enum ast_test_result_state res = AST_TEST_PASS;
static const char group[] = "vartestgroup";
static const char category[] = "vartestcategory";
char *value = NULL;
struct ast_group_meta *gmi;
struct ast_var_t *var;
int count;
switch (cmd) {
case TEST_INIT:
info->name = "app_group_var";
info->category = "/main/app/";
info->summary = "App group variable unit test";
info->description =
"This tests setting, getting, overwriting, and cleaning up group\n"
"variables. It includes a regression test for a use-after-free that\n"
"occurred when overwriting an existing group variable (the old variable\n"
"was freed without being unlinked from the group's variable list), and\n"
"a regression test for a NULL channel crash when setting a variable on\n"
"a group@category that doesn't exist (the path exercised by the AMI\n"
"GroupVarSet action, which always passes a NULL channel).";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
if (!(test_channel1 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, 0, "TestChannel1"))) {
return AST_TEST_FAIL;
}
ast_channel_unlock(test_channel1);
/* A group must already exist (have a member channel) before a variable can be
* set on it. This is also a regression test: with a NULL chan (as the AMI
* GroupVarSet action always passes), this used to crash inside the "doesn't
* exist" error log via an unconditional ast_channel_name(NULL). */
if (ast_app_group_set_var(NULL, group, "", "foo", "bar") != -1) {
ast_test_status_update(test, "Expected failure setting a variable on a nonexistent group\n");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
ast_app_group_set_channel(test_channel1, group);
/* Basic set/get */
if (ast_app_group_set_var(test_channel1, group, "", "myvar", "value1")) {
ast_test_status_update(test, "Failed to set group variable 'myvar'\n");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
if (!(value = ast_app_group_get_var(group, "", "myvar")) || strcmp(value, "value1")) {
ast_test_status_update(test, "Expected 'value1', got '%s'\n", value ? value : "(null)");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
ast_free(value);
value = NULL;
/* Overwrite: regression test for a use-after-free when replacing an existing
* group variable. The old ast_var_t node used to be freed without first being
* unlinked from the group's variable list, leaving a dangling node for the
* next traversal (this get, or another set) to walk into. */
if (ast_app_group_set_var(test_channel1, group, "", "myvar", "value2")) {
ast_test_status_update(test, "Failed to overwrite group variable 'myvar'\n");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
if (ast_app_group_set_var(test_channel1, group, "", "myvar", "value3")) {
ast_test_status_update(test, "Failed to overwrite group variable 'myvar' a second time\n");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
if (!(value = ast_app_group_get_var(group, "", "myvar")) || strcmp(value, "value3")) {
ast_test_status_update(test, "Expected 'value3' after overwriting twice, got '%s'\n", value ? value : "(null)");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
ast_free(value);
value = NULL;
/* Confirm the overwrites left exactly one 'myvar' entry in the list (no
* dangling freed node left behind by an unlinked-then-freed variable) */
ast_app_group_meta_rdlock();
count = 0;
for (gmi = ast_app_group_meta_head(); gmi; gmi = AST_LIST_NEXT(gmi, group_meta_list)) {
if (strcasecmp(gmi->group, group) || strcasecmp(gmi->category, "")) {
continue;
}
AST_LIST_TRAVERSE(&gmi->varshead, var, entries) {
if (!strcasecmp(ast_var_name(var), "myvar")) {
count++;
}
}
}
ast_app_group_meta_unlock();
if (count != 1) {
ast_test_status_update(test, "Expected exactly 1 'myvar' entry after overwriting, found %d "
"(indicates a stale/dangling list node)\n", count);
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
/* Independent variables coexist, and lookups are case-insensitive by name */
if (ast_app_group_set_var(test_channel1, group, "", "othervar", "otherval")) {
ast_test_status_update(test, "Failed to set group variable 'othervar'\n");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
if (!(value = ast_app_group_get_var(group, "", "MYVAR")) || strcmp(value, "value3")) {
ast_test_status_update(test, "Case-insensitive lookup of 'MYVAR' failed, got '%s'\n", value ? value : "(null)");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
ast_free(value);
if (!(value = ast_app_group_get_var(group, "", "othervar")) || strcmp(value, "otherval")) {
ast_test_status_update(test, "Setting 'othervar' should not disturb 'myvar', got '%s' for 'othervar'\n", value ? value : "(null)");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
ast_free(value);
value = NULL;
/* Variables are isolated per category */
if (ast_app_group_get_var(group, category, "myvar") != NULL) {
ast_test_status_update(test, "Variable 'myvar' set on the default category should not be visible under a different category\n");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
/* Nonexistent variable/group lookups return NULL */
if (ast_app_group_get_var(group, "", "novar") != NULL) {
ast_test_status_update(test, "Expected NULL for a variable that was never set\n");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
if (ast_app_group_get_var("nosuchgroup", "", "myvar") != NULL) {
ast_test_status_update(test, "Expected NULL for a variable on a group that doesn't exist\n");
res = AST_TEST_FAIL;
goto exit_group_var_test;
}
exit_group_var_test:
ast_free(value);
ast_hangup(test_channel1);
/* Once the last channel leaves the group, the group (and its variables)
* should be destroyed */
if (res == AST_TEST_PASS && ast_app_group_get_var(group, "", "myvar") != NULL) {
ast_test_status_update(test, "Group variable 'myvar' should have been freed when the group was destroyed\n");
res = AST_TEST_FAIL;
}
return res;
}
/*! \brief Check a group variable has the expected value, NULL meaning it must not exist */
static int group_var_matches(struct ast_test *test, const char *group, const char *category,
const char *name, const char *expected)
{
char *value = ast_app_group_get_var(group, category, name);
int ok = (!value && !expected) || (value && expected && !strcmp(value, expected));
if (!ok) {
ast_test_status_update(test, "Group variable '%s' on %s@%s: expected '%s', got '%s'\n",
name, group, category, expected ? expected : "(none)", value ? value : "(none)");
}
ast_free(value);
return ok;
}
AST_TEST_DEFINE(app_group_update)
{
struct ast_channel *old_chan = NULL;
struct ast_channel *new_chan = NULL;
struct ast_group_info *gi;
enum ast_test_result_state res = AST_TEST_PASS;
int old_count = 0;
int new_count = 0;
switch (cmd) {
case TEST_INIT:
info->name = "app_group_update";
info->category = "/main/app/";
info->summary = "App group update (masquerade) unit test";
info->description =
"This tests that ast_app_group_update() moves the group assignments of the\n"
"old channel to the new channel, replaces any group assignments the new\n"
"channel already had, and keeps group membership counts and group\n"
"variables consistent while doing so.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
if (!(old_chan = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, 0, "TestChannelOld"))) {
return AST_TEST_FAIL;
}
ast_channel_unlock(old_chan);
if (!(new_chan = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, 0, "TestChannelNew"))) {
ast_hangup(old_chan);
return AST_TEST_FAIL;
}
ast_channel_unlock(new_chan);
/* A channel is in at most one group per category. Both channels are in
* 'updshared' (default category), old is also in updonlyold@cat and new is
* also in updonlynew@cat2. */
ast_app_group_set_channel(old_chan, "updshared");
ast_app_group_set_channel(old_chan, "updonlyold@cat");
ast_app_group_set_channel(new_chan, "updshared");
ast_app_group_set_channel(new_chan, "updonlynew@cat2");
if (ast_app_group_set_var(old_chan, "updshared", "", "v", "shared")
|| ast_app_group_set_var(old_chan, "updonlyold", "cat", "v", "old")
|| ast_app_group_set_var(new_chan, "updonlynew", "cat2", "v", "new")) {
ast_test_status_update(test, "Failed to set group variables\n");
res = AST_TEST_FAIL;
goto exit_group_update_test;
}
if (ast_app_group_get_count("updshared", "") != 2) {
ast_test_status_update(test, "Expected 2 channels in 'updshared' before the update, found %d\n",
ast_app_group_get_count("updshared", ""));
res = AST_TEST_FAIL;
goto exit_group_update_test;
}
ast_app_group_update(old_chan, new_chan);
/* A channel that was in both must not be counted twice, and the assignment
* that only the new channel had is gone */
if (ast_app_group_get_count("updshared", "") != 1) {
ast_test_status_update(test, "Expected 1 channel in 'updshared' after the update, found %d\n",
ast_app_group_get_count("updshared", ""));
res = AST_TEST_FAIL;
goto exit_group_update_test;
}
if (ast_app_group_get_count("updonlyold", "cat") != 1) {
ast_test_status_update(test, "Expected 1 channel in 'updonlyold@cat' after the update, found %d\n",
ast_app_group_get_count("updonlyold", "cat"));
res = AST_TEST_FAIL;
goto exit_group_update_test;
}
if (ast_app_group_get_count("updonlynew", "cat2") != 0) {
ast_test_status_update(test, "Expected 0 channels in 'updonlynew@cat2' after the update, found %d\n",
ast_app_group_get_count("updonlynew", "cat2"));
res = AST_TEST_FAIL;
goto exit_group_update_test;
}
/* Everything the old channel was in now belongs to the new channel */
ast_app_group_list_rdlock();
for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
if (gi->chan == old_chan) {
old_count++;
} else if (gi->chan == new_chan) {
new_count++;
}
}
ast_app_group_list_unlock();
if (old_count != 0 || new_count != 2) {
ast_test_status_update(test, "Expected 0 assignments on the old channel and 2 on the new one, found %d and %d\n",
old_count, new_count);
res = AST_TEST_FAIL;
goto exit_group_update_test;
}
/* Groups that still have a member keep their variables, a group that lost
* its last member (updonlynew) loses them */
if (!group_var_matches(test, "updshared", "", "v", "shared")
|| !group_var_matches(test, "updonlyold", "cat", "v", "old")
|| !group_var_matches(test, "updonlynew", "cat2", "v", NULL)) {
res = AST_TEST_FAIL;
goto exit_group_update_test;
}
exit_group_update_test:
ast_hangup(old_chan);
ast_hangup(new_chan);
/* The remaining assignments went with the new channel, nothing may be left */
if (res == AST_TEST_PASS
&& (ast_app_group_get_count("updshared", "") || ast_app_group_get_count("updonlyold", "cat")
|| !group_var_matches(test, "updshared", "", "v", NULL)
|| !group_var_matches(test, "updonlyold", "cat", "v", NULL))) {
ast_test_status_update(test, "Group assignments or variables were left behind after hanging up\n");
res = AST_TEST_FAIL;
}
return res;
}
static int unload_module(void)
{
AST_TEST_UNREGISTER(app_group_update);
AST_TEST_UNREGISTER(app_group_var);
AST_TEST_UNREGISTER(app_group);
AST_TEST_UNREGISTER(options_parsing);
return 0;
@ -230,6 +530,8 @@ static int unload_module(void)
static int load_module(void)
{
AST_TEST_REGISTER(app_group_update);
AST_TEST_REGISTER(app_group_var);
AST_TEST_REGISTER(app_group);
AST_TEST_REGISTER(options_parsing);
return AST_MODULE_LOAD_SUCCESS;

Loading…
Cancel
Save