Skip to content

Commit 2cced89

Browse files
committed
std.posix: Consider invalid signal numbers to sigaction() to be programmer error.
The set of signals that cannot have their action changed is documented in POSIX, and any additional, non-standard signals are documented by the specific OS. I see no valid reason why EINVAL should be considered an unpredictable error here.
1 parent 4d2868f commit 2cced89

File tree

6 files changed

+22
-29
lines changed

6 files changed

+22
-29
lines changed

lib/std/Progress.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,7 @@ pub fn start(options: Options) Node {
414414
.mask = posix.empty_sigset,
415415
.flags = (posix.SA.SIGINFO | posix.SA.RESTART),
416416
};
417-
posix.sigaction(posix.SIG.WINCH, &act, null) catch |err| {
418-
std.log.warn("failed to install SIGWINCH signal handler for noticing terminal resizes: {s}", .{@errorName(err)});
419-
};
417+
posix.sigaction(posix.SIG.WINCH, &act, null);
420418
}
421419

422420
if (switch (global_progress.terminal_mode) {

lib/std/debug.zig

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,11 +2601,11 @@ pub fn maybeEnableSegfaultHandler() void {
26012601

26022602
var windows_segfault_handle: ?windows.HANDLE = null;
26032603

2604-
pub fn updateSegfaultHandler(act: ?*const posix.Sigaction) error{OperationNotSupported}!void {
2605-
try posix.sigaction(posix.SIG.SEGV, act, null);
2606-
try posix.sigaction(posix.SIG.ILL, act, null);
2607-
try posix.sigaction(posix.SIG.BUS, act, null);
2608-
try posix.sigaction(posix.SIG.FPE, act, null);
2604+
pub fn updateSegfaultHandler(act: ?*const posix.Sigaction) void {
2605+
posix.sigaction(posix.SIG.SEGV, act, null);
2606+
posix.sigaction(posix.SIG.ILL, act, null);
2607+
posix.sigaction(posix.SIG.BUS, act, null);
2608+
posix.sigaction(posix.SIG.FPE, act, null);
26092609
}
26102610

26112611
/// Attaches a global SIGSEGV handler which calls `@panic("segmentation fault");`
@@ -2623,9 +2623,7 @@ pub fn attachSegfaultHandler() void {
26232623
.flags = (posix.SA.SIGINFO | posix.SA.RESTART | posix.SA.RESETHAND),
26242624
};
26252625

2626-
updateSegfaultHandler(&act) catch {
2627-
@panic("unable to install segfault handler, maybe adjust have_segfault_handling_support in std/debug.zig");
2628-
};
2626+
updateSegfaultHandler(&act);
26292627
}
26302628

26312629
fn resetSegfaultHandler() void {
@@ -2641,8 +2639,7 @@ fn resetSegfaultHandler() void {
26412639
.mask = posix.empty_sigset,
26422640
.flags = 0,
26432641
};
2644-
// To avoid a double-panic, do nothing if an error happens here.
2645-
updateSegfaultHandler(&act) catch {};
2642+
updateSegfaultHandler(&act);
26462643
}
26472644

26482645
fn handleSegfaultPosix(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*anyopaque) callconv(.C) noreturn {

lib/std/posix.zig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,7 @@ pub fn abort() noreturn {
683683
.mask = empty_sigset,
684684
.flags = 0,
685685
};
686-
sigaction(SIG.ABRT, &sigact, null) catch |err| switch (err) {
687-
error.OperationNotSupported => unreachable,
688-
};
686+
sigaction(SIG.ABRT, &sigact, null);
689687

690688
_ = linux.tkill(linux.gettid(), SIG.ABRT);
691689

@@ -5658,10 +5656,13 @@ pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
56585656
}
56595657

56605658
/// Examine and change a signal action.
5661-
pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) error{OperationNotSupported}!void {
5659+
pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) void {
56625660
switch (errno(system.sigaction(sig, act, oact))) {
56635661
.SUCCESS => return,
5664-
.INVAL => return error.OperationNotSupported,
5662+
// EINVAL means the signal is either invalid or some signal that cannot have its action
5663+
// changed. For POSIX, this means SIGKILL/SIGSTOP. For e.g. Solaris, this also includes the
5664+
// non-standard SIGWAITING, SIGCANCEL, and SIGLWP. Either way, programmer error.
5665+
.INVAL => unreachable,
56655666
else => unreachable,
56665667
}
56675668
}

lib/std/posix/test.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,10 @@ test "sigaction" {
862862
var old_sa: posix.Sigaction = undefined;
863863

864864
// Install the new signal handler.
865-
try posix.sigaction(posix.SIG.USR1, &sa, null);
865+
posix.sigaction(posix.SIG.USR1, &sa, null);
866866

867867
// Check that we can read it back correctly.
868-
try posix.sigaction(posix.SIG.USR1, null, &old_sa);
868+
posix.sigaction(posix.SIG.USR1, null, &old_sa);
869869
try testing.expectEqual(&S.handler, old_sa.handler.sigaction.?);
870870
try testing.expect((old_sa.flags & posix.SA.SIGINFO) != 0);
871871

@@ -874,26 +874,26 @@ test "sigaction" {
874874
try testing.expect(S.handler_called_count == 1);
875875

876876
// Check if passing RESETHAND correctly reset the handler to SIG_DFL
877-
try posix.sigaction(posix.SIG.USR1, null, &old_sa);
877+
posix.sigaction(posix.SIG.USR1, null, &old_sa);
878878
try testing.expectEqual(posix.SIG.DFL, old_sa.handler.handler);
879879

880880
// Reinstall the signal w/o RESETHAND and re-raise
881881
sa.flags = posix.SA.SIGINFO;
882-
try posix.sigaction(posix.SIG.USR1, &sa, null);
882+
posix.sigaction(posix.SIG.USR1, &sa, null);
883883
try posix.raise(posix.SIG.USR1);
884884
try testing.expect(S.handler_called_count == 2);
885885

886886
// Now set the signal to ignored
887887
sa.handler = .{ .handler = posix.SIG.IGN };
888888
sa.flags = 0;
889-
try posix.sigaction(posix.SIG.USR1, &sa, null);
889+
posix.sigaction(posix.SIG.USR1, &sa, null);
890890

891891
// Re-raise to ensure handler is actually ignored
892892
try posix.raise(posix.SIG.USR1);
893893
try testing.expect(S.handler_called_count == 2);
894894

895895
// Ensure that ignored state is returned when querying
896-
try posix.sigaction(posix.SIG.USR1, null, &old_sa);
896+
posix.sigaction(posix.SIG.USR1, null, &old_sa);
897897
try testing.expectEqual(posix.SIG.IGN, old_sa.handler.handler.?);
898898
}
899899

lib/std/start.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,7 @@ fn maybeIgnoreSigpipe() void {
609609
.mask = posix.empty_sigset,
610610
.flags = 0,
611611
};
612-
posix.sigaction(posix.SIG.PIPE, &act, null) catch |err|
613-
std.debug.panic("failed to set noop SIGPIPE handler: {s}", .{@errorName(err)});
612+
posix.sigaction(posix.SIG.PIPE, &act, null);
614613
}
615614
}
616615

src/crash_report.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ pub fn attachSegfaultHandler() void {
163163
.flags = (posix.SA.SIGINFO | posix.SA.RESTART | posix.SA.RESETHAND),
164164
};
165165

166-
debug.updateSegfaultHandler(&act) catch {
167-
@panic("unable to install segfault handler, maybe adjust have_segfault_handling_support in std/debug.zig");
168-
};
166+
debug.updateSegfaultHandler(&act);
169167
}
170168

171169
fn handleSegfaultPosix(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*anyopaque) callconv(.C) noreturn {

0 commit comments

Comments
 (0)