]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
core: Remove non-critical cleanup from startup aborts.
authorCorey Farrell <git@cfware.com>
Sat, 10 Mar 2018 09:33:33 +0000 (04:33 -0500)
committerCorey Farrell <git@cfware.com>
Tue, 13 Mar 2018 17:32:54 +0000 (13:32 -0400)
When built-in components of Asterisk fail to start they cause the
Asterisk startup to abort.  In these cases only the most critical
cleanup should be performed - closing databases and terminating
proceses.  These cleanups are registered using ast_register_atexit, all
other cleanups should not be run during startup abort.

The main reason for this change is that these cleanup procedures are
untestable from the partially initialized states, if they fail it could
prevent us from ever running the critical cleanup with ast_run_atexits.

Create separate initialization for dns_core.c to be run unconditionally
during startup instead of being initialized by the first dns resolver to
be registered. This ensures that 'sched' is initialized before it can be
potentially used.

Replace ast_register_atexit with ast_register_cleanup in media_cache.c.
There is no reason for this cleanup to happen unconditionally.

Change-Id: Iecc2df98008b21509925ff16740bd5fa29527db3

14 files changed:
include/asterisk/_private.h
main/asterisk.c
main/cel.c
main/core_local.c
main/devicestate.c
main/dns_core.c
main/dns_system_resolver.c
main/dsp.c
main/features.c
main/features_config.c
main/indications.c
main/media_cache.c
main/pbx_builtins.c
main/sorcery.c

index e989b16fd1c92156f05c5a022d18d68fc4c9e824..b9f552dd1be293f64279c0dcb5db82c4e743ce73 100644 (file)
@@ -59,6 +59,7 @@ int ast_test_init(void);            /*!< Provided by test.c */
 int ast_msg_init(void);             /*!< Provided by message.c */
 void ast_msg_shutdown(void);        /*!< Provided by message.c */
 int aco_init(void);             /*!< Provided by config_options.c */
+int dns_core_init(void);        /*!< Provided by dns_core.c */
 
 /*!
  * \brief Initialize the bridging system.
index e44c896ef88cef55195e8dc52725f9407fd9748b..f5d1c217b74d27a759cab6a22bfc60580ba04c18 100644 (file)
@@ -4573,6 +4573,7 @@ static void asterisk_daemon(int isroot, const char *runuser, const char *rungrou
        check_init(ast_parking_stasis_init(), "Parking Core");
        check_init(ast_device_state_engine_init(), "Device State Engine");
        check_init(ast_presence_state_engine_init(), "Presence State Engine");
+       check_init(dns_core_init(), "DNS Resolver Core");
        check_init(ast_dns_system_resolver_init(), "Default DNS resolver");
        check_init(load_modules(1), "Module Preload");
        check_init(ast_features_init(), "Call Features");
index e4fae6dca0d8724c9ce4ab3e052faef79ef95bfd..2813e60c4caeac6adc5caad315552a779764c3be 100644 (file)
@@ -1563,7 +1563,6 @@ int ast_cel_engine_init(void)
        ao2_global_obj_replace_unref(cel_linkedids, container);
        ao2_cleanup(container);
        if (!container) {
-               cel_engine_cleanup();
                return -1;
        }
 
@@ -1572,17 +1571,14 @@ int ast_cel_engine_init(void)
        ao2_global_obj_replace_unref(cel_dialstatus_store, container);
        ao2_cleanup(container);
        if (!container) {
-               cel_engine_cleanup();
                return -1;
        }
 
        if (STASIS_MESSAGE_TYPE_INIT(cel_generic_type)) {
-               cel_engine_cleanup();
                return -1;
        }
 
        if (ast_cli_register(&cli_status)) {
-               cel_engine_cleanup();
                return -1;
        }
 
@@ -1590,12 +1586,10 @@ int ast_cel_engine_init(void)
        ao2_global_obj_replace_unref(cel_backends, container);
        ao2_cleanup(container);
        if (!container) {
-               cel_engine_cleanup();
                return -1;
        }
 
        if (aco_info_init(&cel_cfg_info)) {
-               cel_engine_cleanup();
                return -1;
        }
 
@@ -1608,7 +1602,6 @@ int ast_cel_engine_init(void)
                struct cel_config *cel_cfg = cel_config_alloc();
 
                if (!cel_cfg) {
-                       cel_engine_cleanup();
                        return -1;
                }
 
@@ -1621,12 +1614,10 @@ int ast_cel_engine_init(void)
        }
 
        if (create_subscriptions()) {
-               cel_engine_cleanup();
                return -1;
        }
 
        if (ast_cel_check_enabled() && create_routes()) {
-               cel_engine_cleanup();
                return -1;
        }
 
index 23c7cce9d86ba1d64df619d80c94c4c037bffcb0..c3fa15f6c273c13f672a1ea63443fbca4b5f6e98 100644 (file)
@@ -1049,7 +1049,6 @@ static void local_shutdown(void)
 
 int ast_local_init(void)
 {
-
        if (STASIS_MESSAGE_TYPE_INIT(ast_local_optimization_begin_type)) {
                return -1;
        }
@@ -1069,17 +1068,13 @@ int ast_local_init(void)
 
        locals = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, locals_cmp_cb);
        if (!locals) {
-               ao2_cleanup(local_tech.capabilities);
-               local_tech.capabilities = NULL;
                return -1;
        }
 
        /* Make sure we can register our channel type */
        if (ast_channel_register(&local_tech)) {
                ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
-               ao2_ref(locals, -1);
-               ao2_cleanup(local_tech.capabilities);
-               local_tech.capabilities = NULL;
+
                return -1;
        }
        ast_cli_register_multiple(cli_local, ARRAY_LEN(cli_local));
