linux下的/proc/pid/fdinfo和/proc/pid/fd

/proc/pid/fdinfo和/proc/pid/fd目录

/proc/pid/fd目录

fd目录包含了所有该进程使用的文件描述符,而fdinfo目录包含的是对应的fd目录中进程打开的操作权限。

root@mrzhang:/proc/1408/fd# ls
0 1 2 3 4 5 6 7
root@mrzhang:/proc/1408/fd# ls -l
total 0
lrwx------ 1 ywx ywx 64 2011-12-13 13:23 0 -> /dev/null
lrwx------ 1 ywx ywx 64 2011-12-13 13:23 1 -> /dev/null
lrwx------ 1 ywx ywx 64 2011-12-13 13:23 2 -> /dev/null
lr-x------ 1 ywx ywx 64 2011-12-13 13:23 3 -> pipe:[13347]
lrwx------ 1 ywx ywx 64 2011-12-13 13:23 4 -> /dev/null
l-wx------ 1 ywx ywx 64 2011-12-13 13:23 5 -> pipe:[13347]
lrwx------ 1 ywx ywx 64 2011-12-13 13:23 6 -> socket:[13348]
lrwx------ 1 ywx ywx 64 2011-12-13 13:23 7 -> socket:[13349]

我们可以看出fd目录中包含的是进程打开文件的链接,这里我们就可以看到我们经常提到的标准输入(0)、标准输出(1)、标准错误输出(2)。
那么fdinfo中包含的文件的的含义,我们可以从这连个方面探索。一个是内核中的 函数(/fs/proc/base.c),fdinfo目录中的文件中的内容正是由这个函数写的,而flags对应的是文件结构struct file 的f_flgas(访问权限)。另一个是我们可以通过查看fd目录中包含的符号链接文件指向文件的权限得知(fdinfo目录中文件内容)。
“flags:02″表示文件访问权限是O_RDWR(可读可写),而fd中符号链接文件指向的文件/dev/pts/5对用户的权限也是可读可写。

/proc/pid/fdinfo

在/proc/pid/fdinfo里有关于进程打开文件的一些权限和位置信息,按照manpage是这样描述的:

```c
/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 information 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.

```c
就是说pos:表明当前该进程对该文件的读写位置;flags是open文件时的权限,比如O_RDWR|O_TRUNC等等。下面举栗说明:

1)某未知进程打开一个log文件

[root@xx /proc/108490/fdinfo]#ls -l ../fd
total 0
...
l-wx------ 1 xx xx 64 Aug  9 21:39 5 -> /data1/home/xx/workspace/trunk/bin/log/lobby.log

2)查看fd==5
pos:    4114656
flags:  0100001

根据说明,pos是当前读写位置,一般写日志文件肯定都是写到文件尾的,所以目测这个pos可以代表文件的大小,验证一下

3)下面对比flag,0100001

#define O_RDONLY         00
#define O_WRONLY         01
#define O_RDWR             02
#define O_CREAT           0100    /* not fcntl */
#define O_EXCL           0200    /* not fcntl */
#define O_NOCTTY       0400    /* not fcntl */
#define O_TRUNC          01000    /* not fcntl */
#define O_APPEND      02000
#define O_NONBLOCK      04000
#define O_NDELAY    O_NONBLOCK
#define O_SYNC         010000
#define FASYNC         020000    /* fcntl, for BSD compatibility */
#define O_DIRECT     040000    /* direct disk access hint */
#define O_LARGEFILE    0100000
#define O_DIRECTORY    0200000    /* must be a directory */
#define O_NOFOLLOW    0400000 /* don't follow links */
#define O_NOATIME    01000000

比较一下,说明是open(“xxxxx” , O_LARGEFILE|O_WRONLY);这样子打开的–

比如,另外打开一个文件O_WRONLY|O_APPEND|O_SYNC|O_NONBLOCK

然后查看对应的flags:

04116001

一般在64位下会默认加上O_LARGEFILE 所以实际上是

O_LARGEFILE|O_WRONLY|O_APPEND|O_SYNC|O_NONBLOCK=116001

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值