Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/llvm/llvm-project.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/libc/src
diff options
context:
space:
mode:
Diffstat (limited to 'libc/src')
-rw-r--r--libc/src/sys/CMakeLists.txt1
-rw-r--r--libc/src/sys/random/CMakeLists.txt10
-rw-r--r--libc/src/sys/random/getrandom.h20
-rw-r--r--libc/src/sys/random/linux/CMakeLists.txt12
-rw-r--r--libc/src/sys/random/linux/getrandom.cpp29
5 files changed, 72 insertions, 0 deletions
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index f8e25cc8d4e5..bf6283df87ac 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -1,4 +1,5 @@
add_subdirectory(mman)
+add_subdirectory(random)
add_subdirectory(resource)
add_subdirectory(sendfile)
add_subdirectory(stat)
diff --git a/libc/src/sys/random/CMakeLists.txt b/libc/src/sys/random/CMakeLists.txt
new file mode 100644
index 000000000000..2291a86934a0
--- /dev/null
+++ b/libc/src/sys/random/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+ getrandom
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.getrandom
+)
diff --git a/libc/src/sys/random/getrandom.h b/libc/src/sys/random/getrandom.h
new file mode 100644
index 000000000000..dab508d363ff
--- /dev/null
+++ b/libc/src/sys/random/getrandom.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for getrandom ----------------------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_RANDOM_GETRANDOM_H
+#define LLVM_LIBC_SRC_SYS_RANDOM_GETRANDOM_H
+
+#include <sys/types.h>
+
+namespace __llvm_libc {
+
+ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_SYS_RANDOM_GETRANDOM_H
diff --git a/libc/src/sys/random/linux/CMakeLists.txt b/libc/src/sys/random/linux/CMakeLists.txt
new file mode 100644
index 000000000000..474e275ee597
--- /dev/null
+++ b/libc/src/sys/random/linux/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_entrypoint_object(
+ getrandom
+ SRCS
+ getrandom.cpp
+ HDRS
+ ../getrandom.h
+ DEPENDS
+ libc.include.sys_random
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.errno.errno
+)
diff --git a/libc/src/sys/random/linux/getrandom.cpp b/libc/src/sys/random/linux/getrandom.cpp
new file mode 100644
index 000000000000..e9c33da3d294
--- /dev/null
+++ b/libc/src/sys/random/linux/getrandom.cpp
@@ -0,0 +1,29 @@
+//===-- Linux implementation of getrandom ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/random/getrandom.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include <errno.h>
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(ssize_t, getrandom,
+ (void *buf, size_t buflen, unsigned int flags)) {
+ long ret = __llvm_libc::syscall_impl(SYS_getrandom, buf, buflen, flags);
+ if (ret < 0) {
+ errno = -ret;
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace __llvm_libc