1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/sched.h>
3 #include <linux/slab.h>
4 #include <linux/pid_namespace.h>
10 static const char *proc_self_get_link(struct dentry *dentry,
12 struct delayed_call *done)
14 struct pid_namespace *ns = inode->i_sb->s_fs_info;
15 pid_t tgid = task_tgid_nr_ns(current, ns);
19 * Not currently supported. Once we can inherit all of struct pid,
22 if (current->flags & PF_KTHREAD)
23 return ERR_PTR(-EOPNOTSUPP);
26 return ERR_PTR(-ENOENT);
27 /* 11 for max length of signed int in decimal + NULL term */
28 name = kmalloc(12, dentry ? GFP_KERNEL : GFP_ATOMIC);
30 return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
31 sprintf(name, "%d", tgid);
32 set_delayed_call(done, kfree_link, name);
36 static const struct inode_operations proc_self_inode_operations = {
37 .get_link = proc_self_get_link,
40 static unsigned self_inum;
42 int proc_setup_self(struct super_block *s)
44 struct inode *root_inode = d_inode(s->s_root);
45 struct pid_namespace *ns = s->s_fs_info;
48 inode_lock(root_inode);
49 self = d_alloc_name(s->s_root, "self");
51 struct inode *inode = new_inode(s);
53 inode->i_ino = self_inum;
54 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
55 inode->i_mode = S_IFLNK | S_IRWXUGO;
56 inode->i_uid = GLOBAL_ROOT_UID;
57 inode->i_gid = GLOBAL_ROOT_GID;
58 inode->i_op = &proc_self_inode_operations;
62 self = ERR_PTR(-ENOMEM);
65 self = ERR_PTR(-ENOMEM);
67 inode_unlock(root_inode);
69 pr_err("proc_fill_super: can't allocate /proc/self\n");
76 void __init proc_self_init(void)
78 proc_alloc_inum(&self_inum);