]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Fix a variety of ref counting issues
authorMatthew Jordan <mjordan@digium.com>
Tue, 2 Oct 2012 01:09:39 +0000 (01:09 +0000)
committerMatthew Jordan <mjordan@digium.com>
Tue, 2 Oct 2012 01:09:39 +0000 (01:09 +0000)
This patch resolves a number of ref leaks that occur primarily on Asterisk
shutdown.  It adds a variety of shutdown routines to core portions of
Asterisk such that they can reclaim resources allocate duringd initialization.

Review: https://reviewboard.asterisk.org/r/2137
........

Merged revisions 374177 from http://svn.asterisk.org/svn/asterisk/branches/1.8

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/10@374178 65c4cc65-6c06-0410-ace0-fbb531ad65f3

14 files changed:
channels/chan_agent.c
main/asterisk.c
main/ccss.c
main/cel.c
main/channel.c
main/data.c
main/db.c
main/features.c
main/format.c
main/format_pref.c
main/indications.c
main/manager.c
main/message.c
main/pbx.c

index de8842d368ffe99433abde9199cffc8d07f779ae..7bcff86f259d9f8a6813b76feae62dc5de5a7875 100644 (file)
@@ -2556,12 +2556,15 @@ static int load_module(void)
        ast_format_cap_add_all(agent_tech.capabilities);
        /* Make sure we can register our agent channel type */
        if (ast_channel_register(&agent_tech)) {
+               agent_tech.capabilities = ast_format_cap_destroy(agent_tech.capabilities);
                ast_log(LOG_ERROR, "Unable to register channel class 'Agent'\n");
                return AST_MODULE_LOAD_FAILURE;
        }
        /* Read in the config */
-       if (!read_agent_config(0))
+       if (!read_agent_config(0)) {
+               agent_tech.capabilities = ast_format_cap_destroy(agent_tech.capabilities);
                return AST_MODULE_LOAD_DECLINE;
+       }
        /* Dialplan applications */
        ast_register_application_xml(app, login_exec);
        ast_register_application_xml(app3, agentmonitoroutgoing_exec);
index e24e12ebd87bb03f0c67ff9eff18914de0216328..acfd2244ade37efe8ba5cfd700c8d19c97dc9f26 100644 (file)
@@ -1693,6 +1693,9 @@ static int can_safely_quit(shutdown_nice_t niceness, int restart)
         * (if in batch mode). really_quit happens to call it again when running
         * the atexit handlers, otherwise this would be a bit early. */
        ast_cdr_engine_term();
+
+       /* Shutdown the message queue for the technology agnostic message channel.
+        * This has to occur before we pause shutdown pending ast_undestroyed_channels. */
        ast_msg_shutdown();
 
        if (niceness == SHUTDOWN_NORMAL) {
index 9efc280a95693e0ae725d2bfddf0a613e209a40f..e1323e7098dbe43be3e5963da1834d54b9778fec 100644 (file)
@@ -4484,6 +4484,31 @@ static struct ast_cli_entry cc_cli[] = {
        AST_CLI_DEFINE(handle_cc_kill, "Kill a CC transaction"),
 };
 
+static void cc_shutdown(void)
+{
+       ast_devstate_prov_del("ccss");
+       ast_cc_agent_unregister(&generic_agent_callbacks);
+       ast_cc_monitor_unregister(&generic_monitor_cbs);
+       ast_unregister_application(cccancel_app);
+       ast_unregister_application(ccreq_app);
+
+       if (cc_sched_context) {
+               ast_sched_context_destroy(cc_sched_context);
+               cc_sched_context = NULL;
+       }
+       if (cc_core_taskprocessor) {
+               cc_core_taskprocessor = ast_taskprocessor_unreference(cc_core_taskprocessor);
+       }
+       if (generic_monitors) {
+               ao2_t_ref(generic_monitors, -1, "Unref generic_monitor container in cc_shutdown");
+               generic_monitors = NULL;
+       }
+       if (cc_core_instances) {
+               ao2_t_ref(cc_core_instances, -1, "Unref cc_core_instances container in cc_shutdown");
+               cc_core_instances = NULL;
+       }
+}
+
 int ast_cc_init(void)
 {
        int res;
@@ -4521,5 +4546,7 @@ int ast_cc_init(void)
        initialize_cc_devstate_map();
        res |= ast_devstate_prov_add("ccss", ccss_device_state);
 
+       ast_register_atexit(cc_shutdown);
+
        return res;
 }
index a5d82a606fd3f1d5320057bc4bc482b370415a36..117c5de4a3f5041c04fc6cf7154b40b7735449a3 100644 (file)
@@ -711,6 +711,10 @@ static void ast_cel_engine_term(void)
                ao2_ref(appset, -1);
                appset = NULL;
        }
