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 autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 4310 invoked from network); 20 Apr 2021 19:16:07 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 20 Apr 2021 19:16:07 -0000 Received: (qmail 20475 invoked by uid 550); 20 Apr 2021 19:16:00 -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 20445 invoked from network); 20 Apr 2021 19:15:59 -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=1618946143; bh=zOmcxlG5e14vxKwHjENy4eCJMRFFr2Pgj7noi17MaTk=; h=From:To:Cc:Subject:Date; b=L0xGzOqlO7G3xbMaLy42k5qpUTDTtCW6/VyyYF3K5rnc/cvjVir0jb+5GIRGh74SJ mjtWJes7Z4P+gxIyJRtffPdsCwOJ6InToSPUfSjxvdbmhxZDCZ+KvCIs4u5VU+Q/+s OdXa6VQH7t7fNCJSBRsE23H0UDX6Okqr9jaX1aLvTeyVRTCRWCn0uENJbdKj50hOdx 192XC8RAV10BvlPeDRzbmUuOjY0liBQgqQWGTNmVoZVI7MzxcTcqeY6dc9upgi0dYs Uyh0L+Sxf0EvIY54kvP1I28Zv8IJ+A3YCaruGNuTLnHn0jxtR/pWYBKHrwbseCkG1Z 6CuAb/ShqSUnQ== To: musl@lists.openwall.com Cc: =?UTF-8?q?=C3=89rico=20Rolim?= Date: Tue, 20 Apr 2021 16:15:15 -0300 Message-Id: <20210420191519.23822-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 based on the pthread_setname_np implementation --- I might have forgotten to send this final version here include/pthread.h | 1 + src/thread/pthread_getname_np.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) 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/thread/pthread_getname_np.c b/src/thread/pthread_getname_np.c new file mode 100644 index 00000000..48d1a294 --- /dev/null +++ b/src/thread/pthread_getname_np.c @@ -0,0 +1,25 @@ +#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[sizeof "/proc/self/task//comm" + 3*sizeof(int)]; + + if (len < 16) return ERANGE; + + if (thread == pthread_self()) + return prctl(PR_GET_NAME, (unsigned long)name, 0UL, 0UL, 0UL) ? errno : 0; + + snprintf(f, sizeof f, "/proc/self/task/%d/comm", 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; + else name[len-1] = 0; /* remove trailing new line only if successful */ + if (fd >= 0) close(fd); + pthread_setcancelstate(cs, 0); + return status; +} -- 2.31.1