btr: Remove btr_free_node().
authorAndre Noll <maan@systemlinux.org>
Mon, 9 Apr 2012 19:57:54 +0000 (21:57 +0200)
committerAndre Noll <maan@systemlinux.org>
Sun, 8 Jul 2012 17:06:38 +0000 (17:06 +0000)
This has turned out to be source for bugs. Deallocate everything
in btr_remove_node() hence making removing the node and freeing
its resources an atomic operation.

To avoid dangling pointers to freed btrn nodes, the argument of
btr_remove_node() is changed to to struct btr_node **btrnp.

31 files changed:
aacdec_filter.c
alsa_write.c
amp_filter.c
ao_write.c
audioc.c
audiod.c
buffer_tree.c
buffer_tree.h
client.c
client_common.c
compress_filter.c
dccp_recv.c
fecdec_filter.c
file_write.c
filter.c
flacdec_filter.c
grab_client.c
http_recv.c
interactive.c
mp3dec_filter.c
oggdec_filter.c
oss_write.c
osx_write.c
recv.c
spxdec_filter.c
stdin.c
stdout.c
udp_recv.c
wav_filter.c
wmadec_filter.c
write.c

index a4414e8..c8b6092 100644 (file)
@@ -204,7 +204,7 @@ out:
 err:
        assert(ret < 0);
        t->error = ret;
-       btr_remove_node(btrn);
+       btr_remove_node(&fn->btrn);
 }
 
 /**
index f50ad6a..535e719 100644 (file)
@@ -324,7 +324,7 @@ again:
        ret = -E_ALSA;
 err:
        assert(ret < 0);
-       btr_remove_node(btrn);
+       btr_remove_node(&wn->btrn);
        t->error = ret;
 }
 
index 3dc1e41..7e88cc4 100644 (file)
@@ -118,7 +118,7 @@ next_buffer:
 err:
        assert(ret < 0);
        t->error = ret;
-       btr_remove_node(btrn);
+       btr_remove_node(&fn->btrn);
 }
 
 static void amp_free_config(void *conf)
index 93861ab..a45d4fb 100644 (file)
@@ -280,7 +280,6 @@ static void aow_post_select(__a_unused struct sched *s,
                struct task *t)
 {
        struct writer_node *wn = container_of(t, struct writer_node, task);
-       struct btr_node *btrn = wn->btrn;
        struct private_aow_data *pawd = wn->private_data;
        int ret;
 
@@ -288,14 +287,14 @@ static void aow_post_select(__a_unused struct sched *s,
                int32_t rate, ch, format;
                struct btr_node_description bnd;
 
-               ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
+               ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
                if (ret < 0)
                        goto remove_btrn;
                if (ret == 0)
                        return;
-               get_btr_sample_rate(btrn, &rate);
-               get_btr_channels(btrn, &ch);
-               get_btr_sample_format(btrn, &format);
+               get_btr_sample_rate(wn->btrn, &rate);
+               get_btr_channels(wn->btrn, &ch);
+               get_btr_sample_format(wn->btrn, &format);
                ret = aow_init(wn, rate, ch, format);
                if (ret < 0)
                        goto remove_btrn;
@@ -303,7 +302,7 @@ static void aow_post_select(__a_unused struct sched *s,
 
                /* set up thread btr node */
                bnd.name = "ao_thread_btrn";
-               bnd.parent = btrn;
+               bnd.parent = wn->btrn;
                bnd.child = NULL;
                bnd.handler = NULL;
                bnd.context = pawd;
@@ -316,27 +315,24 @@ static void aow_post_select(__a_unused struct sched *s,
                return;
        }
        pthread_mutex_lock(&pawd->mutex);
-       ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
+       ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
        if (ret > 0) {
-               btr_pushdown(btrn);
+               btr_pushdown(wn->btrn);
                pthread_cond_signal(&pawd->data_available);
        }
        pthread_mutex_unlock(&pawd->mutex);
        if (ret >= 0)
                goto out;
        pthread_mutex_lock(&pawd->mutex);
-       btr_remove_node(btrn);
-       btrn = NULL;
+       btr_remove_node(&wn->btrn);
        PARA_INFO_LOG("waiting for thread to terminate\n");
        pthread_cond_signal(&pawd->data_available);
        pthread_mutex_unlock(&pawd->mutex);
        pthread_join(pawd->thread, NULL);
 remove_thread_btrn:
