system_linux.odin 671 B

123456789101112131415161718192021222324252627
  1. package rand
  2. import "core:sys/unix"
  3. _system_random :: proc() -> u32 {
  4. for {
  5. value: u32
  6. ret := unix.sys_getrandom(([^]u8)(&value), 4, 0)
  7. if ret < 0 {
  8. switch ret {
  9. case -4: // EINTR
  10. // Call interupted by a signal handler, just retry the request.
  11. continue
  12. case -38: // ENOSYS
  13. // The kernel is apparently prehistoric (< 3.17 circa 2014)
  14. // and does not support getrandom.
  15. panic("getrandom not available in kernel")
  16. case:
  17. // All other failures are things that should NEVER happen
  18. // unless the kernel interface changes (ie: the Linux
  19. // developers break userland).
  20. panic("getrandom failed")
  21. }
  22. }
  23. return value
  24. }
  25. }