1 //===------------ Implementation of getrandom function ----------*- 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_GETRANDOM_H 10 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H 11 12 #include "hdr/types/ssize_t.h" 13 #include "src/__support/OSUtil/linux/syscall.h" // syscall_impl 14 #include "src/__support/common.h" 15 #include "src/__support/error_or.h" 16 #include "src/__support/macros/config.h" 17 #include <sys/syscall.h> // For syscall numbers 18 19 namespace LIBC_NAMESPACE_DECL { 20 namespace internal { 21 getrandom(void * buf,size_t buflen,unsigned int flags)22LIBC_INLINE static ErrorOr<ssize_t> getrandom(void *buf, size_t buflen, 23 unsigned int flags) { 24 ssize_t ret = 25 LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_getrandom, buf, buflen, flags); 26 if (ret < 0) { 27 return Error(-static_cast<int>(ret)); 28 } 29 return ret; 30 } 31 32 } // namespace internal 33 } // namespace LIBC_NAMESPACE_DECL 34 35 #endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H 36