LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-21-2007, 11:52 AM   #1
ta0kira
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.
ta0kira

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
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

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

Code:
#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)
  open_max=OPEN_MAX;
#else
  open_max=sysconf(_SC_OPEN_MAX);
#endif

  for(open_index=5;
      open_index<open_max;
      open_index++
     )
  {
    close(open_index);
  }

  return 0;

} /* main() */
Hope this helps.
 
Old 07-22-2007, 01:09 PM   #3
ta0kira
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!
ta0kira
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:10 AM.

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