From ed4a94558768b232010568303ac67df4f9f1c3cd Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 16 Oct 2011 14:21:41 +0200 Subject: [PATCH] Better personlity function. Replace including libc by the ctypes module and rather use a proper Python <--> C function for that. --- po/pakfire.pot | 2 +- python/pakfire/chroot.py | 17 ++++++----------- python/pakfire/util.py | 2 +- python/src/_pakfiremodule.c | 7 +++++++ python/src/util.c | 22 ++++++++++++++++++++++ python/src/util.h | 1 + 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/po/pakfire.pot b/po/pakfire.pot index 7a55bcb92..3907b616e 100644 --- a/po/pakfire.pot +++ b/po/pakfire.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2011-10-16 12:45+0200\n" +"POT-Creation-Date: 2011-10-16 14:09+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/python/pakfire/chroot.py b/python/pakfire/chroot.py index 78543d1f6..6001f2ea4 100644 --- a/python/pakfire/chroot.py +++ b/python/pakfire/chroot.py @@ -19,18 +19,16 @@ # # ############################################################################### -import ctypes import fcntl import os import select import subprocess import time -from errors import * +from _pakfire import PERSONALITY_LINUX, PERSONALITY_LINUX32 -_libc = ctypes.cdll.LoadLibrary(None) -_libc.personality.argtypes = [ctypes.c_ulong] -_libc.personality.restype = ctypes.c_int +import pakfire.util as util +from errors import * def logOutput(fds, logger, returnOutput=1, start=0, timeout=0): output="" @@ -151,10 +149,9 @@ class ChildPreExec(object): Return personality value if supported. Otherwise return None. """ - # taken from sys/personality.h personality_defs = { - "linux64": 0x0000, - "linux32": 0x0008, + "linux64": PERSONALITY_LINUX, + "linux32": PERSONALITY_LINUX32, } try: @@ -168,9 +165,7 @@ class ChildPreExec(object): # Set new personality if we got one. if self.personality: - res = _libc.personality(self.personality) - if res == -1: - raise OSError, "Could not set personality" + util.personality(self.personality) # Change into new root. if self.chrootPath: diff --git a/python/pakfire/util.py b/python/pakfire/util.py index 58c34a2ab..638b72e3a 100644 --- a/python/pakfire/util.py +++ b/python/pakfire/util.py @@ -40,7 +40,7 @@ from constants import * from i18n import _ # Import binary version of version_compare and capability functions -from _pakfire import version_compare, get_capabilities, set_capabilities +from _pakfire import version_compare, get_capabilities, set_capabilities, personality def cli_is_interactive(): """ diff --git a/python/src/_pakfiremodule.c b/python/src/_pakfiremodule.c index d5e63db4d..b5c22128f 100644 --- a/python/src/_pakfiremodule.c +++ b/python/src/_pakfiremodule.c @@ -20,6 +20,8 @@ #include +#include + #include "capabilities.h" #include "config.h" #include "pool.h" @@ -38,6 +40,7 @@ static PyMethodDef pakfireModuleMethods[] = { {"version_compare", (PyCFunction)version_compare, METH_VARARGS, NULL}, {"get_capabilities", (PyCFunction)get_capabilities, METH_VARARGS, NULL}, {"set_capabilities", (PyCFunction)set_capabilities, METH_VARARGS, NULL}, + {"personality", (PyCFunction)_personality, METH_VARARGS, NULL}, { NULL, NULL, 0, NULL } }; @@ -261,6 +264,10 @@ void init_pakfire(void) { // Add constants d = PyModule_GetDict(m); + // Personalities + PyDict_SetItemString(d, "PERSONALITY_LINUX", Py_BuildValue("i", PER_LINUX)); + PyDict_SetItemString(d, "PERSONALITY_LINUX32", Py_BuildValue("i", PER_LINUX32)); + // Add constants for relations PyDict_SetItemString(d, "REL_EQ", Py_BuildValue("i", REL_EQ)); PyDict_SetItemString(d, "REL_LT", Py_BuildValue("i", REL_LT)); diff --git a/python/src/util.c b/python/src/util.c index f6e0757ad..d3ee3a224 100644 --- a/python/src/util.c +++ b/python/src/util.c @@ -20,8 +20,30 @@ #include +#include + #include "util.h" +PyObject *_personality(PyObject *self, PyObject *args) { + unsigned long persona; + int ret = 0; + + if (!PyArg_ParseTuple(args, "l", &persona)) { + /* XXX raise exception */ + return NULL; + } + + /* Change personality here. */ + ret = personality(persona); + + if (ret < 0) { + PyErr_SetString(PyExc_RuntimeError, "Could not set personality."); + return NULL; + } + + return Py_BuildValue("i", ret); +} + PyObject *version_compare(PyObject *self, PyObject *args) { Pool *pool; const char *evr1, *evr2; diff --git a/python/src/util.h b/python/src/util.h index e97b56b03..d0a7e0985 100644 --- a/python/src/util.h +++ b/python/src/util.h @@ -25,6 +25,7 @@ #include +extern PyObject *_personality(PyObject *self, PyObject *args); extern PyObject *version_compare(PyObject *self, PyObject *args); #endif -- 2.39.5