index 1db9a1937f89edc631b3fcf4d3cdd0e5cdfbde92..5df34497a76a616e87189b0119c2f898f262c11e 100644 (file)
@@ -911,24 +911,20 @@ int devstate_init(void)
        }
        device_state_topic_all = stasis_topic_create("ast_device_state_topic");
        if (!device_state_topic_all) {
-               devstate_cleanup();
                return -1;
        }
        device_state_topic_pool = stasis_topic_pool_create(ast_device_state_topic_all());
        if (!device_state_topic_pool) {
-               devstate_cleanup();
                return -1;
        }
        device_state_cache = stasis_cache_create_full(device_state_get_id,
                device_state_aggregate_calc, device_state_aggregate_publish);
        if (!device_state_cache) {
-               devstate_cleanup();
                return -1;
        }
        device_state_topic_cached = stasis_caching_topic_create(ast_device_state_topic_all(),
                device_state_cache);
        if (!device_state_topic_cached) {
-               devstate_cleanup();
                return -1;
        }
 
@@ -936,7 +932,6 @@ int devstate_init(void)
                devstate_change_cb, NULL);
        if (!devstate_message_sub) {
                ast_log(LOG_ERROR, "Failed to create subscription creating uncached device state aggregate events.\n");
-               devstate_cleanup();
                return -1;
        }
 
index 3e270aff7eea89bfa810ede62e7775748ca7ad15..6f37a5d46356af0a670876a026f4fbad218f0c64 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "asterisk.h"
 
+#include "asterisk/_private.h"
 #include "asterisk/linkedlists.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/strings.h"
@@ -537,6 +538,22 @@ static void dns_shutdown(void)
        }
 }
 
