authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-29 09:43:39+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-29 22:38:14+02:00
log399e026cc00af430df66d3d4b77a12fd775973cd
tree1d39ac9f12c3b0f752317a936f990eb902555da2
parentbcdbd8d1697725992cb08eb1c3c59bd63fda6dc7

Add sigaltstack wrapper in os.zig


4 files changed, 38 insertions(+), 9 deletions(-)

std/os.zig+23
......@@ -2433,6 +2433,29 @@ pub fn unexpectedErrno(err: usize) UnexpectedError {
24332433 return error.Unexpected;
24342434}
24352435
2436pub const SigaltstackError = error{
2437 /// The supplied stack size was less than MINSIGSTKSZ.
2438 SizeTooSmall,
2439
2440 /// Attempted to change the signal stack while it was active.
2441 PermissionDenied,
2442 Unexpected,
2443};
2444
2445pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
2446 if (windows.is_the_target or uefi.is_the_target or wasi.is_the_target)
2447 @compileError("std.os.sigaltstack not available for this target");
2448
2449 switch (errno(system.sigaltstack(ss, old_ss))) {
2450 0 => return,
2451 EFAULT => unreachable,
2452 EINVAL => unreachable,
2453 ENOMEM => return error.SizeTooSmall,
2454 EPERM => return error.PermissionDenied,
2455 else => |err| return unexpectedErrno(err),
2456 }
2457}
2458
24362459test "" {
24372460 _ = @import("os/darwin.zig");
24382461 _ = @import("os/freebsd.zig");
std/os/linux/test.zig-9
......@@ -83,12 +83,3 @@ test "dl_iterate_phdr" {
8383 expect(linux.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
8484 expect(counter != 0);
8585}
86
87test "sigaltstack" {
88 var st: linux.stack_t = undefined;
89 expect(linux.sigaltstack(null, &st) == 0);
90 // Setting a stack size less than MINSIGSTKSZ returns ENOMEM
91 st.ss_flags = 0;
92 st.ss_size = 1;
93 expect(linux.getErrno(linux.sigaltstack(&st, null)) == linux.ENOMEM);
94}
std/os/test.zig+11
......@@ -149,3 +149,14 @@ test "realpath" {
149149 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
150150 testing.expectError(error.FileNotFound, fs.realpath("definitely_bogus_does_not_exist1234", &buf));
151151}
152
153test "sigaltstack" {
154 if (builtin.os == .windows or builtin.os == .wasi) return error.SkipZigTest;
155
156 var st: os.stack_t = undefined;
157 try os.sigaltstack(null, &st);
158 // Setting a stack size less than MINSIGSTKSZ returns ENOMEM
159 st.ss_flags = 0;
160 st.ss_size = 1;
161 testing.expectError(error.SizeTooSmall, os.sigaltstack(&st, null));
162}
std/os/uefi.zig+4
......@@ -1,2 +1,6 @@
11// TODO this is where the extern declarations go. For example, see
22// inc/efilib.h in gnu-efi-code
3
4const builtin = @import("builtin");
5
6pub const is_the_target = builtin.os == .uefi;