用于获取Linux中另一个进程打开的所有fd的开放标志的系统调用是什么?

时间:2014-11-18 05:10:53

标签: linux-kernel

进程A打开多个fds。现在,进程B想知道进程A打开的所有fds的打开标志?进程B应该使用哪些系统调用来在Linux环境中获取此信息?

1 个答案:

答案 0 :(得分:1)

请参阅man proc,如果您知道某个流程的pid,则可以从/proc/<pid>/fdinfo

获取此信息
  "/proc/[pid]/fdinfo/ (since kernel 2.6.22)
              This  is a subdirectory containing one entry for each file which the process has open, named by its file descriptor.  The contents of each file can be read to obtain informa‐
              tion about the corresponding file descriptor, for example:

                  $ cat /proc/12015/fdinfo/4
                  pos:    1000
                  flags:  01002002

              The pos field is a decimal number showing the current file offset.  The flags field is an octal number that displays the file access mode and file status flags (see open(2)).

          The files in this directory are readable only by the owner of the process.
   "

接下来可以从lsof命令获取此信息: $ lsof +fg

COMMAND    PID  TID            USER   FD      TYPE               FILE-FLAG             DEVICE  SIZE/OFF     NODE NAME

init      1816               test   13w      REG W,AP,ND,NFLK,LG,0x80000                8,3      1438 10354712 /home/test/.cache/upstart/unity-panel-service.log
init      1816               test   14w      REG W,AP,ND,NFLK,LG,0x80000                8,3      2004 10360543 /home/test/.cache/upstart/indicator-messages.log.1 (deleted)
init      1816               test   15u      CHR        RW,ND,LG,0x80000                5,2       0t0     9237 /dev/ptmx
init      1816               test   16w      REG W,AP,ND,NFLK,LG,0x80000                8,3       269 10360135 /home/test/.cache/upstart/indicator-printers.log.1 (deleted)
init      1816               test   17u      CHR        RW,ND,LG,0x80000                5,2       0t0     9237 /dev/ptmx
init      1816               test   18w      REG W,AP,ND,NFLK,LG,0x80000                8,3      4563 10354713 /home/test/.cache/upstart/dbus.log
init      1816               test   19w      REG W,AP,ND,NFLK,LG,0x80000                8,3       783 10355626 /home/test/.cache/upstart/at-spi2-registryd.log.1 (deleted)
init      1816               test   20u     unix           RW,ND,0x80000 0x0000000000000000       0t0    16408 @/com/ubuntu/upstart-session/1000/1816
init      1816               test   21u      CHR        RW,ND,LG,0x80000                5,2       0t0     9237 /dev/ptmx
init      1816               test   22u     unix           RW,ND,0x80000 0x0000000000000000       0t0    15455 @/com/ubuntu/upstart-session/1000/1816
init      1816               test   23w      REG W,AP,ND,NFLK,LG,0x80000                8,3      8715 10354691 /home/test/.cache/upstart/gnome-session-Unity.log