https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/237955
This PR move sendfile behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md. --- <sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub> >From a92fea7fe18f35d2828c42c07e30ab047e54d94e Mon Sep 17 00:00:00 2001 From: c8ef <[email protected]> Date: Mon, 2 Nov 2026 20:55:34 +0800 Subject: [PATCH] [libc] Use a syscall wrapper for sendfile This PR move sendfile behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md. --- .../linux/syscall_wrappers/CMakeLists.txt | 15 ++++++ .../OSUtil/linux/syscall_wrappers/sendfile.h | 46 +++++++++++++++++++ libc/src/sys/sendfile/linux/CMakeLists.txt | 6 ++- libc/src/sys/sendfile/linux/sendfile.cpp | 22 ++------- .../llvm-project-overlay/libc/BUILD.bazel | 40 ++++++++++++++++ 5 files changed, 110 insertions(+), 19 deletions(-) create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/sendfile.h diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt index a5f464b015f31c..ce84f2277bb3c4 100644 --- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt @@ -516,6 +516,21 @@ add_header_library( libc.include.sys_syscall ) +add_header_library( + sendfile + HDRS + sendfile.h + DEPENDS + libc.hdr.types.off_t + libc.hdr.types.size_t + libc.hdr.types.ssize_t + libc.include.sys_syscall + libc.src.__support.OSUtil.osutil + libc.src.__support.common + libc.src.__support.error_or + libc.src.__support.macros.config +) + add_header_library( sendmmsg HDRS diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/sendfile.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/sendfile.h new file mode 100644 index 00000000000000..9596c7f700942c --- /dev/null +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/sendfile.h @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// 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 sendfile. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SENDFILE_H +#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SENDFILE_H + +#include "hdr/types/off_t.h" +#include "hdr/types/size_t.h" +#include "hdr/types/ssize_t.h" +#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" + +#include <sys/syscall.h> // For syscall numbers + +namespace LIBC_NAMESPACE_DECL { +namespace linux_syscalls { + +LIBC_INLINE ErrorOr<ssize_t> sendfile(int out_fd, int in_fd, off_t *offset, + size_t count) { +#ifdef SYS_sendfile + return syscall_checked<ssize_t>(SYS_sendfile, out_fd, in_fd, offset, count); +#elif defined(SYS_sendfile64) + // Same as sendfile but can handle large offsets. + static_assert(sizeof(off_t) == 8); + return syscall_checked<ssize_t>(SYS_sendfile64, out_fd, in_fd, offset, count); +#else +#error "sendfile and sendfile64 syscalls not available." +#endif +} + +} // namespace linux_syscalls +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SENDFILE_H diff --git a/libc/src/sys/sendfile/linux/CMakeLists.txt b/libc/src/sys/sendfile/linux/CMakeLists.txt index 2998fef2e92b4d..dd6c1f32072d94 100644 --- a/libc/src/sys/sendfile/linux/CMakeLists.txt +++ b/libc/src/sys/sendfile/linux/CMakeLists.txt @@ -8,7 +8,9 @@ add_entrypoint_object( libc.hdr.types.off_t libc.hdr.types.size_t libc.hdr.types.ssize_t - libc.include.sys_syscall - libc.src.__support.OSUtil.osutil + libc.src.__support.OSUtil.linux.syscall_wrappers.sendfile + libc.src.__support.common + libc.src.__support.libc_errno + libc.src.__support.macros.config libc.src.errno.errno ) diff --git a/libc/src/sys/sendfile/linux/sendfile.cpp b/libc/src/sys/sendfile/linux/sendfile.cpp index f7321c3eff305b..082bcaafa2f4e8 100644 --- a/libc/src/sys/sendfile/linux/sendfile.cpp +++ b/libc/src/sys/sendfile/linux/sendfile.cpp @@ -8,33 +8,21 @@ #include "src/sys/sendfile/sendfile.h" -#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/OSUtil/linux/syscall_wrappers/sendfile.h" #include "src/__support/common.h" - #include "src/__support/libc_errno.h" #include "src/__support/macros/config.h" -#include <sys/syscall.h> // For syscall numbers. namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(ssize_t, sendfile, (int out_fd, int in_fd, off_t *offset, size_t count)) { -#ifdef SYS_sendfile - ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendfile, out_fd, - in_fd, offset, count); -#elif defined(SYS_sendfile64) - // Same as sendfile but can handle large offsets - static_assert(sizeof(off_t) == 8); - ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendfile64, out_fd, - in_fd, offset, count); -#else -#error "sendfile and sendfile64 syscalls not available." -#endif - if (ret < 0) { - libc_errno = static_cast<int>(-ret); + auto result = linux_syscalls::sendfile(out_fd, in_fd, offset, count); + if (!result) { + libc_errno = result.error(); return -1; } - return ret; + return result.value(); } } // namespace LIBC_NAMESPACE_DECL diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index ade5f28f6e736c..1350cfeb1ec6e3 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -3199,6 +3199,24 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_osutil_linux_syscall_wrappers_sendfile", + hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/sendfile.h"], + target_compatible_with = select({ + "@platforms//os:linux": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + deps = [ + ":__support_common", + ":__support_error_or", + ":__support_macros_config", + ":__support_osutil_syscall", + ":types_off_t", + ":types_size_t", + ":types_ssize_t", + ], +) + libc_support_library( name = "__support_osutil_linux_syscall_wrappers_sendmsg", hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/sendmsg.h"], @@ -19818,6 +19836,28 @@ libc_function( ], ) +############################# sys/sendfile targets ############################# + +libc_function( + name = "sendfile", + srcs = ["src/sys/sendfile/linux/sendfile.cpp"], + hdrs = ["src/sys/sendfile/sendfile.h"], + target_compatible_with = select({ + "@platforms//os:linux": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + deps = [ + ":__support_common", + ":__support_libc_errno", + ":__support_macros_config", + ":__support_osutil_linux_syscall_wrappers_sendfile", + ":errno", + ":types_off_t", + ":types_size_t", + ":types_ssize_t", + ], +) + ############################### sys/stat targets ############################### libc_function( _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
