mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] add pthread_getname_np function
@ 2021-04-20 19:15 Érico Nogueira
  2021-04-20 19:15 ` [musl] [PATCH v2] define __STDC_UTF_{16,32}__ macros Érico Nogueira
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Érico Nogueira @ 2021-04-20 19:15 UTC (permalink / raw)
  To: musl; +Cc: Érico Rolim

From: Érico Rolim <ericonr@disroot.org>

based on the pthread_setname_np implementation
---

I might have forgotten to send this final version here

 include/pthread.h               |  1 +
 src/thread/pthread_getname_np.c | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 src/thread/pthread_getname_np.c

diff --git a/include/pthread.h b/include/pthread.h
index 0492f26a..89fd9ff7 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -221,6 +221,7 @@ int pthread_getaffinity_np(pthread_t, size_t, struct cpu_set_t *);
 int pthread_setaffinity_np(pthread_t, size_t, const struct cpu_set_t *);
 int pthread_getattr_np(pthread_t, pthread_attr_t *);
 int pthread_setname_np(pthread_t, const char *);
+int pthread_getname_np(pthread_t, char *, size_t);
 int pthread_getattr_default_np(pthread_attr_t *);
 int pthread_setattr_default_np(const pthread_attr_t *);
 int pthread_tryjoin_np(pthread_t, void **);
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;
+}
-- 
2.31.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-07-09  8:07 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-20 19:15 [musl] [PATCH] add pthread_getname_np function Érico Nogueira
2021-04-20 19:15 ` [musl] [PATCH v2] define __STDC_UTF_{16,32}__ macros Érico Nogueira
2021-04-20 19:15 ` [musl] [PATCH] extend gethostid beyond a stub Érico Nogueira
2021-04-20 19:15 ` [musl] [PATCH] remove unnecessary cast for map_library return Érico Nogueira
2021-04-20 19:15 ` [musl] [PATCH] shorten __aeabi_memset by one instruction Érico Nogueira
2021-04-21  8:24   ` Szabolcs Nagy
2021-04-21 12:32     ` Jesse DeGuire
2021-04-21 17:38     ` Rich Felker
2021-04-21 19:02       ` Érico Nogueira
2021-05-20 13:04 ` [musl] [PATCH] add pthread_getname_np function Stefan Agner
2021-07-09  8:06 ` Michael Forney

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).