Commit f4d3a9fd by Stéphane Graber

python: Add binding for {get|set}_cgroup_item

Updates the binding for the two new functions. This also fixes some problems with the argument checking of get_config_item that'd otherwise lead to a segfault. The python binding for set_cgroup_item and get_cgroup_item are pretty raw as lxc has little control over the cgroup entries. That means that we don't try to interpret lists as we do for the config entries. Signed-off-by: 's avatarStéphane Graber <stgraber@ubuntu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent 703c562d
......@@ -97,6 +97,13 @@ container.attach("NETWORK|UTSNAME", "/sbin/ifconfig", "eth0")
# A few basic checks of the current state
assert(len(ips) > 0)
## Testing cgroups a bit
print("Testing cgroup API")
max_mem = container.get_cgroup_item("memory.max_usage_in_bytes")
current_limit = container.get_cgroup_item("memory.limit_in_bytes")
assert(container.set_cgroup_item("memory.limit_in_bytes", max_mem))
assert(container.get_cgroup_item("memory.limit_in_bytes") != current_limit)
## Freezing the container
print("Freezing the container")
container.freeze()
......
......@@ -204,13 +204,38 @@ Container_freeze(Container *self, PyObject *args, PyObject *kwds)
}
static PyObject *
Container_get_cgroup_item(Container *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"key", NULL};
char* key = NULL;
int len = 0;
if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist,
&key))
Py_RETURN_FALSE;
len = self->container->get_cgroup_item(self->container, key, NULL, 0);
if (len <= 0) {
Py_RETURN_FALSE;
}
char* value = (char*) malloc(sizeof(char)*len + 1);
if (self->container->get_cgroup_item(self->container, key, value, len + 1) != len) {
Py_RETURN_FALSE;
}
return PyUnicode_FromString(value);
}
static PyObject *
Container_get_config_item(Container *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"key", NULL};
char* key = NULL;
int len = 0;
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwlist,
if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist,
&key))
Py_RETURN_FALSE;
......@@ -288,6 +313,24 @@ Container_save_config(Container *self, PyObject *args, PyObject *kwds)
}
static PyObject *
Container_set_cgroup_item(Container *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"key", "value", NULL};
char *key = NULL;
char *value = NULL;
if (! PyArg_ParseTupleAndKeywords(args, kwds, "ss|", kwlist,
&key, &value))
Py_RETURN_FALSE;
if (self->container->set_cgroup_item(self->container, key, value)) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
static PyObject *
Container_set_config_item(Container *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"key", "value", NULL};
......@@ -441,6 +484,11 @@ static PyMethodDef Container_methods[] = {
"\n"
"Freezes the container and returns its return code."
},
{"get_cgroup_item", (PyCFunction)Container_get_cgroup_item, METH_VARARGS | METH_KEYWORDS,
"get_cgroup_item(key) -> string\n"
"\n"
"Get the current value of a cgroup entry."
},
{"get_config_item", (PyCFunction)Container_get_config_item, METH_VARARGS | METH_KEYWORDS,
"get_config_item(key) -> string\n"
"\n"
......@@ -463,6 +511,11 @@ static PyMethodDef Container_methods[] = {
"Save the container configuration to its default "
"location or to an alternative location if provided."
},
{"set_cgroup_item", (PyCFunction)Container_set_cgroup_item, METH_VARARGS | METH_KEYWORDS,
"set_cgroup_item(key, value) -> boolean\n"
"\n"
"Set a cgroup entry to the provided value."
},
{"set_config_item", (PyCFunction)Container_set_config_item, METH_VARARGS | METH_KEYWORDS,
"set_config_item(key, value) -> boolean\n"
"\n"
......
......@@ -318,6 +318,18 @@ class Container(_lxc.Container):
return False
return True
def get_cgroup_item(self, key):
"""
Returns the value for a given cgroup entry.
A list is returned when multiple values are set.
"""
value = _lxc.Container.get_cgroup_item(self, key)
if value is False:
return False
else:
return value.rstrip("\n")
def get_config_item(self, key):
"""
Returns the value for a given config key.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment