[libc-commits] [libc] cdc2b94 - [libc] Add sys/file.h and flock function. (#222737)
via libc-commits
libc-commits at lists.llvm.org
Thu Sep 10 14:52:39 PDT 2026
Author: Alexey Samsonov
Date: 2026-09-10T14:52:33-07:00
New Revision: cdc2b9445ae4f2c2f9ba19cba2cf249d1fbd2faa
URL: https://github.com/llvm/llvm-project/commit/cdc2b9445ae4f2c2f9ba19cba2cf249d1fbd2faa
DIFF: https://github.com/llvm/llvm-project/commit/cdc2b9445ae4f2c2f9ba19cba2cf249d1fbd2faa.diff
LOG: [libc] Add sys/file.h and flock function. (#222737)
`flock` is a BSD function that manages advisory locks on opened files
(see https://man7.org/linux/man-pages/man2/flock.2.html)
It has enough differences from `fcntl`-based locking mechanism, and is
provided and used on Linux platforms.
* Add `<sys/file.h>` public header on Linux platforms with `flock`
declaration
and proper macro used for `op` function argument.
* Provide a Linux implementation, which is a simple syscall wrapper
around
`SYS_flock` syscall.
Assisted by: automated tooling, human-reviewed
Added:
libc/hdr/sys_file_macros.h
libc/include/llvm-libc-macros/linux/sys-file-macros.h
libc/include/llvm-libc-macros/sys-file-macros.h
libc/include/sys/file.yaml
libc/src/__support/OSUtil/linux/syscall_wrappers/flock.h
libc/src/sys/file/CMakeLists.txt
libc/src/sys/file/flock.h
libc/src/sys/file/linux/CMakeLists.txt
libc/src/sys/file/linux/flock.cpp
libc/test/src/sys/file/CMakeLists.txt
libc/test/src/sys/file/linux/CMakeLists.txt
libc/test/src/sys/file/linux/flock_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/aarch64/headers.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/riscv/headers.txt
libc/config/linux/x86_64/entrypoints.txt
libc/config/linux/x86_64/headers.txt
libc/hdr/CMakeLists.txt
libc/include/CMakeLists.txt
libc/include/llvm-libc-macros/CMakeLists.txt
libc/include/llvm-libc-macros/linux/CMakeLists.txt
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/sys/CMakeLists.txt
libc/test/src/sys/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index acf310d3593c2..c0f06e70ac671 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -280,6 +280,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# https://github.com/llvm/llvm-project/issues/80060
# libc.src.sys.epoll.epoll_pwait2
+ # sys/file.h entrypoints
+ libc.src.sys.file.flock
+
# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 399149d4dde90..0e7854bd5c29d 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -51,6 +51,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_auxv
libc.include.sys_epoll
libc.include.sys_fcntl
+ libc.include.sys_file
libc.include.sys_ioctl
libc.include.sys_mman
libc.include.sys_mount
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index f9315b6977e56..1eab33b2ff94f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -303,6 +303,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# https://github.com/llvm/llvm-project/issues/80060
# libc.src.sys.epoll.epoll_pwait2
+ # sys/file.h entrypoints
+ libc.src.sys.file.flock
+
# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl
diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index fa5376bc1f1f8..a0c7330ebe19f 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -52,6 +52,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_auxv
libc.include.sys_epoll
libc.include.sys_fcntl
+ libc.include.sys_file
libc.include.sys_ioctl
libc.include.sys_mman
libc.include.sys_param
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index c67f51371de80..d9c027dfc2511 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -303,6 +303,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# https://github.com/llvm/llvm-project/issues/80060
# libc.src.sys.epoll.epoll_pwait2
+ # sys/file.h entrypoints
+ libc.src.sys.file.flock
+
# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 6df2b54216c1f..d01bb7b6a40f2 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -52,6 +52,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_auxv
libc.include.sys_epoll
libc.include.sys_fcntl
+ libc.include.sys_file
libc.include.sys_ioctl
libc.include.sys_ipc
libc.include.sys_mman
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 061ff36f8b74f..fa01bc55785da 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -186,6 +186,14 @@ add_proxy_header_library(
libc.include.llvm-libc-macros.sys_epoll_macros
)
+add_proxy_header_library(
+ sys_file_macros
+ HDRS
+ sys_file_macros.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-macros.sys_file_macros
+)
+
add_proxy_header_library(
sys_ioctl_macros
HDRS
diff --git a/libc/hdr/sys_file_macros.h b/libc/hdr/sys_file_macros.h
new file mode 100644
index 0000000000000..aa51277afec3e
--- /dev/null
+++ b/libc/hdr/sys_file_macros.h
@@ -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
+/// Proxy header for macro values defined in sys/file.h
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_SYS_FILE_MACROS_H
+#define LLVM_LIBC_HDR_SYS_FILE_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/sys-file-macros.h"
+
+#else // Overlay mode
+
+#include <sys/file.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SYS_FILE_MACROS_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 35b7bdc8c184e..b2fce125ce0ea 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -724,6 +724,15 @@ add_header_macro(
.llvm-libc-macros.sys_epoll_macros
)
+add_header_macro(
+ sys_file
+ ../libc/include/sys/file.yaml
+ sys/file.h
+ DEPENDS
+ .llvm_libc_common_h
+ .llvm-libc-macros.sys_file_macros
+)
+
add_header_macro(
sys_ioctl
../libc/include/sys/ioctl.yaml
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 52d7f50c0858a..9e9559056dae4 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -264,6 +264,12 @@ add_macro_header(
.fcntl_macros
)
+add_macro_header(
+ sys_file_macros
+ HDR
+ sys-file-macros.h
+)
+
add_macro_header(
sys_ioctl_macros
HDR
diff --git a/libc/include/llvm-libc-macros/linux/CMakeLists.txt b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
index 7c1194d4dd15c..5a032252d9893 100644
--- a/libc/include/llvm-libc-macros/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
@@ -34,6 +34,12 @@ add_header(
sys-epoll-macros.h
)
+add_header(
+ sys_file_macros
+ HDR
+ sys-file-macros.h
+)
+
add_header(
sys_ioctl_macros
HDR
diff --git a/libc/include/llvm-libc-macros/linux/sys-file-macros.h b/libc/include/llvm-libc-macros/linux/sys-file-macros.h
new file mode 100644
index 0000000000000..20b17972d557f
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/sys-file-macros.h
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 specific declarations of macros from sys/file.h.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_LINUX_SYS_FILE_MACROS_H
+#define LLVM_LIBC_MACROS_LINUX_SYS_FILE_MACROS_H
+
+#define LOCK_SH 1 // Shared lock.
+#define LOCK_EX 2 // Exclusive lock.
+#define LOCK_NB 4 // Flag to make a nonblocking request.
+#define LOCK_UN 8 // Remove an existing lock.
+
+#endif // LLVM_LIBC_MACROS_LINUX_SYS_FILE_MACROS_H
diff --git a/libc/include/llvm-libc-macros/sys-file-macros.h b/libc/include/llvm-libc-macros/sys-file-macros.h
new file mode 100644
index 0000000000000..ba3123ed3d178
--- /dev/null
+++ b/libc/include/llvm-libc-macros/sys-file-macros.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Macros defined in sys/file.h header file.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_SYS_FILE_MACROS_H
+#define LLVM_LIBC_MACROS_SYS_FILE_MACROS_H
+
+#ifdef __linux__
+#include "linux/sys-file-macros.h"
+#endif
+
+#endif // LLVM_LIBC_MACROS_SYS_FILE_MACROS_H
diff --git a/libc/include/sys/file.yaml b/libc/include/sys/file.yaml
new file mode 100644
index 0000000000000..a1a52f5e093d8
--- /dev/null
+++ b/libc/include/sys/file.yaml
@@ -0,0 +1,23 @@
+header: sys/file.h
+standards:
+ - bsd
+macros:
+ - macro_name: LOCK_SH
+ macro_header: sys-file-macros.h
+ - macro_name: LOCK_EX
+ macro_header: sys-file-macros.h
+ - macro_name: LOCK_NB
+ macro_header: sys-file-macros.h
+ - macro_name: LOCK_UN
+ macro_header: sys-file-macros.h
+types: []
+enums: []
+objects: []
+functions:
+ - name: flock
+ standards:
+ - bsd
+ return_type: int
+ arguments:
+ - type: int
+ - type: int
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 9c1690f7a27cf..44e505694fab1 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -864,6 +864,18 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ flock
+ HDRS
+ flock.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
+
add_header_library(
fsync
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/flock.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/flock.h
new file mode 100644
index 0000000000000..42309bd8121e1
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/flock.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Syscall wrapper for flock.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FLOCK_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FLOCK_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> flock(int fd, int op) {
+ return syscall_checked<int>(SYS_flock, fd, op);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FLOCK_H
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 4249f72b4d78a..cd7f7bea24058 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -1,5 +1,6 @@
add_subdirectory(auxv)
add_subdirectory(epoll)
+add_subdirectory(file)
add_subdirectory(ipc)
add_subdirectory(mman)
add_subdirectory(personality)
diff --git a/libc/src/sys/file/CMakeLists.txt b/libc/src/sys/file/CMakeLists.txt
new file mode 100644
index 0000000000000..2119130d61fa6
--- /dev/null
+++ b/libc/src/sys/file/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+ flock
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.flock
+)
diff --git a/libc/src/sys/file/flock.h b/libc/src/sys/file/flock.h
new file mode 100644
index 0000000000000..48546a880d193
--- /dev/null
+++ b/libc/src/sys/file/flock.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 flock.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_FILE_FLOCK_H
+#define LLVM_LIBC_SRC_SYS_FILE_FLOCK_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int flock(int fd, int op);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_FILE_FLOCK_H
diff --git a/libc/src/sys/file/linux/CMakeLists.txt b/libc/src/sys/file/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..6ec6ec9b06e29
--- /dev/null
+++ b/libc/src/sys/file/linux/CMakeLists.txt
@@ -0,0 +1,13 @@
+add_entrypoint_object(
+ flock
+ SRCS
+ flock.cpp
+ HDRS
+ ../flock.h
+ DEPENDS
+ libc.src.__support.OSUtil.linux.syscall_wrappers.flock
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
diff --git a/libc/src/sys/file/linux/flock.cpp b/libc/src/sys/file/linux/flock.cpp
new file mode 100644
index 0000000000000..14cb1c06d4e07
--- /dev/null
+++ b/libc/src/sys/file/linux/flock.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 flock.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/file/flock.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/flock.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, flock, (int fd, int op)) {
+ ErrorOr<int> ret = linux_syscalls::flock(fd, op);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 17bf10f2b3d07..f944fdcc9e86f 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -13,6 +13,7 @@ add_subdirectory(wait)
add_subdirectory(prctl)
add_subdirectory(auxv)
add_subdirectory(epoll)
+add_subdirectory(file)
add_subdirectory(ipc)
add_subdirectory(uio)
add_subdirectory(time)
diff --git a/libc/test/src/sys/file/CMakeLists.txt b/libc/test/src/sys/file/CMakeLists.txt
new file mode 100644
index 0000000000000..b4bbe81c92ff2
--- /dev/null
+++ b/libc/test/src/sys/file/CMakeLists.txt
@@ -0,0 +1,3 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/sys/file/linux/CMakeLists.txt b/libc/test/src/sys/file/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..6e9c3d9b3d60d
--- /dev/null
+++ b/libc/test/src/sys/file/linux/CMakeLists.txt
@@ -0,0 +1,22 @@
+add_custom_target(libc_sys_file_unittests)
+
+add_libc_test(
+ flock_test
+ SUITE
+ libc_sys_file_unittests
+ SRCS
+ flock_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fcntl_macros
+ libc.hdr.sys_file_macros
+ libc.hdr.sys_stat_macros
+ libc.src.__support.CPP.scope
+ libc.src.errno.errno
+ libc.src.fcntl.open
+ libc.src.sys.file.flock
+ libc.src.unistd.close
+ libc.src.unistd.unlink
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/file/linux/flock_test.cpp b/libc/test/src/sys/file/linux/flock_test.cpp
new file mode 100644
index 0000000000000..75f89ef981688
--- /dev/null
+++ b/libc/test/src/sys/file/linux/flock_test.cpp
@@ -0,0 +1,69 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Unit tests for flock.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_file_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/fcntl/open.h"
+#include "src/sys/file/flock.h"
+#include "src/unistd/close.h"
+#include "src/unistd/unlink.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcFlockTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST_F(LlvmLibcFlockTest, LockAndUnlock) {
+ constexpr const char *FILENAME = "flock.test";
+ auto TEST_FILE = libc_make_test_file_path(FILENAME);
+
+ int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+ LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+ EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+ });
+
+ // Acquire non-blocking shared lock.
+ EXPECT_THAT(LIBC_NAMESPACE::flock(fd, LOCK_SH | LOCK_NB), Succeeds(0));
+ // Release lock.
+ EXPECT_THAT(LIBC_NAMESPACE::flock(fd, LOCK_UN), Succeeds(0));
+}
+
+TEST_F(LlvmLibcFlockTest, BadFd) {
+ EXPECT_THAT(LIBC_NAMESPACE::flock(-1, LOCK_SH), Fails(EBADF));
+}
+
+TEST_F(LlvmLibcFlockTest, InvalidOp) {
+ constexpr const char *FILENAME = "flock_invalid_op.test";
+ auto TEST_FILE = libc_make_test_file_path(FILENAME);
+
+ int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+ LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+ EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+ });
+
+ // Test various invalid op parameters.
+ EXPECT_THAT(LIBC_NAMESPACE::flock(fd, 0), Fails(EINVAL));
+ EXPECT_THAT(LIBC_NAMESPACE::flock(fd, 0x1000), Fails(EINVAL));
+ EXPECT_THAT(LIBC_NAMESPACE::flock(fd, LOCK_SH | LOCK_EX), Fails(EINVAL));
+}
More information about the libc-commits
mailing list