Skip to contentSkip to conversation

alsa-lib leaves sound device open for child processes

BUG Chris Rankin 5 participants 16 messages Feb 5, 2003 – Feb 12, 2003 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 8 lines · --- Sebastian Kapfer

May I ask what the child process is supposed to to with a raw ALSA file
descriptor? AFAICT, developers are supposed to use the alsa-lib API, and
not write to FD's. So the underlying file descriptor looks to me like a
mere implementation detail which 99% of all users shouldn't need to care about.

I consider this situation analogous to the relationship between file descriptors and FILE* pointers. By your argument, libc should probably set the FD_CLOEXEC flag here, too. But it doesn't.

Chris

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#2 lore raw
Quoted 10 lines · On Wed, 5 Feb 2003 22:39:22 +0000 (GMT) Chris Rankin <[email protected]> wrote:
Quoted 8 lines · --- Sebastian Kapfer

May I ask what the child process is supposed to to with a raw ALSA file
descriptor? AFAICT, developers are supposed to use the alsa-lib API, and
not write to FD's. So the underlying file descriptor looks to me like a
mere implementation detail which 99% of all users shouldn't need to care about.

Quoted 4 lines

I consider this situation analogous to the relationship between file descriptors and FILE* pointers. By your argument, libc should probably set the FD_CLOEXEC flag here, too. But it doesn't.

Can you explain _why_ this is the case? I can find a use for stdio FD's in the new process either. The exec'd process can't even know which FD corresponds to which file.

Signature · 8 lines
Best Regards,   |   Hi! I'm a .signature virus. Copy me into
 Sebastian      |   your ~/.signature to help me spread!


-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
#3 lore raw
Quoted 3 lines · --- Sebastian Kapfer

I can find a use for stdio FD's
in the new process either. The exec'd process can't even know which FD corresponds to which file.

I'm assuming from the context of your message that you meant "CAN'T find a use ... either". But the whole point of a general purpose API (which is what libc is and presumably is what ALSA aspires to have) is to support *ALL* possible uses, whether you can imagine them or not.

The API does not *need* to set the FD_CLOEXEC flag, and so it should not. It is the height of arrogance to assume that something has no use simply because you can't imagine using it.

Chris

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#4 lore raw
Quoted 4 lines

The API does not *need* to set the FD_CLOEXEC flag, and so it should not. It is the height of arrogance to assume that something has no use simply because you can't imagine using it.

there's a little detail you're forgetting. unless the hw supports multi-open and the current number of subunits is below the limit of the number of opens, then having the descriptor will cause all other attempts to access the device to block (unless they explicitly request non-blocking open, which means they then have to unset that flag for normal application operation). this means that even if the parent has closed the ALSA device, the child still (unwittingly) has it open. its easy to imagine situations where the user will be completely puzzled to find that another program cannot start up because they believe they exited from the previous audio program. they don't understand that the child process it started (say, a bug reporting system) still has the device open.

this is quite different from how file descriptors that refer to disk files, ttys and most other devices operate.

of course, its questionable whether the block-on-open approach is correct, but so far discussions on this list have supported it.

--p

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#5 lore raw
Quoted 8 lines · --- Paul Davis

