|
| 1 | +//===-- Implementation header for getsockopt --------------------*- C++ -*-===// |
| 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 | +#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETSOCKOPT_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETSOCKOPT_H |
| 11 | + |
| 12 | +#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl |
| 13 | +#include "src/__support/common.h" |
| 14 | +#include "src/__support/error_or.h" |
| 15 | +#include "src/__support/macros/config.h" |
| 16 | + |
| 17 | +#include "hdr/types/socklen_t.h" |
| 18 | +#include <linux/net.h> // For SYS_GETSOCKOPT socketcall number. |
| 19 | +#include <sys/syscall.h> // For syscall numbers |
| 20 | + |
| 21 | +namespace LIBC_NAMESPACE_DECL { |
| 22 | +namespace linux_syscalls { |
| 23 | + |
| 24 | +LIBC_INLINE ErrorOr<int> getsockopt(int sockfd, int level, int optname, |
| 25 | + void *optval, socklen_t *optlen) { |
| 26 | +#ifdef SYS_getsockopt |
| 27 | + int ret = |
| 28 | + syscall_impl<int>(SYS_getsockopt, sockfd, level, optname, optval, optlen); |
| 29 | +#elif defined(SYS_socketcall) |
| 30 | + unsigned long sockcall_args[5] = {static_cast<unsigned long>(sockfd), |
| 31 | + static_cast<unsigned long>(level), |
| 32 | + static_cast<unsigned long>(optname), |
| 33 | + reinterpret_cast<unsigned long>(optval), |
| 34 | + reinterpret_cast<unsigned long>(optlen)}; |
| 35 | + int ret = syscall_impl<int>(SYS_socketcall, SYS_GETSOCKOPT, sockcall_args); |
| 36 | +#else |
| 37 | +#error "getsockopt and socketcall syscalls unavailable for this platform." |
| 38 | +#endif |
| 39 | + |
| 40 | + if (ret < 0) |
| 41 | + return Error(-static_cast<int>(ret)); |
| 42 | + return ret; |
| 43 | +} |
| 44 | + |
| 45 | +} // namespace linux_syscalls |
| 46 | +} // namespace LIBC_NAMESPACE_DECL |
| 47 | + |
| 48 | +#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETSOCKOPT_H |
0 commit comments