Commit 06435535d3

Andrew Kelley <andrew@ziglang.org>
2019-05-27 20:12:50
fixes for darwin
1 parent f1610f6
std/event/loop.zig
@@ -222,10 +222,10 @@ pub const Loop = struct {
                 }
             },
             .macosx, .freebsd, .netbsd => {
-                self.os_data.kqfd = try os.bsdKQueue();
+                self.os_data.kqfd = try os.kqueue();
                 errdefer os.close(self.os_data.kqfd);
 
-                self.os_data.fs_kqfd = try os.bsdKQueue();
+                self.os_data.fs_kqfd = try os.kqueue();
                 errdefer os.close(self.os_data.fs_kqfd);
 
                 self.os_data.fs_queue = std.atomic.Queue(fs.Request).init();
@@ -264,7 +264,7 @@ pub const Loop = struct {
                     };
                     self.available_eventfd_resume_nodes.push(eventfd_node);
                     const kevent_array = (*const [1]os.Kevent)(&eventfd_node.data.kevent);
-                    _ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null);
+                    _ = try os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null);
                     eventfd_node.data.kevent.flags = os.EV_CLEAR | os.EV_ENABLE;
                     eventfd_node.data.kevent.fflags = os.NOTE_TRIGGER;
                 }
@@ -280,7 +280,7 @@ pub const Loop = struct {
                     .udata = @ptrToInt(&self.final_resume_node),
                 };
                 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);
+                _ = try os.kevent(self.os_data.kqfd, final_kev_arr, empty_kevs, null);
                 self.os_data.final_kevent.flags = os.EV_ENABLE;
                 self.os_data.final_kevent.fflags = os.NOTE_TRIGGER;
 
@@ -315,7 +315,7 @@ pub const Loop = struct {
 
                 var extra_thread_index: usize = 0;
                 errdefer {
-                    _ = os.bsdKEvent(self.os_data.kqfd, final_kev_arr, empty_kevs, null) catch unreachable;
+                    _ = os.kevent(self.os_data.kqfd, final_kev_arr, empty_kevs, null) catch unreachable;
                     while (extra_thread_index != 0) {
                         extra_thread_index -= 1;
                         self.extra_threads[extra_thread_index].wait();
@@ -474,7 +474,7 @@ pub const Loop = struct {
         };
         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);
+        _ = try os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null);
     }
 
     pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void {
@@ -488,7 +488,7 @@ pub const Loop = struct {
         };
         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;
+        _ = os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined;
         self.finishOneEvent();
     }
 