there's a little detail you're forgetting. unless the hw supports multi-open and the current number of subunits is below the limit of the number of opens, then having the descriptor will cause all other attempts to access the device to block (unless they explicitly request non-blocking open, which means they then have to unset that flag for normal application operation

I hadn't forgotten. The situation to which you are referring is called "a bug in the *application*". Determining the destinies of resources, delivery of signals etc when spawning child processes is the application-programmer's responsibility.

And yes, I have tracked down bugs like this before. And I *did* fix the application.

Chris

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#6 lore raw
Quoted 11 lines
Quoted 8 lines

there's a little detail you're forgetting. unless the hw supports multi-open and the current number of subunits is below the limit of the number of opens, then having the descriptor will cause all other attempts to access the device to block (unless they explicitly request non-blocking open, which means they then have to unset that flag for normal application operation

I hadn't forgotten. The situation to which you are referring is called "a bug in the *application*".

not clearly so. there are very few resources accessed via a file descriptor that require this being done.

the fact that an application has to understand this only adds to the problems with the block-on-open design.

--p

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#7 lore raw
In reply to Chris Rankin

At Fri, 7 Feb 2003 13:58:48 +0000 (GMT),

Quoted 11 lines · Chris Rankin
Quoted 8 lines · --- Paul Davis

there's a little detail you're forgetting. unless the hw supports multi-open and the current number of subunits is below the limit of the number of opens, then having the descriptor will cause all other attempts to access the device to block (unless they explicitly request non-blocking open, which means they then have to unset that flag for normal application operation

Quoted 8 lines

I hadn't forgotten. The situation to which you are referring is called "a bug in the *application*". Determining the destinies of resources, delivery of signals etc when spawning child processes is the application-programmer's responsibility.

And yes, I have tracked down bugs like this before. And I *did* fix the application.

i don't think it's a bug of the alsa-lib, too. it's a bug of mplayer. mplayer should be fixed. period.

but, the problem is that this kind of bugs can be rarely found (nor appear). on the contrary, if FD_CLOEXEC is set, you'll be able to notice what's wrong. that's what i mentioned "safer".

Takashi

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#8 lore raw
Quoted 3 lines · --- Takashi Iwai

i don't think it's a bug of the alsa-lib, too. it's a bug of mplayer. mplayer should be fixed. period.

Hooray! ;-)

Quoted 2 lines

but, the problem is that this kind of bugs can be rarely found (nor appear).

I found lsof to be a most useful tool.

Quoted 2 lines

on the contrary, if
FD_CLOEXEC is set, you'll be able to notice what's

On the contrary, if FD_CLOEXEC is set then these kind of bugs will be harder to spot because the library will bury the bodies. But the application bugs will still be there.

Chris

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#9 lore raw
In reply to Chris Rankin
Quoted 7 lines · On Thu, 6 Feb 2003 23:10:34 +0000 (GMT) Chris Rankin <[email protected]> wrote:
Quoted 3 lines · --- Sebastian Kapfer

I can find a use for stdio FD's
in the new process either. The exec'd process can't even know which FD corresponds to which file.

I'm assuming from the context of your message that you meant "CAN'T find a use ... either".

You're right, sorry :)

Quoted 4 lines

But the whole point of a general purpose API (which is what libc is and presumably is what ALSA aspires to have) is to support *ALL* possible uses, whether you can imagine them or not.

Setting FD_CLOEXEC automatically doesn't make the library less powerful. You can still unset the flag yourself __if you really need the feature__ of inherited ALSA FD's. But wouldn't FD_CLOEXEC be the "reasonable default" setting, which is correct for 99% of all applications?

Quoted 2 lines

It is the height of arrogance to assume that something has no use simply because you can't imagine using it.

I'm not implying that this feature should be banned... in fact it can't be banned. As long as the programmer can retrieve the underlying FD number from the ALSA library, he can always (un)set FD_CLOEXEC. My point is that the current default behaviour is (at least IMHO) unfortunate.

Signature · 8 lines
Best Regards,   |   Hi! I'm a .signature virus. Copy me into
 Sebastian      |   your ~/.signature to help me spread!


-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
#10 lore raw
Quoted 8 lines · --- Sebastian Kapfer
Quoted 1 line

Setting FD_CLOEXEC automatically doesn't make the

library less powerful.
You can still unset the flag yourself __if you really need the feature__
of inherited ALSA FD's. But wouldn't FD_CLOEXEC be the "reasonable
default" setting, which is correct for 99% of all applications?

Most of those "99% of all applications" don't spawn child processes at all and so don't care one way or another. (And did you know that 98.68% of all statistics are invented ;-)?)

All of the remaining applications should be considering what will happen to their resources on exec(), and I see no reason why careless programmers should have a few of their bugs swept under the rug for them.

Chris

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#11 lore raw
Quoted 15 lines · On Fri, 7 Feb 2003, [iso-8859-1] Chris Rankin wrote:
Quoted 8 lines · --- Sebastian Kapfer
Quoted 1 line

Setting FD_CLOEXEC automatically doesn't make the

library less powerful.
You can still unset the flag yourself __if you really need the feature__
of inherited ALSA FD's. But wouldn't FD_CLOEXEC be the "reasonable
default" setting, which is correct for 99% of all applications?

Most of those "99% of all applications" don't spawn child processes at all and so don't care one way or another. (And did you know that 98.68% of all statistics are invented ;-)?)

Quoted 5 lines

All of the remaining applications should be considering what will happen to their resources on exec(), and I see no reason why careless programmers should have a few of their bugs swept under the rug for them.

I fully agree here.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#12 lore raw
Quoted 7 lines · On Sat, 8 Feb 2003 12:50:01 +0100 (CET) Jaroslav Kysela <[email protected]> wrote:
Quoted 5 lines

All of the remaining applications should be considering what will happen to their resources on exec(), and I see no reason why careless programmers should have a few of their bugs swept under the rug for them.

I fully agree here.

Well, obviously we're getting nowhere by continuing this thread. I'll try to get a fix for this bug into mplayer CVS. Thanks anyway for your opinions.

Signature · 8 lines
Best Regards,   |   Hi! I'm a .signature virus. Copy me into
 Sebastian      |   your ~/.signature to help me spread!


-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
#13 lore raw
In reply to Jaroslav Kysela
Quoted 7 lines · On Sat, 8 Feb 2003 12:50:01 +0100 (CET) Jaroslav Kysela <[email protected]> wrote:
Quoted 5 lines

