memory leaks: Memory leak cleanup patch by Corey Farrell (second set)
[asterisk/asterisk.git] / main / asterisk.c
index beed010..2e5ffa7 100644 (file)
@@ -241,11 +241,13 @@ int daemon(int, int);  /* defined in libresolv of all places */
 #include "asterisk/aoc.h"
 #include "asterisk/uuid.h"
 #include "asterisk/sorcery.h"
+#include "asterisk/bucket.h"
 #include "asterisk/stasis.h"
 #include "asterisk/json.h"
 #include "asterisk/stasis_endpoints.h"
 #include "asterisk/stasis_system.h"
 #include "asterisk/security_events.h"
+#include "asterisk/endpoints.h"
 
 #include "../defaults.h"
 
@@ -1871,8 +1873,8 @@ static int can_safely_quit(shutdown_nice_t niceness, int restart)
                time_t s, e;
                /* Begin shutdown routine, hanging up active channels */
                ast_begin_shutdown(1);
-               if (option_verbose && ast_opt_console) {
-                       ast_verbose("Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
+               if (ast_opt_console) {
+                       ast_verb(0, "Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
                }
                time(&s);
                for (;;) {
@@ -1888,7 +1890,7 @@ static int can_safely_quit(shutdown_nice_t niceness, int restart)
                if (niceness != SHUTDOWN_REALLY_NICE) {
                        ast_begin_shutdown(0);
                }
-               if (option_verbose && ast_opt_console) {
+               if (ast_opt_console) {
                        ast_verb(0, "Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt");
                }
                for (;;) {
@@ -1904,7 +1906,7 @@ static int can_safely_quit(shutdown_nice_t niceness, int restart)
         */
        ast_mutex_lock(&safe_system_lock);
        if (shuttingdown != niceness) {
-               if (shuttingdown == NOT_SHUTTING_DOWN && option_verbose && ast_opt_console) {
+               if (shuttingdown == NOT_SHUTTING_DOWN && ast_opt_console) {
                        ast_verb(0, "Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
                }
                ast_mutex_unlock(&safe_system_lock);
@@ -1920,7 +1922,7 @@ static int can_safely_quit(shutdown_nice_t niceness, int restart)
 static void really_quit(int num, shutdown_nice_t niceness, int restart)
 {
        int active_channels;
-       RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
+       struct ast_json *json_object = NULL;
        int run_cleanups = niceness >= SHUTDOWN_NICE;
 
        if (run_cleanups) {
@@ -1958,6 +1960,8 @@ static void really_quit(int num, shutdown_nice_t niceness, int restart)
                                "Shutdown", active_channels ? "Uncleanly" : "Cleanly",
                                "Restart", restart ? "True" : "False");
                ast_manager_publish_event("Shutdown", EVENT_FLAG_SYSTEM, json_object);
+               ast_json_unref(json_object);
+               json_object = NULL;
        }
        ast_verb(0, "Asterisk %s ending (%d).\n",
                active_channels ? "uncleanly" : "cleanly", num);
@@ -1971,11 +1975,17 @@ static void really_quit(int num, shutdown_nice_t niceness, int restart)
                close(ast_socket);
                ast_socket = -1;
                unlink(ast_config_AST_SOCKET);
+               pthread_kill(lthread, SIGURG);
+               pthread_join(lthread, NULL);
        }
        if (ast_consock > -1)
                close(ast_consock);
        if (!ast_opt_remote)
                unlink(ast_config_AST_PID);
+       if (sig_alert_pipe[0])
+               close(sig_alert_pipe[0]);
+       if (sig_alert_pipe[1])
+               close(sig_alert_pipe[1]);
        printf("%s", term_quit());
        if (restart) {
                int i;
@@ -2027,49 +2037,127 @@ static void __remote_quit_handler(int num)
        sig_flags.need_quit = 1;
 }
 
-static const char *fix_header(char *outbuf, int maxout, const char *s, char *cmp)
+static const char *fix_header(char *outbuf, int maxout, const char *s, char level)
 {
-       const char *c;
+       const char *cmp;
+
+       switch (level) {
+       case 0: *outbuf = '\0';
+               return s;
+       case 1: cmp = VERBOSE_PREFIX_1;
+               break;
+       case 2: cmp = VERBOSE_PREFIX_2;
+               break;
+       case 3: cmp = VERBOSE_PREFIX_3;
+               break;
+       default: cmp = VERBOSE_PREFIX_4;
+               break;
+       }
 
        if (!strncmp(s, cmp, strlen(cmp))) {
-               c = s + strlen(cmp);
-               term_color(outbuf, cmp, COLOR_GRAY, 0, maxout);
-               return c;
+               s += strlen(cmp);
        }
-       return NULL;
+       term_color(outbuf, cmp, COLOR_GRAY, 0, maxout);
+
+       return s;
 }
 
+struct console_state_data {
+       char verbose_line_level;
+};
+
+static int console_state_init(void *ptr)
+{
+       struct console_state_data *state = ptr;
+       state->verbose_line_level = 0;
+       return 0;
+}
+
+AST_THREADSTORAGE_CUSTOM(console_state, console_state_init, ast_free_ptr);
+
 /* These gymnastics are due to platforms which designate char as unsigned by
  * default. Level is the negative character -- offset by 1, because \0 is the
  * EOS delimiter. */
 #define VERBOSE_MAGIC2LEVEL(x) (((char) -*(signed char *) (x)) - 1)
 #define VERBOSE_HASMAGIC(x)    (*(signed char *) (x) < 0)
 
-static void console_verboser(const char *s)
+static int console_print(const char *s, int local)
 {
-       char tmp[80];
-       const char *c = NULL;
-       char level = 0;
+       struct console_state_data *state =
+               ast_threadstorage_get(&console_state, sizeof(*state));
 
-       if (VERBOSE_HASMAGIC(s)) {
-               level = VERBOSE_MAGIC2LEVEL(s);
-               s++;
-               if (level > option_verbose) {
-                       return;
+       char prefix[80];
+       const char *c;
+       int num, res = 0;
+       unsigned int newline;
+
+       do {
+               if (VERBOSE_HASMAGIC(s)) {
+                       /* always use the given line's level, otherwise
+                          we'll use the last line's level */
+                       state->verbose_line_level = VERBOSE_MAGIC2LEVEL(s);
+                       /* move past magic */
+                       s++;
+
+                       if (local) {
+                               s = fix_header(prefix, sizeof(prefix), s,
+                                              state->verbose_line_level);
+                       }
+               } else {
+                       *prefix = '\0';
                }
+               c = s;
+
+               /* for a given line separate on verbose magic, newline, and eol */
+               if ((s = strchr(c, '\n'))) {
+                       ++s;
+                       newline = 1;
+               } else {
+                       s = strchr(c, '\0');
+                       newline = 0;
+               }
+
+               /* check if we should write this line after calculating begin/end
+                  so we process the case of a higher level line embedded within
+                  two lower level lines */
+               if (state->verbose_line_level > option_verbose) {
+                       continue;
+               }
+
+               if (local && !ast_strlen_zero(prefix)) {
+                       fputs(prefix, stdout);
+               }
+
+               num = s - c;
+               if (fwrite(c, sizeof(char), num, stdout) < num) {
+                       break;
+               }
+
+               if (!res) {
+                       /* if at least some info has been written
+                          we'll want to return true */
+                       res = 1;
+               }
+       } while (*s);
+
+       if (newline) {
+               /* if ending on a newline then reset last level to zero
+                   since what follows may be not be logging output */
+               state->verbose_line_level = 0;
        }
 
-       if ((c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_4)) ||
-           (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_3)) ||
-           (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_2)) ||
-           (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_1))) {
-               fputs(tmp, stdout);
-               fputs(c, stdout);
-       } else {
-               fputs(s, stdout);
+       if (res) {
+               fflush(stdout);
        }
 
-       fflush(stdout);
+       return res;
+}
+
+static void console_verboser(const char *s)
+{
+       if (!console_print(s, 1)) {
+               return;
+       }
 
        /* Wake up a poll()ing console */
        if (ast_opt_console && consolethread != AST_PTHREADT_NULL) {
@@ -2112,6 +2200,11 @@ static int remoteconsolehandler(char *s)
        /* Called when readline data is available */
        if (!ast_all_zeros(s))
                ast_el_add_history(s);
+
+       while (isspace(*s)) {
+               s++;
+       }
+
        /* The real handler for bang */
        if (s[0] == '!') {
                if (s[1])
@@ -2472,23 +2565,6 @@ static struct ast_cli_entry cli_asterisk[] = {
 #endif /* ! LOW_MEMORY */
 };
 
-struct el_read_char_state_struct {
-       unsigned int line_full:1;
-       unsigned int prev_line_full:1;
-       char prev_line_verbosity;
-};
-
-static int el_read_char_state_init(void *ptr)
-{
-       struct el_read_char_state_struct *state = ptr;
-       state->line_full = 1;
-       state->prev_line_full = 1;
-       state->prev_line_verbosity = 0;
-       return 0;
-}
-
-AST_THREADSTORAGE_CUSTOM(el_read_char_state, el_read_char_state_init, ast_free_ptr);
-
 static int ast_el_read_char(EditLine *editline, char *cp)
 {
        int num_read = 0;
@@ -2498,7 +2574,6 @@ static int ast_el_read_char(EditLine *editline, char *cp)
        int max;
 #define EL_BUF_SIZE 512
        char buf[EL_BUF_SIZE];
-       struct el_read_char_state_struct *state = ast_threadstorage_get(&el_read_char_state, sizeof(*state));
 
        for (;;) {
                max = 1;
@@ -2528,8 +2603,6 @@ static int ast_el_read_char(EditLine *editline, char *cp)
                        }
                }
                if (fds[0].revents) {
-                       char level = 0;
-                       char *curline = buf, *nextline;
                        res = read(ast_consock, buf, sizeof(buf) - 1);
                        /* if the remote side disappears exit */
                        if (res < 1) {
@@ -2569,33 +2642,7 @@ static int ast_el_read_char(EditLine *editline, char *cp)
                                }
                        }
 
-                       do {
-                               state->prev_line_full = state->line_full;
-                               if ((nextline = strchr(curline, '\n'))) {
-                                       state->line_full = 1;
-                                       nextline++;
-                               } else {
-                                       state->line_full = 0;
-                                       nextline = strchr(curline, '\0');
-                               }
-
-                               if (state->prev_line_full && VERBOSE_HASMAGIC(curline)) {
-                                       level = VERBOSE_MAGIC2LEVEL(curline);
-                                       curline++;
-                               } else if (state->prev_line_full && !VERBOSE_HASMAGIC(curline)) {
-                                       /* Non-verbose output */
-                                       level = 0;
-                               } else {
-                                       level = state->prev_line_verbosity;
-                               }
-                               if ((!state->prev_line_full && state->prev_line_verbosity <= option_verbose) || (state->prev_line_full && level <= option_verbose)) {
-                                       if (write(STDOUT_FILENO, curline, nextline - curline) < 0) {
-                                       }
-                               }
-
-                               state->prev_line_verbosity = level;
-                               curline = nextline;
-                       } while (!ast_strlen_zero(curline));
+                       console_print(buf, 0);
 
                        if ((res < EL_BUF_SIZE - 1) && ((buf[res-1] == '\n') || (buf[res-2] == '\n'))) {
                                *cp = CC_REFRESH;
@@ -2737,45 +2784,62 @@ static char *cli_prompt(EditLine *editline)
        return ast_str_buffer(prompt);
 }
 
+static void destroy_match_list(char **match_list, int matches)
+{
+       if (match_list) {
+               int idx;
+
+               for (idx = 0; idx < matches; ++idx) {
+                       ast_free(match_list[idx]);
+               }
+               ast_free(match_list);
+       }
+}
+
 static char **ast_el_strtoarr(char *buf)
 {
-       char **match_list = NULL, **match_list_tmp, *retstr;
-       size_t match_list_len;
+       char *retstr;
+       char **match_list = NULL;
+       char **new_list;
+       size_t match_list_len = 1;
        int matches = 0;
 
-       match_list_len = 1;
-       while ( (retstr = strsep(&buf, " ")) != NULL) {
-
-               if (!strcmp(retstr, AST_CLI_COMPLETE_EOF))
+       while ((retstr = strsep(&buf, " "))) {
+               if (!strcmp(retstr, AST_CLI_COMPLETE_EOF)) {
                        break;
+               }
                if (matches + 1 >= match_list_len) {
                        match_list_len <<= 1;
-                       if ((match_list_tmp = ast_realloc(match_list, match_list_len * sizeof(char *)))) {
-                               match_list = match_list_tmp;
-                       } else {
-                               if (match_list)
-                                       ast_free(match_list);
-                               return (char **) NULL;
+                       new_list = ast_realloc(match_list, match_list_len * sizeof(char *));
+                       if (!new_list) {
+                               destroy_match_list(match_list, matches);
+                               return NULL;
                        }
+                       match_list = new_list;
                }
 
-               match_list[matches++] = ast_strdup(retstr);
+               retstr = ast_strdup(retstr);
+               if (!retstr) {
+                       destroy_match_list(match_list, matches);
+                       return NULL;
+               }
+               match_list[matches++] = retstr;
        }
 
-       if (!match_list)
-               return (char **) NULL;
+       if (!match_list) {
+               return NULL;
+       }
 
        if (matches >= match_list_len) {
-               if ((match_list_tmp = ast_realloc(match_list, (match_list_len + 1) * sizeof(char *)))) {
-                       match_list = match_list_tmp;
-               } else {
-                       if (match_list)
-                               ast_free(match_list);
-                       return (char **) NULL;
+               new_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(char *));
+               if (!new_list) {
+                       destroy_match_list(match_list, matches);
+                       return NULL;
                }
+               match_list = new_list;
        }
 
-       match_list[matches] = (char *) NULL;
+       match_list[matches] = NULL;
 
        return match_list;
 }
@@ -2876,7 +2940,9 @@ static char *cli_complete(EditLine *editline, int ch)
 
                if (nummatches > 0) {
                        char *mbuf;
+                       char *new_mbuf;
                        int mlen = 0, maxmbuf = 2048;
+
                        /* Start with a 2048 byte buffer */
                        if (!(mbuf = ast_malloc(maxmbuf))) {
                                *((char *) lf->cursor) = savechr;
@@ -2890,10 +2956,13 @@ static char *cli_complete(EditLine *editline, int ch)
                                if (mlen + 1024 > maxmbuf) {
                                        /* Every step increment buffer 1024 bytes */
                                        maxmbuf += 1024;
-                                       if (!(mbuf = ast_realloc(mbuf, maxmbuf))) {
+                                       new_mbuf = ast_realloc(mbuf, maxmbuf);
+                                       if (!new_mbuf) {
+                                               ast_free(mbuf);
                                                *((char *) lf->cursor) = savechr;
                                                return (char *)(CC_ERROR);
                                        }
+                                       mbuf = new_mbuf;
                                }
                                /* Only read 1024 bytes at a time */
                                res = read(ast_consock, mbuf + mlen, 1024);
@@ -3016,12 +3085,23 @@ static int ast_el_initialize(void)
 static int ast_el_add_history(char *buf)
 {
        HistEvent ev;
+       char *stripped_buf;
 
-       if (el_hist == NULL || el == NULL)
+       if (el_hist == NULL || el == NULL) {
                ast_el_initialize();
-       if (strlen(buf) > (MAX_HISTORY_COMMAND_LENGTH - 1))
+       }
+       if (strlen(buf) > (MAX_HISTORY_COMMAND_LENGTH - 1)) {
                return 0;
-       return (history(el_hist, &ev, H_ENTER, ast_strip(ast_strdupa(buf))));
+       }
+
+       stripped_buf = ast_strip(ast_strdupa(buf));
+
+       /* HISTCONTROL=ignoredups */
+       if (!history(el_hist, &ev, H_FIRST) && strcmp(ev.str, stripped_buf) == 0) {
+               return 0;
+       }
+
+       return history(el_hist, &ev, H_ENTER, stripped_buf);
 }
 
 static int ast_el_write_history(char *filename)
@@ -3608,7 +3688,7 @@ static void env_init(void)
 
 static void print_intro_message(const char *runuser, const char *rungroup)
 {
-       if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
+       if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
                if (ast_register_verbose(console_verboser)) {
                        fprintf(stderr, "Unable to register console verboser?\n");
                        return;
@@ -4102,8 +4182,8 @@ int main(int argc, char *argv[])
 
        print_intro_message(runuser, rungroup);
 
-       if (ast_opt_console && !option_verbose) {
-               ast_verbose("[ Initializing Custom Configuration Options ]\n");
+       if (ast_opt_console) {
+               ast_verb(0, "[ Initializing Custom Configuration Options ]\n");
        }
        /* custom config setup */
        register_config_cli();
@@ -4144,11 +4224,6 @@ int main(int argc, char *argv[])
                exit(1);
        }
 
-       if (ast_event_init()) {
-               printf("%s", term_quit());
-               exit(1);
-       }
-
 #ifdef TEST_FRAMEWORK
        if (ast_test_init()) {
                printf("%s", term_quit());
@@ -4176,6 +4251,11 @@ int main(int argc, char *argv[])
 
        aco_init();
 
+       if (ast_bucket_init()) {
+               printf("%s", term_quit());
+               exit(1);
+       }
+
        if (stasis_init()) {
                printf("Stasis initialization failed.\n%s", term_quit());
                exit(1);
@@ -4267,6 +4347,11 @@ int main(int argc, char *argv[])
 
        ast_channels_init();
 
+       if (ast_endpoint_init()) {
+               printf ("%s", term_quit());
+               exit(1);
+       }
+
        if ((moduleresult = load_modules(1))) {         /* Load modules, pre-load only */
                printf("%s", term_quit());
                exit(moduleresult == -2 ? 2 : 1);