[RFC] Support for pthread_setname_np / pthread_getname_np
john stultz
johnstul@us.ibm.com
Thu Jan 7 02:49:00 GMT 2010
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 (I can't even
get it to build at the moment - probably too old of a distro :) but
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
Index: glibc-2.11/nptl/pthread_setname_np.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ glibc-2.11/nptl/pthread_setname_np.c 2010-01-06 18:24:09.000000000 -0800
@@ -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;
+}
Index: glibc-2.11/nptl/Makefile
===================================================================
--- glibc-2.11.orig/nptl/Makefile 2009-10-30 10:17:08.000000000 -0700
+++ glibc-2.11/nptl/Makefile 2010-01-06 16:23:13.000000000 -0800
@@ -124,6 +124,8 @@ libpthread-routines = nptl-init vars eve
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 \
Index: glibc-2.11/nptl/sysdeps/pthread/pthread.h
===================================================================
--- glibc-2.11.orig/nptl/sysdeps/pthread/pthread.h 2009-10-30 10:17:08.000000000 -0700
+++ glibc-2.11/nptl/sysdeps/pthread/pthread.h 2010-01-06 16:23:13.000000000 -0800
@@ -1099,6 +1099,12 @@ extern int pthread_setspecific (pthread_
__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,
Index: glibc-2.11/nptl/pthread_getname_np.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ glibc-2.11/nptl/pthread_getname_np.c 2010-01-06 18:20:00.000000000 -0800
@@ -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;
+}
More information about the Libc-help
mailing list