xref: /aosp_15_r20/external/llvm-libc/src/sys/socket/linux/recv.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of recv --------------------------------------===//
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/socket/recv.h"
10 
11 #include <linux/net.h>   // For SYS_SOCKET socketcall number.
12 #include <sys/syscall.h> // For syscall numbers.
13 
14 #include "hdr/types/socklen_t.h"
15 #include "hdr/types/ssize_t.h"
16 #include "hdr/types/struct_sockaddr.h"
17 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
18 #include "src/__support/common.h"
19 #include "src/__support/macros/sanitizer.h"
20 #include "src/errno/libc_errno.h"
21 
22 namespace LIBC_NAMESPACE_DECL {
23 
24 LLVM_LIBC_FUNCTION(ssize_t, recv,
25                    (int sockfd, void *buf, size_t len, int flags)) {
26 #ifdef SYS_recv
27   ssize_t ret =
28       LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_recv, sockfd, buf, len, flags);
29 #elif defined(SYS_recvfrom)
30   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
31       SYS_recvfrom, sockfd, buf, len, flags, nullptr, nullptr);
32 #elif defined(SYS_socketcall)
33   unsigned long sockcall_args[4] = {
34       static_cast<unsigned long>(sockfd), reinterpret_cast<unsigned long>(buf),
35       static_cast<unsigned long>(len), static_cast<unsigned long>(flags)};
36   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_socketcall, SYS_RECV,
37                                                       sockcall_args);
38 #else
39 #error "socket and socketcall syscalls unavailable for this platform."
40 #endif
41   if (ret < 0) {
42     libc_errno = static_cast<int>(-ret);
43     return -1;
44   }
45 
46   MSAN_UNPOISON(buf, ret);
47 
48   return ret;
49 }
50 
51 } // namespace LIBC_NAMESPACE_DECL
52