From 3d61e4228298dcb973c13d8d6eba0bff36acf1ca Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 24 May 2019 19:36:09 -0400 Subject: rename "posix" to "bits" --- std/c/linux.zig | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'std/c/linux.zig') diff --git a/std/c/linux.zig b/std/c/linux.zig index 48e359f361..388d8edab8 100644 --- a/std/c/linux.zig +++ b/std/c/linux.zig @@ -1,17 +1,12 @@ -const linux = @import("../os/linux.zig"); -pub use @import("../os/linux/errno.zig"); +const std = @import("../std.zig"); +use std.c; pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int; extern "c" fn __errno_location() *c_int; pub const _errno = __errno_location; -pub const pthread_attr_t = extern struct { - __size: [56]u8, - __align: c_long, -}; - /// See std.elf for constants for this pub extern fn getauxval(__type: c_ulong) c_ulong; -pub const dl_iterate_phdr_callback = extern fn (info: *linux.dl_phdr_info, size: usize, data: ?*c_void) c_int; +pub const dl_iterate_phdr_callback = extern fn (info: *dl_phdr_info, size: usize, data: ?*c_void) c_int; pub extern fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int; -- cgit v1.2.3 From 7cb6279ac0cec065234347bda5944be64fe8b3da Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 25 May 2019 13:07:44 -0400 Subject: clean up references to posix --- CMakeLists.txt | 1 - std/atomic/queue.zig | 8 +- std/atomic/stack.zig | 8 +- std/c/darwin.zig | 2 +- std/c/linux.zig | 1 + std/child_process.zig | 18 +- std/crypto.zig | 2 +- std/debug.zig | 9 +- std/event/fs.zig | 38 +- std/event/loop.zig | 142 +++--- std/event/net.zig | 113 +++-- std/fs/path.zig | 37 +- std/heap.zig | 324 +++++++------ std/mem.zig | 6 +- std/mutex.zig | 4 +- std/net.zig | 24 +- std/os.zig | 35 +- std/os/bits/linux.zig | 25 + std/os/linux.zig | 874 ++++++++++++++++++++++++++++++++++- std/os/linux/sys.zig | 859 ---------------------------------- std/os/linux/tls.zig | 13 +- std/os/linux/x86_64.zig | 2 +- std/os/test.zig | 29 +- std/os/windows.zig | 16 +- std/process.zig | 34 +- std/special/bootstrap.zig | 4 +- std/statically_initialized_mutex.zig | 4 +- std/thread.zig | 150 +++--- 28 files changed, 1390 insertions(+), 1392 deletions(-) delete mode 100644 std/os/linux/sys.zig (limited to 'std/c/linux.zig') diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c7ceb88b9..9f6af9a9de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -620,7 +620,6 @@ set(ZIG_STD_FILES "os/freebsd.zig" "os/linux.zig" "os/linux/arm64.zig" - "os/linux/sys.zig" "os/linux/tls.zig" "os/linux/vdso.zig" "os/linux/x86_64.zig" diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig index 34cd0d22f8..bf5700c51e 100644 --- a/std/atomic/queue.zig +++ b/std/atomic/queue.zig @@ -186,13 +186,13 @@ test "std.atomic.Queue" { } } } else { - var putters: [put_thread_count]*std.os.Thread = undefined; + var putters: [put_thread_count]*std.Thread = undefined; for (putters) |*t| { - t.* = try std.os.spawnThread(&context, startPuts); + t.* = try std.Thread.spawn(&context, startPuts); } - var getters: [put_thread_count]*std.os.Thread = undefined; + var getters: [put_thread_count]*std.Thread = undefined; for (getters) |*t| { - t.* = try std.os.spawnThread(&context, startGets); + t.* = try std.Thread.spawn(&context, startGets); } for (putters) |t| diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig index 773bdf6f1b..e9d070ac7b 100644 --- a/std/atomic/stack.zig +++ b/std/atomic/stack.zig @@ -120,13 +120,13 @@ test "std.atomic.stack" { } } } else { - var putters: [put_thread_count]*std.os.Thread = undefined; + var putters: [put_thread_count]*std.Thread = undefined; for (putters) |*t| { - t.* = try std.os.spawnThread(&context, startPuts); + t.* = try std.Thread.spawn(&context, startPuts); } - var getters: [put_thread_count]*std.os.Thread = undefined; + var getters: [put_thread_count]*std.Thread = undefined; for (getters) |*t| { - t.* = try std.os.spawnThread(&context, startGets); + t.* = try std.Thread.spawn(&context, startGets); } for (putters) |t| diff --git a/std/c/darwin.zig b/std/c/darwin.zig index 3d0c7f4dd2..e45a158f68 100644 --- a/std/c/darwin.zig +++ b/std/c/darwin.zig @@ -3,7 +3,7 @@ const assert = std.debug.assert; const builtin = @import("builtin"); const macho = std.macho; -use @import("posix/darwin.zig"); +use @import("../os/bits.zig"); extern "c" fn __error() *c_int; pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int; diff --git a/std/c/linux.zig b/std/c/linux.zig index 388d8edab8..00fc600dab 100644 --- a/std/c/linux.zig +++ b/std/c/linux.zig @@ -2,6 +2,7 @@ const std = @import("../std.zig"); use std.c; pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int; +pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int; extern "c" fn __errno_location() *c_int; pub const _errno = __errno_location; diff --git a/std/child_process.zig b/std/child_process.zig index fa20a5e814..95d11d46a3 100644 --- a/std/child_process.zig +++ b/std/child_process.zig @@ -351,22 +351,22 @@ pub const ChildProcess = struct { const err_pipe = try os.pipe(); errdefer destroyPipe(err_pipe); - const pid_result = try posix.fork(); + const pid_result = try os.fork(); if (pid_result == 0) { // we are the child - setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); + setUpChildIo(self.stdin_behavior, stdin_pipe[0], os.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); + setUpChildIo(self.stdout_behavior, stdout_pipe[1], os.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); + setUpChildIo(self.stderr_behavior, stderr_pipe[1], os.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - if (self.stdin_behavior == StdIo.Pipe) { + if (self.stdin_behavior == .Pipe) { os.close(stdin_pipe[0]); os.close(stdin_pipe[1]); } - if (self.stdout_behavior == StdIo.Pipe) { + if (self.stdout_behavior == .Pipe) { os.close(stdout_pipe[0]); os.close(stdout_pipe[1]); } - if (self.stderr_behavior == StdIo.Pipe) { + if (self.stderr_behavior == .Pipe) { os.close(stderr_pipe[0]); os.close(stderr_pipe[1]); } @@ -383,7 +383,7 @@ pub const ChildProcess = struct { os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err); } - os.posix.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err); + os.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err); } // we are the parent @@ -736,7 +736,7 @@ fn destroyPipe(pipe: [2]i32) void { // Then the child exits. fn forkChildErrReport(fd: i32, err: ChildProcess.SpawnError) noreturn { writeIntFd(fd, ErrInt(@errorToInt(err))) catch {}; - posix.exit(1); + os.exit(1); } const ErrInt = @IntType(false, @sizeOf(anyerror) * 8); diff --git a/std/crypto.zig b/std/crypto.zig index 2c3d8eba9e..2b57de9e60 100644 --- a/std/crypto.zig +++ b/std/crypto.zig @@ -33,7 +33,7 @@ pub const Poly1305 = @import("crypto/poly1305.zig").Poly1305; pub const X25519 = @import("crypto/x25519.zig").X25519; const std = @import("std.zig"); -pub const randomBytes = std.posix.getrandom; +pub const randomBytes = std.os.getrandom; test "crypto" { _ = @import("crypto/blake2.zig"); diff --git a/std/debug.zig b/std/debug.zig index 83daad6dc5..a309c31bbe 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1012,16 +1012,15 @@ fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DwarfInfo { errdefer S.self_exe_file.close(); const self_exe_mmap_len = try S.self_exe_file.getEndPos(); - const self_exe_mmap = os.posix.mmap( + const self_exe_mmap = try os.mmap( null, self_exe_mmap_len, - os.posix.PROT_READ, - os.posix.MAP_SHARED, + os.PROT_READ, + os.MAP_SHARED, S.self_exe_file.handle, 0, ); - if (self_exe_mmap == os.posix.MAP_FAILED) return error.OutOfMemory; - errdefer assert(os.posix.munmap(self_exe_mmap, self_exe_mmap_len) == 0); + errdefer os.munmap(self_exe_mmap, self_exe_mmap_len); const file_mmap_slice = @intToPtr([*]const u8, self_exe_mmap)[0..self_exe_mmap_len]; S.self_exe_mmap_seekable = io.SliceSeekableInStream.init(file_mmap_slice); diff --git a/std/event/fs.zig b/std/event/fs.zig index 346a6c6b69..fbdac65889 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -5,10 +5,9 @@ const assert = std.debug.assert; const testing = std.testing; const os = std.os; const mem = std.mem; -const posix = os.posix; const windows = os.windows; const Loop = event.Loop; -const fd_t = posix.fd_t; +const fd_t = os.fd_t; const File = std.fs.File; pub const RequestNode = std.atomic.Queue(Request).Node; @@ -33,7 +32,7 @@ pub const Request = struct { pub const PWriteV = struct { fd: fd_t, - iov: []const os.posix.iovec_const, + iov: []const os.iovec_const, offset: usize, result: Error!void, @@ -42,7 +41,7 @@ pub const Request = struct { pub const PReadV = struct { fd: fd_t, - iov: []const os.posix.iovec, + iov: []const os.iovec, offset: usize, result: Error!usize, @@ -89,11 +88,11 @@ pub async fn pwritev(loop: *Loop, fd: fd_t, data: []const []const u8, offset: us builtin.Os.freebsd, builtin.Os.netbsd, => { - const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len); + const iovecs = try loop.allocator.alloc(os.iovec_const, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec_const{ + iovecs[i] = os.iovec_const{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -171,7 +170,7 @@ pub async fn pwriteWindows(loop: *Loop, fd: fd_t, data: []const u8, offset: u64) pub async fn pwritevPosix( loop: *Loop, fd: fd_t, - iovecs: []const posix.iovec_const, + iovecs: []const os.iovec_const, offset: usize, ) os.PosixWriteError!void { // workaround for https://github.com/ziglang/zig/issues/1194 @@ -226,11 +225,11 @@ pub async fn preadv(loop: *Loop, fd: fd_t, data: []const []u8, offset: usize) PR builtin.Os.freebsd, builtin.Os.netbsd, => { - const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len); + const iovecs = try loop.allocator.alloc(os.iovec, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec{ + iovecs[i] = os.iovec{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -319,7 +318,7 @@ pub async fn preadWindows(loop: *Loop, fd: fd_t, data: []u8, offset: u64) !usize pub async fn preadvPosix( loop: *Loop, fd: fd_t, - iovecs: []const posix.iovec, + iovecs: []const os.iovec, offset: usize, ) os.PosixReadError!usize { // workaround for https://github.com/ziglang/zig/issues/1194 @@ -405,7 +404,7 @@ pub async fn openPosix( pub async fn openRead(loop: *Loop, path: []const u8) File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { - const flags = posix.O_LARGEFILE | posix.O_RDONLY | posix.O_CLOEXEC; + const flags = os.O_LARGEFILE | os.O_RDONLY | os.O_CLOEXEC; return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable); }, @@ -435,7 +434,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: File.Mode) File. builtin.Os.freebsd, builtin.Os.netbsd, => { - const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; + const flags = os.O_LARGEFILE | os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC | os.O_TRUNC; return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable); }, builtin.Os.windows => return os.windowsOpen( @@ -457,7 +456,7 @@ pub async fn openReadWrite( ) File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { - const flags = posix.O_LARGEFILE | posix.O_RDWR | posix.O_CREAT | posix.O_CLOEXEC; + const flags = os.O_LARGEFILE | os.O_RDWR | os.O_CREAT | os.O_CLOEXEC; return await (async openPosix(loop, path, flags, mode) catch unreachable); }, @@ -888,10 +887,7 @@ pub fn Watch(comptime V: type) type { var close_op_consumed = false; defer if (!close_op_consumed) close_op.finish(); - const flags = switch (builtin.os) { - builtin.Os.macosx => posix.O_SYMLINK | posix.O_EVTONLY, - else => 0, - }; + const flags = if (os.darwin.is_the_target) os.O_SYMLINK | os.O_EVTONLY else 0; const mode = 0; const fd = try await (async openPosix(self.channel.loop, resolved_path, flags, mode) catch unreachable); close_op.setHandle(fd); @@ -943,16 +939,16 @@ pub fn Watch(comptime V: type) type { while (true) { if (await (async self.channel.loop.bsdWaitKev( @intCast(usize, close_op.getHandle()), - posix.EVFILT_VNODE, - posix.NOTE_WRITE | posix.NOTE_DELETE, + os.EVFILT_VNODE, + os.NOTE_WRITE | os.NOTE_DELETE, ) catch unreachable)) |kev| { // TODO handle EV_ERROR - if (kev.fflags & posix.NOTE_DELETE != 0) { + if (kev.fflags & os.NOTE_DELETE != 0) { await (async self.channel.put(Self.Event{ .id = Event.Id.Delete, .data = value_copy, }) catch unreachable); - } else if (kev.fflags & posix.NOTE_WRITE != 0) { + } else if (kev.fflags & os.NOTE_WRITE != 0) { await (async self.channel.put(Self.Event{ .id = Event.Id.CloseWrite, .data = value_copy, diff --git a/std/event/loop.zig b/std/event/loop.zig index 71bc45d48f..e1848684c7 100644 --- a/std/event/loop.zig +++ b/std/event/loop.zig @@ -7,9 +7,9 @@ const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; const fs = std.event.fs; const os = std.os; -const posix = os.posix; const windows = os.windows; const maxInt = std.math.maxInt; +const Thread = std.Thread; pub const Loop = struct { allocator: *mem.Allocator, @@ -17,7 +17,7 @@ pub const Loop = struct { os_data: OsData, final_resume_node: ResumeNode, pending_event_count: usize, - extra_threads: []*os.Thread, + extra_threads: []*Thread, // pre-allocated eventfds. all permanently active. // this is how we send promises to be resumed on other threads. @@ -65,7 +65,7 @@ pub const Loop = struct { const KEventFd = struct { base: ResumeNode, - kevent: posix.Kevent, + kevent: os.Kevent, }; pub const Basic = switch (builtin.os) { @@ -81,7 +81,7 @@ pub const Loop = struct { const KEventBasic = struct { base: ResumeNode, - kev: posix.Kevent, + kev: os.Kevent, }; }; @@ -127,7 +127,7 @@ pub const Loop = struct { ); errdefer self.allocator.free(self.eventfd_resume_nodes); - self.extra_threads = try self.allocator.alloc(*os.Thread, extra_thread_count); + self.extra_threads = try self.allocator.alloc(*Thread, extra_thread_count); errdefer self.allocator.free(self.extra_threads); try self.initOsData(extra_thread_count); @@ -172,32 +172,32 @@ pub const Loop = struct { .handle = undefined, .overlapped = ResumeNode.overlapped_init, }, - .eventfd = try os.linuxEventFd(1, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK), - .epoll_op = posix.EPOLL_CTL_ADD, + .eventfd = try os.linuxEventFd(1, os.EFD_CLOEXEC | os.EFD_NONBLOCK), + .epoll_op = os.EPOLL_CTL_ADD, }, .next = undefined, }; self.available_eventfd_resume_nodes.push(eventfd_node); } - self.os_data.epollfd = try os.linuxEpollCreate(posix.EPOLL_CLOEXEC); + self.os_data.epollfd = try os.linuxEpollCreate(os.EPOLL_CLOEXEC); errdefer os.close(self.os_data.epollfd); - self.os_data.final_eventfd = try os.linuxEventFd(0, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK); + self.os_data.final_eventfd = try os.linuxEventFd(0, os.EFD_CLOEXEC | os.EFD_NONBLOCK); errdefer os.close(self.os_data.final_eventfd); - self.os_data.final_eventfd_event = posix.epoll_event{ - .events = posix.EPOLLIN, - .data = posix.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) }, + self.os_data.final_eventfd_event = os.epoll_event{ + .events = os.EPOLLIN, + .data = os.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) }, }; try os.linuxEpollCtl( self.os_data.epollfd, - posix.EPOLL_CTL_ADD, + os.EPOLL_CTL_ADD, self.os_data.final_eventfd, &self.os_data.final_eventfd_event, ); - self.os_data.fs_thread = try os.spawnThread(self, posixFsRun); + self.os_data.fs_thread = try Thread.spawn(self, posixFsRun); errdefer { self.posixFsRequest(&self.os_data.fs_end_request); self.os_data.fs_thread.wait(); @@ -218,7 +218,7 @@ pub const Loop = struct { } } while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) { - self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); + self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { @@ -240,7 +240,7 @@ pub const Loop = struct { }, }; - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; for (self.eventfd_resume_nodes) |*eventfd_node, i| { eventfd_node.* = std.atomic.Stack(ResumeNode.EventFd).Node{ @@ -251,10 +251,10 @@ pub const Loop = struct { .overlapped = ResumeNode.overlapped_init, }, // this one is for sending events - .kevent = posix.Kevent{ + .kevent = os.Kevent{ .ident = i, - .filter = posix.EVFILT_USER, - .flags = posix.EV_CLEAR | posix.EV_ADD | posix.EV_DISABLE, + .filter = os.EVFILT_USER, + .flags = os.EV_CLEAR | os.EV_ADD | os.EV_DISABLE, .fflags = 0, .data = 0, .udata = @ptrToInt(&eventfd_node.data.base), @@ -263,46 +263,46 @@ pub const Loop = struct { .next = undefined, }; self.available_eventfd_resume_nodes.push(eventfd_node); - const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.data.kevent); + const kevent_array = (*const [1]os.Kevent)(&eventfd_node.data.kevent); _ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null); - eventfd_node.data.kevent.flags = posix.EV_CLEAR | posix.EV_ENABLE; - eventfd_node.data.kevent.fflags = posix.NOTE_TRIGGER; + eventfd_node.data.kevent.flags = os.EV_CLEAR | os.EV_ENABLE; + eventfd_node.data.kevent.fflags = os.NOTE_TRIGGER; } // Pre-add so that we cannot get error.SystemResources // later when we try to activate it. - self.os_data.final_kevent = posix.Kevent{ + self.os_data.final_kevent = os.Kevent{ .ident = extra_thread_count, - .filter = posix.EVFILT_USER, - .flags = posix.EV_ADD | posix.EV_DISABLE, + .filter = os.EVFILT_USER, + .flags = os.EV_ADD | os.EV_DISABLE, .fflags = 0, .data = 0, .udata = @ptrToInt(&self.final_resume_node), }; - const final_kev_arr = (*const [1]posix.Kevent)(&self.os_data.final_kevent); + const final_kev_arr = (*const [1]os.Kevent)(&self.os_data.final_kevent); _ = try os.bsdKEvent(self.os_data.kqfd, final_kev_arr, empty_kevs, null); - self.os_data.final_kevent.flags = posix.EV_ENABLE; - self.os_data.final_kevent.fflags = posix.NOTE_TRIGGER; + self.os_data.final_kevent.flags = os.EV_ENABLE; + self.os_data.final_kevent.fflags = os.NOTE_TRIGGER; - self.os_data.fs_kevent_wake = posix.Kevent{ + self.os_data.fs_kevent_wake = os.Kevent{ .ident = 0, - .filter = posix.EVFILT_USER, - .flags = posix.EV_ADD | posix.EV_ENABLE, - .fflags = posix.NOTE_TRIGGER, + .filter = os.EVFILT_USER, + .flags = os.EV_ADD | os.EV_ENABLE, + .fflags = os.NOTE_TRIGGER, .data = 0, .udata = undefined, }; - self.os_data.fs_kevent_wait = posix.Kevent{ + self.os_data.fs_kevent_wait = os.Kevent{ .ident = 0, - .filter = posix.EVFILT_USER, - .flags = posix.EV_ADD | posix.EV_CLEAR, + .filter = os.EVFILT_USER, + .flags = os.EV_ADD | os.EV_CLEAR, .fflags = 0, .data = 0, .udata = undefined, }; - self.os_data.fs_thread = try os.spawnThread(self, posixFsRun); + self.os_data.fs_thread = try Thread.spawn(self, posixFsRun); errdefer { self.posixFsRequest(&self.os_data.fs_end_request); self.os_data.fs_thread.wait(); @@ -322,7 +322,7 @@ pub const Loop = struct { } } while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) { - self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); + self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, builtin.Os.windows => { @@ -371,7 +371,7 @@ pub const Loop = struct { } } while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) { - self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); + self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, else => {}, @@ -400,19 +400,19 @@ pub const Loop = struct { /// resume_node must live longer than the promise that it holds a reference to. /// flags must contain EPOLLET pub fn linuxAddFd(self: *Loop, fd: i32, resume_node: *ResumeNode, flags: u32) !void { - assert(flags & posix.EPOLLET == posix.EPOLLET); + assert(flags & os.EPOLLET == os.EPOLLET); self.beginOneEvent(); errdefer self.finishOneEvent(); try self.linuxModFd( fd, - posix.EPOLL_CTL_ADD, + os.EPOLL_CTL_ADD, flags, resume_node, ); } pub fn linuxModFd(self: *Loop, fd: i32, op: u32, flags: u32, resume_node: *ResumeNode) !void { - assert(flags & posix.EPOLLET == posix.EPOLLET); + assert(flags & os.EPOLLET == os.EPOLLET); var ev = os.linux.epoll_event{ .events = flags, .data = os.linux.epoll_data{ .ptr = @ptrToInt(resume_node) }, @@ -440,7 +440,7 @@ pub const Loop = struct { } } - pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !posix.Kevent { + pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !os.Kevent { // TODO #1194 suspend { resume @handle(); @@ -464,30 +464,30 @@ pub const Loop = struct { pub fn bsdAddKev(self: *Loop, resume_node: *ResumeNode.Basic, ident: usize, filter: i16, fflags: u32) !void { self.beginOneEvent(); errdefer self.finishOneEvent(); - var kev = posix.Kevent{ + var kev = os.Kevent{ .ident = ident, .filter = filter, - .flags = posix.EV_ADD | posix.EV_ENABLE | posix.EV_CLEAR, + .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR, .fflags = fflags, .data = 0, .udata = @ptrToInt(&resume_node.base), }; - const kevent_array = (*const [1]posix.Kevent)(&kev); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const kevent_array = (*const [1]os.Kevent)(&kev); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null); } pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void { - var kev = posix.Kevent{ + var kev = os.Kevent{ .ident = ident, .filter = filter, - .flags = posix.EV_DELETE, + .flags = os.EV_DELETE, .fflags = 0, .data = 0, .udata = 0, }; - const kevent_array = (*const [1]posix.Kevent)(&kev); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const kevent_array = (*const [1]os.Kevent)(&kev); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined; self.finishOneEvent(); } @@ -502,8 +502,8 @@ pub const Loop = struct { eventfd_node.base.handle = next_tick_node.data; switch (builtin.os) { builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.kevent); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const kevent_array = (*const [1]os.Kevent)(&eventfd_node.kevent); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch { self.next_tick_queue.unget(next_tick_node); self.available_eventfd_resume_nodes.push(resume_stack_node); @@ -512,7 +512,7 @@ pub const Loop = struct { }, builtin.Os.linux => { // the pending count is already accounted for - const epoll_events = posix.EPOLLONESHOT | os.linux.EPOLLIN | os.linux.EPOLLOUT | + const epoll_events = os.EPOLLONESHOT | os.linux.EPOLLIN | os.linux.EPOLLOUT | os.linux.EPOLLET; self.linuxModFd( eventfd_node.eventfd, @@ -631,8 +631,8 @@ pub const Loop = struct { }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { self.posixFsRequest(&self.os_data.fs_end_request); - const final_kevent = (*const [1]posix.Kevent)(&self.os_data.final_kevent); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const final_kevent = (*const [1]os.Kevent)(&self.os_data.final_kevent); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; // cannot fail because we already added it and this just enables it _ = os.bsdKEvent(self.os_data.kqfd, final_kevent, empty_kevs, null) catch unreachable; return; @@ -676,7 +676,7 @@ pub const Loop = struct { ResumeNode.Id.Stop => return, ResumeNode.Id.EventFd => { const event_fd_node = @fieldParentPtr(ResumeNode.EventFd, "base", resume_node); - event_fd_node.epoll_op = posix.EPOLL_CTL_MOD; + event_fd_node.epoll_op = os.EPOLL_CTL_MOD; const stack_node = @fieldParentPtr(std.atomic.Stack(ResumeNode.EventFd).Node, "data", event_fd_node); self.available_eventfd_resume_nodes.push(stack_node); }, @@ -688,8 +688,8 @@ pub const Loop = struct { } }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - var eventlist: [1]posix.Kevent = undefined; - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + var eventlist: [1]os.Kevent = undefined; + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; const count = os.bsdKEvent(self.os_data.kqfd, empty_kevs, eventlist[0..], null) catch unreachable; for (eventlist[0..count]) |ev| { const resume_node = @intToPtr(*ResumeNode, ev.udata); @@ -751,8 +751,8 @@ pub const Loop = struct { self.os_data.fs_queue.put(request_node); switch (builtin.os) { builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wake); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const fs_kevs = (*const [1]os.Kevent)(&self.os_data.fs_kevent_wake); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, empty_kevs, null) catch unreachable; }, builtin.Os.linux => { @@ -760,7 +760,7 @@ pub const Loop = struct { const rc = os.linux.futex_wake(&self.os_data.fs_queue_item, os.linux.FUTEX_WAKE, 1); switch (os.linux.getErrno(rc)) { 0 => {}, - posix.EINVAL => unreachable, + os.EINVAL => unreachable, else => unreachable, } }, @@ -793,8 +793,8 @@ pub const Loop = struct { }, @TagType(fs.Request.Msg).Close => |*msg| os.close(msg.fd), @TagType(fs.Request.Msg).WriteFile => |*msg| blk: { - const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | - posix.O_CLOEXEC | posix.O_TRUNC; + const flags = os.O_LARGEFILE | os.O_WRONLY | os.O_CREAT | + os.O_CLOEXEC | os.O_TRUNC; const fd = os.openC(msg.path.ptr, flags, msg.mode) catch |err| { msg.result = err; break :blk; @@ -816,13 +816,13 @@ pub const Loop = struct { builtin.Os.linux => { const rc = os.linux.futex_wait(&self.os_data.fs_queue_item, os.linux.FUTEX_WAIT, 0, null); switch (os.linux.getErrno(rc)) { - 0, posix.EINTR, posix.EAGAIN => continue, + 0, os.EINTR, os.EAGAIN => continue, else => unreachable, } }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wait); - var out_kevs: [1]posix.Kevent = undefined; + const fs_kevs = (*const [1]os.Kevent)(&self.os_data.fs_kevent_wait); + var out_kevs: [1]os.Kevent = undefined; _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, out_kevs[0..], null) catch unreachable; }, else => @compileError("Unsupported OS"), @@ -842,10 +842,10 @@ pub const Loop = struct { const KEventData = struct { kqfd: i32, - final_kevent: posix.Kevent, - fs_kevent_wake: posix.Kevent, - fs_kevent_wait: posix.Kevent, - fs_thread: *os.Thread, + final_kevent: os.Kevent, + fs_kevent_wake: os.Kevent, + fs_kevent_wait: os.Kevent, + fs_thread: *Thread, fs_kqfd: i32, fs_queue: std.atomic.Queue(fs.Request), fs_end_request: fs.RequestNode, @@ -855,7 +855,7 @@ pub const Loop = struct { epollfd: i32, final_eventfd: i32, final_eventfd_event: os.linux.epoll_event, - fs_thread: *os.Thread, + fs_thread: *Thread, fs_queue_item: i32, fs_queue: std.atomic.Queue(fs.Request), fs_end_request: fs.RequestNode, diff --git a/std/event/net.zig b/std/event/net.zig index bb492f1715..2346b1eb23 100644 --- a/std/event/net.zig +++ b/std/event/net.zig @@ -4,11 +4,9 @@ const testing = std.testing; const event = std.event; const mem = std.mem; const os = std.os; -const posix = os.posix; const Loop = std.event.Loop; const File = std.fs.File; - -const fd_t = posix.fd_t; +const fd_t = os.fd_t; pub const Server = struct { handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, File) void, @@ -47,19 +45,19 @@ pub const Server = struct { ) !void { self.handleRequestFn = handleRequestFn; - const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp); + const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); self.sockfd = sockfd; try os.posixBind(sockfd, &address.os_addr); - try os.posixListen(sockfd, posix.SOMAXCONN); + try os.posixListen(sockfd, os.SOMAXCONN); self.listen_address = std.net.Address.initPosix(try os.posixGetSockName(sockfd)); self.accept_coro = try async Server.handler(self); errdefer cancel self.accept_coro.?; self.listen_resume_node.handle = self.accept_coro.?; - try self.loop.linuxAddFd(sockfd, &self.listen_resume_node, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); + try self.loop.linuxAddFd(sockfd, &self.listen_resume_node, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); errdefer self.loop.removeFd(sockfd); } @@ -78,7 +76,7 @@ pub const Server = struct { while (true) { var accepted_addr: std.net.Address = undefined; // TODO just inline the following function here and don't expose it as posixAsyncAccept - if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| { + if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, os.SOCK_NONBLOCK | os.SOCK_CLOEXEC)) |accepted_fd| { if (accepted_fd == -1) { // would block suspend; // we will get resumed by epoll_wait in the event loop @@ -108,22 +106,22 @@ pub const Server = struct { pub async fn connectUnixSocket(loop: *Loop, path: []const u8) !i32 { const sockfd = try os.posixSocket( - posix.AF_UNIX, - posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, + os.AF_UNIX, + os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, 0, ); errdefer os.close(sockfd); - var sock_addr = posix.sockaddr_un{ - .family = posix.AF_UNIX, + var sock_addr = os.sockaddr_un{ + .family = os.AF_UNIX, .path = undefined, }; if (path.len > @typeOf(sock_addr.path).len) return error.NameTooLong; mem.copy(u8, sock_addr.path[0..], path); - const size = @intCast(u32, @sizeOf(posix.sa_family_t) + path.len); + const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len); try os.posixConnectAsync(sockfd, &sock_addr, size); - try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); + try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); try os.posixGetSockOptConnectError(sockfd); return sockfd; @@ -143,50 +141,48 @@ pub const ReadError = error{ /// returns number of bytes read. 0 means EOF. pub async fn read(loop: *std.event.Loop, fd: fd_t, buffer: []u8) ReadError!usize { - const iov = posix.iovec{ + const iov = os.iovec{ .iov_base = buffer.ptr, .iov_len = buffer.len, }; - const iovs: *const [1]posix.iovec = &iov; + const iovs: *const [1]os.iovec = &iov; return await (async readvPosix(loop, fd, iovs, 1) catch unreachable); } pub const WriteError = error{}; pub async fn write(loop: *std.event.Loop, fd: fd_t, buffer: []const u8) WriteError!void { - const iov = posix.iovec_const{ + const iov = os.iovec_const{ .iov_base = buffer.ptr, .iov_len = buffer.len, }; - const iovs: *const [1]posix.iovec_const = &iov; + const iovs: *const [1]os.iovec_const = &iov; return await (async writevPosix(loop, fd, iovs, 1) catch unreachable); } -pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const posix.iovec_const, count: usize) !void { +pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const os.iovec_const, count: usize) !void { while (true) { switch (builtin.os) { - builtin.Os.macosx, builtin.Os.linux => { - const rc = posix.writev(fd, iov, count); - const err = posix.getErrno(rc); - switch (err) { + .macosx, .linux => { + switch (os.errno(os.system.writev(fd, iov, count))) { 0 => return, - posix.EINTR => continue, - posix.ESPIPE => unreachable, - posix.EINVAL => unreachable, - posix.EFAULT => unreachable, - posix.EAGAIN => { - try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLOUT) catch unreachable); + os.EINTR => continue, + os.ESPIPE => unreachable, + os.EINVAL => unreachable, + os.EFAULT => unreachable, + os.EAGAIN => { + try await (async loop.linuxWaitFd(fd, os.EPOLLET | os.EPOLLOUT) catch unreachable); continue; }, - posix.EBADF => unreachable, // always a race condition - posix.EDESTADDRREQ => unreachable, // connect was never called - posix.EDQUOT => unreachable, - posix.EFBIG => unreachable, - posix.EIO => return error.InputOutput, - posix.ENOSPC => unreachable, - posix.EPERM => return error.AccessDenied, - posix.EPIPE => unreachable, - else => return os.unexpectedErrorPosix(err), + os.EBADF => unreachable, // always a race condition + os.EDESTADDRREQ => unreachable, // connect was never called + os.EDQUOT => unreachable, + os.EFBIG => unreachable, + os.EIO => return error.InputOutput, + os.ENOSPC => unreachable, + os.EPERM => return error.AccessDenied, + os.EPIPE => unreachable, + else => |err| return os.unexpectedErrno(err), } }, else => @compileError("Unsupported OS"), @@ -195,27 +191,26 @@ pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const posix.iovec_const, } /// returns number of bytes read. 0 means EOF. -pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, count: usize) !usize { +pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]os.iovec, count: usize) !usize { while (true) { switch (builtin.os) { builtin.Os.linux, builtin.Os.freebsd, builtin.Os.macosx => { - const rc = posix.readv(fd, iov, count); - const err = posix.getErrno(rc); - switch (err) { + const rc = os.system.readv(fd, iov, count); + switch (os.errno(rc)) { 0 => return rc, - posix.EINTR => continue, - posix.EINVAL => unreachable, - posix.EFAULT => unreachable, - posix.EAGAIN => { - try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLIN) catch unreachable); + os.EINTR => continue, + os.EINVAL => unreachable, + os.EFAULT => unreachable, + os.EAGAIN => { + try await (async loop.linuxWaitFd(fd, os.EPOLLET | os.EPOLLIN) catch unreachable); continue; }, - posix.EBADF => unreachable, // always a race condition - posix.EIO => return error.InputOutput, - posix.EISDIR => unreachable, - posix.ENOBUFS => return error.SystemResources, - posix.ENOMEM => return error.SystemResources, - else => return os.unexpectedErrorPosix(err), + os.EBADF => unreachable, // always a race condition + os.EIO => return error.InputOutput, + os.EISDIR => unreachable, + os.ENOBUFS => return error.SystemResources, + os.ENOMEM => return error.SystemResources, + else => |err| return os.unexpectedErrno(err), } }, else => @compileError("Unsupported OS"), @@ -224,11 +219,11 @@ pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, cou } pub async fn writev(loop: *Loop, fd: fd_t, data: []const []const u8) !void { - const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len); + const iovecs = try loop.allocator.alloc(os.iovec_const, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec_const{ + iovecs[i] = os.iovec_const{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -238,11 +233,11 @@ pub async fn writev(loop: *Loop, fd: fd_t, data: []const []const u8) !void { } pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { - const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len); + const iovecs = try loop.allocator.alloc(os.iovec, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec{ + iovecs[i] = os.iovec{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -254,11 +249,11 @@ pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { pub async fn connect(loop: *Loop, _address: *const std.net.Address) !File { var address = _address.*; // TODO https://github.com/ziglang/zig/issues/1592 - const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp); + const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); - try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(posix.sockaddr_in)); - try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); + try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(os.sockaddr_in)); + try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); try os.posixGetSockOptConnectError(sockfd); return File.openHandle(sockfd); diff --git a/std/fs/path.zig b/std/fs/path.zig index 4c73834609..b82ddc8117 100644 --- a/std/fs/path.zig +++ b/std/fs/path.zig @@ -9,24 +9,21 @@ const fmt = std.fmt; const Allocator = mem.Allocator; const os = std.os; const math = std.math; -const posix = os.posix; const windows = os.windows; const cstr = std.cstr; pub const sep_windows = '\\'; pub const sep_posix = '/'; -pub const sep = if (is_windows) sep_windows else sep_posix; +pub const sep = if (windows.is_the_target) sep_windows else sep_posix; pub const sep_str = [1]u8{sep}; pub const delimiter_windows = ';'; pub const delimiter_posix = ':'; -pub const delimiter = if (is_windows) delimiter_windows else delimiter_posix; - -const is_windows = builtin.os == builtin.Os.windows; +pub const delimiter = if (windows.is_the_target) delimiter_windows else delimiter_posix; pub fn isSep(byte: u8) bool { - if (is_windows) { + if (windows.is_the_target) { return byte == '/' or byte == '\\'; } else { return byte == '/'; @@ -76,7 +73,7 @@ fn joinSep(allocator: *Allocator, separator: u8, paths: []const []const u8) ![]u return buf; } -pub const join = if (is_windows) joinWindows else joinPosix; +pub const join = if (windows.is_the_target) joinWindows else joinPosix; /// Naively combines a series of paths with the native path seperator. /// Allocates memory for the result, which must be freed by the caller. @@ -133,7 +130,7 @@ test "join" { } pub fn isAbsolute(path: []const u8) bool { - if (is_windows) { + if (windows.is_the_target) { return isAbsoluteWindows(path); } else { return isAbsolutePosix(path); @@ -312,7 +309,7 @@ test "windowsParsePath" { } pub fn diskDesignator(path: []const u8) []const u8 { - if (is_windows) { + if (windows.is_the_target) { return diskDesignatorWindows(path); } else { return ""; @@ -377,7 +374,7 @@ fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool { /// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`. pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { - if (is_windows) { + if (windows.is_the_target) { return resolveWindows(allocator, paths); } else { return resolvePosix(allocator, paths); @@ -394,7 +391,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { /// Without performing actual syscalls, resolving `..` could be incorrect. pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { - assert(is_windows); // resolveWindows called on non windows can't use getCwd + assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd return os.getCwdAlloc(allocator); } @@ -489,7 +486,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { result_disk_designator = result[0..result_index]; }, WindowsPath.Kind.None => { - assert(is_windows); // resolveWindows called on non windows can't use getCwd + assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); const parsed_cwd = windowsParsePath(cwd); @@ -504,7 +501,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { }, } } else { - assert(is_windows); // resolveWindows called on non windows can't use getCwd + assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd // TODO call get cwd for the result_disk_designator instead of the global one const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); @@ -575,7 +572,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { /// Without performing actual syscalls, resolving `..` could be incorrect. pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { - assert(!is_windows); // resolvePosix called on windows can't use getCwd + assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd return os.getCwdAlloc(allocator); } @@ -597,7 +594,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (have_abs) { result = try allocator.alloc(u8, max_size); } else { - assert(!is_windows); // resolvePosix called on windows can't use getCwd + assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); @@ -638,7 +635,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { test "resolve" { const cwd = try os.getCwdAlloc(debug.global_allocator); - if (is_windows) { + if (windows.is_the_target) { if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { cwd[0] = asciiUpper(cwd[0]); } @@ -650,7 +647,7 @@ test "resolve" { } test "resolveWindows" { - if (is_windows) { + if (windows.is_the_target) { const cwd = try os.getCwdAlloc(debug.global_allocator); const parsed_cwd = windowsParsePath(cwd); { @@ -716,7 +713,7 @@ fn testResolvePosix(paths: []const []const u8) []u8 { /// If the path is a file in the current directory (no directory component) /// then returns null pub fn dirname(path: []const u8) ?[]const u8 { - if (is_windows) { + if (windows.is_the_target) { return dirnameWindows(path); } else { return dirnamePosix(path); @@ -848,7 +845,7 @@ fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void { } pub fn basename(path: []const u8) []const u8 { - if (is_windows) { + if (windows.is_the_target) { return basenameWindows(path); } else { return basenamePosix(path); @@ -964,7 +961,7 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) void { /// string is returned. /// On Windows this canonicalizes the drive to a capital letter and paths to `\\`. pub fn relative(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 { - if (is_windows) { + if (windows.is_the_target) { return relativeWindows(allocator, from, to); } else { return relativePosix(allocator, from, to); diff --git a/std/heap.zig b/std/heap.zig index 3bbb35e65d..4ff36f3f5f 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -5,7 +5,6 @@ const testing = std.testing; const mem = std.mem; const os = std.os; const builtin = @import("builtin"); -const Os = builtin.Os; const c = std.c; const maxInt = std.math.maxInt; @@ -51,201 +50,190 @@ pub const DirectAllocator = struct { if (n == 0) return (([*]u8)(undefined))[0..0]; - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { - const p = os.posix; - const alloc_size = if (alignment <= os.page_size) n else n + alignment; - const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0); - if (addr == p.MAP_FAILED) return error.OutOfMemory; - if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n]; - - const aligned_addr = mem.alignForward(addr, alignment); - - // Unmap the extra bytes that were only requested in order to guarantee - // that the range of memory we were provided had a proper alignment in - // it somewhere. The extra bytes could be at the beginning, or end, or both. - const unused_start_len = aligned_addr - addr; - if (unused_start_len != 0) { - const err = p.munmap(addr, unused_start_len); - assert(p.getErrno(err) == 0); - } - const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size); - const unused_end_len = addr + alloc_size - aligned_end_addr; - if (unused_end_len != 0) { - const err = p.munmap(aligned_end_addr, unused_end_len); - assert(p.getErrno(err) == 0); - } + if (os.windows.is_the_target) { + const w = os.windows; + + // Although officially it's at least aligned to page boundary, + // Windows is known to reserve pages on a 64K boundary. It's + // even more likely that the requested alignment is <= 64K than + // 4K, so we're just allocating blindly and hoping for the best. + // see https://devblogs.microsoft.com/oldnewthing/?p=42223 + const addr = w.VirtualAlloc( + null, + n, + w.MEM_COMMIT | w.MEM_RESERVE, + w.PAGE_READWRITE, + ) catch return error.OutOfMemory; + + // If the allocation is sufficiently aligned, use it. + if (@ptrToInt(addr) & (alignment - 1) == 0) { + return @ptrCast([*]u8, addr)[0..n]; + } - return @intToPtr([*]u8, aligned_addr)[0..n]; - }, - .windows => { - const w = os.windows; - - // Although officially it's at least aligned to page boundary, - // Windows is known to reserve pages on a 64K boundary. It's - // even more likely that the requested alignment is <= 64K than - // 4K, so we're just allocating blindly and hoping for the best. - // see https://devblogs.microsoft.com/oldnewthing/?p=42223 - const addr = w.VirtualAlloc( + // If it wasn't, actually do an explicitely aligned allocation. + w.VirtualFree(addr, 0, w.MEM_RELEASE); + const alloc_size = n + alignment; + + const final_addr = while (true) { + // Reserve a range of memory large enough to find a sufficiently + // aligned address. + const reserved_addr = w.VirtualAlloc( null, + alloc_size, + w.MEM_RESERVE, + w.PAGE_NOACCESS, + ) catch return error.OutOfMemory; + const aligned_addr = mem.alignForward(@ptrToInt(reserved_addr), alignment); + + // Release the reserved pages (not actually used). + w.VirtualFree(reserved_addr, 0, w.MEM_RELEASE); + + // At this point, it is possible that another thread has + // obtained some memory space that will cause the next + // VirtualAlloc call to fail. To handle this, we will retry + // until it succeeds. + return w.VirtualAlloc( + @intToPtr(*c_void, aligned_addr), n, w.MEM_COMMIT | w.MEM_RESERVE, w.PAGE_READWRITE, - ) orelse return error.OutOfMemory; + ) catch continue; + }; - // If the allocation is sufficiently aligned, use it. - if (@ptrToInt(addr) & (alignment - 1) == 0) { - return @ptrCast([*]u8, addr)[0..n]; - } + return @ptrCast([*]u8, final_addr)[0..n]; + } - // If it wasn't, actually do an explicitely aligned allocation. - if (w.VirtualFree(addr, 0, w.MEM_RELEASE) == 0) unreachable; - const alloc_size = n + alignment; - - const final_addr = while (true) { - // Reserve a range of memory large enough to find a sufficiently - // aligned address. - const reserved_addr = w.VirtualAlloc( - null, - alloc_size, - w.MEM_RESERVE, - w.PAGE_NOACCESS, - ) orelse return error.OutOfMemory; - const aligned_addr = mem.alignForward(@ptrToInt(reserved_addr), alignment); - - // Release the reserved pages (not actually used). - if (w.VirtualFree(reserved_addr, 0, w.MEM_RELEASE) == 0) unreachable; - - // At this point, it is possible that another thread has - // obtained some memory space that will cause the next - // VirtualAlloc call to fail. To handle this, we will retry - // until it succeeds. - if (w.VirtualAlloc( - @intToPtr(*c_void, aligned_addr), - n, - w.MEM_COMMIT | w.MEM_RESERVE, - w.PAGE_READWRITE, - )) |ptr| break ptr; - } else unreachable; // TODO else unreachable should not be necessary - - return @ptrCast([*]u8, final_addr)[0..n]; - }, - else => @compileError("Unsupported OS"), + const alloc_size = if (alignment <= os.page_size) n else n + alignment; + const addr = os.mmap( + null, + alloc_size, + os.PROT_READ | os.PROT_WRITE, + os.MAP_PRIVATE | os.MAP_ANONYMOUS, + -1, + 0, + ) catch return error.OutOfMemory; + if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n]; + + const aligned_addr = mem.alignForward(addr, alignment); + + // Unmap the extra bytes that were only requested in order to guarantee + // that the range of memory we were provided had a proper alignment in + // it somewhere. The extra bytes could be at the beginning, or end, or both. + const unused_start_len = aligned_addr - addr; + if (unused_start_len != 0) { + os.munmap(addr, unused_start_len); + } + const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size); + const unused_end_len = addr + alloc_size - aligned_end_addr; + if (unused_end_len != 0) { + os.munmap(aligned_end_addr, unused_end_len); } + + return @intToPtr([*]u8, aligned_addr)[0..n]; } fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { + if (os.windows.is_the_target) { + const w = os.windows; + if (new_size == 0) { + // From the docs: + // "If the dwFreeType parameter is MEM_RELEASE, this parameter + // must be 0 (zero). The function frees the entire region that + // is reserved in the initial allocation call to VirtualAlloc." + // So we can only use MEM_RELEASE when actually releasing the + // whole allocation. + w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE); + } else { const base_addr = @ptrToInt(old_mem.ptr); const old_addr_end = base_addr + old_mem.len; const new_addr_end = base_addr + new_size; const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); if (old_addr_end > new_addr_end_rounded) { - _ = os.posix.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded); - } - return old_mem[0..new_size]; - }, - .windows => { - const w = os.windows; - if (new_size == 0) { - // From the docs: - // "If the dwFreeType parameter is MEM_RELEASE, this parameter - // must be 0 (zero). The function frees the entire region that - // is reserved in the initial allocation call to VirtualAlloc." - // So we can only use MEM_RELEASE when actually releasing the - // whole allocation. - if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable; - } else { - const base_addr = @ptrToInt(old_mem.ptr); - const old_addr_end = base_addr + old_mem.len; - const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); - if (old_addr_end > new_addr_end_rounded) { - // For shrinking that is not releasing, we will only - // decommit the pages not needed anymore. - if (w.VirtualFree( - @intToPtr(*c_void, new_addr_end_rounded), - old_addr_end - new_addr_end_rounded, - w.MEM_DECOMMIT, - ) == 0) unreachable; - } + // For shrinking that is not releasing, we will only + // decommit the pages not needed anymore. + w.VirtualFree( + @intToPtr(*c_void, new_addr_end_rounded), + old_addr_end - new_addr_end_rounded, + w.MEM_DECOMMIT, + ); } - return old_mem[0..new_size]; - }, - else => @compileError("Unsupported OS"), + } + return old_mem[0..new_size]; } + const base_addr = @ptrToInt(old_mem.ptr); + const old_addr_end = base_addr + old_mem.len; + const new_addr_end = base_addr + new_size; + const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + if (old_addr_end > new_addr_end_rounded) { + os.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded); + } + return old_mem[0..new_size]; } fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { - if (new_size <= old_mem.len and new_align <= old_align) { - return shrink(allocator, old_mem, old_align, new_size, new_align); - } - const result = try alloc(allocator, new_size, new_align); - if (old_mem.len != 0) { - @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); - _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len); - } - return result; - }, - .windows => { - if (old_mem.len == 0) { - return alloc(allocator, new_size, new_align); - } - - if (new_size <= old_mem.len and new_align <= old_align) { - return shrink(allocator, old_mem, old_align, new_size, new_align); - } - - const w = os.windows; - const base_addr = @ptrToInt(old_mem.ptr); + if (os.windows.is_the_target) { + if (old_mem.len == 0) { + return alloc(allocator, new_size, new_align); + } - if (new_align > old_align and base_addr & (new_align - 1) != 0) { - // Current allocation doesn't satisfy the new alignment. - // For now we'll do a new one no matter what, but maybe - // there is something smarter to do instead. - const result = try alloc(allocator, new_size, new_align); - assert(old_mem.len != 0); - @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); - if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable; + if (new_size <= old_mem.len and new_align <= old_align) { + return shrink(allocator, old_mem, old_align, new_size, new_align); + } - return result; - } + const w = os.windows; + const base_addr = @ptrToInt(old_mem.ptr); - const old_addr_end = base_addr + old_mem.len; - const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size); - const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); - if (new_addr_end_rounded == old_addr_end_rounded) { - // The reallocation fits in the already allocated pages. - return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; - } - assert(new_addr_end_rounded > old_addr_end_rounded); + if (new_align > old_align and base_addr & (new_align - 1) != 0) { + // Current allocation doesn't satisfy the new alignment. + // For now we'll do a new one no matter what, but maybe + // there is something smarter to do instead. + const result = try alloc(allocator, new_size, new_align); + assert(old_mem.len != 0); + @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); + w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE); - // We need to commit new pages. - const additional_size = new_addr_end - old_addr_end_rounded; - const realloc_addr = w.VirtualAlloc( - @intToPtr(*c_void, old_addr_end_rounded), - additional_size, - w.MEM_COMMIT | w.MEM_RESERVE, - w.PAGE_READWRITE, - ) orelse { - // Committing new pages at the end of the existing allocation - // failed, we need to try a new one. - const new_alloc_mem = try alloc(allocator, new_size, new_align); - @memcpy(new_alloc_mem.ptr, old_mem.ptr, old_mem.len); - if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable; - - return new_alloc_mem; - }; + return result; + } - assert(@ptrToInt(realloc_addr) == old_addr_end_rounded); + const old_addr_end = base_addr + old_mem.len; + const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size); + const new_addr_end = base_addr + new_size; + const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + if (new_addr_end_rounded == old_addr_end_rounded) { + // The reallocation fits in the already allocated pages. return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; - }, - else => @compileError("Unsupported OS"), + } + assert(new_addr_end_rounded > old_addr_end_rounded); + + // We need to commit new pages. + const additional_size = new_addr_end - old_addr_end_rounded; + const realloc_addr = w.kernel32.VirtualAlloc( + @intToPtr(*c_void, old_addr_end_rounded), + additional_size, + w.MEM_COMMIT | w.MEM_RESERVE, + w.PAGE_READWRITE, + ) orelse { + // Committing new pages at the end of the existing allocation + // failed, we need to try a new one. + const new_alloc_mem = try alloc(allocator, new_size, new_align); + @memcpy(new_alloc_mem.ptr, old_mem.ptr, old_mem.len); + w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE); + + return new_alloc_mem; + }; + + assert(@ptrToInt(realloc_addr) == old_addr_end_rounded); + return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; + } + if (new_size <= old_mem.len and new_align <= old_align) { + return shrink(allocator, old_mem, old_align, new_size, new_align); } + const result = try alloc(allocator, new_size, new_align); + if (old_mem.len != 0) { + @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); + os.munmap(@ptrToInt(old_mem.ptr), old_mem.len); + } + return result; } }; diff --git a/std/mem.zig b/std/mem.zig index 50ed2771ec..4f7ea768d7 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -1466,17 +1466,17 @@ test "std.mem.alignForward" { pub fn getBaseAddress() usize { switch (builtin.os) { .linux => { - const base = std.os.posix.getauxval(std.elf.AT_BASE); + const base = std.os.system.getauxval(std.elf.AT_BASE); if (base != 0) { return base; } - const phdr = std.os.posix.getauxval(std.elf.AT_PHDR); + const phdr = std.os.system.getauxval(std.elf.AT_PHDR); return phdr - @sizeOf(std.elf.Ehdr); }, .macosx, .freebsd, .netbsd => { return @ptrToInt(&std.c._mh_execute_header); }, - .windows => return @ptrToInt(windows.GetModuleHandleW(null)), + .windows => return @ptrToInt(windows.kernel32.GetModuleHandleW(null)), else => @compileError("Unsupported OS"), } } diff --git a/std/mutex.zig b/std/mutex.zig index 2b3ac4e366..6b8e586ea8 100644 --- a/std/mutex.zig +++ b/std/mutex.zig @@ -152,9 +152,9 @@ test "std.Mutex" { testing.expect(context.data == TestContext.incr_count); } else { const thread_count = 10; - var threads: [thread_count]*std.os.Thread = undefined; + var threads: [thread_count]*std.Thread = undefined; for (threads) |*t| { - t.* = try std.os.spawnThread(&context, worker); + t.* = try std.Thread.spawn(&context, worker); } for (threads) |t| t.wait(); diff --git a/std/net.zig b/std/net.zig index bb292efd83..b426759701 100644 --- a/std/net.zig +++ b/std/net.zig @@ -2,8 +2,8 @@ const std = @import("std.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; const net = @This(); -const posix = std.os.posix; const mem = std.mem; +const os = std.os; pub const TmpWinAddr = struct { family: u8, @@ -12,7 +12,7 @@ pub const TmpWinAddr = struct { pub const OsAddress = switch (builtin.os) { builtin.Os.windows => TmpWinAddr, - else => posix.sockaddr, + else => os.sockaddr, }; pub const Address = struct { @@ -20,9 +20,9 @@ pub const Address = struct { pub fn initIp4(ip4: u32, _port: u16) Address { return Address{ - .os_addr = posix.sockaddr{ - .in = posix.sockaddr_in{ - .family = posix.AF_INET, + .os_addr = os.sockaddr{ + .in = os.sockaddr_in{ + .family = os.AF_INET, .port = mem.nativeToBig(u16, _port), .addr = ip4, .zero = []u8{0} ** 8, @@ -33,10 +33,10 @@ pub const Address = struct { pub fn initIp6(ip6: *const Ip6Addr, _port: u16) Address { return Address{ - .family = posix.AF_INET6, - .os_addr = posix.sockaddr{ - .in6 = posix.sockaddr_in6{ - .family = posix.AF_INET6, + .family = os.AF_INET6, + .os_addr = os.sockaddr{ + .in6 = os.sockaddr_in6{ + .family = os.AF_INET6, .port = mem.nativeToBig(u16, _port), .flowinfo = 0, .addr = ip6.addr, @@ -50,18 +50,18 @@ pub const Address = struct { return mem.bigToNative(u16, self.os_addr.in.port); } - pub fn initPosix(addr: posix.sockaddr) Address { + pub fn initPosix(addr: os.sockaddr) Address { return Address{ .os_addr = addr }; } pub fn format(self: *const Address, out_stream: var) !void { switch (self.os_addr.in.family) { - posix.AF_INET => { + os.AF_INET => { const native_endian_port = mem.bigToNative(u16, self.os_addr.in.port); const bytes = ([]const u8)((*self.os_addr.in.addr)[0..1]); try out_stream.print("{}.{}.{}.{}:{}", bytes[0], bytes[1], bytes[2], bytes[3], native_endian_port); }, - posix.AF_INET6 => { + os.AF_INET6 => { const native_endian_port = mem.bigToNative(u16, self.os_addr.in6.port); try out_stream.print("[TODO render ip6 address]:{}", native_endian_port); }, diff --git a/std/os.zig b/std/os.zig index 4cd847cf8f..3b29570fcd 100644 --- a/std/os.zig +++ b/std/os.zig @@ -164,7 +164,7 @@ pub fn raise(sig: u8) RaiseError!void { } if (windows.is_the_target) { - @compileError("TODO implement std.posix.raise for Windows"); + @compileError("TODO implement std.os.raise for Windows"); } var set: system.sigset_t = undefined; @@ -690,20 +690,6 @@ pub fn getenvC(key: [*]const u8) ?[]const u8 { return getenv(mem.toSliceConst(u8, key)); } -/// See std.elf for the constants. -pub fn getauxval(index: usize) usize { - if (builtin.link_libc) { - return usize(system.getauxval(index)); - } else if (linux.elf_aux_maybe) |auxv| { - var i: usize = 0; - while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { - if (auxv[i].a_type == index) - return auxv[i].a_un.a_val; - } - } - return 0; -} - pub const GetCwdError = error{ NameTooLong, CurrentWorkingDirectoryUnlinked, @@ -1198,7 +1184,7 @@ pub fn isatty(handle: fd_t) bool { return windows.kernel32.GetConsoleMode(handle, &out) != 0; } if (wasi.is_the_target) { - @compileError("TODO implement std.os.posix.isatty for WASI"); + @compileError("TODO implement std.os.isatty for WASI"); } if (linux.is_the_target) { var wsz: system.winsize = undefined; @@ -2365,6 +2351,23 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void { } } +pub const SchedGetAffinityError = error{ + PermissionDenied, + Unexpected, +}; + +pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t { + var set: cpu_set_t = undefined; + switch (errno(system.sched_getaffinity(pid, &set))) { + 0 => return set, + EFAULT => unreachable, + EINVAL => unreachable, + ESRCH => unreachable, + EPERM => return error.PermissionDenied, + else => |err| return unexpectedErrno(err), + } +} + /// Used to convert a slice to a null terminated slice on the stack. /// TODO https://github.com/ziglang/zig/issues/287 pub fn toPosixPath(file_path: []const u8) ![PATH_MAX]u8 { diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index 7957187896..f40beeaf68 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -1,3 +1,5 @@ +const std = @import("../../std.zig"); + pub use @import("linux/errno.zig"); pub use switch (builtin.arch) { .x86_64 => @import("linux/x86_64.zig"), @@ -907,3 +909,26 @@ pub const pthread_attr_t = extern struct { __size: [56]u8, __align: c_long, }; + +pub const CPU_SETSIZE = 128; +pub const cpu_set_t = [CPU_SETSIZE / @sizeOf(usize)]usize; +pub const cpu_count_t = @IntType(false, std.math.log2(CPU_SETSIZE * 8)); + +pub fn CPU_COUNT(set: cpu_set_t) cpu_count_t { + var sum: cpu_count_t = 0; + for (set) |x| { + sum += @popCount(usize, x); + } + return sum; +} + +// TODO port these over +//#define CPU_SET(i, set) CPU_SET_S(i,sizeof(cpu_set_t),set) +//#define CPU_CLR(i, set) CPU_CLR_S(i,sizeof(cpu_set_t),set) +//#define CPU_ISSET(i, set) CPU_ISSET_S(i,sizeof(cpu_set_t),set) +//#define CPU_AND(d,s1,s2) CPU_AND_S(sizeof(cpu_set_t),d,s1,s2) +//#define CPU_OR(d,s1,s2) CPU_OR_S(sizeof(cpu_set_t),d,s1,s2) +//#define CPU_XOR(d,s1,s2) CPU_XOR_S(sizeof(cpu_set_t),d,s1,s2) +//#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t),set) +//#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t),set) +//#define CPU_EQUAL(s1,s2) CPU_EQUAL_S(sizeof(cpu_set_t),s1,s2) diff --git a/std/os/linux.zig b/std/os/linux.zig index 40e9fa5108..88a2980129 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -1,8 +1,878 @@ +// This file provides the system interface functions for Linux matching those +// that are provided by libc, whether or not libc is linked. The following +// abstractions are made: +// * Work around kernel bugs and limitations. For example, see sendmmsg. +// * Implement all the syscalls in the same way that libc functions will +// provide `rename` when only the `renameat` syscall exists. +// * Does not support POSIX thread cancellation. const std = @import("../std.zig"); const builtin = @import("builtin"); +const assert = std.debug.assert; +const maxInt = std.math.maxInt; +const elf = std.elf; +const vdso = @import("linux/vdso.zig"); +const dl = @import("../dynamic_library.zig"); + pub const is_the_target = builtin.os == .linux; -pub const sys = @import("linux/sys.zig"); -pub use if (builtin.link_libc) std.c else sys; +pub use switch (builtin.arch) { + .x86_64 => @import("linux/x86_64.zig"), + .aarch64 => @import("linux/arm64.zig"), + else => struct {}, +}; +pub use @import("bits.zig"); + +/// Set by startup code, used by `getauxval`. +pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; + +/// See `std.elf` for the constants. +pub fn getauxval(index: usize) usize { + const auxv = elf_aux_maybe orelse return 0; + var i: usize = 0; + while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { + if (auxv[i].a_type == index) + return auxv[i].a_un.a_val; + } + return 0; +} + +/// Get the errno from a syscall return value, or 0 for no error. +pub fn getErrno(r: usize) u12 { + const signed_r = @bitCast(isize, r); + return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0; +} + +pub fn dup2(old: i32, new: i32) usize { + if (@hasDecl(@This(), "SYS_dup2")) { + return syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new))); + } else { + if (old == new) { + if (std.debug.runtime_safety) { + const rc = syscall2(SYS_fcntl, @bitCast(usize, isize(old)), F_GETFD); + if (@bitCast(isize, rc) < 0) return rc; + } + return @intCast(usize, old); + } else { + return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), 0); + } + } +} + +pub fn dup3(old: i32, new: i32, flags: u32) usize { + return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn chdir(path: [*]const u8) usize { + return syscall1(SYS_chdir, @ptrToInt(path)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn chroot(path: [*]const u8) usize { + return syscall1(SYS_chroot, @ptrToInt(path)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { + return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); +} + +pub fn fork() usize { + if (@hasDecl(@This(), "SYS_fork")) { + return syscall0(SYS_fork); + } else { + return syscall2(SYS_clone, SIGCHLD, 0); + } +} + +/// This must be inline, and inline call the syscall function, because if the +/// child does a return it will clobber the parent's stack. +/// It is advised to avoid this function and use clone instead, because +/// the compiler is not aware of how vfork affects control flow and you may +/// see different results in optimized builds. +pub inline fn vfork() usize { + return @inlineCall(syscall0, SYS_vfork); +} + +pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize { + return syscall4(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout)); +} + +pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize { + return syscall3(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val)); +} + +pub fn getcwd(buf: [*]u8, size: usize) usize { + return syscall2(SYS_getcwd, @ptrToInt(buf), size); +} + +pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { + return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); +} + +pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize { + return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); +} + +pub fn inotify_init1(flags: u32) usize { + return syscall1(SYS_inotify_init1, flags); +} + +pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize { + return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask); +} + +pub fn inotify_rm_watch(fd: i32, wd: i32) usize { + return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd))); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { + if (@hasDecl(@This(), "SYS_readlink")) { + return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); + } else { + return syscall4(SYS_readlinkat, AT_FDCWD, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { + return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn mkdir(path: [*]const u8, mode: u32) usize { + if (@hasDecl(@This(), "SYS_mkdir")) { + return syscall2(SYS_mkdir, @ptrToInt(path), mode); + } else { + return syscall3(SYS_mkdirat, AT_FDCWD, @ptrToInt(path), mode); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize { + return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize { + return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn umount(special: [*]const u8) usize { + return syscall2(SYS_umount2, @ptrToInt(special), 0); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn umount2(special: [*]const u8, flags: u32) usize { + return syscall2(SYS_umount2, @ptrToInt(special), flags); +} + +pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { + return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); +} + +pub fn mprotect(address: usize, length: usize, protection: usize) usize { + return syscall3(SYS_mprotect, address, length, protection); +} + +pub fn munmap(address: usize, length: usize) usize { + return syscall2(SYS_munmap, address, length); +} + +pub fn read(fd: i32, buf: [*]u8, count: usize) usize { + return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); +} + +pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { + return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); +} + +pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { + return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); +} + +pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { + return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); +} + +pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { + return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn rmdir(path: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_rmdir")) { + return syscall1(SYS_rmdir, @ptrToInt(path)); + } else { + return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), AT_REMOVEDIR); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_symlink")) { + return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); + } else { + return syscall3(SYS_symlinkat, @ptrToInt(existing), AT_FDCWD, @ptrToInt(new)); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize { + return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { + return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn access(path: [*]const u8, mode: u32) usize { + return syscall2(SYS_access, @ptrToInt(path), mode); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize { + return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); +} + +pub fn pipe(fd: *[2]i32) usize { + if (@hasDecl(@This(), "SYS_pipe")) { + return syscall1(SYS_pipe, @ptrToInt(fd)); + } else { + return syscall2(SYS_pipe2, @ptrToInt(fd), 0); + } +} + +pub fn pipe2(fd: *[2]i32, flags: u32) usize { + return syscall2(SYS_pipe2, @ptrToInt(fd), flags); +} + +pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { + return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); +} + +pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { + return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn rename(old: [*]const u8, new: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_rename")) { + return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new)); + } else if (@hasDecl(@This(), "SYS_renameat")) { + return syscall4(SYS_renameat, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new)); + } else { + return syscall5(SYS_renameat2, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new), 0); + } +} + +pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_renameat")) { + return syscall4( + SYS_renameat, + @bitCast(usize, isize(oldfd)), + @ptrToInt(old), + @bitCast(usize, isize(newfd)), + @ptrToInt(new), + ); + } else { + return syscall5( + SYS_renameat2, + @bitCast(usize, isize(oldfd)), + @ptrToInt(old), + @bitCast(usize, isize(newfd)), + @ptrToInt(new), + 0, + ); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize { + return syscall5( + SYS_renameat2, + @bitCast(usize, isize(oldfd)), + @ptrToInt(oldpath), + @bitCast(usize, isize(newfd)), + @ptrToInt(newpath), + flags, + ); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn open(path: [*]const u8, flags: u32, perm: usize) usize { + return syscall3(SYS_open, @ptrToInt(path), flags, perm); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn create(path: [*]const u8, perm: usize) usize { + return syscall2(SYS_creat, @ptrToInt(path), perm); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize { + // dirfd could be negative, for example AT_FDCWD is -100 + return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode); +} + +/// See also `clone` (from the arch-specific include) +pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize { + return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls); +} + +/// See also `clone` (from the arch-specific include) +pub fn clone2(flags: u32, child_stack_ptr: usize) usize { + return syscall2(SYS_clone, flags, child_stack_ptr); +} + +pub fn close(fd: i32) usize { + return syscall1(SYS_close, @bitCast(usize, isize(fd))); +} + +/// Can only be called on 32 bit systems. For 64 bit see `lseek`. +pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize { + return syscall5( + SYS__llseek, + @bitCast(usize, isize(fd)), + @truncate(usize, offset >> 32), + @truncate(usize, offset), + @ptrToInt(result), + whence, + ); +} + +/// Can only be called on 64 bit systems. For 32 bit see `llseek`. +pub fn lseek(fd: i32, offset: i64, whence: usize) usize { + return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), whence); +} + +pub fn exit(status: i32) noreturn { + _ = syscall1(SYS_exit, @bitCast(usize, isize(status))); + unreachable; +} + +pub fn exit_group(status: i32) noreturn { + _ = syscall1(SYS_exit_group, @bitCast(usize, isize(status))); + unreachable; +} + +pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize { + return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags); +} + +pub fn kill(pid: i32, sig: i32) usize { + return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig))); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn unlink(path: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_unlink")) { + return syscall1(SYS_unlink, @ptrToInt(path)); + } else { + return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), 0); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { + return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags); +} + +pub fn waitpid(pid: i32, status: *i32, options: i32) usize { + return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); +} + +var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime); + +// We must follow the C calling convention when we call into the VDSO +const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize; + +pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { + if (VDSO_CGT_SYM.len != 0) { + const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered); + if (ptr) |fn_ptr| { + const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); + const rc = f(clk_id, tp); + switch (rc) { + 0, @bitCast(usize, isize(-EINVAL)) => return rc, + else => {}, + } + } + } + return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize { + const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM)); + // Note that we may not have a VDSO at all, update the stub address anyway + // so that clock_gettime will fall back on the good old (and slow) syscall + _ = @cmpxchgStrong(?*const c_void, &vdso_clock_gettime, &init_vdso_clock_gettime, ptr, .Monotonic, .Monotonic); + // Call into the VDSO if available + if (ptr) |fn_ptr| { + const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); + return f(clk, ts); + } + return @bitCast(usize, isize(-ENOSYS)); +} + +pub fn clock_getres(clk_id: i32, tp: *timespec) usize { + return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +pub fn clock_settime(clk_id: i32, tp: *const timespec) usize { + return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +pub fn gettimeofday(tv: *timeval, tz: *timezone) usize { + return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz)); +} + +pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize { + return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz)); +} + +pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { + return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); +} + +pub fn setuid(uid: u32) usize { + return syscall1(SYS_setuid, uid); +} + +pub fn setgid(gid: u32) usize { + return syscall1(SYS_setgid, gid); +} + +pub fn setreuid(ruid: u32, euid: u32) usize { + return syscall2(SYS_setreuid, ruid, euid); +} + +pub fn setregid(rgid: u32, egid: u32) usize { + return syscall2(SYS_setregid, rgid, egid); +} + +pub fn getuid() u32 { + return u32(syscall0(SYS_getuid)); +} + +pub fn getgid() u32 { + return u32(syscall0(SYS_getgid)); +} + +pub fn geteuid() u32 { + return u32(syscall0(SYS_geteuid)); +} + +pub fn getegid() u32 { + return u32(syscall0(SYS_getegid)); +} + +pub fn seteuid(euid: u32) usize { + return syscall1(SYS_seteuid, euid); +} + +pub fn setegid(egid: u32) usize { + return syscall1(SYS_setegid, egid); +} + +pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize { + return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); +} + +pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize { + return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); +} + +pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize { + return syscall3(SYS_setresuid, ruid, euid, suid); +} + +pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize { + return syscall3(SYS_setresgid, rgid, egid, sgid); +} + +pub fn getgroups(size: usize, list: *u32) usize { + return syscall2(SYS_getgroups, size, @ptrToInt(list)); +} + +pub fn setgroups(size: usize, list: *const u32) usize { + return syscall2(SYS_setgroups, size, @ptrToInt(list)); +} + +pub fn getpid() i32 { + return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid))); +} + +pub fn gettid() i32 { + return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid))); +} + +pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { + return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8); +} + +pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize { + assert(sig >= 1); + assert(sig != SIGKILL); + assert(sig != SIGSTOP); + var ksa = k_sigaction{ + .handler = act.handler, + .flags = act.flags | SA_RESTORER, + .mask = undefined, + .restorer = @ptrCast(extern fn () void, restore_rt), + }; + var ksa_old: k_sigaction = undefined; + @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), 8); + const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); + const err = getErrno(result); + if (err != 0) { + return result; + } + if (oact) |old| { + old.handler = ksa_old.handler; + old.flags = @truncate(u32, ksa_old.flags); + @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); + } + return 0; +} + +fn blockAllSignals(set: *sigset_t) void { + _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8); +} + +fn blockAppSignals(set: *sigset_t) void { + _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8); +} + +fn restoreSignals(set: *sigset_t) void { + _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8); +} + +pub fn sigaddset(set: *sigset_t, sig: u6) void { + const s = sig - 1; + (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1)); +} + +pub fn sigismember(set: *const sigset_t, sig: u6) bool { + const s = sig - 1; + return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0; +} + +pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { + return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { + return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { + return syscall3(SYS_socket, domain, socket_type, protocol); +} + +pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize { + return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen)); +} + +pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize { + return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); +} + +pub fn sendmsg(fd: i32, msg: *msghdr_const, flags: u32) usize { + return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); +} + +pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { + if (@typeInfo(usize).Int.bits > @typeInfo(@typeOf(mmsghdr(undefined).msg_len)).Int.bits) { + // workaround kernel brokenness: + // if adding up all iov_len overflows a i32 then split into multiple calls + // see https://www.openwall.com/lists/musl/2014/06/07/5 + const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel + var next_unsent: usize = 0; + for (msgvec[0..kvlen]) |*msg, i| { + var size: i32 = 0; + const msg_iovlen = @intCast(usize, msg.msg_hdr.msg_iovlen); // kernel side this is treated as unsigned + for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov, j| { + if (iov.iov_len > std.math.maxInt(i32) or @addWithOverflow(i32, size, @intCast(i32, iov.iov_len), &size)) { + // batch-send all messages up to the current message + if (next_unsent < i) { + const batch_size = i - next_unsent; + const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); + if (getErrno(r) != 0) return next_unsent; + if (r < batch_size) return next_unsent + r; + } + // send current message as own packet + const r = sendmsg(fd, &msg.msg_hdr, flags); + if (getErrno(r) != 0) return r; + // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe. + msg.msg_len = @intCast(u32, r); + next_unsent = i + 1; + break; + } + } + } + if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG_EOR) + const batch_size = kvlen - next_unsent; + const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); + if (getErrno(r) != 0) return r; + return next_unsent + r; + } + return kvlen; + } + return syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(msgvec), vlen, flags); +} + +pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { + return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len); +} + +pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { + return syscall3(SYS_recvmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); +} + +pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize { + return syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); +} + +pub fn shutdown(fd: i32, how: i32) usize { + return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how))); +} + +pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { + return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len)); +} + +pub fn listen(fd: i32, backlog: u32) usize { + return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog); +} + +pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize { + return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen)); +} + +pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { + return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0])); +} + +pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { + return accept4(fd, addr, len, 0); +} + +pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize { + return syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags); +} + +pub fn fstat(fd: i32, stat_buf: *Stat) usize { + return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize { + return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize { + return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize { + return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn listxattr(path: [*]const u8, list: [*]u8, size: usize) usize { + return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn llistxattr(path: [*]const u8, list: [*]u8, size: usize) usize { + return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size); +} + +pub fn flistxattr(fd: usize, list: [*]u8, size: usize) usize { + return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn getxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { + return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lgetxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { + return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fgetxattr(fd: usize, name: [*]const u8, value: [*]u8, size: usize) usize { + return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn setxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { + return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lsetxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { + return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fsetxattr(fd: usize, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { + return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn removexattr(path: [*]const u8, name: [*]const u8) usize { + return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lremovexattr(path: [*]const u8, name: [*]const u8) usize { + return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fremovexattr(fd: usize, name: [*]const u8) usize { + return syscall2(SYS_fremovexattr, fd, @ptrToInt(name)); +} + +pub fn sched_getaffinity(pid: i32, size: usize, set: *cpu_set_t) usize { + const rc = syscall3(SYS_sched_getaffinity, @bitCast(usize, isize(pid)), size, @ptrToInt(set)); + if (@bitCast(isize, rc) < 0) return rc; + if (rc < size) @memset(@ptrCast([*]u8, set) + rc, 0, size - rc); + return 0; +} + +pub fn epoll_create() usize { + return epoll_create1(0); +} + +pub fn epoll_create1(flags: usize) usize { + return syscall1(SYS_epoll_create1, flags); +} + +pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize { + return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev)); +} + +pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { + return syscall4( + SYS_epoll_wait, + @bitCast(usize, isize(epoll_fd)), + @ptrToInt(events), + maxevents, + @bitCast(usize, isize(timeout)), + ); +} + +pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { + return syscall6( + SYS_epoll_pwait, + @bitCast(usize, isize(epoll_fd)), + @ptrToInt(events), + @intCast(usize, maxevents), + @bitCast(usize, isize(timeout)), + @ptrToInt(sigmask), + @sizeOf(sigset_t), + ); +} + +pub fn eventfd(count: u32, flags: u32) usize { + return syscall2(SYS_eventfd2, count, flags); +} + +pub fn timerfd_create(clockid: i32, flags: u32) usize { + return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags); +} + +pub const itimerspec = extern struct { + it_interval: timespec, + it_value: timespec, +}; + +pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize { + return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value)); +} + +pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize { + return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value)); +} + +pub fn unshare(flags: usize) usize { + return syscall1(SYS_unshare, flags); +} + +pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize { + return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap)); +} + +pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize { + return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap)); +} + +// XXX: This should be weak +extern const __ehdr_start: elf.Ehdr = undefined; + +pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize { + if (builtin.link_libc) { + return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data)); + } + + const elf_base = @ptrToInt(&__ehdr_start); + const n_phdr = __ehdr_start.e_phnum; + const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr]; + + var it = dl.linkmap_iterator(phdrs) catch return 0; + + // The executable has no dynamic link segment, create a single entry for + // the whole ELF image + if (it.end()) { + var info = dl_phdr_info{ + .dlpi_addr = elf_base, + .dlpi_name = c"/proc/self/exe", + .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff), + .dlpi_phnum = __ehdr_start.e_phnum, + }; + + return callback(&info, @sizeOf(dl_phdr_info), data); + } + + // Last return value from the callback function + var last_r: isize = 0; + while (it.next()) |entry| { + var dlpi_phdr: usize = undefined; + var dlpi_phnum: u16 = undefined; + + if (entry.l_addr != 0) { + const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr); + dlpi_phdr = entry.l_addr + elf_header.e_phoff; + dlpi_phnum = elf_header.e_phnum; + } else { + // This is the running ELF image + dlpi_phdr = elf_base + __ehdr_start.e_phoff; + dlpi_phnum = __ehdr_start.e_phnum; + } + + var info = dl_phdr_info{ + .dlpi_addr = entry.l_addr, + .dlpi_name = entry.l_name, + .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr), + .dlpi_phnum = dlpi_phnum, + }; + + last_r = callback(&info, @sizeOf(dl_phdr_info), data); + if (last_r != 0) break; + } + + return last_r; +} test "" { if (is_the_target) { diff --git a/std/os/linux/sys.zig b/std/os/linux/sys.zig deleted file mode 100644 index 272124f06c..0000000000 --- a/std/os/linux/sys.zig +++ /dev/null @@ -1,859 +0,0 @@ -// This file provides the system interface functions for Linux matching those -// that are provided by libc, whether or not libc is linked. The following -// abstractions are made: -// * Work around kernel bugs and limitations. For example, see sendmmsg. -// * Implement all the syscalls in the same way that libc functions will -// provide `rename` when only the `renameat` syscall exists. -// * Does not support POSIX thread cancellation. -const std = @import("../../std.zig"); -const builtin = @import("builtin"); -const assert = std.debug.assert; -const maxInt = std.math.maxInt; -const elf = std.elf; -const vdso = @import("vdso.zig"); -const dl = @import("../../dynamic_library.zig"); -pub use switch (builtin.arch) { - .x86_64 => @import("x86_64.zig"), - .aarch64 => @import("arm64.zig"), - else => struct {}, -}; -pub use @import("../bits.zig"); - -/// See `std.os.posix.getauxval`. -pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; - -/// Get the errno from a syscall return value, or 0 for no error. -pub fn getErrno(r: usize) u12 { - const signed_r = @bitCast(isize, r); - return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0; -} - -pub fn dup2(old: i32, new: i32) usize { - if (@hasDecl(@This(), "SYS_dup2")) { - return syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new))); - } else { - if (old == new) { - if (std.debug.runtime_safety) { - const rc = syscall2(SYS_fcntl, @bitCast(usize, isize(old)), F_GETFD); - if (@bitCast(isize, rc) < 0) return rc; - } - return @intCast(usize, old); - } else { - return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), 0); - } - } -} - -pub fn dup3(old: i32, new: i32, flags: u32) usize { - return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn chdir(path: [*]const u8) usize { - return syscall1(SYS_chdir, @ptrToInt(path)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn chroot(path: [*]const u8) usize { - return syscall1(SYS_chroot, @ptrToInt(path)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { - return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); -} - -pub fn fork() usize { - if (@hasDecl(@This(), "SYS_fork")) { - return syscall0(SYS_fork); - } else { - return syscall2(SYS_clone, SIGCHLD, 0); - } -} - -/// This must be inline, and inline call the syscall function, because if the -/// child does a return it will clobber the parent's stack. -/// It is advised to avoid this function and use clone instead, because -/// the compiler is not aware of how vfork affects control flow and you may -/// see different results in optimized builds. -pub inline fn vfork() usize { - return @inlineCall(syscall0, SYS_vfork); -} - -pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize { - return syscall4(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout)); -} - -pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize { - return syscall3(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val)); -} - -pub fn getcwd(buf: [*]u8, size: usize) usize { - return syscall2(SYS_getcwd, @ptrToInt(buf), size); -} - -pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { - return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); -} - -pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize { - return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); -} - -pub fn inotify_init1(flags: u32) usize { - return syscall1(SYS_inotify_init1, flags); -} - -pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize { - return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask); -} - -pub fn inotify_rm_watch(fd: i32, wd: i32) usize { - return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd))); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - if (@hasDecl(@This(), "SYS_readlink")) { - return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); - } else { - return syscall4(SYS_readlinkat, AT_FDCWD, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn mkdir(path: [*]const u8, mode: u32) usize { - if (@hasDecl(@This(), "SYS_mkdir")) { - return syscall2(SYS_mkdir, @ptrToInt(path), mode); - } else { - return syscall3(SYS_mkdirat, AT_FDCWD, @ptrToInt(path), mode); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize { - return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize { - return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn umount(special: [*]const u8) usize { - return syscall2(SYS_umount2, @ptrToInt(special), 0); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn umount2(special: [*]const u8, flags: u32) usize { - return syscall2(SYS_umount2, @ptrToInt(special), flags); -} - -pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { - return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); -} - -pub fn mprotect(address: usize, length: usize, protection: usize) usize { - return syscall3(SYS_mprotect, address, length, protection); -} - -pub fn munmap(address: usize, length: usize) usize { - return syscall2(SYS_munmap, address, length); -} - -pub fn read(fd: i32, buf: [*]u8, count: usize) usize { - return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); -} - -pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { - return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); -} - -pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { - return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); -} - -pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { - return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); -} - -pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { - return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn rmdir(path: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_rmdir")) { - return syscall1(SYS_rmdir, @ptrToInt(path)); - } else { - return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), AT_REMOVEDIR); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_symlink")) { - return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); - } else { - return syscall3(SYS_symlinkat, @ptrToInt(existing), AT_FDCWD, @ptrToInt(new)); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize { - return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { - return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn access(path: [*]const u8, mode: u32) usize { - return syscall2(SYS_access, @ptrToInt(path), mode); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize { - return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); -} - -pub fn pipe(fd: *[2]i32) usize { - if (@hasDecl(@This(), "SYS_pipe")) { - return syscall1(SYS_pipe, @ptrToInt(fd)); - } else { - return syscall2(SYS_pipe2, @ptrToInt(fd), 0); - } -} - -pub fn pipe2(fd: *[2]i32, flags: u32) usize { - return syscall2(SYS_pipe2, @ptrToInt(fd), flags); -} - -pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { - return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); -} - -pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { - return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn rename(old: [*]const u8, new: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_rename")) { - return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new)); - } else if (@hasDecl(@This(), "SYS_renameat")) { - return syscall4(SYS_renameat, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new)); - } else { - return syscall5(SYS_renameat2, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new), 0); - } -} - -pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_renameat")) { - return syscall4( - SYS_renameat, - @bitCast(usize, isize(oldfd)), - @ptrToInt(old), - @bitCast(usize, isize(newfd)), - @ptrToInt(new), - ); - } else { - return syscall5( - SYS_renameat2, - @bitCast(usize, isize(oldfd)), - @ptrToInt(old), - @bitCast(usize, isize(newfd)), - @ptrToInt(new), - 0, - ); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize { - return syscall5( - SYS_renameat2, - @bitCast(usize, isize(oldfd)), - @ptrToInt(oldpath), - @bitCast(usize, isize(newfd)), - @ptrToInt(newpath), - flags, - ); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn open(path: [*]const u8, flags: u32, perm: usize) usize { - return syscall3(SYS_open, @ptrToInt(path), flags, perm); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn create(path: [*]const u8, perm: usize) usize { - return syscall2(SYS_creat, @ptrToInt(path), perm); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize { - // dirfd could be negative, for example AT_FDCWD is -100 - return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode); -} - -/// See also `clone` (from the arch-specific include) -pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize { - return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls); -} - -/// See also `clone` (from the arch-specific include) -pub fn clone2(flags: u32, child_stack_ptr: usize) usize { - return syscall2(SYS_clone, flags, child_stack_ptr); -} - -pub fn close(fd: i32) usize { - return syscall1(SYS_close, @bitCast(usize, isize(fd))); -} - -/// Can only be called on 32 bit systems. For 64 bit see `lseek`. -pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize { - return syscall5( - SYS__llseek, - @bitCast(usize, isize(fd)), - @truncate(usize, offset >> 32), - @truncate(usize, offset), - @ptrToInt(result), - whence, - ); -} - -/// Can only be called on 64 bit systems. For 32 bit see `llseek`. -pub fn lseek(fd: i32, offset: i64, whence: usize) usize { - return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), whence); -} - -pub fn exit(status: i32) noreturn { - _ = syscall1(SYS_exit, @bitCast(usize, isize(status))); - unreachable; -} - -pub fn exit_group(status: i32) noreturn { - _ = syscall1(SYS_exit_group, @bitCast(usize, isize(status))); - unreachable; -} - -pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize { - return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags); -} - -pub fn kill(pid: i32, sig: i32) usize { - return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig))); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn unlink(path: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_unlink")) { - return syscall1(SYS_unlink, @ptrToInt(path)); - } else { - return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), 0); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { - return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags); -} - -pub fn waitpid(pid: i32, status: *i32, options: i32) usize { - return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); -} - -var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime); - -// We must follow the C calling convention when we call into the VDSO -const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize; - -pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { - if (VDSO_CGT_SYM.len != 0) { - const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered); - if (ptr) |fn_ptr| { - const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); - const rc = f(clk_id, tp); - switch (rc) { - 0, @bitCast(usize, isize(-EINVAL)) => return rc, - else => {}, - } - } - } - return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); -} - -extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize { - const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM)); - // Note that we may not have a VDSO at all, update the stub address anyway - // so that clock_gettime will fall back on the good old (and slow) syscall - _ = @cmpxchgStrong(?*const c_void, &vdso_clock_gettime, &init_vdso_clock_gettime, ptr, .Monotonic, .Monotonic); - // Call into the VDSO if available - if (ptr) |fn_ptr| { - const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); - return f(clk, ts); - } - return @bitCast(usize, isize(-ENOSYS)); -} - -pub fn clock_getres(clk_id: i32, tp: *timespec) usize { - return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); -} - -pub fn clock_settime(clk_id: i32, tp: *const timespec) usize { - return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); -} - -pub fn gettimeofday(tv: *timeval, tz: *timezone) usize { - return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz)); -} - -pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize { - return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz)); -} - -pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { - return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); -} - -pub fn setuid(uid: u32) usize { - return syscall1(SYS_setuid, uid); -} - -pub fn setgid(gid: u32) usize { - return syscall1(SYS_setgid, gid); -} - -pub fn setreuid(ruid: u32, euid: u32) usize { - return syscall2(SYS_setreuid, ruid, euid); -} - -pub fn setregid(rgid: u32, egid: u32) usize { - return syscall2(SYS_setregid, rgid, egid); -} - -pub fn getuid() u32 { - return u32(syscall0(SYS_getuid)); -} - -pub fn getgid() u32 { - return u32(syscall0(SYS_getgid)); -} - -pub fn geteuid() u32 { - return u32(syscall0(SYS_geteuid)); -} - -pub fn getegid() u32 { - return u32(syscall0(SYS_getegid)); -} - -pub fn seteuid(euid: u32) usize { - return syscall1(SYS_seteuid, euid); -} - -pub fn setegid(egid: u32) usize { - return syscall1(SYS_setegid, egid); -} - -pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize { - return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); -} - -pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize { - return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); -} - -pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize { - return syscall3(SYS_setresuid, ruid, euid, suid); -} - -pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize { - return syscall3(SYS_setresgid, rgid, egid, sgid); -} - -pub fn getgroups(size: usize, list: *u32) usize { - return syscall2(SYS_getgroups, size, @ptrToInt(list)); -} - -pub fn setgroups(size: usize, list: *const u32) usize { - return syscall2(SYS_setgroups, size, @ptrToInt(list)); -} - -pub fn getpid() i32 { - return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid))); -} - -pub fn gettid() i32 { - return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid))); -} - -pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { - return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8); -} - -pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize { - assert(sig >= 1); - assert(sig != SIGKILL); - assert(sig != SIGSTOP); - var ksa = k_sigaction{ - .handler = act.handler, - .flags = act.flags | SA_RESTORER, - .mask = undefined, - .restorer = @ptrCast(extern fn () void, restore_rt), - }; - var ksa_old: k_sigaction = undefined; - @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), 8); - const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); - const err = getErrno(result); - if (err != 0) { - return result; - } - if (oact) |old| { - old.handler = ksa_old.handler; - old.flags = @truncate(u32, ksa_old.flags); - @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); - } - return 0; -} - -fn blockAllSignals(set: *sigset_t) void { - _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8); -} - -fn blockAppSignals(set: *sigset_t) void { - _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8); -} - -fn restoreSignals(set: *sigset_t) void { - _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8); -} - -pub fn sigaddset(set: *sigset_t, sig: u6) void { - const s = sig - 1; - (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1)); -} - -pub fn sigismember(set: *const sigset_t, sig: u6) bool { - const s = sig - 1; - return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0; -} - -pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { - return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { - return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { - return syscall3(SYS_socket, domain, socket_type, protocol); -} - -pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize { - return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen)); -} - -pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize { - return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); -} - -pub fn sendmsg(fd: i32, msg: *msghdr_const, flags: u32) usize { - return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); -} - -pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { - if (@typeInfo(usize).Int.bits > @typeInfo(@typeOf(mmsghdr(undefined).msg_len)).Int.bits) { - // workaround kernel brokenness: - // if adding up all iov_len overflows a i32 then split into multiple calls - // see https://www.openwall.com/lists/musl/2014/06/07/5 - const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel - var next_unsent: usize = 0; - for (msgvec[0..kvlen]) |*msg, i| { - var size: i32 = 0; - const msg_iovlen = @intCast(usize, msg.msg_hdr.msg_iovlen); // kernel side this is treated as unsigned - for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov, j| { - if (iov.iov_len > std.math.maxInt(i32) or @addWithOverflow(i32, size, @intCast(i32, iov.iov_len), &size)) { - // batch-send all messages up to the current message - if (next_unsent < i) { - const batch_size = i - next_unsent; - const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); - if (getErrno(r) != 0) return next_unsent; - if (r < batch_size) return next_unsent + r; - } - // send current message as own packet - const r = sendmsg(fd, &msg.msg_hdr, flags); - if (getErrno(r) != 0) return r; - // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe. - msg.msg_len = @intCast(u32, r); - next_unsent = i + 1; - break; - } - } - } - if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG_EOR) - const batch_size = kvlen - next_unsent; - const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); - if (getErrno(r) != 0) return r; - return next_unsent + r; - } - return kvlen; - } - return syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(msgvec), vlen, flags); -} - -pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { - return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len); -} - -pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { - return syscall3(SYS_recvmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); -} - -pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize { - return syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); -} - -pub fn shutdown(fd: i32, how: i32) usize { - return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how))); -} - -pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { - return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len)); -} - -pub fn listen(fd: i32, backlog: u32) usize { - return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog); -} - -pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize { - return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen)); -} - -pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { - return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0])); -} - -pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { - return accept4(fd, addr, len, 0); -} - -pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize { - return syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags); -} - -pub fn fstat(fd: i32, stat_buf: *Stat) usize { - return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize { - return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize { - return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize { - return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn listxattr(path: [*]const u8, list: [*]u8, size: usize) usize { - return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn llistxattr(path: [*]const u8, list: [*]u8, size: usize) usize { - return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size); -} - -pub fn flistxattr(fd: usize, list: [*]u8, size: usize) usize { - return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn getxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { - return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lgetxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { - return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fgetxattr(fd: usize, name: [*]const u8, value: [*]u8, size: usize) usize { - return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn setxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { - return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lsetxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { - return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fsetxattr(fd: usize, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { - return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn removexattr(path: [*]const u8, name: [*]const u8) usize { - return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lremovexattr(path: [*]const u8, name: [*]const u8) usize { - return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fremovexattr(fd: usize, name: [*]const u8) usize { - return syscall2(SYS_fremovexattr, fd, @ptrToInt(name)); -} - -pub fn sched_getaffinity(pid: i32, set: []usize) usize { - return syscall3(SYS_sched_getaffinity, @bitCast(usize, isize(pid)), set.len * @sizeOf(usize), @ptrToInt(set.ptr)); -} - -pub fn epoll_create() usize { - return epoll_create1(0); -} - -pub fn epoll_create1(flags: usize) usize { - return syscall1(SYS_epoll_create1, flags); -} - -pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize { - return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev)); -} - -pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { - return syscall4( - SYS_epoll_wait, - @bitCast(usize, isize(epoll_fd)), - @ptrToInt(events), - maxevents, - @bitCast(usize, isize(timeout)), - ); -} - -pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { - return syscall6( - SYS_epoll_pwait, - @bitCast(usize, isize(epoll_fd)), - @ptrToInt(events), - @intCast(usize, maxevents), - @bitCast(usize, isize(timeout)), - @ptrToInt(sigmask), - @sizeOf(sigset_t), - ); -} - -pub fn eventfd(count: u32, flags: u32) usize { - return syscall2(SYS_eventfd2, count, flags); -} - -pub fn timerfd_create(clockid: i32, flags: u32) usize { - return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags); -} - -pub const itimerspec = extern struct { - it_interval: timespec, - it_value: timespec, -}; - -pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize { - return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value)); -} - -pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize { - return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value)); -} - -pub fn unshare(flags: usize) usize { - return syscall1(SYS_unshare, flags); -} - -pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize { - return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap)); -} - -pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize { - return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap)); -} - -// XXX: This should be weak -extern const __ehdr_start: elf.Ehdr = undefined; - -pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize { - if (builtin.link_libc) { - return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data)); - } - - const elf_base = @ptrToInt(&__ehdr_start); - const n_phdr = __ehdr_start.e_phnum; - const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr]; - - var it = dl.linkmap_iterator(phdrs) catch return 0; - - // The executable has no dynamic link segment, create a single entry for - // the whole ELF image - if (it.end()) { - var info = dl_phdr_info{ - .dlpi_addr = elf_base, - .dlpi_name = c"/proc/self/exe", - .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff), - .dlpi_phnum = __ehdr_start.e_phnum, - }; - - return callback(&info, @sizeOf(dl_phdr_info), data); - } - - // Last return value from the callback function - var last_r: isize = 0; - while (it.next()) |entry| { - var dlpi_phdr: usize = undefined; - var dlpi_phnum: u16 = undefined; - - if (entry.l_addr != 0) { - const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr); - dlpi_phdr = entry.l_addr + elf_header.e_phoff; - dlpi_phnum = elf_header.e_phnum; - } else { - // This is the running ELF image - dlpi_phdr = elf_base + __ehdr_start.e_phoff; - dlpi_phnum = __ehdr_start.e_phnum; - } - - var info = dl_phdr_info{ - .dlpi_addr = entry.l_addr, - .dlpi_name = entry.l_name, - .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr), - .dlpi_phnum = dlpi_phnum, - }; - - last_r = callback(&info, @sizeOf(dl_phdr_info), data); - if (last_r != 0) break; - } - - return last_r; -} diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index e040a45bc0..f06ed144b9 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -1,6 +1,6 @@ const std = @import("std"); +const os = std.os; const mem = std.mem; -const posix = std.os.posix; const elf = std.elf; const builtin = @import("builtin"); const assert = std.debug.assert; @@ -237,9 +237,14 @@ pub fn allocateTLS(size: usize) usize { return @ptrToInt(&main_thread_tls_buffer); } - const addr = posix.mmap(null, size, posix.PROT_READ | posix.PROT_WRITE, posix.MAP_PRIVATE | posix.MAP_ANONYMOUS, -1, 0); - - if (posix.getErrno(addr) != 0) @panic("out of memory"); + const addr = os.mmap( + null, + size, + os.PROT_READ | os.PROT_WRITE, + os.MAP_PRIVATE | os.MAP_ANONYMOUS, + -1, + 0, + ) catch @panic("out of memory"); return addr; } diff --git a/std/os/linux/x86_64.zig b/std/os/linux/x86_64.zig index 9909b6ee3f..fa866cff4c 100644 --- a/std/os/linux/x86_64.zig +++ b/std/os/linux/x86_64.zig @@ -1,4 +1,4 @@ -use @import("posix/x86_64.zig"); +use @import("../bits.zig"); pub fn syscall0(number: usize) usize { return asm volatile ("syscall" diff --git a/std/os/test.zig b/std/os/test.zig index 54371e94ab..d4bdb06278 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -5,6 +5,7 @@ const expect = std.testing.expect; const io = std.io; const mem = std.mem; const File = std.fs.File; +const Thread = std.Thread; const a = std.debug.global_allocator; @@ -37,25 +38,25 @@ test "access file" { try os.deleteTree(a, "os_test_tmp"); } -fn testThreadIdFn(thread_id: *os.Thread.Id) void { - thread_id.* = os.Thread.getCurrentId(); +fn testThreadIdFn(thread_id: *Thread.Id) void { + thread_id.* = Thread.getCurrentId(); } -test "std.os.Thread.getCurrentId" { +test "std.Thread.getCurrentId" { if (builtin.single_threaded) return error.SkipZigTest; - var thread_current_id: os.Thread.Id = undefined; - const thread = try os.spawnThread(&thread_current_id, testThreadIdFn); + var thread_current_id: Thread.Id = undefined; + const thread = try Thread.spawn(&thread_current_id, testThreadIdFn); const thread_id = thread.handle(); thread.wait(); - if (os.Thread.use_pthreads) { + if (Thread.use_pthreads) { expect(thread_current_id == thread_id); } else { switch (builtin.os) { - builtin.Os.windows => expect(os.Thread.getCurrentId() != thread_current_id), + builtin.Os.windows => expect(Thread.getCurrentId() != thread_current_id), else => { // If the thread completes very quickly, then thread_id can be 0. See the - // documentation comments for `std.os.Thread.handle`. + // documentation comments for `std.Thread.handle`. expect(thread_id == 0 or thread_current_id == thread_id); }, } @@ -67,10 +68,10 @@ test "spawn threads" { var shared_ctx: i32 = 1; - const thread1 = try std.os.spawnThread({}, start1); - const thread2 = try std.os.spawnThread(&shared_ctx, start2); - const thread3 = try std.os.spawnThread(&shared_ctx, start2); - const thread4 = try std.os.spawnThread(&shared_ctx, start2); + const thread1 = try Thread.spawn({}, start1); + const thread2 = try Thread.spawn(&shared_ctx, start2); + const thread3 = try Thread.spawn(&shared_ctx, start2); + const thread4 = try Thread.spawn(&shared_ctx, start2); thread1.wait(); thread2.wait(); @@ -116,8 +117,8 @@ test "AtomicFile" { test "thread local storage" { if (builtin.single_threaded) return error.SkipZigTest; - const thread1 = try std.os.spawnThread({}, testTls); - const thread2 = try std.os.spawnThread({}, testTls); + const thread1 = try Thread.spawn({}, testTls); + const thread2 = try Thread.spawn({}, testTls); testTls({}); thread1.wait(); thread2.wait(); diff --git a/std/os/windows.zig b/std/os/windows.zig index e35d8bbbd2..c18adc66fa 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -591,7 +591,7 @@ pub fn CreateFileW( ERROR.ACCESS_DENIED => return error.AccessDenied, ERROR.PIPE_BUSY => return error.PipeBusy, ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong, - else => |err| return unexpectedErrorWindows(err), + else => |err| return unexpectedError(err), } } @@ -1089,6 +1089,20 @@ pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError } } +pub const VirtualAllocError = error{Unexpected}; + +pub fn VirtualAlloc(addr: ?LPVOID, size: usize, alloc_type: DWORD, flProtect: DWORD) VirtualAllocError!LPVOID { + return kernel32.VirtualAlloc(addr, size, alloc_type, flProtect) orelse { + switch (kernel32.GetLastError()) { + else => |err| return unexpectedError(err), + } + }; +} + +pub fn VirtualFree(lpAddress: ?LPVOID, dwSize: usize, dwFreeType: DWORD) void { + assert(kernel32.VirtualFree(lpAddress, dwSize, dwFreeType) != 0); +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } diff --git a/std/process.zig b/std/process.zig index f2f1dbf4fd..1e29170e88 100644 --- a/std/process.zig +++ b/std/process.zig @@ -1,13 +1,13 @@ const std = @import("std.zig"); -const posix = std.os.posix; +const os = std.os; const BufMap = std.BufMap; const mem = std.mem; const Allocator = mem.Allocator; const assert = std.debug.assert; const testing = std.testing; -pub const abort = posix.abort; -pub const exit = posix.exit; +pub const abort = os.abort; +pub const exit = os.exit; /// Caller must free result when done. /// TODO make this go through libc when we have it @@ -42,13 +42,13 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { try result.setMove(key, value); } - } else if (builtin.os == Os.wasi) { + } else if (builtin.os == .wasi) { var environ_count: usize = undefined; var environ_buf_size: usize = undefined; - const environ_sizes_get_ret = std.os.wasi.environ_sizes_get(&environ_count, &environ_buf_size); + const environ_sizes_get_ret = os.wasi.environ_sizes_get(&environ_count, &environ_buf_size); if (environ_sizes_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(environ_sizes_get_ret); + return os.unexpectedErrno(environ_sizes_get_ret); } // TODO: Verify that the documentation is incorrect @@ -58,9 +58,9 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { var environ_buf = try std.heap.wasm_allocator.alloc(u8, environ_buf_size); defer allocator.free(environ_buf); - const environ_get_ret = std.os.wasi.environ_get(environ.ptr, environ_buf.ptr); + const environ_get_ret = os.wasi.environ_get(environ.ptr, environ_buf.ptr); if (environ_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(environ_get_ret); + return os.unexpectedErrno(environ_get_ret); } for (environ) |env| { @@ -74,7 +74,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { } return result; } else { - for (posix.environ) |ptr| { + for (os.environ) |ptr| { var line_i: usize = 0; while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} const key = ptr[0..line_i]; @@ -331,12 +331,12 @@ pub const ArgIteratorWindows = struct { }; pub const ArgIterator = struct { - const InnerType = if (builtin.os == Os.windows) ArgIteratorWindows else ArgIteratorPosix; + const InnerType = if (builtin.os == .windows) ArgIteratorWindows else ArgIteratorPosix; inner: InnerType, pub fn init() ArgIterator { - if (builtin.os == Os.wasi) { + if (builtin.os == .wasi) { // TODO: Figure out a compatible interface accomodating WASI @compileError("ArgIterator is not yet supported in WASI. Use argsAlloc and argsFree instead."); } @@ -348,7 +348,7 @@ pub const ArgIterator = struct { /// You must free the returned memory when done. pub fn next(self: *ArgIterator, allocator: *Allocator) ?(NextError![]u8) { - if (builtin.os == Os.windows) { + if (builtin.os == .windows) { return self.inner.next(allocator); } else { return mem.dupe(allocator, u8, self.inner.next() orelse return null); @@ -373,13 +373,13 @@ pub fn args() ArgIterator { /// Caller must call argsFree on result. pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { - if (builtin.os == Os.wasi) { + if (builtin.os == .wasi) { var count: usize = undefined; var buf_size: usize = undefined; const args_sizes_get_ret = os.wasi.args_sizes_get(&count, &buf_size); if (args_sizes_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(args_sizes_get_ret); + return os.unexpectedErrno(args_sizes_get_ret); } var argv = try allocator.alloc([*]u8, count); @@ -388,7 +388,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { var argv_buf = try allocator.alloc(u8, buf_size); const args_get_ret = os.wasi.args_get(argv.ptr, argv_buf.ptr); if (args_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(args_get_ret); + return os.unexpectedErrno(args_get_ret); } var result_slice = try allocator.alloc([]u8, count); @@ -438,7 +438,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { } pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void { - if (builtin.os == Os.wasi) { + if (builtin.os == .wasi) { const last_item = args_alloc[args_alloc.len - 1]; const last_byte_addr = @ptrToInt(last_item.ptr) + last_item.len + 1; // null terminated const first_item_ptr = args_alloc[0].ptr; @@ -491,7 +491,7 @@ pub const UserInfo = struct { /// POSIX function which gets a uid from username. pub fn getUserInfo(name: []const u8) !UserInfo { return switch (builtin.os) { - .linux, .macosx, .ios, .freebsd, .netbsd => posixGetUserInfo(name), + .linux, .macosx, .watchos, .tvos, .ios, .freebsd, .netbsd => posixGetUserInfo(name), else => @compileError("Unsupported OS"), }; } diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index c6630a594c..86228acb9f 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -92,14 +92,14 @@ fn posixCallMainAndExit() noreturn { } } - std.os.posix.exit(callMainWithArgs(argc, argv, envp)); + std.os.exit(callMainWithArgs(argc, argv, envp)); } // This is marked inline because for some reason LLVM in release mode fails to inline it, // and we want fewer call frames in stack traces. inline fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { std.os.ArgIteratorPosix.raw = argv[0..argc]; - std.os.posix.environ = envp; + std.os.environ = envp; return callMain(); } diff --git a/std/statically_initialized_mutex.zig b/std/statically_initialized_mutex.zig index cfcaf036d3..9c59fccefe 100644 --- a/std/statically_initialized_mutex.zig +++ b/std/statically_initialized_mutex.zig @@ -96,9 +96,9 @@ test "std.StaticallyInitializedMutex" { expect(context.data == TestContext.incr_count); } else { const thread_count = 10; - var threads: [thread_count]*std.os.Thread = undefined; + var threads: [thread_count]*std.Thread = undefined; for (threads) |*t| { - t.* = try std.os.spawnThread(&context, TestContext.worker); + t.* = try std.Thread.spawn(&context, TestContext.worker); } for (threads) |t| t.wait(); diff --git a/std/thread.zig b/std/thread.zig index 729d4aff60..eac6b73f7d 100644 --- a/std/thread.zig +++ b/std/thread.zig @@ -1,6 +1,8 @@ const builtin = @import("builtin"); const std = @import("std.zig"); +const os = std.os; const windows = std.os.windows; +const c = std.c; pub const Thread = struct { data: Data, @@ -13,8 +15,8 @@ pub const Thread = struct { pub const Handle = if (use_pthreads) c.pthread_t else switch (builtin.os) { - builtin.Os.linux => i32, - builtin.Os.windows => windows.HANDLE, + .linux => i32, + .windows => windows.HANDLE, else => @compileError("Unsupported OS"), }; @@ -22,7 +24,7 @@ pub const Thread = struct { /// May be an integer or pointer depending on the platform. /// On Linux and POSIX, this is the same as Handle. pub const Id = switch (builtin.os) { - builtin.Os.windows => windows.DWORD, + .windows => windows.DWORD, else => Handle, }; @@ -33,12 +35,12 @@ pub const Thread = struct { mmap_len: usize, } else switch (builtin.os) { - builtin.Os.linux => struct { + .linux => struct { handle: Thread.Handle, mmap_addr: usize, mmap_len: usize, }, - builtin.Os.windows => struct { + .windows => struct { handle: Thread.Handle, alloc_start: *c_void, heap_handle: windows.HANDLE, @@ -54,8 +56,8 @@ pub const Thread = struct { return c.pthread_self(); } else return switch (builtin.os) { - builtin.Os.linux => linux.gettid(), - builtin.Os.windows => windows.GetCurrentThreadId(), + .linux => linux.gettid(), + .windows => windows.GetCurrentThreadId(), else => @compileError("Unsupported OS"), }; } @@ -75,28 +77,28 @@ pub const Thread = struct { const err = c.pthread_join(self.data.handle, null); switch (err) { 0 => {}, - posix.EINVAL => unreachable, - posix.ESRCH => unreachable, - posix.EDEADLK => unreachable, + os.EINVAL => unreachable, + os.ESRCH => unreachable, + os.EDEADLK => unreachable, else => unreachable, } - assert(posix.munmap(self.data.mmap_addr, self.data.mmap_len) == 0); + os.munmap(self.data.mmap_addr, self.data.mmap_len); } else switch (builtin.os) { - builtin.Os.linux => { + .linux => { while (true) { const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst); if (pid_value == 0) break; const rc = linux.futex_wait(&self.data.handle, linux.FUTEX_WAIT, pid_value, null); switch (linux.getErrno(rc)) { 0 => continue, - posix.EINTR => continue, - posix.EAGAIN => continue, + os.EINTR => continue, + os.EAGAIN => continue, else => unreachable, } } - assert(posix.munmap(self.data.mmap_addr, self.data.mmap_len) == 0); + os.munmap(self.data.mmap_addr, self.data.mmap_len); }, - builtin.Os.windows => { + .windows => { assert(windows.WaitForSingleObject(self.data.handle, windows.INFINITE) == windows.WAIT_OBJECT_0); assert(windows.CloseHandle(self.data.handle) != 0); assert(windows.HeapFree(self.data.heap_handle, 0, self.data.alloc_start) != 0); @@ -153,10 +155,10 @@ pub const Thread = struct { extern fn threadMain(raw_arg: windows.LPVOID) windows.DWORD { const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), raw_arg)).*; switch (@typeId(@typeOf(startFn).ReturnType)) { - builtin.TypeId.Int => { + .Int => { return startFn(arg); }, - builtin.TypeId.Void => { + .Void => { startFn(arg); return 0; }, @@ -196,10 +198,10 @@ pub const Thread = struct { const arg = if (@sizeOf(Context) == 0) {} else @intToPtr(*const Context, ctx_addr).*; switch (@typeId(@typeOf(startFn).ReturnType)) { - builtin.TypeId.Int => { + .Int => { return startFn(arg); }, - builtin.TypeId.Void => { + .Void => { startFn(arg); return 0; }, @@ -217,7 +219,7 @@ pub const Thread = struct { } }; - const MAP_GROWSDOWN = if (builtin.os == builtin.Os.linux) linux.MAP_GROWSDOWN else 0; + const MAP_GROWSDOWN = if (builtin.os == .linux) linux.MAP_GROWSDOWN else 0; var stack_end_offset: usize = undefined; var thread_start_offset: usize = undefined; @@ -247,9 +249,8 @@ pub const Thread = struct { } break :blk l; }; - const mmap_addr = posix.mmap(null, mmap_len, posix.PROT_READ | posix.PROT_WRITE, posix.MAP_PRIVATE | posix.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); - if (mmap_addr == posix.MAP_FAILED) return error.OutOfMemory; - errdefer assert(posix.munmap(mmap_addr, mmap_len) == 0); + const mmap_addr = try os.mmap(null, mmap_len, os.PROT_READ | os.PROT_WRITE, os.MAP_PRIVATE | os.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); + errdefer os.munmap(mmap_addr, mmap_len); const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(*Thread, mmap_addr + thread_start_offset)); thread_ptr.data.mmap_addr = mmap_addr; @@ -273,31 +274,30 @@ pub const Thread = struct { const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(*c_void, arg)); switch (err) { 0 => return thread_ptr, - posix.EAGAIN => return error.SystemResources, - posix.EPERM => unreachable, - posix.EINVAL => unreachable, - else => return unexpectedErrorPosix(@intCast(usize, err)), + os.EAGAIN => return error.SystemResources, + os.EPERM => unreachable, + os.EINVAL => unreachable, + else => return os.unexpectedErrno(@intCast(usize, err)), } - } else if (builtin.os == builtin.Os.linux) { - var flags: u32 = posix.CLONE_VM | posix.CLONE_FS | posix.CLONE_FILES | posix.CLONE_SIGHAND | - posix.CLONE_THREAD | posix.CLONE_SYSVSEM | posix.CLONE_PARENT_SETTID | posix.CLONE_CHILD_CLEARTID | - posix.CLONE_DETACHED; + } else if (builtin.os == .linux) { + var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND | + os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID | + os.CLONE_DETACHED; var newtls: usize = undefined; if (linux.tls.tls_image) |tls_img| { newtls = linux.tls.copyTLS(mmap_addr + tls_start_offset); - flags |= posix.CLONE_SETTLS; + flags |= os.CLONE_SETTLS; } - const rc = posix.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle); - const err = posix.getErrno(rc); - switch (err) { + const rc = os.linux.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle); + switch (os.errno(rc)) { 0 => return thread_ptr, - posix.EAGAIN => return error.ThreadQuotaExceeded, - posix.EINVAL => unreachable, - posix.ENOMEM => return error.SystemResources, - posix.ENOSPC => unreachable, - posix.EPERM => unreachable, - posix.EUSERS => unreachable, - else => return unexpectedErrorPosix(err), + os.EAGAIN => return error.ThreadQuotaExceeded, + os.EINVAL => unreachable, + os.ENOMEM => return error.SystemResources, + os.ENOSPC => unreachable, + os.EPERM => unreachable, + os.EUSERS => unreachable, + else => |err| return os.unexpectedErrno(err), } } else { @compileError("Unsupported OS"); @@ -310,56 +310,20 @@ pub const Thread = struct { Unexpected, }; - pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize { - switch (builtin.os) { - .macosx, .freebsd, .netbsd => { - var count: c_int = undefined; - var count_len: usize = @sizeOf(c_int); - const name = switch (builtin.os) { - builtin.Os.macosx => c"hw.logicalcpu", - else => c"hw.ncpu", - }; - try posix.sysctlbyname(name, @ptrCast(*c_void, &count), &count_len, null, 0); - return @intCast(usize, count); - }, - .linux => { - const usize_count = 16; - const allocator = std.heap.stackFallback(usize_count * @sizeOf(usize), fallback_allocator).get(); - - var set = try allocator.alloc(usize, usize_count); - defer allocator.free(set); - - while (true) { - const rc = posix.sched_getaffinity(0, set); - const err = posix.getErrno(rc); - switch (err) { - 0 => { - if (rc < set.len * @sizeOf(usize)) { - const result = set[0 .. rc / @sizeOf(usize)]; - var sum: usize = 0; - for (result) |x| { - sum += @popCount(usize, x); - } - return sum; - } else { - set = try allocator.realloc(set, set.len * 2); - continue; - } - }, - posix.EFAULT => unreachable, - posix.EINVAL => unreachable, - posix.EPERM => return CpuCountError.PermissionDenied, - posix.ESRCH => unreachable, - else => return os.unexpectedErrorPosix(err), - } - } - }, - .windows => { - var system_info: windows.SYSTEM_INFO = undefined; - windows.GetSystemInfo(&system_info); - return @intCast(usize, system_info.dwNumberOfProcessors); - }, - else => @compileError("unsupported OS"), + pub fn cpuCount() CpuCountError!usize { + if (os.linux.is_the_target) { + const cpu_set = try os.sched_getaffinity(0); + return os.CPU_COUNT(cpu_set); + } + if (os.windows.is_the_target) { + var system_info: windows.SYSTEM_INFO = undefined; + windows.GetSystemInfo(&system_info); + return @intCast(usize, system_info.dwNumberOfProcessors); } + var count: c_int = undefined; + var count_len: usize = @sizeOf(c_int); + const name = if (os.darwin.is_the_target) c"hw.logicalcpu" else c"hw.ncpu"; + try os.sysctlbyname(name, @ptrCast(*c_void, &count), &count_len, null, 0); + return @intCast(usize, count); } }; -- cgit v1.2.3 From 0c6ab61b228211398841cf11912c7252362009b7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 26 May 2019 23:35:26 -0400 Subject: tests passing on linux --- build.zig | 6 +-- doc/langref.html.in | 4 +- example/cat/main.zig | 9 +++-- example/guess_number/main.zig | 3 +- example/hello_world/hello_libc.zig | 2 +- src-self-hosted/compilation.zig | 1 + src-self-hosted/libc_installation.zig | 6 +-- src-self-hosted/main.zig | 19 +++++----- std/c.zig | 13 +++++-- std/c/linux.zig | 22 +++++++++-- std/child_process.zig | 18 ++++----- std/cstr.zig | 2 +- std/dynamic_library.zig | 38 +++++++------------ std/event/fs.zig | 14 +++---- std/event/loop.zig | 32 ++++++++-------- std/event/net.zig | 27 +++++++------ std/fs.zig | 26 ++----------- std/fs/file.zig | 15 +++++--- std/fs/path.zig | 15 ++++---- std/heap.zig | 2 +- std/io/test.zig | 8 ++-- std/os.zig | 71 ++++++++++++++++++++--------------- std/os/bits/darwin.zig | 16 ++++---- std/os/bits/freebsd.zig | 28 ++++++-------- std/os/bits/linux.zig | 30 +++++++-------- std/os/bits/netbsd.zig | 26 +++++-------- std/os/linux.zig | 2 +- std/os/linux/test.zig | 2 +- std/os/linux/vdso.zig | 2 +- std/os/test.zig | 35 ++++++++--------- std/os/windows.zig | 38 +++++++++++++++++++ std/os/zen.zig | 2 +- std/process.zig | 22 ++++++++++- std/thread.zig | 56 +++++++++++++++++---------- std/time.zig | 24 ++++-------- test/compare_output.zig | 6 +-- test/standalone/empty_env/main.zig | 2 +- test/tests.zig | 2 +- 38 files changed, 348 insertions(+), 298 deletions(-) (limited to 'std/c/linux.zig') diff --git a/build.zig b/build.zig index bb23502d82..82949ad5bc 100644 --- a/build.zig +++ b/build.zig @@ -166,10 +166,8 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void { } fn fileExists(filename: []const u8) !bool { - fs.File.exists(filename) catch |err| switch (err) { - error.PermissionDenied, - error.FileNotFound, - => return false, + fs.File.access(filename) catch |err| switch (err) { + error.FileNotFound => return false, else => return err, }; return true; diff --git a/doc/langref.html.in b/doc/langref.html.in index 5b9beba644..19839904b1 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -796,8 +796,8 @@ const assert = std.debug.assert; threadlocal var x: i32 = 1234; test "thread local storage" { - const thread1 = try std.os.spawnThread({}, testTls); - const thread2 = try std.os.spawnThread({}, testTls); + const thread1 = try std.Thread.spawn({}, testTls); + const thread2 = try std.Thread.spawn({}, testTls); testTls({}); thread1.wait(); thread2.wait(); diff --git a/example/cat/main.zig b/example/cat/main.zig index 7255217831..c57c1e4bcb 100644 --- a/example/cat/main.zig +++ b/example/cat/main.zig @@ -1,12 +1,13 @@ const std = @import("std"); const io = std.io; +const process = std.process; +const File = std.fs.File; const mem = std.mem; -const os = std.os; const warn = std.debug.warn; const allocator = std.debug.global_allocator; pub fn main() !void { - var args_it = os.args(); + var args_it = process.args(); const exe = try unwrapArg(args_it.next(allocator).?); var catted_anything = false; var stdout_file = try io.getStdOut(); @@ -20,7 +21,7 @@ pub fn main() !void { } else if (arg[0] == '-') { return usage(exe); } else { - var file = os.File.openRead(arg) catch |err| { + var file = File.openRead(arg) catch |err| { warn("Unable to open file: {}\n", @errorName(err)); return err; }; @@ -41,7 +42,7 @@ fn usage(exe: []const u8) !void { return error.Invalid; } -fn cat_file(stdout: *os.File, file: *os.File) !void { +fn cat_file(stdout: *File, file: *File) !void { var buf: [1024 * 4]u8 = undefined; while (true) { diff --git a/example/guess_number/main.zig b/example/guess_number/main.zig index b4eb1c292a..b1e557fd20 100644 --- a/example/guess_number/main.zig +++ b/example/guess_number/main.zig @@ -2,7 +2,6 @@ const builtin = @import("builtin"); const std = @import("std"); const io = std.io; const fmt = std.fmt; -const os = std.os; pub fn main() !void { var stdout_file = try io.getStdOut(); @@ -11,7 +10,7 @@ pub fn main() !void { try stdout.print("Welcome to the Guess Number Game in Zig.\n"); var seed_bytes: [@sizeOf(u64)]u8 = undefined; - os.getRandomBytes(seed_bytes[0..]) catch |err| { + std.crypto.randomBytes(seed_bytes[0..]) catch |err| { std.debug.warn("unable to seed random number generator: {}", err); return err; }; diff --git a/example/hello_world/hello_libc.zig b/example/hello_world/hello_libc.zig index 3f077283db..d18b1c26b3 100644 --- a/example/hello_world/hello_libc.zig +++ b/example/hello_world/hello_libc.zig @@ -5,6 +5,6 @@ const c = @cImport({ }); export fn main(argc: c_int, argv: [*]?[*]u8) c_int { - c.fprintf(c.stderr, c"Hello, world!\n"); + _ = c.fprintf(c.stderr, c"Hello, world!\n"); return 0; } diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig index 720c26945d..450fde7219 100644 --- a/src-self-hosted/compilation.zig +++ b/src-self-hosted/compilation.zig @@ -301,6 +301,7 @@ pub const Compilation = struct { InvalidUtf8, BadPathName, DeviceBusy, + CurrentWorkingDirectoryUnlinked, }; pub const Event = union(enum) { diff --git a/src-self-hosted/libc_installation.zig b/src-self-hosted/libc_installation.zig index 53790ec8f4..7ca849d10c 100644 --- a/src-self-hosted/libc_installation.zig +++ b/src-self-hosted/libc_installation.zig @@ -182,7 +182,7 @@ pub const LibCInstallation = struct { } async fn findNativeIncludeDirLinux(self: *LibCInstallation, loop: *event.Loop) !void { - const cc_exe = std.process.getEnvPosix("CC") orelse "cc"; + const cc_exe = std.os.getenv("CC") orelse "cc"; const argv = []const []const u8{ cc_exe, "-E", @@ -392,7 +392,7 @@ pub const LibCInstallation = struct { /// caller owns returned memory async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bool) ![]u8 { - const cc_exe = std.process.getEnvPosix("CC") orelse "cc"; + const cc_exe = std.os.getenv("CC") orelse "cc"; const arg1 = try std.fmt.allocPrint(loop.allocator, "-print-file-name={}", o_file); defer loop.allocator.free(arg1); const argv = []const []const u8{ cc_exe, arg1 }; @@ -463,7 +463,7 @@ fn fileExists(path: []const u8) !bool { if (fs.File.access(path)) |_| { return true; } else |err| switch (err) { - error.FileNotFound, error.PermissionDenied => return false, + error.FileNotFound => return false, else => return error.FileSystem, } } diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 1d2ac4917b..4095caa8c4 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -702,6 +702,7 @@ const FmtError = error{ ReadOnlyFileSystem, LinkQuotaExceeded, FileBusy, + CurrentWorkingDirectoryUnlinked, } || fs.File.OpenError; async fn asyncFmtMain( @@ -851,7 +852,7 @@ fn cmdTargets(allocator: *Allocator, args: []const []const u8) !void { } fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void { - try stdout.print("{}\n", std.cstr.toSliceConst(c.ZIG_VERSION_STRING)); + try stdout.print("{}\n", std.mem.toSliceConst(u8, c.ZIG_VERSION_STRING)); } const args_test_spec = []Flag{Flag.Bool("--help")}; @@ -924,14 +925,14 @@ fn cmdInternalBuildInfo(allocator: *Allocator, args: []const []const u8) !void { \\ZIG_DIA_GUIDS_LIB {} \\ , - std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR), - std.cstr.toSliceConst(c.ZIG_CXX_COMPILER), - std.cstr.toSliceConst(c.ZIG_LLVM_CONFIG_EXE), - std.cstr.toSliceConst(c.ZIG_LLD_INCLUDE_PATH), - std.cstr.toSliceConst(c.ZIG_LLD_LIBRARIES), - std.cstr.toSliceConst(c.ZIG_STD_FILES), - std.cstr.toSliceConst(c.ZIG_C_HEADER_FILES), - std.cstr.toSliceConst(c.ZIG_DIA_GUIDS_LIB), + std.mem.toSliceConst(u8, c.ZIG_CMAKE_BINARY_DIR), + std.mem.toSliceConst(u8, c.ZIG_CXX_COMPILER), + std.mem.toSliceConst(u8, c.ZIG_LLVM_CONFIG_EXE), + std.mem.toSliceConst(u8, c.ZIG_LLD_INCLUDE_PATH), + std.mem.toSliceConst(u8, c.ZIG_LLD_LIBRARIES), + std.mem.toSliceConst(u8, c.ZIG_STD_FILES), + std.mem.toSliceConst(u8, c.ZIG_C_HEADER_FILES), + std.mem.toSliceConst(u8, c.ZIG_DIA_GUIDS_LIB), ); } diff --git a/std/c.zig b/std/c.zig index 9171a99ec9..90d8f0056e 100644 --- a/std/c.zig +++ b/std/c.zig @@ -39,8 +39,8 @@ pub extern "c" fn open(path: [*]const u8, oflag: c_uint, ...) c_int; pub extern "c" fn raise(sig: c_int) c_int; pub extern "c" fn read(fd: fd_t, buf: [*]u8, nbyte: usize) isize; pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize; -pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize; -pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize; +pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: usize) isize; +pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: usize) isize; pub extern "c" fn stat(noalias path: [*]const u8, noalias buf: *Stat) c_int; pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize; pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize; @@ -49,7 +49,7 @@ pub extern "c" fn munmap(addr: *align(page_size) c_void, len: usize) c_int; pub extern "c" fn mprotect(addr: *align(page_size) c_void, len: usize, prot: c_uint) c_int; pub extern "c" fn unlink(path: [*]const u8) c_int; pub extern "c" fn getcwd(buf: [*]u8, size: usize) ?[*]u8; -pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_int, options: c_int) c_int; +pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_uint, options: c_uint) c_int; pub extern "c" fn fork() c_int; pub extern "c" fn access(path: [*]const u8, mode: c_uint) c_int; pub extern "c" fn pipe(fds: *[2]fd_t) c_int; @@ -76,7 +76,12 @@ pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usi pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int; -pub extern "c" fn socket(domain: c_int, sock_type: c_int, protocol: c_int) c_int; +pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int; +pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int; +pub extern "c" fn getsockname(sockfd: fd_t, noalias addr: *sockaddr, noalias addrlen: *socklen_t) c_int; +pub extern "c" fn connect(sockfd: fd_t, sock_addr: *const sockaddr, addrlen: socklen_t) c_int; +pub extern "c" fn accept4(sockfd: fd_t, addr: *sockaddr, addrlen: *socklen_t, flags: c_uint) c_int; +pub extern "c" fn getsockopt(sockfd: fd_t, level: c_int, optname: c_int, optval: *c_void, optlen: *socklen_t) c_int; pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int; pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize; pub extern "c" fn openat(fd: c_int, path: [*]const u8, flags: c_int) c_int; diff --git a/std/c/linux.zig b/std/c/linux.zig index 00fc600dab..9e028728c7 100644 --- a/std/c/linux.zig +++ b/std/c/linux.zig @@ -1,13 +1,27 @@ const std = @import("../std.zig"); use std.c; -pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int; -pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int; extern "c" fn __errno_location() *c_int; pub const _errno = __errno_location; +pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int; +pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int; +pub extern "c" fn eventfd(initval: c_uint, flags: c_uint) c_int; +pub extern "c" fn epoll_ctl(epfd: fd_t, op: c_uint, fd: fd_t, event: *epoll_event) c_int; +pub extern "c" fn epoll_create1(flags: c_uint) c_int; +pub extern "c" fn epoll_wait(epfd: fd_t, events: [*]epoll_event, maxevents: c_uint, timeout: c_int) c_int; +pub extern "c" fn epoll_pwait( + epfd: fd_t, + events: [*]epoll_event, + maxevents: c_int, + timeout: c_int, + sigmask: *const sigset_t, +) c_int; +pub extern "c" fn inotify_init1(flags: c_uint) c_int; +pub extern "c" fn inotify_add_watch(fd: fd_t, pathname: [*]const u8, mask: u32) c_int; + /// See std.elf for constants for this -pub extern fn getauxval(__type: c_ulong) c_ulong; +pub extern "c" fn getauxval(__type: c_ulong) c_ulong; pub const dl_iterate_phdr_callback = extern fn (info: *dl_phdr_info, size: usize, data: ?*c_void) c_int; -pub extern fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int; +pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int; diff --git a/std/child_process.zig b/std/child_process.zig index f870f5e083..2209b2acc3 100644 --- a/std/child_process.zig +++ b/std/child_process.zig @@ -54,10 +54,10 @@ pub const ChildProcess = struct { os.ChangeCurDirError || windows.CreateProcessError; pub const Term = union(enum) { - Exited: i32, - Signal: i32, - Stopped: i32, - Unknown: i32, + Exited: u32, + Signal: u32, + Stopped: u32, + Unknown: u32, }; pub const StdIo = enum { @@ -155,7 +155,7 @@ pub const ChildProcess = struct { } pub const ExecResult = struct { - term: os.ChildProcess.Term, + term: Term, stdout: []u8, stderr: []u8, }; @@ -224,7 +224,7 @@ pub const ChildProcess = struct { if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) { break :x Term{ .Unknown = 0 }; } else { - break :x Term{ .Exited = @bitCast(i32, exit_code) }; + break :x Term{ .Exited = exit_code }; } }); @@ -240,7 +240,7 @@ pub const ChildProcess = struct { self.handleWaitResult(status); } - fn handleWaitResult(self: *ChildProcess, status: i32) void { + fn handleWaitResult(self: *ChildProcess, status: u32) void { self.term = self.cleanupAfterWait(status); } @@ -259,7 +259,7 @@ pub const ChildProcess = struct { } } - fn cleanupAfterWait(self: *ChildProcess, status: i32) !Term { + fn cleanupAfterWait(self: *ChildProcess, status: u32) !Term { defer { os.close(self.err_pipe[0]); os.close(self.err_pipe[1]); @@ -281,7 +281,7 @@ pub const ChildProcess = struct { return statusToTerm(status); } - fn statusToTerm(status: i32) Term { + fn statusToTerm(status: u32) Term { return if (os.WIFEXITED(status)) Term{ .Exited = os.WEXITSTATUS(status) } else if (os.WIFSIGNALED(status)) diff --git a/std/cstr.zig b/std/cstr.zig index c8c3447921..dd28e50449 100644 --- a/std/cstr.zig +++ b/std/cstr.zig @@ -28,7 +28,7 @@ test "cstr fns" { fn testCStrFnsImpl() void { testing.expect(cmp(c"aoeu", c"aoez") == -1); - testing.expect(len(c"123456789") == 9); + testing.expect(mem.len(u8, c"123456789") == 9); } /// Returns a mutable slice with 1 more byte of length which is a null byte. diff --git a/std/dynamic_library.zig b/std/dynamic_library.zig index ce721350ea..3ae3b4c66a 100644 --- a/std/dynamic_library.zig +++ b/std/dynamic_library.zig @@ -6,8 +6,7 @@ const os = std.os; const assert = std.debug.assert; const testing = std.testing; const elf = std.elf; -const windows = os.windows; -const win_util = @import("os/windows/util.zig"); +const windows = std.os.windows; const maxInt = std.math.maxInt; pub const DynLib = switch (builtin.os) { @@ -102,17 +101,16 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator { pub const LinuxDynLib = struct { elf_lib: ElfLib, fd: i32, - map_addr: usize, - map_size: usize, + memory: []align(mem.page_size) u8, /// Trusts the file pub fn open(allocator: *mem.Allocator, path: []const u8) !DynLib { const fd = try os.open(path, 0, os.O_RDONLY | os.O_CLOEXEC); - errdefer std.os.close(fd); + errdefer os.close(fd); - const size = @intCast(usize, (try std.os.posixFStat(fd)).size); + const size = @intCast(usize, (try os.fstat(fd)).size); - const addr = os.mmap( + const bytes = try os.mmap( null, size, os.PROT_READ | os.PROT_EXEC, @@ -120,21 +118,18 @@ pub const LinuxDynLib = struct { fd, 0, ); - errdefer os.munmap(addr, size); - - const bytes = @intToPtr([*]align(mem.page_size) u8, addr)[0..size]; + errdefer os.munmap(bytes); return DynLib{ .elf_lib = try ElfLib.init(bytes), .fd = fd, - .map_addr = addr, - .map_size = size, + .memory = bytes, }; } pub fn close(self: *DynLib) void { - os.munmap(self.map_addr, self.map_size); - std.os.close(self.fd); + os.munmap(self.memory); + os.close(self.fd); self.* = undefined; } @@ -253,28 +248,21 @@ pub const WindowsDynLib = struct { dll: windows.HMODULE, pub fn open(allocator: *mem.Allocator, path: []const u8) !WindowsDynLib { - const wpath = try win_util.sliceToPrefixedFileW(path); + const wpath = try windows.sliceToPrefixedFileW(path); return WindowsDynLib{ .allocator = allocator, - .dll = windows.LoadLibraryW(&wpath) orelse { - switch (windows.GetLastError()) { - windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, - windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, - windows.ERROR.MOD_NOT_FOUND => return error.FileNotFound, - else => |err| return windows.unexpectedError(err), - } - }, + .dll = try windows.LoadLibraryW(&wpath), }; } pub fn close(self: *WindowsDynLib) void { - assert(windows.FreeLibrary(self.dll) != 0); + windows.FreeLibrary(self.dll); self.* = undefined; } pub fn lookup(self: *WindowsDynLib, name: []const u8) ?usize { - return @ptrToInt(windows.GetProcAddress(self.dll, name.ptr)); + return @ptrToInt(windows.kernel32.GetProcAddress(self.dll, name.ptr)); } }; diff --git a/std/event/fs.zig b/std/event/fs.zig index 221070a062..81d180235e 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -36,7 +36,7 @@ pub const Request = struct { offset: usize, result: Error!void, - pub const Error = os.PosixWriteError; + pub const Error = os.WriteError; }; pub const PReadV = struct { @@ -45,7 +45,7 @@ pub const Request = struct { offset: usize, result: Error!usize, - pub const Error = os.PosixReadError; + pub const Error = os.ReadError; }; pub const Open = struct { @@ -172,7 +172,7 @@ pub async fn pwritevPosix( fd: fd_t, iovecs: []const os.iovec_const, offset: usize, -) os.PosixWriteError!void { +) os.WriteError!void { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -320,7 +320,7 @@ pub async fn preadvPosix( fd: fd_t, iovecs: []const os.iovec, offset: usize, -) os.PosixReadError!usize { +) os.ReadError!usize { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -786,7 +786,7 @@ pub fn Watch(comptime V: type) type { switch (builtin.os) { builtin.Os.linux => { - const inotify_fd = try os.linuxINotifyInit1(os.linux.IN_NONBLOCK | os.linux.IN_CLOEXEC); + const inotify_fd = try os.inotify_init1(os.linux.IN_NONBLOCK | os.linux.IN_CLOEXEC); errdefer os.close(inotify_fd); var result: *Self = undefined; @@ -977,7 +977,7 @@ pub fn Watch(comptime V: type) type { var basename_with_null_consumed = false; defer if (!basename_with_null_consumed) self.channel.loop.allocator.free(basename_with_null); - const wd = try os.linuxINotifyAddWatchC( + const wd = try os.inotify_add_watchC( self.os_data.inotify_fd, dirname_with_null.ptr, os.linux.IN_CLOSE_WRITE | os.linux.IN_ONLYDIR | os.linux.IN_EXCL_UNLINK, @@ -1255,7 +1255,7 @@ pub fn Watch(comptime V: type) type { ev = @ptrCast(*os.linux.inotify_event, ptr); if (ev.mask & os.linux.IN_CLOSE_WRITE == os.linux.IN_CLOSE_WRITE) { const basename_ptr = ptr + @sizeOf(os.linux.inotify_event); - const basename_with_null = basename_ptr[0 .. std.cstr.len(basename_ptr) + 1]; + const basename_with_null = basename_ptr[0 .. std.mem.len(u8, basename_ptr) + 1]; const user_value = blk: { const held = await (async watch.os_data.table_lock.acquire() catch unreachable); defer held.release(); diff --git a/std/event/loop.zig b/std/event/loop.zig index e1848684c7..ae19ebb0f8 100644 --- a/std/event/loop.zig +++ b/std/event/loop.zig @@ -99,7 +99,7 @@ pub const Loop = struct { /// have the correct pointer value. pub fn initMultiThreaded(self: *Loop, allocator: *mem.Allocator) !void { if (builtin.single_threaded) @compileError("initMultiThreaded unavailable when building in single-threaded mode"); - const core_count = try os.cpuCount(allocator); + const core_count = try Thread.cpuCount(); return self.initInternal(allocator, core_count); } @@ -139,9 +139,9 @@ pub const Loop = struct { self.allocator.free(self.extra_threads); } - const InitOsDataError = os.LinuxEpollCreateError || mem.Allocator.Error || os.LinuxEventFdError || - os.SpawnThreadError || os.LinuxEpollCtlError || os.BsdKEventError || - os.WindowsCreateIoCompletionPortError; + const InitOsDataError = os.EpollCreateError || mem.Allocator.Error || os.EventFdError || + Thread.SpawnError || os.EpollCtlError || os.KEventError || + windows.CreateIoCompletionPortError; const wakeup_bytes = []u8{0x1} ** 8; @@ -172,7 +172,7 @@ pub const Loop = struct { .handle = undefined, .overlapped = ResumeNode.overlapped_init, }, - .eventfd = try os.linuxEventFd(1, os.EFD_CLOEXEC | os.EFD_NONBLOCK), + .eventfd = try os.eventfd(1, os.EFD_CLOEXEC | os.EFD_NONBLOCK), .epoll_op = os.EPOLL_CTL_ADD, }, .next = undefined, @@ -180,17 +180,17 @@ pub const Loop = struct { self.available_eventfd_resume_nodes.push(eventfd_node); } - self.os_data.epollfd = try os.linuxEpollCreate(os.EPOLL_CLOEXEC); + self.os_data.epollfd = try os.epoll_create1(os.EPOLL_CLOEXEC); errdefer os.close(self.os_data.epollfd); - self.os_data.final_eventfd = try os.linuxEventFd(0, os.EFD_CLOEXEC | os.EFD_NONBLOCK); + self.os_data.final_eventfd = try os.eventfd(0, os.EFD_CLOEXEC | os.EFD_NONBLOCK); errdefer os.close(self.os_data.final_eventfd); self.os_data.final_eventfd_event = os.epoll_event{ .events = os.EPOLLIN, .data = os.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) }, }; - try os.linuxEpollCtl( + try os.epoll_ctl( self.os_data.epollfd, os.EPOLL_CTL_ADD, self.os_data.final_eventfd, @@ -211,7 +211,7 @@ pub const Loop = struct { var extra_thread_index: usize = 0; errdefer { // writing 8 bytes to an eventfd cannot fail - os.posixWrite(self.os_data.final_eventfd, wakeup_bytes) catch unreachable; + os.write(self.os_data.final_eventfd, wakeup_bytes) catch unreachable; while (extra_thread_index != 0) { extra_thread_index -= 1; self.extra_threads[extra_thread_index].wait(); @@ -417,11 +417,11 @@ pub const Loop = struct { .events = flags, .data = os.linux.epoll_data{ .ptr = @ptrToInt(resume_node) }, }; - try os.linuxEpollCtl(self.os_data.epollfd, op, fd, &ev); + try os.epoll_ctl(self.os_data.epollfd, op, fd, &ev); } pub fn linuxRemoveFd(self: *Loop, fd: i32) void { - os.linuxEpollCtl(self.os_data.epollfd, os.linux.EPOLL_CTL_DEL, fd, undefined) catch {}; + os.epoll_ctl(self.os_data.epollfd, os.linux.EPOLL_CTL_DEL, fd, undefined) catch {}; self.finishOneEvent(); } @@ -626,7 +626,7 @@ pub const Loop = struct { builtin.Os.linux => { self.posixFsRequest(&self.os_data.fs_end_request); // writing 8 bytes to an eventfd cannot fail - os.posixWrite(self.os_data.final_eventfd, wakeup_bytes) catch unreachable; + os.write(self.os_data.final_eventfd, wakeup_bytes) catch unreachable; return; }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { @@ -666,7 +666,7 @@ pub const Loop = struct { builtin.Os.linux => { // only process 1 event so we don't steal from other threads var events: [1]os.linux.epoll_event = undefined; - const count = os.linuxEpollWait(self.os_data.epollfd, events[0..], -1); + const count = os.epoll_wait(self.os_data.epollfd, events[0..], -1); for (events[0..count]) |ev| { const resume_node = @intToPtr(*ResumeNode, ev.data.ptr); const handle = resume_node.handle; @@ -783,10 +783,10 @@ pub const Loop = struct { switch (node.data.msg) { @TagType(fs.Request.Msg).End => return, @TagType(fs.Request.Msg).PWriteV => |*msg| { - msg.result = os.posix_pwritev(msg.fd, msg.iov.ptr, msg.iov.len, msg.offset); + msg.result = os.pwritev(msg.fd, msg.iov, msg.offset); }, @TagType(fs.Request.Msg).PReadV => |*msg| { - msg.result = os.posix_preadv(msg.fd, msg.iov.ptr, msg.iov.len, msg.offset); + msg.result = os.preadv(msg.fd, msg.iov, msg.offset); }, @TagType(fs.Request.Msg).Open => |*msg| { msg.result = os.openC(msg.path.ptr, msg.flags, msg.mode); @@ -800,7 +800,7 @@ pub const Loop = struct { break :blk; }; defer os.close(fd); - msg.result = os.posixWrite(fd, msg.contents); + msg.result = os.write(fd, msg.contents); }, } switch (node.data.finish) { diff --git a/std/event/net.zig b/std/event/net.zig index 2346b1eb23..f4398196e3 100644 --- a/std/event/net.zig +++ b/std/event/net.zig @@ -45,13 +45,13 @@ pub const Server = struct { ) !void { self.handleRequestFn = handleRequestFn; - const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); + const sockfd = try os.socket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); self.sockfd = sockfd; - try os.posixBind(sockfd, &address.os_addr); - try os.posixListen(sockfd, os.SOMAXCONN); - self.listen_address = std.net.Address.initPosix(try os.posixGetSockName(sockfd)); + try os.bind(sockfd, &address.os_addr); + try os.listen(sockfd, os.SOMAXCONN); + self.listen_address = std.net.Address.initPosix(try os.getsockname(sockfd)); self.accept_coro = try async Server.handler(self); errdefer cancel self.accept_coro.?; @@ -64,7 +64,10 @@ pub const Server = struct { /// Stop listening pub fn close(self: *Server) void { self.loop.linuxRemoveFd(self.sockfd.?); - os.close(self.sockfd.?); + if (self.sockfd) |fd| { + os.close(fd); + self.sockfd = null; + } } pub fn deinit(self: *Server) void { @@ -76,7 +79,7 @@ pub const Server = struct { while (true) { var accepted_addr: std.net.Address = undefined; // TODO just inline the following function here and don't expose it as posixAsyncAccept - if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, os.SOCK_NONBLOCK | os.SOCK_CLOEXEC)) |accepted_fd| { + if (os.accept4_async(self.sockfd.?, &accepted_addr.os_addr, os.SOCK_NONBLOCK | os.SOCK_CLOEXEC)) |accepted_fd| { if (accepted_fd == -1) { // would block suspend; // we will get resumed by epoll_wait in the event loop @@ -105,7 +108,7 @@ pub const Server = struct { }; pub async fn connectUnixSocket(loop: *Loop, path: []const u8) !i32 { - const sockfd = try os.posixSocket( + const sockfd = try os.socket( os.AF_UNIX, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, 0, @@ -120,9 +123,9 @@ pub async fn connectUnixSocket(loop: *Loop, path: []const u8) !i32 { if (path.len > @typeOf(sock_addr.path).len) return error.NameTooLong; mem.copy(u8, sock_addr.path[0..], path); const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len); - try os.posixConnectAsync(sockfd, &sock_addr, size); + try os.connect_async(sockfd, &sock_addr, size); try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); - try os.posixGetSockOptConnectError(sockfd); + try os.getsockoptError(sockfd); return sockfd; } @@ -249,12 +252,12 @@ pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { pub async fn connect(loop: *Loop, _address: *const std.net.Address) !File { var address = _address.*; // TODO https://github.com/ziglang/zig/issues/1592 - const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); + const sockfd = try os.socket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); - try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(os.sockaddr_in)); + try os.connect_async(sockfd, &address.os_addr, @sizeOf(os.sockaddr_in)); try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); - try os.posixGetSockOptConnectError(sockfd); + try os.getsockoptError(sockfd); return File.openHandle(sockfd); } diff --git a/std/fs.zig b/std/fs.zig index 229f3099c2..5c0db71435 100644 --- a/std/fs.zig +++ b/std/fs.zig @@ -38,24 +38,6 @@ pub const MAX_PATH_BYTES = switch (builtin.os) { else => @compileError("Unsupported OS"), }; -/// The result is a slice of `out_buffer`, from index `0`. -pub fn getCwd(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 { - return os.getcwd(out_buffer); -} - -/// Caller must free the returned memory. -pub fn getCwdAlloc(allocator: *Allocator) ![]u8 { - var buf: [MAX_PATH_BYTES]u8 = undefined; - return mem.dupe(allocator, u8, try os.getcwd(&buf)); -} - -test "getCwdAlloc" { - // at least call it so it gets compiled - var buf: [1000]u8 = undefined; - const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator; - _ = getCwdAlloc(allocator) catch {}; -} - // here we replace the standard +/ with -_ so that it can be used in a file name const b64_fs_encoder = base64.Base64Encoder.init("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", base64.standard_pad_char); @@ -260,17 +242,17 @@ pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { /// Returns `error.DirNotEmpty` if the directory is not empty. /// To delete a directory recursively, see `deleteTree`. -pub fn deleteDir(dir_path: []const u8) DeleteDirError!void { +pub fn deleteDir(dir_path: []const u8) !void { return os.rmdir(dir_path); } /// Same as `deleteDir` except the parameter is a null-terminated UTF8-encoded string. -pub fn deleteDirC(dir_path: [*]const u8) DeleteDirError!void { +pub fn deleteDirC(dir_path: [*]const u8) !void { return os.rmdirC(dir_path); } /// Same as `deleteDir` except the parameter is a null-terminated UTF16LE-encoded string. -pub fn deleteDirW(dir_path: [*]const u16) DeleteDirError!void { +pub fn deleteDirW(dir_path: [*]const u16) !void { return os.rmdirW(dir_path); } @@ -362,7 +344,7 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError! }; defer dir.close(); - var full_entry_buf = ArrayList(u8).init(allocator); + var full_entry_buf = std.ArrayList(u8).init(allocator); defer full_entry_buf.deinit(); while (try dir.next()) |entry| { diff --git a/std/fs/file.zig b/std/fs/file.zig index 915dae331f..7cb722292c 100644 --- a/std/fs/file.zig +++ b/std/fs/file.zig @@ -137,24 +137,27 @@ pub const File = struct { /// Test for the existence of `path`. /// `path` is UTF8-encoded. - pub fn exists(path: []const u8) !void { + /// In general it is recommended to avoid this function. For example, + /// instead of testing if a file exists and then opening it, just + /// open it and handle the error for file not found. + pub fn access(path: []const u8) !void { return os.access(path, os.F_OK); } - /// Same as `exists` except the parameter is null-terminated. - pub fn existsC(path: [*]const u8) !void { + /// Same as `access` except the parameter is null-terminated. + pub fn accessC(path: [*]const u8) !void { return os.accessC(path, os.F_OK); } - /// Same as `exists` except the parameter is null-terminated UTF16LE-encoded. - pub fn existsW(path: [*]const u16) !void { + /// Same as `access` except the parameter is null-terminated UTF16LE-encoded. + pub fn accessW(path: [*]const u16) !void { return os.accessW(path, os.F_OK); } /// Upon success, the stream is in an uninitialized state. To continue using it, /// you must use the open() function. pub fn close(self: File) void { - os.close(self.handle); + return os.close(self.handle); } /// Test whether the file refers to a terminal. diff --git a/std/fs/path.zig b/std/fs/path.zig index 217bb44327..7b95a3e4d2 100644 --- a/std/fs/path.zig +++ b/std/fs/path.zig @@ -9,6 +9,7 @@ const Allocator = mem.Allocator; const math = std.math; const windows = std.os.windows; const fs = std.fs; +const process = std.process; pub const sep_windows = '\\'; pub const sep_posix = '/'; @@ -390,7 +391,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd - return fs.getCwdAlloc(allocator); + return process.getCwdAlloc(allocator); } // determine which disk designator we will result with, if any @@ -485,7 +486,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { }, WindowsPath.Kind.None => { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd - const cwd = try fs.getCwdAlloc(allocator); + const cwd = try process.getCwdAlloc(allocator); defer allocator.free(cwd); const parsed_cwd = windowsParsePath(cwd); result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); @@ -501,7 +502,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { } else { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd // TODO call get cwd for the result_disk_designator instead of the global one - const cwd = try fs.getCwdAlloc(allocator); + const cwd = try process.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); @@ -571,7 +572,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd - return fs.getCwdAlloc(allocator); + return process.getCwdAlloc(allocator); } var first_index: usize = 0; @@ -593,7 +594,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { result = try allocator.alloc(u8, max_size); } else { assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd - const cwd = try fs.getCwdAlloc(allocator); + const cwd = try process.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); mem.copy(u8, result, cwd); @@ -632,7 +633,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { } test "resolve" { - const cwd = try fs.getCwdAlloc(debug.global_allocator); + const cwd = try process.getCwdAlloc(debug.global_allocator); if (windows.is_the_target) { if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { cwd[0] = asciiUpper(cwd[0]); @@ -646,7 +647,7 @@ test "resolve" { test "resolveWindows" { if (windows.is_the_target) { - const cwd = try fs.getCwdAlloc(debug.global_allocator); + const cwd = try process.getCwdAlloc(debug.global_allocator); const parsed_cwd = windowsParsePath(cwd); { const result = testResolveWindows([][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }); diff --git a/std/heap.zig b/std/heap.zig index 198eb2d073..6c6a97d037 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -112,7 +112,7 @@ pub const DirectAllocator = struct { -1, 0, ) catch return error.OutOfMemory; - if (alloc_size == n) return slice; + if (alloc_size == n) return slice[0..n]; const aligned_addr = mem.alignForward(@ptrToInt(slice.ptr), alignment); diff --git a/std/io/test.zig b/std/io/test.zig index 7928baa597..fc3b0f8902 100644 --- a/std/io/test.zig +++ b/std/io/test.zig @@ -7,7 +7,7 @@ const DefaultPrng = std.rand.DefaultPrng; const expect = std.testing.expect; const expectError = std.testing.expectError; const mem = std.mem; -const os = std.os; +const fs = std.fs; const File = std.fs.File; test "write a file, read it, then delete it" { @@ -58,7 +58,7 @@ test "write a file, read it, then delete it" { expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data)); expect(mem.eql(u8, contents[contents.len - "end".len ..], "end")); } - try os.deleteFile(tmp_file_name); + try fs.deleteFile(tmp_file_name); } test "BufferOutStream" { @@ -316,7 +316,7 @@ test "BitStreams with File Stream" { expectError(error.EndOfStream, bit_stream.readBitsNoEof(u1, 1)); } - try os.deleteFile(tmp_file_name); + try fs.deleteFile(tmp_file_name); } fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime packing: io.Packing) !void { @@ -596,7 +596,7 @@ test "c out stream" { const filename = c"tmp_io_test_file.txt"; const out_file = std.c.fopen(filename, c"w") orelse return error.UnableToOpenTestFile; - defer std.os.deleteFileC(filename) catch {}; + defer fs.deleteFileC(filename) catch {}; const out_stream = &io.COutStream.init(out_file).stream; try out_stream.print("hi: {}\n", i32(123)); diff --git a/std/os.zig b/std/os.zig index 1debb16234..087d5e12a5 100644 --- a/std/os.zig +++ b/std/os.zig @@ -284,7 +284,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { /// Number of bytes read is returned. Upon reading end-of-file, zero is returned. /// This function is for blocking file descriptors only. For non-blocking, see /// `preadvAsync`. -pub fn preadv(fd: fd_t, iov: [*]const iovec, count: usize, offset: u64) ReadError!usize { +pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) ReadError!usize { if (darwin.is_the_target) { // Darwin does not have preadv but it does have pread. var off: usize = 0; @@ -301,7 +301,7 @@ pub fn preadv(fd: fd_t, iov: [*]const iovec, count: usize, offset: u64) ReadErro if (inner_off == v.iov_len) { iov_i += 1; inner_off = 0; - if (iov_i == count) { + if (iov_i == iov.len) { return off; } } @@ -323,9 +323,10 @@ pub fn preadv(fd: fd_t, iov: [*]const iovec, count: usize, offset: u64) ReadErro } } while (true) { - const rc = system.preadv(fd, iov, count, offset); + // TODO handle the case when iov_len is too large and get rid of this @intCast + const rc = system.preadv(fd, iov.ptr, @intCast(u32, iov.len), offset); switch (errno(rc)) { - 0 => return rc, + 0 => return @bitCast(usize, rc), EINTR => continue, EINVAL => unreachable, EFAULT => unreachable, @@ -407,7 +408,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!void { /// Write multiple buffers to a file descriptor. Keeps trying if it gets interrupted. /// This function is for blocking file descriptors only. For non-blocking, see /// `pwritevAsync`. -pub fn pwritev(fd: fd_t, iov: [*]const iovec_const, count: usize, offset: u64) WriteError!void { +pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) WriteError!void { if (darwin.is_the_target) { // Darwin does not have pwritev but it does have pwrite. var off: usize = 0; @@ -424,7 +425,7 @@ pub fn pwritev(fd: fd_t, iov: [*]const iovec_const, count: usize, offset: u64) W if (inner_off == v.iov_len) { iov_i += 1; inner_off = 0; - if (iov_i == count) { + if (iov_i == iov.len) { return; } } @@ -449,7 +450,8 @@ pub fn pwritev(fd: fd_t, iov: [*]const iovec_const, count: usize, offset: u64) W } while (true) { - const rc = system.pwritev(fd, iov, count, offset); + // TODO handle the case when iov_len is too large and get rid of this @intCast + const rc = system.pwritev(fd, iov.ptr, @intCast(u32, iov.len), offset); switch (errno(rc)) { 0 => return, EINTR => continue, @@ -724,7 +726,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { EINVAL => unreachable, ENOENT => return error.CurrentWorkingDirectoryUnlinked, ERANGE => return error.NameTooLong, - else => return unexpectedErrno(err), + else => return unexpectedErrno(@intCast(usize, err)), } } @@ -1121,7 +1123,7 @@ pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 { } const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len); switch (errno(rc)) { - 0 => return out_buffer[0..rc], + 0 => return out_buffer[0..@bitCast(usize, rc)], EACCES => return error.AccessDenied, EFAULT => unreachable, EINVAL => unreachable, @@ -1307,7 +1309,7 @@ pub const BindError = error{ /// addr is `*const T` where T is one of the sockaddr pub fn bind(fd: i32, addr: *const sockaddr) BindError!void { - const rc = system.bind(fd, system, @sizeOf(sockaddr)); + const rc = system.bind(fd, addr, @sizeOf(sockaddr)); switch (errno(rc)) { 0 => return, EACCES => return error.AccessDenied, @@ -1521,7 +1523,7 @@ pub fn epoll_wait(epfd: i32, events: []epoll_event, timeout: i32) usize { // TODO get rid of the @intCast const rc = system.epoll_wait(epfd, events.ptr, @intCast(u32, events.len), timeout); switch (errno(rc)) { - 0 => return rc, + 0 => return @intCast(usize, rc), EINTR => continue, EBADF => unreachable, EFAULT => unreachable, @@ -1613,12 +1615,10 @@ pub const ConnectError = error{ /// Initiate a connection on a socket. /// This is for blocking file descriptors only. /// For non-blocking, see `connect_async`. -pub fn connect(sockfd: i32, sockaddr: *const sockaddr) ConnectError!void { +pub fn connect(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!void { while (true) { - switch (errno(system.connect(sockfd, sockaddr, @sizeOf(sockaddr)))) { + switch (errno(system.connect(sockfd, sock_addr, @sizeOf(sockaddr)))) { 0 => return, - else => |err| return unexpectedErrno(err), - EACCES => return error.PermissionDenied, EPERM => return error.PermissionDenied, EADDRINUSE => return error.AddressInUse, @@ -1636,19 +1636,18 @@ pub fn connect(sockfd: i32, sockaddr: *const sockaddr) ConnectError!void { ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. EPROTOTYPE => unreachable, // The socket type does not support the requested communications protocol. ETIMEDOUT => return error.ConnectionTimedOut, + else => |err| return unexpectedErrno(err), } } } /// Same as `connect` except it is for blocking socket file descriptors. /// It expects to receive EINPROGRESS`. -pub fn connect_async(sockfd: i32, sockaddr: *const c_void, len: u32) ConnectError!void { +pub fn connect_async(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!void { while (true) { - switch (errno(system.connect(sockfd, sockaddr, len))) { - 0, EINPROGRESS => return, + switch (errno(system.connect(sockfd, sock_addr, @sizeOf(sockaddr)))) { EINTR => continue, - else => |err| return unexpectedErrno(err), - + 0, EINPROGRESS => return, EACCES => return error.PermissionDenied, EPERM => return error.PermissionDenied, EADDRINUSE => return error.AddressInUse, @@ -1664,13 +1663,14 @@ pub fn connect_async(sockfd: i32, sockaddr: *const c_void, len: u32) ConnectErro ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. EPROTOTYPE => unreachable, // The socket type does not support the requested communications protocol. ETIMEDOUT => return error.ConnectionTimedOut, + else => |err| return unexpectedErrno(err), } } } pub fn getsockoptError(sockfd: i32) ConnectError!void { - var err_code: i32 = undefined; - var size: u32 = @sizeOf(i32); + var err_code: u32 = undefined; + var size: u32 = @sizeOf(u32); const rc = system.getsockopt(sockfd, SOL_SOCKET, SO_ERROR, @ptrCast([*]u8, &err_code), &size); assert(size == 4); switch (errno(rc)) { @@ -1702,11 +1702,13 @@ pub fn getsockoptError(sockfd: i32) ConnectError!void { } } -pub fn waitpid(pid: i32, flags: u32) i32 { - var status: i32 = undefined; +pub fn waitpid(pid: i32, flags: u32) u32 { + // TODO allow implicit pointer cast from *u32 to *c_uint ? + const Status = if (builtin.link_libc) c_uint else u32; + var status: Status = undefined; while (true) { switch (errno(system.waitpid(pid, &status, flags))) { - 0 => return status, + 0 => return @bitCast(u32, status), EINTR => continue, ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. EINVAL => unreachable, // The options argument was invalid @@ -1892,11 +1894,19 @@ pub fn fork() ForkError!pid_t { } pub const MMapError = error{ + /// The underlying filesystem of the specified file does not support memory mapping. + MemoryMappingNotSupported, + + /// A file descriptor refers to a non-regular file. Or a file mapping was requested, + /// but the file descriptor is not open for reading. Or `MAP_SHARED` was requested + /// and `PROT_WRITE` is set, but the file descriptor is not open in `O_RDWR` mode. + /// Or `PROT_WRITE` is set, but the file is append-only. AccessDenied, + + /// The `prot` argument asks for `PROT_EXEC` but the mapped area belongs to a file on + /// a filesystem that was mounted no-exec. PermissionDenied, LockedMemoryLimitExceeded, - SystemFdQuotaExceeded, - MemoryMappingNotSupported, OutOfMemory, Unexpected, }; @@ -1932,7 +1942,6 @@ pub fn mmap( EAGAIN => return error.LockedMemoryLimitExceeded, EBADF => unreachable, // Always a race condition. EOVERFLOW => unreachable, // The number of pages used for length + offset would overflow. - ENFILE => return error.SystemFdQuotaExceeded, ENODEV => return error.MemoryMappingNotSupported, EINVAL => unreachable, // Invalid parameters to mmap() ENOMEM => return error.OutOfMemory, @@ -2265,7 +2274,7 @@ pub fn realpathC(pathname: [*]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPat ENAMETOOLONG => return error.NameTooLong, ELOOP => return error.SymLinkLoop, EIO => return error.InputOutput, - else => |err| return unexpectedErrno(err), + else => |err| return unexpectedErrno(@intCast(usize, err)), }; return mem.toSlice(u8, result_path); } @@ -2349,7 +2358,7 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void { } pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void { - switch (errno(system.clock_getres(clk_id, tp))) { + switch (errno(system.clock_getres(clk_id, res))) { 0 => return, EFAULT => unreachable, EINVAL => return error.UnsupportedClock, @@ -2364,7 +2373,7 @@ pub const SchedGetAffinityError = error{ pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t { var set: cpu_set_t = undefined; - switch (errno(system.sched_getaffinity(pid, &set))) { + switch (errno(system.sched_getaffinity(pid, @sizeOf(cpu_set_t), &set))) { 0 => return set, EFAULT => unreachable, EINVAL => unreachable, diff --git a/std/os/bits/darwin.zig b/std/os/bits/darwin.zig index ed64a6426a..6c578bc57e 100644 --- a/std/os/bits/darwin.zig +++ b/std/os/bits/darwin.zig @@ -219,7 +219,7 @@ pub const MAP_NOCACHE = 0x0400; /// don't reserve needed swap area pub const MAP_NORESERVE = 0x0040; -pub const MAP_FAILED = maxInt(usize); +pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize)); /// [XSI] no hang in wait/no child to reap pub const WNOHANG = 0x00000001; @@ -749,26 +749,26 @@ pub const IPPROTO_UDP = 17; pub const IPPROTO_IP = 0; pub const IPPROTO_IPV6 = 41; -fn wstatus(x: i32) i32 { +fn wstatus(x: u32) u32 { return x & 0o177; } const wstopped = 0o177; -pub fn WEXITSTATUS(x: i32) i32 { +pub fn WEXITSTATUS(x: u32) u32 { return x >> 8; } -pub fn WTERMSIG(x: i32) i32 { +pub fn WTERMSIG(x: u32) u32 { return wstatus(x); } -pub fn WSTOPSIG(x: i32) i32 { +pub fn WSTOPSIG(x: u32) u32 { return x >> 8; } -pub fn WIFEXITED(x: i32) bool { +pub fn WIFEXITED(x: u32) bool { return wstatus(x) == 0; } -pub fn WIFSTOPPED(x: i32) bool { +pub fn WIFSTOPPED(x: u32) bool { return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13; } -pub fn WIFSIGNALED(x: i32) bool { +pub fn WIFSIGNALED(x: u32) bool { return wstatus(x) != wstopped and wstatus(x) != 0; } diff --git a/std/os/bits/freebsd.zig b/std/os/bits/freebsd.zig index 941181f221..d4f49033e1 100644 --- a/std/os/bits/freebsd.zig +++ b/std/os/bits/freebsd.zig @@ -161,7 +161,7 @@ pub const CLOCK_SECOND = 13; pub const CLOCK_THREAD_CPUTIME_ID = 14; pub const CLOCK_PROCESS_CPUTIME_ID = 15; -pub const MAP_FAILED = maxInt(usize); +pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize)); pub const MAP_SHARED = 0x0001; pub const MAP_PRIVATE = 0x0002; pub const MAP_FIXED = 0x0010; @@ -644,29 +644,23 @@ pub const TIOCGPKT = 0x80045438; pub const TIOCGPTLCK = 0x80045439; pub const TIOCGEXCL = 0x80045440; -fn unsigned(s: i32) u32 { - return @bitCast(u32, s); +pub fn WEXITSTATUS(s: u32) u32 { + return (s & 0xff00) >> 8; } -fn signed(s: u32) i32 { - return @bitCast(i32, s); +pub fn WTERMSIG(s: u32) u32 { + return s & 0x7f; } -pub fn WEXITSTATUS(s: i32) i32 { - return signed((unsigned(s) & 0xff00) >> 8); -} -pub fn WTERMSIG(s: i32) i32 { - return signed(unsigned(s) & 0x7f); -} -pub fn WSTOPSIG(s: i32) i32 { +pub fn WSTOPSIG(s: u32) u32 { return WEXITSTATUS(s); } -pub fn WIFEXITED(s: i32) bool { +pub fn WIFEXITED(s: u32) bool { return WTERMSIG(s) == 0; } -pub fn WIFSTOPPED(s: i32) bool { - return @intCast(u16, (((unsigned(s) & 0xffff) *% 0x10001) >> 8)) > 0x7f00; +pub fn WIFSTOPPED(s: u32) bool { + return @intCast(u16, (((s & 0xffff) *% 0x10001) >> 8)) > 0x7f00; } -pub fn WIFSIGNALED(s: i32) bool { - return (unsigned(s) & 0xffff) -% 1 < 0xff; +pub fn WIFSIGNALED(s: u32) bool { + return (s & 0xffff) -% 1 < 0xff; } pub const winsize = extern struct { diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index 5473299488..6532e72362 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -1,5 +1,7 @@ +const builtin = @import("builtin"); const std = @import("../../std.zig"); const maxInt = std.math.maxInt; +use @import("../bits.zig"); pub use @import("linux/errno.zig"); pub use switch (builtin.arch) { @@ -661,29 +663,23 @@ pub const TFD_CLOEXEC = O_CLOEXEC; pub const TFD_TIMER_ABSTIME = 1; pub const TFD_TIMER_CANCEL_ON_SET = (1 << 1); -fn unsigned(s: i32) u32 { - return @bitCast(u32, s); +pub fn WEXITSTATUS(s: u32) u32 { + return (s & 0xff00) >> 8; } -fn signed(s: u32) i32 { - return @bitCast(i32, s); +pub fn WTERMSIG(s: u32) u32 { + return s & 0x7f; } -pub fn WEXITSTATUS(s: i32) i32 { - return signed((unsigned(s) & 0xff00) >> 8); -} -pub fn WTERMSIG(s: i32) i32 { - return signed(unsigned(s) & 0x7f); -} -pub fn WSTOPSIG(s: i32) i32 { +pub fn WSTOPSIG(s: u32) u32 { return WEXITSTATUS(s); } -pub fn WIFEXITED(s: i32) bool { +pub fn WIFEXITED(s: u32) bool { return WTERMSIG(s) == 0; } -pub fn WIFSTOPPED(s: i32) bool { - return @intCast(u16, ((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00; +pub fn WIFSTOPPED(s: u32) bool { + return @intCast(u16, ((s & 0xffff) *% 0x10001) >> 8) > 0x7f00; } -pub fn WIFSIGNALED(s: i32) bool { - return (unsigned(s) & 0xffff) -% 1 < 0xff; +pub fn WIFSIGNALED(s: u32) bool { + return (s & 0xffff) -% 1 < 0xff; } pub const winsize = extern struct { @@ -902,7 +898,7 @@ pub const dirent64 = extern struct { pub const dl_phdr_info = extern struct { dlpi_addr: usize, dlpi_name: ?[*]const u8, - dlpi_phdr: [*]elf.Phdr, + dlpi_phdr: [*]std.elf.Phdr, dlpi_phnum: u16, }; diff --git a/std/os/bits/netbsd.zig b/std/os/bits/netbsd.zig index 4a9cf0391c..fc4c2904e0 100644 --- a/std/os/bits/netbsd.zig +++ b/std/os/bits/netbsd.zig @@ -152,7 +152,7 @@ pub const CLOCK_MONOTONIC = 3; pub const CLOCK_THREAD_CPUTIME_ID = 0x20000000; pub const CLOCK_PROCESS_CPUTIME_ID = 0x40000000; -pub const MAP_FAILED = maxInt(usize); +pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize)); pub const MAP_SHARED = 0x0001; pub const MAP_PRIVATE = 0x0002; pub const MAP_REMAPDUP = 0x0004; @@ -516,34 +516,28 @@ pub const TIOCSWINSZ = 0x80087467; pub const TIOCUCNTL = 0x80047466; pub const TIOCXMTFRAME = 0x80087444; -fn unsigned(s: i32) u32 { - return @bitCast(u32, s); +pub fn WEXITSTATUS(s: u32) u32 { + return (s >> 8) & 0xff; } -fn signed(s: u32) i32 { - return @bitCast(i32, s); +pub fn WTERMSIG(s: u32) u32 { + return s & 0x7f; } -pub fn WEXITSTATUS(s: i32) i32 { - return signed((unsigned(s) >> 8) & 0xff); -} -pub fn WTERMSIG(s: i32) i32 { - return signed(unsigned(s) & 0x7f); -} -pub fn WSTOPSIG(s: i32) i32 { +pub fn WSTOPSIG(s: u32) u32 { return WEXITSTATUS(s); } -pub fn WIFEXITED(s: i32) bool { +pub fn WIFEXITED(s: u32) bool { return WTERMSIG(s) == 0; } -pub fn WIFCONTINUED(s: i32) bool { +pub fn WIFCONTINUED(s: u32) bool { return ((s & 0x7f) == 0xffff); } -pub fn WIFSTOPPED(s: i32) bool { +pub fn WIFSTOPPED(s: u32) bool { return ((s & 0x7f != 0x7f) and !WIFCONTINUED(s)); } -pub fn WIFSIGNALED(s: i32) bool { +pub fn WIFSIGNALED(s: u32) bool { return !WIFSTOPPED(s) and !WIFCONTINUED(s) and !WIFEXITED(s); } diff --git a/std/os/linux.zig b/std/os/linux.zig index 4b37399079..282aa19bf1 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -382,7 +382,7 @@ pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags); } -pub fn waitpid(pid: i32, status: *i32, flags: u32) usize { +pub fn waitpid(pid: i32, status: *u32, flags: u32) usize { return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), flags, 0); } diff --git a/std/os/linux/test.zig b/std/os/linux/test.zig index 1949d00298..c78b071c74 100644 --- a/std/os/linux/test.zig +++ b/std/os/linux/test.zig @@ -11,7 +11,7 @@ test "getpid" { test "timer" { const epoll_fd = linux.epoll_create(); - var err = linux.getErrno(epoll_fd); + var err: usize = linux.getErrno(epoll_fd); expect(err == 0); const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0); diff --git a/std/os/linux/vdso.zig b/std/os/linux/vdso.zig index 44cd0271cd..86d54bfbf8 100644 --- a/std/os/linux/vdso.zig +++ b/std/os/linux/vdso.zig @@ -5,7 +5,7 @@ const mem = std.mem; const maxInt = std.math.maxInt; pub fn lookup(vername: []const u8, name: []const u8) usize { - const vdso_addr = std.os.linuxGetAuxVal(std.elf.AT_SYSINFO_EHDR); + const vdso_addr = std.os.system.getauxval(std.elf.AT_SYSINFO_EHDR); if (vdso_addr == 0) return 0; const eh = @intToPtr(*elf.Ehdr, vdso_addr); diff --git a/std/os/test.zig b/std/os/test.zig index f7d40e1f03..d4d662e97f 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -15,11 +15,11 @@ const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; test "makePath, put some files in it, deleteTree" { - try os.makePath(a, "os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); + try fs.makePath(a, "os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense"); try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah"); - try os.deleteTree(a, "os_test_tmp"); - if (os.Dir.open(a, "os_test_tmp")) |dir| { + try fs.deleteTree(a, "os_test_tmp"); + if (fs.Dir.open(a, "os_test_tmp")) |dir| { @panic("expected error"); } else |err| { expect(err == error.FileNotFound); @@ -27,7 +27,7 @@ test "makePath, put some files in it, deleteTree" { } test "access file" { - try os.makePath(a, "os_test_tmp"); + try fs.makePath(a, "os_test_tmp"); if (File.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt")) |ok| { @panic("expected error"); } else |err| { @@ -35,8 +35,8 @@ test "access file" { } try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", ""); - try File.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt"); - try os.deleteTree(a, "os_test_tmp"); + try os.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", os.F_OK); + try fs.deleteTree(a, "os_test_tmp"); } fn testThreadIdFn(thread_id: *Thread.Id) void { @@ -52,15 +52,12 @@ test "std.Thread.getCurrentId" { thread.wait(); if (Thread.use_pthreads) { expect(thread_current_id == thread_id); + } else if (os.windows.is_the_target) { + expect(Thread.getCurrentId() != thread_current_id); } else { - switch (builtin.os) { - builtin.Os.windows => expect(Thread.getCurrentId() != thread_current_id), - else => { - // If the thread completes very quickly, then thread_id can be 0. See the - // documentation comments for `std.Thread.handle`. - expect(thread_id == 0 or thread_current_id == thread_id); - }, - } + // If the thread completes very quickly, then thread_id can be 0. See the + // documentation comments for `std.Thread.handle`. + expect(thread_id == 0 or thread_current_id == thread_id); } } @@ -92,7 +89,7 @@ fn start2(ctx: *i32) u8 { } test "cpu count" { - const cpu_count = try std.os.cpuCount(a); + const cpu_count = try Thread.cpuCount(); expect(cpu_count >= 1); } @@ -105,7 +102,7 @@ test "AtomicFile" { \\ this is a test file ; { - var af = try os.AtomicFile.init(test_out_file, File.default_mode); + var af = try fs.AtomicFile.init(test_out_file, File.default_mode); defer af.deinit(); try af.file.write(test_content); try af.finish(); @@ -113,7 +110,7 @@ test "AtomicFile" { const content = try io.readFileAlloc(allocator, test_out_file); expect(mem.eql(u8, content, test_content)); - try os.deleteFile(test_out_file); + try fs.deleteFile(test_out_file); } test "thread local storage" { @@ -145,10 +142,10 @@ test "getrandom" { test "getcwd" { // at least call it so it gets compiled var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; - _ = os.getcwd(&buf) catch {}; + _ = os.getcwd(&buf) catch undefined; } test "realpath" { var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; - testing.expectError(error.FileNotFound, os.realpath("definitely_bogus_does_not_exist1234", &buf)); + testing.expectError(error.FileNotFound, fs.realpath("definitely_bogus_does_not_exist1234", &buf)); } diff --git a/std/os/windows.zig b/std/os/windows.zig index 9bfeee9fe1..acad139955 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -1180,6 +1180,44 @@ pub fn CreateProcessW( } } +pub const LoadLibraryError = error{ + FileNotFound, + Unexpected, +}; + +pub fn LoadLibraryW(lpLibFileName: [*]const u16) LoadLibraryError!HMODULE { + return kernel32.LoadLibraryW(lpLibFileName) orelse { + switch (kernel32.GetLastError()) { + ERROR.FILE_NOT_FOUND => return error.FileNotFound, + ERROR.PATH_NOT_FOUND => return error.FileNotFound, + ERROR.MOD_NOT_FOUND => return error.FileNotFound, + else => |err| return unexpectedError(err), + } + }; +} + +pub fn FreeLibrary(hModule: HMODULE) void { + assert(kernel32.FreeLibrary(hModule) != 0); +} + +pub fn QueryPerformanceFrequency() u64 { + // "On systems that run Windows XP or later, the function will always succeed" + // https://docs.microsoft.com/en-us/windows/desktop/api/profileapi/nf-profileapi-queryperformancefrequency + var result: LARGE_INTEGER = undefined; + assert(kernel32.QueryPerformanceFrequency(&result) != 0); + // The kernel treats this integer as unsigned. + return @bitCast(u64, result); +} + +pub fn QueryPerformanceCounter() u64 { + // "On systems that run Windows XP or later, the function will always succeed" + // https://docs.microsoft.com/en-us/windows/desktop/api/profileapi/nf-profileapi-queryperformancecounter + var result: LARGE_INTEGER = undefined; + assert(kernel32.QueryPerformanceCounter(&result) != 0); + // The kernel treats this integer as unsigned. + return @bitCast(u64, result); +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } diff --git a/std/os/zen.zig b/std/os/zen.zig index 6931b07f78..8d2f963486 100644 --- a/std/os/zen.zig +++ b/std/os/zen.zig @@ -80,7 +80,7 @@ pub const STDOUT_FILENO = 1; pub const STDERR_FILENO = 2; // FIXME: let's borrow Linux's error numbers for now. -use @import("../bits/linux/errno.zig"); +use @import("bits/linux/errno.zig"); // Get the errno from a syscall return value, or 0 for no error. pub fn getErrno(r: usize) usize { const signed_r = @bitCast(isize, r); diff --git a/std/process.zig b/std/process.zig index baccbccb10..b45074d67c 100644 --- a/std/process.zig +++ b/std/process.zig @@ -1,7 +1,9 @@ const builtin = @import("builtin"); const std = @import("std.zig"); const os = std.os; +const fs = std.fs; const BufMap = std.BufMap; +const Buffer = std.Buffer; const mem = std.mem; const math = std.math; const Allocator = mem.Allocator; @@ -13,6 +15,24 @@ pub const exit = os.exit; pub const changeCurDir = os.chdir; pub const changeCurDirC = os.chdirC; +/// The result is a slice of `out_buffer`, from index `0`. +pub fn getCwd(out_buffer: *[fs.MAX_PATH_BYTES]u8) ![]u8 { + return os.getcwd(out_buffer); +} + +/// Caller must free the returned memory. +pub fn getCwdAlloc(allocator: *Allocator) ![]u8 { + var buf: [fs.MAX_PATH_BYTES]u8 = undefined; + return mem.dupe(allocator, u8, try os.getcwd(&buf)); +} + +test "getCwdAlloc" { + // at least call it so it gets compiled + var buf: [1000]u8 = undefined; + const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator; + _ = getCwdAlloc(allocator) catch undefined; +} + /// Caller must free result when done. /// TODO make this go through libc when we have it pub fn getEnvMap(allocator: *Allocator) !BufMap { @@ -402,7 +422,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { var contents = try Buffer.initSize(allocator, 0); defer contents.deinit(); - var slice_list = ArrayList(usize).init(allocator); + var slice_list = std.ArrayList(usize).init(allocator); defer slice_list.deinit(); while (it.next(allocator)) |arg_or_err| { diff --git a/std/thread.zig b/std/thread.zig index 53f42e555f..53be5a8c2d 100644 --- a/std/thread.zig +++ b/std/thread.zig @@ -1,8 +1,10 @@ const builtin = @import("builtin"); const std = @import("std.zig"); const os = std.os; +const mem = std.mem; const windows = std.os.windows; const c = std.c; +const assert = std.debug.assert; pub const Thread = struct { data: Data, @@ -31,14 +33,12 @@ pub const Thread = struct { pub const Data = if (use_pthreads) struct { handle: Thread.Handle, - mmap_addr: usize, - mmap_len: usize, + memory: []align(mem.page_size) u8, } else switch (builtin.os) { .linux => struct { handle: Thread.Handle, - mmap_addr: usize, - mmap_len: usize, + memory: []align(mem.page_size) u8, }, .windows => struct { handle: Thread.Handle, @@ -56,7 +56,7 @@ pub const Thread = struct { return c.pthread_self(); } else return switch (builtin.os) { - .linux => linux.gettid(), + .linux => os.linux.gettid(), .windows => windows.GetCurrentThreadId(), else => @compileError("Unsupported OS"), }; @@ -82,21 +82,21 @@ pub const Thread = struct { os.EDEADLK => unreachable, else => unreachable, } - os.munmap(self.data.mmap_addr, self.data.mmap_len); + os.munmap(self.data.memory); } else switch (builtin.os) { .linux => { while (true) { const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst); if (pid_value == 0) break; - const rc = linux.futex_wait(&self.data.handle, linux.FUTEX_WAIT, pid_value, null); - switch (linux.getErrno(rc)) { + const rc = os.linux.futex_wait(&self.data.handle, os.linux.FUTEX_WAIT, pid_value, null); + switch (os.linux.getErrno(rc)) { 0 => continue, os.EINTR => continue, os.EAGAIN => continue, else => unreachable, } } - os.munmap(self.data.mmap_addr, self.data.mmap_len); + os.munmap(self.data.memory); }, .windows => { assert(windows.WaitForSingleObject(self.data.handle, windows.INFINITE) == windows.WAIT_OBJECT_0); @@ -130,6 +130,10 @@ pub const Thread = struct { /// Not enough userland memory to spawn the thread. OutOfMemory, + /// `mlockall` is enabled, and the memory needed to spawn the thread + /// would exceed the limit. + LockedMemoryLimitExceeded, + Unexpected, }; @@ -219,7 +223,7 @@ pub const Thread = struct { } }; - const MAP_GROWSDOWN = if (builtin.os == .linux) linux.MAP_GROWSDOWN else 0; + const MAP_GROWSDOWN = if (os.linux.is_the_target) os.linux.MAP_GROWSDOWN else 0; var stack_end_offset: usize = undefined; var thread_start_offset: usize = undefined; @@ -241,7 +245,7 @@ pub const Thread = struct { } // Finally, the Thread Local Storage, if any. if (!Thread.use_pthreads) { - if (linux.tls.tls_image) |tls_img| { + if (os.linux.tls.tls_image) |tls_img| { l = mem.alignForward(l, @alignOf(usize)); tls_start_offset = l; l += tls_img.alloc_size; @@ -249,12 +253,24 @@ pub const Thread = struct { } break :blk l; }; - const mmap_addr = try os.mmap(null, mmap_len, os.PROT_READ | os.PROT_WRITE, os.MAP_PRIVATE | os.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); - errdefer os.munmap(mmap_addr, mmap_len); + const mmap_slice = os.mmap( + null, + mem.alignForward(mmap_len, mem.page_size), + os.PROT_READ | os.PROT_WRITE, + os.MAP_PRIVATE | os.MAP_ANONYMOUS | MAP_GROWSDOWN, + -1, + 0, + ) catch |err| switch (err) { + error.MemoryMappingNotSupported => unreachable, // no file descriptor + error.AccessDenied => unreachable, // no file descriptor + error.PermissionDenied => unreachable, // no file descriptor + else => |e| return e, + }; + errdefer os.munmap(mmap_slice); + const mmap_addr = @ptrToInt(mmap_slice.ptr); const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(*Thread, mmap_addr + thread_start_offset)); - thread_ptr.data.mmap_addr = mmap_addr; - thread_ptr.data.mmap_len = mmap_len; + thread_ptr.data.memory = mmap_slice; var arg: usize = undefined; if (@sizeOf(Context) != 0) { @@ -269,7 +285,7 @@ pub const Thread = struct { if (c.pthread_attr_init(&attr) != 0) return error.SystemResources; defer assert(c.pthread_attr_destroy(&attr) == 0); - assert(c.pthread_attr_setstack(&attr, @intToPtr(*c_void, mmap_addr), stack_end_offset) == 0); + assert(c.pthread_attr_setstack(&attr, mmap_slice.ptr, stack_end_offset) == 0); const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(*c_void, arg)); switch (err) { @@ -279,13 +295,13 @@ pub const Thread = struct { os.EINVAL => unreachable, else => return os.unexpectedErrno(@intCast(usize, err)), } - } else if (builtin.os == .linux) { + } else if (os.linux.is_the_target) { var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND | os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID | os.CLONE_DETACHED; var newtls: usize = undefined; - if (linux.tls.tls_image) |tls_img| { - newtls = linux.tls.copyTLS(mmap_addr + tls_start_offset); + if (os.linux.tls.tls_image) |tls_img| { + newtls = os.linux.tls.copyTLS(mmap_addr + tls_start_offset); flags |= os.CLONE_SETTLS; } const rc = os.linux.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle); @@ -313,7 +329,7 @@ pub const Thread = struct { pub fn cpuCount() CpuCountError!usize { if (os.linux.is_the_target) { const cpu_set = try os.sched_getaffinity(0); - return os.CPU_COUNT(cpu_set); + return usize(os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast } if (os.windows.is_the_target) { var system_info: windows.SYSTEM_INFO = undefined; diff --git a/std/time.zig b/std/time.zig index 1483a0a132..d4a349880e 100644 --- a/std/time.zig +++ b/std/time.zig @@ -95,7 +95,7 @@ pub const Timer = struct { /// be less precise frequency: switch (builtin.os) { .windows => u64, - .macosx, .ios, .tvos, .watchos => darwin.mach_timebase_info_data, + .macosx, .ios, .tvos, .watchos => os.darwin.mach_timebase_info_data, else => void, }, resolution: u64, @@ -119,20 +119,13 @@ pub const Timer = struct { var self: Timer = undefined; if (os.windows.is_the_target) { - var freq: i64 = undefined; - var err = windows.QueryPerformanceFrequency(&freq); - if (err == windows.FALSE) return error.TimerUnsupported; - self.frequency = @intCast(u64, freq); + self.frequency = os.windows.QueryPerformanceFrequency(); self.resolution = @divFloor(ns_per_s, self.frequency); - - var start_time: i64 = undefined; - err = windows.QueryPerformanceCounter(&start_time); - assert(err != windows.FALSE); - self.start_time = @intCast(u64, start_time); + self.start_time = os.windows.QueryPerformanceCounter(); } else if (os.darwin.is_the_target) { - darwin.mach_timebase_info(&self.frequency); + os.darwin.mach_timebase_info(&self.frequency); self.resolution = @divFloor(self.frequency.numer, self.frequency.denom); - self.start_time = darwin.mach_absolute_time(); + self.start_time = os.darwin.mach_absolute_time(); } else { //On Linux, seccomp can do arbitrary things to our ability to call // syscalls, including return any errno value it wants and @@ -177,13 +170,10 @@ pub const Timer = struct { fn clockNative() u64 { if (os.windows.is_the_target) { - var result: i64 = undefined; - var err = windows.QueryPerformanceCounter(&result); - assert(err != windows.FALSE); - return @intCast(u64, result); + return os.windows.QueryPerformanceCounter(); } if (os.darwin.is_the_target) { - return darwin.mach_absolute_time(); + return os.darwin.mach_absolute_time(); } var ts: os.timespec = undefined; os.clock_gettime(monotonic_clock_id, &ts) catch unreachable; diff --git a/test/compare_output.zig b/test/compare_output.zig index 72f4e223aa..ee84e7caa7 100644 --- a/test/compare_output.zig +++ b/test/compare_output.zig @@ -377,7 +377,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { \\ stdout.print("before\n") catch unreachable; \\ defer stdout.print("defer1\n") catch unreachable; \\ defer stdout.print("defer2\n") catch unreachable; - \\ var args_it = @import("std").os.args(); + \\ var args_it = @import("std").process.args(); \\ if (args_it.skip() and !args_it.skip()) return; \\ defer stdout.print("defer3\n") catch unreachable; \\ stdout.print("after\n") catch unreachable; @@ -444,7 +444,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { \\const allocator = std.debug.global_allocator; \\ \\pub fn main() !void { - \\ var args_it = os.args(); + \\ var args_it = std.process.args(); \\ var stdout_file = try io.getStdOut(); \\ var stdout_adapter = stdout_file.outStream(); \\ const stdout = &stdout_adapter.stream; @@ -485,7 +485,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { \\const allocator = std.debug.global_allocator; \\ \\pub fn main() !void { - \\ var args_it = os.args(); + \\ var args_it = std.process.args(); \\ var stdout_file = try io.getStdOut(); \\ var stdout_adapter = stdout_file.outStream(); \\ const stdout = &stdout_adapter.stream; diff --git a/test/standalone/empty_env/main.zig b/test/standalone/empty_env/main.zig index 20b45c3137..d6d5ecb5af 100644 --- a/test/standalone/empty_env/main.zig +++ b/test/standalone/empty_env/main.zig @@ -1,6 +1,6 @@ const std = @import("std"); pub fn main() void { - const env_map = std.os.getEnvMap(std.debug.global_allocator) catch @panic("unable to get env map"); + const env_map = std.process.getEnvMap(std.debug.global_allocator) catch @panic("unable to get env map"); std.testing.expect(env_map.count() == 0); } diff --git a/test/tests.zig b/test/tests.zig index 9bd9292b84..030864aee0 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -393,7 +393,7 @@ pub const CompareOutputContext = struct { debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); }; - const expected_exit_code: i32 = 126; + const expected_exit_code: u32 = 126; switch (term) { .Exited => |code| { if (code != expected_exit_code) { -- cgit v1.2.3