45 lines · cpp
1//===-- Linux implementation of bind --------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/sys/socket/bind.h"10 11#include "src/__support/OSUtil/syscall.h" // For internal syscall function.12#include "src/__support/common.h"13 14#include "src/__support/libc_errno.h"15#include "src/__support/macros/config.h"16 17#include <linux/net.h> // For SYS_SOCKET socketcall number.18#include <sys/syscall.h> // For syscall numbers.19 20namespace LIBC_NAMESPACE_DECL {21 22LLVM_LIBC_FUNCTION(int, bind,23 (int socket, const struct sockaddr *address,24 socklen_t address_len)) {25#ifdef SYS_bind26 int ret =27 LIBC_NAMESPACE::syscall_impl<int>(SYS_bind, socket, address, address_len);28#elif defined(SYS_socketcall)29 unsigned long sockcall_args[3] = {static_cast<unsigned long>(socket),30 reinterpret_cast<unsigned long>(address),31 static_cast<unsigned long>(address_len)};32 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_BIND,33 sockcall_args);34#else35#error "socket and socketcall syscalls unavailable for this platform."36#endif37 if (ret < 0) {38 libc_errno = -ret;39 return -1;40 }41 return ret;42}43 44} // namespace LIBC_NAMESPACE_DECL45