]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: execute: Automatically set personality from arch
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 12 Jan 2021 16:31:54 +0000 (16:31 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 12 Jan 2021 16:31:54 +0000 (16:31 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
configure.ac
src/_pakfire/_pakfiremodule.c
src/_pakfire/util.c
src/_pakfire/util.h
src/libpakfire/arch.c
src/libpakfire/execute.c
src/libpakfire/include/pakfire/arch.h
src/libpakfire/libpakfire.sym
src/pakfire/builder.py
src/pakfire/shell.py
src/pakfire/util.py

index d602813285a6d78cce632875a7e4c42baa181236..2c14addd8141846be14211a8483a2bfb843b614a 100644 (file)
@@ -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
index 31a91b792dcd137666ffaf486713ea15f3e49910..1ea54086bcb81e149bb007be456d3da42b190b81 100644 (file)
@@ -23,7 +23,6 @@
 #include <libintl.h>
 #include <locale.h>
 #include <sched.h>
-#include <sys/personality.h>
 
 #include <solv/solver.h>
 
@@ -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));
index 222c6b87b8480aa49547c39dfe7a26d25ab4ae5b..d9095f321fd9f8bde15c449ae9b24117ed1e1b95 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <errno.h>
 #include <sched.h>
-#include <sys/personality.h>
 #include <time.h>
 #include <unistd.h>
 
 #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();
index bb7a814f33aba19497feef512cd38bfac1b41ac2..98a736910c600fcae196a885eec2e8de4087dbc5 100644 (file)
@@ -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);
index 100c4548178b2df34d2204f41cb0831f9325af9c..09d1eb148ba4ece87f17a484bcc5b08323f26d67 100644 (file)
@@ -22,6 +22,7 @@
 #include <stddef.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/personality.h>
 #include <sys/utsname.h>
 
 #include <pakfire/arch.h>
@@ -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";
index 7c5120549587157180d45dff40bae764e5b49030..c300bd625062ad7ef17db4780ed0e9da1eeb293b 100644 (file)
 #include <errno.h>
 #include <sched.h>
 #include <stdlib.h>
+#include <sys/personality.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
+#include <pakfire/arch.h>
 #include <pakfire/execute.h>
 #include <pakfire/logging.h>
 #include <pakfire/private.h>
@@ -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);
 
index 6df436b6814722ede51aee778123445348eecabf..cda93781e61b194bc31b0d0e88d6303ddd1f77c2 100644 (file)
@@ -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);
index b11a5b27a6fd4db89f7ff3fa413d8d29c8de25d6..2455e7a0f8832aebd3fdccd735bcc473fa7f83e4 100644 (file)
@@ -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;
index c05ff7a8c0a068e8d029b6515c054f922be4542c..ad77d57697e4f0485dc1b917297b7a949fad6809 100644 (file)
@@ -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
 
index 2a4d8955e81f2e6d2d1031603b11b6f859f5ef90..2c337601341d70898b2ea80e6eb8bf8acbec178d 100644 (file)
@@ -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)
index a414e3642bf8ebeaf6f94cd695be9e3b9a06251c..f0dfd6f536c004ad9893996283b48c0860e307ca 100644 (file)
@@ -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():
        """