authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-12 16:44:10+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-12 16:44:10+01:00
log629cc6cf285f60581f03b69ec8d012d2e370a029
treec616cc3bdb5096753d4c6981a2be4a2257dd16bb
parent1d9b28403a1074cbbbd0605f07819136c9f96cdc

std: Further siginfo refinements

* Define siginfo and sigaction for Darwin * Define sigaction/handler union for maximum libc compatibility * Minor correction to some type definitions

9 files changed, 91 insertions(+), 41 deletions(-)

lib/std/debug.zig+2-2
...@@ -1721,7 +1721,7 @@ pub fn attachSegfaultHandler() void {...@@ -1721,7 +1721,7 @@ pub fn attachSegfaultHandler() void {
1721 return;1721 return;
1722 }1722 }
1723 var act = os.Sigaction{1723 var act = os.Sigaction{
1724 .sigaction = handleSegfaultLinux,1724 .handler = .{ .sigaction = handleSegfaultLinux },
1725 .mask = os.empty_sigset,1725 .mask = os.empty_sigset,
1726 .flags = (os.SA_SIGINFO | os.SA_RESTART | os.SA_RESETHAND),1726 .flags = (os.SA_SIGINFO | os.SA_RESTART | os.SA_RESETHAND),
1727 };1727 };
...@@ -1740,7 +1740,7 @@ fn resetSegfaultHandler() void {...@@ -1740,7 +1740,7 @@ fn resetSegfaultHandler() void {
1740 return;1740 return;
1741 }1741 }
1742 var act = os.Sigaction{1742 var act = os.Sigaction{
1743 .sigaction = os.SIG_DFL,1743 .handler = .{ .sigaction = os.SIG_DFL },
1744 .mask = os.empty_sigset,1744 .mask = os.empty_sigset,
1745 .flags = 0,1745 .flags = 0,
1746 };1746 };
lib/std/os/bits/darwin.zig+26-4
...@@ -124,13 +124,35 @@ pub const timespec = extern struct {...@@ -124,13 +124,35 @@ pub const timespec = extern struct {
124};124};
125125
126pub const sigset_t = u32;126pub const sigset_t = u32;
127pub const empty_sigset = sigset_t(0);127pub const empty_sigset: sigset_t = 0;
128
129pub const siginfo_t = extern struct {
130 signo: c_int,
131 errno: c_int,
132 code: c_int,
133 pid: pid_t,
134 uid: uid_t,
135 status: c_int,
136 addr: *c_void,
137 value: extern union {
138 int: c_int,
139 ptr: *c_void,
140 },
141 si_band: c_long,
142 _pad: [7]c_ulong,
143};
128144
129/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.145/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.
130pub const Sigaction = extern struct {146pub const Sigaction = extern struct {
131 handler: fn (c_int) callconv(.C) void,147 pub const handler_fn = fn (c_int) callconv(.C) void;
132 sa_mask: sigset_t,148 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void;
133 sa_flags: c_int,149
150 handler: extern union {
151 handler: ?handler_fn,
152 sigaction: ?sigaction_fn,
153 },
154 mask: sigset_t,
155 flags: c_uint,
134};156};
135157
136pub const dirent = extern struct {158pub const dirent = extern struct {
lib/std/os/bits/dragonfly.zig+9-5
...@@ -530,12 +530,16 @@ pub const sigset_t = extern struct {...@@ -530,12 +530,16 @@ pub const sigset_t = extern struct {
530};530};
531pub const sig_atomic_t = c_int;531pub const sig_atomic_t = c_int;
532pub const Sigaction = extern struct {532pub const Sigaction = extern struct {
533 __sigaction_u: extern union {533 pub const handler_fn = fn (c_int) callconv(.C) void;
534 __sa_handler: ?fn (c_int) callconv(.C) void,534 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void;
535 __sa_sigaction: ?fn (c_int, [*c]siginfo_t, ?*c_void) callconv(.C) void,535
536 /// signal handler
537 handler: extern union {
538 handler: ?handler_fn,
539 sigaction: ?sigaction_fn,
536 },540 },
537 sa_flags: c_int,541 flags: c_uint,
538 sa_mask: sigset_t,542 mask: sigset_t,
539};543};
540pub const sig_t = [*c]fn (c_int) callconv(.C) void;544pub const sig_t = [*c]fn (c_int) callconv(.C) void;
541545
lib/std/os/bits/freebsd.zig+7-4
...@@ -742,14 +742,17 @@ pub const SIG_IGN = @intToPtr(fn (i32) callconv(.C) void, 1);...@@ -742,14 +742,17 @@ pub const SIG_IGN = @intToPtr(fn (i32) callconv(.C) void, 1);
742742
743/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.743/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
744pub const Sigaction = extern struct {744pub const Sigaction = extern struct {
745 pub const handler_fn = fn (c_int) callconv(.C) void;
746 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void;
747
745 /// signal handler748 /// signal handler
746 __sigaction_u: extern union {749 handler: extern union {
747 __sa_handler: fn (i32) callconv(.C) void,750 handler: ?handler_fn,
748 __sa_sigaction: fn (i32, *__siginfo, usize) callconv(.C) void,751 sigaction: ?sigaction_fn,
749 },752 },
750753
751 /// see signal options754 /// see signal options
752 sa_flags: u32,755 sa_flags: c_uint,
753756
754 /// signal mask to apply757 /// signal mask to apply
755 sa_mask: sigset_t,758 sa_mask: sigset_t,
lib/std/os/bits/linux.zig+26-15
...@@ -864,27 +864,38 @@ pub const sigset_t = [1024 / 32]u32;...@@ -864,27 +864,38 @@ pub const sigset_t = [1024 / 32]u32;
864pub const all_mask: sigset_t = [_]u32{0xffffffff} ** sigset_t.len;864pub const all_mask: sigset_t = [_]u32{0xffffffff} ** sigset_t.len;
865pub const app_mask: sigset_t = [2]u32{ 0xfffffffc, 0x7fffffff } ++ [_]u32{0xffffffff} ** 30;865pub const app_mask: sigset_t = [2]u32{ 0xfffffffc, 0x7fffffff } ++ [_]u32{0xffffffff} ** 30;
866866
867pub const k_sigaction = if (is_mips)867pub const k_sigaction = switch (builtin.arch) {
868 extern struct {868 .mips, .mipsel => extern struct {
869 flags: usize,869 flags: c_uint,
870 sigaction: ?fn (i32, *const siginfo_t, ?*const c_void) callconv(.C) void,870 handler: ?fn (c_int) callconv(.C) void,
871 mask: [4]u32,871 mask: [4]c_ulong,
872 restorer: fn () callconv(.C) void,872 restorer: fn () callconv(.C) void,
873 }873 },
874else874 .mips64, .mips64el => extern struct {
875 extern struct {875 flags: c_uint,
876 sigaction: ?fn (i32, *const siginfo_t, ?*const c_void) callconv(.C) void,876 handler: ?fn (c_int) callconv(.C) void,
877 flags: usize,877 mask: [2]c_ulong,
878 restorer: fn () callconv(.C) void,878 restorer: fn () callconv(.C) void,
879 mask: [2]u32,879 },
880 };880 else => extern struct {
881 handler: ?fn (c_int) callconv(.C) void,
882 flags: c_ulong,
883 restorer: fn () callconv(.C) void,
884 mask: [2]c_uint,
885 },
886};
881887
882/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.888/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
883pub const Sigaction = extern struct {889pub const Sigaction = extern struct {
884 pub const sigaction_fn = fn (i32, *const siginfo_t, ?*const c_void) callconv(.C) void;890 pub const handler_fn = fn (c_int) callconv(.C) void;
885 sigaction: ?sigaction_fn,891 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void;
892
893 handler: extern union {
894 handler: ?handler_fn,
895 sigaction: ?sigaction_fn,
896 },
886 mask: sigset_t,897 mask: sigset_t,
887 flags: u32,898 flags: c_uint,
888 restorer: ?fn () callconv(.C) void = null,899 restorer: ?fn () callconv(.C) void = null,
889};900};
890901
lib/std/os/bits/netbsd.zig+8-3
...@@ -716,13 +716,18 @@ pub const SIG_IGN = @intToPtr(?Sigaction.sigaction_fn, 1);...@@ -716,13 +716,18 @@ pub const SIG_IGN = @intToPtr(?Sigaction.sigaction_fn, 1);
716716
717/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.717/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
718pub const Sigaction = extern struct {718pub const Sigaction = extern struct {
719 pub const sigaction_fn = fn (i32, *siginfo_t, ?*c_void) callconv(.C) void;719 pub const handler_fn = fn (c_int) callconv(.C) void;
720 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void;
721
720 /// signal handler722 /// signal handler
721 sigaction: ?sigaction_fn,723 handler: extern union {
724 handler: ?handler_fn,
725 sigaction: ?sigaction_fn,
726 },
722 /// signal mask to apply727 /// signal mask to apply
723 mask: sigset_t,728 mask: sigset_t,
724 /// signal options729 /// signal options
725 flags: u32,730 flags: c_uint,
726};731};
727732
728pub const sigval_t = extern union {733pub const sigval_t = extern union {
lib/std/os/bits/openbsd.zig+8-3
...@@ -753,13 +753,18 @@ pub const SIG_IGN = @intToPtr(?Sigaction.sigaction_fn, 1);...@@ -753,13 +753,18 @@ pub const SIG_IGN = @intToPtr(?Sigaction.sigaction_fn, 1);
753753
754/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.754/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
755pub const Sigaction = extern struct {755pub const Sigaction = extern struct {
756 pub const sigaction_fn = fn (c_int, *siginfo_t, ?*c_void) callconv(.C) void;756 pub const handler_fn = fn (c_int) callconv(.C) void;
757 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void;
758
757 /// signal handler759 /// signal handler
758 sigaction: ?sigaction_fn,760 handler: extern union {
761 handler: ?handler_fn,
762 sigaction: ?sigaction_fn,
763 },
759 /// signal mask to apply764 /// signal mask to apply
760 mask: sigset_t,765 mask: sigset_t,
761 /// signal options766 /// signal options
762 flags: c_int,767 flags: c_uint,
763};768};
764769
765pub const sigval = extern union {770pub const sigval = extern union {
lib/std/os/linux.zig+3-3
...@@ -869,7 +869,7 @@ pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigact...@@ -869,7 +869,7 @@ pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigact
869 if (act) |new| {869 if (act) |new| {
870 const restorer_fn = if ((new.flags & SA_SIGINFO) != 0) restore_rt else restore;870 const restorer_fn = if ((new.flags & SA_SIGINFO) != 0) restore_rt else restore;
871 ksa = k_sigaction{871 ksa = k_sigaction{
872 .sigaction = new.sigaction,872 .handler = new.handler.handler,
873 .flags = new.flags | SA_RESTORER,873 .flags = new.flags | SA_RESTORER,
874 .mask = undefined,874 .mask = undefined,
875 .restorer = @ptrCast(fn () callconv(.C) void, restorer_fn),875 .restorer = @ptrCast(fn () callconv(.C) void, restorer_fn),
...@@ -888,8 +888,8 @@ pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigact...@@ -888,8 +888,8 @@ pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigact
888 if (getErrno(result) != 0) return result;888 if (getErrno(result) != 0) return result;
889889
890 if (oact) |old| {890 if (oact) |old| {
891 old.sigaction = oldksa.sigaction;891 old.handler.handler = oldksa.handler;
892 old.flags = @truncate(u32, oldksa.flags);892 old.flags = @truncate(c_uint, oldksa.flags);
893 @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &oldksa.mask), mask_size);893 @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &oldksa.mask), mask_size);
894 }894 }
895895
lib/std/os/test.zig+2-2
...@@ -662,7 +662,7 @@ test "sigaction" {...@@ -662,7 +662,7 @@ test "sigaction" {
662 };662 };
663663
664 var sa = os.Sigaction{664 var sa = os.Sigaction{
665 .sigaction = S.handler,665 .handler = .{ .sigaction = S.handler },
666 .mask = os.empty_sigset,666 .mask = os.empty_sigset,
667 .flags = os.SA_SIGINFO | os.SA_RESETHAND,667 .flags = os.SA_SIGINFO | os.SA_RESETHAND,
668 };668 };
...@@ -671,7 +671,7 @@ test "sigaction" {...@@ -671,7 +671,7 @@ test "sigaction" {
671 os.sigaction(os.SIGUSR1, &sa, null);671 os.sigaction(os.SIGUSR1, &sa, null);
672 // Check that we can read it back correctly.672 // Check that we can read it back correctly.
673 os.sigaction(os.SIGUSR1, null, &old_sa);673 os.sigaction(os.SIGUSR1, null, &old_sa);
674 testing.expectEqual(S.handler, old_sa.sigaction.?);674 testing.expectEqual(S.handler, old_sa.handler.sigaction.?);
675 testing.expect((old_sa.flags & os.SA_RESETHAND) != 0);675 testing.expect((old_sa.flags & os.SA_RESETHAND) != 0);
676 // Invoke the handler.676 // Invoke the handler.
677 try os.raise(os.SIGUSR1);677 try os.raise(os.SIGUSR1);