libcgroup
Loading...
Searching...
No Matches
4. Manipulation with tasks

Simple task assignment

Applications can use following functions to simply put a task into given control group and find a groups where given tasks is.

int cgroup_attach_task (struct cgroup *cgrp)
 
int cgroup_attach_task_pid (struct cgroup *cgrp, pid_t tid)
 
int cgroup_change_cgroup_path (const char *path, pid_t pid, const char *const controllers[])
 
int cgroup_get_current_controller_path (pid_t pid, const char *controller, char **current_path)
 

Rules

libcgroup can move tasks to control groups using simple rules, loaded from configuration file. See cgrules.conf man page to see format of the file. Following functions can be used to load these rules from a file.

int cgroup_init_rules_cache (void)
 
int cgroup_reload_cached_rules (void)
 
void cgroup_print_rules_config (FILE *fp)
 

Rule based task assignment

libcgroup can move tasks to control groups using simple rules, loaded from configuration file. See cgrules.conf man page to see format of the file. Applications can move tasks to control groups based on these rules using following functions.

int cgroup_change_all_cgroups (void)
 
int cgroup_change_cgroup_flags (uid_t uid, gid_t gid, const char *procname, pid_t pid, int flags)
 
int cgroup_change_cgroup_uid_gid_flags (uid_t uid, gid_t gid, pid_t pid, int flags)
 
int cgroup_change_cgroup_uid_gid (uid_t uid, gid_t gid, pid_t pid)
 

Communication with cgrulesengd daemon

Users can use cgrulesengd daemon to move tasks to groups based on the rules automatically when they change their UID, GID or executable name. The daemon allows tasks to be 'sticky', i.e. all rules are ignored for these tasks and the daemon never moves them.

int cgroup_register_unchanged_process (pid_t pid, int flags)
 
int cgroup_attach_thread_tid (struct cgroup *cgroup, pid_t tid)
 

Detailed Description

Function Documentation

◆ cgroup_attach_task()

int cgroup_attach_task ( struct cgroup * cgroup)

Move current task (=thread) to given control group.

Parameters
cgrpDestination control group.

cgroup_attach_task is used to attach the current thread to a cgroup. struct cgroup *cgroup: The cgroup to assign the current thread to.

See cg_attach_task_pid for return values.

◆ cgroup_attach_task_pid()

int cgroup_attach_task_pid ( struct cgroup * cgroup,
pid_t tid )

Move given task (=thread) to given control group.

Parameters
cgrpDestination control group.
tidThe task to move.

cgroup_attach_task_pid is used to assign tasks to a cgroup. struct cgroup *cgroup: The cgroup to assign the thread to. pid_t tid: The thread to be assigned to the cgroup.

returns 0 on success. returns ECGROUPNOTOWNER if the caller does not have access to the cgroup. returns ECGROUPNOTALLOWED for other causes of failure.

◆ cgroup_attach_thread_tid()

int cgroup_attach_thread_tid ( struct cgroup * cgroup,
pid_t tid )

Move given threads (=thread) to given control group.

Parameters
cgroupDestination control group.
tidThe task to move.

cgroup_attach_thread_tid is used to assign threads to a cgroup. struct cgroup *cgroup: The cgroup to assign the thread to. pid_t tid: The thread to be assigned to the cgroup.

returns 0 on success. returns ECGROUPNOTOWNER if the caller does not have access to the cgroup. returns ECGROUPNOTALLOWED for other causes of failure.

◆ cgroup_change_all_cgroups()

int cgroup_change_all_cgroups ( void )

Changes the cgroup of all running PIDs based on the rules in the config file. If a rules exists for a PID, then the PID is placed in the correct group.

This function may be called after creating new control groups to move running PIDs into the newly created control groups.

Returns
0 on success, < 0 on error

◆ cgroup_change_cgroup_flags()

int cgroup_change_cgroup_flags ( uid_t uid,
gid_t gid,
const char * procname,
pid_t pid,
int flags )

Changes the cgroup of a program based on the rules in the config file. If a rule exists for the given UID, GID or PROCESS NAME, then the given PID is placed into the correct group. By default, this function parses the configuration file each time it is called.

The flags can alter the behavior of this function: CGFLAG_USECACHE: Use cached rules instead of parsing the config file CGFLAG_USE_TEMPLATE_CACHE: Use cached templates instead of parsing the config file

