2
* Copyright IBM Corporation. 2007
4
* Author: Balbir Singh <balbir@linux.vnet.ibm.com>
6
* This program is free software; you can redistribute it and/or modify it
7
* under the terms of version 2.1 of the GNU Lesser General Public License
8
* as published by the Free Software Foundation.
10
* This program is distributed in the hope that it would be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24
#include <linux/types.h>
28
#include <sys/types.h>
31
#include <linux/cn_proc.h>
41
/* Maximum number of mount points/controllers */
42
#define MAX_MNT_ELEMENTS 8
43
/* Estimated number of groups created */
44
#define MAX_GROUP_ELEMENTS 128
47
* NOTE: Wide characters are not supported at the moment. Wide character support
48
* would require us to use a scanner/parser that can parse beyond ASCII
51
/* Definitions for the uid and gid members of a cgroup_rules */
52
#define CGRULE_INVALID (-1)
53
#define CGRULE_WILD (-2)
55
#define CGRULE_SUCCESS_STORE_PID "SUCCESS_STORE_PID"
57
/* Flags for cgroup_change_cgroup_uid_gid() */
59
CGFLAG_USECACHE = 0x01,
63
* per thread errno variable, to be used when return code is ECGOTHER
65
extern __thread int last_errno;
68
ECGROUPNOTCOMPILED=50000,
72
ECGROUPSUBSYSNOTMOUNTED,
74
ECGROUPMULTIMOUNTED,/* Controllers bound to different mount points */
75
ECGROUPNOTALLOWED, /* This is the stock error. Default error. */
80
ECGCONTROLLERCREATEFAILED,
82
ECGROUPNOTINITIALIZED,
84
/* Represents error coming from other libraries like glibc. libcgroup
85
* users need to check errno upon encoutering ECGOTHER.
87
ECGOTHER, /* OS error, see errno */
89
ECGCONTROLLERNOTEQUAL,
90
ECGROUPPARSEFAIL, /* Failed to parse rules configuration file. */
91
ECGROUPNORULES, /* Rules list does not exist. */
93
ECGSENTINEL, /* Please insert further error codes above this */
94
ECGEOF, /* End of file, iterator */
98
* Don't use CGROUP_WALK_TYPE_FILE right now. It is added here for
99
* later refactoring and better implementation. Most users *should*
100
* use CGROUP_WALK_TYPE_PRE_DIR.
102
enum cgroup_walk_type {
103
CGROUP_WALK_TYPE_PRE_DIR = 0x1, /* Pre Order Directory */
104
CGROUP_WALK_TYPE_POST_DIR = 0x2, /* Post Order Directory */
107
enum cgroup_file_type {
108
CGROUP_FILE_TYPE_FILE, /* File */
109
CGROUP_FILE_TYPE_DIR, /* Directory */
110
CGROUP_FILE_TYPE_OTHER, /* Directory */
113
enum cgroup_daemon_type {
114
CGROUP_DAEMON_UNCHANGE_CHILDREN = 0x1,
117
struct cgroup_file_info {
118
enum cgroup_file_type type;
121
const char *full_path;
125
#define CG_NV_MAX 100
126
#define CG_CONTROLLER_MAX 100
127
#define CG_VALUE_MAX 100
128
/* Max number of mounted hierarchies. Event if one controller is mounted per
129
* hier, it can not exceed CG_CONTROLLER_MAX
131
#define CG_HIER_MAX CG_CONTROLLER_MAX
134
char name[FILENAME_MAX];
135
char value[CG_VALUE_MAX];
138
struct cgroup_mount_point {
139
char name[FILENAME_MAX];
140
char path[FILENAME_MAX];
143
/* Functions and structures that can be used by the application*/
145
struct cgroup_controller;
147
int cgroup_init(void);
148
int cgroup_attach_task(struct cgroup *cgroup);
149
int cgroup_modify_cgroup(struct cgroup *cgroup);
150
int cgroup_create_cgroup(struct cgroup *cgroup, int ignore_ownership);
151
int cgroup_delete_cgroup(struct cgroup *cgroup, int ignore_migration);
152
int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid);
153
int cgroup_get_cgroup(struct cgroup *cgroup);
154
int cgroup_create_cgroup_from_parent(struct cgroup *cgroup, int ignore_ownership);
155
int cgroup_copy_cgroup(struct cgroup *dst, struct cgroup *src);
158
* Changes the cgroup of a program based on the rules in the config file.
159
* If a rule exists for the given UID, GID or PROCESS NAME, then the given
160
* PID is placed into the correct group. By default, this function parses
161
* the configuration file each time it is called.
163
* The flags can alter the behavior of this function:
164
* CGFLAG_USECACHE: Use cached rules instead of parsing the config file
166
* This function may NOT be thread safe.
167
* @param uid The UID to match
168
* @param gid The GID to match
169
* @param procname The PROCESS NAME to match
170
* @param pid The PID of the process to move
171
* @param flags Bit flags to change the behavior, as defined above
172
* @return 0 on success, > 0 on error
173
* TODO: Determine thread-safeness and fix of not safe.
175
int cgroup_change_cgroup_flags(const uid_t uid, const gid_t gid,
176
char *procname, const pid_t pid, const int flags);
179
* Changes the cgroup of a program based on the rules in the config file. If a
180
* rule exists for the given UID or GID, then the given PID is placed into the
181
* correct group. By default, this function parses the configuration file each
184
* The flags can alter the behavior of this function:
185
* CGFLAG_USECACHE: Use cached rules instead of parsing the config file
187
* This function may NOT be thread safe.
188
* @param uid The UID to match
189
* @param gid The GID to match
190
* @param pid The PID of the process to move
191
* @param flags Bit flags to change the behavior, as defined above
192
* @return 0 on success, > 0 on error
193
* TODO: Determine thread-safeness and fix if not safe.
195
int cgroup_change_cgroup_uid_gid_flags(const uid_t uid, const gid_t gid,
196
const pid_t pid, const int flags);
199
* Provides backwards-compatibility with older versions of the API. This
200
* function is deprecated, and cgroup_change_cgroup_uid_gid_flags() should be
201
* used instead. In fact, this function simply calls the newer one with flags
203
* @param uid The UID to match
204
* @param gid The GID to match
205
* @param pid The PID of the process to move
206
* @return 0 on success, > 0 on error
209
int cgroup_change_cgroup_uid_gid(uid_t uid, gid_t gid, pid_t pid);
212
* Changes the cgroup of a program based on the path provided. In this case,
213
* the user must already know into which cgroup the task should be placed and
214
* no rules will be parsed.
216
* returns 0 on success.
218
int cgroup_change_cgroup_path(char *path, pid_t pid, char *controllers[]);
221
* Print the cached rules table. This function should be called only after
222
* first calling cgroup_parse_config(), but it will work with an empty rule
224
* @param fp The file stream to print to
226
void cgroup_print_rules_config(FILE *fp);
229
* Reloads the rules list, using the given configuration file. This function
230
* is probably NOT thread safe (calls cgroup_parse_rules_config()).
231
* @return 0 on success, > 0 on failure
233
int cgroup_reload_cached_rules(void);
236
* Initializes the rules cache.
237
* @return 0 on success, > 0 on failure
239
int cgroup_init_rules_cache(void);
242
* Get the current cgroup path where the task specified by pid_t pid
243
* has been classified
245
int cgroup_get_current_controller_path(pid_t pid, const char *controller,
246
char **current_path);
248
* Return error corresponding to @code in human readable format.
249
* @code: error code for which the corresponding error string is to be
252
char *cgroup_strerror(int code);
255
* Return last errno, which caused ECGOTHER error.
257
int cgroup_get_last_errno();
260
* Walk through the directory tree for the specified controller.
261
* @controller: Name of the controller, for which we want to walk
263
* @base_path: Begin walking from this path
264
* @depth: The maximum depth to which the function should walk, 0
265
* implies all the way down
266
* @handle: Handle to be used during iteration
267
* @info: info filled and returned about directory information
269
int cgroup_walk_tree_begin(char *controller, char *base_path, const int depth,
270
void **handle, struct cgroup_file_info *info,
273
* Get the next element during the walk
274
* @depth: The maximum depth to which the function should walk, 0
275
* implies all the way down
276
* @handle: Handle to be used during iteration
277
* @info: info filled and returned about directory information
279
* Returns ECGEOF when we are done walking through the nodes.
281
int cgroup_walk_tree_next(const int depth, void **handle,
282
struct cgroup_file_info *info, int base_level);
283
int cgroup_walk_tree_end(void **handle);
286
* This API is used to set the flags for walk_tree API. Currently availble
289
* CGROUP_WALK_TYPE_PRE_DIR
290
* CGROUP_WALK_TYPE_POST_DIR
293
int cgroup_walk_tree_set_flags(void **handle, int flags);
296
* Read the statistics values for the specified controller
297
* @controller: Name of the controller for which stats are requested.
298
* @path: cgroup path.
299
* @handle: Handle to be used during iteration.
300
* @stat: Stats values will be filled and returned here.
302
int cgroup_read_stats_begin(char *controller, char *path, void **handle,
303
struct cgroup_stat *stat);
306
* Read the next stat value.
307
* @handle: Handle to be used during iteration.
308
* @stat: Stats values will be filled and returned here.
310
int cgroup_read_stats_next(void **handle, struct cgroup_stat *stat);
312
int cgroup_read_stats_end(void **handle);
315
* Read the tasks file to get the list of tasks in a cgroup
316
* @cgroup: Name of the cgroup
317
* @controller: Name of the cgroup subsystem
318
* @handle: Handle to be used in the iteration
319
* @pid: The pid read from the tasks file. Will be filled in by the API
321
int cgroup_get_task_begin(char *cgroup, char *controller, void **handle,
325
* Register the unchanged process to a cgrulesengd daemon.
326
* If the daemon does not work, this function returns 0 as success.
327
* @param pid: The process id
328
* @param flags Bit flags to change the behavior, as defined above
329
* @return 0 on success, > 0 on error.
331
int cgroup_register_unchanged_process(pid_t pid, int flags);
334
* Read the next task value
335
* @handle: The handle used for iterating
336
* @pid: The variable where the value will be stored
338
* return ECGEOF when the iterator finishes getting the list of tasks.
340
int cgroup_get_task_next(void **handle, pid_t *pid);
341
int cgroup_get_task_end(void **handle);
344
* Read the mount table to give a list where each controller is
346
* @handle: Handle to be used for iteration.
347
* @name: The variable where the name is stored. Should be freed by caller.
348
* @path: Te variable where the path to the controller is stored. Should be
349
* freed by the caller.
351
int cgroup_get_controller_begin(void **handle, struct cgroup_mount_point *info);
353
* While walking through the mount table, the controllers will be
354
* returned in order of their mount points.
356
int cgroup_get_controller_next(void **handle, struct cgroup_mount_point *info);
357
int cgroup_get_controller_end(void **handle);
360
* Reads the mount to table to give the mount point of a controller
361
* @controller: Name of the controller
362
* @mount_point: The string where the mount point is stored. Please note,
363
* the caller must free mount_point.
365
int cgroup_get_subsys_mount_point(char *controller, char **mount_point);
367
/* The wrappers for filling libcg structures */
369
struct cgroup *cgroup_new_cgroup(const char *name);
370
struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup,
372
void cgroup_free(struct cgroup **cgroup);
373
void cgroup_free_controllers(struct cgroup *cgroup);
374
int cgroup_add_value_string(struct cgroup_controller *controller,
375
const char *name, const char *value);
376
int cgroup_add_value_int64(struct cgroup_controller *controller,
377
const char *name, int64_t value);
378
int cgroup_add_value_uint64(struct cgroup_controller *controller,
379
const char *name, u_int64_t value);
380
int cgroup_add_value_bool(struct cgroup_controller *controller,
381
const char *name, bool value);
382
int cgroup_compare_cgroup(struct cgroup *cgroup_a, struct cgroup *cgroup_b);
383
int cgroup_compare_controllers(struct cgroup_controller *cgca,
384
struct cgroup_controller *cgcb);
385
int cgroup_set_uid_gid(struct cgroup *cgroup, uid_t tasks_uid, gid_t tasks_gid,
386
uid_t control_uid, gid_t control_gid);
387
int cgroup_get_uid_gid(struct cgroup *cgroup, uid_t *tasks_uid,
388
gid_t *tasks_gid, uid_t *control_uid, gid_t *control_gid);
389
int cgroup_get_value_string(struct cgroup_controller *controller,
390
const char *name, char **value);
391
int cgroup_set_value_string(struct cgroup_controller *controller,
392
const char *name, const char *value);
393
int cgroup_get_value_int64(struct cgroup_controller *controller,
394
const char *name, int64_t *value);
395
int cgroup_set_value_int64(struct cgroup_controller *controller,
396
const char *name, int64_t value);
397
int cgroup_get_value_uint64(struct cgroup_controller *controller,
398
const char *name, u_int64_t *value);
399
int cgroup_set_value_uint64(struct cgroup_controller *controller,
400
const char *name, u_int64_t value);
401
int cgroup_get_value_bool(struct cgroup_controller *controller,
402
const char *name, bool *value);
403
int cgroup_set_value_bool(struct cgroup_controller *controller,
404
const char *name, bool value);
405
struct cgroup_controller *cgroup_get_controller(struct cgroup *cgroup,
408
* Config related stuff
410
int cgroup_config_load_config(const char *pathname);
411
int cgroup_unload_cgroups(void);
415
#endif /* _LIBCG_H */