[libc-commits] [libc] [libc] implement gethostid (PR #241866)

Sachin 🗲 via libc-commits libc-commits at lists.llvm.org
Thu Nov 12 22:30:51 PST 2026


https://github.com/s4chinjha updated https://github.com/llvm/llvm-project/pull/241866

>From c1770a87973cbf207474e225dc46078fdce5b171 Mon Sep 17 00:00:00 2001
From: s4chinjha <s4chinjha at gmail.com>
Date: Thu, 12 Nov 2026 17:08:20 +0530
Subject: [PATCH 1/2] [libc] implement gethostid

adds gethostid in unistd.h

fixes: [#240373](https://github.com/llvm/llvm-project/issues/240373)

assisted by :- gemini,reviewed by me
---
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/arm/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/include/unistd.yaml                  |  6 ++++
 libc/src/unistd/CMakeLists.txt            |  7 ++++
 libc/src/unistd/gethostid.h               | 25 ++++++++++++++
 libc/src/unistd/linux/CMakeLists.txt      | 17 ++++++++++
 libc/src/unistd/linux/gethostid.cpp       | 41 +++++++++++++++++++++++
 libc/test/src/unistd/CMakeLists.txt       | 12 +++++++
 libc/test/src/unistd/gethostid_test.cpp   | 27 +++++++++++++++
 11 files changed, 139 insertions(+)
 create mode 100644 libc/src/unistd/gethostid.h
 create mode 100644 libc/src/unistd/linux/gethostid.cpp
 create mode 100644 libc/test/src/unistd/gethostid_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index a9941aab496cf..1f03aa12eae32 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -487,6 +487,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.getentropy
     libc.src.unistd.geteuid
     libc.src.unistd.getgroups
+    libc.src.unistd.gethostid
     libc.src.unistd.gethostname
     libc.src.unistd.getpagesize
     libc.src.unistd.getpgid
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 5bf0f4228d07e..6a3c41d2ec81b 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -276,6 +276,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.getentropy
     libc.src.unistd.geteuid
     libc.src.unistd.getgroups
+    libc.src.unistd.gethostid
     libc.src.unistd.gethostname
     libc.src.unistd.getpagesize
     libc.src.unistd.getpgid
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 1f52d96704a42..e9a0f42c0957e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -533,6 +533,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.getentropy
     libc.src.unistd.geteuid
     libc.src.unistd.getgroups
+    libc.src.unistd.gethostid
     libc.src.unistd.gethostname
     libc.src.unistd.getpagesize
     libc.src.unistd.getpgid
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index f30f73548e326..dd47cdae682e1 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -533,6 +533,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.getentropy
     libc.src.unistd.geteuid
     libc.src.unistd.getgroups
+    libc.src.unistd.gethostid
     libc.src.unistd.gethostname
     libc.src.unistd.getpagesize
     libc.src.unistd.getpgid
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 334fee67f09f4..d07fbf1763a15 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -391,6 +391,12 @@ functions:
     arguments:
       - type: int
       - type: gid_t *
+  - name: gethostid
+    standards:
+      - posix
+    return_type: long
+    arguments:
+      - type: void
   - name: gethostname
     standards:
       - posix
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index dbf1155741bd2..a9e2e2d7b0fec 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -252,6 +252,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.getgroups
 )
 
+add_entrypoint_object(
+  gethostid
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.gethostid
+)
+
 add_entrypoint_object(
   gethostname
   ALIAS
diff --git a/libc/src/unistd/gethostid.h b/libc/src/unistd/gethostid.h
new file mode 100644
index 0000000000000..c165e1952a742
--- /dev/null
+++ b/libc/src/unistd/gethostid.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for gethostid function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_GETHOSTID_H
+#define LLVM_LIBC_SRC_UNISTD_GETHOSTID_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+long gethostid(void);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_GETHOSTID_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index bb8086513f515..6ec0e09b420ed 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -477,6 +477,23 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  gethostid
+  SRCS
+    gethostid.cpp
+  HDRS
+    ../gethostid.h
+  DEPENDS
+    libc.hdr.fcntl_macros
+    libc.hdr.stdint_proxy
+    libc.src.__support.OSUtil.linux.syscall_wrappers.close
+    libc.src.__support.OSUtil.linux.syscall_wrappers.open
+    libc.src.__support.OSUtil.linux.syscall_wrappers.read
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   getpgid
   SRCS
diff --git a/libc/src/unistd/linux/gethostid.cpp b/libc/src/unistd/linux/gethostid.cpp
new file mode 100644
index 0000000000000..8f19b71992135
--- /dev/null
+++ b/libc/src/unistd/linux/gethostid.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of gethostid.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/gethostid.h"
+
+#include "hdr/fcntl_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/close.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/open.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/read.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(long, gethostid, (void)) {
+  auto fd = linux_syscalls::open("/etc/hostid", O_RDONLY | O_CLOEXEC, 0);
+  if (!fd)
+    return 0L;
+
+  uint32_t id = 0;
+  auto ret = linux_syscalls::read(*fd, &id, sizeof(id));
+  linux_syscalls::close(*fd);
+  if (!ret || *ret != sizeof(id))
+    return 0L;
+
+  return static_cast<long>(id);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 3ce09ee110e0d..31d939fd3312e 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -865,6 +865,18 @@ add_libc_test(
     libc.test.UnitTest.ErrnoCheckingTest
 )
 
+add_libc_test(
+  gethostid_test
+  SUITE
+    libc-unistd-tests
+  SRCS
+    gethostid_test.cpp
+  DEPENDS
+    libc.src.__support.libc_errno
+    libc.src.unistd.gethostid
+    libc.test.UnitTest.ErrnoCheckingTest
+)
+
 add_libc_test(
   gethostname_test
   SUITE
diff --git a/libc/test/src/unistd/gethostid_test.cpp b/libc/test/src/unistd/gethostid_test.cpp
new file mode 100644
index 0000000000000..846d07598ef18
--- /dev/null
+++ b/libc/test/src/unistd/gethostid_test.cpp
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for gethostid.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/gethostid.h"
+
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcGethostidTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcGethostidTest, Basic) {
+  long id1 = LIBC_NAMESPACE::gethostid();
+  EXPECT_EQ(static_cast<int>(libc_errno), 0);
+
+  long id2 = LIBC_NAMESPACE::gethostid();
+  EXPECT_EQ(id1, id2);
+}

>From b7bb8fa52f2e36b4fb9a9e18313ca26d22a3e535 Mon Sep 17 00:00:00 2001
From: s4chinjha <s4chinjha at gmail.com>
Date: Fri, 13 Nov 2026 11:51:54 +0530
Subject: [PATCH 2/2] [libc] simplify gethostid to return a constant

---
 libc/src/unistd/linux/CMakeLists.txt    |  6 ------
 libc/src/unistd/linux/gethostid.cpp     | 20 +-------------------
 libc/test/src/unistd/gethostid_test.cpp |  1 +
 3 files changed, 2 insertions(+), 25 deletions(-)

diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 9a6deb191575b..8f22f4286dde1 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -487,13 +487,7 @@ add_entrypoint_object(
   HDRS
     ../gethostid.h
   DEPENDS
-    libc.hdr.fcntl_macros
-    libc.hdr.stdint_proxy
-    libc.src.__support.OSUtil.linux.syscall_wrappers.close
-    libc.src.__support.OSUtil.linux.syscall_wrappers.open
-    libc.src.__support.OSUtil.linux.syscall_wrappers.read
     libc.src.__support.common
-    libc.src.__support.error_or
     libc.src.__support.macros.config
 )
 
diff --git a/libc/src/unistd/linux/gethostid.cpp b/libc/src/unistd/linux/gethostid.cpp
index 8f19b71992135..e23e29903a628 100644
--- a/libc/src/unistd/linux/gethostid.cpp
+++ b/libc/src/unistd/linux/gethostid.cpp
@@ -13,29 +13,11 @@
 
 #include "src/unistd/gethostid.h"
 
-#include "hdr/fcntl_macros.h"
-#include "hdr/stdint_proxy.h"
-#include "src/__support/OSUtil/linux/syscall_wrappers/close.h"
-#include "src/__support/OSUtil/linux/syscall_wrappers/open.h"
-#include "src/__support/OSUtil/linux/syscall_wrappers/read.h"
 #include "src/__support/common.h"
-#include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(long, gethostid, (void)) {
-  auto fd = linux_syscalls::open("/etc/hostid", O_RDONLY | O_CLOEXEC, 0);
-  if (!fd)
-    return 0L;
-
-  uint32_t id = 0;
-  auto ret = linux_syscalls::read(*fd, &id, sizeof(id));
-  linux_syscalls::close(*fd);
-  if (!ret || *ret != sizeof(id))
-    return 0L;
-
-  return static_cast<long>(id);
-}
+LLVM_LIBC_FUNCTION(long, gethostid, (void)) { return 1L; }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/gethostid_test.cpp b/libc/test/src/unistd/gethostid_test.cpp
index 846d07598ef18..9c4d03dde343d 100644
--- a/libc/test/src/unistd/gethostid_test.cpp
+++ b/libc/test/src/unistd/gethostid_test.cpp
@@ -20,6 +20,7 @@ using LlvmLibcGethostidTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
 TEST_F(LlvmLibcGethostidTest, Basic) {
   long id1 = LIBC_NAMESPACE::gethostid();
+  EXPECT_EQ(id1, 1L);
   EXPECT_EQ(static_cast<int>(libc_errno), 0);
 
   long id2 = LIBC_NAMESPACE::gethostid();



More information about the libc-commits mailing list