Skip to contentSkip to conversation

External PCM hook SO is dlclosed after calling install, making it hard to have hooks in it

DISCUSSION Applied Paul Fertser 2 participants 8 messages Jun 29, 2009 – Jun 30, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

I've tried to implement an external shared object to be able to execute arbitrary functions on PCM device opening and closing. After quite some source code reading i had an almost working but segfaulting example. This segfault is because snd_dlclose is called right after calling a hook install function and therefore i can't snd_pcm_hook_add functions from the same shared object. To me it looks like a bug but probably i just don't see how this functionality is intended to be used. Takashi, git-blame showed that it's you who stuffed this dlclose call that bothers me ;)

TIA

Background information:

We're working on audio subsystem for OpenMoko Freerunner. It's an idea of Joerg Reisenweber to have virtual alsa devices for every reasonable use-case (like dedicated device for stereout, dedicated device for recording from gsm etc), for that we need a hook on every device opening and closing mostly to set up routing inside WM8753 codec. ctl_elems is not enough because we need a more complex locking scheme (with priorities, some kind of callbacks to the apps using particular devices etc) and other flexibility.

Signature · 2 lines
Be free, use free (http://www.gnu.org/philosophy/free-sw.html) software!
mailto:[email protected]
#1 lore raw

At Tue, 30 Jun 2009 02:11:01 +0400,

Quoted 12 lines · Paul Fertser

Hi,

I've tried to implement an external shared object to be able to execute arbitrary functions on PCM device opening and closing. After quite some source code reading i had an almost working but segfaulting example. This segfault is because snd_dlclose is called right after calling a hook install function and therefore i can't snd_pcm_hook_add functions from the same shared object. To me it looks like a bug but probably i just don't see how this functionality is intended to be used. Takashi, git-blame showed that it's you who stuffed this dlclose call that bothers me ;)

Well, right now, a plugin is designed to be bound with a single PCM instance. That's why dlcolse is called in snd_pcm_close(). Other use-cases are out of design, thus we need to extend something.

Can you give your code to grasp more implementation images?

Quoted 4 lines

TIA

Background information:

Quoted 8 lines

We're working on audio subsystem for OpenMoko Freerunner. It's an idea of Joerg Reisenweber to have virtual alsa devices for every reasonable use-case (like dedicated device for stereout, dedicated device for recording from gsm etc), for that we need a hook on every device opening and closing mostly to set up routing inside WM8753 codec. ctl_elems is not enough because we need a more complex locking scheme (with priorities, some kind of callbacks to the apps using particular devices etc) and other flexibility.

To hook off snd_pcm_open and snd_pcm_close, you could use LD_PRELOAD. But, a plugin sounds more interesting.

Takashi

#2 lore raw
Quoted 12 lines · Takashi Iwai

At Tue, 30 Jun 2009 02:11:01 +0400,

Quoted 9 lines · Paul Fertser

I've tried to implement an external shared object to be able to execute arbitrary functions on PCM device opening and closing. After quite some source code reading i had an almost working but segfaulting example. This segfault is because snd_dlclose is called right after calling a hook install function and therefore i can't snd_pcm_hook_add functions from the same shared object. To me it looks like a bug but probably i just don't see how this functionality is intended to be used. Takashi, git-blame showed that it's you who stuffed this dlclose call that bothers me ;)

Quoted 2 lines

Well, right now, a plugin is designed to be bound with a single PCM instance. That's why dlcolse is called in snd_pcm_close().

It looks like we're talking about different things. You seem to be talking about external PCM plugins, i'm talking about external functions for the "hook" PCM plugin. The code i question is in pcm/pcm_hooks.c in function snd_pcm_hook_add_conf:

        if (err >= 0) {
                if (args && snd_config_get_string(args, &str) >= 0) {
                        err = snd_config_search_definition(root,
                "hook_args", str, &args);
                        if (err < 0)
                                SNDERR("unknown hook_args %s", str);
                        else
                                err = install_func(pcm, args);
                        snd_config_delete(args);
                } else
                        err = install_func(pcm, args);
                snd_dlclose(h);
        }
Quoted 1 line

Can you give your code to grasp more implementation images?

Rough sketch attached.

Quoted 11 lines
Quoted 8 lines

We're working on audio subsystem for OpenMoko Freerunner. It's an idea of Joerg Reisenweber to have virtual alsa devices for every reasonable use-case (like dedicated device for stereout, dedicated device for recording from gsm etc), for that we need a hook on every device opening and closing mostly to set up routing inside WM8753 codec. ctl_elems is not enough because we need a more complex locking scheme (with priorities, some kind of callbacks to the apps using particular devices etc) and other flexibility.

To hook off snd_pcm_open and snd_pcm_close, you could use LD_PRELOAD. But, a plugin sounds more interesting.

Much more, i'd say.

