Skip to content

Commit d937b86

Browse files
committed
sigset_t: sigemptyset() and sigfillset() are functions that return sigset_t
By returning an initialized sigset (instead of taking the set as an output parameter), these functions can be used to directly initialize the `mask` parameter of a `Sigaction` instance.
1 parent b62cd39 commit d937b86

File tree

11 files changed

+63
-57
lines changed

11 files changed

+63
-57
lines changed

lib/std/Progress.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,11 @@ pub fn start(options: Options) Node {
410410
}
411411

412412
if (have_sigwinch) {
413-
var act: posix.Sigaction = .{
413+
const act: posix.Sigaction = .{
414414
.handler = .{ .sigaction = handleSigWinch },
415-
.mask = undefined,
415+
.mask = posix.sigemptyset(),
416416
.flags = (posix.SA.SIGINFO | posix.SA.RESTART),
417417
};
418-
posix.sigemptyset(&act.mask);
419418
posix.sigaction(posix.SIG.WINCH, &act, null);
420419
}
421420

lib/std/debug.zig

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,13 +1385,11 @@ pub fn attachSegfaultHandler() void {
13851385
windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);
13861386
return;
13871387
}
1388-
var act = posix.Sigaction{
1388+
const act = posix.Sigaction{
13891389
.handler = .{ .sigaction = handleSegfaultPosix },
1390-
.mask = undefined,
1390+
.mask = posix.sigemptyset(),
13911391
.flags = (posix.SA.SIGINFO | posix.SA.RESTART | posix.SA.RESETHAND),
13921392
};
1393-
posix.sigemptyset(&act.mask);
1394-
13951393
updateSegfaultHandler(&act);
13961394
}
13971395

@@ -1403,12 +1401,11 @@ fn resetSegfaultHandler() void {
14031401
}
14041402
return;
14051403
}
1406-
var act = posix.Sigaction{
1404+
const act = posix.Sigaction{
14071405
.handler = .{ .handler = posix.SIG.DFL },
1408-
.mask = undefined,
1406+
.mask = posix.sigemptyset(),
14091407
.flags = 0,
14101408
};
1411-
posix.sigemptyset(&act.mask);
14121409
updateSegfaultHandler(&act);
14131410
}
14141411

lib/std/os/emscripten.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,12 @@ pub const Sigaction = extern struct {
560560
};
561561

