Properly clean up manager resources on exit

This patch does two things:
1) It properly unregisters the manager CLI commands
2) It cleans up AMI users on exit.  Prior to this patch, the AMI users
   were not being disposed of properly, resulting in a memory leak.

(closes issue ASTERISK-20646)
Reported by: Corey Farrell
patches:
  manager_shutdown.patch uploaded by Corey Farrell (license 5909)



git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.8@375793 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Matthew Jordan
2012-11-04 02:25:48 +00:00
parent 19ce7a69b3
commit 24d5348b9f

View File

@@ -6616,6 +6616,23 @@ static void load_channelvars(struct ast_variable *var)
AST_RWLIST_UNLOCK(&channelvars); AST_RWLIST_UNLOCK(&channelvars);
} }
/*! \internal \brief Free a user record. Should already be removed from the list */
static void manager_free_user(struct ast_manager_user *user)
{
if (user->a1_hash) {
ast_free(user->a1_hash);
}
if (user->secret) {
ast_free(user->secret);
}
ao2_t_callback(user->whitefilters, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "unlink all white filters");
ao2_t_callback(user->blackfilters, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "unlink all black filters");
ao2_t_ref(user->whitefilters, -1, "decrement ref for white container, should be last one");
ao2_t_ref(user->blackfilters, -1, "decrement ref for black container, should be last one");
ast_free_ha(user->ha);
ast_free(user);
}
/*! \internal \brief Clean up resources on Asterisk shutdown */ /*! \internal \brief Clean up resources on Asterisk shutdown */
static void manager_shutdown(void) static void manager_shutdown(void)
{ {
@@ -6656,6 +6673,23 @@ static void manager_shutdown(void)
ast_manager_unregister("ModuleCheck"); ast_manager_unregister("ModuleCheck");
ast_manager_unregister("AOCMessage"); ast_manager_unregister("AOCMessage");
ast_manager_unregister("Filter"); ast_manager_unregister("Filter");
ast_cli_unregister_multiple(cli_manager, ARRAY_LEN(cli_manager));
}
ast_tcptls_server_stop(&ami_desc);
ast_tcptls_server_stop(&amis_desc);
if (ami_tls_cfg.certfile) {
ast_free(ami_tls_cfg.certfile);
ami_tls_cfg.certfile = NULL;
}
if (ami_tls_cfg.pvtfile) {
ast_free(ami_tls_cfg.pvtfile);
ami_tls_cfg.pvtfile = NULL;
}
if (ami_tls_cfg.cipher) {
ast_free(ami_tls_cfg.cipher);
ami_tls_cfg.cipher = NULL;
} }
if (sessions) { if (sessions) {
@@ -6664,13 +6698,10 @@ static void manager_shutdown(void)
} }
while ((user = AST_LIST_REMOVE_HEAD(&users, list))) { while ((user = AST_LIST_REMOVE_HEAD(&users, list))) {
ao2_ref(user->whitefilters, -1); manager_free_user(user);
ao2_ref(user->blackfilters, -1);
ast_free(user);
} }
} }
static int __init_manager(int reload) static int __init_manager(int reload)
{ {
struct ast_config *ucfg = NULL, *cfg = NULL; struct ast_config *ucfg = NULL, *cfg = NULL;
@@ -7047,19 +7078,7 @@ static int __init_manager(int reload)
/* We do not need to keep this user so take them out of the list */ /* We do not need to keep this user so take them out of the list */
AST_RWLIST_REMOVE_CURRENT(list); AST_RWLIST_REMOVE_CURRENT(list);
ast_debug(4, "Pruning user '%s'\n", user->username); ast_debug(4, "Pruning user '%s'\n", user->username);
/* Free their memory now */ manager_free_user(user);
if (user->a1_hash) {
ast_free(user->a1_hash);
}
if (user->secret) {
ast_free(user->secret);
}
ao2_t_callback(user->whitefilters, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "unlink all white filters");
ao2_t_callback(user->blackfilters, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "unlink all black filters");
ao2_t_ref(user->whitefilters, -1, "decrement ref for white container, should be last one");
ao2_t_ref(user->blackfilters, -1, "decrement ref for black container, should be last one");
ast_free_ha(user->ha);
ast_free(user);
} }
AST_RWLIST_TRAVERSE_SAFE_END; AST_RWLIST_TRAVERSE_SAFE_END;