linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Kent Gibson <warthog618@gmail.com>,
	Linus Walleij <linus.walleij@linaro.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Viresh Kumar <viresh.kumar@linaro.org>
Cc: linux-gpio@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: [libgpiod][PATCH 2/2] bindings: python: tests: set the process name
Date: Wed,  7 Dec 2022 10:14:13 +0100	[thread overview]
Message-ID: <20221207091413.61616-2-brgl@bgdev.pl> (raw)
In-Reply-To: <20221207091413.61616-1-brgl@bgdev.pl>

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

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@linaro.org>
---
 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@bgdev.pl>
 
-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@bgdev.pl>
+
+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@bgdev.pl>
+
+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@bgdev.pl>
+
+#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


  reply	other threads:[~2022-12-07  9:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-07  9:14 [libgpiod][PATCH 1/2] gpiosim: get the process name using prctl() Bartosz Golaszewski
2022-12-07  9:14 ` Bartosz Golaszewski [this message]
2022-12-07 10:57   ` [libgpiod][PATCH 2/2] bindings: python: tests: set the process name Andy Shevchenko
2022-12-07 11:17     ` Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221207091413.61616-2-brgl@bgdev.pl \
    --to=brgl@bgdev.pl \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=viresh.kumar@linaro.org \
    --cc=warthog618@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).