-       btr_remove_node(pawd->thread_btrn);
-       btr_free_node(pawd->thread_btrn);
+       btr_remove_node(&pawd->thread_btrn);
 remove_btrn:
-       if (btrn)
-               btr_remove_node(btrn);
+       btr_remove_node(&wn->btrn);
 out:
        t->error = ret;
 }
index d3e092e..3997001 100644 (file)
--- a/audioc.c
+++ b/audioc.c
@@ -128,9 +128,7 @@ static void audioc_post_select(struct sched *s, struct task *t)
 out:
        if (ret < 0) {
                free(buf);
-               btr_remove_node(at->btrn);
-               btr_free_node(at->btrn);
-               at->btrn = NULL;
+               btr_remove_node(&at->btrn);
                close(at->fd);
        }
        t->error = ret;
index 93bc8da..9f2aa5a 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -381,7 +381,7 @@ static void close_receiver(int slot_num)
        PARA_NOTICE_LOG("closing %s receiver in slot %d\n",
                audio_formats[s->format], slot_num);
        a->receiver->close(s->receiver_node);
-       btr_free_node(s->receiver_node->btrn);
+       btr_remove_node(&s->receiver_node->btrn);
        free(s->receiver_node);
        s->receiver_node = NULL;
        tv_add(now, &(struct timeval)EMBRACE(0, 200 * 1000),
@@ -397,7 +397,7 @@ static void writer_cleanup(struct writer_node *wn)
        w = writers + wn->writer_num;
        PARA_INFO_LOG("closing %s\n", writer_names[wn->writer_num]);
        w->close(wn);
-       btr_free_node(wn->btrn);
+       btr_remove_node(&wn->btrn);
 }
 
 static void close_writers(struct slot_info *s)
@@ -434,7 +434,7 @@ static void close_filters(struct slot_info *s)
                f = filters + fn->filter_num;
                if (f->close)
                        f->close(fn);
-               btr_free_node(fn->btrn);
+               btr_remove_node(&fn->btrn);
        }
        free(s->fns);
        s->fns = NULL;
@@ -446,12 +446,12 @@ static void close_filters(struct slot_info *s)
  * task. Note that the scheduler checks t->error also _before_ each pre/post
  * select call, so the victim will never be scheduled again.
  */
-static void kill_btrn(struct btr_node *btrn, struct task *t, int error)
+static void kill_btrn(struct btr_node **btrnp, struct task *t, int error)
 {
        if (t->error < 0)
                return;
        t->error = error;
-       btr_remove_node(btrn);
+       btr_remove_node(btrnp);
 }
 
 static void kill_all_decoders(int error)
@@ -464,7 +464,7 @@ static void kill_all_decoders(int error)
                        continue;
                if (!s->receiver_node)
                        continue;
-               kill_btrn(s->receiver_node->btrn, &s->receiver_node->task,
+               kill_btrn(&s->receiver_node->btrn, &s->receiver_node->task,
                                error);
        }
 }
@@ -563,7 +563,7 @@ static int open_receiver(int format)
                EMBRACE(.name = r->name, .context = rn));
        ret = r->open(rn);
        if (ret < 0) {
-               btr_free_node(rn->btrn);
+               btr_remove_node(&rn->btrn);
                free(rn);
                return ret;
        }
@@ -1196,7 +1196,7 @@ static void status_post_select(struct sched *s, struct task *t)
                if (!st->ct)
                        goto out;
                if (st->ct->task.error >= 0) {
-                       kill_btrn(st->ct->btrn, &st->ct->task, -E_AUDIOD_OFF);
+                       kill_btrn(&st->ct->btrn, &st->ct->task, -E_AUDIOD_OFF);
                        goto out;
                }
                close_stat_pipe();
@@ -1218,7 +1218,7 @@ static void status_post_select(struct sched *s, struct task *t)
                        struct timeval diff;
                        tv_diff(now, &st->last_status_read, &diff);
                        if (diff.tv_sec > 61)
-                               kill_btrn(st->ct->btrn, &st->ct->task,
+                               kill_btrn(&st->ct->btrn, &st->ct->task,
                                        -E_STATUS_TIMEOUT);
                        goto out;
                }
