thread_self.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <linux/sched.h>
  2. #include <linux/slab.h>
  3. #include <linux/pid_namespace.h>
  4. #include "internal.h"
  5. /*
  6. * /proc/thread_self:
  7. */
  8. static const char *proc_thread_self_get_link(struct dentry *dentry,
  9. struct inode *inode,
  10. struct delayed_call *done)
  11. {
  12. struct pid_namespace *ns = inode->i_sb->s_fs_info;
  13. pid_t tgid = task_tgid_nr_ns(current, ns);
  14. pid_t pid = task_pid_nr_ns(current, ns);
  15. char *name;
  16. if (!pid)
  17. return ERR_PTR(-ENOENT);
  18. name = kmalloc(PROC_NUMBUF + 6 + PROC_NUMBUF,
  19. dentry ? GFP_KERNEL : GFP_ATOMIC);
  20. if (unlikely(!name))
  21. return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
  22. sprintf(name, "%d/task/%d", tgid, pid);
  23. set_delayed_call(done, kfree_link, name);
  24. return name;
  25. }
  26. static const struct inode_operations proc_thread_self_inode_operations = {
  27. .get_link = proc_thread_self_get_link,
  28. };
  29. static unsigned thread_self_inum;
  30. int proc_setup_thread_self(struct super_block *s)
  31. {
  32. struct inode *root_inode = d_inode(s->s_root);
  33. struct pid_namespace *ns = s->s_fs_info;
  34. struct dentry *thread_self;
  35. inode_lock(root_inode);
  36. thread_self = d_alloc_name(s->s_root, "thread-self");
  37. if (thread_self) {
  38. struct inode *inode = new_inode_pseudo(s);
  39. if (inode) {
  40. inode->i_ino = thread_self_inum;
  41. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  42. inode->i_mode = S_IFLNK | S_IRWXUGO;
  43. inode->i_uid = GLOBAL_ROOT_UID;
  44. inode->i_gid = GLOBAL_ROOT_GID;
  45. inode->i_op = &proc_thread_self_inode_operations;
  46. d_add(thread_self, inode);
  47. } else {
  48. dput(thread_self);
  49. thread_self = ERR_PTR(-ENOMEM);
  50. }
  51. } else {
  52. thread_self = ERR_PTR(-ENOMEM);
  53. }
  54. inode_unlock(root_inode);
  55. if (IS_ERR(thread_self)) {
  56. pr_err("proc_fill_super: can't allocate /proc/thread_self\n");
  57. return PTR_ERR(thread_self);
  58. }
  59. ns->proc_thread_self = thread_self;
  60. return 0;
  61. }
  62. void __init proc_thread_self_init(void)
  63. {
  64. proc_alloc_inum(&thread_self_inum);
  65. }