Page MenuHomeWildfire Games

Add a getrandom() codepath on Linux

Authored by linkmauve on Oct 23 2020, 9:00 PM.



This syscall works even if /dev/urandom isn’t available, and doesn’t use
a file descriptor, so it should generally be preferred when available.

Ideally that’d be guarded by a libc function check instead of being set
only on Linux, but premake5 apparently doesn’t let me do that.

Test Plan

Check that e.g. is still random on Linux.

Event Timeline

linkmauve created this revision.Oct 23 2020, 9:00 PM

Build failure - The Moirai have given mortals hearts that can endure.

../../../source/lib/sysdep/os/unix/unix.cpp:42:24: fatal error: sys/random.h: No such file or directory
 #include <sys/random.h>
compilation terminated.
make[1]: *** [obj/lowlevel_Debug/unix.o] Error 1
make: *** [lowlevel] Error 2

Link to build:

Successful build - Chance fights ever on the side of the prudent.

/Applications/ file: ../../../binaries/system/liblowlevel.a(dbghelp.o) has no symbols
/Applications/ file: ../../../binaries/system/liblowlevel.a(file_stats.o) has no symbols
/Applications/ file: ../../../binaries/system/liblowlevel.a(vfs_path.o) has no symbols

Link to build:

linkmauve requested review of this revision.Oct 23 2020, 9:06 PM
vladislavbelov added inline comments.

getrandom() was introduced in version 3.17

We have about 0.4% of users with version less than 3.17, are we ready to drop them?

const ssize_t numread. Also I suppose we need to limit the count by IOSIZE_MAX somehow.


Is it possible to have numread == 0? Also what's going to happen in case we have EAGAIN?

vladislavbelov added inline comments.Oct 26 2020, 9:19 PM

Or we have EINTR.

Does getrandom have some performance expectations? Is it similar to /dev/urandom?

Does getrandom have some performance expectations? Is it similar to /dev/urandom?

Without GRND_RANDOM set in its flags, it is equivalent to /dev/urandom, otherwise to /dev/random. Also note that on modern kernels, both nodes are equivalent and provide bytes fast.

Stan abandoned this revision.Jul 21 2022, 2:32 PM
Stan added a subscriber: Stan.

Superseeded by D4705