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/errno/libc_errno.h" 15 16 #include <linux/net.h> // For SYS_SOCKET socketcall number. 17 #include <sys/syscall.h> // For syscall numbers. 18 19 namespace LIBC_NAMESPACE { 20 21 LLVM_LIBC_FUNCTION(int, socket, (int domain, int type, int protocol)) { 22 #ifdef SYS_socket 23 int ret = 24 LIBC_NAMESPACE::syscall_impl<int>(SYS_socket, domain, type, protocol); 25 #elif defined(SYS_socketcall) 26 unsigned long sockcall_args[3] = {static_cast<unsigned long>(domain), 27 static_cast<unsigned long>(type), 28 static_cast<unsigned long>(protocol)}; 29 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_SOCKET, 30 sockcall_args); 31 #else 32 #error "socket and socketcall syscalls unavailable for this platform." 33 #endif 34 if (ret < 0) { 35 libc_errno = -ret; 36 return -1; 37 } 38 return ret; 39 } 40 41 } // namespace LIBC_NAMESPACE 42