2016-03-02 05:11:38 +08:00
|
|
|
const linux = @import("linux.zig");
|
2016-02-28 13:06:46 +08:00
|
|
|
const errno = @import("errno.zig");
|
2016-02-04 16:00:54 +08:00
|
|
|
|
|
|
|
pub error SigInterrupt;
|
|
|
|
pub error Unexpected;
|
|
|
|
|
2016-08-18 11:11:04 +08:00
|
|
|
pub fn getRandomBytes(buf: []u8) -> %void {
|
2016-08-17 13:42:50 +08:00
|
|
|
switch (@compileVar("os")) {
|
2016-02-14 13:59:49 +08:00
|
|
|
linux => {
|
2016-07-27 11:40:11 +08:00
|
|
|
const ret = linux.getrandom(buf.ptr, buf.len, 0);
|
2016-08-17 13:42:50 +08:00
|
|
|
const err = linux.getErrno(ret);
|
2016-07-27 11:40:11 +08:00
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
2016-09-14 04:46:27 +08:00
|
|
|
errno.EINVAL => @unreachable(),
|
|
|
|
errno.EFAULT => @unreachable(),
|
2016-02-28 13:06:46 +08:00
|
|
|
errno.EINTR => error.SigInterrupt,
|
|
|
|
else => error.Unexpected,
|
2016-02-14 13:59:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-09-06 05:01:54 +08:00
|
|
|
else => @compileError("unsupported os"),
|
2016-02-04 16:00:54 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 07:42:56 +08:00
|
|
|
|
|
|
|
#attribute("cold")
|
|
|
|
pub fn abort() -> unreachable {
|
2016-08-18 11:11:04 +08:00
|
|
|
switch (@compileVar("os")) {
|
|
|
|
linux => {
|
|
|
|
linux.raise(linux.SIGABRT);
|
|
|
|
linux.raise(linux.SIGKILL);
|
|
|
|
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
|
|
|
}
|