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 {
17211721 return;
17221722 }
17231723 var act = os.Sigaction{
1724 .sigaction = handleSegfaultLinux,
1724 .handler = .{ .sigaction = handleSegfaultLinux },
17251725 .mask = os.empty_sigset,
17261726 .flags = (os.SA_SIGINFO | os.SA_RESTART | os.SA_RESETHAND),
17271727 };
......@@ -1740,7 +1740,7 @@ fn resetSegfaultHandler() void {
17401740 return;
17411741 }
17421742 var act = os.Sigaction{
1743 .sigaction = os.SIG_DFL,
1743 .handler = .{ .sigaction = os.SIG_DFL },
17441744 .mask = os.empty_sigset,
17451745 .flags = 0,
17461746 };
lib/std/os/bits/darwin.zig+26-4
......@@ -124,13 +124,35 @@ pub const timespec = extern struct {
124124};
125125
126126pub 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
129145/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.
130146pub const Sigaction = extern struct {
131 handler: fn (c_int) callconv(.C) void,
132 sa_mask: sigset_t,
133 sa_flags: c_int,
147 pub const handler_fn = fn (c_int) callconv(.C) void;
148 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void;
149
150 handler: extern union {
151 handler: ?handler_fn,
152 sigaction: ?sigaction_fn,
153 },
154 mask: sigset_t,
155 flags: c_uint,
134156};
135157
136158pub const dirent = extern struct {
lib/std/os/bits/dragonfly.zig+9-5
......@@ -530,12 +530,16 @@ pub const sigset_t = extern struct {
530530};
531531pub const sig_atomic_t = c_int;
532532pub const Sigaction = extern struct {
533 __sigaction_u: extern union {
534 __sa_handler: ?fn (c_int) callconv(.C) void,
535 __sa_sigaction: ?fn (c_int, [*c]siginfo_t, ?*c_void) callconv(.C) void,
533 pub const handler_fn = fn (c_int) callconv(.C) void;
534 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void;
535
536 /// signal handler
537 handler: extern union {
538 handler: ?handler_fn,
539 sigaction: ?sigaction_fn,
536540 },
537 sa_flags: c_int,
538 sa_mask: sigset_t,
541 flags: c_uint,
542 mask: sigset_t,
539543};
540544pub 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);
742742
743743/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
744744pub 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
745748 /// signal handler
746 __sigaction_u: extern union {
747 __sa_handler: fn (i32) callconv(.C) void,
748 __sa_sigaction: fn (i32, *__siginfo, usize) callconv(.C) void,
749 handler: extern union {
750 handler: ?handler_fn,
751 sigaction: ?sigaction_fn,
749752 },
750753
751754 /// see signal options
752 sa_flags: u32,
755 sa_flags: c_uint,
753756
754757 /// signal mask to apply
755758 sa_mask: sigset_t,
lib/std/os/bits/linux.zig+26-15
......@@ -864,27 +864,38 @@ pub const sigset_t = [1024 / 32]u32;
864864pub const all_mask: sigset_t = [_]u32{0xffffffff} ** sigset_t.len;
865865pub const app_mask: sigset_t = [2]u32{ 0xfffffffc, 0x7fffffff } ++ [_]u32{0xffffffff} ** 30;
866866
867pub const k_sigaction = if (is_mips)
868 extern struct {
869 flags: usize,
870 sigaction: ?fn (i32, *const siginfo_t, ?*const c_void) callconv(.C) void,
871 mask: [4]u32,
867pub const k_sigaction = switch (builtin.arch) {
868 .mips, .mipsel => extern struct {
869 flags: c_uint,
870 handler: ?fn (c_int) callconv(.C) void,
871 mask: [4]c_ulong,
872872 restorer: fn () callconv(.C) void,
873 }
874else
875 extern struct {
876 sigaction: ?fn (i32, *const siginfo_t, ?*const c_void) callconv(.C) void,
877 flags: usize,
873 },
874 .mips64, .mips64el => extern struct {
875 flags: c_uint,
876 handler: ?fn (c_int) callconv(.C) void,
877 mask: [2]c_ulong,
878878 restorer: fn () callconv(.C) void,
879 mask: [2]u32,
880 };
879 },
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
882888/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
883889pub const Sigaction = extern struct {
884 pub const sigaction_fn = fn (i32, *const siginfo_t, ?*const c_void) callconv(.C) void;
885 sigaction: ?sigaction_fn,
890 pub const handler_fn = fn (c_int) callconv(.C) void;
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 },
886897 mask: sigset_t,
887 flags: u32,
898 flags: c_uint,
888899 restorer: ?fn () callconv(.C) void = null,
889900};
890901
lib/std/os/bits/netbsd.zig+8-3
......@@ -716,13 +716,18 @@ pub const SIG_IGN = @intToPtr(?Sigaction.sigaction_fn, 1);
716716
717717/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
718718pub 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
720722 /// signal handler
721 sigaction: ?sigaction_fn,
723 handler: extern union {
724 handler: ?handler_fn,
725 sigaction: ?sigaction_fn,
726 },
722727 /// signal mask to apply
723728 mask: sigset_t,
724729 /// signal options
725 flags: u32,
730 flags: c_uint,
726731};
727732
728733pub 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);
753753
754754/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
755755pub 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
757759 /// signal handler
758 sigaction: ?sigaction_fn,
760 handler: extern union {
761 handler: ?handler_fn,
762 sigaction: ?sigaction_fn,
763 },
759764 /// signal mask to apply
760765 mask: sigset_t,
761766 /// signal options
762 flags: c_int,
767 flags: c_uint,
763768};
764769
765770pub 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
869869 if (act) |new| {
870870 const restorer_fn = if ((new.flags & SA_SIGINFO) != 0) restore_rt else restore;
871871 ksa = k_sigaction{
872 .sigaction = new.sigaction,
872 .handler = new.handler.handler,
873873 .flags = new.flags | SA_RESTORER,
874874 .mask = undefined,
875875 .restorer = @ptrCast(fn () callconv(.C) void, restorer_fn),
......@@ -888,8 +888,8 @@ pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigact
888888 if (getErrno(result) != 0) return result;
889889
890890 if (oact) |old| {
891 old.sigaction = oldksa.sigaction;
892 old.flags = @truncate(u32, oldksa.flags);
891 old.handler.handler = oldksa.handler;
892 old.flags = @truncate(c_uint, oldksa.flags);
893893 @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &oldksa.mask), mask_size);
894894 }
895895
lib/std/os/test.zig+2-2
......@@ -662,7 +662,7 @@ test "sigaction" {
662662 };
663663
664664 var sa = os.Sigaction{
665 .sigaction = S.handler,
665 .handler = .{ .sigaction = S.handler },
666666 .mask = os.empty_sigset,
667667 .flags = os.SA_SIGINFO | os.SA_RESETHAND,
668668 };
......@@ -671,7 +671,7 @@ test "sigaction" {
671671 os.sigaction(os.SIGUSR1, &sa, null);
672672 // Check that we can read it back correctly.
673673 os.sigaction(os.SIGUSR1, null, &old_sa);
674 testing.expectEqual(S.handler, old_sa.sigaction.?);
674 testing.expectEqual(S.handler, old_sa.handler.sigaction.?);
675675 testing.expect((old_sa.flags & os.SA_RESETHAND) != 0);
676676 // Invoke the handler.
677677 try os.raise(os.SIGUSR1);