Skip to content

[libc][fcntl] Simplify the handling of the return value from syscall … #96325

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 3 commits into from
Jun 26, 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
10 changes: 3 additions & 7 deletions libc/src/__support/OSUtil/linux/fcntl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,11 @@ int fcntl(int fd, int cmd, void *arg) {
}
case F_GETOWN: {
struct f_owner_ex fex;
int retVal =
int ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex);
if (retVal == -EINVAL)
return LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, cmd,
reinterpret_cast<void *>(arg));
if (static_cast<unsigned long>(retVal) <= -4096UL)
if (ret >= 0)
return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;

libc_errno = -retVal;
libc_errno = -ret;
return -1;
}
// The general case
Expand Down
1 change: 1 addition & 0 deletions libc/test/src/fcntl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_libc_unittest(
libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.getpid
libc.hdr.types.struct_flock
libc.hdr.fcntl_macros
libc.test.UnitTest.ErrnoSetterMatcher
Expand Down
20 changes: 20 additions & 0 deletions libc/test/src/fcntl/fcntl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "src/fcntl/fcntl.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/getpid.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

Expand Down Expand Up @@ -163,3 +164,22 @@ TEST(LlvmLibcFcntlTest, UseAfterClose) {
ASSERT_EQ(-1, LIBC_NAMESPACE::fcntl(fd, F_GETFL));
ASSERT_ERRNO_EQ(EBADF);
}

TEST(LlvmLibcFcntlTest, SetGetOwnerTest) {
LIBC_NAMESPACE::libc_errno = 0;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
pid_t pid = LIBC_NAMESPACE::getpid();
ASSERT_GT(pid, -1);
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_set_get_owner.test";
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
ASSERT_ERRNO_SUCCESS();
ASSERT_GT(fd, 0);
int ret = LIBC_NAMESPACE::fcntl(fd, F_SETOWN, pid);
ASSERT_ERRNO_SUCCESS();
ASSERT_GT(ret, -1);
int ret2 = LIBC_NAMESPACE::fcntl(fd, F_GETOWN);
ASSERT_ERRNO_SUCCESS();
ASSERT_EQ(ret2, pid);
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
}
Loading