This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
[PATCH] Support for pthread_setname_np / pthread_getname_np
- From: John Stultz <johnstul at us dot ibm dot com>
- To: libc-help at sourceware dot org
- Cc: John Stultz <johnstul at us dot ibm dot com>
- Date: Thu, 18 Mar 2010 17:49:04 -0700
- Subject: [PATCH] Support for pthread_setname_np / pthread_getname_np
I've recently committed support to the Linux kernel for accessing and
setting a threads comm value via the proc interface. This should allow
for pthread_setname_np() / pthread_getname_np() functionality to be
implemented in glibc.
See the kernel git commit here:
http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=4614a696bd1c3a9af3a08f0e5874830a85b889d4
I'm very unfamiliar with glibc and its coding conventions wanted to send
out a very raw first pass of how such functionality could be (poorly)
implemented.
Any feedback, suggestions or total rewrites by more skilled coders would
be welcome!
thanks
-john
---
nptl/Makefile | 2 +
nptl/pthread_getname_np.c | 64 ++++++++++++++++++++++++++++++++++++++
nptl/pthread_setname_np.c | 66 ++++++++++++++++++++++++++++++++++++++++
nptl/sysdeps/pthread/pthread.h | 6 ++++
4 files changed, 138 insertions(+), 0 deletions(-)
create mode 100644 nptl/pthread_getname_np.c
create mode 100644 nptl/pthread_setname_np.c
diff --git a/nptl/Makefile b/nptl/Makefile
index a7b53ed..821a3c4 100644
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -124,6 +124,8 @@ libpthread-routines = nptl-init vars events version \
pthread_mutexattr_setprotocol \
pthread_mutexattr_getprioceiling \
pthread_mutexattr_setprioceiling tpp \
+ pthread_setname_np \
+ pthread_getname_np \
pthread_mutex_getprioceiling pthread_mutex_setprioceiling
# pthread_setuid pthread_seteuid pthread_setreuid \
# pthread_setresuid \
diff --git a/nptl/pthread_getname_np.c b/nptl/pthread_getname_np.c
new file mode 100644
index 0000000..df7d249
--- /dev/null
+++ b/nptl/pthread_getname_np.c
@@ -0,0 +1,64 @@
+/*
+ This file is part of the GNU C Library.
+ Contributed by John Stultz <johnstul@us.ibm.com>, 2010.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+#include <errno.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "pthreadP.h"
+
+
+int
+pthread_getname_np (thread_id, name)
+ pthread_t thread_id;
+ char *name;
+{
+ struct pthread *thread = (struct pthread *) thread_id;
+ int ret = 0;
+ char buf[255];
+ int fd;
+
+ if (INVALID_TD_P (thread))
+ return ESRCH;
+
+ /* Do something to validate name? */
+
+ lll_lock (thread->lock, LLL_PRIVATE);
+
+ sprintf(buf,"/proc/self/task/%i/comm", thread->tid);
+ fd = open(buf, O_WRONLY);
+ if (__builtin_expect (fd == -1, 0))
+ {
+ ret = errno;
+ }
+ else
+ {
+ ssize_t len = read(fd, buf, sizeof buf);
+ if (__builtin_expect (len == -1, 0))
+ {
+ ret = errno;
+ }
+ close(fd);
+ strncpy(name, buf, 255);
+ }
+
+ lll_unlock (thread->lock, LLL_PRIVATE);
+
+ return ret;
+}
diff --git a/nptl/pthread_setname_np.c b/nptl/pthread_setname_np.c
new file mode 100644
index 0000000..85d6b8f
--- /dev/null
+++ b/nptl/pthread_setname_np.c
@@ -0,0 +1,66 @@
+/*
+ This file is part of the GNU C Library.
+ Contributed by John Stultz <johnstul@us.ibm.com>, 2010.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <errno.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "pthreadP.h"
+
+
+int
+pthread_setname_np (thread_id, name)
+ pthread_t thread_id;
+ char *name;
+{
+ struct pthread *thread = (struct pthread *) thread_id;
+ int ret = 0;
+ char buf[255];
+ int fd;
+
+ if (INVALID_TD_P (thread))
+ return ESRCH;
+
+ lll_lock (thread->lock, LLL_PRIVATE);
+
+ sprintf(buf,"/proc/self/task/%i/comm", thread->tid);
+ fd = open(buf, O_WRONLY);
+ if (__builtin_expect (fd == -1, 0))
+ {
+ ret = errno;
+ }
+ else
+ {
+ ssize_t len = strlen(name);
+ ssize_t written = write(fd, name,len);
+ if (__builtin_expect (written != len, 0))
+ {
+ if (written == -1)
+ ret = errno;
+ else
+ ret = EIO;
+ }
+ close(fd);
+ }
+
+ lll_unlock (thread->lock, LLL_PRIVATE);
+
+ return ret;
+}
diff --git a/nptl/sysdeps/pthread/pthread.h b/nptl/sysdeps/pthread/pthread.h
index b84fd5c..b82057c 100644
--- a/nptl/sysdeps/pthread/pthread.h
+++ b/nptl/sysdeps/pthread/pthread.h
@@ -1095,6 +1095,12 @@ extern int pthread_setspecific (pthread_key_t __key,
__const void *__pointer) __THROW ;
+extern int pthread_setname_np (pthread_t __th, char *__name)
+ __THROW __nonnull ((2));
+
+extern int pthread_getname_np (pthread_t __th, char *__name)
+ __THROW __nonnull ((2));
+
#ifdef __USE_XOPEN2K
/* Get ID of CPU-time clock for thread THREAD_ID. */
extern int pthread_getcpuclockid (pthread_t __thread_id,
--
1.6.0.4