lkml.org 
[lkml]   [2002]   [Aug]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: How can a process easily get a list of all it's open fd?
On Tue, Aug 27, 2002 at 02:26:38PM -0700, Mike Touloumtzis wrote:
> On Tue, Aug 27, 2002 at 06:08:42PM +0200, Alex Riesen wrote:
> > tricky. You can use /proc/<getpid>/fd, and close all
> > handles listed here, but this has some caveats:
> > it's _very_ slow if you have many open files.
> > it's not portable.
> > it's not safe if you have a thread/signal handler running.
> >
> > i never heard of a right way to do this.
> >
> > -alex
> >
> > int close_all_fd()
> > {
> > char fdpath[PATH_MAX];
> > DIR * dp;
> > struct dirent * de;
> > int fd;
> >
> > sprintf(fdpath, "/proc/%d/fd", getpid());
> > dp = opendir(fdpath);
>
> This can just be:
>
> dp = opendir("/proc/self/fd/");
>
> then you don't need fdpath.

Oh, indeed.

> You can use sigprocmask() if you're worried about signals coming
> in during this operation.

I, personally, strongly dislike this option.
You never know which signals are to be blocked.

Besides, it's still not portable and not safe agains threads.


-alex
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 13:28    [W:0.072 / U:0.064 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site