xref: /llvm-project/libc/src/sys/socket/linux/sendmsg.cpp (revision ef66936df4e4746c4af0f224796e0070b0f14138)
1f6b4c34dSMichael Jones //===-- Linux implementation of sendmsg -----------------------------------===//
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/sendmsg.h"
10f6b4c34dSMichael Jones 
11*ef66936dSMichael Jones #include <linux/net.h>   // For SYS_SOCKET socketcall number.
12*ef66936dSMichael Jones #include <sys/syscall.h> // For syscall numbers.
13*ef66936dSMichael 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"
18f6b4c34dSMichael Jones #include "src/errno/libc_errno.h"
19f6b4c34dSMichael Jones 
20f6b4c34dSMichael Jones namespace LIBC_NAMESPACE_DECL {
21f6b4c34dSMichael Jones 
22f6b4c34dSMichael Jones LLVM_LIBC_FUNCTION(ssize_t, sendmsg,
23f6b4c34dSMichael Jones                    (int sockfd, const struct msghdr *msg, int flags)) {
24f6b4c34dSMichael Jones #ifdef SYS_sendmsg
25*ef66936dSMichael Jones   ssize_t ret =
26*ef66936dSMichael Jones       LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendmsg, 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)};
31*ef66936dSMichael Jones   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
32*ef66936dSMichael Jones       SYS_socketcall, SYS_SENDMSG, 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   }
40f6b4c34dSMichael Jones   return ret;
41f6b4c34dSMichael Jones }
42f6b4c34dSMichael Jones 
43f6b4c34dSMichael Jones } // namespace LIBC_NAMESPACE_DECL
44