self.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  9. int buflen)
  10. {
  11. struct pid_namespace *ns = dentry->d_sb->s_fs_info;
  12. pid_t tgid = task_tgid_nr_ns(current, ns);
  13. char tmp[PROC_NUMBUF];
  14. if (!tgid)
  15. return -ENOENT;
  16. sprintf(tmp, "%d", tgid);
  17. return readlink_copy(buffer, buflen, tmp);
  18. }
  19. static const char *proc_self_get_link(struct dentry *dentry,
  20. struct inode *inode,
  21. struct delayed_call *done)
  22. {
  23. struct pid_namespace *ns = inode->i_sb->s_fs_info;
  24. pid_t tgid = task_tgid_nr_ns(current, ns);
  25. char *name;
  26. if (!tgid)
  27. return ERR_PTR(-ENOENT);
  28. /* 11 for max length of signed int in decimal + NULL term */
  29. name = kmalloc(12, dentry ? GFP_KERNEL : GFP_ATOMIC);
  30. if (unlikely(!name))
  31. return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
  32. sprintf(name, "%d", tgid);
  33. set_delayed_call(done, kfree_link, name);
  34. return name;
  35. }
  36. static const struct inode_operations proc_self_inode_operations = {
  37. .readlink = proc_self_readlink,
  38. .get_link = proc_self_get_link,
  39. };
  40. static unsigned self_inum;
  41. int proc_setup_self(struct super_block *s)
  42. {
  43. struct inode *root_inode = d_inode(s->s_root);
  44. struct pid_namespace *ns = s->s_fs_info;
  45. struct dentry *self;
  46. inode_lock(root_inode);
  47. self = d_alloc_name(s->s_root, "self");
  48. if (self) {
  49. struct inode *inode = new_inode_pseudo(s);
  50. if (inode) {
  51. inode->i_ino = self_inum;
  52. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  53. inode->i_mode = S_IFLNK | S_IRWXUGO;
  54. inode->i_uid = GLOBAL_ROOT_UID;
  55. inode->i_gid = GLOBAL_ROOT_GID;
  56. inode->i_op = &proc_self_inode_operations;
  57. d_add(self, inode);
  58. } else {
  59. dput(self);
  60. self = ERR_PTR(-ENOMEM);
  61. }
  62. } else {
  63. self = ERR_PTR(-ENOMEM);
  64. }
  65. inode_unlock(root_inode);
  66. if (IS_ERR(self)) {
  67. pr_err("proc_fill_super: can't allocate /proc/self\n");
  68. return PTR_ERR(self);
  69. }
  70. ns->proc_self = self;
  71. return 0;
  72. }
  73. void __init proc_self_init(void)
  74. {
  75. proc_alloc_inum(&self_inum);
  76. }