@@ -1226,7 +1226,7 @@ static void status_post_select(struct sched *s, struct task *t)
                sz = btr_next_buffer(st->btrn, &buf);
                ret = for_each_stat_item(buf, sz, update_item);
                if (ret < 0) {
-                       kill_btrn(st->ct->btrn, &st->ct->task, ret);
+                       kill_btrn(&st->ct->btrn, &st->ct->task, ret);
                        goto out;
                }
                if (sz != ret) {
index 7d79636..5c88470 100644 (file)
@@ -729,44 +729,37 @@ void btr_drain(struct btr_node *btrn)
                btr_drop_buffer_reference(br);
 }
 
-/**
- * Free all resources allocated by btr_new_node().
- *
- * \param btrn Pointer to a btr node obtained by \ref btr_new_node().
- *
- * Like free(3), it is OK to call this with a \p NULL pointer argument.
- */
-void btr_free_node(struct btr_node *btrn)
-{
-       if (!btrn)
-               return;
-       free(btrn->name);
-       free(btrn);
-}
-
 /**
  * Remove a node from a buffer tree.
  *
- * \param btrn The node to remove.
+ * \param btrnp Determines the node to remove.
  *
- * This makes all child nodes of \a btrn orphans and removes \a btrn from the
- * list of children of its parent. Moreover, the input queue of \a btrn is
- * flushed if it is not empty.
+ * This orphans all children of the node given by \a btrnp and removes this
+ * node from the child list of its parent. Moreover, the input queue is flushed
+ * and the node pointer given by \a btrp is set to \p NULL.
  *
  * \sa \ref btr_splice_out_node.
  */
-void btr_remove_node(struct btr_node *btrn)
+void btr_remove_node(struct btr_node **btrnp)
 {
        struct btr_node *ch;
+       struct btr_node *btrn;
 
-       if (!btrn)
+       if (!btrnp)
                return;
+       btrn = *btrnp;
+       if (!btrn)
+               goto out;
        PARA_INFO_LOG("removing btr node %s from buffer tree\n", btrn->name);
        FOR_EACH_CHILD(ch, btrn)
                ch->parent = NULL;
        btr_drain(btrn);
        if (btrn->parent)
                list_del(&btrn->node);
+       free(btrn->name);
+       free(btrn);
+out:
+       *btrnp = NULL;
 }
 
 /**
index 4d27ec7..91106a1 100644 (file)
@@ -182,8 +182,7 @@ void btr_copy(const void *src, size_t n, struct btr_pool *btrp,
        struct btr_node *btrn);
 
 struct btr_node *btr_new_node(struct btr_node_description *bnd);
-void btr_remove_node(struct btr_node *btrn);
-void btr_free_node(struct btr_node *btrn);
+void btr_remove_node(struct btr_node **btrnp);
 void btr_add_output(char *buf, size_t size, struct btr_node *btrn);
 size_t btr_get_input_queue_size(struct btr_node *btrn);
 size_t btr_get_output_queue_size(struct btr_node *btrn);
index c194e19..6268c3d 100644 (file)
--- a/client.c
+++ b/client.c
@@ -114,11 +114,11 @@ static int execute_client_command(const char *cmd, char **result)
                goto out;
        schedule(&command_sched);
        *result = exec_task.result_buf;
-       btr_remove_node(exec_task.btrn);
+       btr_remove_node(&exec_task.btrn);
        client_disconnect(ct);
        ret = 1;
 out:
-       btr_free_node(exec_task.btrn);
+       btr_remove_node(&exec_task.btrn);
        if (ret < 0)
                free(exec_task.result_buf);
        return ret;
@@ -619,7 +619,7 @@ out:
        if (ret < 0)
                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
        client_close(ct);
-       btr_free_node(sit.btrn);
-       btr_free_node(sot.btrn);
+       btr_remove_node(&sit.btrn);
+       btr_remove_node(&sot.btrn);
        return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
 }
index 649a1b4..a4aa6d8 100644 (file)
@@ -48,8 +48,7 @@ void client_disconnect(struct client_task *ct)
        ct->scc.recv = NULL;
        sc_free(ct->scc.send);
        ct->scc.send = NULL;
-       btr_free_node(ct->btrn);
-       ct->btrn = NULL;
+       btr_remove_node(&ct->btrn);
 }
 
 /**
@@ -562,7 +561,7 @@ out:
                if (!ct->use_sideband && ret != -E_SERVER_EOF &&
                                ret != -E_BTR_EOF && ret != -E_EOF)
                        PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
-               btr_remove_node(btrn);
+               btr_remove_node(&ct->btrn);
        }
 }
 
index 74ea59f..3dee5cc 100644 (file)
@@ -109,7 +109,7 @@ next_buffer:
 err:
        assert(ret < 0);
        t->error = ret;
-       btr_remove_node(btrn);
+       btr_remove_node(&fn->btrn);
 }
 
 /** TODO: Add sanity checks */
