Skip to contentSkip to conversation

proc: extend /proc/<pid>/fdinfo/<fd>

PATCH In review Eugene Teo 3 participants 5 messages Feb 9, 2008 – Feb 11, 2008 Proc filesystem

lore
Share & tools
lore

Eugene Teo

author
#0 lore raw

PATCH 1/1 proc: extend /proc/<pid>/fdinfo/<fd>

This patch extends /proc/<pid>/fdinfo/<fd> to report information about open files, and pathname. This information can be useful to know when debugging an application.

For each file descriptor, the information is provided in the following format:

kerndev: ~/code/kernel# cat /proc/`pgrep pickup`/fdinfo/6
mode:   0622
dev:    253,0
ino:    21463057
uid:    89
gid:    89
rdev:   0,0
pos:    0
flags:  04002 FD_CLOEXEC # if close-on-exec flag is set
path:   /var/spool/postfix/public/pickup

You can also use this to find out more information about locked open files:

kerndev: /proc# cat /proc/locks
1: POSIX ADVISORY WRITE 2663 fd:00:21398205 0 EOF

                          ^^^^       ^^^^^^^^
2: FLOCK  ADVISORY  WRITE 2654 fd:00:21398203 0 EOF
3: FLOCK  ADVISORY  WRITE 2564 fd:00:21463056 0 EOF
4: FLOCK  ADVISORY  WRITE 2266 fd:00:21398151 0 EOF
kerndev: /proc# cd 2663/fdinfo/
kerndev: /proc/2663/fdinfo# grep 21398205 * -B2 -A6
               ^^^^
3-mode:	0644
3-dev:	253,0
3:ino:	21398205
        ^^^^^^^^

3-uid: 0
3-gid: 0
3-rdev: 0,0
3-pos: 5
3-flags: 02 FD_CLOEXEC
3-path: /var/run/atd.pid

---
 fs/proc/base.c |   47 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 41 insertions(+), 6 deletions(-)
fs/proc/base.c +41 -6
index c59852b..dde617f 100644
@@ -1402,7 +1402,7 @@ out:
 	return ~0U;
 }
 
-#define PROC_FDINFO_MAX 64
+#define PROC_FDINFO_MAX 256
 
 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
 			struct vfsmount **mnt, char *info)
@@ -1411,6 +1411,7 @@ static int proc_fd_info(struct inode *inode, struct dentry **dentry,
 	struct files_struct *files = NULL;
 	struct file *file;
 	int fd = proc_fd(inode);
+	int ret = -ENOENT;
 
 	if (task) {
 		files = get_files_struct(task);
@@ -1424,24 +1425,58 @@ static int proc_fd_info(struct inode *inode, struct dentry **dentry,
 		spin_lock(&files->file_lock);
 		file = fcheck_files(files, fd);
 		if (file) {
+			ret = 0;
+
 			if (mnt)
 				*mnt = mntget(file->f_path.mnt);
 			if (dentry)
 				*dentry = dget(file->f_path.dentry);
-			if (info)
+			/* called by proc_fdinfo_read() */
+			if (info) {
+				struct fdtable *fdt = files_fdtable(files);
+				struct dentry *d = dget(file->f_path.dentry);
+				struct vfsmount *v = mntget(file->f_path.mnt);
+				char *page = (char *)__get_free_page(GFP_TEMPORARY);
+				int gcoe = FD_ISSET(fd, fdt->close_on_exec);
+				int dev_nr = d->d_inode->i_sb->s_dev;
+				int rdev_nr = d->d_inode->i_rdev;
+
+				if (!page) {
+					ret = -ENOMEM;
+					goto out;
+				}
+
 				snprintf(info, PROC_FDINFO_MAX,
+					 "mode:\t0%o\n"
+					 "dev:\t%d,%d\n"
+					 "ino:\t%ld\n"
+					 "uid:\t%d\n"
+					 "gid:\t%d\n"
+					 "rdev:\t%d,%d\n"
 					 "pos:\t%lli\n"
-					 "flags:\t0%o\n",
+					 "flags:\t0%o %s\n"
+					 "path:\t%s\n",
+					 d->d_inode->i_mode & 0777,
+					 MAJOR(dev_nr), MINOR(dev_nr),
+					 d->d_inode->i_ino,
+					 file->f_uid, file->f_gid,
+					 MAJOR(rdev_nr), MINOR(rdev_nr),
 					 (long long) file->f_pos,
-					 file->f_flags);
+					 file->f_flags, gcoe ? "FD_CLOEXEC" : "",
+					 d_path(d, v, page, PAGE_SIZE));
+				free_page((unsigned long)page);
+out:
+				mntput(v);
+				dput(d);
+			}
 			spin_unlock(&files->file_lock);
 			put_files_struct(files);
-			return 0;
+			return ret;
 		}
 		spin_unlock(&files->file_lock);
 		put_files_struct(files);
 	}
-	return -ENOENT;
+	return ret;
 }
 
 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
