gpiosim: get the process name using prctl()
PATCH 1/2 gpiosim: get the process name using prctl()
From: Bartosz Golaszewski <[email protected]>
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.
--- configure.ac | 2 ++ tests/gpiosim/gpiosim.c | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-)
configure.ac +2 -0
@@ -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])])
tests/gpiosim/gpiosim.c +7 -3
@@ -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 ;
Signature · 1 line
2.37.2
PATCH 2/2 bindings: python: tests: set the process name
From: Bartosz Golaszewski <[email protected]>
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.
--- 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
bindings/python/setup.py +8 -0
@@ -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())
bindings/python/tests/Makefile.am +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> -SUBDIRS = gpiosim +SUBDIRS = gpiosim procname EXTRA_DIST = \ helpers.py \
bindings/python/tests/__main__.py +4 -0
@@ -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()
bindings/python/tests/procname/Makefile.am new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> + +EXTRA_DIST = \ + ext.c \ + __init__.py
bindings/python/tests/procname/__init__.py new file +4 -0
@@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> + +from ._ext import set_process_name
bindings/python/tests/procname/ext.c new file +42 -0
42 lines not on this page · raw message
configure.ac +1 -0
@@ -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
Signature · 1 line
2.37.2
Re: [libgpiod][PATCH 2/2] bindings: python: tests: set the process name
Quoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
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
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [libgpiod][PATCH 2/2] bindings: python: tests: set the process name
Quoted 9 lines · Andy Shevchenko
Quoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
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.
Quoted 5 lines
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.
Sounds good, thanks. I'll add it as Link:.
Quoted 2 lines
[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.
Quoted 1 line
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