Skip to contentSkip to conversation

automount does not close file descriptors at start

DISCUSSION Steinar H. Gunderson 4 participants 8 messages Feb 16, 2005 – Feb 21, 2005 Kernel automounter

lore
Share & tools
lore
#0 lore raw

Hi,

My latest autofs package for Debian (4.1.3+4.1.4beta2-1) had problems installing, freezing after install; I've worked around this now (4.1.3+4.1.4beta2-2 is on its way up to the archive), but I've been told this is really a bug in automount. What I've been told is along the lines of:

"Any daemon going into the background should close all file descriptors from zero up to MAXFDS minus the ones it actually wants to have open."

In other words, one should probably have a for loop of sorts in become_daemon().

/* Steinar */

Signature · 1 line
Homepage: http://www.sesse.net/
#1 lore raw
Quoted 9 lines · Steinar H. Gunderson

Hi,

My latest autofs package for Debian (4.1.3+4.1.4beta2-1) had problems installing, freezing after install; I've worked around this now (4.1.3+4.1.4beta2-2 is on its way up to the archive), but I've been told this is really a bug in automount. What I've been told is along the lines of:

"Any daemon going into the background should close all file descriptors from zero up to MAXFDS minus the ones it actually wants to have open."

And you would like to substantiate that, right!

Ian

#2 lore raw
Quoted 10 lines · On Wed, Feb 16, 2005 at 11:04:07PM +0800, [email protected] wrote:
Quoted 8 lines · Steinar H. Gunderson

Hi,

My latest autofs package for Debian (4.1.3+4.1.4beta2-1) had problems installing, freezing after install; I've worked around this now (4.1.3+4.1.4beta2-2 is on its way up to the archive), but I've been told this
is really a bug in automount. What I've been told is along the lines of:

Quoted 6 lines
Quoted 3 lines

"Any daemon going into the background should close all file descriptors from
zero up to MAXFDS minus the ones it actually wants to have open."

And you would like to substantiate that, right!

Err, maybe closing stdin,stdout and stderr?

Signature · 1 line
Francesco P. Lovergine
#3 lore raw
Quoted 2 lines · Francesco P. Lovergine
Quoted 1 line

And you would like to substantiate that, right!

Err, maybe closing stdin,stdout and stderr?

No. Everything up to MAXFDS.

/* Steinar */

Signature · 1 line
Homepage: http://www.sesse.net/
maintainer
#4 lore raw
In reply to Francesco P. Lovergine
Quoted 11 lines · Francesco P. Lovergine
Quoted 10 lines · On Wed, Feb 16, 2005 at 11:04:07PM +0800, [email protected] wrote:
Quoted 8 lines · Steinar H. Gunderson

Hi,

My latest autofs package for Debian (4.1.3+4.1.4beta2-1) had problems installing, freezing after install; I've worked around this now (4.1.3+4.1.4beta2-2 is on its way up to the archive), but I've been told this
is really a bug in automount. What I've been told is along the lines of:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

"Any daemon going into the background should close all file descriptors from
zero up to MAXFDS minus the ones it actually wants to have open."

And you would like to substantiate that, right!

Err, maybe closing stdin,stdout and stderr?

MAXFDS is much larger that 2.

maintainer
#5 lore raw
In reply to Steinar H. Gunderson
Quoted 7 lines · Steinar H. Gunderson

Hi,

My latest autofs package for Debian (4.1.3+4.1.4beta2-1) had problems installing, freezing after install; I've worked around this now (4.1.3+4.1.4beta2-2 is on its way up to the archive), but I've been told this is really a bug in automount. What I've been told is along the lines of:

Quoted 5 lines

"Any daemon going into the background should close all file descriptors from zero up to MAXFDS minus the ones it actually wants to have open."

In other words, one should probably have a for loop of sorts in become_daemon().

Steinar, I'm still wondering where this came from?

This is the first time I've heard this and the first time I wrote a Unix daemon was fifteen years ago.

As far as I'm concerned redirecting stdin, stdout and stderr to the null device, then closing it and setting the process to a be the group leader (as autofs does) should be all that's needed to daemonize a process.

So are we saying that we don't trust the kernel to reliably duplicate the state of file handles when we fork?

If that's the case then the kernel is badly broken and needs fixing, not autofs!

Ian

#6 lore raw
Quoted 9 lines · Ian Kent

This is the first time I've heard this and the first time I wrote a Unix daemon was fifteen years ago.

As far as I'm concerned redirecting stdin, stdout and stderr to the null device, then closing it and setting the process to a be the group leader (as autofs does) should be all that's needed to daemonize a process.

So are we saying that we don't trust the kernel to reliably duplicate the state of file handles when we fork?

No, you have it 180 degrees off. ;)

We *do* trust the kernel to reliably duplicate the state of file handles. So if we're about to do the whole double-fork thing and all that, we want to loop around and close all the file descriptors we don't want leaking to the double-forked daemon. Yes, we do something reasonable with fd 0,1,2 - but we probably also want to do something with that unclosed fd 3 that's still open on /etc/mydaemon.cf, and any other file descriptors we've left dangling in the breeze after initialization.

And yes, this sort of error happens in Real Live - I need to go and figure out why the /sbin/lvm.static on my initrd is throwing 'File descriptor 3 left open' messages...

#7 lore raw
Quoted 5 lines · On Mon, 21 Feb 2005 [email protected] wrote:
Quoted 3 lines · Ian Kent

This is the first time I've heard this and the first time I wrote a Unix daemon was fifteen years ago.

Quoted 8 lines
Quoted 6 lines

As far as I'm concerned redirecting stdin, stdout and stderr to the null device, then closing it and setting the process to a be the group leader (as autofs does) should be all that's needed to daemonize a process.

So are we saying that we don't trust the kernel to reliably duplicate the state of file handles when we fork?

No, you have it 180 degrees off. ;)

Perhaps the sentence above should start "So you are saying that ...".

I'm arguing that I should'nt have to perform a loop closing file descriptors I haven't opened.

Quoted 2 lines

We *do* trust the kernel to reliably duplicate the state of file handles.

This confirms above.

Quoted 6 lines

So if we're about to do the whole double-fork thing and all that, we want to loop around and close all the file descriptors we don't want leaking to the double-forked daemon. Yes, we do something reasonable with fd 0,1,2 - but we probably also want to do something with that unclosed fd 3 that's still open on /etc/mydaemon.cf, and any other file descriptors we've left dangling in the breeze after initialization.

Now this is a good point.

I don`t do a double fork in autofs (inherited code), although, long ago I did.

Do we really need to do a double fork to disassociate from the controlling terminal or is a single fork and close etc. enough?

I notice that "setsid()" should do this but I've not seen it bofore. Is that sufficient to do the job instead of the double fork?

And it looks like the automount process I just started still has a tty, grumble ....

Ian