0001
0002 #include <linux/cache.h>
0003 #include <linux/sched.h>
0004 #include <linux/slab.h>
0005 #include <linux/pid_namespace.h>
0006 #include "internal.h"
0007
0008
0009
0010
0011 static const char *proc_thread_self_get_link(struct dentry *dentry,
0012 struct inode *inode,
0013 struct delayed_call *done)
0014 {
0015 struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
0016 pid_t tgid = task_tgid_nr_ns(current, ns);
0017 pid_t pid = task_pid_nr_ns(current, ns);
0018 char *name;
0019
0020 if (!pid)
0021 return ERR_PTR(-ENOENT);
0022 name = kmalloc(10 + 6 + 10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
0023 if (unlikely(!name))
0024 return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
0025 sprintf(name, "%u/task/%u", tgid, pid);
0026 set_delayed_call(done, kfree_link, name);
0027 return name;
0028 }
0029
0030 static const struct inode_operations proc_thread_self_inode_operations = {
0031 .get_link = proc_thread_self_get_link,
0032 };
0033
0034 static unsigned thread_self_inum __ro_after_init;
0035
0036 int proc_setup_thread_self(struct super_block *s)
0037 {
0038 struct inode *root_inode = d_inode(s->s_root);
0039 struct proc_fs_info *fs_info = proc_sb_info(s);
0040 struct dentry *thread_self;
0041 int ret = -ENOMEM;
0042
0043 inode_lock(root_inode);
0044 thread_self = d_alloc_name(s->s_root, "thread-self");
0045 if (thread_self) {
0046 struct inode *inode = new_inode(s);
0047 if (inode) {
0048 inode->i_ino = thread_self_inum;
0049 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
0050 inode->i_mode = S_IFLNK | S_IRWXUGO;
0051 inode->i_uid = GLOBAL_ROOT_UID;
0052 inode->i_gid = GLOBAL_ROOT_GID;
0053 inode->i_op = &proc_thread_self_inode_operations;
0054 d_add(thread_self, inode);
0055 ret = 0;
0056 } else {
0057 dput(thread_self);
0058 }
0059 }
0060 inode_unlock(root_inode);
0061
0062 if (ret)
0063 pr_err("proc_fill_super: can't allocate /proc/thread-self\n");
0064 else
0065 fs_info->proc_thread_self = thread_self;
0066
0067 return ret;
0068 }
0069
0070 void __init proc_thread_self_init(void)
0071 {
0072 proc_alloc_inum(&thread_self_inum);
0073 }