+       if (linkedids) {
+               ao2_ref(linkedids, -1);
+               linkedids = NULL;
+       }
 }
 
 int ast_cel_engine_init(void)
index c7c4e27f6e2cd662498cf57f453531188df13145..ce80dfbbd84e8d69cc36a342ff19d29e28f109ef 100644 (file)
@@ -8282,6 +8282,15 @@ static const struct ast_data_entry channel_providers[] = {
        AST_DATA_ENTRY("/asterisk/core/channeltypes", &channeltypes_provider),
 };
 
+static void channels_shutdown(void)
+{
+       ast_data_unregister(NULL);
+       if (channels) {
+               ao2_ref(channels, -1);
+               channels = NULL;
+       }
+}
+
 void ast_channels_init(void)
 {
        channels = ao2_container_alloc(NUM_CHANNEL_BUCKETS,
@@ -8292,6 +8301,8 @@ void ast_channels_init(void)
        ast_data_register_multiple_core(channel_providers, ARRAY_LEN(channel_providers));
 
        ast_plc_reload();
+
+       ast_register_atexit(channels_shutdown);
 }
 
 /*! \brief Print call group and pickup group ---*/
index 1394a2b3e11913c0acd3f39fce925865294bd2e6..9205d8a9292a291d48276b7f1c46558d4fa5877c 100644 (file)
@@ -3314,6 +3314,14 @@ AST_TEST_DEFINE(test_data_get)
 
 #endif
 
+/*! \internal \brief Clean up resources on Asterisk shutdown */
+static void data_shutdown(void)
+{
+       ast_manager_unregister("DataGet");
+       ao2_t_ref(root_data.container, -1, "Unref root_data.container in data_shutdown");
+       ast_rwlock_destroy(&root_data.lock);
+}
+
 int ast_data_init(void)
 {
        int res = 0;
@@ -3333,5 +3341,7 @@ int ast_data_init(void)
        AST_TEST_REGISTER(test_data_get);
 #endif
 
+       ast_register_atexit(data_shutdown);
+
        return res;
 }
index 2fd8c7c8daf1dbdb3f06a8b61fc8f849c4d01aec..97cfae3c2634ca194e24e05687ece0a7c6f19416 100644 (file)
--- a/main/db.c
+++ b/main/db.c
@@ -946,8 +946,14 @@ static void *db_sync_thread(void *data)
        return NULL;
 }
 
+/*! \internal \brief Clean up resources on Asterisk shutdown */
 static void astdb_atexit(void)
 {
+       ast_manager_unregister("DBGet");
+       ast_manager_unregister("DBPut");
+       ast_manager_unregister("DBDel");
+       ast_manager_unregister("DBDelTree");
+
        /* Set doexit to 1 to kill thread. db_sync must be called with
         * mutex held. */
        doexit = 1;
index 4cdde6fdedabaafa266bfc65eb26c6a6ea7c3d81..104086a48be9074060d4476874c827ecca0a532a 100644 (file)
@@ -6659,7 +6659,6 @@ static int load_config(int reload)
                        return -1;
                }
                ast_debug(1, "Configuration of default default parking lot done.\n");
-               parkinglot_addref(default_parkinglot);
        }
 
        cfg = ast_config_load2("features.conf", "features", config_flags);
@@ -8239,6 +8238,22 @@ exit_features_test:
 }
 #endif /* defined(TEST_FRAMEWORK) */
 
