ns_cgroup.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * ns_cgroup.c - namespace cgroup subsystem
  3. *
  4. * Copyright 2006, 2007 IBM Corp
  5. */
  6. #include <linux/module.h>
  7. #include <linux/cgroup.h>
  8. #include <linux/fs.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/slab.h>
  11. #include <linux/nsproxy.h>
  12. struct ns_cgroup {
  13. struct cgroup_subsys_state css;
  14. spinlock_t lock;
  15. };
  16. struct cgroup_subsys ns_subsys;
  17. static inline struct ns_cgroup *cgroup_to_ns(
  18. struct cgroup *cgroup)
  19. {
  20. return container_of(cgroup_subsys_state(cgroup, ns_subsys_id),
  21. struct ns_cgroup, css);
  22. }
  23. int ns_cgroup_clone(struct task_struct *task, struct pid *pid)
  24. {
  25. char name[PROC_NUMBUF];
  26. snprintf(name, PROC_NUMBUF, "%d", pid_vnr(pid));
  27. return cgroup_clone(task, &ns_subsys, name);
  28. }
  29. /*
  30. * Rules:
  31. * 1. you can only enter a cgroup which is a child of your current
  32. * cgroup
  33. * 2. you can only place another process into a cgroup if
  34. * a. you have CAP_SYS_ADMIN
  35. * b. your cgroup is an ancestor of task's destination cgroup
  36. * (hence either you are in the same cgroup as task, or in an
  37. * ancestor cgroup thereof)
  38. */
  39. static int ns_can_attach(struct cgroup_subsys *ss,
  40. struct cgroup *new_cgroup, struct task_struct *task)
  41. {
  42. struct cgroup *orig;
  43. if (current != task) {
  44. if (!capable(CAP_SYS_ADMIN))
  45. return -EPERM;
  46. if (!cgroup_is_descendant(new_cgroup))
  47. return -EPERM;
  48. }
  49. if (atomic_read(&new_cgroup->count) != 0)
  50. return -EPERM;
  51. orig = task_cgroup(task, ns_subsys_id);
  52. if (orig && orig != new_cgroup->parent)
  53. return -EPERM;
  54. return 0;
  55. }
  56. /*
  57. * Rules: you can only create a cgroup if
  58. * 1. you are capable(CAP_SYS_ADMIN)
  59. * 2. the target cgroup is a descendant of your own cgroup
  60. */
  61. static struct cgroup_subsys_state *ns_create(struct cgroup_subsys *ss,
  62. struct cgroup *cgroup)
  63. {
  64. struct ns_cgroup *ns_cgroup;
  65. if (!capable(CAP_SYS_ADMIN))
  66. return ERR_PTR(-EPERM);
  67. if (!cgroup_is_descendant(cgroup))
  68. return ERR_PTR(-EPERM);
  69. ns_cgroup = kzalloc(sizeof(*ns_cgroup), GFP_KERNEL);
  70. if (!ns_cgroup)
  71. return ERR_PTR(-ENOMEM);
  72. spin_lock_init(&ns_cgroup->lock);
  73. return &ns_cgroup->css;
  74. }
  75. static void ns_destroy(struct cgroup_subsys *ss,
  76. struct cgroup *cgroup)
  77. {
  78. struct ns_cgroup *ns_cgroup;
  79. ns_cgroup = cgroup_to_ns(cgroup);
  80. kfree(ns_cgroup);
  81. }
  82. struct cgroup_subsys ns_subsys = {
  83. .name = "ns",
  84. .can_attach = ns_can_attach,
  85. .create = ns_create,
  86. .destroy = ns_destroy,
  87. .subsys_id = ns_subsys_id,
  88. };