10
10
#include " hdr/errno_macros.h"
11
11
#include " src/__support/common.h"
12
12
#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.
14
16
15
17
namespace LIBC_NAMESPACE_DECL {
16
18
LLVM_LIBC_FUNCTION (int , getentropy, (void *buffer, size_t length)) {
@@ -24,23 +26,24 @@ LLVM_LIBC_FUNCTION(int, getentropy, (void *buffer, size_t length)) {
24
26
while (length != 0 ) {
25
27
// 0 flag means urandom and blocking, which meets the assumption of
26
28
// getentropy
27
- ssize_t ret = LIBC_NAMESPACE::getrandom ( cursor, length, 0 );
29
+ auto ret = syscall_impl< long >(SYS_getrandom, cursor, length, 0 );
28
30
29
31
// on success, advance the buffer pointer
30
- if (ret != - 1 ) {
32
+ if (ret >= 0 ) {
31
33
length -= static_cast <size_t >(ret);
32
34
cursor += ret;
33
35
continue ;
34
36
}
35
37
38
+ auto error = -static_cast <int >(ret);
39
+
36
40
// on EINTR, try again
37
- if (libc_errno == EINTR)
41
+ if (error == EINTR)
38
42
continue ;
39
43
40
44
// on ENOSYS, forward errno and exit;
41
45
// otherwise, set EIO and exit
42
- if (libc_errno != ENOSYS)
43
- libc_errno = EIO;
46
+ libc_errno = (error == ENOSYS) ? ENOSYS : EIO;
44
47
return -1 ;
45
48
}
46
49
return 0 ;
0 commit comments