index 21c6932..ea6884b 100644 (file)
@@ -151,7 +151,7 @@ static void dccp_recv_post_select(struct sched *s, struct task *t)
 out:
        if (ret >= 0)
                return;
-       btr_remove_node(rn->btrn);
+       btr_remove_node(&rn->btrn);
        t->error = ret;
 }
 
index aea32bf..945e3e9 100644 (file)
@@ -470,7 +470,7 @@ next_buffer:
 out:
        t->error = ret;
        if (ret < 0)
-               btr_remove_node(btrn);
+               btr_remove_node(&fn->btrn);
 }
 
 static void fecdec_open(struct filter_node *fn)
index 5c5c514..98d15a4 100644 (file)
@@ -129,7 +129,7 @@ static void file_write_post_select(__a_unused struct sched *s,
        btr_consume(btrn, ret);
 out:
        if (ret < 0)
-               btr_remove_node(btrn);
+               btr_remove_node(&wn->btrn);
        t->error = ret;
 }
 
index 81258a6..55b48ea 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -161,13 +161,13 @@ out_cleanup:
                f = filters + fn->filter_num;
                if (f->close)
                        f->close(fn);
-               btr_free_node(fn->btrn);
+               btr_remove_node(&fn->btrn);
                free(fn->conf);
                free(fn);
        }
        free(fns);
-       btr_free_node(sit->btrn);
-       btr_free_node(sot->btrn);
+       btr_remove_node(&sit->btrn);
+       btr_remove_node(&sot->btrn);
 out:
        if (ret < 0)
                PARA_EMERG_LOG("%s\n", para_strerror(-ret));
index e8baa6b..dfd9021 100644 (file)
@@ -257,7 +257,7 @@ static void flacdec_post_select(__a_unused struct sched *s, struct task *t)
 out:
        t->error = ret;
        if (ret < 0)
-               btr_remove_node(btrn);
+               btr_remove_node(&fn->btrn);
 }
 
 static void flacdec_close(struct filter_node *fn)
index 07f779b..2b8c8d3 100644 (file)
@@ -166,9 +166,7 @@ void activate_grab_clients(struct sched *s)
 
 static int gc_close(struct grab_client *gc, int err)
 {
-       btr_remove_node(gc->btrn);
-       btr_free_node(gc->btrn);
-       gc->btrn = NULL;
+       btr_remove_node(&gc->btrn);
        PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
        list_move(&gc->node, &inactive_grab_client_list);
        if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
index b4bf153..11602f9 100644 (file)
@@ -128,7 +128,7 @@ static void http_recv_post_select(struct sched *s, struct task *t)
 out:
        if (ret >= 0)
                return;
-       btr_remove_node(rn->btrn);
+       btr_remove_node(&rn->btrn);
        t->error = ret;
 }
 
index f9ea361..68891ac 100644 (file)
@@ -298,9 +298,7 @@ static void i9e_post_select(struct sched *s, struct task *t)
        btr_consume(btrn, ret);
        goto out;
 rm_btrn:
-       btr_remove_node(btrn);
-       btr_free_node(btrn);
-       i9ep->stdout_btrn = NULL;
+       btr_remove_node(&i9ep->stdout_btrn);
        rl_set_prompt(i9ep->ici->prompt);
        rl_forced_update_display();
 out:
index 4bdbc6f..5a177c1 100644 (file)
@@ -166,7 +166,7 @@ decode:
 err:
        assert(ret < 0);
        t->error = ret;
-       btr_remove_node(btrn);
+       btr_remove_node(&fn->btrn);
 }
 
 static void mp3dec_open(struct filter_node *fn)
index 9498313..c5c0b53 100644 (file)
@@ -259,7 +259,7 @@ static void ogg_post_select(__a_unused struct sched *s, struct task *t)
 out:
        t->error = ret;
        if (ret < 0)
