system_linux.odin 690 B

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