xref: /aosp_15_r20/external/llvm-libc/src/sys/socket/linux/socket.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of socket ------------------------------------===//
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/socket.h"
10 
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
13 
14 #include "src/__support/macros/config.h"
15 #include "src/errno/libc_errno.h"
16 
17 #include <linux/net.h>   // For SYS_SOCKET socketcall number.
18 #include <sys/syscall.h> // For syscall numbers.
19 
20 namespace LIBC_NAMESPACE_DECL {
21 
22 LLVM_LIBC_FUNCTION(int, socket, (int domain, int type, int protocol)) {
23 #ifdef SYS_socket
24   int ret =
25       LIBC_NAMESPACE::syscall_impl<int>(SYS_socket, domain, type, protocol);
26 #elif defined(SYS_socketcall)
27   unsigned long sockcall_args[3] = {static_cast<unsigned long>(domain),
28                                     static_cast<unsigned long>(type),
29                                     static_cast<unsigned long>(protocol)};
30   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_SOCKET,
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   return ret;
40 }
41 
42 } // namespace LIBC_NAMESPACE_DECL
43