From de9e45d7f27dfe70634e9add7c3348aa3628a68e Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 5 Jan 2019 09:08:48 -0600 Subject: pthread: Implement pthread_getname_np Signed-off-by: Samuel Holland --- CHANGELOG.rst | 5 +++++ libgcompat/pthread.c | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) 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 +#define _GNU_SOURCE +#include /* errno */ +#include /* O_CLOEXEC, O_RDONLY */ +#include /* pthread_atfork */ +#include /* 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; +} -- cgit v1.2.3-60-g2f50