[libc-commits] [libc] [libc] Implement posix_close in unistd (PR #240024)
Aman Maurya via libc-commits
libc-commits at lists.llvm.org
Mon Nov 9 21:56:13 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/5] [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/5] [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
}
>From cf378e0aee91aeb2292a1ea2bb7fc5897c8f625a Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Mon, 9 Nov 2026 15:44:48 +0530
Subject: [PATCH 3/5] [libc] Address review feedback for posix_close in unistd
- Move POSIX_CLOSE_RESTART fallback in unistd_macros.h to overlay mode with comment
- Directly define POSIX_CLOSE_RESTART without ifndef in unistd-macros.h
- Update posix_close.h, posix_close.cpp, and posix_close_test.cpp to \file Doxygen headers
- Close descriptor before returning EINVAL on invalid flags per POSIX specification
- Remove redundant POSIX_CLOSE_RESTART ifdefs in posix_close.cpp and posix_close_test.cpp
---
libc/hdr/unistd_macros.h | 5 ++--
.../llvm-libc-macros/linux/unistd-macros.h | 2 --
libc/src/unistd/linux/posix_close.cpp | 22 ++++++++-------
libc/src/unistd/posix_close.h | 7 ++++-
libc/test/src/unistd/posix_close_test.cpp | 28 +++++++++++++------
5 files changed, 40 insertions(+), 24 deletions(-)
diff --git a/libc/hdr/unistd_macros.h b/libc/hdr/unistd_macros.h
index 872bf478080253..f67ee95b59c4d1 100644
--- a/libc/hdr/unistd_macros.h
+++ b/libc/hdr/unistd_macros.h
@@ -18,10 +18,11 @@
#include "unistd_overlay.h"
-#endif // LIBC_FULL_BUILD
-
+// In case the host system libc headers do not define POSIX_CLOSE_RESTART.
#ifndef POSIX_CLOSE_RESTART
#define POSIX_CLOSE_RESTART 0
#endif
+#endif // LIBC_FULL_BUILD
+
#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 7043ea5e00bd22..85f3e441ed2060 100644
--- a/libc/include/llvm-libc-macros/linux/unistd-macros.h
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -116,8 +116,6 @@
}))
#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/src/unistd/linux/posix_close.cpp b/libc/src/unistd/linux/posix_close.cpp
index 1ce578a9469f6f..ac7bf25be91b7f 100644
--- a/libc/src/unistd/linux/posix_close.cpp
+++ b/libc/src/unistd/linux/posix_close.cpp
@@ -1,10 +1,15 @@
-//===-- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of posix_close.
+///
+//===----------------------------------------------------------------------===//
#include "src/unistd/posix_close.h"
@@ -17,21 +22,18 @@
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;
}
+
+ if (flag != 0 && flag != POSIX_CLOSE_RESTART) {
+ libc_errno = EINVAL;
+ return -1;
+ }
+
return result.value();
}
diff --git a/libc/src/unistd/posix_close.h b/libc/src/unistd/posix_close.h
index 1708d86db5e2cd..e0af9ee8c62859 100644
--- a/libc/src/unistd/posix_close.h
+++ b/libc/src/unistd/posix_close.h
@@ -1,10 +1,15 @@
-//===-- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for posix_close.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_UNISTD_POSIX_CLOSE_H
#define LLVM_LIBC_SRC_UNISTD_POSIX_CLOSE_H
diff --git a/libc/test/src/unistd/posix_close_test.cpp b/libc/test/src/unistd/posix_close_test.cpp
index c1a73660bee4c7..075dbc57cd9d82 100644
--- a/libc/test/src/unistd/posix_close_test.cpp
+++ b/libc/test/src/unistd/posix_close_test.cpp
@@ -1,10 +1,15 @@
-//===-- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for posix_close.
+///
+//===----------------------------------------------------------------------===//
#include "hdr/errno_macros.h"
#include "hdr/unistd_macros.h"
@@ -20,18 +25,25 @@ using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
using LlvmLibcPosixCloseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
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));
+ int pipefd[2];
+ ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
+
+ // If flag is invalid, posix_close() may fail with EINVAL, but shall otherwise
+ // behave as if flag had been 0 and close fd.
+ EXPECT_THAT(LIBC_NAMESPACE::posix_close(pipefd[0], ~0), Fails<int>(EINVAL));
+ EXPECT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
+
+ // The descriptor pipefd[0] should now be closed.
+ char buf[1];
+ EXPECT_THAT(LIBC_NAMESPACE::read(pipefd[0], buf, 1), Fails<ssize_t>(EBADF));
}
TEST_F(LlvmLibcPosixCloseTest, InvalidFd) {
- // Valid flag (0) with invalid fd should fail with EBADF.
+ // Valid flags (0 and POSIX_CLOSE_RESTART) 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) {
@@ -47,7 +59,6 @@ TEST_F(LlvmLibcPosixCloseTest, CloseSuccess) {
}
TEST_F(LlvmLibcPosixCloseTest, CloseWithRestartFlag) {
-#ifdef POSIX_CLOSE_RESTART
int pipefd[2];
ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
@@ -58,5 +69,4 @@ TEST_F(LlvmLibcPosixCloseTest, CloseWithRestartFlag) {
// Subsequent operation on closed fd should fail with EBADF.
char buf[1];
EXPECT_THAT(LIBC_NAMESPACE::read(pipefd[0], buf, 1), Fails<ssize_t>(EBADF));
-#endif
}
>From 8b9cf17af4a823dae626b825c5cca0028b7fd18a Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Mon, 9 Nov 2026 22:31:02 +0530
Subject: [PATCH 4/5] [libc] Map EINTR to EINPROGRESS in posix_close when
POSIX_CLOSE_RESTART is 0
---
libc/src/unistd/linux/CMakeLists.txt | 1 +
libc/src/unistd/linux/posix_close.cpp | 11 ++++++++++-
2 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index b854c6f2950d32..5d6b653f7e66da 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -749,6 +749,7 @@ add_entrypoint_object(
HDRS
../posix_close.h
DEPENDS
+ libc.hdr.errno_macros
libc.hdr.unistd_macros
libc.src.__support.OSUtil.linux.syscall_wrappers.close
libc.src.__support.common
diff --git a/libc/src/unistd/linux/posix_close.cpp b/libc/src/unistd/linux/posix_close.cpp
index ac7bf25be91b7f..fd9ef86a3ad28e 100644
--- a/libc/src/unistd/linux/posix_close.cpp
+++ b/libc/src/unistd/linux/posix_close.cpp
@@ -13,6 +13,7 @@
#include "src/unistd/posix_close.h"
+#include "hdr/errno_macros.h"
#include "hdr/unistd_macros.h"
#include "src/__support/OSUtil/linux/syscall_wrappers/close.h"
#include "src/__support/common.h"
@@ -25,7 +26,15 @@ LLVM_LIBC_FUNCTION(int, posix_close, (int fd, int flag)) {
auto result = linux_syscalls::close(fd);
if (!result.has_value()) {
- libc_errno = result.error();
+ int error = result.error();
+#if POSIX_CLOSE_RESTART == 0
+ // If POSIX_CLOSE_RESTART is zero, posix_close shall not return an EINTR
+ // error. On Linux, the descriptor is released immediately, but background
+ // state flush may be interrupted. Map EINTR to EINPROGRESS per POSIX.
+ if (error == EINTR)
+ error = EINPROGRESS;
+#endif
+ libc_errno = error;
return -1;
}
>From f5231510ad53f969d618d90a48d7ee8476d278aa Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Tue, 10 Nov 2026 11:25:26 +0530
Subject: [PATCH 5/5] [libc] Add POSIX_CLOSE_RESTART macro to unistd.yaml
Mention POSIX_CLOSE_RESTART under macros in unistd.yaml for headergen.
---
libc/include/unistd.yaml | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 60f70ee538eae6..18cc1725dd06cd 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -100,6 +100,8 @@ macros:
macro_header: unistd-macros.h
- macro_name: CLOSE_RANGE_CLOEXEC
macro_header: unistd-macros.h
+ - macro_name: POSIX_CLOSE_RESTART
+ macro_header: unistd-macros.h
types:
- type_name: uid_t
- type_name: gid_t
More information about the libc-commits
mailing list