1 #include <linux/sched.h>
2 #include <linux/namei.h>
3 #include <linux/slab.h>
4 #include <linux/pid_namespace.h>
10 static int proc_thread_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
13 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
14 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
15 pid_t pid
= task_pid_nr_ns(current
, ns
);
16 char tmp
[PROC_NUMBUF
+ 6 + PROC_NUMBUF
];
19 sprintf(tmp
, "%d/task/%d", tgid
, pid
);
20 return readlink_copy(buffer
, buflen
, tmp
);
23 static void *proc_thread_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
25 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
26 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
27 pid_t pid
= task_pid_nr_ns(current
, ns
);
28 char *name
= ERR_PTR(-ENOENT
);
30 name
= kmalloc(PROC_NUMBUF
+ 6 + PROC_NUMBUF
, GFP_KERNEL
);
32 name
= ERR_PTR(-ENOMEM
);
34 sprintf(name
, "%d/task/%d", tgid
, pid
);
36 nd_set_link(nd
, name
);
40 static const struct inode_operations proc_thread_self_inode_operations
= {
41 .readlink
= proc_thread_self_readlink
,
42 .follow_link
= proc_thread_self_follow_link
,
43 .put_link
= kfree_put_link
,
46 static unsigned thread_self_inum
;
48 int proc_setup_thread_self(struct super_block
*s
)
50 struct inode
*root_inode
= s
->s_root
->d_inode
;
51 struct pid_namespace
*ns
= s
->s_fs_info
;
52 struct dentry
*thread_self
;
54 mutex_lock(&root_inode
->i_mutex
);
55 thread_self
= d_alloc_name(s
->s_root
, "thread-self");
57 struct inode
*inode
= new_inode_pseudo(s
);
59 inode
->i_ino
= thread_self_inum
;
60 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
61 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
62 inode
->i_uid
= GLOBAL_ROOT_UID
;
63 inode
->i_gid
= GLOBAL_ROOT_GID
;
64 inode
->i_op
= &proc_thread_self_inode_operations
;
65 d_add(thread_self
, inode
);
68 thread_self
= ERR_PTR(-ENOMEM
);
71 thread_self
= ERR_PTR(-ENOMEM
);
73 mutex_unlock(&root_inode
->i_mutex
);
74 if (IS_ERR(thread_self
)) {
75 pr_err("proc_fill_super: can't allocate /proc/thread_self\n");
76 return PTR_ERR(thread_self
);
78 ns
->proc_thread_self
= thread_self
;
82 void __init
proc_thread_self_init(void)
84 proc_alloc_inum(&thread_self_inum
);