From 6f9081ab43d21555c7222f77fcb5c682c859d207 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 17 Mar 2025 11:22:58 +0000 Subject: [PATCH] python: execute: Implement setting the nice value Signed-off-by: Michael Tremer --- src/python/pakfire.c | 15 ++++++++++++++- tests/python/execute.py | 16 ++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/python/pakfire.c b/src/python/pakfire.c index 783ffdfa..6ccbda93 100644 --- a/src/python/pakfire.c +++ b/src/python/pakfire.c @@ -544,15 +544,18 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* PyObject* command = NULL; PyObject* environ = NULL; + int nice = 0; const char* kwlist[] = { "command", "environ", + "nice", NULL, }; // Parse arguments - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", (char**)kwlist, &command, &environ)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi", (char**)kwlist, + &command, &environ, &nice)) goto ERROR; // Check if command is a list @@ -629,6 +632,16 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* goto ERROR; } + // Set the nice level + if (nice) { + r = pakfire_jail_nice(jail, nice); + if (r < 0) { + errno = -r; + PyErr_SetFromErrno(PyExc_OSError); + goto ERROR; + } + } + Py_BEGIN_ALLOW_THREADS // Execute command diff --git a/tests/python/execute.py b/tests/python/execute.py index f3ddad9f..e2f8a268 100755 --- a/tests/python/execute.py +++ b/tests/python/execute.py @@ -92,15 +92,15 @@ class ExecuteTests(tests.TestCase): # Run a command that generates lots of lines self.pakfire.execute(["/command", "lines", "100", "40"]) - #def test_nice(self): - # self.pakfire.execute(["/command", "print-nice"], nice=5) + def test_nice(self): + self.pakfire.execute(["/command", "print-nice"], nice=5) - #def test_nice_invalid_input(self): - # """ - # Tries using an invalid nice value - # """ - # with self.assertRaises(OSError): - # self.pakfire.execute(["/command", "print-nice"], nice=100) + def test_nice_invalid_input(self): + """ + Tries using an invalid nice value + """ + with self.assertRaises(OSError): + self.pakfire.execute(["/command", "print-nice"], nice=100) #def test_check_open_file_descriptors(self): # """ -- 2.39.5