* [musl] [PATCH] extend gethostid beyond a stub
@ 2020-08-03 20:55 Érico Rolim
2020-08-04 3:02 ` Rich Felker
0 siblings, 1 reply; 4+ messages in thread
From: Érico Rolim @ 2020-08-03 20:55 UTC (permalink / raw)
To: musl; +Cc: Érico Rolim
From: Érico Rolim <erico.erc@gmail.com>
Implement part of the glibc behavior, where the 32-bit identifier stored
in /etc/hostid, if the file exists, is returned. If this file doesn't
contain at least 32 bits or can't be opened for some reason, return 0.
---
src/misc/gethostid.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/src/misc/gethostid.c b/src/misc/gethostid.c
index 25bb35db..e2e98b99 100644
--- a/src/misc/gethostid.c
+++ b/src/misc/gethostid.c
@@ -1,6 +1,19 @@
#include <unistd.h>
+#include <stdio.h>
long gethostid()
{
- return 0;
+ FILE *f;
+ unsigned char hostid[4];
+ long rv = 0;
+
+ f = fopen("/etc/hostid", "reb");
+ if (f) {
+ if (fread(hostid, 1, 4, f) == 4) {
+ rv = (hostid[3] << 24) | (hostid[2] << 16) | (hostid[1] << 8) | hostid[0];
+ }
+ fclose(f);
+ }
+
+ return rv;
}
--
2.28.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [musl] [PATCH] extend gethostid beyond a stub
2020-08-03 20:55 [musl] [PATCH] extend gethostid beyond a stub Érico Rolim
@ 2020-08-04 3:02 ` Rich Felker
2020-08-04 7:02 ` Andre McCurdy
0 siblings, 1 reply; 4+ messages in thread
From: Rich Felker @ 2020-08-04 3:02 UTC (permalink / raw)
To: musl
On Mon, Aug 03, 2020 at 05:55:29PM -0300, Érico Rolim wrote:
> From: Érico Rolim <erico.erc@gmail.com>
>
> Implement part of the glibc behavior, where the 32-bit identifier stored
> in /etc/hostid, if the file exists, is returned. If this file doesn't
> contain at least 32 bits or can't be opened for some reason, return 0.
> ---
> src/misc/gethostid.c | 15 ++++++++++++++-
> 1 file changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/src/misc/gethostid.c b/src/misc/gethostid.c
> index 25bb35db..e2e98b99 100644
> --- a/src/misc/gethostid.c
> +++ b/src/misc/gethostid.c
> @@ -1,6 +1,19 @@
> #include <unistd.h>
> +#include <stdio.h>
>
> long gethostid()
> {
> - return 0;
> + FILE *f;
> + unsigned char hostid[4];
> + long rv = 0;
> +
> + f = fopen("/etc/hostid", "reb");
> + if (f) {
> + if (fread(hostid, 1, 4, f) == 4) {
> + rv = (hostid[3] << 24) | (hostid[2] << 16) | (hostid[1] << 8) | hostid[0];
> + }
> + fclose(f);
> + }
> +
> + return rv;
> }
> --
> 2.28.0
I somewhat dislike the use of stdio here, but this is something of a
junk function that's not really worth writing read() retry loop, etc.
hostid[3]<<24 is UB due to integer overflow (the promoted type is int,
a signed type). This could be fixed via promotion to unsigned before
the shift, but rather than constructing the value manually like this
I'd probably lean towards reading it into a uint32_t object x then
returning ntohl(x).
It's unfortunate that fopen can fail for spurious reasons like ENOMEM
or EMFILE/ENFILE, and that gethostid has no way of reporting this
error rather than returning the wrong id, but this seems like a
fundamental design bug in the interface and not something we can fix,
at least not while using the existing design with data in a file. I
think it could be avoided by using readlink() and storing the id in
the contents of a symlink, which should have no spurious failure
modes, but I'm not really keen on inventing a new convention for this
fundamentally-broken function.
So overall this looks pretty good. I'll revisit it after release and
see if anyone else has thoughts on it in the mean time.
Rich
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [musl] [PATCH] extend gethostid beyond a stub
2020-08-04 3:02 ` Rich Felker
@ 2020-08-04 7:02 ` Andre McCurdy
0 siblings, 0 replies; 4+ messages in thread
From: Andre McCurdy @ 2020-08-04 7:02 UTC (permalink / raw)
To: musl
On Mon, Aug 3, 2020 at 8:02 PM Rich Felker <dalias@libc.org> wrote:
>
> On Mon, Aug 03, 2020 at 05:55:29PM -0300, Érico Rolim wrote:
> > From: Érico Rolim <erico.erc@gmail.com>
> >
> > Implement part of the glibc behavior, where the 32-bit identifier stored
> > in /etc/hostid, if the file exists, is returned. If this file doesn't
> > contain at least 32 bits or can't be opened for some reason, return 0.
> > ---
> > src/misc/gethostid.c | 15 ++++++++++++++-
> > 1 file changed, 14 insertions(+), 1 deletion(-)
> >
> > diff --git a/src/misc/gethostid.c b/src/misc/gethostid.c
> > index 25bb35db..e2e98b99 100644
> > --- a/src/misc/gethostid.c
> > +++ b/src/misc/gethostid.c
> > @@ -1,6 +1,19 @@
> > #include <unistd.h>
> > +#include <stdio.h>
> >
> > long gethostid()
> > {
> > - return 0;
> > + FILE *f;
> > + unsigned char hostid[4];
> > + long rv = 0;
> > +
> > + f = fopen("/etc/hostid", "reb");
> > + if (f) {
> > + if (fread(hostid, 1, 4, f) == 4) {
> > + rv = (hostid[3] << 24) | (hostid[2] << 16) | (hostid[1] << 8) | hostid[0];
> > + }
> > + fclose(f);
> > + }
> > +
> > + return rv;
> > }
> > --
> > 2.28.0
>
> I somewhat dislike the use of stdio here, but this is something of a
> junk function that's not really worth writing read() retry loop, etc.
>
> hostid[3]<<24 is UB due to integer overflow (the promoted type is int,
> a signed type). This could be fixed via promotion to unsigned before
> the shift, but rather than constructing the value manually like this
> I'd probably lean towards reading it into a uint32_t object x then
> returning ntohl(x).
The glibc implementation appears to read and write directly into an
int32_t variable, without any endianness conversion. To be
interoperable with /etc/hostid files created by glibc shouldn't musl
skip the ntohl() and just return x ?
> It's unfortunate that fopen can fail for spurious reasons like ENOMEM
> or EMFILE/ENFILE, and that gethostid has no way of reporting this
> error rather than returning the wrong id, but this seems like a
> fundamental design bug in the interface and not something we can fix,
> at least not while using the existing design with data in a file. I
> think it could be avoided by using readlink() and storing the id in
> the contents of a symlink, which should have no spurious failure
> modes, but I'm not really keen on inventing a new convention for this
> fundamentally-broken function.
>
> So overall this looks pretty good. I'll revisit it after release and
> see if anyone else has thoughts on it in the mean time.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [musl] [PATCH] add pthread_getname_np function
@ 2021-04-20 19:15 Érico Nogueira
2021-04-20 19:15 ` [musl] [PATCH] extend gethostid beyond a stub Érico Nogueira
0 siblings, 1 reply; 4+ messages in thread
From: Érico Nogueira @ 2021-04-20 19:15 UTC (permalink / raw)
To: musl; +Cc: Érico Rolim
From: Érico Rolim <ericonr@disroot.org>
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 <fcntl.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;
+ 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* [musl] [PATCH] extend gethostid beyond a stub
2021-04-20 19:15 [musl] [PATCH] add pthread_getname_np function Érico Nogueira
@ 2021-04-20 19:15 ` Érico Nogueira
0 siblings, 0 replies; 4+ messages in thread
From: Érico Nogueira @ 2021-04-20 19:15 UTC (permalink / raw)
To: musl; +Cc: Érico Rolim
From: Érico Rolim <erico.erc@gmail.com>
Implement part of the glibc behavior, where the 32-bit identifier stored
in /etc/hostid, if the file exists, is returned. If this file doesn't
contain at least 32 bits or can't be opened for some reason, return 0.
---
src/misc/gethostid.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/src/misc/gethostid.c b/src/misc/gethostid.c
index 25bb35db..d529de9c 100644
--- a/src/misc/gethostid.c
+++ b/src/misc/gethostid.c
@@ -1,6 +1,19 @@
#include <unistd.h>
+#include <stdio.h>
+#include <stdint.h>
long gethostid()
{
- return 0;
+ FILE *f;
+ int32_t rv = 0;
+
+ f = fopen("/etc/hostid", "reb");
+ if (f) {
+ if (fread(&rv, sizeof(rv), 1, f) == 0) {
+ rv = 0;
+ }
+ fclose(f);
+ }
+
+ return rv;
}
--
2.31.1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-04-20 19:16 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-03 20:55 [musl] [PATCH] extend gethostid beyond a stub Érico Rolim
2020-08-04 3:02 ` Rich Felker
2020-08-04 7:02 ` Andre McCurdy
2021-04-20 19:15 [musl] [PATCH] add pthread_getname_np function Érico Nogueira
2021-04-20 19:15 ` [musl] [PATCH] extend gethostid beyond a stub Érico Nogueira
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).