diff options
-rw-r--r-- | CHANGELOG.rst | 5 | ||||
-rw-r--r-- | libgcompat/pthread.c | 25 |
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; +} |