]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
python/attach: Add function that returns personality for architecture
authorChristian Seiler <christian@iwakd.de>
Sun, 18 Aug 2013 22:52:44 +0000 (00:52 +0200)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Mon, 19 Aug 2013 16:42:08 +0000 (11:42 -0500)
Adds the arch_to_personality function that looks up an architecture
and returns the corresponding personality. This may be used in
conjunction with the attach/attach_wait keyword argument.

Signed-off-by: Christian Seiler <christian@iwakd.de>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/python-lxc/lxc.c
src/python-lxc/lxc/__init__.py

index e0123c0977e0c80be2700ea3d21b2d79b1be716d..8d98050624bdaacf13af0ed1600569297639c147 100644 (file)
@@ -26,6 +26,7 @@
 #include <lxc/lxccontainer.h>
 #include <lxc/utils.h>
 #include <lxc/namespace.h>
+#include <lxc/confile.h>
 #include <stdio.h>
 #include <sys/wait.h>
 
@@ -886,6 +887,35 @@ LXC_attach_run_shell(PyObject *self, PyObject *arg)
     return PyLong_FromLong(rv);
 }
 
+static PyObject *
+LXC_arch_to_personality(PyObject *self, PyObject *arg)
+{
+    long rv = -1;
+    PyObject *pystr;
+    char *str;
+
+    if (!PyUnicode_Check(arg)) {
+        PyErr_SetString(PyExc_ValueError, "Expected a string");
+        return NULL;
+    }
+    
+    pystr = PyUnicode_AsUTF8String(arg);
+    if (!pystr)
+        return NULL;
+    
+    str = PyBytes_AsString(pystr);
+    if (!str)
+        goto out;
+    
+    rv = lxc_config_parse_arch(str);
+    if (rv == -1)
+        PyErr_SetString(PyExc_KeyError, "Failed to lookup architecture.");
+
+out:
+    Py_DECREF(pystr);
+    return rv == -1 ? NULL : PyLong_FromLong(rv);
+}
+
 static PyObject *
 LXC_attach_run_command(PyObject *self, PyObject *arg)
 {
@@ -1140,6 +1170,8 @@ static PyMethodDef LXC_methods[] = {
      "Starts up a shell when attaching, to use as the run parameter for attach or attach_wait"},
     {"attach_run_command", (PyCFunction)LXC_attach_run_command, METH_O,
      "Runs a command when attaching, to use as the run parameter for attach or attach_wait"},
+    {"arch_to_personality", (PyCFunction)LXC_arch_to_personality, METH_O,
+     "Returns the process personality of the corresponding architecture"},
     {"get_default_config_path", (PyCFunction)LXC_get_default_config_path, METH_NOARGS,
      "Returns the current LXC config path"},
     {"get_version", (PyCFunction)LXC_get_version, METH_NOARGS,
index aecbf8cb0bdf449ac064ba2f120a9510f90d4823..2dca4401c3a03004f45d5dcd52181eff3a21f1e6 100644 (file)
@@ -450,6 +450,14 @@ def attach_run_shell():
     """
     return _lxc.attach_run_shell(None)
 
+def arch_to_personality(arch):
+    """
+        Determine the process personality corresponding to the architecture
+    """
+    if isinstance(arch, bytes):
+        arch = str(arch, 'utf-8')
+    return _lxc.arch_to_personality(arch)
+
 # Some constants for attach
 LXC_ATTACH_KEEP_ENV = _lxc.LXC_ATTACH_KEEP_ENV
 LXC_ATTACH_CLEAR_ENV = _lxc.LXC_ATTACH_CLEAR_ENV