All of the remaining applications should be considering what will happen to their resources on exec(), and I see no reason why careless programmers should have a few of their bugs swept under the rug for them.

I fully agree here.

Can anyone comment on exactly how an alsa-lib client app should make sure that exec'd processes don't inherit file handles? I can't find a way to know the FD number which alsa-lib uses. snd_pcm_file_descriptor is gone. What replaced it?

Signature · 8 lines
Best Regards,   |   Hi! I'm a .signature virus. Copy me into
 Sebastian      |   your ~/.signature to help me spread!


-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
#14 lore raw
Quoted 9 lines · Sebastian Kapfer
Quoted 6 lines · On Sat, 8 Feb 2003 12:50:01 +0100 (CET) Jaroslav Kysela <[email protected]> wrote:
Quoted 5 lines

All of the remaining applications should be considering what will happen to their resources on exec(), and I see no reason why careless programmers should have a few of their bugs swept under the rug for them.

Quoted 6 lines
Quoted 1 line

I fully agree here.

Can anyone comment on exactly how an alsa-lib client app should make sure that exec'd processes don't inherit file handles? I can't find a way to know the FD number which alsa-lib uses. snd_pcm_file_descriptor is gone. What replaced it?

The applications should use snd_pcm_close() function (or related one snd_*_close()). Note that more complex plugins can use more file descriptors so the application is not able to enumerate them.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#15 lore raw

At Sat, 8 Feb 2003 20:53:14 +0100 (CET),

Quoted 14 lines · Jaroslav
Quoted 11 lines · Sebastian Kapfer
Quoted 7 lines · On Sat, 8 Feb 2003 12:50:01 +0100 (CET) Jaroslav Kysela <[email protected]> wrote:
Quoted 5 lines

All of the remaining applications should be considering what will happen to their resources on exec(), and I see no reason why careless programmers should have a few of their bugs swept under the rug for them.

I fully agree here.

Quoted 8 lines
Quoted 4 lines

Can anyone comment on exactly how an alsa-lib client app should make sure that exec'd processes don't inherit file handles? I can't find a way to know the FD number which alsa-lib uses. snd_pcm_file_descriptor is gone. What replaced it?

The applications should use snd_pcm_close() function (or related one snd_*_close()). Note that more complex plugins can use more file descriptors so the application is not able to enumerate them.

maybe it's just whipping a dead horse, but let me take this issue back again.

there might be some situations in which you cannot call *_close() functions. for example, if you are sharing the handle with several threads and want to start an external program from one of the threads.

in such a case, the only correct method is to call fcntl(CLOEXEC).

and the problem is exactly what you wrote above. it's difficult to get fds if the plugin becomes complex.

hence, i propose to create a new function as analogy to snd_pcm_nonblock():

	int snd_pcm_set_close_at_exec(snd_pcm_t *pcm, int close);

applications need to call only this for every type of pcm.

comments please?

Takashi

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com

#16 lore raw
Quoted 29 lines · Takashi Iwai

At Sat, 8 Feb 2003 20:53:14 +0100 (CET),

Quoted 14 lines · Jaroslav
Quoted 11 lines · Sebastian Kapfer
Quoted 7 lines · On Sat, 8 Feb 2003 12:50:01 +0100 (CET) Jaroslav Kysela <[email protected]> wrote:
Quoted 5 lines

All of the remaining applications should be considering what will happen to their resources on exec(), and I see no reason why careless programmers should have a few of their bugs swept under the rug for them.

I fully agree here.

Quoted 8 lines
Quoted 4 lines

Can anyone comment on exactly how an alsa-lib client app should make sure that exec'd processes don't inherit file handles? I can't find a way to know the FD number which alsa-lib uses. snd_pcm_file_descriptor is gone. What replaced it?

The applications should use snd_pcm_close() function (or related one snd_*_close()). Note that more complex plugins can use more file descriptors so the application is not able to enumerate them.

maybe it's just whipping a dead horse, but let me take this issue back again.

Quoted 6 lines

there might be some situations in which you cannot call *_close() functions. for example, if you are sharing the handle with several threads and want to start an external program from one of the threads.

in such a case, the only correct method is to call fcntl(CLOEXEC).

Not really:

	int i = sysconf(_SC_OPEN_MAX);
	while (--i >= 3) {
		if (i != do_not_close_fd[])
			close(i);
	}
	exec(...);

But it's true that the application cannot filter ALSA's file descriptors in this case (to pass only specific interfaces to executed process). We have similar troubles with shm.

I'd propose to have a function 'snd_pcm_free_system_resources()' which will close all open file descriptors, detaches all shm handles and cleans all resources necessary before exec() or after fork():

int snd_pcm_free_system_resources(snd_pcm_t *pcm, int memory);

When memory != 0, it will free all memory resources.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com