From: Michael Tremer Date: Tue, 12 Jan 2021 16:31:54 +0000 (+0000) Subject: libpakfire: execute: Automatically set personality from arch X-Git-Tag: 0.9.28~1285^2~889 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9dd163f4f44667b01a9e79aaed37ba50ce08d7f1;p=pakfire.git libpakfire: execute: Automatically set personality from arch Signed-off-by: Michael Tremer --- diff --git a/configure.ac b/configure.ac index d60281328..2c14addd8 100644 --- a/configure.ac +++ b/configure.ac @@ -126,12 +126,14 @@ AC_CHECK_HEADERS([ \ stdlib.h \ string.h \ syslog.h \ + sys/personality.h \ sys/stat.h \ unistd.h ]) AC_CHECK_FUNCS([ \ assert \ + personality \ secure_getenv \ strcmp \ strdup diff --git a/src/_pakfire/_pakfiremodule.c b/src/_pakfire/_pakfiremodule.c index 31a91b792..1ea54086b 100644 --- a/src/_pakfire/_pakfiremodule.c +++ b/src/_pakfire/_pakfiremodule.c @@ -23,7 +23,6 @@ #include #include #include -#include #include @@ -72,7 +71,6 @@ static PyMethodDef pakfireModuleMethods[] = { {"performance_index", (PyCFunction)performance_index, METH_VARARGS, NULL}, {"version_compare", (PyCFunction)version_compare, METH_VARARGS, NULL}, {"get_capabilities", (PyCFunction)get_capabilities, METH_VARARGS, NULL}, - {"personality", (PyCFunction)_personality, METH_VARARGS, NULL}, {"sync", (PyCFunction)_sync, METH_NOARGS, NULL}, {"unshare", (PyCFunction)_unshare, METH_VARARGS, NULL}, {"native_arch", (PyCFunction)_pakfire_native_arch, METH_NOARGS, NULL }, @@ -210,10 +208,6 @@ PyMODINIT_FUNC PyInit__pakfire(void) { // Add constants PyObject* d = PyModule_GetDict(module); - // Personalities - PyDict_SetItemString(d, "PERSONALITY_LINUX", Py_BuildValue("i", PER_LINUX)); - PyDict_SetItemString(d, "PERSONALITY_LINUX32", Py_BuildValue("i", PER_LINUX32)); - // Namespace stuff PyDict_SetItemString(d, "SCHED_CLONE_NEWIPC", Py_BuildValue("i", CLONE_NEWIPC)); PyDict_SetItemString(d, "SCHED_CLONE_NEWPID", Py_BuildValue("i", CLONE_NEWPID)); diff --git a/src/_pakfire/util.c b/src/_pakfire/util.c index 222c6b87b..d9095f321 100644 --- a/src/_pakfire/util.c +++ b/src/_pakfire/util.c @@ -22,7 +22,6 @@ #include #include -#include #include #include @@ -34,26 +33,6 @@ #include "package.h" #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 *_sync(PyObject *self, PyObject *args) { /* Just sync everything to disks. */ sync(); diff --git a/src/_pakfire/util.h b/src/_pakfire/util.h index bb7a814f3..98a736910 100644 --- a/src/_pakfire/util.h +++ b/src/_pakfire/util.h @@ -28,7 +28,6 @@ #include "pakfire.h" -extern PyObject *_personality(PyObject *self, PyObject *args); extern PyObject *_sync(PyObject *self, PyObject *args); extern PyObject *_unshare(PyObject *self, PyObject *args); extern PyObject *version_compare(PyObject *self, PyObject *args); diff --git a/src/libpakfire/arch.c b/src/libpakfire/arch.c index 100c45481..09d1eb148 100644 --- a/src/libpakfire/arch.c +++ b/src/libpakfire/arch.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -33,6 +34,7 @@ struct pakfire_arch { const char* name; const char* platform; const char* compatible[5]; + unsigned long personality; }; static const struct pakfire_arch PAKFIRE_ARCHES[] = { @@ -41,40 +43,48 @@ static const struct pakfire_arch PAKFIRE_ARCHES[] = { .name = "x86_64", .platform = "x86", .compatible = { "i686", NULL }, + .personality = PER_LINUX, }, { .name = "i686", .platform = "x86", + .personality = PER_LINUX32, }, // ARM { .name = "aarch64", .platform = "arm", + .personality = PER_LINUX, }, { .name = "armv7hl", .platform = "arm", .compatible = { "armv7l", "armv6l", "armv5tejl", "armv5tel", NULL }, + .personality = PER_LINUX32, }, { .name = "armv7l", .platform = "arm", .compatible = { "armv6l", "armv5tejl", "armv5tel", NULL }, + .personality = PER_LINUX32, }, { .name = "armv6l", .platform = "arm", .compatible = { "armv5tejl", "armv5tel", NULL }, + .personality = PER_LINUX32, }, { .name = "armv5tejl", .platform = "arm", .compatible = { "armv5tel", NULL }, + .personality = PER_LINUX32, }, { .name = "armv5tel", .platform = "arm", + .personality = PER_LINUX32, }, }; @@ -109,6 +119,15 @@ PAKFIRE_EXPORT const char* pakfire_arch_platform(const char* name) { return NULL; } +PAKFIRE_EXPORT unsigned long pakfire_arch_personality(const char* name) { + const struct pakfire_arch* arch = pakfire_arch_find(name); + + if (arch) + return arch->personality; + + return 0; +} + PAKFIRE_EXPORT char* pakfire_arch_machine(const char* arch, const char* vendor) { if (!vendor) vendor = "unknown"; diff --git a/src/libpakfire/execute.c b/src/libpakfire/execute.c index 7c5120549..c300bd625 100644 --- a/src/libpakfire/execute.c +++ b/src/libpakfire/execute.c @@ -21,10 +21,12 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -41,6 +43,9 @@ struct pakfire_execute_env { static int pakfire_execute_fork(Pakfire pakfire, struct pakfire_execute_env* env) { pid_t pid = getpid(); + // Get architecture + const char* arch = pakfire_get_arch(pakfire); + DEBUG(env->pakfire, "Execution environment has been forked as PID %d\n", pid); DEBUG(env->pakfire, " command = %s, root = %s\n", env->command, env->root); @@ -52,6 +57,15 @@ static int pakfire_execute_fork(Pakfire pakfire, struct pakfire_execute_env* env return errno; } + // Set personality + unsigned long persona = pakfire_arch_personality(arch); + r = personality(persona); + if (r < 0) { + ERROR(env->pakfire, "Could not set personality (%x)\n", persona); + + return errno; + } + // exec() command r = execve(env->command, (char**)env->argv, (char**)env->envp); diff --git a/src/libpakfire/include/pakfire/arch.h b/src/libpakfire/include/pakfire/arch.h index 6df436b68..cda93781e 100644 --- a/src/libpakfire/include/pakfire/arch.h +++ b/src/libpakfire/include/pakfire/arch.h @@ -23,6 +23,7 @@ int pakfire_arch_supported(const char* name); const char* pakfire_arch_platform(const char* name); +unsigned long pakfire_arch_personality(const char* name); char* pakfire_arch_machine(const char* arch, const char* vendor); const char* pakfire_arch_native(); int pakfire_arch_is_compatible(const char* name, const char* compatible_arch); diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index b11a5b27a..2455e7a0f 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -46,6 +46,7 @@ global: pakfire_arch_is_compatible; pakfire_arch_machine; pakfire_arch_native; + pakfire_arch_personality; pakfire_arch_platform; pakfire_arch_supported; pakfire_arch_supported_by_host; diff --git a/src/pakfire/builder.py b/src/pakfire/builder.py index c05ff7a8c..ad77d5769 100644 --- a/src/pakfire/builder.py +++ b/src/pakfire/builder.py @@ -517,10 +517,6 @@ class BuilderContext(object): command = "/usr/sbin/chroot %s %s %s" % (self.chrootPath(), SHELL_SCRIPT) - # Add personality if we require one - if self.pakfire.distro.personality: - command = "%s %s" % (self.pakfire.distro.personality, command) - for key, val in list(self.environ.items()): command = "%s=\"%s\" " % (key, val) + command diff --git a/src/pakfire/shell.py b/src/pakfire/shell.py index 2a4d8955e..2c3376013 100644 --- a/src/pakfire/shell.py +++ b/src/pakfire/shell.py @@ -25,14 +25,12 @@ import select import subprocess import time -from ._pakfire import PERSONALITY_LINUX, PERSONALITY_LINUX32 - from pakfire.i18n import _ import pakfire.util as util from .errors import * class ShellExecuteEnvironment(object): - def __init__(self, command, cwd=None, chroot_path=None, personality=None, shell=False, timeout=0, env=None, + def __init__(self, command, cwd=None, chroot_path=None, shell=False, timeout=0, env=None, cgroup=None, logger=None, log_output=True, log_errors=True, record_output=False, record_stdout=True, record_stderr=True): # The given command that should be executed. self.command = command @@ -49,9 +47,6 @@ class ShellExecuteEnvironment(object): # Set timeout. self.timeout = timeout - # Personality. - self.personality = personality - # Shell. self.shell = shell self.env = env @@ -135,7 +130,7 @@ class ShellExecuteEnvironment(object): def create_subprocess(self): # Create preexecution thingy for command - preexec_fn = ChildPreExec(self.personality, self.chroot_path, self.cwd) + preexec_fn = ChildPreExec(self.chroot_path, self.cwd) kwargs = { "bufsize" : 0, @@ -249,35 +244,14 @@ class ShellExecuteEnvironment(object): class ChildPreExec(object): - def __init__(self, personality, chroot_path, cwd): - self._personality = personality + def __init__(self, chroot_path, cwd): self.chroot_path = chroot_path self.cwd = cwd - @property - def personality(self): - """ - Return personality value if supported. - Otherwise return None. - """ - personality_defs = { - "linux64": PERSONALITY_LINUX, - "linux32": PERSONALITY_LINUX32, - } - - try: - return personality_defs[self._personality] - except KeyError: - pass - def __call__(self, *args, **kargs): # Set a new process group os.setpgrp() - # Set new personality if we got one. - if self.personality: - util.personality(self.personality) - # Change into new root. if self.chroot_path: os.chdir(self.chroot_path) diff --git a/src/pakfire/util.py b/src/pakfire/util.py index a414e3642..f0dfd6f53 100644 --- a/src/pakfire/util.py +++ b/src/pakfire/util.py @@ -35,7 +35,7 @@ from .constants import * from .i18n import _ # Import binary version of capability functions -from ._pakfire import get_capabilities, personality +from ._pakfire import get_capabilities def cli_is_interactive(): """