xref: /llvm-project/libc/src/sys/socket/linux/socketpair.cpp (revision aeb18ebbe0a1a2fbce9b432eefed46c1d90968ea)
1f6b4c34dSMichael Jones //===-- Linux implementation of socketpair --------------------------------===//
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/socketpair.h"
10f6b4c34dSMichael Jones 
11f6b4c34dSMichael Jones #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12f6b4c34dSMichael Jones #include "src/__support/common.h"
13f6b4c34dSMichael Jones #include "src/__support/macros/config.h"
14*aeb18ebbSMichael Jones #include "src/__support/macros/sanitizer.h"
15f6b4c34dSMichael Jones #include "src/errno/libc_errno.h"
16f6b4c34dSMichael Jones #include <linux/net.h>   // For SYS_SOCKET socketcall number.
17f6b4c34dSMichael Jones #include <sys/syscall.h> // For syscall numbers.
18f6b4c34dSMichael Jones 
19f6b4c34dSMichael Jones namespace LIBC_NAMESPACE_DECL {
20f6b4c34dSMichael Jones 
21f6b4c34dSMichael Jones LLVM_LIBC_FUNCTION(int, socketpair,
22f6b4c34dSMichael Jones                    (int domain, int type, int protocol, int sv[2])) {
23f6b4c34dSMichael Jones #ifdef SYS_socketpair
24f6b4c34dSMichael Jones   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketpair, domain, type,
25f6b4c34dSMichael Jones                                               protocol, sv);
26f6b4c34dSMichael Jones #elif defined(SYS_socketcall)
27f6b4c34dSMichael Jones   unsigned long sockcall_args[3] = {
28f6b4c34dSMichael Jones       static_cast<unsigned long>(domain), static_cast<unsigned long>(type),
29f6b4c34dSMichael Jones       static_cast<unsigned long>(protocol), static_cast<unsigned long>(sv)};
30f6b4c34dSMichael Jones   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_SOCKETPAIR,
31f6b4c34dSMichael Jones                                               sockcall_args);
32f6b4c34dSMichael Jones #else
33f6b4c34dSMichael Jones #error "socket and socketcall syscalls unavailable for this platform."
34f6b4c34dSMichael Jones #endif
35f6b4c34dSMichael Jones   if (ret < 0) {
36f6b4c34dSMichael Jones     libc_errno = -ret;
37f6b4c34dSMichael Jones     return -1;
38f6b4c34dSMichael Jones   }
39*aeb18ebbSMichael Jones 
40*aeb18ebbSMichael Jones   MSAN_UNPOISON(sv, sizeof(int) * 2);
41*aeb18ebbSMichael Jones 
42f6b4c34dSMichael Jones   return ret;
43f6b4c34dSMichael Jones }
44f6b4c34dSMichael Jones 
45f6b4c34dSMichael Jones } // namespace LIBC_NAMESPACE_DECL
46