self.c 2.0 KB

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