+/*! \internal \brief Clean up resources on Asterisk shutdown */
+static void features_shutdown(void)
+{
+       ast_devstate_prov_del("Park");
+       ast_manager_unregister("Bridge");
+       ast_manager_unregister("Park");
+       ast_manager_unregister("Parkinglots");
+       ast_manager_unregister("ParkedCalls");
+       ast_unregister_application(parkcall);
+       ast_unregister_application(parkedcall);
+       ast_unregister_application(app_bridge);
+
+       pthread_cancel(parking_thread);
+       ao2_ref(parkinglots, -1);
+}
+
 int ast_features_init(void)
 {
        int res;
@@ -8271,5 +8286,7 @@ int ast_features_init(void)
        res |= AST_TEST_REGISTER(features_test);
 #endif /* defined(TEST_FRAMEWORK) */
 
+       ast_register_atexit(features_shutdown);
+
        return res;
 }
index 6c234bfcefc4ccf374675a6e9cbd29386d73f6ce..b739038b9c8aba29c7e24257b97a5879d65bcc8b 100644 (file)
@@ -927,6 +927,7 @@ static int format_list_add_static(
        entry->custom_entry = 0;
 
        ao2_link(format_list, entry);
+       ao2_ref(entry, -1);
        return 0;
 }
 
@@ -995,6 +996,7 @@ static int build_format_list_array(void)
        ast_rwlock_unlock(&format_list_array_lock);
        return 0;
 }
+
 static int format_list_init(void)
 {
        struct ast_format tmpfmt;
@@ -1044,6 +1046,20 @@ static int format_list_init(void)
        return 0;
 }
 
+/*! \internal \brief Clean up resources on Asterisk shutdown */
+static void format_list_shutdown(void)
+{
+       ast_rwlock_destroy(&format_list_array_lock);
+       if (format_list) {
+               ao2_t_ref(format_list, -1, "Unref format_list container in shutdown");
+               format_list = NULL;
+       }
+       if (format_list_array) {
+               ao2_t_ref(format_list_array, -1, "Unref format_list_array in shutdown");
+               format_list_array = NULL;
+       }
+}
+
 int ast_format_list_init(void)
 {
        if (ast_rwlock_init(&format_list_array_lock)) {
@@ -1056,17 +1072,23 @@ int ast_format_list_init(void)
                goto init_list_cleanup;
        }
 
+       ast_register_atexit(format_list_shutdown);
        return 0;
 init_list_cleanup:
 
-       ast_rwlock_destroy(&format_list_array_lock);
-       ao2_ref(format_list, -1);
-       if (format_list_array) {
-               ao2_ref(format_list_array, -1);
-       }
+       format_list_shutdown();
        return -1;
 }
 
+/*! \internal \brief Clean up resources on Asterisk shutdown */
+static void format_attr_shutdown(void)
+{
+       if (interfaces) {
+               ao2_ref(interfaces, -1);
+               interfaces = NULL;
+       }
+}
+
 int ast_format_attr_init(void)
 {
        ast_cli_register_multiple(my_clis, ARRAY_LEN(my_clis));
@@ -1178,6 +1200,7 @@ static int conf_process_format_name(const char *name, enum ast_format_id *id)
                *id = 0;
                return -1;
        }
+       ast_register_atexit(format_attr_shutdown);
        return 0;
 }
 
@@ -1349,7 +1372,7 @@ int ast_format_attr_reg_interface(const struct ast_format_attr_interface *interf
                        ast_rtp_engine_load_format(&f_list[x].format);
                }
        }
-
+       f_list = ast_format_list_destroy(f_list);
        return 0;
 }
 
@@ -1387,6 +1410,6 @@ int ast_format_attr_unreg_interface(const struct ast_format_attr_interface *inte
 
        /* This will remove all custom formats previously created for this interface */
        load_format_config();
-
+       f_list = ast_format_list_destroy(f_list);
        return 0;
 }
index 9cb513c2285a364f4c73d274fa40e7aa328c4ffa..8b495ba56740ef86ddf1888f97365349930e47c3 100644 (file)
@@ -285,6 +285,7 @@ struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, struc
 
        if (idx < 0) {
                ast_log(AST_LOG_WARNING, "Format %s unknown; unable to get preferred codec packet size\n", ast_getformatname(format));
+               ast_format_list_destroy(f_list);
                return fmt;
        }
 