-               btr_remove_node(btrn);
+               btr_remove_node(&fn->btrn);
 }
 
 /**
index eae4167..1d9add7 100644 (file)
@@ -199,7 +199,7 @@ static void oss_post_select(__a_unused struct sched *s,
 out:
        t->error = ret;
        if (ret < 0)
-               btr_remove_node(btrn);
+               btr_remove_node(&wn->btrn);
 }
 
 __malloc static void *oss_parse_config_or_die(const char *options)
index 0f9d960..2c6fd0d 100644 (file)
@@ -309,10 +309,9 @@ static void osx_write_post_select(__a_unused struct sched *s, struct task *t)
        AudioOutputUnitStop(powd->audio_unit);
        AudioUnitUninitialize(powd->audio_unit);
        CloseComponent(powd->audio_unit);
-       btr_remove_node(powd->callback_btrn);
-       btr_free_node(powd->callback_btrn);
+       btr_remove_node(&powd->callback_btrn);
 remove_btrn:
-       btr_remove_node(btrn);
+       btr_remove_node(&wn->btrn);
        PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
 out:
        t->error = ret;
diff --git a/recv.c b/recv.c
index f14ea28..c021b17 100644 (file)
--- a/recv.c
+++ b/recv.c
@@ -112,10 +112,11 @@ int main(int argc, char *argv[])
 out:
        if (r_opened)
                r->close(&rn);
-       btr_free_node(rn.btrn);
-       btr_free_node(sot.btrn);
+       btr_remove_node(&rn.btrn);
+       btr_remove_node(&sot.btrn);
        if (rn.conf)
                r->free_config(rn.conf);
+
        if (ret < 0)
                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
        return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
index e54b265..9a827d5 100644 (file)
@@ -292,7 +292,7 @@ next_buffer:
 fail:
        if (ret < 0) {
                t->error = ret;
-               btr_remove_node(btrn);
+               btr_remove_node(&fn->btrn);
        }
 }
 
diff --git a/stdin.c b/stdin.c
index ac1581d..8dce76b 100644 (file)
--- a/stdin.c
+++ b/stdin.c
@@ -83,7 +83,7 @@ static void stdin_post_select(struct sched *s, struct task *t)
        if (ret >= 0)
                return;
 err:
-       btr_remove_node(sit->btrn);
+       btr_remove_node(&sit->btrn);
        //btr_pool_free(sit->btrp);
        t->error = ret;
 }
index abe7abc..066f1af 100644 (file)
--- a/stdout.c
+++ b/stdout.c
@@ -75,7 +75,7 @@ static void stdout_post_select(struct sched *s, struct task *t)
        }
 out:
        if (ret < 0)
-               btr_remove_node(btrn);
+               btr_remove_node(&sot->btrn);
        t->error = ret;
 }
 /**
index c1f2383..615553c 100644 (file)
@@ -83,7 +83,7 @@ static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
 out:
        if (ret >= 0)
                return;
-       btr_remove_node(btrn);
+       btr_remove_node(&rn->btrn);
        t->error = ret;
        close(rn->fd);
        rn->fd = -1;
index c507906..e8630f9 100644 (file)
@@ -117,7 +117,7 @@ err:
        if (ret == -E_WAV_SUCCESS)
                btr_splice_out_node(btrn);
        else {
-               btr_remove_node(btrn);
+               btr_remove_node(&fn->btrn);
                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
        }
 }
index e58754f..a35c174 100644 (file)
@@ -1267,7 +1267,7 @@ success:
 err:
        assert(ret < 0);
        t->error = ret;
-       btr_remove_node(btrn);
+       btr_remove_node(&fn->btrn);
 }
 
 static void wmadec_open(struct filter_node *fn)
diff --git a/write.c b/write.c
index a8e2429..de73000 100644 (file)
--- a/write.c
+++ b/write.c
@@ -143,7 +143,7 @@ pushdown:
 out:
        t->error = ret;
        if (ret < 0)
-               btr_remove_node(btrn);
+               btr_remove_node(&cwt->btrn);
 }
 
 static int loglevel;
@@ -253,12 +253,12 @@ static int setup_and_schedule(void)
                struct writer *w = writers + wn->writer_num;
 
                w->close(wn);
-               btr_free_node(wn->btrn);
+               btr_remove_node(&wn->btrn);
                w->free_config(wn->conf);
                free(wn->conf);
        }
        free(wns);
-       btr_free_node(cwt->btrn);
+       btr_remove_node(&cwt->btrn);
        return ret;
 }