Skip to content

Commit 4f77677

Browse files
[libc] Implement fcntl when only SYS_fcntl64 is available (#97740)
This patch tries to implement fcntl with either SYS_fcntl or SYS_fcntl64, and also changes a test case to call the fcntl function instead of using the syscall
1 parent 834ecc8 commit 4f77677

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

libc/src/__support/OSUtil/linux/fcntl.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
namespace LIBC_NAMESPACE::internal {
2323

2424
int fcntl(int fd, int cmd, void *arg) {
25+
#if SYS_fcntl
26+
constexpr auto FCNTL_SYSCALL_ID = SYS_fcntl;
27+
#elif defined(SYS_fcntl64)
28+
constexpr auto FCNTL_SYSCALL_ID = SYS_fcntl64;
29+
#else
30+
#error "fcntl and fcntl64 syscalls not available."
31+
#endif
32+
2533
switch (cmd) {
2634
case F_OFD_SETLKW: {
2735
struct flock *flk = reinterpret_cast<struct flock *>(arg);
@@ -33,7 +41,7 @@ int fcntl(int fd, int cmd, void *arg) {
3341
flk64.l_len = flk->l_len;
3442
flk64.l_pid = flk->l_pid;
3543
// create a syscall
36-
return LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, cmd, &flk64);
44+
return LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
3745
}
3846
case F_OFD_GETLK:
3947
case F_OFD_SETLK: {
@@ -46,7 +54,8 @@ int fcntl(int fd, int cmd, void *arg) {
4654
flk64.l_len = flk->l_len;
4755
flk64.l_pid = flk->l_pid;
4856
// create a syscall
49-
int retVal = LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, cmd, &flk64);
57+
int retVal =
58+
LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
5059
// On failure, return
5160
if (retVal == -1)
5261
return -1;
@@ -67,8 +76,8 @@ int fcntl(int fd, int cmd, void *arg) {
6776
}
6877
case F_GETOWN: {
6978
struct f_owner_ex fex;
70-
int ret =
71-
LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex);
79+
int ret = LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd,
80+
F_GETOWN_EX, &fex);
7281
if (ret >= 0)
7382
return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;
7483
libc_errno = -ret;
@@ -77,7 +86,7 @@ int fcntl(int fd, int cmd, void *arg) {
7786
// The general case
7887
default: {
7988
int retVal = LIBC_NAMESPACE::syscall_impl<int>(
80-
SYS_fcntl, fd, cmd, reinterpret_cast<void *>(arg));
89+
FCNTL_SYSCALL_ID, fd, cmd, reinterpret_cast<void *>(arg));
8190
if (retVal >= 0) {
8291
return retVal;
8392
}

libc/test/src/sys/mman/linux/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ add_libc_unittest(
138138
libc.include.sys_mman
139139
libc.include.sys_syscall
140140
libc.src.errno.errno
141+
libc.src.fcntl.fcntl
141142
libc.src.sys.mman.shm_open
142143
libc.src.sys.mman.shm_unlink
143144
libc.src.sys.mman.mmap

libc/test/src/sys/mman/linux/shm_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

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

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

0 commit comments

Comments
 (0)