[libc-commits] [libc] [libc] implement unistd/getentropy (PR #122692)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 13 08:30:13 PST 2025


================
@@ -0,0 +1,48 @@
+//===-- Linux implementation of getentropy --------------------------------===//
+//
+// 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/unistd/getentropy.h"
+#include "hdr/errno_macros.h"
+#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
+#include "src/sys/random/getrandom.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(int, getentropy, (void *buffer, size_t length)) {
+  // check the length limit
+  if (length > 256) {
+    libc_errno = EIO;
+    return -1;
+  }
+
+  char *cursor = static_cast<char *>(buffer);
+  while (length != 0) {
+    // 0 flag means urandom and blocking, which meets the assumption of
+    // getentropy
+    ssize_t ret = LIBC_NAMESPACE::getrandom(cursor, length, 0);
----------------
lntue wrote:

Can this be independent of `getrandom` entry point?  And it looks like its implementation doesn't need to be `linux` specific?

https://github.com/llvm/llvm-project/pull/122692


More information about the libc-commits mailing list