This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Support for pthread_setname_np / pthread_getname_np
On Fri, 2010-03-19 at 11:14 -0700, john stultz wrote:
> On Fri, 2010-03-19 at 08:14 +0100, Bert Wesarg wrote:
> > On Fri, Mar 19, 2010 at 01:49, John Stultz <johnstul@us.ibm.com> wrote:
> > > +extern int pthread_getname_np (pthread_t __th, char *__name)
> > > + __THROW __nonnull ((2));
> >
> > Why does have the get routine not a size parameter for the name buffer?
>
> Ah. Good catch. I missed implementing it.
Here's an updated patch including the size.
Thanks again for the review!
-john
>From cc920c0169c302eafdd92477d1f845c6ddf6df1c Mon Sep 17 00:00:00 2001
From: John Stultz <johnstul@us.ibm.com>
Date: Tue, 16 Mar 2010 17:19:25 -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..aef8cd0
--- /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, len)
+ pthread_t thread_id;
+ char *name;
+ size_t len;
+{
+ 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 l = read(fd, name, len);
+ if (__builtin_expect (l == -1, 0))
+ {
+ ret = errno;
+ }
+ close(fd);
+ }
+
+ 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..37576e3 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, size_t len)
+ __THROW __nonnull ((3));
+
#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