[libc-commits] [libc] [libc] Implement fcntl() function (PR #89507)
Vinayak Dev via libc-commits
libc-commits at lists.llvm.org
Wed May 1 05:35:05 PDT 2024
https://github.com/vinayakdsci updated https://github.com/llvm/llvm-project/pull/89507
>From 71efb372b33bc7fa3ffdced67a979bb37fa6fcf5 Mon Sep 17 00:00:00 2001
From: Vinayak Dev <vinayakdev.sci at gmail.com>
Date: Sat, 20 Apr 2024 20:50:20 +0530
Subject: [PATCH 1/6] [libc] Implement fcntl() function
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/src/fcntl/CMakeLists.txt | 7 ++
libc/src/fcntl/fcntl.h | 20 +++
libc/src/fcntl/linux/CMakeLists.txt | 12 ++
libc/src/fcntl/linux/fcntl.cpp | 96 ++++++++++++++
libc/test/src/fcntl/CMakeLists.txt | 15 +++
libc/test/src/fcntl/fcntl_test.cpp | 146 ++++++++++++++++++++++
9 files changed, 299 insertions(+)
create mode 100644 libc/src/fcntl/fcntl.h
create mode 100644 libc/src/fcntl/linux/fcntl.cpp
create mode 100644 libc/test/src/fcntl/fcntl_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 1ac6bd93000082..5606b06967d629 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -22,6 +22,7 @@ set(TARGET_LIBC_ENTRYPOINTS
# fcntl.h entrypoints
libc.src.fcntl.creat
+ libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.fcntl.openat
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 87e82e5eb9a067..1caba997742b8a 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -22,6 +22,7 @@ set(TARGET_LIBC_ENTRYPOINTS
# fcntl.h entrypoints
libc.src.fcntl.creat
+ libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.fcntl.openat
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a8e28992766712..2b7f27d1511f3c 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -22,6 +22,7 @@ set(TARGET_LIBC_ENTRYPOINTS
# fcntl.h entrypoints
libc.src.fcntl.creat
+ libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.fcntl.openat
diff --git a/libc/src/fcntl/CMakeLists.txt b/libc/src/fcntl/CMakeLists.txt
index 0b9ee47c4f7c13..77400e9050d08b 100644
--- a/libc/src/fcntl/CMakeLists.txt
+++ b/libc/src/fcntl/CMakeLists.txt
@@ -9,6 +9,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.creat
)
+add_entrypoint_object(
+ fcntl
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.fcntl
+)
+
add_entrypoint_object(
open
ALIAS
diff --git a/libc/src/fcntl/fcntl.h b/libc/src/fcntl/fcntl.h
new file mode 100644
index 00000000000000..821e498f767fdd
--- /dev/null
+++ b/libc/src/fcntl/fcntl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header of fcntl --------------------------*- 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_FCNTL_FCNTL_H
+#define LLVM_LIBC_SRC_FCNTL_FCNTL_H
+
+#include <fcntl.h>
+
+namespace LIBC_NAMESPACE {
+
+int fcntl(int fd, int cmd, ...);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_FCNTL_FCNTL_H
diff --git a/libc/src/fcntl/linux/CMakeLists.txt b/libc/src/fcntl/linux/CMakeLists.txt
index 87b8d4695c4fc5..f12d57a6b40a7b 100644
--- a/libc/src/fcntl/linux/CMakeLists.txt
+++ b/libc/src/fcntl/linux/CMakeLists.txt
@@ -10,6 +10,18 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ fcntl
+ SRCS
+ fcntl.cpp
+ HDRS
+ ../fcntl.h
+ DEPENDS
+ libc.include.fcntl
+ libc.src.__support.OSUtil.osutil
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
open
SRCS
diff --git a/libc/src/fcntl/linux/fcntl.cpp b/libc/src/fcntl/linux/fcntl.cpp
new file mode 100644
index 00000000000000..12376a518bdfa3
--- /dev/null
+++ b/libc/src/fcntl/linux/fcntl.cpp
@@ -0,0 +1,96 @@
+//===-- Implementation of fcntl -------------------------------------------===//
+//
+// 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/fcntl/fcntl.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
+
+#include <stdarg.h>
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace {
+struct fowner_ex {
+ int type;
+ pid_t pid;
+};
+} // namespace
+
+// The OFD file locks require special handling for LARGEFILES
+namespace LIBC_NAMESPACE {
+LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
+ void *arg;
+ va_list varargs;
+ va_start(varargs, cmd);
+ arg = va_arg(varargs, void *);
+ va_end(varargs);
+
+ switch (cmd) {
+ case F_SETLKW:
+ return syscall_impl<int>(SYS_fcntl, fd, cmd, arg);
+ case F_OFD_SETLKW: {
+ struct flock *flk = (struct flock *)arg;
+ // convert the struct to a flock64
+ struct flock64 flk64;
+ flk64.l_type = flk->l_type;
+ flk64.l_whence = flk->l_whence;
+ flk64.l_start = flk->l_start;
+ flk64.l_len = flk->l_len;
+ flk64.l_pid = flk->l_pid;
+ // create a syscall
+ return syscall_impl<int>(SYS_fcntl, fd, cmd, &flk64);
+ }
+ case F_OFD_GETLK:
+ case F_OFD_SETLK: {
+ struct flock *flk = (struct flock *)arg;
+ // convert the struct to a flock64
+ struct flock64 flk64;
+ flk64.l_type = flk->l_type;
+ flk64.l_whence = flk->l_whence;
+ flk64.l_start = flk->l_start;
+ flk64.l_len = flk->l_len;
+ flk64.l_pid = flk->l_pid;
+ // create a syscall
+ int retVal = syscall_impl<int>(SYS_fcntl, fd, cmd, &flk64);
+ // On failure, return
+ if (retVal == -1)
+ return -1;
+ // Check for overflow, i.e. the offsets are not the same when cast
+ // to off_t from off64_t.
+ if ((off_t)flk64.l_len != flk64.l_len ||
+ (off_t)flk64.l_start != flk64.l_start) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+ // Now copy back into flk, in case flk64 got modified
+ flk->l_type = flk64.l_type;
+ flk->l_whence = flk64.l_whence;
+ flk->l_start = flk64.l_start;
+ flk->l_len = flk64.l_len;
+ flk->l_pid = flk64.l_pid;
+ return retVal;
+ }
+ // The general case
+ default: {
+ if (cmd == F_GETOWN) {
+ struct fowner_ex fex;
+ int retVal = syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex);
+ if (retVal == -EINVAL)
+ return syscall_impl<int>(SYS_fcntl, fd, cmd, (void *)arg);
+ if ((uint64_t)retVal <= -4096UL)
+ return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;
+
+ libc_errno = -retVal;
+ return -1;
+ }
+ return syscall_impl<int>(SYS_fcntl, fd, cmd, (void *)arg);
+ }
+ }
+}
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index ae39d8d5f878c5..1c99009d5a6d4e 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -17,6 +17,21 @@ add_libc_unittest(
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_unittest(
+ fcntl_test
+ SUITE
+ libc_fcntl_unittests
+ SRCS
+ fcntl_test.cpp
+ DEPENDS
+ libc.include.fcntl
+ libc.src.errno.errno
+ libc.src.fcntl.fcntl
+ libc.src.fcntl.open
+ libc.src.unistd.close
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
add_libc_unittest(
openat_test
SUITE
diff --git a/libc/test/src/fcntl/fcntl_test.cpp b/libc/test/src/fcntl/fcntl_test.cpp
new file mode 100644
index 00000000000000..81a7feaf151cf8
--- /dev/null
+++ b/libc/test/src/fcntl/fcntl_test.cpp
@@ -0,0 +1,146 @@
+//===-- Unittest for fcntl ------------------------------------------------===//
+//
+// 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/errno/libc_errno.h"
+#include "src/fcntl/fcntl.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include <sys/stat.h>
+
+TEST(LlvmLibcFcntlTest, FcntlDupfd) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+ constexpr const char *TEST_FILE = "testdata/fcntl.test";
+ int fd2, fd3;
+ int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+
+ fd2 = LIBC_NAMESPACE::fcntl(fd, F_DUPFD, 0);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd2, 0);
+
+ fd3 = LIBC_NAMESPACE::fcntl(fd, F_DUPFD, 10);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd3, 0);
+
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd2), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd3), Succeeds(0));
+}
+
+TEST(LlvmLibcFcntlTest, FcntlGetFl) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+ constexpr const char *TEST_FILE = "testdata/fcntl.test";
+ int retVal;
+ int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+
+ retVal = LIBC_NAMESPACE::fcntl(fd, F_GETFL);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(retVal, -1);
+
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
+
+TEST(LlvmLibcFcntlTest, FcntlSetFl) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+ constexpr const char *TEST_FILE = "testdata/fcntl.test";
+
+ int retVal;
+ int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+
+ retVal = LIBC_NAMESPACE::fcntl(fd, F_GETFL);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(retVal, -1);
+
+ int oldFlags = LIBC_NAMESPACE::fcntl(fd, F_GETFL, 0);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(oldFlags, 0);
+
+ // Add the APPEND flag;
+ oldFlags |= O_APPEND;
+
+ retVal = LIBC_NAMESPACE::fcntl(fd, F_SETFL, oldFlags);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(retVal, -1);
+
+ // Remove the APPEND flag;
+ oldFlags = -oldFlags & O_APPEND;
+
+ retVal = LIBC_NAMESPACE::fcntl(fd, F_SETFL, oldFlags);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(retVal, -1);
+
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
+
+TEST(LlvmLibcFcntlTest, FcntlGetLkRead) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+ constexpr const char *TEST_FILE = "testdata/fcntl.test";
+
+ struct flock flk, svflk;
+ int retVal;
+ int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDONLY);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+
+ flk.l_type = F_RDLCK;
+ flk.l_start = 0;
+ flk.l_whence = SEEK_SET;
+ flk.l_len = 50;
+
+ // copy flk into svflk
+ svflk = flk;
+
+ retVal = LIBC_NAMESPACE::fcntl(fd, F_GETLK, &svflk);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(retVal, -1);
+ ASSERT_NE((int)flk.l_type, F_WRLCK); // File should not be write locked.
+
+ retVal = LIBC_NAMESPACE::fcntl(fd, F_SETLK, &svflk);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(retVal, -1);
+
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
+
+TEST(LlvmLibcFcntlTest, FcntlGetLkWrite) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+ constexpr const char *TEST_FILE = "testdata/fcntl.test";
+
+ struct flock flk, svflk;
+ int retVal;
+ int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+
+ flk.l_type = F_WRLCK;
+ flk.l_start = 0;
+ flk.l_whence = SEEK_SET;
+ flk.l_len = 0;
+
+ // copy flk into svflk
+ svflk = flk;
+
+ retVal = LIBC_NAMESPACE::fcntl(fd, F_GETLK, &svflk);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(retVal, -1);
+ ASSERT_NE((int)flk.l_type, F_RDLCK); // File should not be read locked.
+
+ retVal = LIBC_NAMESPACE::fcntl(fd, F_SETLK, &svflk);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(retVal, -1);
+
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
>From a07f4430f36deb65562a8b9edfb43cf7f81efd06 Mon Sep 17 00:00:00 2001
From: Vinayak Dev <vinayakdev.sci at gmail.com>
Date: Fri, 26 Apr 2024 23:58:28 +0530
Subject: [PATCH 2/6] Remove the use of system headers, address comments
---
libc/hdr/CMakeLists.txt | 9 +++++
libc/hdr/fcntl_macros.h | 22 ++++++++++++
libc/hdr/types/CMakeLists.txt | 16 +++++++++
libc/hdr/types/struct_flock.h | 21 +++++++++++
libc/hdr/types/struct_flock64.h | 21 +++++++++++
.../llvm-libc-macros/linux/fcntl-macros.h | 31 ++++++++++++++++
libc/include/llvm-libc-types/CMakeLists.txt | 2 ++
libc/include/llvm-libc-types/struct_flock.h | 23 ++++++++++++
libc/include/llvm-libc-types/struct_flock64.h | 23 ++++++++++++
libc/spec/posix.td | 5 +++
libc/src/fcntl/fcntl.h | 2 --
libc/src/fcntl/linux/CMakeLists.txt | 3 ++
libc/src/fcntl/linux/fcntl.cpp | 35 ++++++++++++-------
libc/test/src/fcntl/fcntl_test.cpp | 2 --
14 files changed, 199 insertions(+), 16 deletions(-)
create mode 100644 libc/hdr/fcntl_macros.h
create mode 100644 libc/hdr/types/struct_flock.h
create mode 100644 libc/hdr/types/struct_flock64.h
create mode 100644 libc/include/llvm-libc-types/struct_flock.h
create mode 100644 libc/include/llvm-libc-types/struct_flock64.h
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index fb7c342f92b78f..179b05e6ee966d 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -32,6 +32,15 @@ add_proxy_header_library(
libc.include.math
)
+add_proxy_header_library(
+ fcntl_macros
+ HDRS
+ fcntl_macros.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-macros.fcntl_macros
+ libc.include.fcntl
+)
+
add_proxy_header_library(
fenv_macros
HDRS
diff --git a/libc/hdr/fcntl_macros.h b/libc/hdr/fcntl_macros.h
new file mode 100644
index 00000000000000..828cb984c0cb14
--- /dev/null
+++ b/libc/hdr/fcntl_macros.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from fcntl/fcntl.h ---------------------------===//
+//
+// 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_HDR_FCNTL_MACROS_H
+#define LLVM_LIBC_HDR_FCNTL_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/fcntl-macros.h"
+
+#else // Overlay mode
+
+#include <fcntl.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_FCNTL_MACROS_H
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index f53766777e7530..3ba7853015209e 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -14,6 +14,22 @@ add_proxy_header_library(
libc.include.llvm-libc-types.struct_epoll_event
)
+add_proxy_header_library(
+ struct_flock
+ HDRS
+ struct_flock.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_flock
+)
+
+add_proxy_header_library(
+ struct_flock64
+ HDRS
+ struct_flock64.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_flock64
+)
+
add_proxy_header_library(
struct_timespec
HDRS
diff --git a/libc/hdr/types/struct_flock.h b/libc/hdr/types/struct_flock.h
new file mode 100644
index 00000000000000..a552b91c432b38
--- /dev/null
+++ b/libc/hdr/types/struct_flock.h
@@ -0,0 +1,21 @@
+//===-- Proxy for struct flock -------------------------------------------===//
+//
+// 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_HDR_TYPES_STRUCT_FLOCK_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_flock.h"
+
+#else
+
+#include <fcntl.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK_H
diff --git a/libc/hdr/types/struct_flock64.h b/libc/hdr/types/struct_flock64.h
new file mode 100644
index 00000000000000..84fe67816c3372
--- /dev/null
+++ b/libc/hdr/types/struct_flock64.h
@@ -0,0 +1,21 @@
+//===-- Proxy for struct flock64 -----------------------------------------===//
+//
+// 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_HDR_TYPES_STRUCT_FLOCK64_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK64_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_flock64.h"
+
+#else
+
+#include <fcntl.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK64_H
diff --git a/libc/include/llvm-libc-macros/linux/fcntl-macros.h b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
index 1d4e5bbbdc770a..c37ea3c110c14a 100644
--- a/libc/include/llvm-libc-macros/linux/fcntl-macros.h
+++ b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
@@ -67,5 +67,36 @@
#define F_SETFD 2
#define F_GETFL 3
#define F_SETFL 4
+#define F_GETLK 5
+#define F_SETLK 6
+#define F_SETLKW 7
+#define F_SETOWN 8
+#define F_GETOWN 9
+#define F_SETSIG 10
+#define F_GETSIG 11
+#define F_GETLK64 12
+#define F_SETLK64 13
+#define F_SETLKW64 14
+#define F_SETOWN_EX 15
+#define F_GETOWN_EX 16
+
+// Open File Description Locks.
+#define F_OFD_GETLK 36
+#define F_OFD_SETLK 37
+#define F_OFD_SETLKW 38
+
+// Close on succesful
+#define F_CLOEXEC 1
+
+#define F_RDLCK 0
+#define F_WRLOCK 1
+#define F_UNLCK 2
+
+// For Large File Support
+#if defined(_LARGEFILE64_SOURCE)
+#define F_GETLK F_GETLK64
+#define F_SETLK F_SETLK64
+#define F_SETLKW F_SETLKW64
+#endif
#endif // LLVM_LIBC_MACROS_LINUX_FCNTL_MACROS_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 310374fb62ffe0..1acc2881dcac9a 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -60,6 +60,8 @@ add_header(rlim_t HDR rlim_t.h)
add_header(time_t HDR time_t.h)
add_header(stack_t HDR stack_t.h)
add_header(suseconds_t HDR suseconds_t.h)
+add_header(struct_flock HDR struct_flock.h DEPENDS .off_t .pid_t)
+add_header(struct_flock64 HDR struct_flock64.h DEPENDS .off64_t .pid_t)
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
diff --git a/libc/include/llvm-libc-types/struct_flock.h b/libc/include/llvm-libc-types/struct_flock.h
new file mode 100644
index 00000000000000..6202c25f358517
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_flock.h
@@ -0,0 +1,23 @@
+//===-- Definition of type struct flock64 ---------------------------------===//
+//
+// 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_TYPES_STRUCT_FLOCK_H
+#define LLVM_LIBC_TYPES_STRUCT_FLOCK_H
+
+#include "llvm-libc-types/off_t.h"
+#include "llvm-libc-types/pid_t.h"
+
+struct flock64 {
+ int16_t l_type;
+ int16_t l_whence;
+ off_t l_start;
+ off_t l_len;
+ pid_t l_pid;
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_FLOCK_H
diff --git a/libc/include/llvm-libc-types/struct_flock64.h b/libc/include/llvm-libc-types/struct_flock64.h
new file mode 100644
index 00000000000000..bec78ba8e4cb05
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_flock64.h
@@ -0,0 +1,23 @@
+//===-- Definition of type struct flock64 ---------------------------------===//
+//
+// 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_TYPES_STRUCT_FLOCK64_H
+#define LLVM_LIBC_TYPES_STRUCT_FLOCK64_H
+
+#include "llvm-libc-types/off64_t.h"
+#include "llvm-libc-types/pid_t.h"
+
+struct flock64 {
+ int16_t l_type;
+ int16_t l_whence;
+ off64_t l_start;
+ off64_t l_len;
+ pid_t l_pid;
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_FLOCK64_H
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index d428d54e32a331..e7a0cf883c6077 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -230,6 +230,11 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<IntType>,
[ArgSpec<ConstCharPtr>, ArgSpec<ModeTType>]
>,
+ FunctionSpec<
+ "fcntl",
+ RetValSpec<IntType>,
+ [ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<VarArgType>]
+ >,
FunctionSpec<
"open",
RetValSpec<IntType>,
diff --git a/libc/src/fcntl/fcntl.h b/libc/src/fcntl/fcntl.h
index 821e498f767fdd..8fe3fb3146b91d 100644
--- a/libc/src/fcntl/fcntl.h
+++ b/libc/src/fcntl/fcntl.h
@@ -9,8 +9,6 @@
#ifndef LLVM_LIBC_SRC_FCNTL_FCNTL_H
#define LLVM_LIBC_SRC_FCNTL_FCNTL_H
-#include <fcntl.h>
-
namespace LIBC_NAMESPACE {
int fcntl(int fd, int cmd, ...);
diff --git a/libc/src/fcntl/linux/CMakeLists.txt b/libc/src/fcntl/linux/CMakeLists.txt
index f12d57a6b40a7b..58509c6d58569a 100644
--- a/libc/src/fcntl/linux/CMakeLists.txt
+++ b/libc/src/fcntl/linux/CMakeLists.txt
@@ -18,6 +18,9 @@ add_entrypoint_object(
../fcntl.h
DEPENDS
libc.include.fcntl
+ libc.include.llvm-libc-types.struct_flock
+ libc.include.llvm-libc-types.struct_flock64
+ libc.hdr.fcntl_macros
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
diff --git a/libc/src/fcntl/linux/fcntl.cpp b/libc/src/fcntl/linux/fcntl.cpp
index 12376a518bdfa3..311fd120bd0a94 100644
--- a/libc/src/fcntl/linux/fcntl.cpp
+++ b/libc/src/fcntl/linux/fcntl.cpp
@@ -8,6 +8,9 @@
#include "src/fcntl/fcntl.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/types/struct_flock.h"
+#include "hdr/types/struct_flock64.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
@@ -15,12 +18,19 @@
#include <stdarg.h>
#include <sys/syscall.h> // For syscall numbers.
-namespace {
+// To avoid conflict with system headers when building
+// in overlay mode.
+namespace helper {
+enum pid_type {
+ F_OWNER_TID = 0,
+ F_OWNER_PID,
+ F_OWNER_PGRP,
+};
struct fowner_ex {
- int type;
+ enum pid_type type;
pid_t pid;
};
-} // namespace
+} // namespace helper
// The OFD file locks require special handling for LARGEFILES
namespace LIBC_NAMESPACE {
@@ -35,7 +45,7 @@ LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
case F_SETLKW:
return syscall_impl<int>(SYS_fcntl, fd, cmd, arg);
case F_OFD_SETLKW: {
- struct flock *flk = (struct flock *)arg;
+ struct flock *flk = reinterpret_cast<struct flock *>(arg);
// convert the struct to a flock64
struct flock64 flk64;
flk64.l_type = flk->l_type;
@@ -48,7 +58,7 @@ LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
}
case F_OFD_GETLK:
case F_OFD_SETLK: {
- struct flock *flk = (struct flock *)arg;
+ struct flock *flk = reinterpret_cast<struct flock *>(arg);
// convert the struct to a flock64
struct flock64 flk64;
flk64.l_type = flk->l_type;
@@ -63,8 +73,8 @@ LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
return -1;
// Check for overflow, i.e. the offsets are not the same when cast
// to off_t from off64_t.
- if ((off_t)flk64.l_len != flk64.l_len ||
- (off_t)flk64.l_start != flk64.l_start) {
+ if (static_cast<off_t>(flk64.l_len) != flk64.l_len ||
+ static_cast<off_t>(flk64.l_start) != flk64.l_start) {
libc_errno = EOVERFLOW;
return -1;
}
@@ -79,17 +89,18 @@ LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
// The general case
default: {
if (cmd == F_GETOWN) {
- struct fowner_ex fex;
+ struct helper::fowner_ex fex;
int retVal = syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex);
if (retVal == -EINVAL)
- return syscall_impl<int>(SYS_fcntl, fd, cmd, (void *)arg);
- if ((uint64_t)retVal <= -4096UL)
- return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;
+ return syscall_impl<int>(SYS_fcntl, fd, cmd,
+ reinterpret_cast<void *>(arg));
+ if (static_cast<uint64_t>(retVal) <= -4096UL)
+ return fex.type == helper::F_OWNER_PGRP ? -fex.pid : fex.pid;
libc_errno = -retVal;
return -1;
}
- return syscall_impl<int>(SYS_fcntl, fd, cmd, (void *)arg);
+ return syscall_impl<int>(SYS_fcntl, fd, cmd, reinterpret_cast<void *>(arg));
}
}
}
diff --git a/libc/test/src/fcntl/fcntl_test.cpp b/libc/test/src/fcntl/fcntl_test.cpp
index 81a7feaf151cf8..5cf73960734544 100644
--- a/libc/test/src/fcntl/fcntl_test.cpp
+++ b/libc/test/src/fcntl/fcntl_test.cpp
@@ -13,8 +13,6 @@
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
-#include <sys/stat.h>
-
TEST(LlvmLibcFcntlTest, FcntlDupfd) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/fcntl.test";
>From 4b5c842544d505b9144ccc70d3a173785e8f4f09 Mon Sep 17 00:00:00 2001
From: Vinayak Dev <vinayakdev.sci at gmail.com>
Date: Sun, 28 Apr 2024 23:58:32 +0530
Subject: [PATCH 3/6] Use internal types, fix breakages
---
libc/hdr/types/CMakeLists.txt | 8 ++++
libc/hdr/types/struct_f_owner_ex.h | 21 ++++++++++
libc/include/CMakeLists.txt | 4 ++
libc/include/llvm-libc-types/CMakeLists.txt | 1 +
.../llvm-libc-types/struct_f_owner_ex.h | 25 +++++++++++
libc/include/llvm-libc-types/struct_flock.h | 6 +--
libc/include/llvm-libc-types/struct_flock64.h | 4 +-
libc/src/fcntl/linux/CMakeLists.txt | 5 ++-
libc/src/fcntl/linux/fcntl.cpp | 42 +++++++------------
libc/test/src/fcntl/fcntl_test.cpp | 18 +++++---
10 files changed, 93 insertions(+), 41 deletions(-)
create mode 100644 libc/hdr/types/struct_f_owner_ex.h
create mode 100644 libc/include/llvm-libc-types/struct_f_owner_ex.h
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 3ba7853015209e..46a66ec590202d 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -30,6 +30,14 @@ add_proxy_header_library(
libc.include.llvm-libc-types.struct_flock64
)
+add_proxy_header_library(
+ struct_f_owner_ex
+ HDRS
+ struct_f_owner_ex.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_f_owner_ex
+)
+
add_proxy_header_library(
struct_timespec
HDRS
diff --git a/libc/hdr/types/struct_f_owner_ex.h b/libc/hdr/types/struct_f_owner_ex.h
new file mode 100644
index 00000000000000..49985115ae4bb2
--- /dev/null
+++ b/libc/hdr/types/struct_f_owner_ex.h
@@ -0,0 +1,21 @@
+//===-- Proxy for struct f_owner_ex --------------------------------------===//
+//
+// 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_HDR_TYPES_STRUCT_F_OWNER_EX_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_F_OWNER_EX_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_f_owner_ex.h"
+
+#else
+
+#include <fcntl.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_F_OWNER_EX_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index aeef46aabfce5c..578b1777251a87 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -43,6 +43,10 @@ add_gen_header(
DEPENDS
.llvm-libc-macros.fcntl_macros
.llvm-libc-types.mode_t
+ .llvm-libc-types.struct_flock
+ .llvm-libc-types.struct_flock64
+ .llvm-libc-types.off64
+ .llvm-libc-types.pid_t
.llvm-libc-types.off_t
.llvm_libc_common_h
)
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 1acc2881dcac9a..a5687d5bbc853f 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -62,6 +62,7 @@ add_header(stack_t HDR stack_t.h)
add_header(suseconds_t HDR suseconds_t.h)
add_header(struct_flock HDR struct_flock.h DEPENDS .off_t .pid_t)
add_header(struct_flock64 HDR struct_flock64.h DEPENDS .off64_t .pid_t)
+add_header(struct_f_owner_ex HDR struct_f_owner_ex.h DEPENDS .pid_t)
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
diff --git a/libc/include/llvm-libc-types/struct_f_owner_ex.h b/libc/include/llvm-libc-types/struct_f_owner_ex.h
new file mode 100644
index 00000000000000..c9cc85f69d2b45
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_f_owner_ex.h
@@ -0,0 +1,25 @@
+//===-- Definition of type struct f_owner_ex ------------------------------===//
+//
+// 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_TYPES_STRUCT_F_OWNER_EX_H
+#define LLVM_LIBC_TYPES_STRUCT_F_OWNER_EX_H
+
+#include "llvm-libc-types/pid_t.h"
+
+enum pid_type {
+ F_OWNER_TID = 0,
+ F_OWNER_PID,
+ F_OWNER_PGRP,
+};
+
+struct f_owner_ex {
+ enum pid_type type;
+ pid_t pid;
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_F_OWNER_EX_H
diff --git a/libc/include/llvm-libc-types/struct_flock.h b/libc/include/llvm-libc-types/struct_flock.h
index 6202c25f358517..da94343eaff512 100644
--- a/libc/include/llvm-libc-types/struct_flock.h
+++ b/libc/include/llvm-libc-types/struct_flock.h
@@ -12,9 +12,9 @@
#include "llvm-libc-types/off_t.h"
#include "llvm-libc-types/pid_t.h"
-struct flock64 {
- int16_t l_type;
- int16_t l_whence;
+struct flock {
+ short int l_type;
+ short int l_whence;
off_t l_start;
off_t l_len;
pid_t l_pid;
diff --git a/libc/include/llvm-libc-types/struct_flock64.h b/libc/include/llvm-libc-types/struct_flock64.h
index bec78ba8e4cb05..d4b141e91e44ef 100644
--- a/libc/include/llvm-libc-types/struct_flock64.h
+++ b/libc/include/llvm-libc-types/struct_flock64.h
@@ -13,8 +13,8 @@
#include "llvm-libc-types/pid_t.h"
struct flock64 {
- int16_t l_type;
- int16_t l_whence;
+ short int l_type;
+ short int l_whence;
off64_t l_start;
off64_t l_len;
pid_t l_pid;
diff --git a/libc/src/fcntl/linux/CMakeLists.txt b/libc/src/fcntl/linux/CMakeLists.txt
index 58509c6d58569a..732b7beac41bfb 100644
--- a/libc/src/fcntl/linux/CMakeLists.txt
+++ b/libc/src/fcntl/linux/CMakeLists.txt
@@ -18,8 +18,9 @@ add_entrypoint_object(
../fcntl.h
DEPENDS
libc.include.fcntl
- libc.include.llvm-libc-types.struct_flock
- libc.include.llvm-libc-types.struct_flock64
+ libc.hdr.types.struct_flock
+ libc.hdr.types.struct_flock64
+ libc.hdr.types.struct_f_owner_ex
libc.hdr.fcntl_macros
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
diff --git a/libc/src/fcntl/linux/fcntl.cpp b/libc/src/fcntl/linux/fcntl.cpp
index 311fd120bd0a94..24a20fb364109b 100644
--- a/libc/src/fcntl/linux/fcntl.cpp
+++ b/libc/src/fcntl/linux/fcntl.cpp
@@ -9,6 +9,7 @@
#include "src/fcntl/fcntl.h"
#include "hdr/fcntl_macros.h"
+#include "hdr/types/struct_f_owner_ex.h"
#include "hdr/types/struct_flock.h"
#include "hdr/types/struct_flock64.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
@@ -18,20 +19,6 @@
#include <stdarg.h>
#include <sys/syscall.h> // For syscall numbers.
-// To avoid conflict with system headers when building
-// in overlay mode.
-namespace helper {
-enum pid_type {
- F_OWNER_TID = 0,
- F_OWNER_PID,
- F_OWNER_PGRP,
-};
-struct fowner_ex {
- enum pid_type type;
- pid_t pid;
-};
-} // namespace helper
-
// The OFD file locks require special handling for LARGEFILES
namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
@@ -86,22 +73,21 @@ LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
flk->l_pid = flk64.l_pid;
return retVal;
}
- // The general case
- default: {
- if (cmd == F_GETOWN) {
- struct helper::fowner_ex fex;
- int retVal = syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex);
- if (retVal == -EINVAL)
- return syscall_impl<int>(SYS_fcntl, fd, cmd,
- reinterpret_cast<void *>(arg));
- if (static_cast<uint64_t>(retVal) <= -4096UL)
- return fex.type == helper::F_OWNER_PGRP ? -fex.pid : fex.pid;
+ case F_GETOWN: {
+ struct f_owner_ex fex;
+ int retVal = syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex);
+ if (retVal == -EINVAL)
+ return syscall_impl<int>(SYS_fcntl, fd, cmd,
+ reinterpret_cast<void *>(arg));
+ if (static_cast<unsigned long>(retVal) <= -4096UL)
+ return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;
- libc_errno = -retVal;
- return -1;
- }
- return syscall_impl<int>(SYS_fcntl, fd, cmd, reinterpret_cast<void *>(arg));
+ libc_errno = -retVal;
+ return -1;
}
+ // The general case
+ default:
+ return syscall_impl<int>(SYS_fcntl, fd, cmd, reinterpret_cast<void *>(arg));
}
}
} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/fcntl/fcntl_test.cpp b/libc/test/src/fcntl/fcntl_test.cpp
index 5cf73960734544..3f8151d90dfe30 100644
--- a/libc/test/src/fcntl/fcntl_test.cpp
+++ b/libc/test/src/fcntl/fcntl_test.cpp
@@ -15,7 +15,8 @@
TEST(LlvmLibcFcntlTest, FcntlDupfd) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
- constexpr const char *TEST_FILE = "testdata/fcntl.test";
+ constexpr const char *TEST_FILE_NAME = "testdata/fcntl_dup.test";
+ auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
int fd2, fd3;
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC, S_IRWXU);
ASSERT_ERRNO_SUCCESS();
@@ -36,7 +37,8 @@ TEST(LlvmLibcFcntlTest, FcntlDupfd) {
TEST(LlvmLibcFcntlTest, FcntlGetFl) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
- constexpr const char *TEST_FILE = "testdata/fcntl.test";
+ constexpr const char *TEST_FILE_NAME = "testdata/fcntl_getfl.test";
+ auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
int retVal;
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC, S_IRWXU);
ASSERT_ERRNO_SUCCESS();
@@ -51,7 +53,8 @@ TEST(LlvmLibcFcntlTest, FcntlGetFl) {
TEST(LlvmLibcFcntlTest, FcntlSetFl) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
- constexpr const char *TEST_FILE = "testdata/fcntl.test";
+ constexpr const char *TEST_FILE_NAME = "testdata/fcntl_setfl.test";
+ auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
int retVal;
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
@@ -85,11 +88,13 @@ TEST(LlvmLibcFcntlTest, FcntlSetFl) {
TEST(LlvmLibcFcntlTest, FcntlGetLkRead) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
- constexpr const char *TEST_FILE = "testdata/fcntl.test";
+ constexpr const char *TEST_FILE_NAME = "testdata/fcntl_getlkread.test";
+ auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
struct flock flk, svflk;
int retVal;
- int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDONLY);
+ int fd =
+ LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDONLY, S_IRWXU);
ASSERT_ERRNO_SUCCESS();
ASSERT_GT(fd, 0);
@@ -115,7 +120,8 @@ TEST(LlvmLibcFcntlTest, FcntlGetLkRead) {
TEST(LlvmLibcFcntlTest, FcntlGetLkWrite) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
- constexpr const char *TEST_FILE = "testdata/fcntl.test";
+ constexpr const char *TEST_FILE_NAME = "testdata/fcntl_getlkwrite.test";
+ auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
struct flock flk, svflk;
int retVal;
>From ee519c26592a3fa8501cce199c8b919ae1d90648 Mon Sep 17 00:00:00 2001
From: Vinayak Dev <vinayakdev.sci at gmail.com>
Date: Tue, 30 Apr 2024 22:08:48 +0530
Subject: [PATCH 4/6] Include stdint.h to use int16_t
---
libc/include/CMakeLists.txt | 2 +-
libc/include/llvm-libc-types/struct_flock.h | 6 ++++--
libc/include/llvm-libc-types/struct_flock64.h | 6 ++++--
3 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 578b1777251a87..7e087ccec59cd6 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -45,7 +45,7 @@ add_gen_header(
.llvm-libc-types.mode_t
.llvm-libc-types.struct_flock
.llvm-libc-types.struct_flock64
- .llvm-libc-types.off64
+ .llvm-libc-types.off64_t
.llvm-libc-types.pid_t
.llvm-libc-types.off_t
.llvm_libc_common_h
diff --git a/libc/include/llvm-libc-types/struct_flock.h b/libc/include/llvm-libc-types/struct_flock.h
index da94343eaff512..51c9d27640ea1a 100644
--- a/libc/include/llvm-libc-types/struct_flock.h
+++ b/libc/include/llvm-libc-types/struct_flock.h
@@ -12,9 +12,11 @@
#include "llvm-libc-types/off_t.h"
#include "llvm-libc-types/pid_t.h"
+#include <stdint.h>
+
struct flock {
- short int l_type;
- short int l_whence;
+ int16_t l_type;
+ int16_t l_whence;
off_t l_start;
off_t l_len;
pid_t l_pid;
diff --git a/libc/include/llvm-libc-types/struct_flock64.h b/libc/include/llvm-libc-types/struct_flock64.h
index d4b141e91e44ef..ac50003ca62f60 100644
--- a/libc/include/llvm-libc-types/struct_flock64.h
+++ b/libc/include/llvm-libc-types/struct_flock64.h
@@ -12,9 +12,11 @@
#include "llvm-libc-types/off64_t.h"
#include "llvm-libc-types/pid_t.h"
+#include <stdint.h>
+
struct flock64 {
- short int l_type;
- short int l_whence;
+ int16_t l_type;
+ int16_t l_whence;
off64_t l_start;
off64_t l_len;
pid_t l_pid;
>From e5130052009b46a50adc7bdcb94e7b94431e7c74 Mon Sep 17 00:00:00 2001
From: Vinayak Dev <vinayakdev.sci at gmail.com>
Date: Tue, 30 Apr 2024 23:01:59 +0530
Subject: [PATCH 5/6] Fix build failures in full build mode
---
libc/include/llvm-libc-macros/linux/fcntl-macros.h | 4 ++++
libc/test/src/fcntl/CMakeLists.txt | 2 ++
libc/test/src/fcntl/fcntl_test.cpp | 5 +++++
3 files changed, 11 insertions(+)
diff --git a/libc/include/llvm-libc-macros/linux/fcntl-macros.h b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
index c37ea3c110c14a..05e136f5bdbd3b 100644
--- a/libc/include/llvm-libc-macros/linux/fcntl-macros.h
+++ b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
@@ -46,6 +46,10 @@
#define O_RDWR 00000002
#define O_WRONLY 00000001
+#define F_RDLCK 0
+#define F_WRLCK 1
+#define F_UNLCK 2
+
// Special directory FD to indicate that the path argument to
// openat is relative to the current directory.
#define AT_FDCWD -100
diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index 1c99009d5a6d4e..aae296f074bea0 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -29,6 +29,8 @@ add_libc_unittest(
libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.unistd.close
+ libc.hdr.types.struct_flock
+ libc.hdr.fcntl_macros
libc.test.UnitTest.ErrnoSetterMatcher
)
diff --git a/libc/test/src/fcntl/fcntl_test.cpp b/libc/test/src/fcntl/fcntl_test.cpp
index 3f8151d90dfe30..c5cbb61b4ed8a5 100644
--- a/libc/test/src/fcntl/fcntl_test.cpp
+++ b/libc/test/src/fcntl/fcntl_test.cpp
@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
+#include "hdr/fcntl_macros.h"
+#include "hdr/types/struct_flock.h"
#include "src/errno/libc_errno.h"
#include "src/fcntl/fcntl.h"
#include "src/fcntl/open.h"
@@ -13,6 +15,9 @@
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
+#include <stdio.h>
+#include <sys/stat.h> // For S_IRWXU
+
TEST(LlvmLibcFcntlTest, FcntlDupfd) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_dup.test";
>From e1aa582187a68efa221f619b321e7c68a5c85f68 Mon Sep 17 00:00:00 2001
From: Vinayak Dev <vinayakdev.sci at gmail.com>
Date: Wed, 1 May 2024 17:59:30 +0530
Subject: [PATCH 6/6] Remove duplicate definitions
---
libc/include/llvm-libc-macros/linux/fcntl-macros.h | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/libc/include/llvm-libc-macros/linux/fcntl-macros.h b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
index 05e136f5bdbd3b..8ee95863728e15 100644
--- a/libc/include/llvm-libc-macros/linux/fcntl-macros.h
+++ b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
@@ -46,10 +46,6 @@
#define O_RDWR 00000002
#define O_WRONLY 00000001
-#define F_RDLCK 0
-#define F_WRLCK 1
-#define F_UNLCK 2
-
// Special directory FD to indicate that the path argument to
// openat is relative to the current directory.
#define AT_FDCWD -100
@@ -93,7 +89,7 @@
#define F_CLOEXEC 1
#define F_RDLCK 0
-#define F_WRLOCK 1
+#define F_WRLCK 1
#define F_UNLCK 2
// For Large File Support
More information about the libc-commits
mailing list