self.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/cache.h>
  3. #include <linux/sched.h>
  4. #include <linux/slab.h>
  5. #include <linux/pid_namespace.h>
  6. #include "internal.h"
  7. /*
  8. * /proc/self:
  9. */
  10. static const char *proc_self_get_link(struct dentry *dentry,
  11. struct inode *inode,
  12. struct delayed_call *done)
  13. {
  14. struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
  15. pid_t tgid = task_tgid_nr_ns(current, ns);
  16. char *name;
  17. if (!tgid)
  18. return ERR_PTR(-ENOENT);
  19. /* max length of unsigned int in decimal + NULL term */
  20. name = kmalloc(10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
  21. if (unlikely(!name))
  22. return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
  23. sprintf(name, "%u", tgid);
  24. set_delayed_call(done, kfree_link, name);
  25. return name;
  26. }
  27. static const struct inode_operations proc_self_inode_operations = {
  28. .get_link = proc_self_get_link,
  29. };
  30. static unsigned self_inum __ro_after_init;
  31. int proc_setup_self(struct super_block *s)
  32. {
  33. struct inode *root_inode = d_inode(s->s_root);
  34. struct proc_fs_info *fs_info = proc_sb_info(s);
  35. struct dentry *self;
  36. int ret = -ENOMEM;
  37. inode_lock(root_inode);
  38. self = d_alloc_name(s->s_root, "self");
  39. if (self) {
  40. struct inode *inode = new_inode(s);
  41. if (inode) {
  42. inode->i_ino = self_inum;
  43. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  44. inode->i_mode = S_IFLNK | S_IRWXUGO;
  45. inode->i_uid = GLOBAL_ROOT_UID;
  46. inode->i_gid = GLOBAL_ROOT_GID;
  47. inode->i_op = &proc_self_inode_operations;
  48. d_add(self, inode);
  49. ret = 0;
  50. } else {
  51. dput(self);
  52. }
  53. }
  54. inode_unlock(root_inode);
  55. if (ret)
  56. pr_err("proc_fill_super: can't allocate /proc/self\n");
  57. else
  58. fs_info->proc_self = self;
  59. return ret;
  60. }
  61. void __init proc_self_init(void)
  62. {
  63. proc_alloc_inum(&self_inum);
  64. }