Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the list of all my open file(descriptor)s and locks?

57 views
Skip to first unread message

Ismael Farfán

unread,
Sep 19, 2012, 1:34:25 PM9/19/12
to pytho...@python.org
Hello list

>From man 2 EXECVE
"By default, file descriptors remain open across an execve()"

And from man 2 FCNTL
"Record locks are... preserved across an execve(2)."

So the question:
* If I execve a python script (from C), how can I retrieve the list of
files, and optionally the list of locks, from within the execve(d)
python process so that I can use them?


Some more info:
I'm working with exotic stuff like AIX and Solaris 10 (Windows and
linux too :) and my lowest common denominator is python 2.3.

>From a standalone test within the interpreter I'd expect to get (at
least) std(in/out/err).

If more information is needed in order to help me, please let me know.

Cheers
Ismael


--
Do not let me induce you to satisfy my curiosity, from an expectation,
that I shall gratify yours. What I may judge proper to conceal, does
not concern myself alone.

Ismael Farfán

unread,
Sep 19, 2012, 4:36:02 PM9/19/12
to pytho...@python.org
2012/9/19 Ismael Farfán <sulf...@gmail.com>:
It seems like I can use os.fstat to find out if a fd exists and also
get it's type and mode (I'm getting some pipes too : )

I hope it's portable across platforms, if someone knows a better
solution please let me know.

Ian Kelly

unread,
Sep 19, 2012, 5:09:47 PM9/19/12
to Python
On Wed, Sep 19, 2012 at 11:34 AM, Ismael Farfán <sulf...@gmail.com> wrote:
> So the question:
> * If I execve a python script (from C), how can I retrieve the list of
> files, and optionally the list of locks, from within the execve(d)
> python process so that I can use them?
>
>
> Some more info:
> I'm working with exotic stuff like AIX and Solaris 10 (Windows and
> linux too :) and my lowest common denominator is python 2.3.

You could do:

os.listdir("/proc/%d/fd" % os.getpid())

This should work on Linux, AIX, and Solaris, but obviously not on Windows.

Ian Kelly

unread,
Sep 19, 2012, 5:40:11 PM9/19/12
to Python
On Wed, Sep 19, 2012 at 2:36 PM, Ismael Farfán <sulf...@gmail.com> wrote:
> It seems like I can use os.fstat to find out if a fd exists and also
> get it's type and mode (I'm getting some pipes too : )

Sure, because files and pipes both use the file descriptor
abstraction. If your process does any networking, you'll find sockets
in there as well.

Ismael Farfán

unread,
Sep 19, 2012, 5:58:56 PM9/19/12
to Ian Kelly, Python
2012/9/19 Ian Kelly <ian.g...@gmail.com>:
> --
> http://mail.python.org/mailman/listinfo/python-list

Seems like things will get interesting :D

Christian Heimes

unread,
Sep 19, 2012, 8:39:42 PM9/19/12
to pytho...@python.org
Am 19.09.2012 19:34, schrieb Ismael Farfán:
> Hello list
>
> From man 2 EXECVE
> "By default, file descriptors remain open across an execve()"
>
> And from man 2 FCNTL
> "Record locks are... preserved across an execve(2)."
>
> So the question:
> * If I execve a python script (from C), how can I retrieve the list of
> files, and optionally the list of locks, from within the execve(d)
> python process so that I can use them?

Have a look at psutil:

http://code.google.com/p/psutil/#Process_management

Christian

Chris Angelico

unread,
Sep 19, 2012, 11:11:11 PM9/19/12
to pytho...@python.org
On Thu, Sep 20, 2012 at 7:09 AM, Ian Kelly <ian.g...@gmail.com> wrote:
> You could do:
>
> os.listdir("/proc/%d/fd" % os.getpid())
>
> This should work on Linux, AIX, and Solaris, but obviously not on Windows.

I'm not sure how cross-platform it is, but at least on Linux, you can
use /proc/self as an alias for "/proc/"+os.getpid() - worth a try,
might make your code a bit easier to read.

It's documented as useful when you don't know the PID yet (eg telling
a process to read the file /proc/self/fd/0 to force it to use stdin).
So I'm confident enough to recommend testing it. :)

ChrisA

Ismael Farfán

unread,
Sep 20, 2012, 12:21:25 AM9/20/12
to Christian Heimes, pytho...@python.org
2012/9/19 Christian Heimes <chri...@python.org>:
>> So the question:
>> * If I execve a python script (from C), how can I retrieve the list of
>> files, and optionally the list of locks, from within the execve(d)
>> python process so that I can use them?
>
> Have a look at psutil:
>
> http://code.google.com/p/psutil/#Process_management
>
> Christian
>
> --
> http://mail.python.org/mailman/listinfo/python-list

Unfortunately I'm not allowed to install 3rd party SW unless it's
absolutely necessary, but thanks for sharing, I'll have a look at the
arch dependent code : )

Hans Mulder

unread,
Sep 20, 2012, 4:30:30 AM9/20/12
to
On 20/09/12 05:11:11, Chris Angelico wrote:
> On Thu, Sep 20, 2012 at 7:09 AM, Ian Kelly <ian.g...@gmail.com> wrote:
>> You could do:
>>
>> os.listdir("/proc/%d/fd" % os.getpid())
>>
>> This should work on Linux, AIX, and Solaris, but obviously not on Windows.

On MacOS X, you can use

os.listdir("/dev/fd")

This might work on other OSes.

> I'm not sure how cross-platform it is, but at least on Linux, you can
> use /proc/self as an alias for "/proc/"+os.getpid() - worth a try,
> might make your code a bit easier to read.
>
> It's documented as useful when you don't know the PID yet (eg telling
> a process to read the file /proc/self/fd/0 to force it to use stdin).

At least on Linux, Solaris and MacOS X, you shouldd use /dev/stdin,
which is a symlink to whatever the official name is.
"/dev/stdin" is more readable and more portable than the official name.

> So I'm confident enough to recommend testing it. :)

Testing such things is the best way to learn.


Hope this helps,

-- HansM

0 new messages