[libc-commits] [libc] [libc] Implement posix_close in unistd (#240019) (PR #240024)

Aman Maurya via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 12:21:06 PST 2026


https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/240024

>From 767376fc24a0c62f03dab368d81746de2ecda5bc Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sun, 8 Nov 2026 01:34:15 +0530
Subject: [PATCH 1/2] [libc] Implement posix_close in unistd (#240019)

Implement the standard POSIX.1-2024 function posix_close in <unistd.h>.

- Adds posix_close entrypoint in libc/src/unistd/linux/posix_close.cpp
  validating the flag parameter (0 or POSIX_CLOSE_RESTART) and
  delegating to SYS_close.
- Adds POSIX_CLOSE_RESTART macro definition in unistd-macros.h.
- Adds public specification to libc/include/unistd.yaml.
- Registers entrypoints across Linux targets (x86_64, aarch64, arm, riscv).
- Adds unit tests verifying EINVAL on invalid flags, EBADF on invalid descriptors,
  and successful closure.

Fixes #240019.
---
 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/hdr/unistd_macros.h                      |  4 +
 .../llvm-libc-macros/linux/unistd-macros.h    |  4 +
 libc/include/unistd.yaml                      |  7 ++
 libc/src/unistd/CMakeLists.txt                |  7 ++
 libc/src/unistd/linux/CMakeLists.txt          | 15 ++++
 libc/src/unistd/linux/posix_close.cpp         | 38 +++++++++
 libc/src/unistd/posix_close.h                 | 20 +++++
 libc/test/src/unistd/CMakeLists.txt           | 19 +++++
 libc/test/src/unistd/posix_close_test.cpp     | 77 +++++++++++++++++++
 13 files changed, 195 insertions(+)
 create mode 100644 libc/src/unistd/linux/posix_close.cpp
 create mode 100644 libc/src/unistd/posix_close.h
 create mode 100644 libc/test/src/unistd/posix_close_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8ba560989af6af..2603a6e23b0704 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -508,6 +508,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.pause
     libc.src.unistd.pipe
     libc.src.unistd.pipe2
+    libc.src.unistd.posix_close
     libc.src.unistd.pread
     libc.src.unistd.pwrite
     libc.src.unistd.read
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 5e04635e298e77..5c5afd7a693fd7 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -297,6 +297,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.pause
     libc.src.unistd.pipe
     libc.src.unistd.pipe2
+    libc.src.unistd.posix_close
     libc.src.unistd.read
     libc.src.unistd.readlink
     libc.src.unistd.readlinkat
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 56813aaf66a91f..46958c14c6ba12 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -534,6 +534,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.pause
     libc.src.unistd.pipe
     libc.src.unistd.pipe2
+    libc.src.unistd.posix_close
     libc.src.unistd.pread
     libc.src.unistd.pwrite
     libc.src.unistd.read
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 25b24740400b97..5df0cda4a40188 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -554,6 +554,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.pause
     libc.src.unistd.pipe
     libc.src.unistd.pipe2
+    libc.src.unistd.posix_close
     libc.src.unistd.pread
     libc.src.unistd.pwrite
     libc.src.unistd.read
diff --git a/libc/hdr/unistd_macros.h b/libc/hdr/unistd_macros.h
index b1a63b2dd1e7d2..872bf478080253 100644
--- a/libc/hdr/unistd_macros.h
+++ b/libc/hdr/unistd_macros.h
@@ -20,4 +20,8 @@
 
 #endif // LIBC_FULL_BUILD
 
+#ifndef POSIX_CLOSE_RESTART
+#define POSIX_CLOSE_RESTART 0
+#endif
+
 #endif // LLVM_LIBC_HDR_UNISTD_MACROS_H
diff --git a/libc/include/llvm-libc-macros/linux/unistd-macros.h b/libc/include/llvm-libc-macros/linux/unistd-macros.h
index 74b1232467ba25..7043ea5e00bd22 100644
--- a/libc/include/llvm-libc-macros/linux/unistd-macros.h
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -116,4 +116,8 @@
   }))
 #endif // TEMP_FAILURE_RETRY
 
+#ifndef POSIX_CLOSE_RESTART
+#define POSIX_CLOSE_RESTART 0
+#endif
+
 #endif // LLVM_LIBC_MACROS_LINUX_UNISTD_MACROS_H
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index faac5c197b29c5..60f70ee538eae6 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -544,6 +544,13 @@ functions:
     arguments:
       - type: int *
       - type: int
+  - name: posix_close
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: int
+      - type: int
   - name: pread
     standards:
       - posix
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index d7980762b97268..f5e4d6d8b3af5a 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -406,6 +406,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.pipe2
 )
 
