xref: /llvm-project/libc/src/sys/socket/linux/recvmsg.cpp (revision fedb9fdb98314ff0ddff065dbd6ef8b2b7e6ec96)
1f6b4c34dSMichael Jones //===-- Linux implementation of recvmsg -----------------------------------===//
2f6b4c34dSMichael Jones //
3f6b4c34dSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4f6b4c34dSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5f6b4c34dSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f6b4c34dSMichael Jones //
7f6b4c34dSMichael Jones //===----------------------------------------------------------------------===//
8f6b4c34dSMichael Jones 
9f6b4c34dSMichael Jones #include "src/sys/socket/recvmsg.h"
10f6b4c34dSMichael Jones 
11ef66936dSMichael Jones #include <linux/net.h>   // For SYS_SOCKET socketcall number.
12ef66936dSMichael Jones #include <sys/syscall.h> // For syscall numbers.
13ef66936dSMichael Jones 
14f6b4c34dSMichael Jones #include "hdr/types/ssize_t.h"
15f6b4c34dSMichael Jones #include "hdr/types/struct_msghdr.h"
16f6b4c34dSMichael Jones #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
17f6b4c34dSMichael Jones #include "src/__support/common.h"
18aeb18ebbSMichael Jones #include "src/__support/macros/sanitizer.h"
19f6b4c34dSMichael Jones #include "src/errno/libc_errno.h"
20f6b4c34dSMichael Jones 
21f6b4c34dSMichael Jones namespace LIBC_NAMESPACE_DECL {
22f6b4c34dSMichael Jones 
23*fedb9fdbSMichael Jones LLVM_LIBC_FUNCTION(ssize_t, recvmsg, (int sockfd, msghdr *msg, int flags)) {
24f6b4c34dSMichael Jones #ifdef SYS_recvmsg
25ef66936dSMichael Jones   ssize_t ret =
26ef66936dSMichael Jones       LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_recvmsg, sockfd, msg, flags);
27f6b4c34dSMichael Jones #elif defined(SYS_socketcall)
28f6b4c34dSMichael Jones   unsigned long sockcall_args[3] = {static_cast<unsigned long>(sockfd),
29f6b4c34dSMichael Jones                                     reinterpret_cast<unsigned long>(msg),
30f6b4c34dSMichael Jones                                     static_cast<unsigned long>(flags)};
31ef66936dSMichael Jones   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
32ef66936dSMichael Jones       SYS_socketcall, SYS_RECVMSG, sockcall_args);
33f6b4c34dSMichael Jones #else
34f6b4c34dSMichael Jones #error "socket and socketcall syscalls unavailable for this platform."
35f6b4c34dSMichael Jones #endif
36f6b4c34dSMichael Jones   if (ret < 0) {
37f6b4c34dSMichael Jones     libc_errno = static_cast<int>(-ret);
38f6b4c34dSMichael Jones     return -1;
39f6b4c34dSMichael Jones   }
40aeb18ebbSMichael Jones 
41aeb18ebbSMichael Jones   // Unpoison the msghdr, as well as all its components.
42*fedb9fdbSMichael Jones   MSAN_UNPOISON(msg, sizeof(msghdr));
43aeb18ebbSMichael Jones   MSAN_UNPOISON(msg->msg_name, msg->msg_namelen);
44*fedb9fdbSMichael Jones 
45aeb18ebbSMichael Jones   for (size_t i = 0; i < msg->msg_iovlen; ++i) {
46*fedb9fdbSMichael Jones     MSAN_UNPOISON(msg->msg_iov[i].iov_base, msg->msg_iov[i].iov_len);
47aeb18ebbSMichael Jones   }
48aeb18ebbSMichael Jones   MSAN_UNPOISON(msg->msg_control, msg->msg_controllen);
49aeb18ebbSMichael Jones 
50f6b4c34dSMichael Jones   return ret;
51f6b4c34dSMichael Jones }
52f6b4c34dSMichael Jones 
53f6b4c34dSMichael Jones } // namespace LIBC_NAMESPACE_DECL
54