ns_cgroup.c 2.2 KB

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