[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [ot] Linux stdio question, howto find fopened files



On Thu, Mar 20, 2003 at 09:07:39PM -0800, Joey Hess wrote:
> Walter Tautz wrote:
> > please retain the CC to rbutterworth
> > 
> > 
> > Subject: Linux stdio question.
> > 
> > On non-linux unix systems, one can reference __iob[]
> > to find all currently fopen()ed files
> > (e.g. when forking a new process one would generally
> >  want to flush their buffers first, or perhaps close most of them).
> > 
> > Linux's stdio.h doesn't provide such an array of open FILE pointers,
> > or at least if it does I can't find it. 
> > 
> > Any idea what they call it,
> > or how one can find all currently open FILEs?
> > 
> > Perhaps there is a better way?
> > 
> > A general guide to porting underlinux /debian would be appreciated.
> 
> I've never run into __iob, but it looks quite nonportable. A similarly
> nonportable way in linux is to examine /proc/self/fd/.
> 
> I wonder if whatever unixes implement __iob have special kernel support
> for it, or does libc just fill it in? I can't see how it could always
> get at the info without kernel support. Just a point of personal interest..

Well, __iob is reasonably portable because it looks like it's a standard
part of a System V libc. In theory, I think glibc is supposed to support
the System V ABI, but it doesn't seem to have an __iob[]. I don't think
__iob is specified in any other standard, and different versions of the
System V ABI standard don't even define it in quite the same way.

Even if it did have an __iob[] you probably shouldn't be relying on
somebody else to keep track of the files you have open. You're the only
one who knows (or at least you're supposed to know) why you have files
open, so you should usually be trying to make an informed decision about
what to do with them.

Thinking ahead about things like that is a good habit, I've had no end
of headaches from listening to programmers going on about how the
compiler/runtime/server/whatever "should do that for me" when you tell
them their code is crashing because they didn't think about the side
effects of what they were doing.

-- 
Michael Heironimus



Reply to: