]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cgroup.h
cgmanager: chown cgroups to the container root
[mirror_lxc.git] / src / lxc / cgroup.h
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 #ifndef _ncgroup_h
24 #define _ncgroup_h
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stddef.h>
28
29 #include "state.h"
30
31 struct cgroup_hierarchy;
32 struct cgroup_meta_data;
33 struct cgroup_mount_point;
34
35 /*
36 * cgroup_meta_data: the metadata about the cgroup infrastructure on this
37 * host
38 */
39 struct cgroup_meta_data {
40 ptrdiff_t ref; /* simple refcount */
41 struct cgroup_hierarchy **hierarchies;
42 struct cgroup_mount_point **mount_points;
43 int maximum_hierarchy;
44 };
45
46 /*
47 * cgroup_hierarchy: describes a single cgroup hierarchy
48 * (may have multiple mount points)
49 */
50 struct cgroup_hierarchy {
51 int index;
52 bool used; /* false if the hierarchy should be ignored by lxc */
53 char **subsystems;
54 struct cgroup_mount_point *rw_absolute_mount_point;
55 struct cgroup_mount_point *ro_absolute_mount_point;
56 struct cgroup_mount_point **all_mount_points;
57 size_t all_mount_point_capacity;
58 };
59
60 /*
61 * cgroup_mount_point: a mount point to where a hierarchy
62 * is mounted to
63 */
64 struct cgroup_mount_point {
65 struct cgroup_hierarchy *hierarchy;
66 char *mount_point;
67 char *mount_prefix;
68 bool read_only;
69 };
70
71 /*
72 * cgroup_process_info: describes the membership of a
73 * process to the different cgroup
74 * hierarchies
75 *
76 * Note this is the per-process info tracked by the cgfs_ops.
77 * This is not used with cgmanager.
78 */
79 struct cgroup_process_info {
80 struct cgroup_process_info *next;
81 struct cgroup_meta_data *meta_ref;
82 struct cgroup_hierarchy *hierarchy;
83 char *cgroup_path;
84 char *cgroup_path_sub;
85 char **created_paths;
86 size_t created_paths_capacity;
87 size_t created_paths_count;
88 struct cgroup_mount_point *designated_mount_point;
89 };
90
91 /* meta data management:
92 * lxc_cgroup_load_meta loads the meta data (using subsystem
93 * whitelist from main lxc configuration)
94 * lxc_cgroup_load_meta2 does the same, but allows one to specify
95 * a custom whitelist
96 * lxc_cgroup_get_meta increments the refcount of a meta data
97 * object
98 * lxc_cgroup_put_meta decrements the refcount of a meta data
99 * object, potentially destroying it
100 */
101 extern struct cgroup_meta_data *lxc_cgroup_load_meta();
102 extern struct cgroup_meta_data *lxc_cgroup_load_meta2(const char **subsystem_whitelist);
103 extern struct cgroup_meta_data *lxc_cgroup_get_meta(struct cgroup_meta_data *meta_data);
104 extern struct cgroup_meta_data *lxc_cgroup_put_meta(struct cgroup_meta_data *meta_data);
105
106 /* find the hierarchy corresponding to a given subsystem */
107 extern struct cgroup_hierarchy *lxc_cgroup_find_hierarchy(struct cgroup_meta_data *meta_data, const char *subsystem);
108
109 /* find a mount point for a given hierarchy that has access to the cgroup in 'cgroup' and (if wanted) is writable */
110 extern struct cgroup_mount_point *lxc_cgroup_find_mount_point(struct cgroup_hierarchy *hierarchy, const char *group, bool should_be_writable);
111
112 /* all-in-one: find a mount point for a given hierarchy that has access to the cgroup and return the correct path within */
113 extern char *lxc_cgroup_find_abs_path(const char *subsystem, const char *group, bool should_be_writable, const char *suffix);
114
115 /* determine the cgroup membership of a given process */
116 extern struct cgroup_process_info *lxc_cgroup_process_info_get(pid_t pid, struct cgroup_meta_data *meta);
117 extern struct cgroup_process_info *lxc_cgroup_process_info_get_init(struct cgroup_meta_data *meta);
118 extern struct cgroup_process_info *lxc_cgroup_process_info_get_self(struct cgroup_meta_data *meta);
119
120 /* create a new cgroup */
121 extern struct cgroup_process_info *lxc_cgroup_create(const char *name, const char *path_pattern, struct cgroup_meta_data *meta_data, const char *sub_pattern);
122 extern int lxc_cgroup_create_legacy(struct cgroup_process_info *base_info, const char *name, pid_t pid);
123
124 /* get the cgroup membership of a given container */
125 extern struct cgroup_process_info *lxc_cgroup_get_container_info(const char *name, const char *lxcpath, struct cgroup_meta_data *meta_data);
126
127 /* move a processs to the cgroups specified by the membership TODO - deprecated, switch users to cgroup_enter() */
128 extern int lxc_cgroupfs_enter(struct cgroup_process_info *info, pid_t pid, bool enter_sub);
129
130 /* free process membership information */
131 extern void lxc_cgroup_process_info_free(struct cgroup_process_info *info);
132 extern void lxc_cgroup_process_info_free_and_remove(struct cgroup_process_info *info);
133
134 struct lxc_handler;
135 extern char *lxc_cgroup_get_hierarchy_path(const char *subsystem, const char *name, const char *lxcpath);
136 extern char *lxc_cgroup_get_hierarchy_abs_path_handler(const char *subsystem, struct lxc_handler *handler);
137 extern char *lxc_cgroup_get_hierarchy_abs_path(const char *subsystem, const char *name, const char *lxcpath);
138 extern int lxc_cgroup_set_handler(const char *filename, const char *value, struct lxc_handler *handler);
139 extern int lxc_cgroup_get_handler(const char *filename, char *value, size_t len, struct lxc_handler *handler);
140
141 /*
142 * lxc_cgroup_path_get: Get the absolute pathname for a cgroup
143 * file for a running container.
144 *
145 * @filename : the file of interest (e.g. "freezer.state") or
146 * the subsystem name (e.g. "freezer") in which case
147 * the directory where the cgroup may be modified
148 * will be returned
149 * @name : name of container to connect to
150 * @lxcpath : the lxcpath in which the container is running
151 *
152 * This is the exported function, which determines cgpath from the
153 * lxc-start of the @name container running in @lxcpath.
154 *
155 * Returns path on success, NULL on error. The caller must free()
156 * the returned path.
157 */
158 extern char *lxc_cgroup_path_get(const char *filename, const char *name,
159 const char *lxcpath);
160
161 struct lxc_list;
162 extern int lxc_setup_cgroup_without_devices(struct lxc_handler *h, struct lxc_list *cgroup_settings);
163 extern int lxc_setup_cgroup_devices(struct lxc_handler *h, struct lxc_list *cgroup_settings);
164
165 extern int lxc_cgroup_nrtasks_handler(struct lxc_handler *handler);
166
167 extern int do_unfreeze(int freeze, const char *name, const char *lxcpath);
168 extern int freeze_unfreeze(const char *name, int freeze, const char *lxcpath);
169 extern const char *lxc_state2str(lxc_state_t state);
170 extern lxc_state_t freezer_state(const char *name, const char *lxcpath);
171 /* per-backend cgroup hooks */
172 struct cgroup_ops {
173 void (*destroy)(struct lxc_handler *handler);
174 bool (*init)(struct lxc_handler *handler);
175 bool (*create)(struct lxc_handler *handler);
176 bool (*enter)(struct lxc_handler *handler);
177 bool (*create_legacy)(struct lxc_handler *handler);
178 char *(*get_cgroup)(struct lxc_handler *handler, const char *subsystem);
179 int (*set)(const char *filename, const char *value, const char *name, const char *lxcpath);
180 int (*get)(const char *filename, char *value, size_t len, const char *name, const char *lxcpath);
181 int (*unfreeze_fromhandler)(struct lxc_handler *handler);
182 bool (*setup_limits)(struct lxc_handler *handler, bool with_devices);
183 bool (*chown)(struct lxc_handler *handler);
184 const char *name;
185 };
186
187 /*
188 * cgroup-related data for backend use in start/stop of a
189 * container. This is tacked to the lxc_handler.
190 */
191 struct lxc_cgroup_info {
192 /* handlers to actually do the cgroup stuff */
193 struct cgroup_ops *ops;
194 /* extra data for the cgroup_ops, i.e. mountpoints for fs backend */
195 void *data;
196 const char *cgroup_pattern;
197 };
198
199 extern int lxc_setup_mount_cgroup(const char *root, struct lxc_cgroup_info *base_info, int type);
200
201 struct cgfs_data {
202 struct cgroup_meta_data *meta;
203 struct cgroup_process_info *info;
204 };
205
206 /*
207 * backend-independent cgroup handlers
208 */
209 extern void cgroup_destroy(struct lxc_handler *handler);
210 extern bool cgroup_init(struct lxc_handler *handler);
211 extern bool cgroup_create(struct lxc_handler *handler);
212 extern bool cgroup_setup_limits(struct lxc_handler *handler, bool with_devices);
213 extern bool cgroup_chown(struct lxc_handler *handler);
214 extern bool cgroup_enter(struct lxc_handler *handler);
215 extern void cgroup_cleanup(struct lxc_handler *handler);
216 extern bool cgroup_create_legacy(struct lxc_handler *handler);
217 extern char *cgroup_get_cgroup(struct lxc_handler *handler, const char *subsystem);
218 extern int lxc_cgroup_set(const char *filename, const char *value, const char *name, const char *lxcpath);
219 extern int lxc_cgroup_get(const char *filename, char *value, size_t len, const char *name, const char *lxcpath);
220 extern int lxc_unfreeze_fromhandler(struct lxc_handler *handler);
221
222 #endif