self.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
  37. void *cookie)
  38. {
  39. char *s = nd_get_link(nd);
  40. if (!IS_ERR(s))
  41. kfree(s);
  42. }
  43. static const struct inode_operations proc_self_inode_operations = {
  44. .readlink = proc_self_readlink,
  45. .follow_link = proc_self_follow_link,
  46. .put_link = proc_self_put_link,
  47. };
  48. static unsigned self_inum;
  49. int proc_setup_self(struct super_block *s)
  50. {
  51. struct inode *root_inode = s->s_root->d_inode;
  52. struct pid_namespace *ns = s->s_fs_info;
  53. struct dentry *self;
  54. mutex_lock(&root_inode->i_mutex);
  55. self = d_alloc_name(s->s_root, "self");
  56. if (self) {
  57. struct inode *inode = new_inode_pseudo(s);
  58. if (inode) {
  59. inode->i_ino = self_inum;
  60. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  61. inode->i_mode = S_IFLNK | S_IRWXUGO;
  62. inode->i_uid = GLOBAL_ROOT_UID;
  63. inode->i_gid = GLOBAL_ROOT_GID;
  64. inode->i_op = &proc_self_inode_operations;
  65. d_add(self, inode);
  66. } else {
  67. dput(self);
  68. self = ERR_PTR(-ENOMEM);
  69. }
  70. } else {
  71. self = ERR_PTR(-ENOMEM);
  72. }
  73. mutex_unlock(&root_inode->i_mutex);
  74. if (IS_ERR(self)) {
  75. pr_err("proc_fill_super: can't allocate /proc/self\n");
  76. return PTR_ERR(self);
  77. }
  78. ns->proc_self = self;
  79. return 0;
  80. }
  81. void __init proc_self_init(void)
  82. {
  83. proc_alloc_inum(&self_inum);
  84. }