index ec9a72048196b732b3a00b63953acf9399f7e333..a43c1c0b30f956380a1dfe6d77cceed0f8a89686 100644 (file)
@@ -1150,6 +1150,15 @@ int ast_tone_zone_data_add_structure(struct ast_data *tree, struct ast_tone_zone
        return 0;
 }
 
+/*! \internal \brief Clean up resources on Asterisk shutdown */
+static void indications_shutdown(void)
+{
+       if (ast_tone_zones) {
+               ao2_ref(ast_tone_zones, -1);
+               ast_tone_zones = NULL;
+       }
+}
+
 /*! \brief Load indications module */
 int ast_indications_init(void)
 {
@@ -1164,6 +1173,7 @@ int ast_indications_init(void)
 
        ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
 
+       ast_register_atexit(indications_shutdown);
        return 0;
 }
 
index dbf3b9bd19499c3da7768f2f93d3d7a306ecde88..2bfae23a68377ee698228071cf8c66f87f1fc8ad 100644 (file)
@@ -6728,6 +6728,61 @@ static void load_channelvars(struct ast_variable *var)
        AST_RWLIST_UNLOCK(&channelvars);
 }
 
+/*! \internal \brief Clean up resources on Asterisk shutdown */
+static void manager_shutdown(void)
+{
+       struct ast_manager_user *user;
+
+       if (registered) {
+               ast_manager_unregister("Ping");
+               ast_manager_unregister("Events");
+               ast_manager_unregister("Logoff");
+               ast_manager_unregister("Login");
+               ast_manager_unregister("Challenge");
+               ast_manager_unregister("Hangup");
+               ast_manager_unregister("Status");
+               ast_manager_unregister("Setvar");
+               ast_manager_unregister("Getvar");
+               ast_manager_unregister("GetConfig");
+               ast_manager_unregister("GetConfigJSON");
+               ast_manager_unregister("UpdateConfig");
+               ast_manager_unregister("CreateConfig");
+               ast_manager_unregister("ListCategories");
+               ast_manager_unregister("Redirect");
+               ast_manager_unregister("Atxfer");
+               ast_manager_unregister("Originate");
+               ast_manager_unregister("Command");
+               ast_manager_unregister("ExtensionState");
+               ast_manager_unregister("AbsoluteTimeout");
+               ast_manager_unregister("MailboxStatus");
+               ast_manager_unregister("MailboxCount");
+               ast_manager_unregister("ListCommands");
+               ast_manager_unregister("SendText");
+               ast_manager_unregister("UserEvent");
+               ast_manager_unregister("WaitEvent");
+               ast_manager_unregister("CoreSettings");
+               ast_manager_unregister("CoreStatus");
+               ast_manager_unregister("Reload");
+               ast_manager_unregister("CoreShowChannels");
+               ast_manager_unregister("ModuleLoad");
+               ast_manager_unregister("ModuleCheck");
+               ast_manager_unregister("AOCMessage");
+               ast_manager_unregister("Filter");
+       }
+
+       if (sessions) {
+               ao2_ref(sessions, -1);
+               sessions = NULL;
+       }
+
+       while ((user = AST_LIST_REMOVE_HEAD(&users, list))) {
+               ao2_ref(user->whitefilters, -1);
+               ao2_ref(user->blackfilters, -1);
+               ast_free(user);
+       }
+}
+
+
 static int __init_manager(int reload)
 {
        struct ast_config *ucfg = NULL, *cfg = NULL;
@@ -6788,6 +6843,9 @@ static int __init_manager(int reload)
                /* Append placeholder event so master_eventq never runs dry */
                append_event("Event: Placeholder\r\n\r\n", 0);
        }
+
+       ast_register_atexit(manager_shutdown);
+
        if ((cfg = ast_config_load2("manager.conf", "manager", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
                return 0;
        }
@@ -7141,6 +7199,7 @@ static int __init_manager(int reload)
        } else if (ast_ssl_setup(amis_desc.tls_cfg)) {
                ast_tcptls_server_start(&amis_desc);
        }
+
        return 0;
 }
 