+int dns_core_init(void)
+{
+       sched = ast_sched_context_create();
+       if (!sched) {
+               return -1;
+       }
+
+       if (ast_sched_start_thread(sched)) {
+               return -1;
+       }
+
+       ast_register_cleanup(dns_shutdown);
+
+       return 0;
+}
+
 int ast_dns_resolver_register(struct ast_dns_resolver *resolver)
 {
        struct ast_dns_resolver *iter;
@@ -559,27 +576,6 @@ int ast_dns_resolver_register(struct ast_dns_resolver *resolver)
 
        AST_RWLIST_WRLOCK(&resolvers);
 
-       /* On the first registration of a resolver start a scheduler for recurring queries */
-       if (AST_LIST_EMPTY(&resolvers) && !sched) {
-               sched = ast_sched_context_create();
-               if (!sched) {
-                       ast_log(LOG_ERROR, "DNS resolver '%s' could not be registered: Failed to create scheduler for recurring DNS queries\n",
-                               resolver->name);
-                       AST_RWLIST_UNLOCK(&resolvers);
-                       return -1;
-               }
-
-               if (ast_sched_start_thread(sched)) {
-                       ast_log(LOG_ERROR, "DNS resolver '%s' could not be registered: Failed to start thread for recurring DNS queries\n",
-                               resolver->name);
-                       dns_shutdown();
-                       AST_RWLIST_UNLOCK(&resolvers);
-                       return -1;
-               }
-
-               ast_register_cleanup(dns_shutdown);
-       }
-
        AST_LIST_TRAVERSE(&resolvers, iter, next) {
                if (!strcmp(iter->name, resolver->name)) {
                        ast_log(LOG_ERROR, "A DNS resolver with the name '%s' is already registered\n", resolver->name);
index 9358577b6b8e3f3d023e4b79ae8c694e312fdc47..8cb92c0588c3d46198b06d88fb89e895c0dc4f2a 100644 (file)
@@ -255,7 +255,6 @@ int ast_dns_system_resolver_init(void)
 
        /* Return error if the task processor failed to instantiate */
        if (!dns_system_resolver_tp) {
-               dns_system_resolver_destroy();
                return DNS_SYSTEM_RESOLVER_FAILURE;
        }
 
index 8b39fe55e61c8b39850cba503cc160af79396ee1..66d95adc2bc762d3e755f5b719e37bba31849774 100644 (file)
@@ -2402,17 +2402,18 @@ static void test_dsp_shutdown(void)
 
 int ast_dsp_init(void)
 {
-       int res = _dsp_init(0);
+       if (_dsp_init(0)) {
+               return -1;
+       }
 
 #ifdef TEST_FRAMEWORK
-       if (!res) {
-               AST_TEST_REGISTER(test_dsp_fax_detect);
-               AST_TEST_REGISTER(test_dsp_dtmf_detect);
+       AST_TEST_REGISTER(test_dsp_fax_detect);
+       AST_TEST_REGISTER(test_dsp_dtmf_detect);
 
-               ast_register_cleanup(test_dsp_shutdown);
-       }
+       ast_register_cleanup(test_dsp_shutdown);
 #endif
-       return res;
+
+       return 0;
 }
 
 int ast_dsp_reload(void)
index 516c64a4ab5ee0f5b3d541072bec80ef2e939183..35039e0fa5ba28013ec2b834fa6b765ac04c0174 100644 (file)
@@ -1164,17 +1164,10 @@ int ast_features_init(void)
        int res;
 
        res = ast_features_config_init();
-       if (res) {
-               return res;
-       }
        res |= ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
        res |= ast_manager_register_xml_core("Bridge", EVENT_FLAG_CALL, action_bridge);
 
-       if (res) {
-               features_shutdown();
-       } else {
-               ast_register_cleanup(features_shutdown);
-       }
+       ast_register_cleanup(features_shutdown);
 
        return res;
 }
index a773f497fd5b51fcbbae0a994b1275d95bde41bc..195fbaceb101a7fd3b473a36dd6d61e6e2c4b9a7 100644 (file)
@@ -2000,9 +2000,5 @@ int ast_features_config_init(void)
        res |= __ast_custom_function_register(&featuremap_function, NULL);
        res |= ast_cli_register_multiple(cli_features_config, ARRAY_LEN(cli_features_config));
 
-       if (res) {
-               ast_features_config_shutdown();
-       }
-
        return res;
 }
index bde6e012da83a3885f2fd140612e467db564b1ee..89710584b587160a6209bbed96cbc37ef2617ffa 100644 (file)
@@ -1130,13 +1130,13 @@ static void indications_shutdown(void)
 /*! \brief Load indications module */
 int ast_indications_init(void)
 {
-       if (!(ast_tone_zones = ao2_container_alloc(NUM_TONE_ZONE_BUCKETS,
-                       ast_tone_zone_hash, ast_tone_zone_cmp))) {
+       ast_tone_zones = ao2_container_alloc(NUM_TONE_ZONE_BUCKETS,
+                       ast_tone_zone_hash, ast_tone_zone_cmp);
+       if (!ast_tone_zones) {
                return -1;
        }
 
        if (load_indications(0)) {
-               indications_shutdown();
                return -1;
        }
 
index 90057dc6f1febbd742a9427b2f977c2aadd1936e..e93d1a02fbe3e07d16851b7ee236c1151efe2a73 100644 (file)
@@ -645,7 +645,7 @@ static struct ast_cli_entry cli_media_cache[] = {
  */
 static void media_cache_shutdown(void)
 {
-       ao2_ref(media_cache, -1);
+       ao2_cleanup(media_cache);
        media_cache = NULL;
 
        ast_cli_unregister_multiple(cli_media_cache, ARRAY_LEN(cli_media_cache));
@@ -653,7 +653,7 @@ static void media_cache_shutdown(void)
 
 int ast_media_cache_init(void)
 {
-       ast_register_atexit(media_cache_shutdown);
+       ast_register_cleanup(media_cache_shutdown);
 
        media_cache = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_MUTEX, AO2_BUCKETS,
                ast_sorcery_object_id_hash, ast_sorcery_object_id_compare);
@@ -662,7 +662,6 @@ int ast_media_cache_init(void)
        }
 
        if (ast_cli_register_multiple(cli_media_cache, ARRAY_LEN(cli_media_cache))) {
-               ao2_ref(media_cache, -1);
                return -1;
        }
 
index 9d43c10ffc2ca5898449b5b427483108da54bee2..7f76b9776567c6b6f16712b19d84dcab2abc48dc 100644 (file)
@@ -1509,7 +1509,6 @@ int load_pbx_builtins(void)
        for (x = 0; x < ARRAY_LEN(builtins); x++) {
                if (ast_register_application2(builtins[x].name, builtins[x].execute, NULL, NULL, NULL)) {
                        ast_log(LOG_ERROR, "Unable to register builtin application '%s'\n", builtins[x].name);
-                       unload_pbx_builtins();
                        return -1;
                }
        }
index f4c5dcea4712a7ba7e0d426443c2e38902824bc8..d95a6e127a48b6eca85b976127c6f6f0c4c5271a 100644 (file)
@@ -388,20 +388,17 @@ int ast_sorcery_init(void)
        wizards = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, WIZARD_BUCKETS,
                ast_sorcery_internal_wizard_hash_fn, NULL, ast_sorcery_internal_wizard_cmp_fn);
        if (!wizards) {
-               sorcery_cleanup();
                return -1;
        }
 
        observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL);
        if (!observers) {
-               sorcery_cleanup();
                return -1;
        }
 
        instances = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, INSTANCE_BUCKETS,
                sorcery_proxy_hash_fn, NULL, sorcery_proxy_cmp_fn);
        if (!instances) {
-               sorcery_cleanup();
                return -1;
        }