1
0
Fork 0
mirror of https://github.com/vbatts/go-cgroup.git synced 2024-11-23 17:15:39 +00:00

cg: halfway to golint

This commit is contained in:
Vincent Batts 2016-11-29 10:06:09 -05:00
parent c2855cec9b
commit 727ac7a93b

227
cg.go
View file

@ -16,10 +16,8 @@ import (
"unsafe" "unsafe"
) )
/* // Cgroup is the structure describing one or more control groups. The structure
Structure describing one or more control groups. The structure is opaque to // is opaque to applications.
applications.
*/
type Cgroup struct { type Cgroup struct {
g *C.struct_cgroup g *C.struct_cgroup
} }
@ -117,7 +115,7 @@ func (cg Cgroup) Modify() error {
} }
/* /*
Physically remove a control group from kernel. The group is removed from Delete removes a control group from kernel. The group is removed from
all hierarchies, which cover controllers added by Cgroup.AddController() all hierarchies, which cover controllers added by Cgroup.AddController()
or GetCgroup(). All tasks inside the group are automatically moved or GetCgroup(). All tasks inside the group are automatically moved
to parent group. to parent group.
@ -132,14 +130,14 @@ func (cg Cgroup) Delete() error {
} }
/* /*
Same as Delete(), but ignores errors when migrating. DeleteIgnoreMigration is the same as Delete(), but ignores errors when migrating.
*/ */
func (cg Cgroup) DeleteIgnoreMigration() error { func (cg Cgroup) DeleteIgnoreMigration() error {
return _err(C.cgroup_delete_cgroup(cg.g, C.int(1))) return _err(C.cgroup_delete_cgroup(cg.g, C.int(1)))
} }
/* /*
Physically remove a control group from kernel. DeleteExt removes a control group from kernel.
All tasks are automatically moved to parent group. All tasks are automatically moved to parent group.
If DeleteIgnoreMigration flag is used, the errors that occurred If DeleteIgnoreMigration flag is used, the errors that occurred
during the task movement are ignored. during the task movement are ignored.
@ -152,7 +150,7 @@ func (cg Cgroup) DeleteExt(flags DeleteFlag) error {
} }
/* /*
Read all information regarding the group from kernel. Get reads all information regarding the group from kernel.
Based on name of the group, list of controllers and all parameters and their 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. 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 All existing controllers are replaced. I.e. following code will fill root with
@ -168,48 +166,48 @@ func (cg Cgroup) Get() error {
return _err(C.cgroup_get_cgroup(cg.g)) return _err(C.cgroup_get_cgroup(cg.g))
} }
type UID C.uid_t type (
type GID C.gid_t UID C.uid_t
GID C.gid_t
)
/* /*
Set owner of the group control files and the @c tasks file. This function SetUIDGID sets owner of the group control files and the @c tasks file. This function
modifies only libcgroup internal cgroup structure, use modifies only libcgroup internal cgroup structure, use
Cgroup.Create() afterwards to create the group with given owners. Cgroup.Create() afterwards to create the group with given owners.
@param cgroup @param cgroup
@param tasks_uid UID of the owner of group's @c tasks file. @param tasksUID UID of the owner of group's @c tasks file.
@param tasks_gid GID of the owner of group's @c tasks file. @param tasksGID GID of the owner of group's @c tasks file.
@param control_uid UID of the owner of group's control files (i.e. @param controlUID UID of the owner of group's control files (i.e.
parameters). parameters).
@param control_gid GID of the owner of group's control files (i.e. @param controlGID GID of the owner of group's control files (i.e.
parameters). parameters).
*/ */
func (cg Cgroup) SetUidGid(tasks_uid UID, tasks_gid GID, func (cg Cgroup) SetUIDGID(tasksUID UID, tasksGID GID,
control_uid UID, control_gid GID) error { controlUID UID, controlGID GID) error {
return _err(C.cgroup_set_uid_gid(cg.g, return _err(C.cgroup_set_uid_gid(cg.g,
C.uid_t(tasks_uid), C.gid_t(tasks_gid), C.uid_t(tasksUID), C.gid_t(tasksGID),
C.uid_t(control_uid), C.gid_t(control_gid))) C.uid_t(controlUID), C.gid_t(controlGID)))
} }
/* // GetUIDGID returns owners of the group's @c tasks file and control files.
Return owners of the group's @c tasks file and control files. // The data is read from libcgroup internal cgroup structure, use
The data is read from libcgroup internal cgroup structure, use // Cgroup.SetUIDGID() or Cgroup.Get() to fill it.
Cgroup.SetUidGid() or Cgroup.Get() to fill it. func (cg Cgroup) GetUIDGID() (tasksUID UID, tasksGID GID, controlUID UID, controlGID GID, err error) {
*/
func (cg Cgroup) GetUidGid() (tasks_uid UID, tasks_gid GID, control_uid UID, control_gid GID, err error) {
var ( var (
c_t_u C.uid_t cTU C.uid_t
c_t_g C.gid_t cTG C.gid_t
c_c_u C.uid_t cCU C.uid_t
c_c_g C.gid_t cCG C.gid_t
) )
err = _err(C.cgroup_set_uid_gid(cg.g, err = _err(C.cgroup_set_uid_gid(cg.g,
c_t_u, cTU,
c_t_g, cTG,
c_c_u, cCU,
c_c_g)) cCG))
return UID(c_t_u), GID(c_t_g), UID(c_c_u), GID(c_c_g), err return UID(cTU), GID(cTG), UID(cCU), GID(cCG), err
} }
@ -224,7 +222,7 @@ const (
type Mode C.mode_t type Mode C.mode_t
/* /*
Stores given file permissions of the group's control and tasks files SetPermissions stores given file permissions of the group's control and tasks files
into the cgroup data structure. Use NO_PERMS if permissions shouldn't 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 be changed or a value which applicable to chmod(2). Please note that
the given permissions are masked with the file owner's permissions. the given permissions are masked with the file owner's permissions.
@ -243,42 +241,33 @@ func (cg Cgroup) SetPermissions(control_dperm, control_fperm, task_fperm Mode) {
C.mode_t(control_fperm), C.mode_t(task_fperm)) C.mode_t(control_fperm), C.mode_t(task_fperm))
} }
/* // CopyCgroup copies all controllers, parameters and their values. All existing
Copy all controllers, parameters and their values. All existing controllers // controllers in the source group are discarded.
in the source group are discarded.
*/
func CopyCgroup(src, dest Cgroup) error { func CopyCgroup(src, dest Cgroup) error {
return _err(C.cgroup_copy_cgroup(src.g, dest.g)) return _err(C.cgroup_copy_cgroup(src.g, dest.g))
} }
/* // CompareCgroup compares names, owners, controllers, parameters and values of two groups.
Compare names, owners, controllers, parameters and values of two groups. //
// Return value of:
Return value of: // * nil - a and b are equal
* nil - a and b are equal // * ErrGroupNotEqual - groups are not equal
* ECGROUPNOTEQUAL - groups are not equal // * ErrControllerNotEqual - controllers are not equal
* ECGCONTROLLERNOTEQUAL - controllers are not equal
*/
func CompareCgroup(a, b Cgroup) error { func CompareCgroup(a, b Cgroup) error {
return _err(C.cgroup_compare_cgroup(a.g, b.g)) return _err(C.cgroup_compare_cgroup(a.g, b.g))
} }
/* // Controller is the structure describing a controller attached to one struct
Structure describing a controller attached to one struct @c cgroup, including // @c cgroup, including parameters of the group and their values. The structure
parameters of the group and their values. The structure is opaque to // is opaque to applications.
applications.
*/
type Controller struct { type Controller struct {
c *C.struct_cgroup_controller c *C.struct_cgroup_controller
} }
/* // AddValueString adds parameter and its value to internal libcgroup
Add parameter and its value to internal libcgroup structures. // structures. Use Cgroup.Modify() or Cgroup.Create() to write it to kernel.
Use Cgroup.Modify() or Cgroup.Create() to write it to kernel. //
// Name of the parameter and its value
Name of the parameter and its value
*/
func (c Controller) AddValueString(name, value string) error { func (c Controller) AddValueString(name, value string) error {
return _err(C.cgroup_add_value_string(c.c, C.CString(name), C.CString(value))) return _err(C.cgroup_add_value_string(c.c, C.CString(name), C.CString(value)))
} }
@ -291,9 +280,8 @@ func (c Controller) AddValueBool(name string, value bool) error {
return _err(C.cgroup_add_value_bool(c.c, C.CString(name), C.bool(value))) return _err(C.cgroup_add_value_bool(c.c, C.CString(name), C.bool(value)))
} }
/* // GetValueString fetches the values from the controller. Use Cgroup.Get() to
Use Cgroup.Get() to fill these values with data from the kernel // get the names available to fetch values from the kernel.
*/
func (c Controller) GetValueString(name string) (value string, err error) { func (c Controller) GetValueString(name string) (value string, err error) {
var v *C.char var v *C.char
err = _err(C.cgroup_get_value_string(c.c, C.CString(name), &v)) err = _err(C.cgroup_get_value_string(c.c, C.CString(name), &v))
@ -333,35 +321,30 @@ func (c Controller) SetValueBool(name string, value bool) error {
} }
/* /*
Compare names, parameters and values of two controllers. CompareControllers compares names, parameters and values of two controllers.
Return value of: Return value of:
* nil - a and b are equal * nil - a and b are equal
* ECGCONTROLLERNOTEQUAL - controllers are not equal * ErrControllerNotEqual - controllers are not equal
*/ */
func CompareControllers(a, b Controller) error { func CompareControllers(a, b Controller) error {
return _err(C.cgroup_compare_controllers(a.c, b.c)) return _err(C.cgroup_compare_controllers(a.c, b.c))
} }
/* // Init initializes libcgroup. Information about mounted hierarchies are
Initialize libcgroup. Information about mounted hierarchies are examined // examined and cached internally (just what's mounted where, not the groups
and cached internally (just what's mounted where, not the groups themselves). // themselves).
*/
func Init() error { func Init() error {
return _err(C.cgroup_init()) return _err(C.cgroup_init())
} }
/* // LoadConfig file and mount and create control groups described there.
Load configuration file and mount and create control groups described there. // See cgconfig.conf(5) 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)))
} }
/* // Unload deletes 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())
} }
@ -369,27 +352,25 @@ func Unload() error {
type DeleteFlag int type DeleteFlag int
const ( const (
// Ignore errors caused by migration of tasks to parent group. // DeleteIgnoreMigration 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. // DeleteRecursive recursively delete all child groups.
DeleteRecursive = DeleteFlag(C.CGFLAG_DELETE_RECURSIVE) DeleteRecursive = DeleteFlag(C.CGFLAG_DELETE_RECURSIVE)
/* // DeleteEmptyOnly deletes the cgroup only if it is empty, i.e. it has no
Delete the cgroup only if it is empty, i.e. it has no subgroups and // 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 UnloadFromConfig deletes all cgroups and unmount all mount points defined in
file. specified config 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
on given flags. Mount point are always umounted only if they are empty, given flags. Mount point are always umounted only if they are empty, regardless
regardless of any flags. 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).
@ -399,7 +380,7 @@ func UnloadFromConfig(filename string, flags DeleteFlag) error {
} }
/* /*
Sets default permissions of groups created by subsequent SetDefault permissions of groups created by subsequent
cgroup_config_load_config() calls. If a config file contains a 'default {}' cgroup_config_load_config() calls. If a config file contains a 'default {}'
section, the default permissions from the config file is then used. section, the default permissions from the config file is then used.
@ -417,21 +398,47 @@ func SetDefault(cg Cgroup) error {
return _err(C.cgroup_config_set_default(cg.g)) return _err(C.cgroup_config_set_default(cg.g))
} }
// FileInfo is the Information about found cgroup directory (= a control group).
type FileInfo struct { type FileInfo struct {
Type FileType fileType FileType
Path string path string
Parent string parent string
FullPath string fullPath string
Depth int8 depth int8
}
// FileType of this cgroup's
func (fi FileInfo) FileType() FileType {
return fi.fileType
}
// Path to this cgroup
func (fi FileInfo) Path() string {
return fi.path
}
// Parent of this cgroup
func (fi FileInfo) Parent() string {
return fi.parent
}
// FullPath to this cgroup
func (fi FileInfo) FullPath() string {
return fi.fullPath
}
// Depth of this cgroup
func (fi FileInfo) Depth() int8 {
return fi.depth
} }
func fromCFileInfo(cData C.struct_cgroup_file_info) FileInfo { func fromCFileInfo(cData C.struct_cgroup_file_info) FileInfo {
return FileInfo{ return FileInfo{
Type: FileType(C.type_from_file_info(cData)), fileType: FileType(C.type_from_file_info(cData)),
Path: C.GoString(cData.path), path: C.GoString(cData.path),
Parent: C.GoString(cData.parent), parent: C.GoString(cData.parent),
FullPath: C.GoString(cData.full_path), fullPath: C.GoString(cData.full_path),
Depth: int8(cData.depth), depth: int8(cData.depth),
} }
} }
@ -503,7 +510,7 @@ func GetAllControllers() (controllers []ControllerData, err error) {
for { for {
err = _err(C.cgroup_get_all_controller_next(&handle, &cd)) err = _err(C.cgroup_get_all_controller_next(&handle, &cd))
if err != nil { if err != nil {
if err == ECGEOF { if err == ErrEOF {
break break
} }
@ -524,17 +531,15 @@ func GetSubSysMountPoint(controller string) (string, error) {
return C.GoString(mp), nil return C.GoString(mp), nil
} }
// Various errors
var ( var (
// End-of-file for iterators ErrEOF = errors.New(C.GoString(C.cgroup_strerror(C.ECGEOF)))
ECGEOF = errors.New(C.GoString(C.cgroup_strerror(C.ECGEOF))) ErrOTHER = errors.New(C.GoString(C.cgroup_strerror(C.ECGOTHER)))
ECGOTHER = errors.New(C.GoString(C.cgroup_strerror(C.ECGOTHER))) ErrGroupNotEqual = errors.New(C.GoString(C.cgroup_strerror(C.ECGROUPNOTEQUAL)))
ECGROUPNOTEQUAL = errors.New(C.GoString(C.cgroup_strerror(C.ECGROUPNOTEQUAL))) ErrControllerNotEqual = errors.New(C.GoString(C.cgroup_strerror(C.ECGCONTROLLERNOTEQUAL)))
ECGCONTROLLERNOTEQUAL = errors.New(C.GoString(C.cgroup_strerror(C.ECGCONTROLLERNOTEQUAL)))
) )
/* // LastError returns last errno, which caused ErrOTHER 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())
} }
@ -544,13 +549,13 @@ func _err(num C.int) error {
case 0: case 0:
return nil return nil
case C.ECGEOF: case C.ECGEOF:
return ECGEOF return ErrEOF
case C.ECGOTHER: case C.ECGOTHER:
return ECGOTHER return ErrOTHER
case C.ECGROUPNOTEQUAL: case C.ECGROUPNOTEQUAL:
return ECGROUPNOTEQUAL return ErrGroupNotEqual
case C.ECGCONTROLLERNOTEQUAL: case C.ECGCONTROLLERNOTEQUAL:
return ECGCONTROLLERNOTEQUAL return ErrControllerNotEqual
} }
// There's a lot. We'll create them as they come // There's a lot. We'll create them as they come
return errors.New(C.GoString(C.cgroup_strerror(num))) return errors.New(C.GoString(C.cgroup_strerror(num)))