Replace most uses of ast_register_atexit with ast_register_cleanup.
[asterisk/asterisk.git] / main / app.c
index a13116b..f3fc298 100644 (file)
@@ -519,6 +519,57 @@ void ast_vm_unregister(const char *module_name)
        ao2_cleanup(table);
 }
 
+#ifdef TEST_FRAMEWORK
+/*! \brief Holding container for the voicemail provider used while testing */
+static AO2_GLOBAL_OBJ_STATIC(vm_provider_holder);
+static int provider_is_swapped = 0;
+
+void ast_vm_test_swap_table_in(const struct ast_vm_functions *vm_table)
+{
+       RAII_VAR(struct ast_vm_functions *, holding_table, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_vm_functions *, new_table, NULL, ao2_cleanup);
+
+       if (provider_is_swapped) {
+               ast_log(LOG_ERROR, "Attempted to swap in test function table without swapping out old test table.\n");
+               return;
+       }
+
+       holding_table = ao2_global_obj_ref(vm_provider);
+
+       if (holding_table) {
+               ao2_global_obj_replace_unref(vm_provider_holder, holding_table);
+       }
+
+       new_table = ao2_alloc_options(sizeof(*new_table), NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
+       if (!new_table) {
+               return;
+       }
+       *new_table = *vm_table;
+
+       ao2_global_obj_replace_unref(vm_provider, new_table);
+       provider_is_swapped = 1;
+}
+
+void ast_vm_test_swap_table_out(void)
+{
+       RAII_VAR(struct ast_vm_functions *, held_table, NULL, ao2_cleanup);
+
+       if (!provider_is_swapped) {
+               ast_log(LOG_ERROR, "Attempted to swap out test function table, but none is currently installed.\n");
+               return;
+       }
+
+       held_table = ao2_global_obj_ref(vm_provider_holder);
+       if (!held_table) {
+               return;
+       }
+
+       ao2_global_obj_replace_unref(vm_provider, held_table);
+       ao2_global_obj_release(vm_provider_holder);
+       provider_is_swapped = 0;
+}
+#endif
+
 /*! \brief The container for the voicemail greeter provider */
 static AO2_GLOBAL_OBJ_STATIC(vm_greeter_provider);
 
@@ -1046,9 +1097,6 @@ static int control_streamfile(struct ast_channel *chan,
                        strcat(breaks, restart);
                }
        }
-       if (ast_channel_state(chan) != AST_STATE_UP) {
-               res = ast_answer(chan);
-       }
 
        if ((end = strchr(file, ':'))) {
                if (!strcasecmp(end, ":end")) {
@@ -1328,7 +1376,6 @@ int ast_play_and_wait(struct ast_channel *chan, const char *fn)
 {
        int d = 0;
 
-       ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", fn, ast_channel_name(chan));
        if ((d = ast_streamfile(chan, fn, ast_channel_language(chan)))) {
                return d;
        }
@@ -1786,18 +1833,20 @@ static int __ast_play_and_record(struct ast_channel *chan, const char *playfile,
                        ast_truncstream(others[x]);
                        ast_closestream(others[x]);
                }
-       }
-
-       if (prepend && outmsg) {
+       } else if (prepend && outmsg) {
                struct ast_filestream *realfiles[AST_MAX_FORMATS];
                struct ast_frame *fr;
 
                for (x = 0; x < fmtcnt; x++) {
                        snprintf(comment, sizeof(comment), "Opening the real file %s.%s\n", recordfile, sfmt[x]);
                        realfiles[x] = ast_readfile(recordfile, sfmt[x], comment, O_RDONLY, 0, 0);
-                       if (!others[x] || !realfiles[x]) {
+                       if (!others[x]) {
                                break;
                        }
+                       if (!realfiles[x]) {
+                               ast_closestream(others[x]);
+                               continue;
+                       }
                        /*!\note Same logic as above. */
                        if (dspsilence) {
                                ast_stream_rewind(others[x], dspsilence - 200);
@@ -1814,7 +1863,15 @@ static int __ast_play_and_record(struct ast_channel *chan, const char *playfile,
                        ast_verb(4, "Recording Format: sfmts=%s, prependfile %s, recordfile %s\n", sfmt[x], prependfile, recordfile);
                        ast_filedelete(prependfile, sfmt[x]);
                }
+       } else {
+               for (x = 0; x < fmtcnt; x++) {
+                       if (!others[x]) {
+                               break;
+                       }
+                       ast_closestream(others[x]);
+               }
        }
+
        if (rfmt && ast_set_read_format(chan, rfmt)) {
                ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_format_get_name(rfmt), ast_channel_name(chan));
        }
@@ -1833,7 +1890,7 @@ static const char default_canceldtmf[] = "";
 
 int ast_play_and_record_full(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence, const char *path, const char *acceptdtmf, const char *canceldtmf, int skip_confirmation_sound, enum ast_record_if_exists if_exists)
 {
-       return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, sound_duration, beep, silencethreshold, maxsilence, path, 0, S_OR(acceptdtmf, default_acceptdtmf), S_OR(canceldtmf, default_canceldtmf), skip_confirmation_sound, if_exists);
+       return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, sound_duration, beep, silencethreshold, maxsilence, path, 0, S_OR(acceptdtmf, ""), S_OR(canceldtmf, default_canceldtmf), skip_confirmation_sound, if_exists);
 }
 
 int ast_play_and_record(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int *sound_duration, int silencethreshold, int maxsilence, const char *path)
@@ -1896,7 +1953,7 @@ int ast_app_group_set_channel(struct ast_channel *chan, const char *data)
        AST_RWLIST_TRAVERSE_SAFE_BEGIN(&groups, gi, group_list) {
                if ((gi->chan == chan) && ((ast_strlen_zero(category) && ast_strlen_zero(gi->category)) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))) {
                        AST_RWLIST_REMOVE_CURRENT(group_list);
-                       free(gi);
+                       ast_free(gi);
                        break;
                }
        }
@@ -1904,7 +1961,7 @@ int ast_app_group_set_channel(struct ast_channel *chan, const char *data)
 
        if (ast_strlen_zero(group)) {
                /* Enable unsetting the group */
-       } else if ((gi = calloc(1, len))) {
+       } else if ((gi = ast_calloc(1, len))) {
                gi->chan = chan;
                gi->group = (char *) gi + sizeof(*gi);
                strcpy(gi->group, group);
@@ -2180,9 +2237,9 @@ static void path_lock_destroy(struct path_lock *obj)
                close(obj->fd);
        }
        if (obj->path) {
-               free(obj->path);
+               ast_free(obj->path);
        }
-       free(obj);
+       ast_free(obj);
 }
 
 static enum AST_LOCK_RESULT ast_lock_path_flock(const char *path)
@@ -2226,7 +2283,7 @@ static enum AST_LOCK_RESULT ast_lock_path_flock(const char *path)
                return AST_LOCK_FAILURE;
        }
        pl->fd = fd;
-       pl->path = strdup(path);
+       pl->path = ast_strdup(path);
 
        time(&start);
        while (
@@ -2905,7 +2962,9 @@ int ast_safe_fork(int stop_reaper)
                ast_replace_sigchld();
        }
 
-       sigfillset(&signal_set);
+       /* GCC 4.9 gives a bogus "right-hand operand of comma expression has
+        * no effect" warning */
+       (void) sigfillset(&signal_set);
        pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
 
        pid = fork();