mirror of
https://github.com/vbatts/go-cgroup.git
synced 2024-11-27 10:55:40 +00:00
more progress
This commit is contained in:
parent
8a1807ae2f
commit
eec1be6a82
1 changed files with 84 additions and 42 deletions
110
cg.go
110
cg.go
|
@ -17,8 +17,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Structure describing one or more control groups. The structure is opaque to
|
Structure describing one or more control groups. The structure is opaque to
|
||||||
* applications.
|
applications.
|
||||||
*/
|
*/
|
||||||
type Cgroup struct {
|
type Cgroup struct {
|
||||||
g *C.struct_group
|
g *C.struct_group
|
||||||
|
@ -28,7 +28,7 @@ func NewCgroup(name string) Cgroup {
|
||||||
cg := Cgroup{
|
cg := Cgroup{
|
||||||
C.cgroup_new_cgroup(C.CString(name)),
|
C.cgroup_new_cgroup(C.CString(name)),
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(cg, freeCgroup)
|
runtime.SetFinalizer(cg, freeCgroupThings)
|
||||||
return cg
|
return cg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,37 +43,64 @@ func (cg Cgroup) GetController(name string) Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func freeCgroupThings(cg Cgroup) {
|
||||||
|
freeCgroup(cg)
|
||||||
|
freeControllers(cg)
|
||||||
|
}
|
||||||
|
|
||||||
func freeCgroup(cg Cgroup) {
|
func freeCgroup(cg Cgroup) {
|
||||||
C.cgroup_free(&cg.g)
|
C.cgroup_free(&cg.g)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func freeControllers(cg Cgroup) {
|
||||||
|
C.cgroup_free_controllers(cg.g)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Structure describing a controller attached to one struct @c cgroup, including
|
Physically create a control group in kernel. The group is created in all
|
||||||
* parameters of the group and their values. The structure is opaque to
|
hierarchies, which cover controllers added by Cgroup.AddController().
|
||||||
* applications.
|
|
||||||
|
TODO correct docs for golang implementation
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
func CreateGroup(cg Cgroup, ignore_ownership bool) error {
|
||||||
|
var i int = 0
|
||||||
|
if ignore_ownership == true {
|
||||||
|
i = 1
|
||||||
|
}
|
||||||
|
return _err(C.cgroup_create_cgroup(cg.g, C.int(i)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Structure describing a controller attached to one struct @c cgroup, including
|
||||||
|
parameters of the group and their values. The structure is opaque to
|
||||||
|
applications.
|
||||||
*/
|
*/
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
c *C.struct_cgroup_controller
|
c *C.struct_cgroup_controller
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Initialize libcgroup. Information about mounted hierarchies are examined
|
Initialize libcgroup. Information about mounted hierarchies are examined
|
||||||
* and cached internally (just what's mounted where, not the groups themselves).
|
and cached internally (just what's mounted where, not the groups themselves).
|
||||||
*/
|
*/
|
||||||
func Init() error {
|
func Init() error {
|
||||||
return _err(C.cgroup_init())
|
return _err(C.cgroup_init())
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Load configuration file and mount and create control groups described there.
|
Load configuration file and mount and create control groups described there.
|
||||||
* See cgconfig.conf man page for format of the file.
|
See cgconfig.conf man page for format of the file.
|
||||||
*/
|
*/
|
||||||
func LoadConfig(filename string) error {
|
func LoadConfig(filename string) error {
|
||||||
return _err(C.cgroup_config_load_config(C.CString(filename)))
|
return _err(C.cgroup_config_load_config(C.CString(filename)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Delete all control groups and unmount all hierarchies.
|
Delete all control groups and unmount all hierarchies.
|
||||||
*/
|
*/
|
||||||
func Unload() error {
|
func Unload() error {
|
||||||
return _err(C.cgroup_unload_cgroups())
|
return _err(C.cgroup_unload_cgroups())
|
||||||
|
@ -82,39 +109,54 @@ func Unload() error {
|
||||||
type DeleteFlag int
|
type DeleteFlag int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
/**
|
// Ignore errors caused by migration of tasks to parent group.
|
||||||
* Ignore errors caused by migration of tasks to parent group.
|
|
||||||
*/
|
|
||||||
DeleteIgnoreMigration = DeleteFlag(C.CGFLAG_DELETE_IGNORE_MIGRATION)
|
DeleteIgnoreMigration = DeleteFlag(C.CGFLAG_DELETE_IGNORE_MIGRATION)
|
||||||
|
|
||||||
/**
|
// Recursively delete all child groups.
|
||||||
* Recursively delete all child groups.
|
|
||||||
*/
|
|
||||||
DeleteRecursive = DeleteFlag(C.CGFLAG_DELETE_RECURSIVE)
|
DeleteRecursive = DeleteFlag(C.CGFLAG_DELETE_RECURSIVE)
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Delete the cgroup only if it is empty, i.e. it has no subgroups and
|
Delete the cgroup only if it is empty, i.e. it has no subgroups and
|
||||||
* no processes inside. This flag cannot be used with
|
no processes inside. This flag cannot be used with
|
||||||
* DeleteRecursive
|
DeleteRecursive
|
||||||
*/
|
*/
|
||||||
DeleteEmptyOnly = DeleteFlag(C.CGFLAG_DELETE_EMPTY_ONLY)
|
DeleteEmptyOnly = DeleteFlag(C.CGFLAG_DELETE_EMPTY_ONLY)
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Delete all cgroups and unmount all mount points defined in specified config
|
Delete all cgroups and unmount all mount points defined in specified config
|
||||||
* file.
|
file.
|
||||||
*
|
|
||||||
* The groups are either removed recursively or only the empty ones, based
|
The groups are either removed recursively or only the empty ones, based
|
||||||
* on given flags. Mount point are always umounted only if they are empty,
|
on given flags. Mount point are always umounted only if they are empty,
|
||||||
* regardless of any flags.
|
regardless of any flags.
|
||||||
*
|
|
||||||
* The groups are sorted before they are removed, so the removal of empty ones
|
The groups are sorted before they are removed, so the removal of empty ones
|
||||||
* actually works (i.e. subgroups are removed first).
|
actually works (i.e. subgroups are removed first).
|
||||||
*/
|
*/
|
||||||
func UnloadFromConfig(filename string, flags DeleteFlag) error {
|
func UnloadFromConfig(filename string, flags DeleteFlag) error {
|
||||||
return _err(C.cgroup_config_unload_config(C.CString(filename), C.int(flags)))
|
return _err(C.cgroup_config_unload_config(C.CString(filename), C.int(flags)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Sets default permissions of groups created by subsequent
|
||||||
|
cgroup_config_load_config() calls. If a config file contains a 'default {}'
|
||||||
|
section, the default permissions from the config file is then used.
|
||||||
|
|
||||||
|
Use cgroup_new_cgroup() to create a dummy group and cgroup_set_uid_gid() and
|
||||||
|
cgroup_set_permissions() to set its permissions. Use NO_UID_GID instead of
|
||||||
|
GID/UID and NO_PERMS instead of file/directory permissions to let kernel
|
||||||
|
decide the default permissions where you don't want specific user and/or
|
||||||
|
permissions. Kernel then uses current user/group and permissions from umask
|
||||||
|
then.
|
||||||
|
|
||||||
|
New default permissions from this group are copied to libcgroup internal
|
||||||
|
structures.
|
||||||
|
*/
|
||||||
|
func SetDefault(cg Cgroup) error {
|
||||||
|
return _err(C.cgroup_config_set_default(cg.g))
|
||||||
|
}
|
||||||
|
|
||||||
type FileInfo struct {
|
type FileInfo struct {
|
||||||
Type FileType
|
Type FileType
|
||||||
Path string
|
Path string
|
||||||
|
@ -207,7 +249,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return last errno, which caused ECGOTHER error.
|
Return last errno, which caused ECGOTHER error.
|
||||||
*/
|
*/
|
||||||
func LastError() error {
|
func LastError() error {
|
||||||
return _err(C.cgroup_get_last_errno())
|
return _err(C.cgroup_get_last_errno())
|
||||||
|
|
Loading…
Reference in a new issue