Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-21-2007, 11:52 AM   #1
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Question POSIX-portable (i.e. glibc) way to list all open file descriptors?

I would like to obtain a list of all open file descriptors for a program. This is to mitigate a security issue with an exec program having access to all of its parent's file descriptors; they need to be closed before the exec call except for 0-4.

My first thought is to list all character devices in /proc/self/fd and close all but those I need. Is there a libc way to do this so that I can use this application on BSD, etc.? Thanks.

edit: close-on-exec should work I think...

Last edited by ta0kira; 07-21-2007 at 11:54 AM.
Old 07-22-2007, 08:28 AM   #2
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
This takes a very short time to run.

#define _POSIX_SOURCE 1

#include <stdio.h>
#include <limits.h>
#include <unistd.h>

int main(void)
  long open_index;
  long open_max;

#if defined(OPEN_MAX)


  return 0;

} /* main() */
Hope this helps.
Old 07-22-2007, 01:09 PM   #3
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Yes, that indeed works. I was skeptical at first that I might be able to dup2 past OPEN_MAX, but it doesn't let me. I suppose I could create an actual list using fstat using the same method. Thanks!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Increasing File Descriptors Geoffrey_Firmin Slackware 5 04-21-2008 09:48 AM
How to build a C++ portable network application on Win32 and POSIX systems? Bassam Programming 1 05-17-2007 03:43 PM
How to find out the number of open file descriptors? skie_knite007 Programming 2 12-11-2005 10:23 PM
file descriptors node047 Linux - Newbie 1 03-29-2005 05:47 AM
unix file descriptors versus c FILE pointers nodger Programming 9 11-25-2004 07:02 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:54 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration