]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
When shutting down "gracefully", go through and run the unload() callbacks for
authorRussell Bryant <russell@russellbryant.com>
Tue, 5 Jun 2007 15:51:53 +0000 (15:51 +0000)
committerRussell Bryant <russell@russellbryant.com>
Tue, 5 Jun 2007 15:51:53 +0000 (15:51 +0000)
all of the modules.  "stop now" is considered a non-graceful shutdown and will
not go through this process.
(issue #9804, reported by chrisost, patch by me)

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

include/asterisk/module.h
main/asterisk.c
main/loader.c

index 1d00f45049c213d1dfad1dc0fe62982e9d12d919..b1585b60f12258144e8c3600a6d5a7f362aa6abb 100644 (file)
@@ -134,6 +134,13 @@ int ast_loader_register(int (*updater)(void));
  */
 int ast_loader_unregister(int (*updater)(void));
 
+/*!
+ * \brief Run the unload() callback for all loaded modules
+ *
+ * This function should be called when Asterisk is shutting down gracefully.
+ */
+void ast_module_shutdown(void);
+
 /*! 
  * \brief Match modules names for the Asterisk cli.
  * \param line Unused by this function, but this should be the line we are
index 570337168fe10f59e955357c80ac4312b58b7e4a..7eccefdbfc510ceff80fcb6117600fbb94a5730e 100644 (file)
@@ -124,6 +124,7 @@ int daemon(int, int);  /* defined in libresolv of all places */
 #include "asterisk/version.h"
 #include "asterisk/linkedlists.h"
 #include "asterisk/devicestate.h"
+#include "asterisk/module.h"
 
 #include "asterisk/doxyref.h"          /* Doxygen documentation */
 
@@ -1253,6 +1254,9 @@ static void quit_handler(int num, int nice, int safeshutdown, int restart)
                                ast_verbose("Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
                        return;
                }
+
+               if (nice)
+                       ast_module_shutdown();
        }
        if (ast_opt_console || ast_opt_remote) {
                if (getenv("HOME")) 
index 012f0e810d3bf12621860946e104587435534a5f..69d0be52c55c5f2ec78846f836cd18e029e43374 100644 (file)
@@ -427,6 +427,31 @@ static struct ast_module *load_dynamic_module(const char *resource_in, unsigned
 }
 #endif
 
+void ast_module_shutdown(void)
+{
+       struct ast_module *mod;
+       AST_LIST_HEAD_NOLOCK_STATIC(local_module_list, ast_module);
+
+       /* We have to call the unload() callbacks in reverse order that the modules
+        * exist in the module list so it is the reverse order of how they were
+        * loaded. */
+
+       AST_LIST_LOCK(&module_list);
+       while ((mod = AST_LIST_REMOVE_HEAD(&module_list, entry)))
+               AST_LIST_INSERT_HEAD(&local_module_list, mod, entry);
+       AST_LIST_UNLOCK(&module_list);
+
+       while ((mod = AST_LIST_REMOVE_HEAD(&local_module_list, entry))) {
+               if (mod->info->unload)
+                       mod->info->unload();
+               /* Since this should only be called when shutting down "gracefully",
+                * all channels should be down before we get to this point, meaning
+                * there will be no module users left. */
+               AST_LIST_HEAD_DESTROY(&mod->users);
+               free(mod);
+       }
+}
+
 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode force)
 {
        struct ast_module *mod;