Re: _cleanup() vs Linux fcloseall()



On Thu, Mar 30, 2006 at 07:16:19PM +1100, Peter Jeremy wrote:
On Thu, 2006-Mar-30 00:41:00 +0200, Maxime Henrion wrote:
John Baldwin wrote:
I would vote for fcloseall() personally.

Seconded.

I'd prefer to see a function that closed all file descriptors greater
than a specified limit (which makes it easy for a process to preserve
stdin/out/err but close everything else). Two obvious places for this
would be:
- At the beginning of a setuid program (making sure that the user hasn't
passed in any unexpected open FDs).
- Before an exec() to prevent fd's leaking into a child.

Does fcloseall() really closes all the open file descriptors? The name
suggests that it will close all the FILE * streams instead, which is not
quite the same. The manpage I found on the net seems to confirm this
indirectly by saying that fflush() is used prior to closing the streams.

Something that just closed all stdio FILE streams would be far less
useful (close to totally useless IMHO) than something that closed file
descriptors.

FWIW, NetBSD has something that close all the file descriptors, and it's
done with fcntl(fd, F_CLOSEM) (using fcntl() on one file descriptor to
close all of them looks a bit weird to me, but well...).

If it close any fd's numerically greater than the passed fd, that would
make a lot of sense.

According to fcntl(2) manpage from the netbsd, this is exactly the
functionality of F_CLOSEM. FreeBSD already has a way to close all
file descriptors: rfork(RFCFDG).

Attachment: pgpUvk9wgTx3Y.pgp
Description: PGP signature



Relevant Pages

  • Re: _cleanup() vs Linux fcloseall()
    ... I'd prefer to see a function that closed all file descriptors greater ... than a specified limit (which makes it easy for a process to preserve ... stdin/out/err but close everything else). ... indirectly by saying that fflushis used prior to closing the streams. ...
    (freebsd-current)
  • Re: how truncate a file in C?
    ... > FC> fflush() guarantees nothing here. ... > fflushshall cause any unwritten data for that stream to be written ... for defined interaction between stdio streams and file descriptors, ...
    (comp.unix.programmer)
  • Re: Passing file descriptor to another process
    ... How does one pass file descriptors AFTER the process has started? ... Using system V streams, or unix domain sockets. ... Passing open network ...
    (comp.unix.programmer)
  • Re: File Descriptors With High Values
    ... Jeff wrote: ... > file descriptors, then yes, every socket gets closed. ... Usually file descriptors start at the bottom ... However, if you're talking about FILE * streams, then on many systems, ...
    (comp.unix.programmer)
  • Re: access() is a security hole?
    ... > Accessis a potential security hole and should never be used. ... > This seems to have been part of the manpage forever, or so to speak, ... original file with another file which that user does not have ... File descriptors don't suffer from this binding problem, ...
    (FreeBSD-Security)