Commit 399e026cc0

LemonBoy <thatlemon@gmail.com>
2019-05-29 09:43:39
Add sigaltstack wrapper in os.zig
1 parent bcdbd8d
Changed files (4)
std/os/linux/test.zig
@@ -83,12 +83,3 @@ test "dl_iterate_phdr" {
     expect(linux.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
     expect(counter != 0);
 }
-
-test "sigaltstack" {
-    var st: linux.stack_t = undefined;
-    expect(linux.sigaltstack(null, &st) == 0);
-    // Setting a stack size less than MINSIGSTKSZ returns ENOMEM
-    st.ss_flags = 0;
-    st.ss_size = 1;
-    expect(linux.getErrno(linux.sigaltstack(&st, null)) == linux.ENOMEM);
-}
std/os/test.zig
@@ -149,3 +149,14 @@ test "realpath" {
     var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
     testing.expectError(error.FileNotFound, fs.realpath("definitely_bogus_does_not_exist1234", &buf));
 }
+
+test "sigaltstack" {
+    if (builtin.os == .windows or builtin.os == .wasi) return error.SkipZigTest;
+
+    var st: os.stack_t = undefined;
+    try os.sigaltstack(null, &st);
+    // Setting a stack size less than MINSIGSTKSZ returns ENOMEM
+    st.ss_flags = 0;
+    st.ss_size = 1;
+    testing.expectError(error.SizeTooSmall, os.sigaltstack(&st, null));
+}
std/os/uefi.zig
@@ -1,2 +1,6 @@
 // TODO this is where the extern declarations go. For example, see
 // inc/efilib.h in gnu-efi-code
+
+const builtin = @import("builtin");
+
+pub const is_the_target = builtin.os == .uefi;
std/os.zig
@@ -2433,6 +2433,29 @@ pub fn unexpectedErrno(err: usize) UnexpectedError {
     return error.Unexpected;
 }
 
+pub const SigaltstackError = error{
+    /// The supplied stack size was less than MINSIGSTKSZ.
+    SizeTooSmall,
+
+    /// Attempted to change the signal stack while it was active.
+    PermissionDenied,
+    Unexpected,
+};
+
+pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
+    if (windows.is_the_target or uefi.is_the_target or wasi.is_the_target)
+        @compileError("std.os.sigaltstack not available for this target");
+
+    switch (errno(system.sigaltstack(ss, old_ss))) {
+        0 => return,
+        EFAULT => unreachable,
+        EINVAL => unreachable,
+        ENOMEM => return error.SizeTooSmall,
+        EPERM => return error.PermissionDenied,
+        else => |err| return unexpectedErrno(err),
+    }
+}
+
 test "" {
     _ = @import("os/darwin.zig");
     _ = @import("os/freebsd.zig");