diff --git a/mod.zig b/mod.zig index bebf87e9209fd03f182af6dca108f03837c86bf7..4dd50378701f66b4c3c1bba6a5892f72a8f9f401 100644 --- a/mod.zig +++ b/mod.zig @@ -2749,6 +2749,7 @@ pub const libc = struct { pub extern fn sendfile(out_fd: c_int, in_fd: c_int, offset: ?*const off_t, count: usize) isize; pub extern fn getdents(dirfd: c_int, buf: [*]u8, nbytes: usize) c_int; pub extern fn sched_getaffinity(pid: pid_t, cpusetsize: usize, mask: *cpu_set_t) c_int; + pub extern fn getrandom(buf: [*]u8, size: usize, flags: c_uint) isize; }; pub const clock_t = c_long; @@ -3199,6 +3200,12 @@ pub const DT = enum(u8) { WHT = 14, }; +pub const GRND = struct { + pub const NONBLOCK = 0x0001; + pub const RANDOM = 0x0002; + pub const INSECURE = 0x0004; +}; + pub fn getpid() pid_t { return libc.getpid(); } @@ -3486,3 +3493,9 @@ pub fn mkdtemp(template: [*:0]u8) ![*:0]u8 { if (rc == null) return errno.fromInt(errno.fromLibC()); return rc.?; } +pub fn getrandom(buf: []u8, flags: c_uint) ![]u8 { + const rc = libc.getrandom(buf.ptr, buf.len, flags); + if (rc == -1) return errno.fromInt(errno.fromLibC()); + std.debug.assert(rc >= 0); + return buf[0..@intCast(rc)]; +} diff --git a/test.zig b/test.zig index 4ce5163aea3b26947e7e8f85b743945b26bd73e7..29f1b12d7d8c3d043f263927d3267c725cb3d1b6 100644 --- a/test.zig +++ b/test.zig @@ -59,4 +59,5 @@ test { _ = &linux.pthread_cond_signal; _ = &linux.pthread_cond_broadcast; _ = &linux.mkdtemp; + _ = &linux.getrandom; }