^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/pid_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * /proc/thread_self:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static const char *proc_thread_self_get_link(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct delayed_call *done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) pid_t tgid = task_tgid_nr_ns(current, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) pid_t pid = task_pid_nr_ns(current, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Not currently supported. Once we can inherit all of struct pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * we can allow this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (current->flags & PF_IO_WORKER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return ERR_PTR(-EOPNOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (!pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) name = kmalloc(10 + 6 + 10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (unlikely(!name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) sprintf(name, "%u/task/%u", tgid, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) set_delayed_call(done, kfree_link, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static const struct inode_operations proc_thread_self_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .get_link = proc_thread_self_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static unsigned thread_self_inum __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int proc_setup_thread_self(struct super_block *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct inode *root_inode = d_inode(s->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct proc_fs_info *fs_info = proc_sb_info(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct dentry *thread_self;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) inode_lock(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) thread_self = d_alloc_name(s->s_root, "thread-self");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (thread_self) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct inode *inode = new_inode(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) inode->i_ino = thread_self_inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) inode->i_mode = S_IFLNK | S_IRWXUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) inode->i_uid = GLOBAL_ROOT_UID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) inode->i_gid = GLOBAL_ROOT_GID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) inode->i_op = &proc_thread_self_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) d_add(thread_self, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) dput(thread_self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) inode_unlock(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) pr_err("proc_fill_super: can't allocate /proc/thread-self\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) fs_info->proc_thread_self = thread_self;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) void __init proc_thread_self_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) proc_alloc_inum(&thread_self_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }