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