[v3] extend gethostid beyond a stub

Submitted by Érico Rolim on Aug. 4, 2020, 10:42 p.m.

Details

Message ID 20200804224230.27774-1-ericonr@disroot.org
State New
Series "extend gethostid beyond a stub"
Headers show

Commit Message

Érico Rolim Aug. 4, 2020, 10:42 p.m.
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.
---

Andre McCurdy, thanks for the review.
 src/misc/gethostid.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/src/misc/gethostid.c b/src/misc/gethostid.c
index 25bb35db..ac84b82d 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, 4, 1, f) == 0) {
+			rv = 0;
+		}
+		fclose(f);
+	}
+
+	return rv;
 }