Skip to content

Commit 6d06ba6

Browse files
simonzgxAlexisPerry
authored andcommitted
[libc][fcntl] Simplify the handling of the return value from syscall … (llvm#96325)
Fixes llvm#95570
1 parent 81122ff commit 6d06ba6

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-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: 20 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,22 @@ 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+
LIBC_NAMESPACE::libc_errno = 0;
170+
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
171+
pid_t pid = LIBC_NAMESPACE::getpid();
172+
ASSERT_GT(pid, -1);
173+
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_set_get_owner.test";
174+
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
175+
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
176+
ASSERT_ERRNO_SUCCESS();
177+
ASSERT_GT(fd, 0);
178+
int ret = LIBC_NAMESPACE::fcntl(fd, F_SETOWN, pid);
179+
ASSERT_ERRNO_SUCCESS();
180+
ASSERT_GT(ret, -1);
181+
int ret2 = LIBC_NAMESPACE::fcntl(fd, F_GETOWN);
182+
ASSERT_ERRNO_SUCCESS();
183+
ASSERT_EQ(ret2, pid);
184+
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
185+
}

0 commit comments

Comments
 (0)