2016-09-12 04:01:06 +00:00
|
|
|
const system = switch(@compileVar("os")) {
|
|
|
|
linux => @import("linux.zig"),
|
|
|
|
darwin => @import("darwin.zig"),
|
|
|
|
else => @compileError("Unsupported OS"),
|
|
|
|
};
|
2016-02-28 05:06:46 +00:00
|
|
|
const errno = @import("errno.zig");
|
2016-02-04 08:00:54 +00:00
|
|
|
|
|
|
|
pub error Unexpected;
|
|
|
|
|
2016-08-18 03:11:04 +00:00
|
|
|
pub fn getRandomBytes(buf: []u8) -> %void {
|
2016-09-12 04:01:06 +00:00
|
|
|
while (true) {
|
|
|
|
const ret = switch (@compileVar("os")) {
|
|
|
|
linux => system.getrandom(buf.ptr, buf.len, 0),
|
|
|
|
darwin => system.getrandom(buf.ptr, buf.len),
|
|
|
|
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 05:59:49 +00:00
|
|
|
}
|
2016-09-12 04:01:06 +00:00
|
|
|
}
|
|
|
|
return;
|
2016-02-04 08:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-18 23:42:56 +00:00
|
|
|
|
2016-09-28 06:33:32 +00:00
|
|
|
pub coldcc fn abort() -> unreachable {
|
2016-08-18 03:11:04 +00:00
|
|
|
switch (@compileVar("os")) {
|
2016-09-12 04:01:06 +00:00
|
|
|
linux, darwin => {
|
|
|
|
system.raise(system.SIGABRT);
|
|
|
|
system.raise(system.SIGKILL);
|
2016-08-18 03:11:04 +00:00
|
|
|
while (true) {}
|
|
|
|
},
|
2016-09-05 21:01:54 +00:00
|
|
|
else => @compileError("unsupported os"),
|
2016-08-18 03:11:04 +00:00
|
|
|
}
|
2016-04-18 23:42:56 +00:00
|
|
|
}
|