self.c 2.1 KB

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