xref: /aosp_15_r20/external/llvm-libc/src/sys/socket/linux/bind.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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-exception
6 //
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/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, bind,
23                    (int socket, const struct sockaddr *address,
24                     socklen_t address_len)) {
25 #ifdef SYS_bind
26   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 #else
35 #error "socket and socketcall syscalls unavailable for this platform."
36 #endif
37   if (ret < 0) {
38     libc_errno = -ret;
39     return -1;
40   }
41   return ret;
42 }
43 
44 } // namespace LIBC_NAMESPACE_DECL
45