[libc-commits] [libc] [libc] Implement posix_close in unistd (#240019) (PR #240024)
via libc-commits
libc-commits at lists.llvm.org
Sat Nov 7 12:05:49 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Aman Maurya (amanmaurya92)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/240024.diff
13 Files Affected:
- (modified) libc/config/linux/aarch64/entrypoints.txt (+1)
- (modified) libc/config/linux/arm/entrypoints.txt (+1)
- (modified) libc/config/linux/riscv/entrypoints.txt (+1)
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
- (modified) libc/hdr/unistd_macros.h (+4)
- (modified) libc/include/llvm-libc-macros/linux/unistd-macros.h (+4)
- (modified) libc/include/unistd.yaml (+7)
- (modified) libc/src/unistd/CMakeLists.txt (+7)
- (modified) libc/src/unistd/linux/CMakeLists.txt (+15)
- (added) libc/src/unistd/linux/posix_close.cpp (+38)
- (added) libc/src/unistd/posix_close.h (+20)
- (modified) libc/test/src/unistd/CMakeLists.txt (+19)
- (added) libc/test/src/unistd/posix_close_test.cpp (+77)
``````````diff
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
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/240024
More information about the libc-commits
mailing list