linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libgpiod][PATCH 1/2] gpiosim: get the process name using prctl()
@ 2022-12-07  9:14 Bartosz Golaszewski
  2022-12-07  9:14 ` [libgpiod][PATCH 2/2] bindings: python: tests: set the process name Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2022-12-07  9:14 UTC (permalink / raw)
  To: Kent Gibson, Linus Walleij, Andy Shevchenko, Viresh Kumar
  Cc: linux-gpio, Bartosz Golaszewski

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

program_invocation_short_name is set once at the program's start. If we
change the process name using prctl(), we need to retrieve it using the
same system call as program_invocation_short_name will not be updated.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 configure.ac            |  2 ++
 tests/gpiosim/gpiosim.c | 10 +++++++---
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index dc945ef..07706f0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -85,10 +85,12 @@ AC_CHECK_FUNC([scandir], [], [FUNC_NOT_FOUND_LIB([scandir])])
 AC_CHECK_FUNC([alphasort], [], [FUNC_NOT_FOUND_LIB([alphasort])])
 AC_CHECK_FUNC([ppoll], [], [FUNC_NOT_FOUND_LIB([ppoll])])
 AC_CHECK_FUNC([realpath], [], [FUNC_NOT_FOUND_LIB([realpath])])
+AC_CHECK_FUNC([prctl], [], [FUNC_NOT_FOUND_LIB([prctl])])
 AC_CHECK_HEADERS([getopt.h], [], [HEADER_NOT_FOUND_LIB([getopt.h])])
 AC_CHECK_HEADERS([dirent.h], [], [HEADER_NOT_FOUND_LIB([dirent.h])])
 AC_CHECK_HEADERS([sys/poll.h], [], [HEADER_NOT_FOUND_LIB([sys/poll.h])])
 AC_CHECK_HEADERS([sys/sysmacros.h], [], [HEADER_NOT_FOUND_LIB([sys/sysmacros.h])])
+AC_CHECK_HEADERS([sys/prctl.h], [], [HEADER_NOT_FOUND_LIB([sys/prctl.h])])
 AC_CHECK_HEADERS([linux/version.h], [], [HEADER_NOT_FOUND_LIB([linux/version.h])])
 AC_CHECK_HEADERS([linux/const.h], [], [HEADER_NOT_FOUND_LIB([linux/const.h])])
 AC_CHECK_HEADERS([linux/ioctl.h], [], [HEADER_NOT_FOUND_LIB([linux/ioctl.h])])
diff --git a/tests/gpiosim/gpiosim.c b/tests/gpiosim/gpiosim.c
index 881ecc8..e2a97fa 100644
--- a/tests/gpiosim/gpiosim.c
+++ b/tests/gpiosim/gpiosim.c
@@ -12,6 +12,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mount.h>
+#include <sys/prctl.h>
 #include <sys/random.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -365,11 +366,14 @@ out_unref_kmod:
 
 static char *configfs_make_item(int at, int id)
 {
-	char *item_name;
+	char *item_name, prname[17];
 	int ret;
 
-	ret = asprintf(&item_name, "%s.%u.%d",
-		       program_invocation_short_name, getpid(), id);
+	ret = prctl(PR_GET_NAME, prname);
+	if (ret)
+		return NULL;
+
+	ret = asprintf(&item_name, "%s.%u.%d", prname, getpid(), id);
 	if (ret < 0)
 		return NULL;
 
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [libgpiod][PATCH 2/2] bindings: python: tests: set the process name
  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
  2022-12-07 10:57   ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2022-12-07  9:14 UTC (permalink / raw)
  To: Kent Gibson, Linus Walleij, Andy Shevchenko, Viresh Kumar
  Cc: linux-gpio, Bartosz Golaszewski

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [libgpiod][PATCH 2/2] bindings: python: tests: set the process name
  2022-12-07  9:14 ` [libgpiod][PATCH 2/2] bindings: python: tests: set the process name Bartosz Golaszewski
@ 2022-12-07 10:57   ` Andy Shevchenko
  2022-12-07 11:17     ` Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2022-12-07 10:57 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Kent Gibson, Linus Walleij, Viresh Kumar, linux-gpio,
	Bartosz Golaszewski

On Wed, Dec 07, 2022 at 10:14:13AM +0100, Bartosz Golaszewski wrote:
> 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.

Perhaps you can refer to the [1] to explain why it's needed in Python.
Also it can be done via ctype module [2][3] or via module [4], although
I haven't checked if most used distros pack it.

[1]: https://bugs.python.org/issue5672
[2]: https://stackoverflow.com/a/923034
[3]: https://stackoverflow.com/a/31134436
[4]: https://github.com/dvarrazzo/py-setproctitle

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [libgpiod][PATCH 2/2] bindings: python: tests: set the process name
  2022-12-07 10:57   ` Andy Shevchenko
@ 2022-12-07 11:17     ` Bartosz Golaszewski
  0 siblings, 0 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2022-12-07 11:17 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Kent Gibson, Linus Walleij, Viresh Kumar, linux-gpio,
	Bartosz Golaszewski

On Wed, 7 Dec 2022 at 11:57, Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Wed, Dec 07, 2022 at 10:14:13AM +0100, Bartosz Golaszewski wrote:
> > 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.
>
> Perhaps you can refer to the [1] to explain why it's needed in Python.
> Also it can be done via ctype module [2][3] or via module [4], although
> I haven't checked if most used distros pack it.
>
> [1]: https://bugs.python.org/issue5672

Sounds good, thanks. I'll add it as Link:.

> [2]: https://stackoverflow.com/a/923034
> [3]: https://stackoverflow.com/a/31134436

We're already using cpython C extensions so let's not mix multiple
different FFIs.

> [4]: https://github.com/dvarrazzo/py-setproctitle

I was aware of this module, just don't want to pull third-party
requirements for python bindings if I don't need to.

Fun fact: the above C extension is a modified version of the code
generated by ChatGPT[1].

Bart

[1] https://imgur.com/a/SuDnyuO

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-12-07 11:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-07  9:14 [libgpiod][PATCH 1/2] gpiosim: get the process name using prctl() Bartosz Golaszewski
2022-12-07  9:14 ` [libgpiod][PATCH 2/2] bindings: python: tests: set the process name Bartosz Golaszewski
2022-12-07 10:57   ` Andy Shevchenko
2022-12-07 11:17     ` Bartosz Golaszewski

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).