#1 lore raw
Quoted 10 lines · Eugene Teo

This patch extends /proc/<pid>/fdinfo/<fd> to report information about open files, and pathname. This information can be useful to know when debugging an application.

For each file descriptor, the information is provided in the following format:

kerndev: ~/code/kernel# cat /proc/`pgrep pickup`/fdinfo/6
mode:   0622
dev:    253,0
ino:    21463057

These are already available via 'stat /proc/<pid>/fd/<fd>'

Quoted 2 lines
uid:    89
gid:    89

AFAICS file->f_[ug]id are only used by the netfilter code, but I guess it wouldn't hurt to show them here. But please add fields at the end of the file, instead of the beggining.

Quoted 1 line

rdev: 0,0

This is in struct stat too.

Quoted 2 lines
pos:    0
flags:  04002 FD_CLOEXEC # if close-on-exec flag is set

FD_CLOEXEC should be on a separate line, because it's not an O_FOO flag.

Quoted 1 line

path: /var/spool/postfix/public/pickup

readlink /proc/<pid>/fd/<fd>

Thanks,
Miklos

Quoted 114 lines

You can also use this to find out more information about locked open files:

kerndev: /proc# cat /proc/locks
1: POSIX ADVISORY WRITE 2663 fd:00:21398205 0 EOF

                          ^^^^       ^^^^^^^^
2: FLOCK  ADVISORY  WRITE 2654 fd:00:21398203 0 EOF
3: FLOCK  ADVISORY  WRITE 2564 fd:00:21463056 0 EOF
4: FLOCK  ADVISORY  WRITE 2266 fd:00:21398151 0 EOF
kerndev: /proc# cd 2663/fdinfo/
kerndev: /proc/2663/fdinfo# grep 21398205 * -B2 -A6
               ^^^^
3-mode:	0644
3-dev:	253,0
3:ino:	21398205
        ^^^^^^^^

3-uid: 0
3-gid: 0
3-rdev: 0,0
3-pos: 5
3-flags: 02 FD_CLOEXEC
3-path: /var/run/atd.pid

---
 fs/proc/base.c |   47 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 41 insertions(+), 6 deletions(-)
fs/proc/base.c +41 -6
index c59852b..dde617f 100644
@@ -1402,7 +1402,7 @@ out:
 	return ~0U;
 }
 
-#define PROC_FDINFO_MAX 64
+#define PROC_FDINFO_MAX 256
 
 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
 			struct vfsmount **mnt, char *info)
