From: Nadav Har'El <n...@scylladb.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

libc: implement getentropy()

Starting in gcc commit
https://gcc.gnu.org/g:3439657b02869299685d259c3a77aa38714565b7, libstdc++
started to use the getentropy() function, so the OSv kernel cannot be
linked without implementing it.

It's easy to implement getentropy() by calling getrandom().

Musl also has its own getentropy.c but it has a silly compilation error
(when len=0, it returns an uninitialized "ret") so let's just write
one of our own and later reconsider taking Musl's one.

After this patch, OSv can finally build on Fedora 36 with Gcc 12.1.1.

Fixes #1198.

Signed-off-by: Nadav Har'El <n...@scylladb.com>

---
diff --git a/include/api/unistd.h b/include/api/unistd.h
--- a/include/api/unistd.h
+++ b/include/api/unistd.h
@@ -179,6 +179,7 @@ char *getusershell(void);
 int acct(const char *);
 long syscall(long, ...);
 long __syscall(long, ...);
+int getentropy(void *, size_t);
 #endif
 
 #ifdef _GNU_SOURCE
diff --git a/libc/random.c b/libc/random.c
--- a/libc/random.c
+++ b/libc/random.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2018 Waldemar Kozaczuk
+ * Copyright (C) 2022 Nadav Har'El
  *
  * This work is open source software, licensed under the terms of the
  * BSD license as described in the LICENSE file in the top-level directory.
@@ -49,3 +50,14 @@ ssize_t getrandom(void *buf, size_t count, unsigned int 
flags)
     close(fd);
     return read;
 }
+
+int getentropy(void *buf,  size_t len)
+{
+    if (len > 256) {
+        errno = EIO;
+        return -1;
+    } else if (len == 0) {
+        return 0;
+    }
+    return getrandom(buf, len, 0) >= 0;
+}

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/000000000000eb551705e2574363%40google.com.

Reply via email to