2016-09-12 12:01:06 +08:00
|
|
|
const system = switch(@compileVar("os")) {
|
2016-12-22 12:34:14 +08:00
|
|
|
Os.linux => @import("linux.zig"),
|
|
|
|
Os.darwin => @import("darwin.zig"),
|
2016-09-12 12:01:06 +08:00
|
|
|
else => @compileError("Unsupported OS"),
|
|
|
|
};
|
2016-02-28 13:06:46 +08:00
|
|
|
const errno = @import("errno.zig");
|
2016-02-04 16:00:54 +08:00
|
|
|
|
2016-12-22 12:34:14 +08:00
|
|
|
error Unexpected;
|
2016-02-04 16:00:54 +08:00
|
|
|
|
2016-08-18 11:11:04 +08:00
|
|
|
pub fn getRandomBytes(buf: []u8) -> %void {
|
2016-09-12 12:01:06 +08:00
|
|
|
while (true) {
|
|
|
|
const ret = switch (@compileVar("os")) {
|
2017-01-01 06:10:29 +08:00
|
|
|
Os.linux => system.getrandom(buf.ptr, buf.len, 0),
|
|
|
|
Os.darwin => system.getrandom(buf.ptr, buf.len),
|
2016-09-12 12:01:06 +08:00
|
|
|
else => @compileError("unsupported os"),
|
|
|
|
};
|
|
|
|
const err = system.getErrno(ret);
|
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
errno.EINVAL => @unreachable(),
|
|
|
|
errno.EFAULT => @unreachable(),
|
|
|
|
errno.EINTR => continue,
|
|
|
|
else => error.Unexpected,
|
2016-02-14 13:59:49 +08:00
|
|
|
}
|
2016-09-12 12:01:06 +08:00
|
|
|
}
|
|
|
|
return;
|
2016-02-04 16:00:54 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 07:42:56 +08:00
|
|
|
|
2016-09-28 14:33:32 +08:00
|
|
|
pub coldcc fn abort() -> unreachable {
|
2016-08-18 11:11:04 +08:00
|
|
|
switch (@compileVar("os")) {
|
2017-01-01 06:10:29 +08:00
|
|
|
Os.linux, Os.darwin => {
|
2016-09-12 12:01:06 +08:00
|
|
|
system.raise(system.SIGABRT);
|
|
|
|
system.raise(system.SIGKILL);
|
2016-08-18 11:11:04 +08:00
|
|
|
while (true) {}
|
|
|
|
},
|
2016-09-06 05:01:54 +08:00
|
|
|
else => @compileError("unsupported os"),
|
2016-08-18 11:11:04 +08:00
|
|
|
}
|
2016-04-19 07:42:56 +08:00
|
|
|
}
|