--- a/include/pthread.h +++ b/include/pthread.h @@ -214,6 +214,7 @@ struct cpu_set_t; 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_tryjoin_np(pthread_t, void **); int pthread_timedjoin_np(pthread_t, void **, const struct timespec *); #endif --- /dev/null +++ b/src/thread/pthread_setname_np.c @@ -0,0 +1,24 @@ +#include +#include +#include + +#include "pthread_impl.h" + +int pthread_setname_np(pthread_t thread, const char *name) +{ + int fd, status = 0; + char f[sizeof "/proc/self/task//comm" + 7]; + ssize_t len; + + if ((len = strlen(name)) > 15) { + errno = ERANGE; + return 1; + } + + snprintf(f, sizeof f, "/proc/self/task/%d/comm", thread->tid); + if ((fd = open(f, O_WRONLY)) == -1) return 1; + if (write(fd, name, len) == -1) status = 1; + + close(fd); + return status; +}