[libc-commits] [libc] [libc] Change fcntl cmd when only fcntl64 is available (PR #99675)

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Sat Jul 20 08:38:39 PDT 2024


https://github.com/mikhailramalho updated https://github.com/llvm/llvm-project/pull/99675

>From 19776b14d4e1dcb538b96edeb6e50a30592102e3 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Fri, 19 Jul 2024 14:53:16 -0300
Subject: [PATCH] [libc] Change fcntl cmd when only fcntl64 is available

In some systems like rv32, only fcntl64 is available and it employs a
different structure for file locking and the correspoding F_GETLK64,
F_SETLK64, and F_SETLKW64 commands.

So if we use fcntl64, the F_GETLK, F_SETLK, and F_SETLKW commands need
to be changed to their 64 versions. This patch adds new cases to the
swich(cmd) in our implementation of fcntl to do that.

The default case was moved to outside the switch, so we don't need to
change anything, the F_GETLK, F_SETLK, and F_SETLKW commands will just
go through the old implementation.
---
 libc/src/__support/OSUtil/linux/fcntl.cpp | 32 ++++++++++++++++-------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/libc/src/__support/OSUtil/linux/fcntl.cpp b/libc/src/__support/OSUtil/linux/fcntl.cpp
index e11013121b04b..7731aa04a178d 100644
--- a/libc/src/__support/OSUtil/linux/fcntl.cpp
+++ b/libc/src/__support/OSUtil/linux/fcntl.cpp
@@ -86,17 +86,31 @@ int fcntl(int fd, int cmd, void *arg) {
     libc_errno = -ret;
     return -1;
   }
-  // The general case
-  default: {
-    int retVal = LIBC_NAMESPACE::syscall_impl<int>(
-        FCNTL_SYSCALL_ID, fd, cmd, reinterpret_cast<void *>(arg));
-    if (retVal >= 0) {
-      return retVal;
-    }
-    libc_errno = -retVal;
-    return -1;
+#ifdef SYS_fcntl64
+  case F_GETLK: {
+    if constexpr (FCNTL_SYSCALL_ID == SYS_fcntl64)
+      return fcntl(fd, F_GETLK64, arg);
+    break;
+  }
+  case F_SETLK: {
+    if constexpr (FCNTL_SYSCALL_ID == SYS_fcntl64)
+      return fcntl(fd, F_SETLK64, arg);
+    break;
+  }
+  case F_SETLKW: {
+    if constexpr (FCNTL_SYSCALL_ID == SYS_fcntl64)
+      return fcntl(fd, F_SETLKW64, arg);
+    break;
   }
+#endif
+  }
+  int retVal = LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd,
+                                                 reinterpret_cast<void *>(arg));
+  if (retVal >= 0) {
+    return retVal;
   }
+  libc_errno = -retVal;
+  return -1;
 }
 
 } // namespace internal



More information about the libc-commits mailing list