mailing list of musl libc
 help / color / mirror / code / Atom feed
* pthread_getname_np implementation
@ 2019-06-21 13:36 Poggiali, Antonio
  2019-06-21 14:04 ` Rich Felker
  0 siblings, 1 reply; 4+ messages in thread
From: Poggiali, Antonio @ 2019-06-21 13:36 UTC (permalink / raw)
  To: musl; +Cc: Malaguti, Filippo


[-- Attachment #1.1.1: Type: text/plain, Size: 954 bytes --]

Hello,

I've written the missing API pthread_getname_np for the latest version of the library.
 In attachment the patch to apply in on the master branch.

I'd really like to see it integrated in the next release.

Regards


Antonio Poggiali
R&D Senior SW Engineer Automation Products

[cid:image003.png@01D4CACC.0F48DEC0]

Phone: +39 051 676 5613
Datalogic S.r.l.
via Lavino, 265
40050 Monte San Pietro - Bologna - Italy
antonio.poggiali@datalogic.com<mailto:antonio.poggiali@datalogic.com>  |  www.datalogic.com<http://www.datalogic.com/>






CONFIDENTIALITY NOTICE. This message is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. Any unauthorized review, use, disclosure, or distribution is prohibited. If you are not the intended recipient, please destroy all copies of the original message, including any and all attachments, and notify the sender immediately.


[-- Attachment #1.1.2: Type: text/html, Size: 6287 bytes --]

[-- Attachment #1.2: image001.png --]
[-- Type: image/png, Size: 4840 bytes --]

[-- Attachment #2: 0001-Add-pthread_getname_np-API.patch --]
[-- Type: application/octet-stream, Size: 1975 bytes --]

From 20cea82579d482b90bd3db956fb0aecde8fd9fc8 Mon Sep 17 00:00:00 2001
From: apoggiali <antonio.poggiali@datalogic.com>
Date: Fri, 21 Jun 2019 14:57:04 +0200
Subject: [PATCH] Add pthread_getname_np API

---
 include/pthread.h               |  1 +
 src/thread/pthread_getname_np.c | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 src/thread/pthread_getname_np.c

diff --git a/include/pthread.h b/include/pthread.h
index e238321b..57fa905c 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -218,6 +218,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..3b5584ca
--- /dev/null
+++ b/src/thread/pthread_getname_np.c
@@ -0,0 +1,27 @@
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/prctl.h>
+
+#include "pthread_impl.h"
+
+int pthread_getname_np(pthread_t thread, char *name, size_t len)
+{
+	int fd, cs, status = 0;
+	ssize_t nlen;
+	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)) < 0 || (nlen =read(fd, name, len)) < 0) status = errno;
+	if (fd >= 0) close(fd);
+	pthread_setcancelstate(cs, 0);
+    name[nlen-1] = '\0';	
+	return status;
+}
-- 
2.19.1


^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: pthread_getname_np implementation
@ 2019-07-03  7:31 Poggiali, Antonio
  0 siblings, 0 replies; 4+ messages in thread
From: Poggiali, Antonio @ 2019-07-03  7:31 UTC (permalink / raw)
  To: musl

On Fri, 21 Jun 2019 14:18:38 -0400, Rich Felker wrote:
> So the situation is nasty, but not as bad as I remember. The
> conflicting definitions are basically junk (unusable) or minor
> gratuitous differences (int vs size_t). However this does suggest that
> there's a decent motivation for discouraging the proliferation of its
> use in applications.

> Thoughts?

I think if you provide a way to set a name,  there should be a way to get it as well.
The majority of the systems lacks the API or have it as glibc does so why not implement it glibc style?

Antonio






CONFIDENTIALITY NOTICE. This message is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. Any unauthorized review, use, disclosure, or distribution is prohibited. If you are not the intended recipient, please destroy all copies of the original message, including any and all attachments, and notify the sender immediately.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-07-03  7:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-21 13:36 pthread_getname_np implementation Poggiali, Antonio
2019-06-21 14:04 ` Rich Felker
2019-06-21 18:18   ` Rich Felker
2019-07-03  7:31 Poggiali, Antonio

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).