Skip to content

Commit 7dc7442

Browse files
[libc] remove dependency from getentropy to getrandom
1 parent 48a3a1c commit 7dc7442

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ add_entrypoint_object(
581581
libc.hdr.types.size_t
582582
libc.hdr.types.ssize_t
583583
libc.hdr.errno_macros
584-
libc.src.sys.random.getrandom
584+
libc.include.sys_syscall
585+
libc.src.__support.OSUtil.osutil
585586
libc.src.errno.errno
586587
)

libc/src/unistd/linux/getentropy.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include "hdr/errno_macros.h"
1111
#include "src/__support/common.h"
1212
#include "src/errno/libc_errno.h"
13-
#include "src/sys/random/getrandom.h"
13+
#include "src/__support/OSUtil/syscall.h"
14+
15+
#include <sys/syscall.h> // For syscall numbers.
1416

1517
namespace LIBC_NAMESPACE_DECL {
1618
LLVM_LIBC_FUNCTION(int, getentropy, (void *buffer, size_t length)) {
@@ -24,23 +26,24 @@ LLVM_LIBC_FUNCTION(int, getentropy, (void *buffer, size_t length)) {
2426
while (length != 0) {
2527
// 0 flag means urandom and blocking, which meets the assumption of
2628
// getentropy
27-
ssize_t ret = LIBC_NAMESPACE::getrandom(cursor, length, 0);
29+
auto ret = syscall_impl<long>(SYS_getrandom, cursor, length, 0);
2830

2931
// on success, advance the buffer pointer
30-
if (ret != -1) {
32+
if (ret >= 0) {
3133
length -= static_cast<size_t>(ret);
3234
cursor += ret;
3335
continue;
3436
}
3537

38+
auto error = -static_cast<int>(ret);
39+
3640
// on EINTR, try again
37-
if (libc_errno == EINTR)
41+
if (error == EINTR)
3842
continue;
3943

4044
// on ENOSYS, forward errno and exit;
4145
// otherwise, set EIO and exit
42-
if (libc_errno != ENOSYS)
43-
libc_errno = EIO;
46+
libc_errno = (error == ENOSYS) ? ENOSYS : EIO;
4447
return -1;
4548
}
4649
return 0;

0 commit comments

Comments
 (0)