How can I get file names of opened by a process?

1

I want to list down all file names opened by init process. I know about /proc/PID/fd directory. But that gives file descriptors only. How can I get file names from list of file descriptors?

techfun

Posted 2014-02-12T19:09:06.030

Reputation: 327

Answers

2

Use lsof to list open files of a process, in your case "init":

$ sudo lsof -ln -p `pgrep init`
COMMAND PID     USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
init      1        0  cwd    DIR    8,1     4096      2 /
init      1        0  rtd    DIR    8,1     4096      2 /
init      1        0  txt    REG    8,1    36992 354775 /sbin/init
init      1        0  mem    REG    8,1    14768 354922 /lib/x86_64-linux-gnu/libdl-2.13.so
init      1        0  mem    REG    8,1  1599504 350331 /lib/x86_64-linux-gnu/libc-2.13.so
init      1        0  mem    REG    8,1   126232 354818 /lib/x86_64-linux-gnu/libselinux.so.1
init      1        0  mem    REG    8,1   261184 354836 /lib/x86_64-linux-gnu/libsepol.so.1
init      1        0  mem    REG    8,1   136936 354986 /lib/x86_64-linux-gnu/ld-2.13.so
init      1        0   10u  FIFO   0,14      0t0   3054 /run/initctl

ckujau

Posted 2014-02-12T19:09:06.030

Reputation: 515

I know you wanted a general answer so you added pgrep, but you do remember that init is always PID 1 right? Not sure if systemd and upstart constrain to PID 1 though. – Rich Homolka – 2014-02-12T20:12:36.673

Hehe, yes, I know. But indeed, I wanted a general answer :) systemd also has PID 1, upstart runs as /sbin/init, also with PID 1. – ckujau – 2014-02-12T20:17:36.830