[libc-commits] [libc] [libc] Migrate file I/O syscall wrappers to syscall_checked (PR #226477)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Fri Sep 25 06:03:27 PDT 2026


https://github.com/labath created https://github.com/llvm/llvm-project/pull/226477

Migrate a batch of related file descriptor and core I/O syscall wrappers to use syscall_checked instead of manual syscall_impl error checking.

While in there:
- update file headers to the new style
- reformat includes into a single block so clang-format can sort them

Assisted-by: Gemini

>From 4954b022600677738300a62ae8cb05a929e94351 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 25 Sep 2026 12:55:06 +0000
Subject: [PATCH] [libc] Migrate file I/O syscall wrappers to syscall_checked

Migrate a batch of related file descriptor and core I/O syscall wrappers
to use syscall_checked instead of manual syscall_impl error checking.

While in there:
- update file headers to the new style
- reformat includes into a single block so clang-format can sort them

Assisted-by: Gemini
---
 .../OSUtil/linux/syscall_wrappers/close.h     | 14 ++++++-----
 .../OSUtil/linux/syscall_wrappers/dup.h       |  7 ++----
 .../OSUtil/linux/syscall_wrappers/dup2.h      |  9 +++----
 .../OSUtil/linux/syscall_wrappers/dup3.h      |  7 ++----
 .../OSUtil/linux/syscall_wrappers/fcntl.h     | 24 +++++++------------
 .../OSUtil/linux/syscall_wrappers/fsync.h     |  7 ++----
 .../OSUtil/linux/syscall_wrappers/ftruncate.h | 16 +++++++------
 .../OSUtil/linux/syscall_wrappers/lseek.h     | 20 +++++++---------
 .../linux/syscall_wrappers/memfd_create.h     |  7 ++----
 .../OSUtil/linux/syscall_wrappers/read.h      | 14 ++++++-----
 .../OSUtil/linux/syscall_wrappers/write.h     | 14 ++++++-----
 11 files changed, 62 insertions(+), 77 deletions(-)

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/close.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/close.h
index fe305af5051c09..88c71d9d5af74b 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/close.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/close.h
@@ -1,15 +1,20 @@
-//===-- Implementation header for close -------------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for close.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOSE_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOSE_H
 
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -19,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> close(int fd) {
-  int ret = syscall_impl<int>(SYS_close, fd);
-  if (ret < 0)
-    return Error(-static_cast<int>(ret));
-  return ret;
+  return syscall_checked<int>(SYS_close, fd);
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup.h
index 3d54b684bd66e9..9d61414993072c 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup.h
@@ -14,7 +14,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP_H
 
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -24,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> dup(int fd) {
-  int ret = syscall_impl<int>(SYS_dup, fd);
-  if (ret < 0)
-    return Error(-ret);
-  return ret;
+  return syscall_checked<int>(SYS_dup, fd);
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup2.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup2.h
index 78da9801895abb..55687c3dad9c79 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup2.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup2.h
@@ -15,7 +15,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP2_H
 
 #include "hdr/fcntl_macros.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/OSUtil/linux/syscall_wrappers/fcntl.h"
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
@@ -27,7 +27,7 @@ namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> dup2(int oldfd, int newfd) {
 #ifdef SYS_dup2
-  int ret = syscall_impl<int>(SYS_dup2, oldfd, newfd);
+  return syscall_checked<int>(SYS_dup2, oldfd, newfd);
 #elif defined(SYS_dup3)
   if (oldfd == newfd) {
     auto ret = fcntl(oldfd, F_GETFD);
@@ -35,13 +35,10 @@ LIBC_INLINE ErrorOr<int> dup2(int oldfd, int newfd) {
       return Error(ret.error());
     return oldfd;
   }
-  int ret = syscall_impl<int>(SYS_dup3, oldfd, newfd, 0);
+  return syscall_checked<int>(SYS_dup3, oldfd, newfd, 0);
 #else
 #error "dup2 and dup3 syscalls not available."
 #endif
-  if (ret < 0)
-    return Error(-ret);
-  return ret;
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup3.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup3.h
index 3be0ad4526e90a..e112cb85d8f644 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup3.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup3.h
@@ -14,7 +14,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP3_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP3_H
 
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -24,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> dup3(int oldfd, int newfd, int flags) {
-  int ret = syscall_impl<int>(SYS_dup3, oldfd, newfd, flags);
-  if (ret < 0)
-    return Error(-ret);
-  return ret;
+  return syscall_checked<int>(SYS_dup3, oldfd, newfd, flags);
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fcntl.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fcntl.h
index 0581693f9612c8..5bc3482bfe192c 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/fcntl.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fcntl.h
@@ -20,7 +20,7 @@
 #include "hdr/types/struct_f_owner_ex.h"
 #include "hdr/types/struct_flock.h"
 #include "hdr/types/struct_flock64.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -49,10 +49,7 @@ LIBC_INLINE ErrorOr<int> fcntl(int fd, int cmd, void *arg = nullptr) {
     flk64.l_len = flk->l_len;
     flk64.l_pid = flk->l_pid;
     // create a syscall
-    int ret = syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
-    if (ret < 0)
-      return Error(-ret);
-    return ret;
+    return syscall_checked<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
   }
   case F_OFD_GETLK:
   case F_OFD_SETLK: {
@@ -65,10 +62,10 @@ LIBC_INLINE ErrorOr<int> fcntl(int fd, int cmd, void *arg = nullptr) {
     flk64.l_len = flk->l_len;
     flk64.l_pid = flk->l_pid;
     // create a syscall
-    int ret = syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
+    auto ret = syscall_checked<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
     // On failure, return
-    if (ret < 0)
-      return Error(-ret);
+    if (!ret)
+      return ret;
     // Check for overflow, i.e. the offsets are not the same when cast
     // to off_t from off64_t.
     if (static_cast<off_t>(flk64.l_len) != flk64.l_len ||
@@ -85,9 +82,9 @@ LIBC_INLINE ErrorOr<int> fcntl(int fd, int cmd, void *arg = nullptr) {
   }
   case F_GETOWN: {
     struct f_owner_ex fex;
-    int ret = syscall_impl<int>(FCNTL_SYSCALL_ID, fd, F_GETOWN_EX, &fex);
-    if (ret < 0)
-      return Error(-ret);
+    auto ret = syscall_checked<int>(FCNTL_SYSCALL_ID, fd, F_GETOWN_EX, &fex);
+    if (!ret)
+      return ret;
     return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;
   }
 #ifdef SYS_fcntl64
@@ -112,10 +109,7 @@ LIBC_INLINE ErrorOr<int> fcntl(int fd, int cmd, void *arg = nullptr) {
   // Plain passthrough for all other commands. When only SYS_fcntl64 is
   // available, F_GETLK/F_SETLK/F_SETLKW have been rewritten to their 64-bit
   // variants by the cases above.
-  int ret = syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, arg);
-  if (ret < 0)
-    return Error(-ret);
-  return ret;
+  return syscall_checked<int>(FCNTL_SYSCALL_ID, fd, cmd, arg);
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fsync.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fsync.h
index 1ebbca23f30194..4b0a864b6992e0 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/fsync.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fsync.h
@@ -14,7 +14,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FSYNC_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FSYNC_H
 
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -24,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> fsync(int fd) {
-  int ret = syscall_impl<int>(SYS_fsync, fd);
-  if (ret < 0)
-    return Error(-ret);
-  return ret;
+  return syscall_checked<int>(SYS_fsync, fd);
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h
index 6835de85d33211..76d692d589ec44 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h
@@ -1,16 +1,21 @@
-//===-- Implementation header for ftruncate ---------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for ftruncate.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_FTRUNCATE_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_FTRUNCATE_H
 
 #include "hdr/types/off_t.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -21,18 +26,15 @@ namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> ftruncate(int fd, off_t len) {
 #ifdef SYS_ftruncate
-  int ret = syscall_impl<int>(SYS_ftruncate, fd, len);
+  return syscall_checked<int>(SYS_ftruncate, fd, len);
 #elif defined(SYS_ftruncate64)
   // Same as ftruncate but can handle large offsets on 32-bit systems.
   static_assert(sizeof(off_t) == 8);
-  int ret = syscall_impl<int>(SYS_ftruncate64, fd, (long)len,
+  return syscall_checked<int>(SYS_ftruncate64, fd, (long)len,
                               (long)(((uint64_t)(len)) >> 32));
 #else
 #error "ftruncate and ftruncate64 syscalls not available."
 #endif
-  if (ret < 0)
-    return Error(-static_cast<int>(ret));
-  return 0;
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/lseek.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/lseek.h
index 861a1174d367f2..30b17328a8f178 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/lseek.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/lseek.h
@@ -16,7 +16,7 @@
 
 #include "hdr/stdint_proxy.h"
 #include "hdr/types/off_t.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -26,27 +26,25 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<off_t> lseek(int fd, off_t offset, int whence) {
-  off_t result;
 #ifdef SYS_lseek
-  result = syscall_impl<off_t>(SYS_lseek, fd, offset, whence);
-  if (result < 0)
-    return Error(-static_cast<int>(result));
+  return syscall_checked<off_t>(SYS_lseek, fd, offset, whence);
 #elif defined(SYS_llseek) || defined(SYS__llseek)
 #ifdef SYS_llseek
   constexpr long LLSEEK_SYSCALL_NO = SYS_llseek;
 #elif defined(SYS__llseek)
   constexpr long LLSEEK_SYSCALL_NO = SYS__llseek;
 #endif
+  off_t result;
   uint64_t offset_64 = static_cast<uint64_t>(offset);
-  int ret = syscall_impl<int>(LLSEEK_SYSCALL_NO, fd,
-                              static_cast<long>(offset_64 >> 32),
-                              static_cast<long>(offset_64), &result, whence);
-  if (ret < 0)
-    return Error(-ret);
+  auto ret = syscall_checked<int>(
+      LLSEEK_SYSCALL_NO, fd, static_cast<long>(offset_64 >> 32),
+      static_cast<long>(offset_64), &result, whence);
+  if (!ret)
+    return Error(ret.error());
+  return result;
 #else
 #error "lseek, llseek and _llseek syscalls not available."
 #endif
-  return result;
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/memfd_create.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/memfd_create.h
index e748c3c6fb80db..b54ffbbf6e8fcd 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/memfd_create.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/memfd_create.h
@@ -14,7 +14,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MEMFD_CREATE_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MEMFD_CREATE_H
 
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -24,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> memfd_create(const char *name, unsigned int flags) {
-  int ret = syscall_impl<int>(SYS_memfd_create, name, flags);
-  if (ret < 0)
-    return Error(-ret);
-  return ret;
+  return syscall_checked<int>(SYS_memfd_create, name, flags);
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/read.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/read.h
index 86123904fc6b25..c30a0bb3480e95 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/read.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/read.h
@@ -1,16 +1,21 @@
-//===-- Implementation header for read --------------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for read.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_READ_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_READ_H
 
 #include "hdr/types/ssize_t.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -20,10 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<ssize_t> read(int fd, void *buf, size_t count) {
-  ssize_t ret = syscall_impl<ssize_t>(SYS_read, fd, buf, count);
-  if (ret < 0)
-    return Error(-static_cast<int>(ret));
-  return ret;
+  return syscall_checked<ssize_t>(SYS_read, fd, buf, count);
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/write.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/write.h
index e57986d357a216..d253ee68d764cb 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/write.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/write.h
@@ -1,16 +1,21 @@
-//===-- Implementation header for write -------------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for write.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_WRITE_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_WRITE_H
 
 #include "hdr/types/ssize_t.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -20,10 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<ssize_t> write(int fd, const void *buf, size_t count) {
-  ssize_t ret = syscall_impl<ssize_t>(SYS_write, fd, buf, count);
-  if (ret < 0)
-    return Error(-static_cast<int>(ret));
-  return ret;
+  return syscall_checked<ssize_t>(SYS_write, fd, buf, count);
 }
 
 } // namespace linux_syscalls



More information about the libc-commits mailing list