This function may NOT be thread safe.

Parameters
uidThe UID to match.
gidThe GID to match.
procnameThe PROCESS NAME to match.
pidThe PID of the process to move.
flagsBit flags to change the behavior, as defined in enum cgflags.
Todo
Determine thread-safeness and fix of not safe.

◆ cgroup_change_cgroup_path()

int cgroup_change_cgroup_path ( const char * dest,
pid_t pid,
const char *const controllers[] )

Changes the cgroup of a task based on the path provided. In this case, the user must already know into which cgroup the task should be placed and no rules will be parsed.

Parameters
pathName of the destination group.
pidThe task to move.
controllersList of controllers.
Todo
should this function be really public?

Changes the cgroup of a program based on the path provided. In this case, the user must already know into which cgroup the task should be placed and no rules will be parsed.

returns 0 on success.

◆ cgroup_change_cgroup_uid_gid()

int cgroup_change_cgroup_uid_gid ( uid_t uid,
gid_t gid,
pid_t pid )

Provides backwards-compatibility with older versions of the API. This function is deprecated, and cgroup_change_cgroup_uid_gid_flags() should be used instead. In fact, this function simply calls the newer one with flags set to 0 (none).

Parameters
uidThe UID to match.
gidThe GID to match.
pidThe PID of the process to move.

Provides backwards-compatibility with older versions of the API. This function is deprecated, and cgroup_change_cgroup_uid_gid_flags() should be used instead. In fact, this function simply calls the newer one with flags set to 0 (none).

Parameters
uidThe UID to match
gidThe GID to match
pidThe PID of the process to move
Returns
0 on success, > 0 on error

◆ cgroup_change_cgroup_uid_gid_flags()

int cgroup_change_cgroup_uid_gid_flags ( uid_t uid,
gid_t gid,
pid_t pid,
int flags )

Changes the cgroup of a program based on the rules in the config file. If a rule exists for the given UID or GID, then the given PID is placed into the correct group. By default, this function parses the configuration file each time it is called.

This function may NOT be thread safe.

Parameters
uidThe UID to match.
gidThe GID to match.
pidThe PID of the process to move.
flagsBit flags to change the behavior, as defined in enum cgflags.
Todo
Determine thread-safeness and fix if not safe.

◆ cgroup_get_current_controller_path()

int cgroup_get_current_controller_path ( pid_t pid,
const char * controller,
char ** current_path )

Get the current control group path where the given task is.

Parameters
pidThe task to find.
controllerThe controller (hierarchy), where to find the task.
current_pathThe path to control group, where the task has been found. The patch is relative to the root of the hierarchy. The caller must free this memory.

cgroup_get_current_controller_path @pid: pid of the current process for which the path is to be determined @controller: name of the controller for which to determine current path @current_path: a pointer that is filled with the value of the current path as seen in /proc/<pid>/cgroup

◆ cgroup_init_rules_cache()

int cgroup_init_rules_cache ( void )

Initializes the rules cache and load it from /etc/cgrules.conf.

Todo
add parameter with the filename?

Initializes the rules cache.

Returns
0 on success, > 0 on error

◆ cgroup_print_rules_config()

void cgroup_print_rules_config ( FILE * fp)

Print the cached rules table. This function should be called only after first calling cgroup_parse_config(), but it will work with an empty rule list.

Parameters
fpDestination file, where the rules will be printed.

Print the cached rules table. This function should be called only after first calling cgroup_parse_config(), but it will work with an empty rule list.

Parameters
fpThe file stream to print to

◆ cgroup_register_unchanged_process()

int cgroup_register_unchanged_process ( pid_t pid,
int flags )

Register the unchanged process to a cgrulesengd daemon. This process is never moved to another control group by the daemon. If the daemon does not work, this function returns 0 as success.

Parameters
pidThe task id.
flagsBit flags to change the behavior, as defined in cgroup_daemon_type

◆ cgroup_reload_cached_rules()

int cgroup_reload_cached_rules ( void )

Reloads the rules list from /etc/cgrules.conf. This function is probably NOT thread safe (calls cgroup_parse_rules_config()).

Reloads the rules list, using the given configuration file. This function is probably NOT thread safe (calls cgroup_parse_rules()).

Returns
0 on success, > 0 on failure