| ... | @@ -739,8 +739,6 @@ test "shutdown socket" { | ... | @@ -739,8 +739,6 @@ test "shutdown socket" { |
| 739 | os.closeSocket(sock); | 739 | os.closeSocket(sock); |
| 740 | } | 740 | } |
| 741 | | 741 | |
| 742 | var signal_test_handler_called_count: u32 = 0; | | |
| 743 | | | |
| 744 | test "sigaction" { | 742 | test "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; |
| 751 | | 749 | |
| 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_DFL | 789 | |
| | 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 signal | 793 | |
| | 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 ignored | 804 | |
| | 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 querying | 808 | |
| | 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 | } |