@@ -1411,6 +1411,7 @@ static int proc_fd_info(struct inode *inode, struct dentry **dentry,
 	struct files_struct *files = NULL;
 	struct file *file;
 	int fd = proc_fd(inode);
+	int ret = -ENOENT;
 
 	if (task) {
 		files = get_files_struct(task);
@@ -1424,24 +1425,58 @@ static int proc_fd_info(struct inode *inode, struct dentry **dentry,
 		spin_lock(&files->file_lock);
 		file = fcheck_files(files, fd);
 		if (file) {
+			ret = 0;
+
 			if (mnt)
 				*mnt = mntget(file->f_path.mnt);
 			if (dentry)
 				*dentry = dget(file->f_path.dentry);
-			if (info)
+			/* called by proc_fdinfo_read() */
+			if (info) {
+				struct fdtable *fdt = files_fdtable(files);
+				struct dentry *d = dget(file->f_path.dentry);
+				struct vfsmount *v = mntget(file->f_path.mnt);
+				char *page = (char *)__get_free_page(GFP_TEMPORARY);
+				int gcoe = FD_ISSET(fd, fdt->close_on_exec);
+				int dev_nr = d->d_inode->i_sb->s_dev;
+				int rdev_nr = d->d_inode->i_rdev;
+
+				if (!page) {
+					ret = -ENOMEM;
+					goto out;
+				}
+
 				snprintf(info, PROC_FDINFO_MAX,
+					 "mode:\t0%o\n"
+					 "dev:\t%d,%d\n"
+					 "ino:\t%ld\n"
+					 "uid:\t%d\n"
+					 "gid:\t%d\n"
+					 "rdev:\t%d,%d\n"
 					 "pos:\t%lli\n"
-					 "flags:\t0%o\n",
+					 "flags:\t0%o %s\n"
+					 "path:\t%s\n",
+					 d->d_inode->i_mode & 0777,
+					 MAJOR(dev_nr), MINOR(dev_nr),
+					 d->d_inode->i_ino,
+					 file->f_uid, file->f_gid,
+					 MAJOR(rdev_nr), MINOR(rdev_nr),
 					 (long long) file->f_pos,
-					 file->f_flags);
+					 file->f_flags, gcoe ? "FD_CLOEXEC" : "",
+					 d_path(d, v, page, PAGE_SIZE));
+				free_page((unsigned long)page);
+out:
+				mntput(v);
+				dput(d);
+			}
 			spin_unlock(&files->file_lock);
 			put_files_struct(files);
-			return 0;
+			return ret;
 		}
 		spin_unlock(&files->file_lock);
 		put_files_struct(files);
 	}
-	return -ENOENT;
+	return ret;
 }
 
 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
author
#2 lore raw

Hi Miklos,

<quote sender="Miklos Szeredi">

Quoted 12 lines · Eugene Teo
Quoted 10 lines

This patch extends /proc/<pid>/fdinfo/<fd> to report information about open files, and pathname. This information can be useful to know when debugging an application.

For each file descriptor, the information is provided in the following format:

kerndev: ~/code/kernel# cat /proc/`pgrep pickup`/fdinfo/6
mode:   0622
dev:    253,0
ino:    21463057
Quoted 8 lines

These are already available via 'stat /proc/<pid>/fd/<fd>'

Quoted 2 lines
uid:    89
gid:    89

AFAICS file->f_[ug]id are only used by the netfilter code, but I guess it wouldn't hurt to show them here. But please add fields at the end of the file, instead of the beggining.

Ok, will do.

Quoted 11 lines
Quoted 1 line

rdev: 0,0

This is in struct stat too.

Quoted 2 lines
pos:    0
flags:  04002 FD_CLOEXEC # if close-on-exec flag is set

FD_CLOEXEC should be on a separate line, because it's not an O_FOO flag.

Quoted 1 line

path: /var/spool/postfix/public/pickup

readlink /proc/<pid>/fd/<fd>

Thanks for the review. I agree that some of the information can be omitted since it can be accessed using stat and readlink. I still think it is useful to have ino in the output though.

Here's the updated patch:

[PATCH] proc: extend /proc/<pid>/fdinfo/<fd>

This patch extends /proc/<pid>/fdinfo/<fd> to report information about open files, and pathname. This information can be useful to know when debugging an application.

For each file descriptor, the information is provided in the following format:

