1 #include <linux/sched.h> 2 #include <linux/namei.h> 3 #include <linux/slab.h> 4 #include <linux/pid_namespace.h> 5 #include "internal.h" 6 7 /* 8 * /proc/thread_self: 9 */ 10 static int proc_thread_self_readlink(struct dentry *dentry, char __user *buffer, 11 int buflen) 12 { 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]; 17 if (!pid) 18 return -ENOENT; 19 sprintf(tmp, "%d/task/%d", tgid, pid); 20 return readlink_copy(buffer, buflen, tmp); 21 } 22 23 static void *proc_thread_self_follow_link(struct dentry *dentry, struct nameidata *nd) 24 { 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); 29 if (pid) { 30 name = kmalloc(PROC_NUMBUF + 6 + PROC_NUMBUF, GFP_KERNEL); 31 if (!name) 32 name = ERR_PTR(-ENOMEM); 33 else 34 sprintf(name, "%d/task/%d", tgid, pid); 35 } 36 nd_set_link(nd, name); 37 return NULL; 38 } 39 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, 44 }; 45 46 static unsigned thread_self_inum; 47 48 int proc_setup_thread_self(struct super_block *s) 49 { 50 struct inode *root_inode = s->s_root->d_inode; 51 struct pid_namespace *ns = s->s_fs_info; 52 struct dentry *thread_self; 53 54 mutex_lock(&root_inode->i_mutex); 55 thread_self = d_alloc_name(s->s_root, "thread-self"); 56 if (thread_self) { 57 struct inode *inode = new_inode_pseudo(s); 58 if (inode) { 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); 66 } else { 67 dput(thread_self); 68 thread_self = ERR_PTR(-ENOMEM); 69 } 70 } else { 71 thread_self = ERR_PTR(-ENOMEM); 72 } 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); 77 } 78 ns->proc_thread_self = thread_self; 79 return 0; 80 } 81 82 void __init proc_thread_self_init(void) 83 { 84 proc_alloc_inum(&thread_self_inum); 85 } 86