Skip to content

Commit

Permalink
std.os.linux: accept u8 in sigaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihe774 committed Jan 3, 2025
1 parent 46c23d2 commit 78f40d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
23 changes: 10 additions & 13 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1744,24 +1744,21 @@ pub fn sigprocmask(flags: u32, noalias set: ?*const sigset_t, noalias oldset: ?*
return syscall4(.rt_sigprocmask, flags, @intFromPtr(set), @intFromPtr(oldset), NSIG / 8);
}

pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) usize {
assert(sig >= 1);
assert(sig != SIG.KILL);
assert(sig != SIG.STOP);

pub fn sigaction(sig: u8, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) usize {
var ksa: k_sigaction = undefined;
var oldksa: k_sigaction = undefined;
const mask_size = @sizeOf(@TypeOf(ksa.mask));

if (act) |new| {
const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) &restore_rt else &restore;
ksa = k_sigaction{
.handler = new.handler.handler,
.flags = new.flags | SA.RESTORER,
.mask = undefined,
.restorer = @ptrCast(restorer_fn),
};
@memcpy(@as([*]u8, @ptrCast(&ksa.mask))[0..mask_size], @as([*]const u8, @ptrCast(&new.mask)));
ksa.handler = new.handler.handler;
if (ksa.handler == SIG.DFL or ksa.handler == SIG.IGN) {
ksa.flags = new.flags;
} else {
const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) &restore_rt else &restore;
ksa.flags = new.flags | SA.RESTORER;
ksa.restorer = @ptrCast(restorer_fn);
@memcpy(@as([*]u8, @ptrCast(&ksa.mask))[0..mask_size], @as([*]const u8, @ptrCast(&new.mask)));
}
}

const ksa_arg = if (act != null) @intFromPtr(&ksa) else 0;
Expand Down
9 changes: 0 additions & 9 deletions lib/std/process/Child.zig
Original file line number Diff line number Diff line change
Expand Up @@ -571,17 +571,8 @@ fn spawnPosixChildHelper(arg: usize) callconv(.c) u8 {
if (native_os == .linux and child_arg.sigmask != null) {
std.debug.assert(linux.SIG.DFL == null);
for (1..linux.NSIG) |sig| {
if (sig == posix.SIG.KILL or sig == posix.SIG.STOP) {
continue;
}
if (sig > std.math.maxInt(u6)) {
// XXX: We cannot disable all signals.
// sigaction accepts u6, which is too narrow.
break;
}
var old_act: posix.Sigaction = undefined;
const new_act = mem.zeroes(posix.Sigaction);
// Do not use posix.sigaction. It reaches unreachable.
_ = linux.sigaction(@intCast(sig), &new_act, &old_act);
if (old_act.handler.handler == linux.SIG.IGN) {
_ = linux.sigaction(@intCast(sig), &old_act, null);
Expand Down

0 comments on commit 78f40d1

Please sign in to comment.