Add a massive set of changes for converting to use the ast_debug() macro.
[asterisk/asterisk.git] / main / asterisk.c
index be8ef8c..4f5ada9 100644 (file)
@@ -126,6 +126,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 */
 
@@ -292,8 +293,9 @@ void ast_unregister_file_version(const char *file)
        }
        AST_LIST_TRAVERSE_SAFE_END;
        AST_LIST_UNLOCK(&file_versions);
+
        if (find)
-               free(find);
+               ast_free(find);
 }
 
 struct thread_list_t {
@@ -335,8 +337,8 @@ void ast_unregister_thread(void *id)
        AST_LIST_TRAVERSE_SAFE_END;
        AST_LIST_UNLOCK(&thread_list);
        if (x) {
-               free(x->name);
-               free(x);
+               ast_free(x->name);
+               ast_free(x);
        }
 }
 
@@ -932,9 +934,6 @@ static void *listener(void *unused)
        int x;
        int flags;
        struct pollfd fds[1];
-       pthread_attr_t attr;
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        for (;;) {
                if (ast_socket < 0)
                        return NULL;
@@ -966,7 +965,7 @@ static void *listener(void *unused)
                                        fcntl(consoles[x].p[1], F_SETFL, flags | O_NONBLOCK);
                                        consoles[x].fd = s;
                                        consoles[x].mute = ast_opt_mute;
-                                       if (ast_pthread_create_background(&consoles[x].t, &attr, netconsole, &consoles[x])) {
+                                       if (ast_pthread_create_detached_background(&consoles[x].t, NULL, netconsole, &consoles[x])) {
                                                ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno));
                                                close(consoles[x].p[0]);
                                                close(consoles[x].p[1]);
@@ -1247,6 +1246,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")) 
@@ -1264,8 +1266,7 @@ static void quit_handler(int num, int nice, int safeshutdown, int restart)
        /* Called on exit */
        if (option_verbose && ast_opt_console)
                ast_verbose("Asterisk %s ending (%d).\n", ast_active_channels() ? "uncleanly" : "cleanly", num);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "Asterisk ending (%d).\n", num);
+       ast_debug(1, "Asterisk ending (%d).\n", num);
        manager_event(EVENT_FLAG_SYSTEM, "Shutdown", "Shutdown: %s\r\nRestart: %s\r\n", ast_active_channels() ? "Uncleanly" : "Cleanly", restart ? "True" : "False");
        if (ast_socket > -1) {
                pthread_cancel(lthread);
@@ -2035,7 +2036,7 @@ static int ast_cli_display_match_list(char **matches, int len, int max)
                        /* Don't print dupes */
                        if ( (matches[idx+1] != NULL && strcmp(matches[idx], matches[idx+1]) == 0 ) ) {
                                i--;
-                               free(matches[idx]);
+                               ast_free(matches[idx]);
                                matches[idx] = NULL;
                                continue;
                        }
@@ -2043,7 +2044,7 @@ static int ast_cli_display_match_list(char **matches, int len, int max)
                        numoutput++;
                        numoutputline++;
                        fprintf(stdout, "%-*s  ", max, matches[idx]);
-                       free(matches[idx]);
+                       ast_free(matches[idx]);
                        matches[idx] = NULL;
                }
                if (numoutputline > 0)
@@ -2112,7 +2113,7 @@ static char *cli_complete(EditLine *el, int ch)
                        mbuf[mlen] = '\0';
 
                        matches = ast_el_strtoarr(mbuf);
-                       free(mbuf);
+                       ast_free(mbuf);
                } else
                        matches = (char **) NULL;
        } else {
@@ -2158,8 +2159,8 @@ static char *cli_complete(EditLine *el, int ch)
                        }
                }
                for (i = 0; matches[i]; i++)
-                       free(matches[i]);
-               free(matches);
+                       ast_free(matches[i]);
+               ast_free(matches);
        }
 
        return (char *)(long)retval;
@@ -2374,6 +2375,7 @@ static void ast_readconfig(void)
        struct ast_config *cfg;
        struct ast_variable *v;
        char *config = AST_CONFIG_FILE;
+       char hostname[MAXHOSTNAMELEN] = "";
 
        if (ast_opt_override_config) {
                cfg = ast_config_load(ast_config_AST_CONFIG_FILE);
@@ -2523,6 +2525,17 @@ static void ast_readconfig(void)
                        ast_copy_string(ast_config_AST_RUN_GROUP, v->value, sizeof(ast_config_AST_RUN_GROUP));
                } else if (!strcasecmp(v->name, "systemname")) {
                        ast_copy_string(ast_config_AST_SYSTEM_NAME, v->value, sizeof(ast_config_AST_SYSTEM_NAME));
+               } else if (!strcasecmp(v->name, "autosystemname")) {
+                       if (ast_true(v->value)) {
+                               if (!gethostname(hostname, sizeof(hostname) - 1))
+                                       ast_copy_string(ast_config_AST_SYSTEM_NAME, hostname, sizeof(ast_config_AST_SYSTEM_NAME));
+                               else {
+                                       if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)){
+                                               ast_copy_string(ast_config_AST_SYSTEM_NAME, "localhost", sizeof(ast_config_AST_SYSTEM_NAME));
+                                       }
+                                       ast_log(LOG_ERROR, "Cannot obtain hostname for this system.  Using '%s' instead.\n", ast_config_AST_SYSTEM_NAME);
+                               }
+                       }
                } else if (!strcasecmp(v->name, "languageprefix")) {
                        ast_language_is_prefix = ast_true(v->value);
 #if defined(HAVE_SYSINFO)
@@ -2921,7 +2934,7 @@ int main(int argc, char *argv[])
 
        threadstorage_init();
 
-       if (load_modules(1)) {          /* Load modules */
+       if (load_modules(1)) {          /* Load modules, pre-load only */
                printf(term_quit());
                exit(1);
        }
@@ -3017,13 +3030,9 @@ int main(int argc, char *argv[])
                /* Console stuff now... */
                /* Register our quit function */
                char title[256];
-               pthread_attr_t attr;
                pthread_t dont_care;
 
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-               ast_pthread_create(&dont_care, &attr, monitor_sig_flags, NULL);
-               pthread_attr_destroy(&attr);
+               ast_pthread_create_detached(&dont_care, NULL, monitor_sig_flags, NULL);
 
                set_icon("Asterisk");
                snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %ld)", hostname, (long)ast_mainpid);