zig/std/os.zig

41 lines
1.1 KiB
Zig
Raw Normal View History

const system = switch(@compileVar("os")) {
linux => @import("linux.zig"),
darwin => @import("darwin.zig"),
else => @compileError("Unsupported OS"),
};
const errno = @import("errno.zig");
2016-02-04 16:00:54 +08:00
pub error Unexpected;
2016-08-18 11:11:04 +08:00
pub fn getRandomBytes(buf: []u8) -> %void {
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,
}
}
return;
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, darwin => {
system.raise(system.SIGABRT);
system.raise(system.SIGKILL);
2016-08-18 11:11:04 +08:00
while (true) {}
},
else => @compileError("unsupported os"),
2016-08-18 11:11:04 +08:00
}
2016-04-19 07:42:56 +08:00
}