From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SUBJ_OBFU_PUNCT_FEW autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 9256 invoked from network); 19 Dec 2020 18:37:55 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 19 Dec 2020 18:37:55 -0000 Received: (qmail 23938 invoked by uid 550); 19 Dec 2020 18:37:52 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 23905 invoked from network); 19 Dec 2020 18:37:51 -0000 X-Virus-Scanned: Debian amavisd-new at disroot.org From: =?UTF-8?q?=C3=89rico=20Nogueira?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1608403059; bh=ggheBkwXt1iyEBK/YVre+2019P38+ZUok2nU2igaCT8=; h=From:To:Cc:Subject:Date; b=XpvXWn6u/mkCQkPwF/YF7ayfCivXFNVrujK7L6qWNkcU6gjpp1CGqf25PFkY6uOxC M9nCPZ6R1GGYWzEhlhAgGefg40dDvNU7vKuxaxt+WnLnSUG9L2gOwWhCOUZe7NP8VB ibZrZUUQZMXWRzW+S+MGBVsQGvpcjBQ23+LiXt8LiUXpiASpN7xt72GoXD+GIB264K xtZOSYCthQMViptaVNtjvtuHNT+GY+vzRjmanJwM8q0jb609MfFJOaC6YmfsaRWNMv ZQCDXZMp2L5am+QWtuTL4/urV2ouWfvg7smKFcygzb35AWPUt9DXDNUf5AioIIm+Ew gyVG8XPJHCgoQ== To: musl@lists.openwall.com Cc: =?UTF-8?q?=C3=89rico=20Rolim?= Date: Sat, 19 Dec 2020 15:37:21 -0300 Message-Id: <20201219183721.25532-1-ericonr@disroot.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH] add pthread_getname_np function From: Érico Rolim 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 #include #include #include #include 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 + +#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 +#include +#include + +#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