Commit de9e45d7 authored by Samuel Holland's avatar Samuel Holland

pthread: Implement pthread_getname_np

Signed-off-by: Samuel Holland's avatarSamuel Holland <samuel@sholland.org>
parent 59d0cbb4
......@@ -16,6 +16,11 @@ Build system
* Allow building against libobstack.
pthread
-------
* Add pthread_getname_np.
wchar
-----
......
#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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment