about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorÉrico Rolim <ericonr@disroot.org>2021-04-20 16:15:15 -0300
committerRich Felker <dalias@aerifal.cx>2021-04-20 15:34:30 -0400
commitbd3b9c4ca5e93f10f7fd891b8c07cc0c5dfd198f (patch)
tree17a06c1c571c1bce713b6976959cef94110a7965 /src
parente1a51185ceb4386481491e11f6dd39569b9e54f7 (diff)
downloadmusl-bd3b9c4ca5e93f10f7fd891b8c07cc0c5dfd198f.tar.gz
musl-bd3b9c4ca5e93f10f7fd891b8c07cc0c5dfd198f.tar.xz
musl-bd3b9c4ca5e93f10f7fd891b8c07cc0c5dfd198f.zip
add pthread_getname_np function
based on the pthread_setname_np implementation
Diffstat (limited to 'src')
-rw-r--r--src/thread/pthread_getname_np.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/thread/pthread_getname_np.c b/src/thread/pthread_getname_np.c
new file mode 100644
index 00000000..48d1a294
--- /dev/null
+++ b/src/thread/pthread_getname_np.c
@@ -0,0 +1,25 @@
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/prctl.h>
+
+#include "pthread_impl.h"
+
+int pthread_getname_np(pthread_t thread, char *name, size_t len)
+{
+	int fd, cs, status = 0;
+	char f[sizeof "/proc/self/task//comm" + 3*sizeof(int)];
+
+	if (len < 16) return ERANGE;
+
+	if (thread == pthread_self())
+		return prctl(PR_GET_NAME, (unsigned long)name, 0UL, 0UL, 0UL) ? errno : 0;
+
+	snprintf(f, sizeof f, "/proc/self/task/%d/comm", thread->tid);
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+	if ((fd = open(f, O_RDONLY|O_CLOEXEC)) < 0 || (len = read(fd, name, len)) < 0) status = errno;
+	else name[len-1] = 0; /* remove trailing new line only if successful */
+	if (fd >= 0) close(fd);
+	pthread_setcancelstate(cs, 0);
+	return status;
+}