Signature · 2 lines
Be free, use free (http://www.gnu.org/philosophy/free-sw.html) software!
mailto:[email protected]

#include <alsa/asoundlib.h>
#include <alsa/conf.h>
#include <alsa/pcm.h>
#include <dlfcn.h>
#include <stdio.h>

snd_config_t *conf_copy;

static int snd_pcm_hook_testhook_hw_params(snd_pcm_hook_t *hook)

{
	const char *str;
        snd_config_t *h = snd_pcm_hook_get_private(hook);
	snd_config_t *s;
	snd_config_search(h, "hw", &s);
	snd_config_get_string(s, &str);
	fprintf(stderr, "testhook: hw_params, %s\n", str);
        return 0;
}

static int snd_pcm_hook_testhook_hw_free(snd_pcm_hook_t *hook)

{
	const char *str;
        snd_config_t *h = snd_pcm_hook_get_private(hook);
	snd_config_t *s;
	snd_config_search(h, "close", &s);
	snd_config_get_string(s, &str);
	fprintf(stderr, "testhook: hw_free, %s\n", str);
        return 0;
}

static int snd_pcm_hook_testhook_close(snd_pcm_hook_t *hook)

{
	fprintf(stderr, "testhook: close\n");
        return 0;
}

int _snd_pcm_hook_testhook_install(snd_pcm_t *pcm, snd_config_t *conf)

{
	int err;
        snd_pcm_hook_t *h_hw_params = NULL, *h_hw_free = NULL, *h_close = NULL;

	snd_config_copy(&conf_copy, conf);

// dlopen("testhook.so", RTLD_NOW);

        err = snd_pcm_hook_add(&h_hw_params, pcm, SND_PCM_HOOK_TYPE_HW_PARAMS,
                               snd_pcm_hook_testhook_hw_params, conf_copy);
        if (err < 0)
                goto _err;
        err = snd_pcm_hook_add(&h_hw_free, pcm, SND_PCM_HOOK_TYPE_HW_FREE,
                               snd_pcm_hook_testhook_hw_free, conf_copy);
        if (err < 0)
                goto _err;
        err = snd_pcm_hook_add(&h_close, pcm, SND_PCM_HOOK_TYPE_CLOSE,
                               snd_pcm_hook_testhook_close, NULL);
	return 0;
 _err:
        if (h_hw_params)
                snd_pcm_hook_remove(h_hw_params);
        if (h_hw_free)
                snd_pcm_hook_remove(h_hw_free);
        if (h_close)
                snd_pcm_hook_remove(h_close);
        return err;
}

SND_DLSYM_BUILD_VERSION(_snd_pcm_hook_testhook_install, SND_PCM_DLSYM_VERSION);

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#3 lore raw

At Tue, 30 Jun 2009 10:20:54 +0400,

Quoted 22 lines · Paul Fertser
Quoted 12 lines · Takashi Iwai

At Tue, 30 Jun 2009 02:11:01 +0400,

Quoted 9 lines · Paul Fertser

I've tried to implement an external shared object to be able to execute arbitrary functions on PCM device opening and closing. After quite some source code reading i had an almost working but segfaulting example. This segfault is because snd_dlclose is called right after calling a hook install function and therefore i can't snd_pcm_hook_add functions from the same shared object. To me it looks like a bug but probably i just don't see how this functionality is intended to be used. Takashi, git-blame showed that it's you who stuffed this dlclose call that bothers me ;)

Quoted 2 lines

Well, right now, a plugin is designed to be bound with a single PCM instance. That's why dlcolse is called in snd_pcm_close().

It looks like we're talking about different things. You seem to be talking about external PCM plugins, i'm talking about external functions for the "hook" PCM plugin. The code i question is in pcm/pcm_hooks.c in function snd_pcm_hook_add_conf:

Quoted 13 lines
        if (err >= 0) {
                if (args && snd_config_get_string(args, &str) >= 0) {
                        err = snd_config_search_definition(root,
                "hook_args", str, &args);
                        if (err < 0)
                                SNDERR("unknown hook_args %s", str);
                        else
                                err = install_func(pcm, args);
                        snd_config_delete(args);
                } else
                        err = install_func(pcm, args);
                snd_dlclose(h);
        }

Ah, I understand now. Yes, this looks like a bug. Meanwhile, it makes another bug if we just remove it. There would be no dlclose(), then. It'd need resource tracking. A lack of dlclose is no serious bug, though.

So... just removing snd_dlclose() there fixes your problem? If yes, I'll commit the fix.

thanks,

Takashi

#4 lore raw
Quoted 19 lines · Takashi Iwai
Quoted 5 lines

It looks like we're talking about different things. You seem to be talking about external PCM plugins, i'm talking about external functions for the "hook" PCM plugin. The code i question is in pcm/pcm_hooks.c in function snd_pcm_hook_add_conf:

Quoted 13 lines
        if (err >= 0) {
                if (args && snd_config_get_string(args, &str) >= 0) {
                        err = snd_config_search_definition(root,
                "hook_args", str, &args);
                        if (err < 0)
                                SNDERR("unknown hook_args %s", str);
                        else
                                err = install_func(pcm, args);
                        snd_config_delete(args);
                } else
                        err = install_func(pcm, args);
                snd_dlclose(h);
        }
Quoted 3 lines

Ah, I understand now. Yes, this looks like a bug. Meanwhile, it makes another bug if we just remove it. There would be no dlclose(), then. It'd need resource tracking.

