예제 #1
0
    def test_create(self):
        ret = cgroup_init()
        self.assertEqual(0, ret)

        cgroup = cgroup_new_cgroup(b'test')
        self.assertIsNotNone(cgroup)

        uid = os.geteuid()
        gid = os.getegid()
        ret = cgroup_set_uid_gid(cgroup, uid, gid, uid, gid)
        self.assertEqual(0, ret, f'{cgroup_strerror(ret).decode()}')

        cgroup_controller = cgroup_add_controller(cgroup, b'cpuset')
        self.assertIsNotNone(cgroup_controller)

        ret = cgroup_create_cgroup(cgroup, 0)
        self.assertEqual(0, ret, f'{cgroup_strerror(ret).decode()}')

        pid = os.getpid()

        arr_t = c_char_p * 1
        cgroup_register_unchanged_process(pid, DaemonType.UNCHANGE_CHILDREN)
        ret = cgroup_change_cgroup_path(b'test', pid, arr_t(b'cpuset'))
        self.assertEqual(0, ret, f'{cgroup_strerror(ret).decode()}')

        cgroup_free(byref(cgroup))
예제 #2
0
    def from_existing(cls,
                      name_path: Union[os.PathLike, str],
                      auto_delete: bool = False,
                      auto_delete_flag: DeleteFlag = DeleteFlag.NONE) -> CGroup:
        obj = CGroup.__new__(cls)

        obj._path = name_path
        obj._raw_path = str(name_path).encode()

        obj._auto_delete = auto_delete
        obj._auto_delete_flag = auto_delete_flag

        obj._cgroup = cgroup_new_cgroup(obj._raw_path)
        if not obj._cgroup:
            _raise_error(ErrorCode.FAIL)

        ret = cgroup_get_cgroup(obj._cgroup)
        if ret is not 0:
            _raise_error(ret)

        obj._controllers = dict()
        for mount_point in _all_controllers():
            cg_ctrl = cgroup_get_controller(obj._cgroup, mount_point.name)
            if cg_ctrl:
                obj._controllers[mount_point.name] = cg_ctrl

        return obj
예제 #3
0
    def reload(self) -> None:
        cgroup_free(byref(self._cgroup))

        self._cgroup = cgroup_new_cgroup(self._raw_path)
        if not self._cgroup:
            _raise_error(ErrorCode.FAIL)

        ret = cgroup_get_cgroup(self._cgroup)
        if ret is not 0:
            _raise_error(ret)

        for controller in self._controllers.keys():
            cg_ctrl = cgroup_get_controller(self._cgroup, controller)
            if cg_ctrl:
                self._controllers[controller] = cg_ctrl
            else:
                _raise_error(ErrorCode.INVAL)
예제 #4
0
    def delete(self, del_flag: DeleteFlag = DeleteFlag.NONE) -> None:
        if not self._cgroup:
            raise ValueError('This group has already been deleted.')

        cgroup_free(byref(self._cgroup))

        cgroup = cgroup_new_cgroup(self._raw_path)

        for controller in self._controllers:
            cg_ctrl = cgroup_add_controller(cgroup, controller)
            if not cg_ctrl:
                _raise_error(ErrorCode.INVAL)

        ret = cgroup_delete_cgroup_ext(cgroup, del_flag)
        if ret is not 0:
            _raise_error(ret)

        cgroup_free(byref(cgroup))
예제 #5
0
    def __init__(self, name_path: Union[os.PathLike, str], first_controller: str, *controllers: str,
                 dir_mode: int = None, file_mode: int = None, tasks_mode: int = None,
                 t_uid: int = None, t_gid: int = None, a_uid: int = None, a_gid: int = None,
                 ignore_ownership: bool = False,
                 auto_delete: bool = False, auto_delete_flag: DeleteFlag = DeleteFlag.NONE) -> None:
        """

        :param name_path: Control group which should be added (e.g. `cgrp_test/set1`)
        :type name_path: os.PathLike or str
        :param controller: The controller to which the control group to be added belongs (e.g. `cpuset` or `cpu`)
        :type controller: str
        :param dir_mode: Group directory permissions
        :type dir_mode: int
        :param file_mode: Group file permissions
        :type file_mode: int
        :param tasks_mode: Tasks file permissions
        :type tasks_mode: int
        :param t_uid: Owner of the tasks file
        :type t_uid: int
        :param t_gid: Owner group of the tasks file
        :type t_gid: int
        :param a_uid: Owner of the control group and all its files
        :type a_uid: int
        :param a_gid: Owner group of the control group and all its files
        :type a_gid: int
        :param ignore_ownership: When nozero, all errors are ignored
         when setting owner of the group and/or its tasks file.
        :type ignore_ownership: bool
        :param auto_delete: Delete this control group when this object is deleted
        :type auto_delete: bool
        """
        self._path = name_path
        self._raw_path = str(name_path).encode()

        self._auto_delete = auto_delete
        self._auto_delete_flag = auto_delete_flag

        self._cgroup = cgroup_new_cgroup(str(name_path).encode())
        if not self._cgroup:
            _raise_error(ErrorCode.FAIL)

        self._controllers = dict()
        for controller_name in chain((first_controller,), controllers):
            cg_ctrl = cgroup_add_controller(self._cgroup, controller_name.encode())
            if cg_ctrl:
                self._controllers[controller_name.encode()] = cg_ctrl
            else:
                _raise_error(ErrorCode.INVAL)

        # set permission
        if dir_mode is not None or file_mode is not None:
            if dir_mode is None:
                dir_mode = 0o7777
            if file_mode is None:
                file_mode = 0o7777
            if tasks_mode is None:
                tasks_mode = 0o7777
            cgroup_set_permissions(self._cgroup, dir_mode, file_mode, tasks_mode)

        # set ownership
        if t_uid is None:
            t_uid = os.geteuid()
        if t_gid is None:
            t_gid = os.getegid()
        if a_uid is None:
            a_uid = os.geteuid()
        if a_gid is None:
            a_gid = os.getegid()
        ret = cgroup_set_uid_gid(self._cgroup, t_uid, t_gid, a_uid, a_gid)
        if ret is not 0:
            _raise_error(ret)

        # create cgroup
        ret = cgroup_create_cgroup(self._cgroup, ignore_ownership)
        if ret is not 0:
            _raise_error(ret)