libcgroup
|
Data Structures | |
struct | cgroup |
struct | cgroup_controller |
Basic infrastructure | |
The most important information is that one This approach is different to the one in the Linux kernel - a control group must be part of exactly one hierarchy there. In
Following functions are provided to create/destroy various libcgroup structures. Please note that none of these functions actually create or delete a cgroup in kernel! | |
struct cgroup * | cgroup_new_cgroup (const char *name) |
struct cgroup_controller * | cgroup_add_controller (struct cgroup *cgrp, const char *name) |
int | cgroup_add_all_controllers (struct cgroup *cgrp) |
struct cgroup_controller * | cgroup_get_controller (struct cgroup *cgrp, const char *name) |
void | cgroup_free (struct cgroup **cgrp) |
void | cgroup_free_controllers (struct cgroup *cgrp) |
#define | NO_PERMS (-1U) |
#define | NO_UID_GID (-1U) |
Group manipulation API | |
Using following functions you can create and remove control groups and change their parameters.
| |
int | cgroup_create_cgroup (struct cgroup *cgrp, int ignore_ownership) |
int | cgroup_create_cgroup_from_parent (struct cgroup *cgrp, int ignore_ownership) |
int | cgroup_modify_cgroup (struct cgroup *cgrp) |
int | cgroup_delete_cgroup (struct cgroup *cgrp, int ignore_migration) |
int | cgroup_delete_cgroup_ext (struct cgroup *cgrp, int flags) |
Other functions | |
Helper functions to manipulate with control groups. | |
int | cgroup_get_cgroup (struct cgroup *cgrp) |
int | cgroup_copy_cgroup (struct cgroup *dst, struct cgroup *src) |
int | cgroup_compare_cgroup (struct cgroup *cgrp_a, struct cgroup *cgrp_b) |
int | cgroup_compare_controllers (struct cgroup_controller *cgca, struct cgroup_controller *cgcb) |
int | cgroup_set_uid_gid (struct cgroup *cgrp, uid_t tasks_uid, gid_t tasks_gid, uid_t control_uid, gid_t control_gid) |
int | cgroup_get_uid_gid (struct cgroup *cgrp, uid_t *tasks_uid, gid_t *tasks_gid, uid_t *control_uid, gid_t *control_gid) |
void | cgroup_set_permissions (struct cgroup *cgrp, mode_t control_dperm, mode_t control_fperm, mode_t task_fperm) |
Group parameters | |
These are functions can read or modify parameter of a group.
| |
int | cgroup_add_value_string (struct cgroup_controller *controller, const char *name, const char *value) |
int | cgroup_add_value_int64 (struct cgroup_controller *controller, const char *name, int64_t value) |
int | cgroup_add_value_uint64 (struct cgroup_controller *controller, const char *name, u_int64_t value) |
int | cgroup_add_value_bool (struct cgroup_controller *controller, const char *name, bool value) |
int | cgroup_get_value_string (struct cgroup_controller *controller, const char *name, char **value) |
int | cgroup_get_value_int64 (struct cgroup_controller *controller, const char *name, int64_t *value) |
int | cgroup_get_value_uint64 (struct cgroup_controller *controller, const char *name, u_int64_t *value) |
int | cgroup_get_value_bool (struct cgroup_controller *controller, const char *name, bool *value) |
int | cgroup_set_value_string (struct cgroup_controller *controller, const char *name, const char *value) |
int | cgroup_set_value_int64 (struct cgroup_controller *controller, const char *name, int64_t value) |
int | cgroup_set_value_uint64 (struct cgroup_controller *controller, const char *name, u_int64_t value) |
int | cgroup_set_value_bool (struct cgroup_controller *controller, const char *name, bool value) |
int | cgroup_get_value_name_count (struct cgroup_controller *controller) |
char * | cgroup_get_value_name (struct cgroup_controller *controller, int index) |
int | cgroup_get_procs (const char *name, const char *controller, pid_t **pids, int *size) |
int | cgroup_get_threads (const char *name, const char *controller, pid_t **pids, int *size) |
int | cg_chmod_recursive (struct cgroup *cgrp, mode_t dir_mode, int dirm_change, mode_t file_mode, int filem_change) |
char * | cgroup_get_cgroup_name (struct cgroup *cgrp) |
int | cgroup_convert_cgroup (struct cgroup *const out_cgrp, enum cg_version_t out_version, const struct cgroup *const in_cgrp, enum cg_version_t in_version) |
int | cgroup_list_mount_points (const enum cg_version_t cgrp_version, char ***mount_paths) |
int | cgroup_get_controller_version (const char *const controller, enum cg_version_t *const version) |
enum cg_setup_mode_t | cgroup_setup_mode (void) |
int | cgroup_get_controller_count (struct cgroup *cgrp) |
struct cgroup_controller * | cgroup_get_controller_by_index (struct cgroup *cgrp, int index) |
char * | cgroup_get_controller_name (struct cgroup_controller *controller) |
bool | is_cgroup_mode_legacy (void) |
bool | is_cgroup_mode_hybrid (void) |
bool | is_cgroup_mode_unified (void) |
#define NO_PERMS (-1U) |
Uninitialized file/directory permissions used for task/control files.
#define NO_UID_GID (-1U) |
Uninitialized UID/GID used for task/control files.
int cg_chmod_recursive | ( | struct cgroup * | cgrp, |
mode_t | dir_mode, | ||
int | dirm_change, | ||
mode_t | file_mode, | ||
int | filem_change ) |
Change permission of files and directories of given group
cgrp | The cgroup which permissions should be changed |
dir_mode | The permission mode of group directory |
dirm_change | Denotes whether the directory change should be done |
file_mode | The permission mode of group files |
filem_change | Denotes whether the directory change should be done |
int cgroup_add_all_controllers | ( | struct cgroup * | cgrp | ) |
Attach all mounted controllers to given cgroup. This function just modifies internal libcgroup structure, not the kernel control group.
cgrp |
struct cgroup_controller * cgroup_add_controller | ( | struct cgroup * | cgrp, |
const char * | name ) |
Attach new controller to cgroup. This function just modifies internal libcgroup structure, not the kernel control group.
cgrp | |
name | The name of the controller, e.g. "freezer". |
int cgroup_add_value_bool | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
bool | value ) |
Add parameter and its value to internal libcgroup
structures. Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
controller | |
name | The name of the parameter. |
value |
int cgroup_add_value_int64 | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
int64_t | value ) |
Add parameter and its value to internal libcgroup
structures. Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel. Content of the value is copied to internal structures and is not needed after return from the function.
controller | |
name | The name of the parameter. |
value |
int cgroup_add_value_string | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
const char * | value ) |
Add parameter and its value to internal libcgroup
structures. Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
controller | |
name | The name of the parameter. |
value |
int cgroup_add_value_uint64 | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
u_int64_t | value ) |
Add parameter and its value to internal libcgroup
structures. Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
controller | |
name | The name of the parameter. |
value |
Compare names, owners, controllers, parameters and values of two groups.
cgrp_a | |
cgrp_b |
0 | if the groups are the same. |
ECGROUPNOTEQUAL | if the groups are not the same. |
ECGCONTROLLERNOTEQUAL | if the only difference are controllers, parameters or their values. |
int cgroup_compare_controllers | ( | struct cgroup_controller * | cgca, |
struct cgroup_controller * | cgcb ) |
Compare names, parameters and values of two controllers.
cgca | |
cgcb |
0 | if the controllers are the same. |
ECGCONTROLLERNOTEQUAL | if the controllers are not equal. |
Copy all controllers, their parameters and values. Group name, permissions and ownerships are not copied. All existing controllers in the source group are discarded.
dst | Destination group. |
src | Source group. |
@dst: Destination control group @src: Source from which values will be copied to dst
Create a duplicate copy of src in dst. This will be useful for those who that intend to create new instances based on an existing control group
int cgroup_create_cgroup | ( | struct cgroup * | cgrp, |
int | ignore_ownership ) |
Physically create a control group in kernel. The group is created in all hierarchies, which cover controllers added by cgroup_add_controller(). All parameters set by cgroup_add_value_* functions are written. The created groups has owner which was set by cgroup_set_uid_gid() and permissions set by cgroup_set_permissions.
cgrp | |
ignore_ownership | When nozero, all errors are ignored when setting owner of the group and/or its tasks file. |
ECGROUPNOTEQUAL | if not all specified controller parameters were successfully set. |
cgroup_create_cgroup creates a new control group. struct cgroup *cgrp: The control group to be created
returns 0 on success. We recommend calling cg_delete_cgroup if this routine fails. That should do the cleanup operation. If ECGCANTSETVALUE is returned, the group was created successfully but not all controller parameters were successfully set.
int cgroup_create_cgroup_from_parent | ( | struct cgroup * | cgrp, |
int | ignore_ownership ) |
Physically create new control group in kernel, with all parameters and values copied from its parent group. The group is created in all hierarchies, where the parent group exists. I.e. following code creates subgroup in all hierarchies, because all of them have root (=parent) group.
cgrp | The cgroup to create. Only it's name is used, everything else is discarded. |
ignore_ownership | When nozero, all errors are ignored when setting owner of the group and/or its tasks file. |
ECGROUPNOTEQUAL | if not all inherited controller parameters were successfully set (this is expected). |
@cgrp: cgroup data structure to be filled with parent values and then passed down for creation @ignore_ownership: Ignore doing a chown on the newly created cgroup
int cgroup_delete_cgroup | ( | struct cgroup * | cgrp, |
int | ignore_migration ) |
Physically remove a control group from kernel. The group is removed from all hierarchies, which cover controllers added by cgroup_add_controller() or cgroup_get_cgroup(). All tasks inside the group are automatically moved to parent group.
The group being removed must be empty, i.e. without subgroups. Use cgroup_delete_cgroup_ext() for recursive delete.
cgrp | |
ignore_migration | When nozero, all errors are ignored when migrating tasks from the group to the parent group. |
cgroup_delete cgroup deletes a control group. struct cgroup *cgrp takes the group which is to be deleted.
returns 0 on success.
int cgroup_delete_cgroup_ext | ( | struct cgroup * | cgrp, |
int | flags ) |
Physically remove a control group from kernel. All tasks are automatically moved to parent group. If CGFLAG_DELETE_IGNORE_MIGRATION flag is used, the errors that occurred during the task movement are ignored. CGFLAG_DELETE_RECURSIVE flag specifies that all subgroups should be removed too. If root group is being removed with this flag specified, all subgroups are removed but the root group itself is left undeleted.
cgrp | |
flags | Combination of CGFLAG_DELETE_* flags, which indicate what and how to delete. |
void cgroup_free | ( | struct cgroup ** | cgrp | ) |
Free internal cgroup
structure. This function frees also all controllers attached to the cgroup
, including all parameters and their values.
cgrp |
void cgroup_free_controllers | ( | struct cgroup * | cgrp | ) |
Free internal list of controllers from the group.
cgrp |
int cgroup_get_cgroup | ( | struct cgroup * | cgrp | ) |
Read all information regarding the group from kernel. Based on name of the group, list of controllers and all parameters and their values are read from all hierarchies, where a group with given name exists. All existing controllers are replaced. I.e. following code will fill root
with controllers from all hierarchies, because the root group is available in all of them.
cgrp | The cgroup to load. Only it's name is used, everything else is replaced. |
char * cgroup_get_cgroup_name | ( | struct cgroup * | cgrp | ) |
Get the name of the cgroup from a given cgroup
cgrp | The cgroup whose name is needed |
struct cgroup_controller * cgroup_get_controller | ( | struct cgroup * | cgrp, |
const char * | name ) |
Return appropriate controller from given group. The controller must be added before using cgroup_add_controller() or loaded from kernel using cgroup_get_cgroup().
cgrp | |
name | The name of the controller, e.g. "freezer". |
struct cgroup_controller * cgroup_get_controller_by_index | ( | struct cgroup * | cgrp, |
int | index ) |
Return requested controller from given group
cgrp | |
index | The index into the cgroup controller list |
int cgroup_get_controller_count | ( | struct cgroup * | cgrp | ) |
Return the number of controllers for the specified cgroup in libcgroup internal structures.
cgrp |
char * cgroup_get_controller_name | ( | struct cgroup_controller * | controller | ) |
Given a controller pointer, get the name of the controller
controller |
int cgroup_get_controller_version | ( | const char *const | controller, |
enum cg_version_t *const | version ) |
Get the cgroup version of a controller. Version is set to CGROUP_UNK if the version cannot be determined.
controller | The controller of interest |
version | The version of the controller |
int cgroup_get_procs | ( | const char * | name, |
const char * | controller, | ||
pid_t ** | pids, | ||
int * | size ) |
Get the list of process in a cgroup. This list is guaranteed to be sorted. It is not necessary that it is unique.
name | The name of the cgroup |
controller | The name of the controller |
pids | The list of pids. Should be uninitialized when passed to the API. Should be freed by the caller using free. |
size | The size of the pids array returned by the API. |
int cgroup_get_threads | ( | const char * | name, |
const char * | controller, | ||
pid_t ** | pids, | ||
int * | size ) |
Get the list of threads in a cgroup. This list is guaranteed to be sorted. It is not necessary that it is unique.
name | The name of the cgroup |
controller | The name of the controller |
pids | The list of pids. Should be uninitialized when passed to the API. Should be freed by the caller using free. |
size | The size of the pids array returned by the API. |
int cgroup_get_uid_gid | ( | struct cgroup * | cgrp, |
uid_t * | tasks_uid, | ||
gid_t * | tasks_gid, | ||
uid_t * | control_uid, | ||
gid_t * | control_gid ) |
Return owners of the group's tasks
file and control files. The data is read from libcgroup
internal cgroup
structure, use cgroup_set_uid_gid() or cgroup_get_cgroup() to fill it.
int cgroup_get_value_bool | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
bool * | value ) |
Read a parameter value from libcgroup
internal structures. Use cgroup_get_cgroup()
to fill these structures with data from kernel.
controller | |
name | The name of the parameter. |
value |
int cgroup_get_value_int64 | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
int64_t * | value ) |
Read a parameter value from libcgroup
internal structures. Use cgroup_get_cgroup()
to fill these structures with data from kernel.
controller | |
name | The name of the parameter. |
value |
char * cgroup_get_value_name | ( | struct cgroup_controller * | controller, |
int | index ) |
Return the name of parameter of controller at given index. The index goes from 0 to cgroup_get_value_name_count()-1. Use this function to list all parameter of the controller.
libcgroup
structure, do not free it.controller | |
index | The index of the parameter. |
int cgroup_get_value_name_count | ( | struct cgroup_controller * | controller | ) |
Return the number of variables for the specified controller in libcgroup
internal structures. Use cgroup_get_cgroup() to fill these structures with data from kernel. Use this function together with cgroup_get_value_name() to list all parameters of a group.
controller |
int cgroup_get_value_string | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
char ** | value ) |
Read a parameter value from libcgroup
internal structures. Use cgroup_get_cgroup()
to fill these structures with data from kernel. It's up to the caller to free returned value.
This function works only for 'short' parameters. Use cgroup_read_stats_begin(), cgroup_read_stats_next() and cgroup_read_stats_end() to read stats
parameter, which can be longer than libcgroup's internal buffers.
controller | |
name | The name of the parameter. |
value |
int cgroup_get_value_uint64 | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
u_int64_t * | value ) |
Read a parameter value from libcgroup
internal structures. Use cgroup_get_cgroup()
to fill these structures with data from kernel.
controller | |
name | The name of the parameter. |
value |
int cgroup_list_mount_points | ( | const enum cg_version_t | cgrp_version, |
char *** | mount_paths ) |
List the mount paths, that matches the specified version
cgrp_version | The cgroup type/version |
mount_paths | Holds the list of mount paths |
int cgroup_modify_cgroup | ( | struct cgroup * | cgrp | ) |
Physically modify a control group in kernel. All parameters added by cgroup_add_value_ or cgroup_set_value_ are written. Currently it's not possible to change and owner of a group.
cgrp |
cgroup_modify_cgroup modifies the cgroup control files. struct cgroup *cgrp: The name will be the cgroup to be modified. The values will be the values to be modified, those not mentioned in the structure will not be modified.
The uids cannot be modified yet.
returns 0 on success.
struct cgroup * cgroup_new_cgroup | ( | const char * | name | ) |
Allocate new cgroup structure. This function itself does not create new control group in kernel, only new struct cgroup
inside libcgroup!
name | Path to the group, relative from root group. Use "/" or "." for the root group itself and "/foo/bar/baz" or "foo/bar/baz" for subgroups. |
void cgroup_set_permissions | ( | struct cgroup * | cgrp, |
mode_t | control_dperm, | ||
mode_t | control_fperm, | ||
mode_t | task_fperm ) |
Stores given file permissions of the group's control and tasks files into the cgroup
data structure. Use NO_PERMS if permissions shouldn't be changed or a value which applicable to chmod(2). Please note that the given permissions are masked with the file owner's permissions. For example if a control file has permissions 640 and control_fperm is 471 the result will be 460.
cgrp | |
control_dperm | Directory permission for the group. |
control_fperm | File permission for the control files. |
task_fperm | File permissions for task file. |
int cgroup_set_uid_gid | ( | struct cgroup * | cgrp, |
uid_t | tasks_uid, | ||
gid_t | tasks_gid, | ||
uid_t | control_uid, | ||
gid_t | control_gid ) |
Set owner of the group control files and the tasks
file. This function modifies only libcgroup
internal cgroup
structure, use cgroup_create_cgroup() afterwards to create the group with given owners.
cgrp | |
tasks_uid | UID of the owner of group's tasks file. |
tasks_gid | GID of the owner of group's tasks file. |
control_uid | UID of the owner of group's control files (i.e. parameters). |
control_gid | GID of the owner of group's control files (i.e. parameters). |
int cgroup_set_value_bool | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
bool | value ) |
Set a parameter value in libcgroup
internal structures. Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
controller | |
name | The name of the parameter. |
value |
int cgroup_set_value_int64 | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
int64_t | value ) |
Set a parameter value in libcgroup
internal structures. Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel. Content of the value is copied to internal structures and is not needed after return from the function.
controller | |
name | The name of the parameter. |
value |
int cgroup_set_value_string | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
const char * | value ) |
Set a parameter value in libcgroup
internal structures. Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
controller | |
name | The name of the parameter. |
value |
int cgroup_set_value_uint64 | ( | struct cgroup_controller * | controller, |
const char * | name, | ||
u_int64_t | value ) |
Set a parameter value in libcgroup
internal structures. Use cgroup_modify_cgroup() or cgroup_create_cgroup() to write it to kernel.
controller | |
name | The name of the parameter. |
value |
enum cg_setup_mode_t cgroup_setup_mode | ( | void | ) |
Get the current group setup mode (legacy/unified/hybrid)
Finds the current cgroup setup mode (legacy/unified/hybrid). Returns unknown of failure and setup mode on success.
bool is_cgroup_mode_hybrid | ( | void | ) |
Return true if cgroup setup mode is cgroup v1/v2 (hybrid), else returns false.
bool is_cgroup_mode_legacy | ( | void | ) |
Return true if cgroup setup mode is cgroup v1 (legacy), else returns false.
bool is_cgroup_mode_unified | ( | void | ) |
Return true if cgroup setup mode is cgroup v2 (unified), else returns false.