From: "Érico Nogueira" <ericonr@disroot.org>
To: musl@lists.openwall.com
Cc: "Érico Rolim" <ericonr@disroot.org>
Subject: [musl] [PATCH] add pthread_getname_np function
Date: Sat, 19 Dec 2020 15:37:21 -0300 [thread overview]
Message-ID: <20201219183721.25532-1-ericonr@disroot.org> (raw)
From: Érico Rolim <ericonr@disroot.org>
add general helper __proctidcomm to assemble the path to where the
thread name is stored, and take the opportunity to add O_CLOEXEC flag to
open() in pthread_setname_np.
---
I added the proctidcomm helper so information wouldn't be duplicated in
multiple places; same with the THREAD_NAME_PATH_SIZE macro. I could turn
proctidcomm into a macro, if you want.
Tested with the following C program:
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
void *me(void *p)
{
pause();
}
int main()
{
char n[16];
pthread_t t;
printf("pid: %ld\n", (long)getpid());
pthread_setname_np(pthread_self(), "hello");
errno = pthread_getname_np(pthread_self(), n, sizeof n);
perror("getname");
puts(n);
pthread_create(&t, 0, me, 0);
errno = pthread_setname_np(t, "long name oh boooooy!");
perror("setname other");
errno = pthread_setname_np(t, "value");
perror("setname other 2");
/* check that the string is cut off at the right size */
strcpy(n, "value431");
errno = pthread_getname_np(t, n, sizeof n);
perror("getname other");
puts(n);
pause();
}
include/pthread.h | 1 +
src/internal/proctidcomm.c | 8 ++++++++
src/internal/pthread_impl.h | 3 +++
src/thread/pthread_getname_np.c | 26 ++++++++++++++++++++++++++
src/thread/pthread_setname_np.c | 6 +++---
5 files changed, 41 insertions(+), 3 deletions(-)
create mode 100644 src/internal/proctidcomm.c
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/internal/proctidcomm.c b/src/internal/proctidcomm.c
new file mode 100644
index 00000000..91e81e16
--- /dev/null
+++ b/src/internal/proctidcomm.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+#include "pthread_impl.h"
+
+void __proctidcomm(char *buf, int tid)
+{
+ snprintf(buf, THREAD_NAME_PATH_SIZE, "/proc/self/task/%d/comm", tid);
+}
diff --git a/src/internal/pthread_impl.h b/src/internal/pthread_impl.h
index de2b9d8b..5cb3b74a 100644
--- a/src/internal/pthread_impl.h
+++ b/src/internal/pthread_impl.h
@@ -194,6 +194,9 @@ extern hidden volatile int __abort_lock[1];
extern hidden unsigned __default_stacksize;
extern hidden unsigned __default_guardsize;
+#define THREAD_NAME_PATH_SIZE (sizeof "/proc/self/task//comm" + 3*sizeof(int))
+hidden void __proctidcomm(char *, int);
+
#define DEFAULT_STACK_SIZE 131072
#define DEFAULT_GUARD_SIZE 8192
diff --git a/src/thread/pthread_getname_np.c b/src/thread/pthread_getname_np.c
new file mode 100644
index 00000000..60e6fd4e
--- /dev/null
+++ b/src/thread/pthread_getname_np.c
@@ -0,0 +1,26 @@
+#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[THREAD_NAME_PATH_SIZE];
+
+ if (len < 16) return ERANGE;
+
+ if (thread == pthread_self())
+ return prctl(PR_GET_NAME, (unsigned long)name, 0UL, 0UL, 0UL) ? errno : 0;
+
+ __proctidcomm(f, 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;
+ if (fd >= 0) close(fd);
+ pthread_setcancelstate(cs, 0);
+ /* remove trailing new line */
+ name[len-1] = 0;
+ return status;
+}
diff --git a/src/thread/pthread_setname_np.c b/src/thread/pthread_setname_np.c
index 82d35e17..6f53f408 100644
--- a/src/thread/pthread_setname_np.c
+++ b/src/thread/pthread_setname_np.c
@@ -9,7 +9,7 @@
int pthread_setname_np(pthread_t thread, const char *name)
{
int fd, cs, status = 0;
- char f[sizeof "/proc/self/task//comm" + 3*sizeof(int)];
+ char f[THREAD_NAME_PATH_SIZE];
size_t len;
if ((len = strnlen(name, 16)) > 15) return ERANGE;
@@ -17,9 +17,9 @@ int pthread_setname_np(pthread_t thread, const char *name)
if (thread == pthread_self())
return prctl(PR_SET_NAME, (unsigned long)name, 0UL, 0UL, 0UL) ? errno : 0;
- snprintf(f, sizeof f, "/proc/self/task/%d/comm", thread->tid);
+ __proctidcomm(f, thread->tid);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
- if ((fd = open(f, O_WRONLY)) < 0 || write(fd, name, len) < 0) status = errno;
+ if ((fd = open(f, O_WRONLY|O_CLOEXEC)) < 0 || write(fd, name, len) < 0) status = errno;
if (fd >= 0) close(fd);
pthread_setcancelstate(cs, 0);
return status;
--
2.29.2
next reply other threads:[~2020-12-19 18:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-19 18:37 Érico Nogueira [this message]
2021-03-25 20:19 Érico Nogueira
2021-04-20 19:15 Érico Nogueira
2021-05-20 13:04 ` Stefan Agner
2021-07-09 8:06 ` Michael Forney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201219183721.25532-1-ericonr@disroot.org \
--to=ericonr@disroot.org \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).