Skip to content

Commit be61408

Browse files
ebiedermLinus Torvalds
authored andcommitted
proc: implement proc_single_file_operations
Currently many /proc/pid files use a crufty precursor to the current seq_file api, and they don't have direct access to the pid_namespace or the pid of for which they are displaying data. So implement proc_single_file_operations to make the seq_file routines easy to use, and to give access to the full state of the pid of we are displaying data for. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Cc: Oleg Nesterov <oleg@tv-sign.ru> Cc: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 94413d8 commit be61408

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ struct pid_entry {
121121
NOD(NAME, (S_IFREG|(MODE)), \
122122
NULL, &proc_info_file_operations, \
123123
{ .proc_read = &proc_##OTYPE } )
124+
#define ONE(NAME, MODE, OTYPE) \
125+
NOD(NAME, (S_IFREG|(MODE)), \
126+
NULL, &proc_single_file_operations, \
127+
{ .proc_show = &proc_##OTYPE } )
124128

125129
int maps_protect;
126130
EXPORT_SYMBOL(maps_protect);
@@ -658,6 +662,45 @@ static const struct file_operations proc_info_file_operations = {
658662
.read = proc_info_read,
659663
};
660664

665+
static int proc_single_show(struct seq_file *m, void *v)
666+
{
667+
struct inode *inode = m->private;
668+
struct pid_namespace *ns;
669+
struct pid *pid;
670+
struct task_struct *task;
671+
int ret;
672+
673+
ns = inode->i_sb->s_fs_info;
674+
pid = proc_pid(inode);
675+
task = get_pid_task(pid, PIDTYPE_PID);
676+
if (!task)
677+
return -ESRCH;
678+
679+
ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
680+
681+
put_task_struct(task);
682+
return ret;
683+
}
684+
685+
static int proc_single_open(struct inode *inode, struct file *filp)
686+
{
687+
int ret;
688+
ret = single_open(filp, proc_single_show, NULL);
689+
if (!ret) {
690+
struct seq_file *m = filp->private_data;
691+
692+
m->private = inode;
693+
}
694+
return ret;
695+
}
696+
697+
static const struct file_operations proc_single_file_operations = {
698+
.open = proc_single_open,
699+
.read = seq_read,
700+
.llseek = seq_lseek,
701+
.release = single_release,
702+
};
703+
661704
static int mem_open(struct inode* inode, struct file* file)
662705
{
663706
file->private_data = (void*)((long)current->self_exec_id);

‎include/linux/proc_fs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ extern void kclist_add(struct kcore_list *, void *, size_t);
262262
union proc_op {
263263
int (*proc_get_link)(struct inode *, struct dentry **, struct vfsmount **);
264264
int (*proc_read)(struct task_struct *task, char *page);
265+
int (*proc_show)(struct seq_file *m,
266+
struct pid_namespace *ns, struct pid *pid,
267+
struct task_struct *task);
265268
};
266269

267270
struct proc_inode {

0 commit comments

Comments
 (0)