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_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 char *name;
0018
0019 if (!tgid)
0020 return ERR_PTR(-ENOENT);
0021
0022 name = kmalloc(10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
0023 if (unlikely(!name))
0024 return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
0025 sprintf(name, "%u", tgid);
0026 set_delayed_call(done, kfree_link, name);
0027 return name;
0028 }
0029
0030 static const struct inode_operations proc_self_inode_operations = {
0031 .get_link = proc_self_get_link,
0032 };
0033
0034 static unsigned self_inum __ro_after_init;
0035
0036 int proc_setup_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 *self;
0041 int ret = -ENOMEM;
0042
0043 inode_lock(root_inode);
0044 self = d_alloc_name(s->s_root, "self");
0045 if (self) {
0046 struct inode *inode = new_inode(s);
0047 if (inode) {
0048 inode->i_ino = 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_self_inode_operations;
0054 d_add(self, inode);
0055 ret = 0;
0056 } else {
0057 dput(self);
0058 }
0059 }
0060 inode_unlock(root_inode);
0061
0062 if (ret)
0063 pr_err("proc_fill_super: can't allocate /proc/self\n");
0064 else
0065 fs_info->proc_self = self;
0066
0067 return ret;
0068 }
0069
0070 void __init proc_self_init(void)
0071 {
0072 proc_alloc_inum(&self_inum);
0073 }