aboutsummaryrefslogtreecommitdiff
path: root/std/os.zig
blob: 8def16601d58276dd84366ab829150f550305cc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const system = switch(@compileVar("os")) {
    linux => @import("linux.zig"),
    darwin => @import("darwin.zig"),
    else => @compileError("Unsupported OS"),
};
const errno = @import("errno.zig");

pub error Unexpected;

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;
    }
}

pub coldcc fn abort() -> unreachable {
    switch (@compileVar("os")) {
        linux, darwin => {
            system.raise(system.SIGABRT);
            system.raise(system.SIGKILL);
            while (true) {}
        },
        else => @compileError("unsupported os"),
    }
}