From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx> When we run the test suite by calling the __main__ function of the module, the name of the process as visible in the system becomes "python". Let's set it to "python-gpiod" before running the tests. This way gpiosim will name its configfs attributes appropriately. Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx> --- bindings/python/setup.py | 8 +++++ bindings/python/tests/Makefile.am | 2 +- bindings/python/tests/__main__.py | 4 +++ bindings/python/tests/procname/Makefile.am | 6 ++++ bindings/python/tests/procname/__init__.py | 4 +++ bindings/python/tests/procname/ext.c | 42 ++++++++++++++++++++++ configure.ac | 1 + 7 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 bindings/python/tests/procname/Makefile.am create mode 100644 bindings/python/tests/procname/__init__.py create mode 100644 bindings/python/tests/procname/ext.c diff --git a/bindings/python/setup.py b/bindings/python/setup.py index 7ad5de3..a951069 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -27,9 +27,17 @@ gpiosim_ext = Extension( extra_compile_args=["-Wall", "-Wextra"], ) +procname_ext = Extension( + "tests.procname._ext", + sources=["tests/procname/ext.c"], + define_macros=[("_GNU_SOURCE", "1")], + extra_compile_args=["-Wall", "-Wextra"], +) + extensions = [gpiod_ext] if "GPIOD_WITH_TESTS" in environ and environ["GPIOD_WITH_TESTS"] == "1": extensions.append(gpiosim_ext) + extensions.append(procname_ext) with open("gpiod/version.py", "r") as fd: exec(fd.read()) diff --git a/bindings/python/tests/Makefile.am b/bindings/python/tests/Makefile.am index 7dcdebb..c89241e 100644 --- a/bindings/python/tests/Makefile.am +++ b/bindings/python/tests/Makefile.am @@ -1,7 +1,7 @@ # SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@xxxxxxxx> -SUBDIRS = gpiosim +SUBDIRS = gpiosim procname EXTRA_DIST = \ helpers.py \ diff --git a/bindings/python/tests/__main__.py b/bindings/python/tests/__main__.py index b5d7f0a..cc39c29 100644 --- a/bindings/python/tests/__main__.py +++ b/bindings/python/tests/__main__.py @@ -13,4 +13,8 @@ from .tests_line_settings import * from .tests_module import * from .tests_line_request import * +from . import procname + +procname.set_process_name("python-gpiod") + unittest.main() diff --git a/bindings/python/tests/procname/Makefile.am b/bindings/python/tests/procname/Makefile.am new file mode 100644 index 0000000..c4a8fd5 --- /dev/null +++ b/bindings/python/tests/procname/Makefile.am @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@xxxxxxxx> + +EXTRA_DIST = \ + ext.c \ + __init__.py diff --git a/bindings/python/tests/procname/__init__.py b/bindings/python/tests/procname/__init__.py new file mode 100644 index 0000000..af6abdd --- /dev/null +++ b/bindings/python/tests/procname/__init__.py @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@xxxxxxxx> + +from ._ext import set_process_name diff --git a/bindings/python/tests/procname/ext.c b/bindings/python/tests/procname/ext.c new file mode 100644 index 0000000..bf7d2fe --- /dev/null +++ b/bindings/python/tests/procname/ext.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +// SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@xxxxxxxx> + +#include <Python.h> +#include <sys/prctl.h> + +static PyObject * +module_set_process_name(PyObject *Py_UNUSED(self), PyObject *args) +{ + const char *name; + int ret; + + ret = PyArg_ParseTuple(args, "s", &name); + if (!ret) + return NULL; + + ret = prctl(PR_SET_NAME, name); + if (ret) + return PyErr_SetFromErrno(PyExc_OSError); + + Py_RETURN_NONE; +} + +static PyMethodDef module_methods[] = { + { + .ml_name = "set_process_name", + .ml_meth = (PyCFunction)module_set_process_name, + .ml_flags = METH_VARARGS, + }, + { } +}; + +static PyModuleDef module_def = { + PyModuleDef_HEAD_INIT, + .m_name = "procname._ext", + .m_methods = module_methods, +}; + +PyMODINIT_FUNC PyInit__ext(void) +{ + return PyModule_Create(&module_def); +} diff --git a/configure.ac b/configure.ac index 07706f0..1c8f192 100644 --- a/configure.ac +++ b/configure.ac @@ -268,6 +268,7 @@ AC_CONFIG_FILES([Makefile bindings/python/examples/Makefile bindings/python/tests/Makefile bindings/python/tests/gpiosim/Makefile + bindings/python/tests/procname/Makefile bindings/rust/libgpiod-sys/src/Makefile bindings/rust/libgpiod-sys/Makefile bindings/rust/libgpiod/src/Makefile -- 2.37.2