xref: /aosp_15_r20/external/llvm-libc/src/sys/sendfile/linux/sendfile.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of sendfile ----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/sys/sendfile/sendfile.h"
10 
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
13 
14 #include "src/__support/macros/config.h"
15 #include "src/errno/libc_errno.h"
16 #include <sys/sendfile.h>
17 #include <sys/syscall.h> // For syscall numbers.
18 
19 namespace LIBC_NAMESPACE_DECL {
20 
21 LLVM_LIBC_FUNCTION(ssize_t, sendfile,
22                    (int out_fd, int in_fd, off_t *offset, size_t count)) {
23 #ifdef SYS_sendfile
24   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendfile, in_fd,
25                                                       out_fd, offset, count);
26 #elif defined(SYS_sendfile64)
27   // Same as sendfile but can handle large offsets
28   static_assert(sizeof(off_t) == 8);
29   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendfile64, in_fd,
30                                                       out_fd, offset, count);
31 #else
32 #error "sendfile and sendfile64 syscalls not available."
33 #endif
34   if (ret < 0) {
35     libc_errno = static_cast<int>(-ret);
36     return -1;
37   }
38   return ret;
39 }
40 
41 } // namespace LIBC_NAMESPACE_DECL
42