2016-02-28 13:06:46 +08:00
|
|
|
const syscall = @import("syscall.zig");
|
|
|
|
const errno = @import("errno.zig");
|
2016-02-04 16:00:54 +08:00
|
|
|
|
|
|
|
pub error SigInterrupt;
|
|
|
|
pub error Unexpected;
|
|
|
|
|
2016-02-28 13:06:46 +08:00
|
|
|
pub fn get_random_bytes(buf: []u8) -> %void {
|
2016-02-14 13:59:49 +08:00
|
|
|
switch (@compile_var("os")) {
|
|
|
|
linux => {
|
2016-02-28 13:06:46 +08:00
|
|
|
const amt_got = syscall.getrandom(buf.ptr, buf.len, 0);
|
2016-02-14 13:59:49 +08:00
|
|
|
if (amt_got < 0) {
|
|
|
|
return switch (-amt_got) {
|
2016-02-28 13:06:46 +08:00
|
|
|
errno.EINVAL => unreachable{},
|
|
|
|
errno.EFAULT => unreachable{},
|
|
|
|
errno.EINTR => error.SigInterrupt,
|
|
|
|
else => error.Unexpected,
|
2016-02-14 13:59:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
windows => {
|
|
|
|
// TODO
|
|
|
|
for (buf) |_, i| {
|
|
|
|
buf[i] = 4;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => unreachable{},
|
2016-02-04 16:00:54 +08:00
|
|
|
}
|
|
|
|
}
|