@@ -504,7 +504,7 @@ pub const Loop = struct {
                 .macosx, .freebsd, .netbsd => {
                     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 {
+                    _ = os.kevent(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);
                         return;
@@ -634,7 +634,7 @@ pub const Loop = struct {
                     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;
+                    _ = os.kevent(self.os_data.kqfd, final_kevent, empty_kevs, null) catch unreachable;
                     return;
                 },
                 .windows => {
@@ -690,7 +690,7 @@ pub const Loop = struct {
                 .macosx, .freebsd, .netbsd => {
                     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;
+                    const count = os.kevent(self.os_data.kqfd, empty_kevs, eventlist[0..], null) catch unreachable;
                     for (eventlist[0..count]) |ev| {
                         const resume_node = @intToPtr(*ResumeNode, ev.udata);
                         const handle = resume_node.handle;
@@ -753,7 +753,7 @@ pub const Loop = struct {
             builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
                 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;
+                _ = os.kevent(self.os_data.fs_kqfd, fs_kevs, empty_kevs, null) catch unreachable;
             },
             builtin.Os.linux => {
                 _ = @atomicRmw(i32, &self.os_data.fs_queue_item, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
@@ -823,7 +823,7 @@ pub const Loop = struct {
                 builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
                     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;
+                    _ = os.kevent(self.os_data.fs_kqfd, fs_kevs, out_kevs[0..], null) catch unreachable;
                 },
                 else => @compileError("Unsupported OS"),
             }
std/os/bits/darwin.zig
@@ -1,32 +1,10 @@
 const std = @import("../../std.zig");
 const assert = std.debug.assert;
+const maxInt = std.math.maxInt;
 
 pub const fd_t = c_int;
 pub const pid_t = c_int;
 
-pub fn sigaction(sig: u5, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize {
-    assert(sig != SIGKILL);
-    assert(sig != SIGSTOP);
-    var cact = c.Sigaction{
-        .handler = @ptrCast(extern fn (c_int) void, act.handler),
-        .sa_flags = @bitCast(c_int, act.flags),
-        .sa_mask = act.mask,
-    };
-    var coact: c.Sigaction = undefined;
-    const result = errnoWrap(c.sigaction(sig, &cact, &coact));
-    if (result != 0) {
-        return result;
-    }
-    if (oact) |old| {
-        old.* = Sigaction{
-            .handler = @ptrCast(extern fn (i32) void, coact.handler),
-            .flags = @bitCast(u32, coact.sa_flags),
-            .mask = coact.sa_mask,
-        };
-    }
-    return result;
-}
-
 pub const in_port_t = u16;
 pub const sa_family_t = u8;
 pub const socklen_t = u32;
std/os/bits/windows.zig
@@ -159,5 +159,4 @@ pub const EWOULDBLOCK = 140;
 // for if branches that aren't taken.
 pub const SIGKILL = @compileError("Windows libc does not have this");
 pub const EDQUOT = @compileError("Windows libc does not have this");
-pub const TIOCGWINSZ = @compileError("Windows libc does not have this");
 pub const F_OK = 0;
std/child_process.zig
@@ -47,7 +47,7 @@ pub const ChildProcess = struct {
     /// Set to change the current working directory when spawning the child process.
     pub cwd: ?[]const u8,
 
-    err_pipe: if (os.windows.is_the_target) void else [2]i32,
+    err_pipe: if (os.windows.is_the_target) void else [2]os.fd_t,
     llnode: if (os.windows.is_the_target) void else LinkedList(*ChildProcess).Node,
 
     pub const SpawnError = error{OutOfMemory} || os.ExecveError || os.SetIdError ||
@@ -701,7 +701,7 @@ fn windowsMakePipeOut(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *const
     wr.* = wr_h;
 }
 
-fn destroyPipe(pipe: [2]i32) void {
+fn destroyPipe(pipe: [2]os.fd_t) void {
     os.close(pipe[0]);
     os.close(pipe[1]);
 }
std/fs.zig
@@ -509,7 +509,7 @@ pub const Dir = struct {
                         }
                     }
                     self.handle.index = 0;
-                    self.handle.end_index = @intCast(usize, result);
+                    self.handle.end_index = @intCast(usize, rc);
                     break;
                 }
             }
@@ -694,7 +694,7 @@ pub fn readLinkC(pathname: [*]const u8, buffer: *[os.PATH_MAX]u8) ![]u8 {
     return os.readlinkC(pathname, buffer);
 }
 
-pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError;
+pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError || SelfExePathError;
 
 pub fn openSelfExe() OpenSelfExeError!File {
     if (os.linux.is_the_target) {
@@ -730,10 +730,10 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;
 /// On Linux, depends on procfs being mounted. If the currently executing binary has
 /// been deleted, the file path looks something like `/a/b/c/exe (deleted)`.
 /// TODO make the return type of this a null terminated pointer
-pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
+pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
     if (os.darwin.is_the_target) {
         var u32_len: u32 = out_buffer.len;
-        const rc = c._NSGetExecutablePath(out_buffer, &u32_len);
+        const rc = std.c._NSGetExecutablePath(out_buffer, &u32_len);
         if (rc != 0) return error.NameTooLong;
         return mem.toSlice(u8, out_buffer);
     }
@@ -765,13 +765,13 @@ pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
 }
 
 /// Same as `selfExePath` except the result is UTF16LE-encoded.
-pub fn selfExePathW(out_buffer: *[os.windows.PATH_MAX_WIDE]u16) ![]u16 {
+pub fn selfExePathW(out_buffer: *[os.windows.PATH_MAX_WIDE]u16) SelfExePathError![]u16 {
     return os.windows.GetModuleFileNameW(null, out_buffer, out_buffer.len);
 }
 
 /// `selfExeDirPath` except allocates the result on the heap.
 /// Caller owns returned memory.
-pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 {
+pub fn selfExeDirPathAlloc(allocator: *Allocator) SelfExePathError![]u8 {
     var buf: [MAX_PATH_BYTES]u8 = undefined;
     return mem.dupe(allocator, u8, try selfExeDirPath(&buf));
 }
std/os.zig
@@ -297,8 +297,9 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) ReadError!usize {
             const err = darwin.getErrno(rc);
             switch (err) {
                 0 => {
-                    off += rc;
-                    inner_off += rc;
+                    const amt_read = @bitCast(usize, rc);
+                    off += amt_read;
+                    inner_off += amt_read;
                     if (inner_off == v.iov_len) {
                         iov_i += 1;
                         inner_off = 0;
@@ -421,8 +422,9 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) WriteError!void
             const err = darwin.getErrno(rc);
             switch (err) {
                 0 => {
-                    off += rc;
-                    inner_off += rc;
+                    const amt_written = @bitCast(usize, rc);
+                    off += amt_written;
+                    inner_off += amt_written;
                     if (inner_off == v.iov_len) {
                         iov_i += 1;
                         inner_off = 0;
@@ -1205,8 +1207,8 @@ pub fn isatty(handle: fd_t) bool {
         @compileError("TODO implement std.os.isatty for WASI");
     }
     if (linux.is_the_target) {
-        var wsz: system.winsize = undefined;
-        return system.syscall3(system.SYS_ioctl, @bitCast(usize, isize(handle)), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
+        var wsz: linux.winsize = undefined;
+        return linux.syscall3(linux.SYS_ioctl, @bitCast(usize, isize(handle)), linux.TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
     }
     unreachable;
 }
@@ -1778,6 +1780,10 @@ pub const KEventError = error{
 
     /// The specified process to attach to does not exist.
     ProcessNotFound,
+
+    /// changelist or eventlist had too many items on it.
+    /// TODO remove this possibility
+    Overflow,
 };
 
 pub fn kevent(
@@ -1787,9 +1793,16 @@ pub fn kevent(
     timeout: ?*const timespec,
 ) KEventError!usize {
     while (true) {
-        const rc = system.kevent(kq, changelist, eventlist, timeout);
+        const rc = system.kevent(
+            kq,
+            changelist.ptr,
+            try math.cast(c_int, changelist.len),
+            eventlist.ptr,
+            try math.cast(c_int, eventlist.len),
+            timeout,
+        );
         switch (errno(rc)) {
-            0 => return rc,
+            0 => return @intCast(usize, rc),
             EACCES => return error.AccessDenied,
             EFAULT => unreachable,
             EBADF => unreachable, // Always a race condition.
@@ -2028,7 +2041,7 @@ pub const PipeError = error{
 
 /// Creates a unidirectional data channel that can be used for interprocess communication.
 pub fn pipe() PipeError![2]fd_t {
-    var fds: [2]i32 = undefined;
+    var fds: [2]fd_t = undefined;
     switch (errno(system.pipe(&fds))) {
         0 => return fds,
         EINVAL => unreachable, // Invalid parameters to pipe()
@@ -2040,7 +2053,7 @@ pub fn pipe() PipeError![2]fd_t {
 }
 
 pub fn pipe2(flags: u32) PipeError![2]fd_t {
-    var fds: [2]i32 = undefined;
+    var fds: [2]fd_t = undefined;
     switch (errno(system.pipe2(&fds, flags))) {
         0 => return fds,
         EINVAL => unreachable, // Invalid flags
std/thread.zig
@@ -323,6 +323,7 @@ pub const Thread = struct {
     pub const CpuCountError = error{
         OutOfMemory,
         PermissionDenied,
+        SystemResources,
         Unexpected,
     };
 
@@ -339,7 +340,7 @@ pub const Thread = struct {
         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);
+        try os.sysctlbynameC(name, @ptrCast(*c_void, &count), &count_len, null, 0);
         return @intCast(usize, count);
     }
 };