[libc-commits] [libc] b09e3a0 - [libc] Implement posix_close in unistd (#240024)
via libc-commits
libc-commits at lists.llvm.org
Tue Nov 10 04:54:16 PST 2026
Author: Aman Maurya
Date: 2026-11-10T18:24:10+05:30
New Revision: b09e3a0cd8b093b9d2eb386f8ce8d57ea5726e5c
URL: https://github.com/llvm/llvm-project/commit/b09e3a0cd8b093b9d2eb386f8ce8d57ea5726e5c
DIFF: https://github.com/llvm/llvm-project/commit/b09e3a0cd8b093b9d2eb386f8ce8d57ea5726e5c.diff
LOG: [libc] Implement posix_close in unistd (#240024)
Implement the standard POSIX.1-2024 function `posix_close` in
`<unistd.h>`.
### Changes
- Added `posix_close` entrypoint in
`libc/src/unistd/linux/posix_close.cpp` which validates `flag` (`0` or
`POSIX_CLOSE_RESTART`) and delegates to `SYS_close`.
- Added `POSIX_CLOSE_RESTART` macro definition to `unistd-macros.h`.
- Registered entrypoint across Linux targets (`x86_64`, `aarch64`,
`arm`, `riscv`).
- Added unit tests in `libc/test/src/unistd/posix_close_test.cpp`.
Fixes #240019.
Added:
libc/src/unistd/linux/posix_close.cpp
libc/src/unistd/posix_close.h
libc/test/src/unistd/posix_close_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/arm/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/hdr/unistd_macros.h
libc/include/llvm-libc-macros/linux/unistd-macros.h
libc/include/unistd.yaml
libc/src/unistd/CMakeLists.txt
libc/src/unistd/linux/CMakeLists.txt
libc/test/src/unistd/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 42408a6a4113ea..9653d47475d481 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -510,6 +510,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 44c62c2e6b29cb..5bf0f4228d07ed 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -298,6 +298,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 220d0f8ae397e4..f990ee87139d73 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -536,6 +536,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 9a7d37ac0920e6..4bfc28beb34d15 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -556,6 +556,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..f67ee95b59c4d1 100644
--- a/libc/hdr/unistd_macros.h
+++ b/libc/hdr/unistd_macros.h
@@ -18,6 +18,11 @@
#include "unistd_overlay.h"
+// 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 74b1232467ba25..85f3e441ed2060 100644
--- a/libc/include/llvm-libc-macros/linux/unistd-macros.h
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -116,4 +116,6 @@
}))
#endif // TEMP_FAILURE_RETRY
+#define POSIX_CLOSE_RESTART 0
+
#endif // LLVM_LIBC_MACROS_LINUX_UNISTD_MACROS_H
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 2860ac7d3ee0bf..334fee67f09f4e 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
@@ -555,6 +557,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 54fc70e4282673..dbf1155741bd25 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -413,6 +413,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 dee379a02de200..238ffacbd0ac03 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -761,6 +761,22 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ posix_close
+ SRCS
+ posix_close.cpp
+ 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
+ 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..502af35d84169a
--- /dev/null
+++ b/libc/src/unistd/linux/posix_close.cpp
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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"
+
+#include "hdr/errno_macros.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)) {
+ static_assert(POSIX_CLOSE_RESTART == 0);
+
+ auto result = linux_syscalls::close(fd);
+
+ if (!result.has_value()) {
+ int error = result.error();
+ // 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;
+ libc_errno = error;
+ return -1;
+ }
+
+ if (flag != 0) {
+ libc_errno = EINVAL;
+ 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..e0af9ee8c62859
--- /dev/null
+++ b/libc/src/unistd/posix_close.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 posix_close.
+///
+//===----------------------------------------------------------------------===//
+
+#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 4d76e5a99837eb..3ce09ee110e0df 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -467,6 +467,23 @@ 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.errno_macros
+ libc.hdr.unistd_macros
+ libc.src.unistd.close
+ libc.src.unistd.pipe
+ libc.src.unistd.posix_close
+ libc.src.unistd.read
+ 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..075dbc57cd9d82
--- /dev/null
+++ b/libc/test/src/unistd/posix_close_test.cpp
@@ -0,0 +1,72 @@
+//===----------------------------------------------------------------------===//
+//
+// 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"
+#include "src/unistd/close.h"
+#include "src/unistd/pipe.h"
+#include "src/unistd/posix_close.h"
+#include "src/unistd/read.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcPosixCloseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcPosixCloseTest, InvalidFlag) {
+ 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 flags (0 and POSIX_CLOSE_RESTART) with invalid fd should fail with
+ // EBADF.
+ EXPECT_THAT(LIBC_NAMESPACE::posix_close(-1, 0), Fails<int>(EBADF));
+ EXPECT_THAT(LIBC_NAMESPACE::posix_close(-1, POSIX_CLOSE_RESTART),
+ Fails<int>(EBADF));
+}
+
+TEST_F(LlvmLibcPosixCloseTest, CloseSuccess) {
+ int pipefd[2];
+ ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), 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(pipefd[0], buf, 1), Fails<ssize_t>(EBADF));
+}
+
+TEST_F(LlvmLibcPosixCloseTest, CloseWithRestartFlag) {
+ int pipefd[2];
+ ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
+
+ 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(pipefd[0], buf, 1), Fails<ssize_t>(EBADF));
+}
More information about the libc-commits
mailing list