zig/std/os.zig

36 lines
982 B
Zig
Raw Normal View History

2016-03-01 21:11:38 +00:00
const linux = @import("linux.zig");
const errno = @import("errno.zig");
2016-02-04 08:00:54 +00:00
pub error SigInterrupt;
pub error Unexpected;
2016-08-18 03:11:04 +00:00
pub fn getRandomBytes(buf: []u8) -> %void {
2016-08-17 05:42:50 +00:00
switch (@compileVar("os")) {
linux => {
const ret = linux.getrandom(buf.ptr, buf.len, 0);
2016-08-17 05:42:50 +00:00
const err = linux.getErrno(ret);
if (err > 0) {
return switch (err) {
errno.EINVAL => @unreachable(),
errno.EFAULT => @unreachable(),
errno.EINTR => error.SigInterrupt,
else => error.Unexpected,
}
}
},
else => @compileError("unsupported os"),
2016-02-04 08:00:54 +00:00
}
}
2016-04-18 23:42:56 +00:00
#attribute("cold")
pub fn abort() -> unreachable {
2016-08-18 03:11:04 +00:00
switch (@compileVar("os")) {
linux => {
linux.raise(linux.SIGABRT);
linux.raise(linux.SIGKILL);
while (true) {}
},
else => @compileError("unsupported os"),
2016-08-18 03:11:04 +00:00
}
2016-04-18 23:42:56 +00:00
}