Skip to content

Commit ad9cb40

Browse files
authored
Merge pull request #23601 from rootbeer/sig-split
Split glibc and linux sigset_t ABIs and the accessor functions
2 parents 971d19a + a55ecd7 commit ad9cb40

22 files changed

+393
-155
lines changed

lib/std/Progress.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,9 @@ 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 = posix.empty_sigset,
415+
.mask = posix.sigemptyset(),
416416
.flags = (posix.SA.SIGINFO | posix.SA.RESTART),
417417
};
418418
posix.sigaction(posix.SIG.WINCH, &act, null);

lib/std/c.zig

+34-19
Original file line numberDiff line numberDiff line change
@@ -3115,6 +3115,21 @@ pub const SYS = switch (native_os) {
31153115
.linux => linux.SYS,
31163116
else => void,
31173117
};
3118+
3119+
/// A common format for the Sigaction struct across a variety of Linux flavors.
3120+
const common_linux_Sigaction = extern struct {
3121+
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
3122+
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
3123+
3124+
handler: extern union {
3125+
handler: ?handler_fn,
3126+
sigaction: ?sigaction_fn,
3127+
},
3128+
mask: sigset_t,
3129+
flags: c_uint,
3130+
restorer: ?*const fn () callconv(.c) void = null, // C library will fill this in
3131+
};
3132+
31183133
/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.
31193134
pub const Sigaction = switch (native_os) {
31203135
.linux => switch (native_arch) {
@@ -3123,7 +3138,7 @@ pub const Sigaction = switch (native_os) {
31233138
.mips64,
31243139
.mips64el,
31253140
=> if (builtin.target.abi.isMusl())
3126-
linux.Sigaction
3141+
common_linux_Sigaction
31273142
else if (builtin.target.ptrBitWidth() == 64) extern struct {
31283143
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
31293144
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
@@ -3160,8 +3175,8 @@ pub const Sigaction = switch (native_os) {
31603175
flags: c_uint,
31613176
restorer: ?*const fn () callconv(.c) void = null,
31623177
mask: sigset_t,
3163-
} else linux.Sigaction,
3164-
else => linux.Sigaction,
3178+
} else common_linux_Sigaction,
3179+
else => common_linux_Sigaction,
31653180
},
31663181
.emscripten => emscripten.Sigaction,
31673182
.netbsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct {
@@ -4518,27 +4533,18 @@ pub const siginfo_t = switch (native_os) {
45184533
else => void,
45194534
};
45204535
pub const sigset_t = switch (native_os) {
4521-
.linux => linux.sigset_t,
4536+
.linux => [1024 / @bitSizeOf(c_ulong)]c_ulong, // glibc and musl present a 1024-bit sigset_t, while kernel's is 128-bit or less.
45224537
.emscripten => emscripten.sigset_t,
45234538
// https://github.com/SerenityOS/serenity/blob/ec492a1a0819e6239ea44156825c4ee7234ca3db/Kernel/API/POSIX/signal.h#L19
4524-
.openbsd, .macos, .ios, .tvos, .watchos, .visionos, .serenity => u32,
4539+
.openbsd, .serenity => u32,
4540+
.macos, .ios, .tvos, .watchos, .visionos => darwin.sigset_t,
45254541
.dragonfly, .netbsd, .solaris, .illumos, .freebsd => extern struct {
45264542
__bits: [SIG.WORDS]u32,
45274543
},
45284544
.haiku => u64,
45294545
else => u0,
45304546
};
4531-
pub const empty_sigset: sigset_t = switch (native_os) {
4532-
.linux => linux.empty_sigset,
4533-
.emscripten => emscripten.empty_sigset,
4534-
.dragonfly, .netbsd, .solaris, .illumos, .freebsd => .{ .__bits = [_]u32{0} ** SIG.WORDS },
4535-
else => 0,
4536-
};
4537-
pub const filled_sigset = switch (native_os) {
4538-
.linux => linux.filled_sigset,
4539-
.haiku => ~@as(sigset_t, 0),
4540-
else => 0,
4541-
};
4547+
45424548
pub const sigval = switch (native_os) {
45434549
.linux => linux.sigval,
45444550
// https://github.com/SerenityOS/serenity/blob/ec492a1a0819e6239ea44156825c4ee7234ca3db/Kernel/API/POSIX/signal.h#L22-L25
@@ -6665,7 +6671,7 @@ pub const timezone = switch (native_os) {
66656671
};
66666672

66676673
pub const ucontext_t = switch (native_os) {
6668-
.linux => linux.ucontext_t,
6674+
.linux => linux.ucontext_t, // std.os.linux.ucontext_t is currently glibc-compatible, but it should probably not be.
66696675
.emscripten => emscripten.ucontext_t,
66706676
.macos, .ios, .tvos, .watchos, .visionos => extern struct {
66716677
onstack: c_int,
@@ -9596,6 +9602,7 @@ pub const NSIG = switch (native_os) {
95969602
.windows => 23,
95979603
.haiku => 65,
95989604
.netbsd, .freebsd => 32,
9605+
.macos => darwin.NSIG,
95999606
.solaris, .illumos => 75,
96009607
// https://github.com/SerenityOS/serenity/blob/046c23f567a17758d762a33bdf04bacbfd088f9f/Kernel/API/POSIX/signal_numbers.h#L42
96019608
.openbsd, .serenity => 33,
@@ -10345,6 +10352,11 @@ pub const sigfillset = switch (native_os) {
1034510352
else => private.sigfillset,
1034610353
};
1034710354

10355+
pub const sigaddset = private.sigaddset;
10356+
pub const sigemptyset = private.sigemptyset;
10357+
pub const sigdelset = private.sigdelset;
10358+
pub const sigismember = private.sigismember;
10359+
1034810360
pub const sigprocmask = switch (native_os) {
1034910361
.netbsd => private.__sigprocmask14,
1035010362
else => private.sigprocmask,
@@ -11025,7 +11037,6 @@ pub const pthread_attr_set_qos_class_np = darwin.pthread_attr_set_qos_class_np;
1102511037
pub const pthread_get_qos_class_np = darwin.pthread_get_qos_class_np;
1102611038
pub const pthread_set_qos_class_self_np = darwin.pthread_set_qos_class_self_np;
1102711039
pub const ptrace = darwin.ptrace;
11028-
pub const sigaddset = darwin.sigaddset;
1102911040
pub const task_for_pid = darwin.task_for_pid;
1103011041
pub const task_get_exception_ports = darwin.task_get_exception_ports;
1103111042
pub const task_info = darwin.task_info;
@@ -11148,7 +11159,11 @@ const private = struct {
1114811159
extern "c" fn sched_yield() c_int;
1114911160
extern "c" fn sendfile(out_fd: fd_t, in_fd: fd_t, offset: ?*off_t, count: usize) isize;
1115011161
extern "c" fn sigaction(sig: c_int, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) c_int;
11151-
extern "c" fn sigfillset(set: ?*sigset_t) void;
11162+
extern "c" fn sigdelset(set: ?*sigset_t, signo: c_int) c_int;
11163+
extern "c" fn sigaddset(set: ?*sigset_t, signo: c_int) c_int;
11164+
extern "c" fn sigfillset(set: ?*sigset_t) c_int;
11165+
extern "c" fn sigemptyset(set: ?*sigset_t) c_int;
11166+
extern "c" fn sigismember(set: ?*const sigset_t, signo: c_int) c_int;
1115211167
extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
1115311168
extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
1115411169
extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;

lib/std/c/darwin.zig

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const mode_t = std.c.mode_t;
1010
const off_t = std.c.off_t;
1111
const pid_t = std.c.pid_t;
1212
const pthread_attr_t = std.c.pthread_attr_t;
13-
const sigset_t = std.c.sigset_t;
1413
const timespec = std.c.timespec;
1514
const sf_hdtr = std.c.sf_hdtr;
1615

@@ -840,9 +839,11 @@ pub extern "c" fn sendfile(
840839
flags: u32,
841840
) c_int;
842841

843-
pub fn sigaddset(set: *sigset_t, signo: u5) void {
844-
set.* |= @as(u32, 1) << (signo - 1);
845-
}
842+
// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/_types.h#L74
843+
pub const sigset_t = u32;
844+
845+
// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/signal.h#L76
846+
pub const NSIG = 32;
846847

847848
pub const qos_class_t = enum(c_uint) {
848849
/// highest priority QOS class for critical tasks

lib/std/debug.zig

+4-5
Original file line numberDiff line numberDiff line change
@@ -1387,12 +1387,11 @@ pub fn attachSegfaultHandler() void {
13871387
windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);
13881388
return;
13891389
}
1390-
var act = posix.Sigaction{
1390+
const act = posix.Sigaction{
13911391
.handler = .{ .sigaction = handleSegfaultPosix },
1392-
.mask = posix.empty_sigset,
1392+
.mask = posix.sigemptyset(),
13931393
.flags = (posix.SA.SIGINFO | posix.SA.RESTART | posix.SA.RESETHAND),
13941394
};
1395-
13961395
updateSegfaultHandler(&act);
13971396
}
13981397

@@ -1404,9 +1403,9 @@ fn resetSegfaultHandler() void {
14041403
}
14051404
return;
14061405
}
1407-
var act = posix.Sigaction{
1406+
const act = posix.Sigaction{
14081407
.handler = .{ .handler = posix.SIG.DFL },
1409-
.mask = posix.empty_sigset,
1408+
.mask = posix.sigemptyset(),
14101409
.flags = 0,
14111410
};
14121411
updateSegfaultHandler(&act);

lib/std/os/emscripten.zig

+3-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,9 @@ 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+
}
564566
pub const siginfo_t = extern struct {
565567
signo: i32,
566568
errno: i32,

0 commit comments

Comments
 (0)