xref: /aosp_15_r20/external/llvm-libc/src/sys/socket/linux/sendmsg.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of sendmsg -----------------------------------===//
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/sendmsg.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/ssize_t.h"
15 #include "hdr/types/struct_msghdr.h"
16 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
17 #include "src/__support/common.h"
18 #include "src/errno/libc_errno.h"
19 
20 namespace LIBC_NAMESPACE_DECL {
21 
22 LLVM_LIBC_FUNCTION(ssize_t, sendmsg,
23                    (int sockfd, const struct msghdr *msg, int flags)) {
24 #ifdef SYS_sendmsg
25   ssize_t ret =
26       LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendmsg, sockfd, msg, flags);
27 #elif defined(SYS_socketcall)
28   unsigned long sockcall_args[3] = {static_cast<unsigned long>(sockfd),
29                                     reinterpret_cast<unsigned long>(msg),
30                                     static_cast<unsigned long>(flags)};
31   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
32       SYS_socketcall, SYS_SENDMSG, sockcall_args);
33 #else
34 #error "socket and socketcall syscalls unavailable for this platform."
35 #endif
36   if (ret < 0) {
37     libc_errno = static_cast<int>(-ret);
38     return -1;
39   }
40   return ret;
41 }
42 
43 } // namespace LIBC_NAMESPACE_DECL
44