summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2019-01-05 09:08:48 -0600
committerSamuel Holland <samuel@sholland.org>2019-01-05 09:35:32 -0600
commitde9e45d7f27dfe70634e9add7c3348aa3628a68e (patch)
tree73135160071fad895a3ae62d6ca5e852521f68eb
parent59d0cbb49bc0c69d5bd754215faaec16e2c20057 (diff)
downloadgcompat-de9e45d7f27dfe70634e9add7c3348aa3628a68e.tar.gz
gcompat-de9e45d7f27dfe70634e9add7c3348aa3628a68e.tar.bz2
gcompat-de9e45d7f27dfe70634e9add7c3348aa3628a68e.tar.xz
gcompat-de9e45d7f27dfe70634e9add7c3348aa3628a68e.zip
pthread: Implement pthread_getname_np
Signed-off-by: Samuel Holland <samuel@sholland.org>
-rw-r--r--CHANGELOG.rst5
-rw-r--r--libgcompat/pthread.c25
2 files changed, 29 insertions, 1 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 7df686f..25b57e0 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -16,6 +16,11 @@ Build system
* Allow building against libobstack.
+pthread
+-------
+
+* Add pthread_getname_np.
+
wchar
-----
diff --git a/libgcompat/pthread.c b/libgcompat/pthread.c
index 4ebbe6b..0456c40 100644
--- a/libgcompat/pthread.c
+++ b/libgcompat/pthread.c
@@ -1,4 +1,8 @@
-#include <pthread.h>
+#define _GNU_SOURCE
+#include <errno.h> /* errno */
+#include <fcntl.h> /* O_CLOEXEC, O_RDONLY */
+#include <pthread.h> /* pthread_atfork */
+#include <unistd.h> /* open, read */
#include "alias.h" /* weak_alias */
@@ -27,3 +31,22 @@ int __register_atfork(void (*prepare)(void), void (*parent)(void),
return pthread_atfork(prepare, parent, child);
}
weak_alias(__register_atfork, register_atfork);
+
+/**
+ * Get the name of a thread.
+ */
+int pthread_getname_np(pthread_t thread, char *name, size_t len)
+{
+ int fd = open("/proc/thread-self/comm", O_RDONLY | O_CLOEXEC);
+ char dummy;
+
+ if (fd < 0)
+ return errno;
+ if (read(fd, name, len) < 0)
+ return errno;
+ /* If there's more to read, the buffer was too small. */
+ if (read(fd, &dummy, 1) > 0)
+ return ERANGE;
+
+ return 0;
+}