Skip to content

Commit b25d759

Browse files
committed
[libc][fcntl] Simplify the handling of the return value from syscall for the F_GETOWN command.
1 parent 30299b8 commit b25d759

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ int fcntl(int fd, int cmd, void *arg) {
6767
}
6868
case F_GETOWN: {
6969
struct f_owner_ex fex;
70-
int retVal =
70+
int ret =
7171
LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex);
72-
if (retVal == -EINVAL)
73-
return LIBC_NAMESPACE::syscall_impl<int>(SYS_fcntl, fd, cmd,
74-
reinterpret_cast<void *>(arg));
75-
if (static_cast<unsigned long>(retVal) <= -4096UL)
72+
if (ret >= 0)
7673
return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;
77-
78-
libc_errno = -retVal;
74+
libc_errno = -ret;
7975
return -1;
8076
}
8177
// The general case

libc/test/src/fcntl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_libc_unittest(
2929
libc.src.fcntl.fcntl
3030
libc.src.fcntl.open
3131
libc.src.unistd.close
32+
libc.src.unistd.getpid
3233
libc.hdr.types.struct_flock
3334
libc.hdr.fcntl_macros
3435
libc.test.UnitTest.ErrnoSetterMatcher

libc/test/src/fcntl/fcntl_test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/fcntl/fcntl.h"
1313
#include "src/fcntl/open.h"
1414
#include "src/unistd/close.h"
15+
#include "src/unistd/getpid.h"
1516
#include "test/UnitTest/ErrnoSetterMatcher.h"
1617
#include "test/UnitTest/Test.h"
1718

@@ -163,3 +164,21 @@ TEST(LlvmLibcFcntlTest, UseAfterClose) {
163164
ASSERT_EQ(-1, LIBC_NAMESPACE::fcntl(fd, F_GETFL));
164165
ASSERT_ERRNO_EQ(EBADF);
165166
}
167+
168+
TEST(LlvmLibcFcntlTest, SetGetOwnerTest) {
169+
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
170+
pid_t pid = LIBC_NAMESPACE::getpid();
171+
ASSERT_GT(pid, -1);
172+
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_set_get_owner.test";
173+
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
174+
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
175+
ASSERT_ERRNO_SUCCESS();
176+
ASSERT_GT(fd, 0);
177+
int ret = LIBC_NAMESPACE::fcntl(fd, F_SETOWN, pid);
178+
ASSERT_ERRNO_SUCCESS();
179+
ASSERT_GT(ret, -1);
180+
int ret2 = LIBC_NAMESPACE::fcntl(fd, F_GETOWN);
181+
ASSERT_ERRNO_SUCCESS();
182+
ASSERT_EQ(ret2, pid);
183+
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
184+
}

0 commit comments

Comments
 (0)