system_linux.odin 735 B

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