Skip to content

Split glibc and linux sigset_t ABIs and the accessor functions #23601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/std/Progress.zig
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,9 @@ pub fn start(options: Options) Node {
}

if (have_sigwinch) {
var act: posix.Sigaction = .{
const act: posix.Sigaction = .{
.handler = .{ .sigaction = handleSigWinch },
.mask = posix.empty_sigset,
.mask = posix.sigemptyset(),
.flags = (posix.SA.SIGINFO | posix.SA.RESTART),
};
posix.sigaction(posix.SIG.WINCH, &act, null);
Expand Down
53 changes: 34 additions & 19 deletions lib/std/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3115,6 +3115,21 @@ pub const SYS = switch (native_os) {
.linux => linux.SYS,
else => void,
};

/// A common format for the Sigaction struct across a variety of Linux flavors.
const common_linux_Sigaction = extern struct {
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;

handler: extern union {
handler: ?handler_fn,
sigaction: ?sigaction_fn,
},
mask: sigset_t,
flags: c_uint,
restorer: ?*const fn () callconv(.c) void = null, // C library will fill this in
};

/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.
pub const Sigaction = switch (native_os) {
.linux => switch (native_arch) {
Expand All @@ -3123,7 +3138,7 @@ pub const Sigaction = switch (native_os) {
.mips64,
.mips64el,
=> if (builtin.target.abi.isMusl())
linux.Sigaction
common_linux_Sigaction
else if (builtin.target.ptrBitWidth() == 64) extern struct {
pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
Expand Down Expand Up @@ -3160,8 +3175,8 @@ pub const Sigaction = switch (native_os) {
flags: c_uint,
restorer: ?*const fn () callconv(.c) void = null,
mask: sigset_t,
} else linux.Sigaction,
else => linux.Sigaction,
} else common_linux_Sigaction,
else => common_linux_Sigaction,
},
.emscripten => emscripten.Sigaction,
.netbsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct {
Expand Down Expand Up @@ -4518,27 +4533,18 @@ pub const siginfo_t = switch (native_os) {
else => void,
};
pub const sigset_t = switch (native_os) {
.linux => linux.sigset_t,
.linux => [1024 / @bitSizeOf(c_ulong)]c_ulong, // glibc and musl present a 1024-bit sigset_t, while kernel's is 128-bit or less.
.emscripten => emscripten.sigset_t,
// https://github.com/SerenityOS/serenity/blob/ec492a1a0819e6239ea44156825c4ee7234ca3db/Kernel/API/POSIX/signal.h#L19
.openbsd, .macos, .ios, .tvos, .watchos, .visionos, .serenity => u32,
.openbsd, .serenity => u32,
.macos, .ios, .tvos, .watchos, .visionos => darwin.sigset_t,
.dragonfly, .netbsd, .solaris, .illumos, .freebsd => extern struct {
__bits: [SIG.WORDS]u32,
},
.haiku => u64,
else => u0,
};
pub const empty_sigset: sigset_t = switch (native_os) {
.linux => linux.empty_sigset,
.emscripten => emscripten.empty_sigset,
.dragonfly, .netbsd, .solaris, .illumos, .freebsd => .{ .__bits = [_]u32{0} ** SIG.WORDS },
else => 0,
};
pub const filled_sigset = switch (native_os) {
.linux => linux.filled_sigset,
.haiku => ~@as(sigset_t, 0),
else => 0,
};

pub const sigval = switch (native_os) {
.linux => linux.sigval,
// https://github.com/SerenityOS/serenity/blob/ec492a1a0819e6239ea44156825c4ee7234ca3db/Kernel/API/POSIX/signal.h#L22-L25
Expand Down Expand Up @@ -6665,7 +6671,7 @@ pub const timezone = switch (native_os) {
};

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

pub const sigaddset = private.sigaddset;
pub const sigemptyset = private.sigemptyset;
pub const sigdelset = private.sigdelset;
pub const sigismember = private.sigismember;

pub const sigprocmask = switch (native_os) {
.netbsd => private.__sigprocmask14,
else => private.sigprocmask,
Expand Down Expand Up @@ -11025,7 +11037,6 @@ pub const pthread_attr_set_qos_class_np = darwin.pthread_attr_set_qos_class_np;
pub const pthread_get_qos_class_np = darwin.pthread_get_qos_class_np;
pub const pthread_set_qos_class_self_np = darwin.pthread_set_qos_class_self_np;
pub const ptrace = darwin.ptrace;
pub const sigaddset = darwin.sigaddset;
pub const task_for_pid = darwin.task_for_pid;
pub const task_get_exception_ports = darwin.task_get_exception_ports;
pub const task_info = darwin.task_info;
Expand Down Expand Up @@ -11148,7 +11159,11 @@ const private = struct {
extern "c" fn sched_yield() c_int;
extern "c" fn sendfile(out_fd: fd_t, in_fd: fd_t, offset: ?*off_t, count: usize) isize;
extern "c" fn sigaction(sig: c_int, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) c_int;
extern "c" fn sigfillset(set: ?*sigset_t) void;
extern "c" fn sigdelset(set: ?*sigset_t, signo: c_int) c_int;
extern "c" fn sigaddset(set: ?*sigset_t, signo: c_int) c_int;
extern "c" fn sigfillset(set: ?*sigset_t) c_int;
extern "c" fn sigemptyset(set: ?*sigset_t) c_int;
extern "c" fn sigismember(set: ?*const sigset_t, signo: c_int) c_int;
extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
Expand Down
9 changes: 5 additions & 4 deletions lib/std/c/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const mode_t = std.c.mode_t;
const off_t = std.c.off_t;
const pid_t = std.c.pid_t;
const pthread_attr_t = std.c.pthread_attr_t;
const sigset_t = std.c.sigset_t;
const timespec = std.c.timespec;
const sf_hdtr = std.c.sf_hdtr;

Expand Down Expand Up @@ -840,9 +839,11 @@ pub extern "c" fn sendfile(
flags: u32,
) c_int;

pub fn sigaddset(set: *sigset_t, signo: u5) void {
set.* |= @as(u32, 1) << (signo - 1);
}
// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/_types.h#L74
pub const sigset_t = u32;

// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/signal.h#L76
pub const NSIG = 32;

pub const qos_class_t = enum(c_uint) {
/// highest priority QOS class for critical tasks
Expand Down
9 changes: 4 additions & 5 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1389,12 +1389,11 @@ pub fn attachSegfaultHandler() void {
windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);
return;
}
var act = posix.Sigaction{
const act = posix.Sigaction{
.handler = .{ .sigaction = handleSegfaultPosix },
.mask = posix.empty_sigset,
.mask = posix.sigemptyset(),
.flags = (posix.SA.SIGINFO | posix.SA.RESTART | posix.SA.RESETHAND),
};

updateSegfaultHandler(&act);
}

Expand All @@ -1406,9 +1405,9 @@ fn resetSegfaultHandler() void {
}
return;
}
var act = posix.Sigaction{
const act = posix.Sigaction{
.handler = .{ .handler = posix.SIG.DFL },
.mask = posix.empty_sigset,
.mask = posix.sigemptyset(),
.flags = 0,
};
updateSegfaultHandler(&act);
Expand Down
4 changes: 3 additions & 1 deletion lib/std/os/emscripten.zig
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,9 @@ pub const Sigaction = extern struct {
};

pub const sigset_t = [1024 / 32]u32;
pub const empty_sigset = [_]u32{0} ** @typeInfo(sigset_t).array.len;
pub fn sigemptyset() sigset_t {
return [_]u32{0} ** @typeInfo(sigset_t).array.len;
}
pub const siginfo_t = extern struct {
signo: i32,
errno: i32,
Expand Down
Loading
Loading