Skip to content

[libc] Implement fcntl when only SYS_fcntl64 is available #97740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions libc/src/__support/OSUtil/linux/fcntl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
namespace LIBC_NAMESPACE::internal {

int fcntl(int fd, int cmd, void *arg) {
#if SYS_fcntl
constexpr auto FCNTL_SYSCALL_ID = SYS_fcntl;
#elif defined(SYS_fcntl64)
constexpr auto FCNTL_SYSCALL_ID = SYS_fcntl64;
#else
#error "fcntl and fcntl64 syscalls not available."
#endif

switch (cmd) {
case F_OFD_SETLKW: {
struct flock *flk = reinterpret_cast<struct flock *>(arg);
Expand All @@ -33,7 +41,7 @@ int fcntl(int fd, int cmd, void *arg) {
flk64.l_len = flk->l_len;
flk64.l_pid = flk->l_pid;
// create a syscall
return LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, cmd, &flk64);
return LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
}
case F_OFD_GETLK:
case F_OFD_SETLK: {
Expand All @@ -46,7 +54,8 @@ int fcntl(int fd, int cmd, void *arg) {
flk64.l_len = flk->l_len;
flk64.l_pid = flk->l_pid;
// create a syscall
int retVal = LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, cmd, &flk64);
int retVal =
LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
// On failure, return
if (retVal == -1)
return -1;
Expand All @@ -67,8 +76,8 @@ int fcntl(int fd, int cmd, void *arg) {
}
case F_GETOWN: {
struct f_owner_ex fex;
int ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex);
int ret = LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd,
F_GETOWN_EX, &fex);
if (ret >= 0)
return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;
libc_errno = -ret;
Expand All @@ -77,7 +86,7 @@ int fcntl(int fd, int cmd, void *arg) {
// The general case
default: {
int retVal = LIBC_NAMESPACE::syscall_impl<int>(
SYS_fcntl, fd, cmd, reinterpret_cast<void *>(arg));
FCNTL_SYSCALL_ID, fd, cmd, reinterpret_cast<void *>(arg));
if (retVal >= 0) {
return retVal;
}
Expand Down
1 change: 1 addition & 0 deletions libc/test/src/sys/mman/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ add_libc_unittest(
libc.include.sys_mman
libc.include.sys_syscall
libc.src.errno.errno
libc.src.fcntl.fcntl
libc.src.sys.mman.shm_open
libc.src.sys.mman.shm_unlink
libc.src.sys.mman.mmap
Expand Down
5 changes: 2 additions & 3 deletions libc/test/src/sys/mman/linux/shm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/__support/OSUtil/syscall.h"
#include "src/fcntl/fcntl.h"
#include "src/sys/mman/mmap.h"
#include "src/sys/mman/munmap.h"
#include "src/sys/mman/shm_open.h"
Expand All @@ -29,9 +30,7 @@ TEST(LlvmLibcShmTest, Basic) {
returns(GE(0)).with_errno(EQ(0)));

// check that FD_CLOEXEC is set by default.
// TODO: use fcntl when implemented.
// https://github.com/llvm/llvm-project/issues/84968
long flag = LIBC_NAMESPACE::syscall_impl(SYS_fcntl, fd, F_GETFD);
long flag = LIBC_NAMESPACE::fcntl(fd, F_GETFD);
ASSERT_GE(static_cast<int>(flag), 0);
EXPECT_NE(static_cast<int>(flag) & FD_CLOEXEC, 0);

Expand Down
Loading