From 14a954f3507d97e1a6e6c995f3e4d3db76c64b3d Mon Sep 17 00:00:00 2001 From: nia Date: Fri, 1 May 2020 17:22:27 +0100 Subject: [PATCH 1/5] Add arc4random_buf() in NetBSD libc, use it to implement getrandom() --- lib/std/c/netbsd.zig | 1 + lib/std/os.zig | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/lib/std/c/netbsd.zig b/lib/std/c/netbsd.zig index f3c34b5cad..20960b3f77 100644 --- a/lib/std/c/netbsd.zig +++ b/lib/std/c/netbsd.zig @@ -9,6 +9,7 @@ pub const _errno = __errno; pub const dl_iterate_phdr_callback = extern fn (info: *dl_phdr_info, size: usize, data: ?*c_void) c_int; pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int; +pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; pub extern "c" fn __fstat50(fd: fd_t, buf: *Stat) c_int; pub extern "c" fn __stat50(path: [*:0]const u8, buf: *Stat) c_int; pub extern "c" fn __clock_gettime50(clk_id: c_int, tp: *timespec) c_int; diff --git a/lib/std/os.zig b/lib/std/os.zig index 06a4c12dfc..9502e8d9df 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -153,6 +153,10 @@ pub fn getrandom(buffer: []u8) GetRandomError!void { } return; } + if (builtin.os.tag == .netbsd) { + netbsd.arc4random_buf(buffer.ptr, buffer.len); + return; + } if (builtin.os.tag == .wasi) { switch (wasi.random_get(buffer.ptr, buffer.len)) { 0 => return, From 74ad31536051e68b4b9edf924662b34c30b09da7 Mon Sep 17 00:00:00 2001 From: nia Date: Fri, 1 May 2020 17:23:27 +0100 Subject: [PATCH 2/5] In init_rand avoid reading from /dev/urandom on NetBSD/FreeBSD Use the KERN_ARND sysctl instead. --- src/os.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/os.cpp b/src/os.cpp index 16d6847d4e..26ed0dc4e1 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1462,6 +1462,14 @@ static void init_rand() { unsigned seed; memcpy(&seed, ptr_random, sizeof(seed)); srand(seed); +#elif defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) + unsigned seed; + size_t len = sizeof(seed); + int mib[2] = { CTL_KERN, KERN_ARND }; + if (sysctl(mib, 2, &seed, &len, NULL, 0) != 0) { + zig_panic("unable to query random data from sysctl"); + } + srand(seed); #else int fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); if (fd == -1) { From 77376a54bf4a379135261f40f7f4025a79314897 Mon Sep 17 00:00:00 2001 From: nycex Date: Sat, 2 May 2020 10:19:07 +0200 Subject: [PATCH 3/5] correct usages of std.fs.dir.DeleteFileError (#5058) * correct usages of std.fs.dir.DeleteFileError * test std.fs.createFileAbsolute() and std.fs.deleteFileAbsolute() --- lib/std/fs.zig | 6 +++--- lib/std/fs/test.zig | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 3da3d1d9d7..df8bfe97d7 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -1442,7 +1442,7 @@ pub fn createFileAbsoluteW(absolute_path_w: [*:0]const u16, flags: File.CreateFl /// Asserts that the path is absolute. See `Dir.deleteFile` for a function that /// operates on both absolute and relative paths. /// Asserts that the path parameter has no null bytes. -pub fn deleteFileAbsolute(absolute_path: []const u8) DeleteFileError!void { +pub fn deleteFileAbsolute(absolute_path: []const u8) Dir.DeleteFileError!void { assert(path.isAbsolute(absolute_path)); return cwd().deleteFile(absolute_path); } @@ -1450,13 +1450,13 @@ pub fn deleteFileAbsolute(absolute_path: []const u8) DeleteFileError!void { pub const deleteFileAbsoluteC = @compileError("deprecated: renamed to deleteFileAbsoluteZ"); /// Same as `deleteFileAbsolute` except the parameter is null-terminated. -pub fn deleteFileAbsoluteZ(absolute_path_c: [*:0]const u8) DeleteFileError!void { +pub fn deleteFileAbsoluteZ(absolute_path_c: [*:0]const u8) Dir.DeleteFileError!void { assert(path.isAbsoluteZ(absolute_path_c)); return cwd().deleteFileZ(absolute_path_c); } /// Same as `deleteFileAbsolute` except the parameter is WTF-16 encoded. -pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) DeleteFileError!void { +pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) Dir.DeleteFileError!void { assert(path.isAbsoluteWindowsW(absolute_path_w)); return cwd().deleteFileW(absolute_path_w); } diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index da2d2c0d0c..2d0141a5d9 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -100,6 +100,22 @@ test "create file, lock and read from multiple process at once" { }; } +test "open file with exclusive nonblocking lock twice (absolute paths)" { + const allocator = std.testing.allocator; + + const file_paths: [1][]const u8 = .{"zig-test-absolute-paths.txt"}; + const filename = try fs.path.resolve(allocator, &file_paths); + defer allocator.free(filename); + + const file1 = try fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); + + const file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); + file1.close(); + std.testing.expectError(error.WouldBlock, file2); + + try fs.deleteFileAbsolute(filename); +} + const FileLockTestContext = struct { filename: []const u8, pid: if (builtin.os.tag == .windows) ?void else ?std.os.pid_t = null, From 5fb8d7dcdca352e708a22ad4df86c902488c9775 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Sat, 2 May 2020 04:35:03 -0400 Subject: [PATCH 4/5] ci linux: bump qemu-5.0.0-z2 closes #5245 see https://github.com/ziglang/qemu-static/commit/1b41e31a5c013dea52774ba664230402eacbc75b --- ci/azure/linux_script | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/azure/linux_script b/ci/azure/linux_script index fb608d48e1..2a5d2ef1a8 100755 --- a/ci/azure/linux_script +++ b/ci/azure/linux_script @@ -14,7 +14,7 @@ sudo apt-get remove -y llvm-* sudo rm -rf /usr/local/* sudo apt-get install -y libxml2-dev libclang-10-dev llvm-10 llvm-10-dev liblld-10-dev cmake s3cmd gcc-7 g++-7 -QEMUBASE="qemu-linux-x86_64-5.0.0" +QEMUBASE="qemu-linux-x86_64-5.0.0-z2" wget https://ziglang.org/deps/$QEMUBASE.tar.xz tar xf $QEMUBASE.tar.xz PATH=$PWD/$QEMUBASE/bin:$PATH From 8ebcca6734e07aea29098ca4c63c0216b3099d0e Mon Sep 17 00:00:00 2001 From: Chris Heyes Date: Sat, 2 May 2020 19:14:46 +0100 Subject: [PATCH 5/5] Get evented io code paths to build on macOS (#5233) * Get evented io code paths to build on macOS * Use mode_t instead of usize where appropriate --- lib/std/event/loop.zig | 58 +++++++++++++++++++++++++++++------------- lib/std/os.zig | 6 ++--- lib/std/os/linux.zig | 6 ++--- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/lib/std/event/loop.zig b/lib/std/event/loop.zig index f0ac85d4f0..fc122a1862 100644 --- a/lib/std/event/loop.zig +++ b/lib/std/event/loop.zig @@ -502,18 +502,43 @@ pub const Loop = struct { } pub fn waitUntilFdReadable(self: *Loop, fd: os.fd_t) void { - return self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLIN); + switch (builtin.os.tag) { + .linux => { + self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLIN); + }, + .macosx, .freebsd, .netbsd, .dragonfly => { + self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT); + }, + else => @compileError("Unsupported OS"), + } } pub fn waitUntilFdWritable(self: *Loop, fd: os.fd_t) void { - return self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT); + switch (builtin.os.tag) { + .linux => { + self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT); + }, + .macosx, .freebsd, .netbsd, .dragonfly => { + self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT); + }, + else => @compileError("Unsupported OS"), + } } pub fn waitUntilFdWritableOrReadable(self: *Loop, fd: os.fd_t) void { - return self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT | os.EPOLLIN); + switch (builtin.os.tag) { + .linux => { + self.linuxWaitFd(@intCast(usize, fd), os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT | os.EPOLLIN); + }, + .macosx, .freebsd, .netbsd, .dragonfly => { + self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT); + self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT); + }, + else => @compileError("Unsupported OS"), + } } - pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !os.Kevent { + pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) void { var resume_node = ResumeNode.Basic{ .base = ResumeNode{ .id = ResumeNode.Id.Basic, @@ -524,40 +549,37 @@ pub const Loop = struct { }; defer self.bsdRemoveKev(ident, filter); suspend { - try self.bsdAddKev(&resume_node, ident, filter, fflags); + self.bsdAddKev(&resume_node, ident, filter, fflags) catch unreachable; } - return resume_node.kev; } /// resume_node must live longer than the anyframe that it holds a reference to. pub fn bsdAddKev(self: *Loop, resume_node: *ResumeNode.Basic, ident: usize, filter: i16, fflags: u32) !void { self.beginOneEvent(); errdefer self.finishOneEvent(); - var kev = os.Kevent{ + var kev = [1]os.Kevent{os.Kevent{ .ident = ident, .filter = filter, .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR, .fflags = fflags, .data = 0, .udata = @ptrToInt(&resume_node.base), - }; - const kevent_array = (*const [1]os.Kevent)(&kev); - const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; - _ = try os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null); + }}; + const empty_kevs = &[0]os.Kevent{}; + _ = try os.kevent(self.os_data.kqfd, &kev, empty_kevs, null); } pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void { - var kev = os.Kevent{ + var kev = [1]os.Kevent{os.Kevent{ .ident = ident, .filter = filter, .flags = os.EV_DELETE, .fflags = 0, .data = 0, .udata = 0, - }; - const kevent_array = (*const [1]os.Kevent)(&kev); - const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; - _ = os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined; + }}; + const empty_kevs = &[0]os.Kevent{}; + _ = os.kevent(self.os_data.kqfd, &kev, empty_kevs, null) catch undefined; self.finishOneEvent(); } @@ -712,7 +734,7 @@ pub const Loop = struct { } /// Performs an async `os.open` using a separate thread. - pub fn openZ(self: *Loop, file_path: [*:0]const u8, flags: u32, mode: usize) os.OpenError!os.fd_t { + pub fn openZ(self: *Loop, file_path: [*:0]const u8, flags: u32, mode: os.mode_t) os.OpenError!os.fd_t { var req_node = Request.Node{ .data = .{ .msg = .{ @@ -733,7 +755,7 @@ pub const Loop = struct { } /// Performs an async `os.opent` using a separate thread. - pub fn openatZ(self: *Loop, fd: os.fd_t, file_path: [*:0]const u8, flags: u32, mode: usize) os.OpenError!os.fd_t { + pub fn openatZ(self: *Loop, fd: os.fd_t, file_path: [*:0]const u8, flags: u32, mode: os.mode_t) os.OpenError!os.fd_t { var req_node = Request.Node{ .data = .{ .msg = .{ diff --git a/lib/std/os.zig b/lib/std/os.zig index 9502e8d9df..5a502035d5 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -374,7 +374,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize { const first = iov[0]; return read(fd, first.iov_base[0..first.iov_len]); } - + const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31); while (true) { // TODO handle the case when iov_len is too large and get rid of this @intCast @@ -859,7 +859,7 @@ pub const OpenError = error{ /// Open and possibly create a file. Keeps trying if it gets interrupted. /// See also `openC`. /// TODO support windows -pub fn open(file_path: []const u8, flags: u32, perm: usize) OpenError!fd_t { +pub fn open(file_path: []const u8, flags: u32, perm: mode_t) OpenError!fd_t { const file_path_c = try toPosixPath(file_path); return openZ(&file_path_c, flags, perm); } @@ -869,7 +869,7 @@ pub const openC = @compileError("deprecated: renamed to openZ"); /// Open and possibly create a file. Keeps trying if it gets interrupted. /// See also `open`. /// TODO support windows -pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: usize) OpenError!fd_t { +pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: mode_t) OpenError!fd_t { while (true) { const rc = system.open(file_path, flags, perm); switch (errno(rc)) { diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 5631a90ef9..15f9bf9b62 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -492,7 +492,7 @@ pub fn renameat2(oldfd: i32, oldpath: [*:0]const u8, newfd: i32, newpath: [*:0]c ); } -pub fn open(path: [*:0]const u8, flags: u32, perm: usize) usize { +pub fn open(path: [*:0]const u8, flags: u32, perm: mode_t) usize { if (@hasField(SYS, "open")) { return syscall3(.open, @ptrToInt(path), flags, perm); } else { @@ -506,11 +506,11 @@ pub fn open(path: [*:0]const u8, flags: u32, perm: usize) usize { } } -pub fn create(path: [*:0]const u8, perm: usize) usize { +pub fn create(path: [*:0]const u8, perm: mode_t) usize { return syscall2(.creat, @ptrToInt(path), perm); } -pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, mode: usize) usize { +pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, mode: mode_t) usize { // dirfd could be negative, for example AT_FDCWD is -100 return syscall4(.openat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), flags, mode); }