Skip to content

Commit 2793274

Browse files
Miklos SzerediLinus Torvalds
authored andcommitted
add file position info to proc
Add support for finding out the current file position, open flags and possibly other info in the future. These new entries are added: /proc/PID/fdinfo/FD /proc/PID/task/TID/fdinfo/FD For each fd the information is provided in the following format: pos: 1234 flags: 0100002 [bunk@stusta.de: make struct proc_fdinfo_file_operations static] Signed-off-by: Miklos Szeredi <mszeredi@suse.cz> Cc: Alexey Dobriyan <adobriyan@sw.ru> Signed-off-by: Adrian Bunk <bunk@stusta.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c5141e6 commit 2793274

1 file changed

Lines changed: 114 additions & 18 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 114 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,10 @@ static unsigned name_to_int(struct dentry *dentry)
12381238
return ~0U;
12391239
}
12401240

1241-
static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1241+
#define PROC_FDINFO_MAX 64
1242+
1243+
static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1244+
struct vfsmount **mnt, char *info)
12421245
{
12431246
struct task_struct *task = get_proc_task(inode);
12441247
struct files_struct *files = NULL;
@@ -1257,8 +1260,16 @@ static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsm
12571260
spin_lock(&files->file_lock);
12581261
file = fcheck_files(files, fd);
12591262
if (file) {
1260-
*mnt = mntget(file->f_path.mnt);
1261-
*dentry = dget(file->f_path.dentry);
1263+
if (mnt)
1264+
*mnt = mntget(file->f_path.mnt);
1265+
if (dentry)
1266+
*dentry = dget(file->f_path.dentry);
1267+
if (info)
1268+
snprintf(info, PROC_FDINFO_MAX,
1269+
"pos:\t%lli\n"
1270+
"flags:\t0%o\n",
1271+
(long long) file->f_pos,
1272+
file->f_flags);
12621273
spin_unlock(&files->file_lock);
12631274
put_files_struct(files);
12641275
return 0;
@@ -1269,6 +1280,12 @@ static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsm
12691280
return -ENOENT;
12701281
}
12711282

1283+
static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1284+
struct vfsmount **mnt)
1285+
{
1286+
return proc_fd_info(inode, dentry, mnt, NULL);
1287+
}
1288+
12721289
static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
12731290
{
12741291
struct inode *inode = dentry->d_inode;
@@ -1364,7 +1381,9 @@ static struct dentry *proc_fd_instantiate(struct inode *dir,
13641381
goto out;
13651382
}
13661383

1367-
static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1384+
static struct dentry *proc_lookupfd_common(struct inode *dir,
1385+
struct dentry *dentry,
1386+
instantiate_t instantiate)
13681387
{
13691388
struct task_struct *task = get_proc_task(dir);
13701389
unsigned fd = name_to_int(dentry);
@@ -1375,23 +1394,15 @@ static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry,
13751394
if (fd == ~0U)
13761395
goto out;
13771396

1378-
result = proc_fd_instantiate(dir, dentry, task, &fd);
1397+
result = instantiate(dir, dentry, task, &fd);
13791398
out:
13801399
put_task_struct(task);
13811400
out_no_task:
13821401
return result;
13831402
}
13841403

1385-
static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1386-
struct task_struct *task, int fd)
1387-
{
1388-
char name[PROC_NUMBUF];
1389-
int len = snprintf(name, sizeof(name), "%d", fd);
1390-
return proc_fill_cache(filp, dirent, filldir, name, len,
1391-
proc_fd_instantiate, task, &fd);
1392-
}
1393-
1394-
static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1404+
static int proc_readfd_common(struct file * filp, void * dirent,
1405+
filldir_t filldir, instantiate_t instantiate)
13951406
{
13961407
struct dentry *dentry = filp->f_path.dentry;
13971408
struct inode *inode = dentry->d_inode;
@@ -1427,12 +1438,17 @@ static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
14271438
for (fd = filp->f_pos-2;
14281439
fd < fdt->max_fds;
14291440
fd++, filp->f_pos++) {
1441+
char name[PROC_NUMBUF];
1442+
int len;
14301443

14311444
if (!fcheck_files(files, fd))
14321445
continue;
14331446
rcu_read_unlock();
14341447

1435-
if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1448+
len = snprintf(name, sizeof(name), "%d", fd);
1449+
if (proc_fill_cache(filp, dirent, filldir,
1450+
name, len, instantiate,
1451+
p, &fd) < 0) {
14361452
rcu_read_lock();
14371453
break;
14381454
}
@@ -1447,6 +1463,32 @@ static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
14471463
return retval;
14481464
}
14491465

1466+
static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1467+
struct nameidata *nd)
1468+
{
1469+
return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1470+
}
1471+
1472+
static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1473+
{
1474+
return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1475+
}
1476+
1477+
static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1478+
size_t len, loff_t *ppos)
1479+
{
1480+
char tmp[PROC_FDINFO_MAX];
1481+
int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1482+
if (!err)
1483+
err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1484+
return err;
1485+
}
1486+
1487+
static const struct file_operations proc_fdinfo_file_operations = {
1488+
.open = nonseekable_open,
1489+
.read = proc_fdinfo_read,
1490+
};
1491+
14501492
static const struct file_operations proc_fd_operations = {
14511493
.read = generic_read_dir,
14521494
.readdir = proc_readfd,
@@ -1478,6 +1520,58 @@ static const struct inode_operations proc_fd_inode_operations = {
14781520
.setattr = proc_setattr,
14791521
};
14801522

1523+
static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1524+
struct dentry *dentry, struct task_struct *task, const void *ptr)
1525+
{
1526+
unsigned fd = *(unsigned *)ptr;
1527+
struct inode *inode;
1528+
struct proc_inode *ei;
1529+
struct dentry *error = ERR_PTR(-ENOENT);
1530+
1531+
inode = proc_pid_make_inode(dir->i_sb, task);
1532+
if (!inode)
1533+
goto out;
1534+
ei = PROC_I(inode);
1535+
ei->fd = fd;
1536+
inode->i_mode = S_IFREG | S_IRUSR;
1537+
inode->i_fop = &proc_fdinfo_file_operations;
1538+
dentry->d_op = &tid_fd_dentry_operations;
1539+
d_add(dentry, inode);
1540+
/* Close the race of the process dying before we return the dentry */
1541+
if (tid_fd_revalidate(dentry, NULL))
1542+
error = NULL;
1543+
1544+
out:
1545+
return error;
1546+
}
1547+
1548+
static struct dentry *proc_lookupfdinfo(struct inode *dir,
1549+
struct dentry *dentry,
1550+
struct nameidata *nd)
1551+
{
1552+
return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1553+
}
1554+
1555+
static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1556+
{
1557+
return proc_readfd_common(filp, dirent, filldir,
1558+
proc_fdinfo_instantiate);
1559+
}
1560+
1561+
static const struct file_operations proc_fdinfo_operations = {
1562+
.read = generic_read_dir,
1563+
.readdir = proc_readfdinfo,
1564+
};
1565+
1566+
/*
1567+
* proc directories can do almost nothing..
1568+
*/
1569+
static const struct inode_operations proc_fdinfo_inode_operations = {
1570+
.lookup = proc_lookupfdinfo,
1571+
.setattr = proc_setattr,
1572+
};
1573+
1574+
14811575
static struct dentry *proc_pident_instantiate(struct inode *dir,
14821576
struct dentry *dentry, struct task_struct *task, const void *ptr)
14831577
{
@@ -1888,6 +1982,7 @@ static const struct inode_operations proc_task_inode_operations;
18881982
static const struct pid_entry tgid_base_stuff[] = {
18891983
DIR("task", S_IRUGO|S_IXUGO, task),
18901984
DIR("fd", S_IRUSR|S_IXUSR, fd),
1985+
DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
18911986
INF("environ", S_IRUSR, pid_environ),
18921987
INF("auxv", S_IRUSR, pid_auxv),
18931988
INF("status", S_IRUGO, pid_status),
@@ -2041,7 +2136,7 @@ static struct dentry *proc_pid_instantiate(struct inode *dir,
20412136
inode->i_op = &proc_tgid_base_inode_operations;
20422137
inode->i_fop = &proc_tgid_base_operations;
20432138
inode->i_flags|=S_IMMUTABLE;
2044-
inode->i_nlink = 4;
2139+
inode->i_nlink = 5;
20452140
#ifdef CONFIG_SECURITY
20462141
inode->i_nlink += 1;
20472142
#endif
@@ -2171,6 +2266,7 @@ int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
21712266
*/
21722267
static const struct pid_entry tid_base_stuff[] = {
21732268
DIR("fd", S_IRUSR|S_IXUSR, fd),
2269+
DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
21742270
INF("environ", S_IRUSR, pid_environ),
21752271
INF("auxv", S_IRUSR, pid_auxv),
21762272
INF("status", S_IRUGO, pid_status),
@@ -2251,7 +2347,7 @@ static struct dentry *proc_task_instantiate(struct inode *dir,
22512347
inode->i_op = &proc_tid_base_inode_operations;
22522348
inode->i_fop = &proc_tid_base_operations;
22532349
inode->i_flags|=S_IMMUTABLE;
2254-
inode->i_nlink = 3;
2350+
inode->i_nlink = 4;
22552351
#ifdef CONFIG_SECURITY
22562352
inode->i_nlink += 1;
22572353
#endif

0 commit comments

Comments
 (0)