mailing list of musl libc
 help / color / mirror / Atom feed
* [musl] [PATCH v2] extend gethostid beyond a stub
@ 2020-08-04 13:32 Érico Rolim
  2020-08-04 18:41 ` Andre McCurdy
  0 siblings, 1 reply; 2+ messages in thread
From: Érico Rolim @ 2020-08-04 13:32 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.
---

> 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 ?

I have changed it to directly read the file into a variable. Given that
the return type is long, should I still use int32_t inside the function?

 src/misc/gethostid.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/misc/gethostid.c b/src/misc/gethostid.c
index 25bb35db..2877842f 100644
--- a/src/misc/gethostid.c
+++ b/src/misc/gethostid.c
@@ -1,6 +1,18 @@
 #include <unistd.h>
+#include <stdio.h>
 
 long gethostid()
 {
-	return 0;
+	FILE *f;
+	long rv = 0;
+
+	f = fopen("/etc/hostid", "reb");
+	if (f) {
+		if (fread(&rv, 4, 1, f) == 0) {
+			rv = 0;
+		}
+		fclose(f);
+	}
+
+	return rv;
 }
-- 
2.28.0


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

* Re: [musl] [PATCH v2] extend gethostid beyond a stub
  2020-08-04 13:32 [musl] [PATCH v2] extend gethostid beyond a stub Érico Rolim
@ 2020-08-04 18:41 ` Andre McCurdy
  0 siblings, 0 replies; 2+ messages in thread
From: Andre McCurdy @ 2020-08-04 18:41 UTC (permalink / raw)
  To: musl; +Cc: Érico Rolim

On Tue, Aug 4, 2020 at 6:33 AM Érico Rolim <ericonr@disroot.org> 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.
> ---
>
> > 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 ?
>
> I have changed it to directly read the file into a variable. Given that
> the return type is long, should I still use int32_t inside the function?

Yes. Consider what happens on a big-endian 64bit system.

For reference, the glibc code is basically:

long int
gethostid (void)
{
  int32_t id;
  int fd;

  /* First try to get the ID from a former invocation of sethostid.  */
  fd = __open_nocancel (HOSTIDFILE, O_RDONLY|O_LARGEFILE, 0);
  if (fd >= 0)
    {
      ssize_t n = __read_nocancel (fd, &id, sizeof (id));

      __close_nocancel_nostatus (fd);

      if (n == sizeof (id))
        return id;
    }

   ...
}

>  src/misc/gethostid.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/src/misc/gethostid.c b/src/misc/gethostid.c
> index 25bb35db..2877842f 100644
> --- a/src/misc/gethostid.c
> +++ b/src/misc/gethostid.c
> @@ -1,6 +1,18 @@
>  #include <unistd.h>
> +#include <stdio.h>
>
>  long gethostid()
>  {
> -       return 0;
> +       FILE *f;
> +       long rv = 0;
> +
> +       f = fopen("/etc/hostid", "reb");
> +       if (f) {
> +               if (fread(&rv, 4, 1, f) == 0) {
> +                       rv = 0;
> +               }
> +               fclose(f);
> +       }
> +
> +       return rv;
>  }
> --
> 2.28.0
>

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

end of thread, other threads:[~2020-08-04 18:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-04 13:32 [musl] [PATCH v2] extend gethostid beyond a stub Érico Rolim
2020-08-04 18:41 ` Andre McCurdy

mailing list of musl libc

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/musl

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 musl musl/ http://inbox.vuxu.org/musl \
		musl@inbox.vuxu.org
	public-inbox-index musl

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git