file descriptors number

file descriptors number

Post by Alvise Dorig » Fri, 12 Apr 2002 22:53:33



Hi all;
I'm trying to write a procedure in C language that reads from
/proc/<PID>/fd and counts the number of
file descriptors open by the process with process ID <PID> (I'm working
under Linux 2.4.x).
I get a "permission denied" on /proc/<PID>/fd owned by other users when
I try to "opendir" that directory (as expected !).
But I also see that 'lsof' is correctly working reporting all file
descriptors for all user processes, and it is executes as "normal" user,
not as root.

Furthermore, doing a "strace /usr/sbin/lsof" I see in its output there
are so many
"permission denied" messages when it tries to open the dir
/proc/<PID>/fd (as I wanted to do with my procedure), but the fact is
that lsof CAN report every file desc. for all user.

any idea ? and some workaround to solve my problem ?

thanks a lot

        Alvise

 
 
 

file descriptors number

Post by Vic Abe » Sat, 13 Apr 2002 05:33:01



> ...

> Furthermore, doing a "strace /usr/sbin/lsof" I see in its output there
> are so many
> "permission denied" messages when it tries to open the dir
> /proc/<PID>/fd (as I wanted to do with my procedure), but the fact is
> that lsof CAN report every file desc. for all user.

I don't think that's correct.  When lsof gets an EPERM while
opening /proc/<PID>/fd, it skips the file descriptors and
reports what other information it is able to read from the
/proc directory

Typically only the memory map file, /proc/<PID?/maps, is
readable by a non-privileged lsof, so you're probably seeing
this:

COMMAND   PID   USER     FD  TYPE DEVICE    SIZE    NODE NAME
init        1   root    mem   REG    8,1   28040   56394 /sbin/init
init        1   root    mem   REG    8,1   92511   24100 /lib/ld-2.2.5.so
init        1   root    mem   REG    8,1 1170812   24117 /lib/libc-2.2.5.so
portmap   122   daemon  mem   REG    8,1   11692   56447 /sbin/portmap
...