kerndev: ~/code/kernel$ cat /proc/`pgrep firefox-bin`/fdinfo/29
pos:    49152
flags:  02
uid:    500
gid:    500
ino:    13205793
fd_flags:       FD_CLOEXEC # if close-on-exec flag is set
---
 fs/proc/base.c |   22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
fs/proc/base.c +18 -4
index c59852b..5ea5dee 100644
@@ -1402,7 +1402,7 @@ out:
 	return ~0U;
 }
 
-#define PROC_FDINFO_MAX 64
+#define PROC_FDINFO_MAX 128
 
 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
 			struct vfsmount **mnt, char *info)
@@ -1428,12 +1428,26 @@ static int proc_fd_info(struct inode *inode, struct dentry **dentry,
 				*mnt = mntget(file->f_path.mnt);
 			if (dentry)
 				*dentry = dget(file->f_path.dentry);
-			if (info)
+			/* called by proc_fdinfo_read() */
+			if (info) {
+				struct fdtable *fdt = files_fdtable(files);
+				struct dentry *d = dget(file->f_path.dentry);
+				int gcoe = FD_ISSET(fd, fdt->close_on_exec);
+
 				snprintf(info, PROC_FDINFO_MAX,
 					 "pos:\t%lli\n"
-					 "flags:\t0%o\n",
+					 "flags:\t0%o\n"
+					 "uid:\t%d\n"
+					 "gid:\t%d\n"
+					 "ino:\t%ld\n"
+					 "fd_flags:\t%s\n",
 					 (long long) file->f_pos,
-					 file->f_flags);
+					 file->f_flags,
+					 file->f_uid, file->f_gid,
+					 d->d_inode->i_ino,
+					 gcoe ? "FD_CLOEXEC" : "0");
+				dput(d);
+			}
 			spin_unlock(&files->file_lock);
 			put_files_struct(files);
 			return 0;
#3 lore raw
In reply to Eugene Teo

Hi Eugene

In general, I think this patch isn't wrong idea.
but it shuld be brush up more, may be.

Quoted 2 lines

kerndev: ~/code/kernel# cat /proc/pgrep pickup/fdinfo/6
mode: 0622

I think this is inode attribute, but not fd attribute.

Quoted 2 lines
dev:    253,0
ino:    21463057

may be useful, agreed with you :)

Quoted 5 lines
uid:    89
gid:    89
rdev:   0,0
pos:    0
flags:  04002 FD_CLOEXEC # if close-on-exec flag is set

agreed with Miklos's opinion.

Quoted 1 line

path: /var/spool/postfix/public/pickup

may be, we shold think namespace..

Quoted 1 line

You can also use this to find out more information about locked open files:

is your requirement only fd -> ino pick up?

author
#4 lore raw

Hi Motohiro-san,

<quote sender="KOSAKI Motohiro">

Quoted 2 lines

In general, I think this patch isn't wrong idea.
but it shuld be brush up more, may be.

Thanks.

Quoted 4 lines
Quoted 2 lines

kerndev: ~/code/kernel# cat /proc/pgrep pickup/fdinfo/6
mode: 0622

I think this is inode attribute, but not fd attribute.

Yes, it is an inode attribute.

Quoted 4 lines
Quoted 2 lines
dev:    253,0
ino:    21463057

may be useful, agreed with you :)

Thanks.

Quoted 11 lines
Quoted 5 lines
uid:    89
gid:    89
rdev:   0,0
pos:    0
flags:  04002 FD_CLOEXEC # if close-on-exec flag is set

agreed with Miklos's opinion.

Quoted 1 line

path: /var/spool/postfix/public/pickup

may be, we shold think namespace..

Besides readlink, this info can be gathered by running:
ls -laF /proc/<pid>/fd/<fd>

So, I agree with Miklo's opinion that we can omit this in the output.

Quoted 3 lines
Quoted 1 line

You can also use this to find out more information about locked open files:

is your requirement only fd -> ino pick up?

Not only this, but it is useful to know quickly if the open file descriptor has a close-on-exec flag set.

Thanks,
Eugene