2 * ns_cgroup.c - namespace cgroup subsystem
4 * Copyright 2006, 2007 IBM Corp
7 #include <linux/module.h>
8 #include <linux/cgroup.h>
10 #include <linux/slab.h>
13 struct cgroup_subsys_state css;
17 struct cgroup_subsys ns_subsys;
19 static inline struct ns_cgroup *cgroup_to_ns(
20 struct cgroup *cgroup)
22 return container_of(cgroup_subsys_state(cgroup, ns_subsys_id),
23 struct ns_cgroup, css);
26 int ns_cgroup_clone(struct task_struct *task)
28 return cgroup_clone(task, &ns_subsys);
33 * 1. you can only enter a cgroup which is a child of your current
35 * 2. you can only place another process into a cgroup if
36 * a. you have CAP_SYS_ADMIN
37 * b. your cgroup is an ancestor of task's destination cgroup
38 * (hence either you are in the same cgroup as task, or in an
39 * ancestor cgroup thereof)
41 static int ns_can_attach(struct cgroup_subsys *ss,
42 struct cgroup *new_cgroup, struct task_struct *task)
46 if (current != task) {
47 if (!capable(CAP_SYS_ADMIN))
50 if (!cgroup_is_descendant(new_cgroup))
54 if (atomic_read(&new_cgroup->count) != 0)
57 orig = task_cgroup(task, ns_subsys_id);
58 if (orig && orig != new_cgroup->parent)
65 * Rules: you can only create a cgroup if
66 * 1. you are capable(CAP_SYS_ADMIN)
67 * 2. the target cgroup is a descendant of your own cgroup
69 static struct cgroup_subsys_state *ns_create(struct cgroup_subsys *ss,
70 struct cgroup *cgroup)
72 struct ns_cgroup *ns_cgroup;
74 if (!capable(CAP_SYS_ADMIN))
75 return ERR_PTR(-EPERM);
76 if (!cgroup_is_descendant(cgroup))
77 return ERR_PTR(-EPERM);
79 ns_cgroup = kzalloc(sizeof(*ns_cgroup), GFP_KERNEL);
81 return ERR_PTR(-ENOMEM);
82 spin_lock_init(&ns_cgroup->lock);
83 return &ns_cgroup->css;
86 static void ns_destroy(struct cgroup_subsys *ss,
87 struct cgroup *cgroup)
89 struct ns_cgroup *ns_cgroup;
91 ns_cgroup = cgroup_to_ns(cgroup);
95 struct cgroup_subsys ns_subsys = {
97 .can_attach = ns_can_attach,
99 .destroy = ns_destroy,
100 .subsys_id = ns_subsys_id,