authorgravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2025-04-13 15:54:56-07:00
committergravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2025-04-30 20:32:04-07:00
log51654aea877d1da92f7f2ad2c6c781c73e08d7f0
tree698a62b9d91f1d8105bff5fa2c97498639f81323
parent9f7c8b8b1bafe91bf5035c06f72de967a92f6a6c

c.zig: glibc/musl export 1024-bit sigset_t

Export the sigset_t ops (sigaddset, etc) from the C library. Don't rely on the linux.zig defintions (which will be defined to use the kernel ABI). Move Darwin sigset and NSIG declarations into darwin.zig. Remove extraneous (?) sigaddset. The C library sigaddset can reject some signals being added, so need to defer to it.

2 files changed, 39 insertions(+), 23 deletions(-)

lib/std/c.zig+34-19
...@@ -3115,6 +3115,21 @@ pub const SYS = switch (native_os) {...@@ -3115,6 +3115,21 @@ pub const SYS = switch (native_os) {
3115 .linux => linux.SYS,3115 .linux => linux.SYS,
3116 else => void,3116 else => void,
3117};3117};
3118
3119/// A common format for the Sigaction struct across a variety of Linux flavors.
3120const 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
3118/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.3133/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.
3119pub const Sigaction = switch (native_os) {3134pub const Sigaction = switch (native_os) {
3120 .linux => switch (native_arch) {3135 .linux => switch (native_arch) {
...@@ -3123,7 +3138,7 @@ pub const Sigaction = switch (native_os) {...@@ -3123,7 +3138,7 @@ pub const Sigaction = switch (native_os) {
3123 .mips64,3138 .mips64,
3124 .mips64el,3139 .mips64el,
3125 => if (builtin.target.abi.isMusl())3140 => if (builtin.target.abi.isMusl())
3126 linux.Sigaction3141 common_linux_Sigaction
3127 else if (builtin.target.ptrBitWidth() == 64) extern struct {3142 else if (builtin.target.ptrBitWidth() == 64) extern struct {
3128 pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;3143 pub const handler_fn = *align(1) const fn (i32) callconv(.c) void;
3129 pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;3144 pub const sigaction_fn = *const fn (i32, *const siginfo_t, ?*anyopaque) callconv(.c) void;
...@@ -3160,8 +3175,8 @@ pub const Sigaction = switch (native_os) {...@@ -3160,8 +3175,8 @@ pub const Sigaction = switch (native_os) {
3160 flags: c_uint,3175 flags: c_uint,
3161 restorer: ?*const fn () callconv(.c) void = null,3176 restorer: ?*const fn () callconv(.c) void = null,
3162 mask: sigset_t,3177 mask: sigset_t,
3163 } else linux.Sigaction,3178 } else common_linux_Sigaction,
3164 else => linux.Sigaction,3179 else => common_linux_Sigaction,
3165 },3180 },
3166 .emscripten => emscripten.Sigaction,3181 .emscripten => emscripten.Sigaction,
3167 .netbsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct {3182 .netbsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct {
...@@ -4518,27 +4533,18 @@ pub const siginfo_t = switch (native_os) {...@@ -4518,27 +4533,18 @@ pub const siginfo_t = switch (native_os) {
4518 else => void,4533 else => void,
4519};4534};
4520pub const sigset_t = switch (native_os) {4535pub 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.
4522 .emscripten => emscripten.sigset_t,4537 .emscripten => emscripten.sigset_t,
4523 // https://github.com/SerenityOS/serenity/blob/ec492a1a0819e6239ea44156825c4ee7234ca3db/Kernel/API/POSIX/signal.h#L194538 // 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,
4525 .dragonfly, .netbsd, .solaris, .illumos, .freebsd => extern struct {4541 .dragonfly, .netbsd, .solaris, .illumos, .freebsd => extern struct {
4526 __bits: [SIG.WORDS]u32,4542 __bits: [SIG.WORDS]u32,
4527 },4543 },
4528 .haiku => u64,4544 .haiku => u64,
4529 else => u0,4545 else => u0,
4530};4546};
4531pub const empty_sigset: sigset_t = switch (native_os) {4547
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};
4537pub const filled_sigset = switch (native_os) {
4538 .linux => linux.filled_sigset,
4539 .haiku => ~@as(sigset_t, 0),
4540 else => 0,
4541};
4542pub const sigval = switch (native_os) {4548pub const sigval = switch (native_os) {
4543 .linux => linux.sigval,4549 .linux => linux.sigval,
4544 // https://github.com/SerenityOS/serenity/blob/ec492a1a0819e6239ea44156825c4ee7234ca3db/Kernel/API/POSIX/signal.h#L22-L254550 // https://github.com/SerenityOS/serenity/blob/ec492a1a0819e6239ea44156825c4ee7234ca3db/Kernel/API/POSIX/signal.h#L22-L25
...@@ -6665,7 +6671,7 @@ pub const timezone = switch (native_os) {...@@ -6665,7 +6671,7 @@ pub const timezone = switch (native_os) {
6665};6671};
66666672
6667pub const ucontext_t = switch (native_os) {6673pub 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.
6669 .emscripten => emscripten.ucontext_t,6675 .emscripten => emscripten.ucontext_t,
6670 .macos, .ios, .tvos, .watchos, .visionos => extern struct {6676 .macos, .ios, .tvos, .watchos, .visionos => extern struct {
6671 onstack: c_int,6677 onstack: c_int,
...@@ -9596,6 +9602,7 @@ pub const NSIG = switch (native_os) {...@@ -9596,6 +9602,7 @@ pub const NSIG = switch (native_os) {
9596 .windows => 23,9602 .windows => 23,
9597 .haiku => 65,9603 .haiku => 65,
9598 .netbsd, .freebsd => 32,9604 .netbsd, .freebsd => 32,
9605 .macos => darwin.NSIG,
9599 .solaris, .illumos => 75,9606 .solaris, .illumos => 75,
9600 // https://github.com/SerenityOS/serenity/blob/046c23f567a17758d762a33bdf04bacbfd088f9f/Kernel/API/POSIX/signal_numbers.h#L429607 // https://github.com/SerenityOS/serenity/blob/046c23f567a17758d762a33bdf04bacbfd088f9f/Kernel/API/POSIX/signal_numbers.h#L42
9601 .openbsd, .serenity => 33,9608 .openbsd, .serenity => 33,
...@@ -10345,6 +10352,11 @@ pub const sigfillset = switch (native_os) {...@@ -10345,6 +10352,11 @@ pub const sigfillset = switch (native_os) {
10345 else => private.sigfillset,10352 else => private.sigfillset,
10346};10353};
1034710354
10355pub const sigaddset = private.sigaddset;
10356pub const sigemptyset = private.sigemptyset;
10357pub const sigdelset = private.sigdelset;
10358pub const sigismember = private.sigismember;
10359
10348pub const sigprocmask = switch (native_os) {10360pub const sigprocmask = switch (native_os) {
10349 .netbsd => private.__sigprocmask14,10361 .netbsd => private.__sigprocmask14,
10350 else => private.sigprocmask,10362 else => private.sigprocmask,
...@@ -11025,7 +11037,6 @@ pub const pthread_attr_set_qos_class_np = darwin.pthread_attr_set_qos_class_np;...@@ -11025,7 +11037,6 @@ pub const pthread_attr_set_qos_class_np = darwin.pthread_attr_set_qos_class_np;
11025pub const pthread_get_qos_class_np = darwin.pthread_get_qos_class_np;11037pub const pthread_get_qos_class_np = darwin.pthread_get_qos_class_np;
11026pub const pthread_set_qos_class_self_np = darwin.pthread_set_qos_class_self_np;11038pub const pthread_set_qos_class_self_np = darwin.pthread_set_qos_class_self_np;
11027pub const ptrace = darwin.ptrace;11039pub const ptrace = darwin.ptrace;
11028pub const sigaddset = darwin.sigaddset;
11029pub const task_for_pid = darwin.task_for_pid;11040pub const task_for_pid = darwin.task_for_pid;
11030pub const task_get_exception_ports = darwin.task_get_exception_ports;11041pub const task_get_exception_ports = darwin.task_get_exception_ports;
11031pub const task_info = darwin.task_info;11042pub const task_info = darwin.task_info;
...@@ -11148,7 +11159,11 @@ const private = struct {...@@ -11148,7 +11159,11 @@ const private = struct {
11148 extern "c" fn sched_yield() c_int;11159 extern "c" fn sched_yield() c_int;
11149 extern "c" fn sendfile(out_fd: fd_t, in_fd: fd_t, offset: ?*off_t, count: usize) isize;11160 extern "c" fn sendfile(out_fd: fd_t, in_fd: fd_t, offset: ?*off_t, count: usize) isize;
11150 extern "c" fn sigaction(sig: c_int, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) c_int;11161 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;
11152 extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;11167 extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
11153 extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;11168 extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
11154 extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;11169 extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
lib/std/c/darwin.zig+5-4
...@@ -10,7 +10,6 @@ const mode_t = std.c.mode_t;...@@ -10,7 +10,6 @@ const mode_t = std.c.mode_t;
10const off_t = std.c.off_t;10const off_t = std.c.off_t;
11const pid_t = std.c.pid_t;11const pid_t = std.c.pid_t;
12const pthread_attr_t = std.c.pthread_attr_t;12const pthread_attr_t = std.c.pthread_attr_t;
13const sigset_t = std.c.sigset_t;
14const timespec = std.c.timespec;13const timespec = std.c.timespec;
15const sf_hdtr = std.c.sf_hdtr;14const sf_hdtr = std.c.sf_hdtr;
1615
...@@ -840,9 +839,11 @@ pub extern "c" fn sendfile(...@@ -840,9 +839,11 @@ pub extern "c" fn sendfile(
840 flags: u32,839 flags: u32,
841) c_int;840) c_int;
842841
843pub fn sigaddset(set: *sigset_t, signo: u5) void {842// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/_types.h#L74
844 set.* |= @as(u32, 1) << (signo - 1);843pub const sigset_t = u32;
845}844
845// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/signal.h#L76
846pub const NSIG = 32;
846847
847pub const qos_class_t = enum(c_uint) {848pub const qos_class_t = enum(c_uint) {
848 /// highest priority QOS class for critical tasks849 /// highest priority QOS class for critical tasks