Exactly the reason i haven't tried to make a proper patch ;)

Quoted 1 line

A lack of dlclose is no serious bug, though.

I hope you know better than me ;)

Quoted 2 lines

So... just removing snd_dlclose() there fixes your problem? If yes, I'll commit the fix.

TBH, i've not tried but i'm pretty sure it will fix the problem. I tried with dlopen workaround (to increase the refcounter) in install function and i tried with LD_PRELOAD'ing the library before starting an alsa app; both workarounds worked.

Signature · 2 lines
Be free, use free (http://www.gnu.org/philosophy/free-sw.html) software!
mailto:[email protected]
#5 lore raw

At Tue, 30 Jun 2009 10:38:04 +0400,

Quoted 31 lines · Paul Fertser
Quoted 19 lines · Takashi Iwai
Quoted 5 lines

It looks like we're talking about different things. You seem to be talking about external PCM plugins, i'm talking about external functions for the "hook" PCM plugin. The code i question is in pcm/pcm_hooks.c in function snd_pcm_hook_add_conf:

Quoted 13 lines
        if (err >= 0) {
                if (args && snd_config_get_string(args, &str) >= 0) {
                        err = snd_config_search_definition(root,
                "hook_args", str, &args);
                        if (err < 0)
                                SNDERR("unknown hook_args %s", str);
                        else
                                err = install_func(pcm, args);
                        snd_config_delete(args);
                } else
                        err = install_func(pcm, args);
                snd_dlclose(h);
        }
Quoted 3 lines

Ah, I understand now. Yes, this looks like a bug. Meanwhile, it makes another bug if we just remove it. There would be no dlclose(), then. It'd need resource tracking.

Exactly the reason i haven't tried to make a proper patch ;)

Quoted 1 line

A lack of dlclose is no serious bug, though.

I hope you know better than me ;)

Quoted 7 lines
Quoted 2 lines

So... just removing snd_dlclose() there fixes your problem? If yes, I'll commit the fix.

TBH, i've not tried but i'm pretty sure it will fix the problem. I tried with dlopen workaround (to increase the refcounter) in install function and i tried with LD_PRELOAD'ing the library before starting an alsa app; both workarounds worked.

OK, I fixed this on GIT now.

Takashi

#6 lore raw
Quoted 7 lines · Takashi Iwai
Quoted 4 lines · Paul Fertser

TBH, i've not tried but i'm pretty sure it will fix the problem. I tried with dlopen workaround (to increase the refcounter) in install function and i tried with LD_PRELOAD'ing the library before starting an alsa app; both workarounds worked.

OK, I fixed this on GIT now.

Wow! Thanks a lot for this fast and proper fix! I compile-tested it, will try running later, but it should be ok. :)

While reading the diff i saw you introduced several unused variables, patch attached.

Signature · 2 lines
Be free, use free (http://www.gnu.org/philosophy/free-sw.html) software!
mailto:[email protected]
Quoted 1 line

From 3847baf57561871f09c7be48b10f3058feb117ee Mon Sep 17 00:00:00 2001

From: Paul Fertser <[email protected]>
Date: Tue, 30 Jun 2009 15:18:36 +0400
Subject: [PATCH] pcm_hooks: cosmetic removal of unused variables

---
 src/pcm/pcm_hooks.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
src/pcm/pcm_hooks.c +0 -2
index 2fbee73..3a99d55 100644
@@ -99,7 +99,6 @@ static int snd_pcm_hooks_close(snd_pcm_t *pcm)
 		}
 	}
 	while (!list_empty(&h->dllist)) {
-		struct snd_pcm_hook_dllist *dl;
 		pos = h->dllist.next;
 		hook_remove_dlobj(list_entry(pos, struct snd_pcm_hook_dllist, list));
 	}
@@ -345,7 +344,6 @@ static int snd_pcm_hook_add_conf(snd_pcm_t *pcm, snd_config_t *root, snd_config_
 	snd_config_iterator_t i, next;
 	int (*install_func)(snd_pcm_t *pcm, snd_config_t *args) = NULL;
 	void *h = NULL;
-	struct snd_pcm_hook_dllist *dl;
 
 	if (snd_config_get_type(conf) != SND_CONFIG_TYPE_COMPOUND) {
 		SNDERR("Invalid hook definition");
Signature · 1 line
1.6.1.3

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

Applied
#7 lore raw

At Tue, 30 Jun 2009 15:24:51 +0400,

Quoted 8 lines · Paul Fertser
Quoted 6 lines · Takashi Iwai
Quoted 4 lines · Paul Fertser

TBH, i've not tried but i'm pretty sure it will fix the problem. I tried with dlopen workaround (to increase the refcounter) in install function and i tried with LD_PRELOAD'ing the library before starting an alsa app; both workarounds worked.

Quoted 7 lines
Quoted 1 line

OK, I fixed this on GIT now.

Wow! Thanks a lot for this fast and proper fix! I compile-tested it, will try running later, but it should be ok. :)

While reading the diff i saw you introduced several unused variables, patch attached.

Thanks, applied now.

Takashi