mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alex Riesen <Alexander.Riesen@synopsys.com>
To: Mark Atwood <mra@pobox.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: How can a process easily get a list of all it's open fd?
Date: Tue, 27 Aug 2002 18:08:42 +0200	[thread overview]
Message-ID: <20020827160842.GA16092@riesen-pc.gr05.synopsys.com> (raw)
In-Reply-To: <m38z2s1fkj.fsf@khem.blackfedora.com>

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);
    if ( !dp )
	return -errno;
    while ( (de = readdir(dp)) )
    {
	if ( !strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") )
	    continue;
	fd = strtol(de->d_name, 0, 10);
	if ( fd == dirfd(dp) || fd == 0 || fd == 1 || fd == 2 )
	    continue;

	if ( close(fd) < 0 )
	    fprintf(stderr, "%s: %s\n", de->d_name, strerror(errno));
    }
    closedir(dp);
    return 0;
}


On Tue, Aug 27, 2002 at 08:34:04AM -0700, Mark Atwood wrote:
> 
> I need to close all the none std[in|out|err] open fd's.
> 
> I've been told to do it like so:
> 
>   {
>     int i;
>     for (i=3; i<OPEN_MAX; i++)
>       close(i);
>   }
> 
> This is very slow, plus I have discovered that I can have open fd's with
> values greater than OPEN_MAX.
> 
> I thought about getting the max fd from rlimit, but that doesn't work
> either.  Say I have a rlimit of 1024 open fd's, and I open numbers 3
> thru 1023, then I close 3 thru 1022, then I set the rlimit down to
> 16. rlimit then returns 16, but the largest open fd is still 1023.
> 
> So that doesn't work.
> 
> And I still have the problem that looping between 3 and whatever I pick
> as the top and calling close on each in turn is very slow.
> 
> So what's the "right way" to do it?
> 
> I would *love* for there to be an ioctl or some syscall that I could
> pass a pointer to an int and a pointer to an int array, and it would
> come back telling me how many open fd's I've got, and fill in the
> array with those fd's.
> 
> -- 
> Mark Atwood   | Well done is better than well said.
> mra@pobox.com | 
> http://www.pobox.com/~mra
> -

  reply	other threads:[~2002-08-27 16:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-26 20:06 [PATCH] make raid5 checksums preempt-safe Robert Love
2002-08-26 21:09 ` Thunder from the hill
2002-08-26 21:15   ` Robert Love
2002-08-27  1:38     ` Horst von Brand
2002-08-27 15:25       ` Thunder from the hill
2002-08-27 15:34       ` How can a process easily get a list of all it's open fd? Mark Atwood
2002-08-27 16:08         ` Alex Riesen [this message]
2002-08-27 21:26           ` Mike Touloumtzis
2002-08-28  8:28             ` Alex Riesen
2002-08-27 23:21         ` DervishD
2002-08-26 22:33 ` [PATCH] make raid5 checksums preempt-safe Brian Gerst

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020827160842.GA16092@riesen-pc.gr05.synopsys.com \
    --to=alexander.riesen@synopsys.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mra@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®