562562
pub const sigset_t = [1024 / 32]u32;
563-
pub const empty_sigset = [_]u32{0} ** @typeInfo(sigset_t).array.len;
563+
pub fn sigemptyset() sigset_t {
564+
return [_]u32{0} ** @typeInfo(sigset_t).array.len;
565+
}
566+
pub fn sigfillset() sigset_t {
567+
return [_]u32{~@as(u32, 0)} ** @typeInfo(sigset_t).array.len;
568+
}
564569
pub const siginfo_t = extern struct {
565570
signo: i32,
566571
errno: i32,

lib/std/os/linux.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,11 +1800,15 @@ const SigsetElement = c_ulong;
18001800

18011801
const sigset_len = @typeInfo(sigset_t).array.len;
18021802

1803-
/// Empty set to initialize sigset_t instances from. No need for `sigemptyset`.
1804-
pub const empty_sigset: sigset_t = [_]SigsetElement{0} ** sigset_len;
1803+
/// Zig's version of sigemptyset. Returns initialized sigset_t.
1804+
pub fn sigemptyset() sigset_t {
1805+
return [_]SigsetElement{0} ** sigset_len;
1806+
}
18051807

1806-
/// Filled set to initialize sigset_t instances from. No need for `sigfillset`.
1807-
pub const filled_sigset: sigset_t = [_]SigsetElement{~@as(SigsetElement, 0)} ** sigset_len;
1808+
/// Zig's version of sigfillset. Returns initalized sigset_t.
1809+
pub fn sigfillset() sigset_t {
1810+
return [_]SigsetElement{~@as(SigsetElement, 0)} ** sigset_len;
1811+
}
18081812

18091813
fn sigset_bit_index(sig: usize) struct { word: usize, mask: SigsetElement } {
18101814
assert(sig > 0);

lib/std/os/linux/test.zig

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ test "fadvise" {
128128
test "sigset_t" {
129129
std.debug.assert(@sizeOf(linux.sigset_t) == (linux.NSIG / 8));
130130

131-
var sigset = linux.empty_sigset;
131+
var sigset = linux.sigemptyset();
132132

133133
// See that none are set, then set each one, see that they're all set, then
134134
// remove them all, and then see that none are set.
@@ -140,8 +140,6 @@ test "sigset_t" {
140140
}
141141
for (1..linux.NSIG) |i| {
142142
try expectEqual(linux.sigismember(&sigset, @truncate(i)), true);
143-
try expectEqual(linux.sigismember(&linux.filled_sigset, @truncate(i)), true);
144-
try expectEqual(linux.sigismember(&linux.empty_sigset, @truncate(i)), false);
145143
}
146144
for (1..linux.NSIG) |i| {
147145
linux.sigdelset(&sigset, @truncate(i));
@@ -183,16 +181,16 @@ test "sigset_t" {
183181
}
184182
}
185183

186-
test "filled_sigset" {
184+
test "sigfillset" {
187185
// unlike the C library, all the signals are set in the kernel-level fillset
188-
const sigset = linux.filled_sigset;
186+
const sigset = linux.sigfillset();
189187
for (1..linux.NSIG) |i| {
190188
try expectEqual(linux.sigismember(&sigset, @truncate(i)), true);
191189
}
192190
}
193191

194-
test "empty_sigset" {
195-
const sigset = linux.empty_sigset;
192+
test "sigemptyset" {
193+
const sigset = linux.sigemptyset();
196194
for (1..linux.NSIG) |i| {
197195
try expectEqual(linux.sigismember(&sigset, @truncate(i)), false);
198196
}

lib/std/os/plan9.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ pub const SIG = struct {
182182
pub const TTOU = 20;
183183
};
184184
pub const sigset_t = c_long;
185-
pub const empty_sigset = 0;
186185
pub const siginfo_t = c_long;
187186
// TODO plan9 doesn't have sigaction_fn. Sigaction is not a union, but we include it here to be compatible.
188187
pub const Sigaction = extern struct {
@@ -199,6 +198,12 @@ pub const Sigaction = extern struct {
199198
pub const AT = struct {
200199
pub const FDCWD = -100; // we just make up a constant; FDCWD and openat don't actually exist in plan9
201200
};
201+
pub fn sigemptyset() sigset_t {
202+
return 0;
203+
}
204+
pub fn sigfillset() sigset_t {
205+
return ~@as(sigset_t, 0);
206+
}
202207
// TODO implement sigaction
203208
// right now it is just a shim to allow using start.zig code
204209
pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) usize {

lib/std/posix.zig

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,8 @@ pub fn abort() noreturn {
677677
raise(SIG.ABRT) catch {};
678678

679679
// Disable all signal handlers.
680-
sigprocmask(SIG.BLOCK, &linux.filled_sigset, null);
680+
const filledset = linux.sigfillset();
681+
sigprocmask(SIG.BLOCK, &filledset, null);
681682

682683
// Only one thread may proceed to the rest of abort().
683684
if (!builtin.single_threaded) {
@@ -690,14 +691,14 @@ pub fn abort() noreturn {
690691
// Install default handler so that the tkill below will terminate.
691692
const sigact = Sigaction{
692693
.handler = .{ .handler = SIG.DFL },
693-
.mask = linux.empty_sigset,
694+
.mask = sigemptyset(),
694695
.flags = 0,
695696
};
696697
sigaction(SIG.ABRT, &sigact, null);
697698

698699
_ = linux.tkill(linux.gettid(), SIG.ABRT);
699700

700-
var sigabrtmask = linux.empty_sigset;
701+
var sigabrtmask = sigemptyset();
701702
sigaddset(&sigabrtmask, SIG.ABRT);
702703
sigprocmask(SIG.UNBLOCK, &sigabrtmask, null);
703704

@@ -727,7 +728,7 @@ pub fn raise(sig: u8) RaiseError!void {
727728
// cannot trigger an extra, unexpected, inter-process signal. Signal paranoia inherited from Musl.
728729
const filled = linux.sigfillset();
729730
var orig: sigset_t = undefined;
730-
sigprocmask(SIG.BLOCK, &linux.filled_sigset, &orig);
731+
sigprocmask(SIG.BLOCK, &filled, &orig);
731732
const rc = linux.tkill(linux.gettid(), sig);
732733
sigprocmask(SIG.SETMASK, &orig, null);
733734

@@ -5813,24 +5814,28 @@ pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
58135814
}
58145815
}
58155816

5816-
pub fn sigfillset(set: *sigset_t) void {
5817+
/// Return a filled sigset_t.
5818+
pub fn sigfillset() sigset_t {
58175819
if (builtin.link_libc) {
5818-
switch (errno(system.sigfillset(set))) {
5819-
.SUCCESS => return,
5820+
var set: sigset_t = undefined;
5821+
switch (errno(system.sigfillset(&set))) {
5822+
.SUCCESS => return set,
58205823
else => unreachable,
58215824
}
58225825
}
5823-
set.* = system.filled_sigset;
5826+
return system.sigfillset();
58245827
}
58255828

5826-
pub fn sigemptyset(set: *sigset_t) void {
5829+
/// Return an empty sigset_t.
5830+
pub fn sigemptyset() sigset_t {
58275831
if (builtin.link_libc) {
5828-
switch (errno(system.sigemptyset(set))) {
5829-
.SUCCESS => return,
5832+
var set: sigset_t = undefined;
5833+
switch (errno(system.sigemptyset(&set))) {
5834+
.SUCCESS => return set,
58305835
else => unreachable,
58315836
}
58325837
}
5833-
set.* = mem.zeroes(sigset_t);
5838+
return system.sigemptyset();
58345839
}
58355840

58365841
pub fn sigaddset(set: *sigset_t, sig: u8) void {

lib/std/posix/test.zig

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -863,16 +863,14 @@ test "sigset empty/full" {
863863
if (native_os == .wasi or native_os == .windows)
864864
return error.SkipZigTest;
865865

866-
var set: posix.sigset_t = undefined;
867-
868-
posix.sigemptyset(&set);
866+
var set: posix.sigset_t = posix.sigemptyset();
869867
for (1..posix.NSIG) |i| {
870868
try expectEqual(false, posix.sigismember(&set, @truncate(i)));
871869
}
872870

873871
// The C library can reserve some (unnamed) signals, so can't check the full
874872
// NSIG set is defined, but just test a couple:
875-
posix.sigfillset(&set);
873+
set = posix.sigfillset();
876874
try expectEqual(true, posix.sigismember(&set, @truncate(posix.SIG.USR1)));
877875
try expectEqual(true, posix.sigismember(&set, @truncate(posix.SIG.INT)));
878876
}
@@ -887,8 +885,7 @@ test "sigset add/del" {
887885
if (native_os == .wasi or native_os == .windows)
888886
return error.SkipZigTest;
889887

890-
var sigset: posix.sigset_t = undefined;
891-
posix.sigemptyset(&sigset);
888+
var sigset: posix.sigset_t = posix.sigemptyset();
892889

893890
// See that none are set, then set each one, see that they're all set, then
894891
// remove them all, and then see that none are set.
@@ -944,10 +941,10 @@ test "sigaction" {
944941

945942
var sa: posix.Sigaction = .{
946943
.handler = .{ .sigaction = &S.handler },
947-
.mask = undefined,
944+
.mask = posix.sigemptyset(),
948945
.flags = posix.SA.SIGINFO | posix.SA.RESETHAND,
949946
};
950-
posix.sigemptyset(&sa.mask);
947+
951948
var old_sa: posix.Sigaction = undefined;
952949

953950
// Install the new signal handler.
@@ -1018,20 +1015,19 @@ test "sigset_t bits" {
10181015
S.expected_sig = test_signo;
10191016
S.handler_called_count = 0;
10201017

1021-
var sa: posix.Sigaction = .{
1018+
const sa: posix.Sigaction = .{
10221019
.handler = .{ .sigaction = &S.handler },
1023-
.mask = undefined,
1020+
.mask = posix.sigemptyset(),
10241021
.flags = posix.SA.SIGINFO | posix.SA.RESETHAND,
10251022
};
1026-
posix.sigemptyset(&sa.mask);
1023+
10271024
var old_sa: posix.Sigaction = undefined;
10281025

10291026
// Install the new signal handler.
10301027
posix.sigaction(test_signo, &sa, &old_sa);
10311028

10321029
// block the signal and see that its delayed until unblocked
1033-
var block_one: posix.sigset_t = undefined;
1034-
posix.sigemptyset(&block_one);
1030+
var block_one: posix.sigset_t = posix.sigemptyset();
10351031
posix.sigaddset(&block_one, test_signo);
10361032
posix.sigprocmask(posix.SIG.BLOCK, &block_one, null);
10371033

lib/std/start.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,14 +745,13 @@ fn maybeIgnoreSigpipe() void {
745745

746746
if (have_sigpipe_support and !std.options.keep_sigpipe) {
747747
const posix = std.posix;
748-
var act: posix.Sigaction = .{
748+
const act: posix.Sigaction = .{
749749
// Set handler to a noop function instead of `SIG.IGN` to prevent
750750
// leaking signal disposition to a child process.
751751
.handler = .{ .handler = noopSigHandler },
752-
.mask = undefined,
752+
.mask = posix.sigemptyset(),
753753
.flags = 0,
754754
};
755-
posix.sigemptyset(&act.mask);
756755
posix.sigaction(posix.SIG.PIPE, &act, null);
757756
}
758757
}

src/crash_report.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,11 @@ pub fn attachSegfaultHandler() void {
175175
_ = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);
176176
return;
177177
}
178-
var act: posix.Sigaction = .{
178+
const act: posix.Sigaction = .{
179179
.handler = .{ .sigaction = handleSegfaultPosix },
180-
.mask = undefined,
180+
.mask = posix.sigemptyset(),
181181
.flags = (posix.SA.SIGINFO | posix.SA.RESTART | posix.SA.RESETHAND),
182182
};
183-
posix.sigemptyset(&act.mask);
184183
debug.updateSegfaultHandler(&act);
185184
}
186185

test/standalone/sigpipe/build.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ pub fn build(b: *std.build.Builder) !void {
1616
// This test runs "breakpipe" as a child process and that process
1717
// depends on inheriting a SIGPIPE disposition of "default".
1818
{
19-
var act = posix.Sigaction{
19+
const act = posix.Sigaction{
2020
.handler = .{ .handler = posix.SIG.DFL },
21-
.mask = undefined,
21+
.mask = posix.sigemptyset(),
2222
.flags = 0,
2323
};
24-
posix.sigemptyset(&act.mask);
2524
try posix.sigaction(posix.SIG.PIPE, &act, null);
2625
}
2726

0 commit comments

Comments
 (0)