+add_entrypoint_object(
+  posix_close
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.posix_close
+)
+
 add_entrypoint_object(
   pread
   ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 36ac888e563a54..b854c6f2950d32 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -742,6 +742,21 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  posix_close
+  SRCS
+    posix_close.cpp
+  HDRS
+    ../posix_close.h
+  DEPENDS
+    libc.hdr.unistd_macros
+    libc.src.__support.OSUtil.linux.syscall_wrappers.close
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   pread
   SRCS
diff --git a/libc/src/unistd/linux/posix_close.cpp b/libc/src/unistd/linux/posix_close.cpp
new file mode 100644
index 00000000000000..1ce578a9469f6f
--- /dev/null
+++ b/libc/src/unistd/linux/posix_close.cpp
@@ -0,0 +1,38 @@
+//===-- Linux implementation of posix_close -------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/posix_close.h"
+
+#include "hdr/unistd_macros.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/close.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, posix_close, (int fd, int flag)) {
+#ifdef POSIX_CLOSE_RESTART
+  if (flag != 0 && flag != POSIX_CLOSE_RESTART) {
+#else
+  if (flag != 0) {
+#endif
+    libc_errno = EINVAL;
+    return -1;
+  }
+
+  auto result = linux_syscalls::close(fd);
+
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/posix_close.h b/libc/src/unistd/posix_close.h
new file mode 100644
index 00000000000000..1708d86db5e2cd
--- /dev/null
+++ b/libc/src/unistd/posix_close.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for posix_close -------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_POSIX_CLOSE_H
+#define LLVM_LIBC_SRC_UNISTD_POSIX_CLOSE_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int posix_close(int fd, int flag);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_POSIX_CLOSE_H
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 8d08b382b81047..00515d61f4d1a6 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -447,6 +447,25 @@ add_libc_test(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
+add_libc_test(
+  posix_close_test
+  SUITE
+    libc-unistd-tests
+  SRCS
+    posix_close_test.cpp
+  DEPENDS
+    libc.hdr.fcntl_macros
+    libc.hdr.sys_stat_macros
+    libc.hdr.unistd_macros
+    libc.src.fcntl.open
+    libc.src.unistd.posix_close
+    libc.src.unistd.read
+    libc.src.unistd.unlink
+    libc.src.unistd.write
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
 add_libc_test(
   rmdir_test
   SUITE
diff --git a/libc/test/src/unistd/posix_close_test.cpp b/libc/test/src/unistd/posix_close_test.cpp
new file mode 100644
index 00000000000000..90da6293b4084e
--- /dev/null
+++ b/libc/test/src/unistd/posix_close_test.cpp
@@ -0,0 +1,77 @@
+//===-- Unittests for posix_close -----------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "hdr/unistd_macros.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/posix_close.h"
+#include "src/unistd/read.h"
+#include "src/unistd/unlink.h"
+#include "src/unistd/write.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+class LlvmLibcPosixCloseTest
+    : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
+protected:
+  static constexpr const char *FILENAME = "posix_close.test";
+  const char *test_path = nullptr;
+
+  void SetUp() override { test_path = libc_make_test_file_path(FILENAME); }
+
+  void TearDown() override {
+    if (test_path)
+      LIBC_NAMESPACE::unlink(test_path);
+  }
+};
+
+TEST_F(LlvmLibcPosixCloseTest, InvalidFlag) {
+  // Invalid flag should fail with EINVAL.
+  EXPECT_THAT(LIBC_NAMESPACE::posix_close(-1, ~0), Fails<int>(EINVAL));
+  EXPECT_THAT(LIBC_NAMESPACE::posix_close(-1, 0x1234), Fails<int>(EINVAL));
+}
+
+TEST_F(LlvmLibcPosixCloseTest, InvalidFd) {
+  // Valid flag (0) with invalid fd should fail with EBADF.
+  EXPECT_THAT(LIBC_NAMESPACE::posix_close(-1, 0), Fails<int>(EBADF));
+#ifdef POSIX_CLOSE_RESTART
+  EXPECT_THAT(LIBC_NAMESPACE::posix_close(-1, POSIX_CLOSE_RESTART),
+              Fails<int>(EBADF));
+#endif
+}
+
+TEST_F(LlvmLibcPosixCloseTest, CloseSuccess) {
+  int fd = LIBC_NAMESPACE::open(test_path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+
+  EXPECT_THAT(LIBC_NAMESPACE::posix_close(fd, 0), Succeeds(0));
+
+  // Subsequent operation on closed fd should fail with EBADF.
+  char buf[1];
+  EXPECT_THAT(LIBC_NAMESPACE::read(fd, buf, 1), Fails<ssize_t>(EBADF));
+}
+
+TEST_F(LlvmLibcPosixCloseTest, CloseWithRestartFlag) {
+#ifdef POSIX_CLOSE_RESTART
+  int fd = LIBC_NAMESPACE::open(test_path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+
+  EXPECT_THAT(LIBC_NAMESPACE::posix_close(fd, POSIX_CLOSE_RESTART),
+              Succeeds(0));
+
+  // Subsequent operation on closed fd should fail with EBADF.
+  char buf[1];
+  EXPECT_THAT(LIBC_NAMESPACE::read(fd, buf, 1), Fails<ssize_t>(EBADF));
+#endif
+}

>From b070026a6b48c55c8bed10a14a8f1cb6a503ffab Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sun, 8 Nov 2026 01:50:34 +0530
Subject: [PATCH 2/2] [libc] Use pipe in posix_close_test to avoid temporary
 file dependency

Using pipe for posix_close testing avoids filesystem file path issues
and eliminates temporary CString lifetime risks in the test fixture.
---
 libc/test/src/unistd/CMakeLists.txt       |  8 ++---
 libc/test/src/unistd/posix_close_test.cpp | 43 ++++++++---------------
 2 files changed, 17 insertions(+), 34 deletions(-)

diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 00515d61f4d1a6..293f1e678e44b1 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -454,14 +454,12 @@ add_libc_test(
   SRCS
     posix_close_test.cpp
   DEPENDS
-    libc.hdr.fcntl_macros
-    libc.hdr.sys_stat_macros
+    libc.hdr.errno_macros
     libc.hdr.unistd_macros
-    libc.src.fcntl.open
+    libc.src.unistd.close
+    libc.src.unistd.pipe
     libc.src.unistd.posix_close
     libc.src.unistd.read
-    libc.src.unistd.unlink
-    libc.src.unistd.write
     libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
diff --git a/libc/test/src/unistd/posix_close_test.cpp b/libc/test/src/unistd/posix_close_test.cpp
index 90da6293b4084e..c1a73660bee4c7 100644
--- a/libc/test/src/unistd/posix_close_test.cpp
+++ b/libc/test/src/unistd/posix_close_test.cpp
@@ -6,33 +6,18 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "hdr/fcntl_macros.h"
-#include "hdr/sys_stat_macros.h"
+#include "hdr/errno_macros.h"
 #include "hdr/unistd_macros.h"
-#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/pipe.h"
 #include "src/unistd/posix_close.h"
 #include "src/unistd/read.h"
-#include "src/unistd/unlink.h"
-#include "src/unistd/write.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
-
-class LlvmLibcPosixCloseTest
-    : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
-protected:
-  static constexpr const char *FILENAME = "posix_close.test";
-  const char *test_path = nullptr;
-
-  void SetUp() override { test_path = libc_make_test_file_path(FILENAME); }
-
-  void TearDown() override {
-    if (test_path)
-      LIBC_NAMESPACE::unlink(test_path);
-  }
-};
+using LlvmLibcPosixCloseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
 TEST_F(LlvmLibcPosixCloseTest, InvalidFlag) {
   // Invalid flag should fail with EINVAL.
@@ -50,28 +35,28 @@ TEST_F(LlvmLibcPosixCloseTest, InvalidFd) {
 }
 
 TEST_F(LlvmLibcPosixCloseTest, CloseSuccess) {
-  int fd = LIBC_NAMESPACE::open(test_path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
-  ASSERT_ERRNO_SUCCESS();
-  ASSERT_GT(fd, 0);
+  int pipefd[2];
+  ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
 
-  EXPECT_THAT(LIBC_NAMESPACE::posix_close(fd, 0), Succeeds(0));
+  EXPECT_THAT(LIBC_NAMESPACE::posix_close(pipefd[0], 0), Succeeds(0));
+  EXPECT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
 
   // Subsequent operation on closed fd should fail with EBADF.
   char buf[1];
-  EXPECT_THAT(LIBC_NAMESPACE::read(fd, buf, 1), Fails<ssize_t>(EBADF));
+  EXPECT_THAT(LIBC_NAMESPACE::read(pipefd[0], buf, 1), Fails<ssize_t>(EBADF));
 }
 
 TEST_F(LlvmLibcPosixCloseTest, CloseWithRestartFlag) {
 #ifdef POSIX_CLOSE_RESTART
-  int fd = LIBC_NAMESPACE::open(test_path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
-  ASSERT_ERRNO_SUCCESS();
-  ASSERT_GT(fd, 0);
+  int pipefd[2];
+  ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
 
-  EXPECT_THAT(LIBC_NAMESPACE::posix_close(fd, POSIX_CLOSE_RESTART),
+  EXPECT_THAT(LIBC_NAMESPACE::posix_close(pipefd[0], POSIX_CLOSE_RESTART),
               Succeeds(0));
+  EXPECT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
 
   // Subsequent operation on closed fd should fail with EBADF.
   char buf[1];
-  EXPECT_THAT(LIBC_NAMESPACE::read(fd, buf, 1), Fails<ssize_t>(EBADF));
+  EXPECT_THAT(LIBC_NAMESPACE::read(pipefd[0], buf, 1), Fails<ssize_t>(EBADF));
 #endif
 }



More information about the libc-commits mailing list