index 1f2638e61b849f067d8f70ea98f3c5682c3f18f9..17c831f99db06cd3eb9f4321a87437b876ab9760 100644 (file)
@@ -1113,6 +1113,29 @@ int ast_msg_tech_unregister(const struct ast_msg_tech *tech)
        return 0;
 }
 
+void ast_msg_shutdown()
+{
+       if (msg_q_tp) {
+               msg_q_tp = ast_taskprocessor_unreference(msg_q_tp);
+       }
+}
+
+/*! \internal \brief Clean up other resources on Asterisk shutdown
+ * \note This does not include the msg_q_tp object, which must be disposed
+ * of prior to Asterisk checking for channel destruction in its shutdown
+ * sequence.  The atexit handlers are executed after this occurs. */
+static void message_shutdown(void)
+{
+       ast_custom_function_unregister(&msg_function);
+       ast_custom_function_unregister(&msg_data_function);
+       ast_unregister_application(app_msg_send);
+
+       if (msg_techs) {
+               ao2_ref(msg_techs, -1);
+               msg_techs = NULL;
+       }
+}
+
 /*
  * \internal
  * \brief Initialize stuff during Asterisk startup.
@@ -1141,10 +1164,7 @@ int ast_msg_init(void)
        res |= __ast_custom_function_register(&msg_data_function, NULL);
        res |= ast_register_application2(app_msg_send, msg_send_exec, NULL, NULL, NULL);
 
-       return res;
-}
+       ast_register_atexit(message_shutdown);
 
-void ast_msg_shutdown(void)
-{
-       msg_q_tp = ast_taskprocessor_unreference(msg_q_tp);
+       return res;
 }
index 165cf22627b04ab31b6b55ad1e4dbabe8abf0da7..0d7a93b03885d25c5b19fa05eb546fee5b4c28f6 100644 (file)
@@ -10460,6 +10460,26 @@ static const struct ast_data_entry pbx_data_providers[] = {
        AST_DATA_ENTRY("asterisk/core/hints", &hints_data_provider),
 };
 
+/*! \internal \brief Clean up resources on Asterisk shutdown.
+ * \note Cleans up resources allocated in load_pbx */
+static void unload_pbx(void)
+{
+       int x;
+
+       if (device_state_sub) {
+               device_state_sub = ast_event_unsubscribe(device_state_sub);
+       }
+
+       /* Unregister builtin applications */
+       for (x = 0; x < ARRAY_LEN(builtins); x++) {
+               ast_unregister_application(builtins[x].name);
+       }
+       ast_manager_unregister("ShowDialPlan");
+       ast_custom_function_unregister(&exception_function);
+       ast_custom_function_unregister(&testtime_function);
+       ast_data_unregister(NULL);
+}
+
 int load_pbx(void)
 {
        int x;
@@ -10493,6 +10513,7 @@ int load_pbx(void)
                return -1;
        }
 
+       ast_register_atexit(unload_pbx);
        return 0;
 }
 
@@ -10853,11 +10874,26 @@ static int statecbs_cmp(void *obj, void *arg, int flags)
        return (state_cb->change_cb == change_cb) ? CMP_MATCH | CMP_STOP : 0;
 }
 
+/*! \internal \brief Clean up resources on Asterisk shutdown */
+static void pbx_shutdown(void)
+{
+       if (hints) {
+               ao2_ref(hints, -1);
+       }
+       if (hintdevices) {
+               ao2_ref(hintdevices, -1);
+       }
+       if (statecbs) {
+               ao2_ref(statecbs, -1);
+       }
+}
+
 int ast_pbx_init(void)
 {
        hints = ao2_container_alloc(HASH_EXTENHINT_SIZE, hint_hash, hint_cmp);
        hintdevices = ao2_container_alloc(HASH_EXTENHINT_SIZE, hintdevice_hash_cb, hintdevice_cmp_multiple);
        statecbs = ao2_container_alloc(1, NULL, statecbs_cmp);
 
+       ast_register_atexit(pbx_shutdown);
        return (hints && hintdevices && statecbs) ? 0 : -1;
 }