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/proc_fs.h>
11 #include <linux/slab.h>
12 #include <linux/nsproxy.h>
15 struct cgroup_subsys_state css
;
19 struct cgroup_subsys ns_subsys
;
21 static inline struct ns_cgroup
*cgroup_to_ns(
22 struct cgroup
*cgroup
)
24 return container_of(cgroup_subsys_state(cgroup
, ns_subsys_id
),
25 struct ns_cgroup
, css
);
28 int ns_cgroup_clone(struct task_struct
*task
, struct pid
*pid
)
30 char name
[PROC_NUMBUF
];
32 snprintf(name
, PROC_NUMBUF
, "%d", pid_vnr(pid
));
33 return cgroup_clone(task
, &ns_subsys
, name
);
38 * 1. you can only enter a cgroup which is a child of your current
40 * 2. you can only place another process into a cgroup if
41 * a. you have CAP_SYS_ADMIN
42 * b. your cgroup is an ancestor of task's destination cgroup
43 * (hence either you are in the same cgroup as task, or in an
44 * ancestor cgroup thereof)
46 static int ns_can_attach(struct cgroup_subsys
*ss
,
47 struct cgroup
*new_cgroup
, struct task_struct
*task
)
51 if (current
!= task
) {
52 if (!capable(CAP_SYS_ADMIN
))
55 if (!cgroup_is_descendant(new_cgroup
))
59 if (atomic_read(&new_cgroup
->count
) != 0)
62 orig
= task_cgroup(task
, ns_subsys_id
);
63 if (orig
&& orig
!= new_cgroup
->parent
)
70 * Rules: you can only create a cgroup if
71 * 1. you are capable(CAP_SYS_ADMIN)
72 * 2. the target cgroup is a descendant of your own cgroup
74 static struct cgroup_subsys_state
*ns_create(struct cgroup_subsys
*ss
,
75 struct cgroup
*cgroup
)
77 struct ns_cgroup
*ns_cgroup
;
79 if (!capable(CAP_SYS_ADMIN
))
80 return ERR_PTR(-EPERM
);
81 if (!cgroup_is_descendant(cgroup
))
82 return ERR_PTR(-EPERM
);
84 ns_cgroup
= kzalloc(sizeof(*ns_cgroup
), GFP_KERNEL
);
86 return ERR_PTR(-ENOMEM
);
87 spin_lock_init(&ns_cgroup
->lock
);
88 return &ns_cgroup
->css
;
91 static void ns_destroy(struct cgroup_subsys
*ss
,
92 struct cgroup
*cgroup
)
94 struct ns_cgroup
*ns_cgroup
;
96 ns_cgroup
= cgroup_to_ns(cgroup
);
100 struct cgroup_subsys ns_subsys
= {
102 .can_attach
= ns_can_attach
,
104 .destroy
= ns_destroy
,
105 .subsys_id
= ns_subsys_id
,