self.c 2.0 KB

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