Skip to contentSkip to conversation

gpiosim: get the process name using prctl()

PATCH In review Bartosz Golaszewski 2 participants 4 messages Dec 7, 2022 GPIO

lore
Share & tools
lore
#0 lore raw

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
index dc945ef..07706f0 100644
@@ -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
index 881ecc8..e2a97fa 100644
@@ -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
#1 lore raw

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
index 7ad5de3..a951069 100644
@@ -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
index 7dcdebb..c89241e 100644
@@ -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
index b5d7f0a..cc39c29 100644
@@ -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
new file mode 100644
index 0000000..c4a8fd5
@@ -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
new file mode 100644
index 0000000..af6abdd
@@ -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
index 07706f0..1c8f192 100644
@@ -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
#2 lore raw

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
#3 lore raw

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.

[1]: https://bugs.python.org/issue5672

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

[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