authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-29 18:03:09-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-12-29 18:03:09-05:00
log6b960331ee1f688d9a6b4159e3aae48fdd0c184d
treef552833cab99e29e81f4c0d40e9682c749122613
parentbda355d976c11a6bf1c820b6f1c2a477acd214fd
parent95c83388e452d83e407fbaaf1431d5a19cfca4a3
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3968 from daurnimator/sigprocmask

Clean up linux sigprocmask, raise

4 files changed, 33 insertions(+), 37 deletions(-)

lib/std/c.zig+1-1
......@@ -101,7 +101,7 @@ pub extern "c" fn dup(fd: fd_t) c_int;
101101pub extern "c" fn dup2(old_fd: fd_t, new_fd: fd_t) c_int;
102102pub extern "c" fn readlink(noalias path: [*:0]const u8, noalias buf: [*]u8, bufsize: usize) isize;
103103pub extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
104pub extern "c" fn sigprocmask(how: c_int, noalias set: *const sigset_t, noalias oset: ?*sigset_t) c_int;
104pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
105105pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
106106pub extern "c" fn sigaction(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int;
107107pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
lib/std/os.zig+9-4
......@@ -225,10 +225,15 @@ pub fn raise(sig: u8) RaiseError!void {
225225
226226 if (builtin.os == .linux) {
227227 var set: linux.sigset_t = undefined;
228 linux.blockAppSignals(&set);
229 const tid = linux.syscall0(linux.SYS_gettid);
230 const rc = linux.syscall2(linux.SYS_tkill, tid, sig);
231 linux.restoreSignals(&set);
228 // block application signals
229 _ = linux.sigprocmask(SIG_BLOCK, &linux.app_mask, &set);
230
231 const tid = linux.gettid();
232 const rc = linux.tkill(tid, sig);
233
234 // restore signal mask
235 _ = linux.sigprocmask(SIG_SETMASK, &set, null);
236
232237 switch (errno(rc)) {
233238 0 => return,
234239 else => |err| return unexpectedErrno(err),
lib/std/os/bits/linux.zig+7-12
......@@ -764,19 +764,14 @@ pub const winsize = extern struct {
764764 ws_ypixel: u16,
765765};
766766
767/// NSIG is the total number of signals defined.
768/// As signal numbers are sequential, NSIG is one greater than the largest defined signal number.
767769pub const NSIG = if (is_mips) 128 else 65;
768pub const sigset_t = [128 / @sizeOf(usize)]usize;
769770
770pub usingnamespace if (NSIG == 65)
771 struct {
772 pub const all_mask = [2]u32{ 0xffffffff, 0xffffffff };
773 pub const app_mask = [2]u32{ 0xfffffffc, 0x7fffffff };
774 }
775else
776 struct {
777 pub const all_mask = [4]u32{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
778 pub const app_mask = [4]u32{ 0xfffffffc, 0x7fffffff, 0xffffffff, 0xffffffff };
779 };
771pub const sigset_t = [1024 / 32]u32;
772
773pub const all_mask: sigset_t = [_]u32{0xffffffff} ** sigset_t.len;
774pub const app_mask: sigset_t = [2]u32{ 0xfffffffc, 0x7fffffff } ++ [_]u32{0xffffffff} ** 30;
780775
781776pub const k_sigaction = if (is_mips)
782777 extern struct {
......@@ -804,7 +799,7 @@ pub const Sigaction = extern struct {
804799pub const SIG_ERR = @intToPtr(extern fn (i32, *siginfo_t, *c_void) void, maxInt(usize));
805800pub const SIG_DFL = @intToPtr(?extern fn (i32, *siginfo_t, *c_void) void, 0);
806801pub const SIG_IGN = @intToPtr(extern fn (i32, *siginfo_t, *c_void) void, 1);
807pub const empty_sigset = [_]usize{0} ** sigset_t.len;
802pub const empty_sigset = [_]u32{0} ** sigset_t.len;
808803
809804pub const in_port_t = u16;
810805pub const sa_family_t = u16;
lib/std/os/linux.zig+16-20
......@@ -464,10 +464,18 @@ pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
464464 return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags);
465465}
466466
467pub fn kill(pid: i32, sig: i32) usize {
467pub fn kill(pid: pid_t, sig: i32) usize {
468468 return syscall2(SYS_kill, @bitCast(usize, @as(isize, pid)), @bitCast(usize, @as(isize, sig)));
469469}
470470
471pub fn tkill(tid: pid_t, sig: i32) usize {
472 return syscall2(SYS_tkill, @bitCast(usize, @as(isize, tid)), @bitCast(usize, @as(isize, sig)));
473}
474
475pub fn tgkill(tgid: pid_t, tid: pid_t, sig: i32) usize {
476 return syscall2(SYS_tgkill, @bitCast(usize, @as(isize, tgid)), @bitCast(usize, @as(isize, tid)), @bitCast(usize, @as(isize, sig)));
477}
478
471479pub fn unlink(path: [*:0]const u8) usize {
472480 if (@hasDecl(@This(), "SYS_unlink")) {
473481 return syscall1(SYS_unlink, @ptrToInt(path));
......@@ -480,7 +488,7 @@ pub fn unlinkat(dirfd: i32, path: [*:0]const u8, flags: u32) usize {
480488 return syscall3(SYS_unlinkat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), flags);
481489}
482490
483pub fn waitpid(pid: i32, status: *u32, flags: u32) usize {
491pub fn waitpid(pid: pid_t, status: *u32, flags: u32) usize {
484492 return syscall4(SYS_wait4, @bitCast(usize, @as(isize, pid)), @ptrToInt(status), flags, 0);
485493}
486494
......@@ -657,15 +665,15 @@ pub fn setgroups(size: usize, list: *const u32) usize {
657665 }
658666}
659667
660pub fn getpid() i32 {
661 return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid)));
668pub fn getpid() pid_t {
669 return @bitCast(pid_t, @truncate(u32, syscall0(SYS_getpid)));
662670}
663671
664pub fn gettid() i32 {
665 return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid)));
672pub fn gettid() pid_t {
673 return @bitCast(pid_t, @truncate(u32, syscall0(SYS_gettid)));
666674}
667675
668pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
676pub fn sigprocmask(flags: u32, noalias set: ?*const sigset_t, noalias oldset: ?*sigset_t) usize {
669677 return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8);
670678}
671679
......@@ -697,18 +705,6 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti
697705 return 0;
698706}
699707
700pub fn blockAllSignals(set: *sigset_t) void {
701 _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8);
702}
703
704pub fn blockAppSignals(set: *sigset_t) void {
705 _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8);
706}
707
708pub fn restoreSignals(set: *sigset_t) void {
709 _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8);
710}
711
712708pub fn sigaddset(set: *sigset_t, sig: u6) void {
713709 const s = sig - 1;
714710 (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1));
......@@ -969,7 +965,7 @@ pub fn sched_yield() usize {
969965 return syscall0(SYS_sched_yield);
970966}
971967
972pub fn sched_getaffinity(pid: i32, size: usize, set: *cpu_set_t) usize {
968pub fn sched_getaffinity(pid: pid_t, size: usize, set: *cpu_set_t) usize {
973969 const rc = syscall3(SYS_sched_getaffinity, @bitCast(usize, @as(isize, pid)), size, @ptrToInt(set));
974970 if (@bitCast(isize, rc) < 0) return rc;
975971 if (rc < size) @memset(@ptrCast([*]u8, set) + rc, 0, size - rc);