self.c 1.7 KB

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