authorgravatar for ryanleeschneider@gmail.comRyan Schneider <ryanleeschneider@gmail.com> 2022-11-03 08:43:56-07:00
committergravatar for ryanleeschneider@gmail.comRyan Schneider <ryanleeschneider@gmail.com> 2022-11-04 08:07:44-07:00
loga68b27c252669f3a42b8f789359893ee82cd0aad
treeeed32cd9beb19e5f1772756d5f03238d73877b9c
parent7f1f2e653d6f3cd541e92727f2001596935f3c62

std.os: improve sigaction test coverage.


1 files changed, 24 insertions(+), 10 deletions(-)

lib/std/os/test.zig+24-10
...@@ -739,8 +739,6 @@ test "shutdown socket" {...@@ -739,8 +739,6 @@ test "shutdown socket" {
739 os.closeSocket(sock);739 os.closeSocket(sock);
740}740}
741741
742var signal_test_handler_called_count: u32 = 0;
743
744test "sigaction" {742test "sigaction" {
745 if (native_os == .wasi or native_os == .windows)743 if (native_os == .wasi or native_os == .windows)
746 return error.SkipZigTest;744 return error.SkipZigTest;
...@@ -750,17 +748,19 @@ test "sigaction" {...@@ -750,17 +748,19 @@ test "sigaction" {
750 return error.SkipZigTest;748 return error.SkipZigTest;
751749
752 const S = struct {750 const S = struct {
751 var handler_called_count: u32 = 0;
752
753 fn handler(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const anyopaque) callconv(.C) void {753 fn handler(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const anyopaque) callconv(.C) void {
754 _ = ctx_ptr;754 _ = ctx_ptr;
755 // Check that we received the correct signal.755 // Check that we received the correct signal.
756 switch (native_os) {756 switch (native_os) {
757 .netbsd => {757 .netbsd => {
758 if (sig == os.SIG.USR1 and sig == info.info.signo)758 if (sig == os.SIG.USR1 and sig == info.info.signo)
759 signal_test_handler_called_count += 1;759 handler_called_count += 1;
760 },760 },
761 else => {761 else => {
762 if (sig == os.SIG.USR1 and sig == info.signo)762 if (sig == os.SIG.USR1 and sig == info.signo)
763 signal_test_handler_called_count += 1;763 handler_called_count += 1;
764 },764 },
765 }765 }
766 }766 }
...@@ -774,25 +774,39 @@ test "sigaction" {...@@ -774,25 +774,39 @@ test "sigaction" {
774 .flags = os.SA.SIGINFO | os.SA.RESETHAND,774 .flags = os.SA.SIGINFO | os.SA.RESETHAND,
775 };775 };
776 var old_sa: os.Sigaction = undefined;776 var old_sa: os.Sigaction = undefined;
777
777 // Install the new signal handler.778 // Install the new signal handler.
778 try os.sigaction(os.SIG.USR1, &sa, null);779 try os.sigaction(os.SIG.USR1, &sa, null);
780
779 // Check that we can read it back correctly.781 // Check that we can read it back correctly.
780 try os.sigaction(os.SIG.USR1, null, &old_sa);782 try os.sigaction(os.SIG.USR1, null, &old_sa);
781 try testing.expectEqual(actual_handler, old_sa.handler.sigaction.?);783 try testing.expectEqual(actual_handler, old_sa.handler.sigaction.?);
782 try testing.expect((old_sa.flags & os.SA.SIGINFO) != 0);784 try testing.expect((old_sa.flags & os.SA.SIGINFO) != 0);
785
783 // Invoke the handler.786 // Invoke the handler.
784 try os.raise(os.SIG.USR1);787 try os.raise(os.SIG.USR1);
785 try testing.expect(signal_test_handler_called_count == 1);788 try testing.expect(S.handler_called_count == 1);
786 // Check if the handler has been correctly reset to SIG_DFL789
790 // Check if passing RESETHAND correctly reset the handler to SIG_DFL
787 try os.sigaction(os.SIG.USR1, null, &old_sa);791 try os.sigaction(os.SIG.USR1, null, &old_sa);
788 try testing.expectEqual(os.SIG.DFL, old_sa.handler.handler);792 try testing.expectEqual(os.SIG.DFL, old_sa.handler.handler);
789 // ensure we can ignore a signal793
794 // Reinstall the signal w/o RESETHAND and re-raise
795 sa.flags = os.SA.SIGINFO;
796 try os.sigaction(os.SIG.USR1, &sa, null);
797 try os.raise(os.SIG.USR1);
798 try testing.expect(S.handler_called_count == 2);
799
800 // Now set the signal to ignored
790 sa.handler = .{ .handler = os.SIG.IGN };801 sa.handler = .{ .handler = os.SIG.IGN };
802 sa.flags = 0;
791 try os.sigaction(os.SIG.USR1, &sa, null);803 try os.sigaction(os.SIG.USR1, &sa, null);
792 // and ensure it is now ignored804
805 // Re-raise to ensure handler is actually ignored
793 try os.raise(os.SIG.USR1);806 try os.raise(os.SIG.USR1);
794 try testing.expect(signal_test_handler_called_count == 1);807 try testing.expect(S.handler_called_count == 2);
795 // and that ignored state is returned when querying808
809 // Ensure that ignored state is returned when querying
796 try os.sigaction(os.SIG.USR1, null, &old_sa);810 try os.sigaction(os.SIG.USR1, null, &old_sa);
797 try testing.expectEqual(os.SIG.IGN, old_sa.handler.handler.?);811 try testing.expectEqual(os.SIG.IGN, old_sa.handler.handler.?);
798}812}