]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
python: Add binding for {get|set}_cgroup_item
authorStéphane Graber <stgraber@ubuntu.com>
Fri, 7 Dec 2012 20:47:11 +0000 (15:47 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Mon, 10 Dec 2012 14:26:07 +0000 (09:26 -0500)
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: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
src/python-lxc/examples/api_test.py.in
src/python-lxc/lxc.c
src/python-lxc/lxc/__init__.py.in

index 0b17bd6ab39a008a871ee636bc3b75e09d9127e0..7711291eed9b535d299d034bbccdc36363ca6a82 100644 (file)
@@ -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()
index b489079b5a51ffa62dff7bc5e70964766e2cc1aa..83e265926a818e530e12394a4cb09e4c8dafbebb 100644 (file)
@@ -203,6 +203,31 @@ Container_freeze(Container *self, PyObject *args, PyObject *kwds)
     Py_RETURN_FALSE;
 }
 
+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)
 {
@@ -210,7 +235,7 @@ Container_get_config_item(Container *self, PyObject *args, PyObject *kwds)
     char* key = NULL;
     int len = 0;
 
-    if (! PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwlist,
+    if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist,
                                       &key))
         Py_RETURN_FALSE;
 
@@ -287,6 +312,24 @@ Container_save_config(Container *self, PyObject *args, PyObject *kwds)
     Py_RETURN_FALSE;
 }
 
+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)
 {
@@ -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"
index 9ef3459f192339c4df2a8f908dd3c0a09362124c..91a59edfa6b3840af48b8236c6cfa136ed08fa5e 100644 (file)
@@ -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.